source: roaraudio/roard/roard.c @ 5567:6ecf012d7063

Last change on this file since 5567:6ecf012d7063 was 5567:6ecf012d7063, checked in by phi, 12 years ago

roard now tries to auto load missing protocols as plugins (Closes: #275)

File size: 77.8 KB
Line 
1//roard.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26/* ckport options:
27 * Stuff checked by configure:
28 * ckport: ignore-symbol: getuid of target win32
29 * ckport: ignore-symbol: setuid of target win32
30 * ckport: ignore-symbol: getgid of target win32
31 * ckport: ignore-symbol: setgid of target win32
32 * ckport: ignore-symbol: setsid of target win32
33 * ckport: ignore-symbol: kill of target win32
34 * ckport: ignore-symbol: chroot of target win32
35 * ckport: ignore-symbol: fork of target win32
36 * ckport: ignore-symbol: getservbyname of target win32
37 * ckport: ignore-symbol: nice of target win32
38 *
39 */
40
41#include "roard.h"
42
43enum action {
44 START,
45 STOP,
46 RESTART,
47 RESTART_RETRY,
48 SHUTDOWN
49};
50
51enum af_mode {
52 AF_MODE_NONE,
53 AF_MODE_LOAD,
54 AF_MODE_GEN
55};
56
57// some stuff we only define extern globally.
58int g_verbose = ROAR_DBG_INFO_NONE;
59
60int alive;
61#ifdef ROAR_SUPPORT_LISTEN
62int g_no_listen;
63#endif
64
65uint32_t g_pos; // current position in output stream
66
67int g_standby;
68int g_autostandby;
69
70struct roard_listen g_listen[ROAR_MAX_LISTEN_SOCKETS];
71
72int g_self_client;
73
74int g_terminate;
75
76struct roar_audio_info * g_sa, * g_max_sa;
77
78struct roard_config * g_config;
79
80struct counters g_counters;
81// end of extern block.
82
83#ifdef ROAR_SUPPORT_LISTEN
84static char * server[ROAR_MAX_LISTEN_SOCKETS];
85#endif
86
87#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_FS_POSIX)
88#define SUPPORT_PIDFILE
89static char * pidfile = NULL;
90#endif
91
92#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
93static int    setids    = 0;
94#endif
95
96#ifdef ROAR_HAVE_LIBX11
97static char * x11display = NULL;
98#endif
99
100void dbg_notify_cb(struct roar_notify_core * core, struct roar_event * event, void * userdata) {
101 char buf[1024] = "";
102 char estr[1024] = "/* ROAR_??? */";
103 char ttstr[1024] = "/* ROAR_OT_??? */";
104 const char * ttname;
105 uint32_t ev = ROAR_EVENT_GET_TYPE(event);
106 int i;
107
108 ttname = roar_ot2str(event->target_type);
109 if ( ttname != NULL ) {
110  snprintf(ttstr, sizeof(ttstr)-1, "/* ROAR_OT_%s */", ttname);
111  buf[sizeof(ttstr)-1] = 0;
112  for (i = 0; ttstr[i] != 0; i++)
113   if ( islower(ttstr[i]) )
114    ttstr[i] = toupper(ttstr[i]);
115 }
116
117 if ( ev == ROAR_NOTIFY_SPECIAL ) {
118  snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_SPECIAL */");
119 } else if ( ROAR_NOTIFY_IS_CMD(ev) ) {
120  if ( command_get_name(ROAR_NOTIFY_EVENT2CMD(ev), &ttname) == -1 ) {
121   snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_CMD2EVENT(%lu) */", (long unsigned int)ROAR_NOTIFY_EVENT2CMD(ev));
122  } else {
123   snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_CMD2EVENT(ROAR_CMD_%s) */", ttname);
124   for (i = 0; estr[i] != 0; i++)
125    if ( islower(estr[i]) )
126     estr[i] = toupper(estr[i]);
127  }
128 } else if ( ROAR_NOTIFY_IS_EGRP(ev) ) {
129  snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_EGRP2EVENT(%lu) */", (long unsigned int)ROAR_NOTIFY_EVENT2EGRP(ev));
130 } else if ( ROAR_NOTIFY_IS_OE(ev) ) {
131  switch (ev) {
132   // OE basics:
133   case ROAR_OE_BASICS_CHANGE_STATE:
134     snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_BASICS_CHANGE_STATE */");
135    break;
136   case ROAR_OE_BASICS_CHANGE_FLAGS:
137     snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_BASICS_CHANGE_FLAGS */");
138    break;
139   case ROAR_OE_BASICS_NEW:
140     snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_BASICS_NEW */");
141    break;
142   case ROAR_OE_BASICS_DELETE:
143     snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_BASICS_DELETE */");
144    break;
145   // OE Streams:
146   case ROAR_OE_STREAM_CHANGE_VOLUME:
147     snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_STREAM_CHANGE_VOLUME */");
148    break;
149   case ROAR_OE_STREAM_XRUN:
150     snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_STREAM_XRUN */");
151    break;
152   case ROAR_OE_STREAM_META_UPDATE:
153     snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_STREAM_META_UPDATE */");
154    break;
155   // OE Default:
156   default:
157     snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_OE2EVENT(%lu) */", (long unsigned int)ROAR_NOTIFY_EVENT2OE(ev));
158    break;
159  }
160 } else if ( ROAR_NOTIFY_IS_USER(ev) ) {
161  snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_USER2EVENT(%lu) */", (long unsigned int)ROAR_NOTIFY_EVENT2USER(ev));
162 }
163
164 buf[sizeof(estr)-1] = 0;
165
166 if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT ) {
167  snprintf(buf, sizeof(buf)-1, ".event_proxy=0x%.8x%s, ", (int)event->event_proxy, estr);
168  buf[sizeof(buf)-1] = 0;
169 }
170
171 ROAR_INFO("dbg_notify_cb(core=%p, event=%p{.flags=0x%.8x, event=0x%.8x%s, %s.emitter=%i, .target=%i, .target_type=%i%s, .arg0=%i, .arg1=%i, .arg2=%p}, userdata=%p) = (void)",
172           0 /* always print: minlevel = 0 */,
173           core, event,
174           (int)event->flags,
175           (int)event->event,
176           (event->flags & ROAR_EVENT_FLAG_PROXYEVENT ? "" : estr),
177           buf,
178           event->emitter,
179           event->target,
180           event->target_type,
181           ttstr,
182           event->arg0,
183           event->arg1,
184           event->arg2,
185           userdata);
186}
187
188void dbg_notify_cb_register (void) {
189 struct roar_event event;
190
191 memset(&event, 0, sizeof(event));
192
193 event.event = ROAR_EGRP_ANY_EVENT;
194
195 event.emitter = -1;
196 event.target = -1;
197 event.target_type = -1;
198
199 roar_notify_core_subscribe(NULL, &event, dbg_notify_cb, NULL);
200}
201
202#ifdef ROAR_HAVE_MAIN_ARGS
203void usage (void) {
204 printf("Usage: roard [OPTIONS]...\n\n");
205
206 printf("Misc Options:\n\n");
207 printf(
208#if defined(ROAR_HAVE_FORK) || defined(ROAR_TARGET_WIN32)
209        " --daemon              - Bring the server into background after init\n"
210#endif
211        " --verbose             - Be more verbose, can be used multiple times\n"
212        " --terminate           - Terminate after last client quited\n"
213        " --start               - No op parameter (starting roard is default operation)\n"
214        " --restart             - Trys to stop an old instance and start a new with new settings\n"
215        " --stop                - Stops a running roard (provide --pidfile!)\n"
216        " --shutdown            - Terminates a running roard (provide --pidfile!)\n"
217        " --realtime            - Trys to get realtime priority,\n"
218        "                         give multible times for being more realtime\n"
219        " --memlock LEVEL       - Set default memory locking level to LEVEL\n"
220#ifdef ROAR_HAVE_CHROOT
221        " --chroot DIR          - chroots to the given dir\n"
222#endif
223#ifdef ROAR_HAVE_SETGID
224        " --setgid              - GroupID to the audio group as specified via -G\n"
225#endif
226#ifdef ROAR_HAVE_SETUID
227        " --setuid              - UserID to the audio user as specified via -U\n"
228#endif
229        " --sysclocksync        - calculate exact sample rate using the system clock\n"
230        " --location  LOC       - Set lion readable location of server\n"
231        " --description  DESC   - Set lion readable description of server\n"
232        " --contact CONTACT     - Set contact for this server\n"
233        " --serial SERIAL       - Set serial for this device or server\n"
234        "                         (for embedded devices only)\n"
235        " --uiurl UIURL         - Set URL for userinterface of this device or server\n"
236        "                         (for embedded devices only)\n"
237#ifdef SUPPORT_PIDFILE
238        " --pidfile PIDFILE     - Write a pidfile at PIDFILE\n"
239#endif
240#ifdef ROAR_HAVE_SYSLOG
241        " --log-syslog          - Log Warnings, Errors, ... to syslog\n"
242#endif
243#ifdef ROAR_HAVE_SYSTEM
244        " --script-postdown S   - Run command lion S after complet shutdown.\n"
245#endif
246       );
247
248 printf("\nAuth Options:\n\n");
249 printf(
250        " --guest-acclev ACCLEV - Sets the access level for guest access to ACCLEV,\n"
251        "                         Use \"none\" to disable guest access.\n"
252        " --trust-acclev ACCLEV - Sets the access level for trust-authed\n"
253        "                         connections to ACCLEV. Use \"none\" to disable trust auth.\n"
254        " --trust-root          - Trust root user\n"
255        " --no-trust-root       - Don't trust root user\n"
256        " --authfile-gen FILE   - Generate an new authfile\n"
257        " --authfile-load FILE  - Load an authfile\n"
258        " --authfile-type TYPE  - Type of authfile\n"
259        " --authfile-acclev ACCLEV\n"
260        "                       - Sets the access level for authfile\n"
261        " --new-authfile        - Parameters for new authfile follow\n"
262       );
263
264 printf("\nPlugin Options:\n\n");
265 printf(
266        " --plugin-load FILE    - Load plugin FILE\n"
267        " --plugin-args ARGS    - Arguments for the plugin\n"
268        "                         (must be given before the --plugin-load)\n"
269       );
270
271 printf("\nAudio Options:\n\n");
272 printf(
273        " -R  --rate   RATE     - Set server rate\n"
274        " -B  --bits   BITS     - Set server bits\n"
275        " -C  --chans  CHANNELS - Set server channels\n"
276        " --aiprofile  PROFILE  - Use the given audio profile\n"
277       );
278
279 printf("\nStream Options:\n\n");
280 printf(
281        " --stream-flags D=F    - Set default flags for stream directions\n"
282        "                         D is the stream direction and F is a comma separated\n"
283        "                         list of flags in form +flag or -flag to set or unset\n"
284        "                         a flag as default or remove it from the default\n"
285       );
286 printf("\nStream Options:\n\n");
287 printf(
288        " --list-rolestack      - Show the current role stack\n"
289        " --rolestack-push R:A  - Add a rule for role R with action A on top\n"
290        "                         of the role stack.\n"
291       );
292
293 printf("\nOutput Options:\n\n");
294 printf(" -o  --odriver DRV     - Set the driver, use '--list-driver' to get a list\n");
295 printf(" -O  --odevice DEV     - Set the device\n");
296 printf(" -oO OPTS              - Set output options\n");
297 printf(" -oN                   - Adds another output\n");
298 printf(" -oP                   - Mark output as primary\n");
299#ifdef ROAR_DRIVER_DEFAULT
300 printf(" --list-driver         - List all drivers (default driver: %s)\n", ROAR_DRIVER_DEFAULT);
301#else
302 printf(" --list-driver         - List all drivers (default driver: (autodetect))\n");
303#endif
304
305#ifndef ROAR_WITHOUT_DCOMP_SOURCES
306 printf("\nSource Options:\n\n");
307 printf(" -s  --source DRV      - Use DRV as input driver\n"
308        " -S           DEV      - Use DEV as input device\n"
309        " -sO          OPTS     - Use OPTS as input options\n"
310        " -sN                   - Adds another source\n"
311        " -sP                   - Make souce as primary\n"
312       );
313 printf(" --list-sources        - List all sources\n");
314#endif
315
316#ifndef ROAR_WITHOUT_DCOMP_MIXER
317 printf("\nHardware Mixer Options:\n\n");
318 printf(" -m  --mixer  DRV      - Use DRV as mixer driver\n"
319        " -M           DEV      - Use DEV as mixer device\n"
320        " -mO          OPTS     - Use OPTS as mixer options\n"
321        " -mN                   - Adds another mixer\n"
322        " -mP                   - Make mixer as primary\n"
323       );
324 printf(" --list-mixers         - List all mixers\n");
325#endif
326
327 printf("\nCodec Filter Options:\n\n");
328 printf(" --list-cf             - List all codec filter\n"
329       );
330
331#ifndef ROAR_WITHOUT_DCOMP_MIDI
332 printf("\nMIDI Options:\n\n");
333 printf(" --midi-no-console     - Disable console based MIDI synth\n"
334        " --midi-console-enable - Enables the console based MIDI synth\n"
335        " --midi-console DEV    - Set device for MIDI console\n"
336#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
337        " --ssynth-enable       - Enable simple software synth\n"
338        " --ssynth-disable      - Disable simple software synth\n"
339#endif
340       );
341#endif
342
343#ifndef ROAR_WITHOUT_DCOMP_LIGHT
344 printf("\nLight Control Options:\n\n");
345 printf(" --light-channels NUM  - Sets the number of channels for Light control (default: %i)\n",
346                                  LIGHT_CHANNELS_DEFAULT
347       );
348#endif
349
350#ifndef ROAR_WITHOUT_DCOMP_RDTCS
351 printf("\nRadio Data and Transmitter Control System Options:\n\n");
352 printf(" --rds-pi   PI         - Sets the RDS Programme Identification (PI)\n"
353        " --rds-ps   PS         - Sets the RDS Programme Service Name (PS)\n"
354        " --rds-pty  PTY        - Sets the RDS Programme Type (PTY)\n"
355        " --rds-tp              - Sets the RDS Traffic Programme (TP) flag\n"
356        " --rds-ct              - Enables sending of RDS Clock Time (CT)\n"
357       );
358#endif
359
360#ifdef ROAR_HAVE_LIBX11
361 printf("\nX11 Options:\n\n");
362 printf(
363        " --x11-display DISPLAY - Set display for X11\n"
364        " --display DISPLAY     - Set display for X11\n"
365       );
366#endif
367
368 printf("\nServer Options:\n\n");
369 printf(" -t  --tcp             - Use TCP listen socket\n"
370#ifdef ROAR_HAVE_UNIX
371        " -u  --unix            - Use UNIX Domain listen socket (default)\n"
372#endif
373#ifdef ROAR_HAVE_LIBDNET
374        " -n  --decnet          - use DECnet listen socket\n"
375#endif
376        " -4                    - Use IPv4 connections (implies -t)\n"
377#ifdef AF_INET6
378        " -6                    - Use IPv6 connections (implies -t)\n"
379#endif
380#ifdef IPV6_ADDRFORM
381        " -64                   - Try to downgrade sockets from IPv6 into IPv4,\n"
382        "                         this is normaly not useful.\n"
383#endif
384        " -p  --port            - TCP Port to bind to\n"
385        " -b  --bind            - IP/Hostname to bind to\n"
386        "     --sock            - Filename for UNIX Domain Socket\n"
387        "     --proto PROTO     - Use PROTO as protocol on Socket\n"
388        "     --proto-dir DIR   - Set direction parameter for protocol\n"
389        "     --proto-rate RATE - Set sample rate parameter for protocol\n"
390        "     --proto-bits BITS - Set bits per sample parameter for protocol\n"
391        "     --proto-codec E   - Set codec parameter for protocol\n"
392        "     --proto-chans C   - Set number of channels parameter for protocol\n"
393        "     --proto-aiprofile PROFILE\n"
394        "                       - Sets the audio profile for socket\n"
395        "     --proto-profile P - Set profile for listen socket\n"
396        "     --list-proto      - List supported protocols\n"
397        "     --list-profiles   - List supported profiles for --proto-profile\n"
398        "     --list-aiprofiles - List supported profiles for --proto-aiprofile\n"
399        "     --new-sock        - Parameters for new socket follow\n"
400#ifdef ROAR_HAVE_LIBSLP
401        "     --slp             - Enable OpenSLP support\n"
402#endif
403#ifdef ROAR_HAVE_LIBX11
404        "     --x11             - Enable X11 support\n"
405#endif
406        " --jumbo-mtu MTU       - Sets the MTU for Jumbo Packets\n"
407        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: %s)\n"
408        "                         You need the permissions to change the GID\n"
409        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
410        "                         You need the permissions to change the UID (normaly only root has)\n"
411        " --no-listen           - Do not listen for new clients\n"
412        "                         (only useful for relaing, impleys --terminate)\n"
413        " --client-fh           - Comunicate with a client over this handle\n"
414        "                         (only useful for relaing)\n"
415        " --close-fh            - Closes the given fh\n"
416        " --standby             - Start in standby state\n"
417        " --auto-standby        - Automatical goes into standby if there are no streams\n",
418#ifdef ROAR_DEFAULT_SOCKGRP
419        ROAR_DEFAULT_SOCKGRP
420#else
421        "(none)"
422#endif
423       );
424// printf("\n Options:\n\n");
425 printf("\n");
426}
427
428#endif
429
430
431struct counters * counters_getptr(void) {
432 return &g_counters;
433}
434
435#define _pmsg(format, args...) roar_debug_msg(type, __LINE__, __FILE__, ROAR_DBG_PREFIX, format, ## args)
436#define _pmsgc(group, counter, name) _pmsg("  Counter %-10s: %llu", (name), (long long unsigned int)(counters->group.counter))
437void counters_print(int type, int force) {
438 struct counters * counters = counters_getptr();
439 if ( type != ROAR_DEBUG_TYPE_INFO || force || (ROAR_DBG_INFOVAR) >= ROAR_DBG_INFO_INFO ) {
440  _pmsg("--- Counter Listing ---");
441  _pmsg(" Current:");
442  _pmsgc(cur, clients, "Clients");
443  _pmsgc(cur, streams, "Streams");
444  _pmsg(" Total:");
445  _pmsgc(sum, clients, "Clients");
446  _pmsgc(sum, streams, "Streams");
447  _pmsg("--- End of Counter Listing ---");
448 }
449}
450#undef _pmsgc
451#undef _pmsg
452
453int restart_server (const char * server, int terminate) {
454 struct roar_connection con;
455#ifdef ROAR_HAVE_KILL
456 char buf[80];
457 ssize_t l;
458 struct roar_vio_calls fh;
459 pid_t pid;
460 int ok;
461
462 ROAR_INFO("restart_server(server='%s', terminate=%i): trying to restart server", ROAR_DBG_INFO_INFO, server, terminate);
463
464 if ( pidfile != NULL ) {
465  if ( roar_vio_open_dstr_simple(&fh, pidfile, O_RDONLY) ) {
466   ROAR_WARN("restart_server(*): Can not read pidfile: %s", pidfile);
467  } else {
468   l = roar_vio_read(&fh, buf, 80);
469   roar_vio_close(&fh);
470   if ( l > 0 ) {
471    buf[l-1] = 0;
472    buf[79]  = 0;
473    pid = atoi(buf);
474    if ( terminate ) {
475     ok = kill(pid, SIGUSR1);
476    } else {
477     ok = kill(pid, SIGINT);
478    }
479    if ( ok == 0 ) {
480     return 0;
481    } else {
482     ROAR_WARN("restart_server(*): Can not kill roard by pidfile");
483    }
484   } else {
485    ROAR_WARN("restart_server(*): Can not find a PID in the pidfile");
486   }
487  }
488 }
489#endif
490
491 if ( roar_simple_connect(&con, server, "roard") == -1 ) {
492  return -1;
493 }
494
495 if ( roar_terminate(&con, terminate) == -1 ) {
496  return -1;
497 }
498
499 return roar_disconnect(&con);
500}
501
502#define R_SETUID 1
503#define R_SETGID 2
504
505int init_config (void) {
506 int i;
507
508 memset(g_config, 0, sizeof(struct roard_config));
509
510 for (i = 0; i < ROAR_DIR_DIRIDS; i++) {
511  g_config->streams[i].mixer_channels = 1;
512  g_config->streams[i].mixer.rpg_mul  = 1;
513  g_config->streams[i].mixer.rpg_div  = 1;
514  g_config->streams[i].mixer.scale    = 65535;
515  g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale;
516 }
517
518 g_config->streams[ROAR_DIR_PLAY    ].flags = ROAR_FLAG_META;
519 g_config->streams[ROAR_DIR_OUTPUT  ].flags = ROAR_FLAG_PASSMIXER;
520 g_config->streams[ROAR_DIR_FILTER  ].flags = ROAR_FLAG_SYNC;
521 g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC;
522 g_config->streams[ROAR_DIR_BIDIR   ].flags = ROAR_FLAG_ANTIECHO;
523
524 g_config->location    = CONF_DEF_STRING;
525 g_config->description = CONF_DEF_STRING;
526 g_config->contact     = NULL;
527 g_config->serial      = NULL;
528 g_config->uiurl       = NULL;
529
530 g_config->memlock_level = -1;
531
532#ifdef ROAR_HAVE_SYSTEM
533 g_config->scripts.post_shutdown = NULL;
534#endif
535
536 return 0;
537}
538
539#ifdef ROAR_SUPPORT_LISTEN
540int init_listening (void) {
541 int i;
542
543 memset(g_listen, 0, sizeof(g_listen));
544
545 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
546  g_listen[i].proto  = ROAR_PROTO_ROARAUDIO;
547  server[i]          = NULL;
548 }
549
550 return 0;
551}
552
553int get_listen(struct roard_listen ** sock, char *** sockname) {
554 int i;
555
556 if ( sock == NULL )
557  return -1;
558
559 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
560  if ( ! g_listen[i].used ) {
561   server[i] = NULL;
562   *sock = &(g_listen[i]);
563
564   if ( sockname != NULL )
565    *sockname = &(server[i]);
566
567   return 0;
568  }
569 }
570
571 return -1;
572}
573
574#ifdef ROAR_SUPPORT_LISTEN
575static const struct _listen_profile {
576 const char * name;
577 int          type;
578 int          port;
579 const char * sockaddr;
580 int          proto;
581 int          dir;
582 const char * aiprofile;
583 const char * desc;
584} _g_listen_profiles[] = {
585 // RoarAudio:
586#ifdef ROAR_HAVE_UNIX
587 {"roar-usock",     ROAR_SOCKET_TYPE_UNIX,   0,                 "~/" ROAR_DEFAULT_SOCK_USER,
588                    ROAR_PROTO_ROARAUDIO, -1, NULL,
589                    "RoarAudio default user profile"},
590 {"roar-gsock",     ROAR_SOCKET_TYPE_UNIX,   0,                 ROAR_DEFAULT_SOCK_GLOBAL,
591                    ROAR_PROTO_ROARAUDIO, -1, NULL,
592                    "RoarAudio default global profile"},
593#endif
594#ifdef ROAR_HAVE_IPV4
595 {"roar-tcp",       ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_PORT, ROAR_DEFAULT_INET4_HOST,
596                    ROAR_PROTO_ROARAUDIO, -1, NULL,
597                    "RoarAudio local TCP profile"},
598 {"roar-tcp-pub",   ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_PORT, ROAR_NET_INET4_ANYHOST,
599                    ROAR_PROTO_ROARAUDIO, -1, NULL,
600                    "RoarAudio network TCP profile"},
601#endif
602#ifdef ROAR_HAVE_IPV6
603 {"roar-tcp6",      ROAR_SOCKET_TYPE_TCP6,   ROAR_DEFAULT_PORT, ROAR_DEFAULT_INET6_HOST,
604                    ROAR_PROTO_ROARAUDIO, -1, NULL,
605                    "RoarAudio local TCP profile"},
606 {"roar-tcp-pub6",  ROAR_SOCKET_TYPE_TCP6,   ROAR_DEFAULT_PORT, ROAR_NET_INET6_ANYHOST,
607                    ROAR_PROTO_ROARAUDIO, -1, NULL,
608                    "RoarAudio network TCP profile"},
609#endif
610#ifdef ROAR_HAVE_LIBDNET
611 {"roar-dnet",      ROAR_SOCKET_TYPE_DECNET, 0,                 ROAR_DEFAULT_LISTEN_OBJECT,
612                    ROAR_PROTO_ROARAUDIO, -1, NULL,
613                    "RoarAudio default DECnet"},
614#endif
615#ifdef ROAR_HAVE_UNIX
616 {"roar-abstract",  ROAR_SOCKET_TYPE_UNKNOWN,   0,              "+abstract",        ROAR_PROTO_ROARAUDIO, -1, NULL,
617                    "RoarAudio abstract namespace profile"},
618#endif
619
620 // EsounD:
621#if !defined(ROAR_WITHOUT_DCOMP_EMUL_ESD) && defined(ROAR_HAVE_H_ESD)
622#ifdef ROAR_HAVE_UNIX
623 {"esd-unix",       ROAR_SOCKET_TYPE_UNIX,   0,                 ROAR_DEFAULT_ESD_GSOCK,
624                    ROAR_PROTO_ESOUND,    -1, NULL,
625                    "EsounD default local profile"},
626#endif
627#ifdef ROAR_HAVE_IPV4
628 {"esd-tcp",        ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_ESD_PORT, ROAR_NET_INET4_LOCALHOST,
629                    ROAR_PROTO_ESOUND,    -1, NULL,
630                    "EsounD local TCP profile"},
631 {"esd-tcp-pub",    ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_ESD_PORT, ROAR_NET_INET4_ANYHOST,
632                    ROAR_PROTO_ESOUND,    -1, NULL,
633                    "EsounD network TCP profile"},
634#endif
635#ifdef ROAR_HAVE_IPV6
636 {"esd-tcp6",       ROAR_SOCKET_TYPE_TCP6,   ROAR_DEFAULT_ESD_PORT, ROAR_NET_INET6_LOCALHOST,
637                    ROAR_PROTO_ESOUND,    -1, NULL,
638                    "EsounD local TCP profile"},
639 {"esd-tcp-pub6",   ROAR_SOCKET_TYPE_TCP6,   ROAR_DEFAULT_ESD_PORT, ROAR_NET_INET6_ANYHOST,
640                    ROAR_PROTO_ESOUND,    -1, NULL,
641                    "EsounD network TCP profile"},
642#endif
643#endif
644
645 // RSound:
646#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
647#ifdef ROAR_HAVE_UNIX
648 {"rsound-unix",    ROAR_SOCKET_TYPE_UNIX,   0,                 ROAR_DEFAULT_RSOUND_GSOCK,
649                    ROAR_PROTO_RSOUND,    -1, NULL,
650                    "RSound default local profile"},
651#endif
652#ifdef ROAR_HAVE_IPV4
653 {"rsound-tcp",     ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_RSOUND_PORT, ROAR_NET_INET4_LOCALHOST,
654                    ROAR_PROTO_RSOUND,    -1, NULL,
655                    "RSound local TCP profile"},
656 {"rsound-tcp-pub", ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_RSOUND_PORT, ROAR_NET_INET4_ANYHOST,
657                    ROAR_PROTO_RSOUND,    -1, NULL,
658                    "RSound network TCP profile"},
659#endif
660#ifdef ROAR_HAVE_IPV6
661 {"rsound-tcp6",    ROAR_SOCKET_TYPE_TCP6,   ROAR_DEFAULT_RSOUND_PORT, ROAR_NET_INET6_LOCALHOST,
662                    ROAR_PROTO_RSOUND,    -1, NULL,
663                    "RSound local TCP profile"},
664 {"rsound-tcp-pub6", ROAR_SOCKET_TYPE_TCP6,  ROAR_DEFAULT_RSOUND_PORT, ROAR_NET_INET6_ANYHOST,
665                    ROAR_PROTO_RSOUND,    -1, NULL,
666                    "RSound network TCP profile"},
667#endif
668#ifdef ROAR_HAVE_LIBDNET
669 {"rsound-dnet",    ROAR_SOCKET_TYPE_DECNET, 0,                 ROAR_DEFAULT_RSOUND_OBJECT,
670                    ROAR_PROTO_RSOUND,    -1, NULL,
671                    "RSound DECnet profile"},
672#endif
673#endif
674
675 // PulseAudio Simple:
676#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
677#ifdef ROAR_HAVE_IPV4
678 {"pas-play-tcp",   ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_PA_PORT, ROAR_NET_INET4_ANYHOST,
679                    ROAR_PROTO_SIMPLE, ROAR_DIR_PLAY, "default",
680                    "PulseAudio Simple TCP play profile"},
681 {"pas-mon-tcp",    ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_PA_PORT, ROAR_NET_INET4_ANYHOST,
682                    ROAR_PROTO_SIMPLE, ROAR_DIR_MONITOR, "default",
683                    "PulseAudio Simple TCP monitor profile"},
684#endif
685#ifdef ROAR_HAVE_IPV6
686 {"pas-play-tcp6",  ROAR_SOCKET_TYPE_TCP6,   ROAR_DEFAULT_PA_PORT, ROAR_NET_INET6_ANYHOST,
687                    ROAR_PROTO_SIMPLE, ROAR_DIR_PLAY, "default",
688                    "PulseAudio Simple TCP play profile"},
689 {"pas-mon-tcp6",   ROAR_SOCKET_TYPE_TCP6,   ROAR_DEFAULT_PA_PORT, ROAR_NET_INET6_ANYHOST,
690                    ROAR_PROTO_SIMPLE, ROAR_DIR_MONITOR, "default",
691                    "PulseAudio Simple TCP monitor profile"},
692#endif
693#endif
694
695 // RPlay:
696#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
697#ifdef ROAR_HAVE_IPV4
698 {"rplay-tcp",      ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_RPLAY_PORT, ROAR_NET_INET4_LOCALHOST,
699                    ROAR_PROTO_RPLAY,     -1, NULL,
700                    "RPlay local TCP profile"},
701 {"rplay-tcp-pub",  ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_RPLAY_PORT, ROAR_NET_INET4_ANYHOST,
702                    ROAR_PROTO_RPLAY,     -1, NULL,
703                    "RPlay network TCP profile"},
704#endif
705#ifdef ROAR_HAVE_IPV6
706 {"rplay-tcp6",     ROAR_SOCKET_TYPE_TCP6,   ROAR_DEFAULT_RPLAY_PORT, ROAR_NET_INET6_LOCALHOST,
707                    ROAR_PROTO_RPLAY,     -1, NULL,
708                    "RPlay local TCP profile"},
709 {"rplay-tcp-pub6", ROAR_SOCKET_TYPE_TCP6,   ROAR_DEFAULT_RPLAY_PORT, ROAR_NET_INET6_ANYHOST,
710                    ROAR_PROTO_RPLAY,     -1, NULL,
711                    "RPlay network TCP profile"},
712#endif
713#endif
714
715// Gopher:
716#ifndef ROAR_WITHOUT_DCOMP_EMUL_GOPHER
717#ifdef ROAR_HAVE_IPV4
718 {"gopher-tcp",     ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_GOPHER_PORT, ROAR_NET_INET4_LOCALHOST,
719                    ROAR_PROTO_GOPHER,    -1, NULL,
720                    "Gopher local TCP profile"},
721 {"gopher-tcp-pub", ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_GOPHER_PORT, ROAR_NET_INET4_ANYHOST,
722                    ROAR_PROTO_GOPHER,    -1, NULL,
723                    "Gopher network TCP profile"},
724#endif
725#ifdef ROAR_HAVE_IPV6
726 {"gopher-tcp6",    ROAR_SOCKET_TYPE_TCP6,   ROAR_DEFAULT_GOPHER_PORT, ROAR_NET_INET6_LOCALHOST,
727                    ROAR_PROTO_GOPHER,    -1, NULL,
728                    "Gopher local TCP profile"},
729 {"gopher-tcp-pub6", ROAR_SOCKET_TYPE_TCP6,  ROAR_DEFAULT_GOPHER_PORT, ROAR_NET_INET6_ANYHOST,
730                    ROAR_PROTO_GOPHER,    -1, NULL,
731                    "Gopher network TCP profile"},
732#endif
733#endif
734
735 // End of List:
736 {NULL, -1, -1, NULL, -1, -1, NULL, NULL}
737};
738
739void listen_listen_profiles (void) {
740 const struct _listen_profile * p;
741 char * type;
742 int i;
743 char port[8];
744
745 printf("Name            Type    Address          Port    Protocol  Dir        Audio Profile - Description\n");
746 printf("-------------------------------------------------------------------------------------------------\n");
747      //roar-tcp-pub 0.0.0.0       16002   RoarAudio unknown    (null) - (null)
748
749 for (i = 0; (p = &(_g_listen_profiles[i]))->name != NULL; i++) {
750  switch (p->type) {
751   case ROAR_SOCKET_TYPE_UNIX:   type = "UNIX";   break;
752   case ROAR_SOCKET_TYPE_TCP:    type = "TCP";    break;
753   case ROAR_SOCKET_TYPE_DECNET: type = "DECnet"; break;
754   case ROAR_SOCKET_TYPE_TCP6:   type = "TCP6";   break;
755   default:
756     type = "unknown";
757    break;
758  }
759
760  if ( p->port ) {
761   snprintf(port, sizeof(port)-1, "%i", p->port);
762   port[sizeof(port)-1] = 0;
763  } else {
764   strcpy(port, "(none)");
765  }
766
767  printf("%-15s %-7s %-16s %-7s %-9s %-10s %-13s - %s\n",
768           p->name,
769           type,
770           p->sockaddr, port,
771           roar_proto2str(p->proto),
772           p->dir == -1 ? "(none)" : roar_dir2str(p->dir), p->aiprofile == NULL ? "(none)" : p->aiprofile,
773           p->desc == NULL ? "" : p->desc);
774 }
775}
776
777int get_listen_profile (const char * name,
778                        int * port, char ** sockaddr, int * type,
779                        int * proto,
780                        int * dir, struct roar_audio_info * info) {
781 static char buf[1024];
782 const struct _listen_profile * p;
783 int i;
784
785 for (i = 0; (p = &(_g_listen_profiles[i]))->name != NULL; i++) {
786  if ( !strcasecmp(p->name, name) ) {
787   *port     = p->port;
788
789   if ( p->type == ROAR_SOCKET_TYPE_UNIX && p->sockaddr[0] != '+' ) {
790    roar_env_render_path_r(buf, sizeof(buf), p->sockaddr);
791   } else {
792    strncpy(buf, p->sockaddr, sizeof(buf));
793   }
794   *sockaddr = buf;
795   *type     = p->type;
796
797   *proto    = p->proto;
798   *dir      = p->dir;
799
800   if ( p->aiprofile != NULL ) {
801    if ( roar_profile2info(info, p->aiprofile) == -1 ) {
802     ROAR_ERR("Unknown audio profile: %s", p->aiprofile);
803     return -1;
804    }
805   }
806   return 0;
807  }
808 }
809
810 return -1;
811}
812#endif
813
814int add_listen (char * addr, int port, int sock_type, char * user, char * group, int proto, int dir, struct roar_audio_info * info) {
815#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
816 struct group   * grp  = NULL;
817#endif
818#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
819 struct passwd  * pwd  = NULL;
820#endif
821#ifdef ROAR_HAVE_UNIX
822 char * env_roar_proxy_backup;
823#endif
824 int    sockid = -1;
825#ifdef ROAR_HAVE_UNIX
826 int    sock;
827#endif
828 int    i;
829
830 ROAR_INFO("add_listen(addr='%s', port=%i, sock_type=%i, user='%s', group='%s', proto=%s(%i), dir=%s(%i), info={.rate=%u, .bits=%u, .channels=%u, .codec=%s(%u)}): trying to add listen socket",
831            ROAR_DBG_INFO_INFO,
832            addr, port, sock_type, user, group, roar_proto2str(proto), proto, roar_dir2str(dir), dir,
833            info->rate, info->bits, info->channels, roar_codec2str(info->codec), info->codec);
834
835 if ( *addr != 0 ) {
836  for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
837   if ( ! g_listen[i].used ) {
838    sockid = i;
839    break;
840   }
841  }
842
843  if ( sockid == -1 )
844   return -1;
845
846  g_listen[sockid].proto = proto;
847
848  ROAR_DBG("add_listen(*): proto=0x%.4x", proto);
849
850  if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) {
851#ifdef ROAR_HAVE_UNIX
852   if ( *addr == '/' ) {
853    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
854     env_roar_proxy_backup = roar_mm_strdup(env_roar_proxy_backup);
855     unsetenv("ROAR_PROXY");
856    }
857    if ( (sock = roar_socket_connect(ROAR_SOCKET_TYPE_UNKNOWN, addr, port)) != -1 ) {
858     close(sock);
859     ROAR_ERR("Can not open listen socket: Socket allready in use");
860     return -1;
861    } else {
862     unlink(addr);
863     if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) {
864      ROAR_ERR("Can not open listen socket: %s", strerror(errno));
865      return -1;
866     }
867    }
868    if ( env_roar_proxy_backup != NULL ) {
869     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
870     roar_mm_free(env_roar_proxy_backup);
871    }
872#else
873   if (0) { // noop
874#endif
875   } else {
876    ROAR_ERR("Can not open listen socket: %s", strerror(errno));
877    return -1;
878   }
879  }
880
881  ROAR_DBG("add_listen(*) = ?");
882
883#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
884  if ( group != NULL ) {
885   if ( (grp = getgrnam(group)) == NULL ) {
886    ROAR_ERR("Can not get GID for group %s: %s", group, strerror(errno));
887   }
888  }
889#endif
890
891  ROAR_DBG("add_listen(*) = ?");
892
893#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
894  if ( user != NULL ) {
895   if ( (pwd = getpwnam(user)) == NULL ) {
896    ROAR_ERR("Can not get UID for user %s: %s", user, strerror(errno));
897   }
898  }
899#endif
900
901  ROAR_DBG("add_listen(*) = ?");
902
903#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
904  if ( *addr == '/' ) {
905   if ( grp != NULL || pwd != NULL ) {
906     if ( chown(addr, pwd != NULL ? pwd->pw_uid : -1, grp != NULL ? grp->gr_gid : -1) == -1 )
907      return -1;
908   }
909#ifdef ROAR_HAVE_GETUID
910   if ( grp != NULL ) {
911    if ( getuid() == 0 )
912     if ( chmod(addr, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
913      return -1;
914   }
915#endif
916  }
917#endif
918 }
919
920 ROAR_DBG("add_listen(*) = ?");
921
922 // in case we opened the listening socket correctly.
923 if ( dir == -1 )
924  dir = ROAR_DIR_PLAY;
925
926 g_listen[sockid].inst.stpl.dir = dir;
927 memcpy(&(g_listen[sockid].inst.stpl.info), info, sizeof(struct roar_audio_info));
928
929 switch (dir) {
930  case ROAR_DIR_PLAY:
931  case ROAR_DIR_RECORD:
932  case ROAR_DIR_MONITOR:
933  case ROAR_DIR_FILTER:
934  case ROAR_DIR_BIDIR:
935  case ROAR_DIR_RECPLAY:
936    if ( !g_listen[sockid].inst.stpl.info.rate )
937     g_listen[sockid].inst.stpl.info.rate = g_sa->rate;
938
939    if ( !g_listen[sockid].inst.stpl.info.bits )
940     g_listen[sockid].inst.stpl.info.bits = g_sa->bits;
941
942    if ( !g_listen[sockid].inst.stpl.info.channels )
943     g_listen[sockid].inst.stpl.info.channels = g_sa->channels;
944
945    if ( !g_listen[sockid].inst.stpl.info.codec )
946     g_listen[sockid].inst.stpl.info.codec = g_sa->codec;
947   break;
948 }
949
950 g_listen[sockid].used = 1;
951 server[sockid]        = addr;
952
953 return 0;
954}
955
956static int check_listen(void) {
957 const struct roard_proto_handle * proto;
958 const char * protoname;
959 char buffer[80];
960 size_t i, j;
961
962 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
963  if ( g_listen[i].used ) {
964   proto = clients_get_protohandle(g_listen[i].proto);
965   if ( proto != NULL )
966    continue;
967   protoname = roar_proto2str(g_listen[i].proto);
968   if ( protoname == NULL ) {
969    ROAR_ERR("check_listen(void): protocol %i is unknown (protocol ID not assigned?).");
970   } else {
971    ROAR_DBG("check_listen(void): Unknown protocol %s(%i) is used.", protoname, g_listen[i].proto);
972    snprintf(buffer, sizeof(buffer), "protocol-%s", protoname);
973    for (j = 0; buffer[j]; j++)
974     buffer[j] = tolower(buffer[j]);
975
976    ROAR_DBG("check_listen(void): Trying to load plugin \"%s\"", buffer);
977    if ( plugins_load(buffer, NULL) == -1 ) {
978     ROAR_WARN("check_listen(void): unabled to load plugin: %s: %s", buffer, roar_errorstring);
979    }
980   }
981
982   // recheck:
983   proto = clients_get_protohandle(g_listen[i].proto);
984   if ( proto == NULL ) {
985    ROAR_WARN("check_listen(void): Protocol %s(%i) is still unknown. Deleting listen socket.", protoname, g_listen[i].proto);
986    roar_vio_close(&(g_listen[i].sock));
987#ifdef ROAR_HAVE_UNIX
988    if ( server[i] != NULL )
989     if ( server[i][0] == '/' )
990      unlink(server[i]);
991#endif
992    g_listen[i].used = 0;
993    server[i] = NULL;
994   }
995  }
996 }
997
998 return 0;
999}
1000#endif
1001
1002#ifdef ROAR_HAVE_MAIN_ARGS
1003static void list_aiprofiles (void) {
1004 struct roar_audio_info info;
1005 const char * list[1024];
1006 const char * mime;
1007 ssize_t ret;
1008 ssize_t i;
1009
1010 ret = roar_profiles_list(list, 1024, 0);
1011
1012 if ( ret == -1 )
1013  return;
1014
1015 printf("  Name             Rate  Bits  Channels  Codec\n");
1016 printf("-----------------------------------------------------------------------------\n");
1017 for (i = 0; i < ret; i++) {
1018  if ( roar_profile2info(&info, list[i]) == -1 ) {
1019   printf("  %-10s --- unknown parameters ---\n", list[i]);
1020   continue;
1021  }
1022
1023  mime = roar_codec2mime(info.codec);
1024
1025  printf("  %-16s %5u %4u  %8u  %3u (%s%s%s%s)\n", list[i],
1026            info.rate, info.bits, info.channels,
1027            info.codec, roar_codec2str(info.codec),
1028            info.codec == ROAR_CODEC_DEFAULT ? " native" : "",
1029            mime == NULL ? "" : " mimetype:",
1030            mime == NULL ? "" : mime
1031        );
1032 }
1033}
1034#endif
1035
1036int update_stream_flags (char * str) {
1037 int    dir;
1038 char * flags;
1039 char * k;
1040 int    op;
1041 int    flag;
1042
1043 if ( (flags = strstr(str, "=")) == NULL )
1044  return -1;
1045
1046 *flags = 0;
1047  flags++;
1048
1049 if ( (dir = roar_str2dir(str)) == -1 )
1050  return -1;
1051
1052 while (flags != NULL) {
1053  k = flags;
1054  flags = strstr(flags, ",");
1055
1056  if ( flags != NULL )
1057   *(flags++) = 0;
1058
1059  switch (*k) {
1060   case '+': k++; op = ROAR_SET_FLAG;   break;
1061   case '-': k++; op = ROAR_RESET_FLAG; break;
1062   default:
1063     op = ROAR_SET_FLAG;
1064  }
1065
1066  flag = 0;
1067
1068  if ( !strcmp(k, "sync") ) {
1069   flag = ROAR_FLAG_SYNC;
1070  } else if ( !strcmp(k, "meta") ) {
1071   flag = ROAR_FLAG_META;
1072  } else if ( !strcmp(k, "cleanmeta") ) {
1073   flag = ROAR_FLAG_CLEANMETA;
1074  } else if ( !strcmp(k, "pause") ) {
1075   flag = ROAR_FLAG_PAUSE;
1076  } else if ( !strcmp(k, "mute") ) {
1077   flag = ROAR_FLAG_MUTE;
1078  } else if ( !strcmp(k, "antiecho") ) {
1079   flag = ROAR_FLAG_ANTIECHO;
1080  } else if ( !strcmp(k, "passmixer") ) {
1081   flag = ROAR_FLAG_PASSMIXER;
1082  } else if ( !strcmp(k, "recsource") ) {
1083   flag = ROAR_FLAG_RECSOURCE;
1084  } else {
1085   return -1;
1086  }
1087
1088  g_config->streams[dir].flags |= flag;
1089
1090  if ( op == ROAR_RESET_FLAG )
1091   g_config->streams[dir].flags -= flag;
1092 }
1093
1094 return 0;
1095}
1096
1097int add_authfile (const char * file, const char * type, enum af_mode mode, enum roard_client_acclev acclev) {
1098 struct roar_authfile * authfile = NULL;
1099 struct roar_authfile_key *  key = NULL;
1100 int af_type = ROAR_AUTHFILE_TYPE_AUTO;
1101 void * keydata;
1102 int i;
1103
1104 if ( type == NULL ) {
1105  // noop.
1106 } else if ( !strcasecmp(type, "roar") ) {
1107  af_type = ROAR_AUTHFILE_TYPE_ROAR;
1108 } else if ( !strcasecmp(type, "roar") ) {
1109  af_type = ROAR_AUTHFILE_TYPE_ROAR;
1110 } else if ( !strcasecmp(type, "esd") ) {
1111  af_type = ROAR_AUTHFILE_TYPE_ESD;
1112 } else if ( !strcasecmp(type, "pulse") ) {
1113  af_type = ROAR_AUTHFILE_TYPE_PULSE;
1114 } else if ( !strcasecmp(type, "htpasswd") ) {
1115  af_type = ROAR_AUTHFILE_TYPE_HTPASSWD;
1116 } else if ( !strcasecmp(type, "xauth") ) {
1117  af_type = ROAR_AUTHFILE_TYPE_XAUTH;
1118 } else {
1119  ROAR_ERR("add_authfile(*): unknown authfile type '%s'.", type);
1120  return -1;
1121 }
1122
1123 if ( mode == AF_MODE_GEN && af_type == ROAR_AUTHFILE_TYPE_AUTO )
1124  af_type = ROAR_AUTHFILE_TYPE_ESD;
1125
1126 switch (mode) {
1127  case AF_MODE_NONE:
1128    return 0;
1129   break;
1130  case AF_MODE_GEN:
1131    switch (af_type) {
1132     case ROAR_AUTHFILE_TYPE_ESD:
1133       key = roar_authfile_key_new_random(ROAR_AUTH_T_COOKIE, 16, NULL);
1134      break;
1135     case ROAR_AUTHFILE_TYPE_PULSE:
1136       key = roar_authfile_key_new_random(ROAR_AUTH_T_COOKIE, 256, NULL);
1137      break;
1138     default:
1139       return -1;
1140      break;
1141    }
1142
1143    if ( key == NULL ) {
1144     ROAR_ERR("add_authfile(*): Can not generate key.");
1145     return -1;
1146    }
1147
1148    if ( (authfile = roar_authfile_open(af_type, file, 1, ROAR_AUTHFILE_VERSION_AUTO)) == NULL ) {
1149     roar_authfile_key_unref(key);
1150     return -1;
1151    }
1152
1153    if ( roar_authfile_add_key(authfile, key) == -1 ) {
1154     ROAR_WARN("add_authfile(*): Can not add key to authfile.");
1155    }
1156
1157    keydata = roar_mm_memdup(key->data, key->len);
1158
1159    if ( keydata == NULL ) {
1160     ROAR_WARN("add_authfile(*): Can not allocate memory for key.");
1161    } else if ( auth_addkey_cookie(acclev, keydata, key->len) == -1 ) {
1162     ROAR_WARN("add_authfile(*): Can not add key to internal key storage.");
1163    }
1164
1165    roar_authfile_key_unref(key);
1166    if ( roar_authfile_close(authfile) != 0 )
1167     return -1;
1168    return 0;
1169   break;
1170  case AF_MODE_LOAD:
1171    if ( (authfile = roar_authfile_open(af_type, file, 0, ROAR_AUTHFILE_VERSION_AUTO)) == NULL ) {
1172     return -1;
1173    }
1174
1175    for (i = 0; ; i++) {
1176     key = roar_authfile_lookup_key(authfile, ROAR_AUTH_T_AUTO, i, NULL);
1177     if ( key == NULL )
1178      break;
1179
1180     if ( key->type == ROAR_AUTH_T_COOKIE ) {
1181      keydata = roar_mm_memdup(key->data, key->len);
1182
1183      if ( keydata == NULL ) {
1184       ROAR_WARN("add_authfile(*): Can not allocate memory for key.");
1185      } else if ( auth_addkey_cookie(acclev, keydata, key->len) == -1 ) {
1186       ROAR_WARN("add_authfile(*): Can not add key to internal key storage.");
1187      }
1188     } else {
1189      ROAR_WARN("add_authfile(*): Unknown key type: %i", key->type);
1190     }
1191
1192     roar_authfile_key_unref(key);
1193    }
1194
1195    if ( roar_authfile_close(authfile) != 0 )
1196     return -1;
1197    return 0;
1198   break;
1199 }
1200
1201 return -1;
1202}
1203
1204// X11:
1205#ifdef ROAR_HAVE_LIBX11
1206int register_x11 (int unreg, char * sockname) {
1207 struct roar_x11_connection * x11con = NULL;
1208 int ret = 0;
1209
1210 if ( (x11con = roar_x11_connect(x11display)) == NULL ) {
1211  ROAR_ERR("Can not connect to X11 server for %sregistering", unreg ? "un" : "");
1212  return -1;
1213 }
1214
1215 if ( unreg ) {
1216  if ( roar_x11_delete_prop(x11con, "ROAR_SERVER") == -1 ) {
1217   ret = -1;
1218   ROAR_ERR("Error while unregistereing from X11", ROAR_DBG_INFO_INFO);
1219  } else {
1220   ROAR_INFO("Successfully unregistered from X11", ROAR_DBG_INFO_INFO);
1221  }
1222 } else {
1223  if ( roar_x11_set_prop(x11con, "ROAR_SERVER", sockname) == -1 ) {
1224   ret = -1;
1225   ROAR_ERR("Error while registereing to X11", ROAR_DBG_INFO_INFO);
1226  } else {
1227   ROAR_INFO("Successfully registered to X11", ROAR_DBG_INFO_INFO);
1228  }
1229 }
1230
1231 roar_x11_disconnect(x11con);
1232
1233 return ret;
1234}
1235#endif
1236
1237// SLP:
1238void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
1239 /* return the error code in the cookie */
1240 *(SLPError*)cookie = errcode;
1241}
1242
1243int register_slp (int unreg, char * sockname) {
1244#ifdef ROAR_HAVE_LIBSLP
1245 static int regged = 0;
1246 static char * sn = NULL;
1247 SLPError err;
1248 SLPError callbackerr;
1249 SLPHandle hslp;
1250 char addr[1024];
1251 char attr[1024] = "";
1252 char * location;
1253 char * description;
1254 char * standards;
1255
1256 if ( sockname != NULL )
1257  sn = sockname;
1258
1259 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE_ROAR "://%s", sn);
1260
1261 err = SLPOpen("en", SLP_FALSE, &hslp);
1262
1263 if (err != SLP_OK) {
1264  ROAR_ERR("Error opening slp handle: Error #%i", err);
1265  return -1;
1266 }
1267
1268 if (!unreg) {
1269
1270  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
1271   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
1272   SLPClose(hslp);
1273   return -1;
1274  }
1275
1276  if ( SLPEscape(g_config->description, &description, SLP_FALSE) != SLP_OK ) {
1277   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
1278   SLPClose(hslp);
1279   return -1;
1280  }
1281
1282  standards = stds_string();
1283
1284  if ( standards == NULL ) {
1285   if ( (standards = roar_mm_malloc(1)) == NULL ) {
1286    SLPClose(hslp);
1287    return -1;
1288   }
1289   standards[0] = 0;
1290  }
1291
1292  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
1293#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1294                               "(light-channels=%i),"
1295#endif
1296                               "(standards=%s),"
1297                               "(location=%s),(description=%s)",
1298           g_sa->rate, g_sa->channels, g_sa->bits,
1299#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1300           g_light_state.channels,
1301#endif
1302           standards,
1303           location, description
1304          );
1305
1306  roar_mm_free(standards);
1307
1308  /* Register a service with SLP */
1309  err = SLPReg(hslp,
1310               addr,
1311               SLP_LIFETIME_MAXIMUM,
1312               0,
1313               attr,
1314               SLP_TRUE,
1315               register_slp_callback,
1316               &callbackerr);
1317  regged = 1;
1318 } else if ( unreg && regged ) {
1319  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
1320  regged = 0;
1321 } else {
1322  SLPClose(hslp);
1323  return -1;
1324 }
1325
1326 /* err may contain an error code that occurred as the slp library    */
1327 /* _prepared_ to make the call.                                     */
1328 if ( err != SLP_OK ) {
1329  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
1330  return -1;
1331 }
1332
1333 /* callbackerr may contain an error code (that was assigned through */
1334 /* the callback cookie) that occurred as slp packets were sent on    */
1335 /* the wire */
1336 if (callbackerr != SLP_OK) {
1337  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
1338  return -1;
1339 }
1340
1341 SLPClose(hslp);
1342 return 0;
1343#else
1344 return -1;
1345#endif
1346}
1347
1348static int auth_setup (enum roard_client_acclev none_acclev,
1349                       enum roard_client_acclev trust_acclev, int trust_root, uid_t trust_uid, gid_t trust_gid) {
1350 union auth_typeunion * key;
1351 size_t i;
1352
1353 // Info:
1354 // if a authlevel is <= ACCLEV_IDENTED we do not need to add this to the keyring
1355 // as ACCLEV_IDENTED is already gained if IDENTIFY call was done correctly.
1356
1357 if ( !(none_acclev <= ACCLEV_IDENTED) ) {
1358  if ( auth_addkey_anonymous(none_acclev) == -1 )
1359   return -1;
1360 }
1361
1362 if ( !(trust_acclev <= ACCLEV_IDENTED) ) {
1363  if ( (key = auth_regkey_simple(ROAR_AUTH_T_TRUST, trust_acclev)) == NULL )
1364   return -1;
1365
1366  // zerosize all counters.
1367  memset(key, 0, sizeof(union auth_typeunion));
1368
1369  i = 0;
1370
1371  if ( trust_uid != -1 )
1372   key->trust.uids[i++] = trust_uid;
1373
1374#ifdef ROAR_ROOT_UID
1375  if ( trust_root )
1376   key->trust.uids[i++] = ROAR_ROOT_UID;
1377#endif
1378
1379  key->trust.uids_len = i;
1380
1381  i = 0;
1382
1383  if ( trust_gid != -1 )
1384   key->trust.gids[i++] = trust_gid;
1385
1386  key->trust.gids_len = i;
1387 }
1388
1389 return 0;
1390}
1391
1392
1393// MAIN:
1394
1395#define _CKHAVEARGS(x) if ( (i + (x)) >= argc ) { \
1396                        ROAR_ERR("Option %s requires more arguments. See --help for more details.", k); \
1397                        return 70; \
1398                       }
1399
1400#ifdef ROAR_HAVE_MAIN_ARGS
1401int main (int argc, char * argv[]) {
1402#else
1403int main (void) {
1404#endif
1405#ifdef ROAR_HAVE_MAIN_ARGS
1406 enum action action = START;
1407 int i;
1408 char * k;
1409 enum output_format print_format = FORMAT_NATIVE;
1410 const struct rolestack * rolestack;
1411 const char * plugin_args = NULL;
1412#endif
1413#if defined(ROAR_SUPPORT_LISTEN) && defined(ROAR_HAVE_GETUID)
1414 char user_sock[80]  = {0};
1415#endif
1416 struct roar_audio_info sa, max_sa;
1417 struct roard_config config;
1418#if defined(ROAR_HAVE_FORK) || defined(ROAR_TARGET_WIN32)
1419 int    daemon       = 0;
1420#endif
1421 int    realtime     = 0;
1422 int    sysclocksync = 0;
1423// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
1424#ifdef ROAR_SUPPORT_LISTEN
1425 int    port       = ROAR_DEFAULT_PORT;
1426 char * sock_addr  = NULL;
1427 int    sock_proto = ROAR_PROTO_ROARAUDIO;
1428 int    sock_dir   = -1;
1429 struct roar_audio_info sock_info = {0, 0, 0, 0};
1430#endif
1431#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1432 char * s_drv     = NULL;
1433 char * s_dev     = NULL;
1434 char * s_con     = NULL;
1435 char * s_opt     = NULL;
1436 int    s_prim    = 0;
1437#endif
1438 char * o_drv     = getenv("ROAR_DRIVER");
1439 char * o_dev     = getenv("ROAR_DEVICE");
1440 char * o_opts    = NULL;
1441 int    o_prim    = 0;
1442 int    o_count   = 0;
1443#ifndef ROAR_WITHOUT_DCOMP_MIXER
1444 char * m_drv     = NULL;
1445 char * m_dev     = NULL;
1446 char * m_opts    = NULL;
1447 int    m_prim    = 0;
1448 int    m_count   = 0;
1449#endif
1450 enum roard_client_acclev none_acclev  = ACCLEV_ALL;
1451 enum roard_client_acclev trust_acclev = ACCLEV_ALL;
1452 int                      trust_root   = 1;
1453 char * af_file                        = NULL;
1454 char * af_type                        = NULL;
1455 enum af_mode af_mode                  = AF_MODE_NONE;
1456 enum roard_client_acclev af_acclev    = ACCLEV_ALL;
1457#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1458 uint32_t light_channels = LIGHT_CHANNELS_DEFAULT;
1459#endif
1460#ifdef ROAR_DEFAULT_SOCKGRP
1461 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
1462#else
1463 char * sock_grp  = NULL;
1464#endif
1465 char * sock_user = NULL;
1466#ifdef ROAR_SUPPORT_LISTEN
1467 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
1468#endif
1469#ifdef ROAR_HAVE_LIBSLP
1470 int    reg_slp   = 0;
1471#endif
1472#ifdef ROAR_HAVE_LIBX11
1473 int    reg_x11   = 0;
1474#endif
1475#ifdef ROAR_HAVE_CHROOT
1476 char * chrootdir = NULL;
1477#endif
1478#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
1479 struct group   * grp  = NULL;
1480#endif
1481#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1482 struct passwd  * pwd  = NULL;
1483#endif
1484#ifdef ROAR_HAVE_GETSERVBYNAME
1485 struct servent * serv = NULL;
1486#endif
1487 struct roar_client * self = NULL;
1488#ifdef ROAR_HAVE_LIBDNET
1489 char decnethost[80];
1490#endif
1491#ifdef SUPPORT_PIDFILE
1492 struct roar_vio_calls pidfile_vio;
1493#endif
1494
1495 ROAR_DBG("main(*): starting roard...");
1496
1497 g_standby       =  0;
1498 g_autostandby   =  0;
1499 alive           =  1;
1500#ifdef ROAR_SUPPORT_LISTEN
1501 g_no_listen     =  0;
1502#else
1503 g_terminate     =  1;
1504#endif
1505
1506 g_verbose       = ROAR_DBG_INFO_NONE;
1507
1508 _LIBROAR_IGNORE_RET(roar_profile2info(&sa, "default-server"));
1509
1510 g_sa        = &sa;
1511 g_max_sa    = &max_sa;
1512
1513 memcpy(g_max_sa, g_sa, sizeof(max_sa));
1514
1515 counters_init();
1516
1517 g_config = &config;
1518
1519 if ( init_config() == -1 ) {
1520  ROAR_ERR("Can not init default config!");
1521  return 1;
1522 }
1523
1524 // load config
1525 _LIBROAR_IGNORE_RET(roar_libroar_get_config());
1526
1527 // init notify core:
1528 // TODO: reconsider number of lists.
1529 if ( roar_notify_core_new_global(-1) == -1 ) {
1530  ROAR_ERR("Can not init notify core!");
1531  return 1;
1532 }
1533
1534 if ( roar_notify_core_register_proxy(NULL, roar_notify_proxy_std, NULL) == -1 ) {
1535  ROAR_ERR("Can not init notify core!");
1536  return 1;
1537 }
1538
1539#ifdef DEBUG
1540 // enable early in case we have DEBUG set.
1541 dbg_notify_cb_register();
1542#endif
1543
1544 rolestack_init();
1545
1546 if ( auth_init() == -1 ) {
1547  ROAR_ERR("Can not init auth subsystem!");
1548  return 1;
1549 }
1550
1551#ifdef ROAR_SUPPORT_LISTEN
1552 if ( init_listening() == -1 ) {
1553  ROAR_ERR("Can not init listening sockets!");
1554  return 1;
1555 }
1556#endif
1557
1558#ifndef ROAR_WITHOUT_DCOMP_MIDI
1559 if ( midi_init_config() == -1 ) {
1560  ROAR_ERR("Can not init MIDI config!");
1561  return 1;
1562 }
1563#endif
1564
1565#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1566 if ( ssynth_init_config() == -1 ) {
1567  ROAR_ERR("Can not init ssynth config!");
1568  return 1;
1569 }
1570#endif
1571
1572#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1573 if ( rdtcs_init_config() == -1 ) {
1574  ROAR_ERR("Can not init RDTCS config!");
1575  return 1;
1576 }
1577#endif
1578
1579 if ( plugins_preinit() == -1 ) {
1580  ROAR_ERR("Can not pre-init plugins!");
1581  return 1;
1582 }
1583
1584#ifdef ROAR_SUPPORT_LISTEN
1585#ifndef ROAR_TARGET_WIN32
1586 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
1587#else
1588 sock_addr = ROAR_DEFAULT_HOST;
1589#endif
1590
1591#ifdef ROAR_HAVE_GETUID
1592 if ( getuid() != 0 && getenv("HOME") != NULL ) {
1593/*
1594  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
1595*/
1596  roar_env_render_path_r(user_sock, sizeof(user_sock), "~/" ROAR_DEFAULT_SOCK_USER);
1597  sock_addr = user_sock;
1598  ROAR_DBG("main(*): setting sock_addr='%s'", sock_addr);
1599 }
1600#endif
1601
1602 if ( getenv("ROAR_SERVER") != NULL )
1603  sock_addr = getenv("ROAR_SERVER");
1604#endif
1605
1606 if ( clients_init() == -1 ) {
1607  ROAR_ERR("Can not init clients!");
1608  return 1;
1609 }
1610
1611 if ( streams_init() == -1 ) {
1612  ROAR_ERR("Can not init streams!");
1613  return 1;
1614 }
1615
1616 if ( (g_self_client = clients_new()) == -1 ) {
1617  ROAR_ERR("Can not create self client!");
1618  return 1;
1619 }
1620
1621#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1622 if ( sources_init() == -1 ) {
1623  ROAR_ERR("Can not init sources!");
1624  return 1;
1625 }
1626
1627 if ( (sources_set_client(g_self_client)) == -1 ) {
1628  ROAR_ERR("Can not init set source client!");
1629  return 1;
1630 }
1631#endif
1632
1633#ifdef ROAR_HAVE_MAIN_ARGS
1634 for (i = 1; i < argc; i++) {
1635  k = argv[i];
1636
1637  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
1638   usage();
1639   return 0;
1640
1641  } else if ( strcmp(k, "--start") == 0 ) {
1642   // this is a no op
1643   action = START;
1644  } else if ( strcmp(k, "--restart") == 0 ) {
1645   action = RESTART;
1646#ifdef ROAR_SUPPORT_LISTEN
1647   if ( restart_server(sock_addr, 1) == -1 ) {
1648    ROAR_WARN("Can not terminate old server (not running at %s?), will retry later.", sock_addr);
1649    action = RESTART_RETRY;
1650   }
1651#else
1652   ROAR_ERR("--restart not supported");
1653#endif
1654  } else if ( strcmp(k, "--shutdown") == 0 ) {
1655   action = SHUTDOWN;
1656  } else if ( strcmp(k, "--stop") == 0 ) {
1657   action = STOP;
1658
1659
1660  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
1661#if defined(ROAR_HAVE_FORK) || defined(ROAR_TARGET_WIN32)
1662   daemon = 1;
1663#else
1664   ROAR_ERR("--daemon not supported");
1665#endif
1666  } else if ( strcmp(k, "--verbose") == 0 ) {
1667   g_verbose++;
1668
1669  } else if ( strcmp(k, "--print-format") == 0 ) {
1670   k = argv[++i];
1671   if ( !strcasecmp(k, "native") ) {
1672    print_format = FORMAT_NATIVE;
1673   } else if ( !strcasecmp(k, "wiki") ) {
1674    print_format = FORMAT_WIKI;
1675   } else if ( !strcasecmp(k, "csv") ) {
1676    print_format = FORMAT_CSV;
1677   } else {
1678    ROAR_WARN("Unknown print format: %s", k);
1679   }
1680
1681  } else if ( strcmp(k, "--print-format") == 0 ) {
1682   _CKHAVEARGS(1);
1683
1684  } else if ( strcmp(k, "--terminate") == 0 ) {
1685   g_terminate = 1;
1686  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1687   sysclocksync = 1000;
1688  } else if ( strcmp(k, "--realtime") == 0 ) {
1689   realtime++;
1690  } else if ( strcmp(k, "--memlock") == 0 ) {
1691   _CKHAVEARGS(1);
1692   g_config->memlock_level = memlock_str2level(argv[++i]);
1693  } else if ( strcmp(k, "--chroot") == 0 ) {
1694   _CKHAVEARGS(1);
1695#ifdef ROAR_HAVE_CHROOT
1696   chrootdir = argv[++i];
1697#else
1698   ROAR_ERR("--chroot not supported");
1699   i++;
1700#endif
1701  } else if ( strcmp(k, "--setgid") == 0 ) {
1702#ifdef ROAR_HAVE_SETGID
1703   setids |= R_SETGID;
1704#else
1705   ROAR_ERR("--setgid not supported");
1706#endif
1707  } else if ( strcmp(k, "--setuid") == 0 ) {
1708#ifdef ROAR_HAVE_SETUID
1709   setids |= R_SETUID;
1710#else
1711   ROAR_ERR("--setuid not supported");
1712#endif
1713  } else if ( strcmp(k, "--location") == 0 ) {
1714   _CKHAVEARGS(1);
1715   g_config->location = argv[++i];
1716  } else if ( strcmp(k, "--description") == 0 ) {
1717   _CKHAVEARGS(1);
1718   g_config->description = argv[++i];
1719  } else if ( strcmp(k, "--contact") == 0 ) {
1720   _CKHAVEARGS(1);
1721   g_config->contact = argv[++i];
1722  } else if ( strcmp(k, "--serial") == 0 ) {
1723   _CKHAVEARGS(1);
1724   g_config->serial = argv[++i];
1725  } else if ( strcmp(k, "--uiurl") == 0 ) {
1726   _CKHAVEARGS(1);
1727   g_config->uiurl = argv[++i];
1728  } else if ( strcmp(k, "--pidfile") == 0 ) {
1729   _CKHAVEARGS(1);
1730#ifdef SUPPORT_PIDFILE
1731   pidfile = argv[++i];
1732#else
1733   ROAR_ERR("--pidfile not supported");
1734   i++;
1735#endif
1736  } else if ( strcmp(k, "--log-syslog") == 0 ) {
1737#ifdef ROAR_HAVE_SYSLOG
1738   roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
1739#else
1740   ROAR_ERR("--log-syslog not supported");
1741#endif
1742  } else if ( strcmp(k, "--script-postdown") == 0 ) {
1743   _CKHAVEARGS(1);
1744#ifdef ROAR_HAVE_SYSTEM
1745   g_config->scripts.post_shutdown = argv[++i];
1746#else
1747   ROAR_ERR("--script-postdown not supported");
1748   i++;
1749#endif
1750
1751
1752  } else if ( strcmp(k, "--plugin-load") == 0 ) {
1753   _CKHAVEARGS(1);
1754   if ( plugins_load(argv[++i], plugin_args) == -1 ) {
1755    ROAR_ERR("Can not load plugin");
1756   }
1757   plugin_args = NULL;
1758  } else if ( strcmp(k, "--plugin-args") == 0 ) {
1759   _CKHAVEARGS(1);
1760   plugin_args = argv[++i];
1761
1762  } else if ( strcmp(k, "--guest-acclev") == 0 ) {
1763   _CKHAVEARGS(1);
1764   none_acclev = clients_str2acclev(argv[++i]);
1765   if ( none_acclev == -1 ) {
1766    ROAR_ERR("Invalid access level: %s", argv[i]);
1767    return 1;
1768   }
1769  } else if ( strcmp(k, "--trust-acclev") == 0 ) {
1770   _CKHAVEARGS(1);
1771   trust_acclev = clients_str2acclev(argv[++i]);
1772   if ( trust_acclev == -1 ) {
1773    ROAR_ERR("Invalid access level: %s", argv[i]);
1774    return 1;
1775   }
1776  } else if ( strcmp(k, "--trust-root") == 0 ) {
1777   trust_root = 1;
1778  } else if ( strcmp(k, "--no-trust-root") == 0 ) {
1779   trust_root = 0;
1780
1781  } else if ( strcmp(k, "--authfile-gen") == 0 ) {
1782   _CKHAVEARGS(1);
1783   af_file = argv[++i];
1784   af_mode = AF_MODE_GEN;
1785  } else if ( strcmp(k, "--authfile-load") == 0 ) {
1786   _CKHAVEARGS(1);
1787   af_file = argv[++i];
1788   af_mode = AF_MODE_LOAD;
1789  } else if ( strcmp(k, "--authfile-type") == 0 ) {
1790   _CKHAVEARGS(1);
1791   af_type = argv[++i];
1792  } else if ( strcmp(k, "--authfile-acclev") == 0 ) {
1793   _CKHAVEARGS(1);
1794   af_acclev = clients_str2acclev(argv[++i]);
1795  } else if ( strcmp(k, "--new-authfile") == 0 ) {
1796   if ( af_mode != AF_MODE_NONE ) {
1797    if ( add_authfile(af_file, af_type, af_mode, af_acclev) == -1 ) {
1798     ROAR_ERR("main(*): adding authfile '%s' failed!", af_file);
1799    }
1800   }
1801   af_file   = NULL;
1802   af_type   = NULL;
1803   af_mode   = AF_MODE_NONE;
1804   af_acclev = ACCLEV_ALL;
1805
1806  } else if ( strcmp(k, "--list-cf") == 0 ) {
1807   print_codecfilterlist();
1808   return 0;
1809
1810  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
1811   _CKHAVEARGS(1);
1812   sa.rate = atoi(argv[++i]);
1813  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
1814   _CKHAVEARGS(1);
1815   sa.bits = atoi(argv[++i]);
1816  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
1817   _CKHAVEARGS(1);
1818   sa.channels = atoi(argv[++i]);
1819
1820  } else if ( strcmp(k, "--aiprofile") == 0 ) {
1821   _CKHAVEARGS(1);
1822   if ( roar_profile2info(&sa, argv[++i]) == -1 ) {
1823    ROAR_ERR("Unknown audio profile: %s", argv[i]);
1824    return 1;
1825   }
1826   sa.codec    = ROAR_CODEC_DEFAULT;
1827
1828  } else if ( strcmp(k, "--stream-flags") == 0 ) {
1829   _CKHAVEARGS(1);
1830   if ( update_stream_flags(argv[++i]) == -1 ) {
1831    ROAR_ERR("Can not set stream flags");
1832    return 1;
1833   }
1834
1835  } else if ( strcmp(k, "--list-rolestack") == 0 ) {
1836   print_rolestack();
1837   return 0;
1838  } else if ( strcmp(k, "--rolestack-push") == 0 ) {
1839   _CKHAVEARGS(1);
1840  if ( (rolestack = rolestack_parse(argv[++i])) == NULL ) {
1841   ROAR_ERR("Can not parse rolestack request: %s", roar_error2str(roar_error));
1842  } else {
1843   if ( rolestack_push(rolestack) == -1 ) {
1844    ROAR_ERR("Can not push request to rolestack: %s", roar_error2str(roar_error));
1845   }
1846  }
1847
1848  } else if ( strcmp(k, "--list-aiprofiles") == 0 ) {
1849   list_aiprofiles();
1850   return 0;
1851
1852  } else if ( strcmp(k, "--list-driver") == 0 ) {
1853   print_driverlist(print_format);
1854   return 0;
1855
1856  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
1857   _CKHAVEARGS(1);
1858   o_drv  = argv[++i];
1859  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
1860   _CKHAVEARGS(1);
1861   o_dev  = argv[++i];
1862  } else if ( strcmp(k, "-oO") == 0 ) {
1863   _CKHAVEARGS(1);
1864   o_opts = argv[++i];
1865  } else if ( strcmp(k, "-oP") == 0 ) {
1866   o_prim = 1;
1867  } else if ( strcmp(k, "-oN") == 0 ) {
1868   if ( action == START || action == RESTART ) {
1869    if ( output_add(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1870     o_count++;
1871
1872    o_drv  = o_dev = o_opts = NULL;
1873    o_prim = 0;
1874   }
1875
1876  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
1877   _CKHAVEARGS(1);
1878#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1879   s_drv = argv[++i];
1880#else
1881   ROAR_ERR("main(*): No support for sources compiled in");
1882   i++;
1883#endif
1884  } else if ( strcmp(k, "-S") == 0 ) {
1885   _CKHAVEARGS(1);
1886#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1887   s_dev = argv[++i];
1888#else
1889   ROAR_ERR("main(*): No support for sources compiled in");
1890   i++;
1891#endif
1892  } else if ( strcmp(k, "-sO") == 0 ) {
1893   _CKHAVEARGS(1);
1894#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1895   s_opt = argv[++i];
1896#else
1897   ROAR_ERR("main(*): No support for sources compiled in");
1898   i++;
1899#endif
1900  } else if ( strcmp(k, "-sC") == 0 ) {
1901   _CKHAVEARGS(1);
1902#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1903   s_con = argv[++i];
1904#else
1905   ROAR_ERR("main(*): No support for sources compiled in");
1906   i++;
1907#endif
1908  } else if ( strcmp(k, "-sP") == 0 ) {
1909#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1910   s_prim = 1;
1911#else
1912   ROAR_ERR("main(*): No support for sources compiled in");
1913#endif
1914  } else if ( strcmp(k, "-sN") == 0 ) {
1915#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1916   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1917    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1918   }
1919   s_opt = s_dev = s_con = NULL;
1920   s_drv = NULL;
1921   s_prim = 0;
1922#else
1923   ROAR_ERR("main(*): No support for sources compiled in");
1924#endif
1925  } else if ( strcmp(k, "--list-sources") == 0 ) {
1926#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1927   print_sourcelist();
1928   return 0;
1929#else
1930   ROAR_ERR("main(*): No support for sources compiled in");
1931   return 1;
1932#endif
1933
1934  } else if ( strcmp(k, "-m") == 0 || strcmp(k, "--mixer") == 0 ) {
1935   _CKHAVEARGS(1);
1936#ifndef ROAR_WITHOUT_DCOMP_MIXER
1937   m_drv  = argv[++i];
1938#else
1939   ROAR_ERR("main(*): No support for mixer compiled in");
1940   return 1;
1941#endif
1942  } else if ( strcmp(k, "-M") == 0 ) {
1943   _CKHAVEARGS(1);
1944#ifndef ROAR_WITHOUT_DCOMP_MIXER
1945   m_dev  = argv[++i];
1946#else
1947   ROAR_ERR("main(*): No support for mixer compiled in");
1948   return 1;
1949#endif
1950  } else if ( strcmp(k, "-mO") == 0 ) {
1951   _CKHAVEARGS(1);
1952#ifndef ROAR_WITHOUT_DCOMP_MIXER
1953   m_opts = argv[++i];
1954#else
1955   ROAR_ERR("main(*): No support for mixer compiled in");
1956   return 1;
1957#endif
1958  } else if ( strcmp(k, "-mP") == 0 ) {
1959#ifndef ROAR_WITHOUT_DCOMP_MIXER
1960   m_prim = 1;
1961#else
1962   ROAR_ERR("main(*): No support for mixer compiled in");
1963   return 1;
1964#endif
1965  } else if ( strcmp(k, "-mN") == 0 ) {
1966#ifndef ROAR_WITHOUT_DCOMP_MIXER
1967   if ( hwmixer_add(m_drv, m_dev, m_opts, m_prim, m_count) != -1 )
1968    m_count++;
1969
1970   m_drv  = o_dev = o_opts = NULL;
1971   m_prim = 0;
1972#else
1973   ROAR_ERR("main(*): No support for mixer compiled in");
1974   return 1;
1975#endif
1976  } else if ( strcmp(k, "--list-mixers") == 0 ) {
1977#ifndef ROAR_WITHOUT_DCOMP_MIXER
1978   print_hwmixerlist();
1979   return 0;
1980#else
1981   ROAR_ERR("main(*): No support for mixer compiled in");
1982   return 1;
1983#endif
1984
1985  } else if ( strcmp(k, "--light-channels") == 0 ) {
1986   _CKHAVEARGS(1);
1987#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1988   light_channels = atoi(argv[++i]);
1989#else
1990   ROAR_WARN("main(*): no light subsystem compiled in");
1991   i++;
1992#endif
1993
1994  } else if ( strcmp(k, "--rds-pi") == 0 ) {
1995   _CKHAVEARGS(1);
1996#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1997   g_rdtcs.rds.pi = atoi(argv[++i]);
1998#else
1999   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2000   i++;
2001#endif
2002  } else if ( strcmp(k, "--rds-ps") == 0 ) {
2003   _CKHAVEARGS(1);
2004#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2005   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
2006    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]);
2007    return 1;
2008   }
2009#else
2010   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2011   i++;
2012#endif
2013  } else if ( strcmp(k, "--rds-pty") == 0 ) {
2014   _CKHAVEARGS(1);
2015#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2016   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
2017    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
2018    return 1;
2019   }
2020#else
2021   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2022   i++;
2023#endif
2024  } else if ( strcmp(k, "--rds-tp") == 0 ) {
2025#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2026   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) {
2027    ROAR_ERR("Can not set RDS TP flag");
2028    return 1;
2029   }
2030#else
2031   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2032#endif
2033  } else if ( strcmp(k, "--rds-ct") == 0 ) {
2034#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2035   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) {
2036    ROAR_ERR("Can not set RDS CT flag");
2037    return 1;
2038   }
2039#else
2040   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2041#endif
2042
2043
2044  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
2045#ifndef ROAR_WITHOUT_DCOMP_CB
2046   midi_config.init_cb = 0;
2047#else
2048   // no warning here as this is the disable option
2049#endif
2050  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
2051#ifndef ROAR_WITHOUT_DCOMP_CB
2052   midi_config.init_cb = 1;
2053#else
2054   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
2055#endif
2056  } else if ( strcmp(k, "--midi-console") == 0 ) {
2057   _CKHAVEARGS(1);
2058#ifndef ROAR_WITHOUT_DCOMP_CB
2059   midi_config.console_dev = argv[++i];
2060   midi_config.init_cb = 1;
2061#else
2062   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
2063   i++;
2064#endif
2065
2066  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
2067#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2068   ssynth_conf.enable = 1;
2069#else
2070   ROAR_ERR("main(*): No support for ssynth compiled in");
2071#endif
2072  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
2073#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2074   ssynth_conf.enable = 0;
2075#else
2076   // we can safely ignore the disable
2077#endif
2078
2079  } else if ( strcmp(k, "--x11-display") == 0 || strcmp(k, "--display") == 0 ) {
2080   _CKHAVEARGS(1);
2081#ifdef ROAR_HAVE_LIBX11
2082   x11display = argv[++i];
2083#else
2084   ROAR_ERR("No X11 support compiled in!");
2085   return 1;
2086#endif
2087
2088
2089  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
2090   _CKHAVEARGS(1);
2091   // This is only useful in INET not UNIX mode.
2092#ifdef ROAR_SUPPORT_LISTEN
2093   if ( *sock_addr == '/' )
2094    sock_addr = ROAR_DEFAULT_HOST;
2095
2096   errno = 0;
2097   if ( (port = atoi(argv[++i])) < 1 ) {
2098#ifdef ROAR_HAVE_GETSERVBYNAME
2099    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
2100     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
2101     return 1;
2102    }
2103    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
2104    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
2105            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
2106    port = ROAR_NET2HOST16(serv->s_port);
2107#else
2108    ROAR_ERR("invalite port number: %s", argv[i]);
2109    return 1;
2110#endif
2111   }
2112#endif
2113  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
2114   _CKHAVEARGS(1);
2115#ifdef ROAR_SUPPORT_LISTEN
2116   sock_addr = argv[++i];
2117#else
2118   i++;
2119#endif
2120
2121  } else if ( strcmp(k, "--proto") == 0 ) {
2122#ifdef ROAR_SUPPORT_LISTEN
2123   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
2124    ROAR_ERR("Unknown protocol: %s", argv[i]);
2125    return 1;
2126   }
2127#endif
2128  } else if ( strcmp(k, "--proto-dir") == 0 ) {
2129   _CKHAVEARGS(1);
2130#ifdef ROAR_SUPPORT_LISTEN
2131   if ( (sock_dir = roar_str2dir(argv[++i])) == -1 ) {
2132    ROAR_ERR("Unknown stream direction: %s", argv[i]);
2133    return 1;
2134   }
2135#else
2136   i++;
2137#endif
2138  } else if ( strcmp(k, "--proto-rate") == 0 ) {
2139   _CKHAVEARGS(1);
2140#ifdef ROAR_SUPPORT_LISTEN
2141   sock_info.rate = atoi(argv[++i]);
2142#else
2143   i++;
2144#endif
2145  } else if ( strcmp(k, "--proto-bits") == 0 ) {
2146   _CKHAVEARGS(1);
2147#ifdef ROAR_SUPPORT_LISTEN
2148   sock_info.bits = atoi(argv[++i]);
2149#else
2150   i++;
2151#endif
2152  } else if ( strcmp(k, "--proto-chans") == 0 ) {
2153   _CKHAVEARGS(1);
2154#ifdef ROAR_SUPPORT_LISTEN
2155   sock_info.channels = atoi(argv[++i]);
2156#else
2157   i++;
2158#endif
2159  } else if ( strcmp(k, "--proto-codec") == 0 ) {
2160   _CKHAVEARGS(1);
2161#ifdef ROAR_SUPPORT_LISTEN
2162   if ( (sock_info.codec = roar_str2codec(argv[++i])) == -1 ) {
2163    ROAR_ERR("Unknown codec: %s", argv[i]);
2164    return 1;
2165   }
2166#else
2167   i++;
2168#endif
2169  } else if ( strcmp(k, "--proto-aiprofile") == 0 ) {
2170   _CKHAVEARGS(1);
2171#ifdef ROAR_SUPPORT_LISTEN
2172   if ( roar_profile2info(&sock_info, argv[++i]) == -1 ) {
2173    ROAR_ERR("Unknown audio profile: %s", argv[i]);
2174    return 1;
2175   }
2176#else
2177   i++;
2178#endif
2179  } else if ( strcmp(k, "--list-profiles") == 0 ) {
2180#ifdef ROAR_SUPPORT_LISTEN
2181   listen_listen_profiles();
2182   return 0;
2183#endif
2184  } else if ( strcmp(k, "--proto-profile") == 0 ) {
2185   _CKHAVEARGS(1);
2186#ifdef ROAR_SUPPORT_LISTEN
2187   if ( get_listen_profile(argv[++i], &port, &sock_addr, &sock_type, &sock_proto, &sock_dir, &sock_info) == -1 ) {
2188    ROAR_ERR("Unknown listen profile: %s", argv[i]);
2189    return 1;
2190   }
2191#else
2192   i++;
2193#endif
2194
2195
2196  } else if ( strcmp(k, "--list-proto") == 0 ) {
2197   print_protolist(print_format);
2198   return 0;
2199
2200  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
2201#ifdef ROAR_SUPPORT_LISTEN
2202   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
2203    sock_type = ROAR_SOCKET_TYPE_TCP;
2204
2205   if ( *sock_addr == '/' )
2206    sock_addr = ROAR_DEFAULT_HOST;
2207#endif
2208
2209  } else if ( strcmp(k, "-4") == 0 ) {
2210#ifdef ROAR_SUPPORT_LISTEN
2211   sock_type = ROAR_SOCKET_TYPE_TCP;
2212   if ( *sock_addr == '/' )
2213    sock_addr = ROAR_DEFAULT_HOST;
2214#endif
2215  } else if ( strcmp(k, "-6") == 0 ) {
2216#ifdef ROAR_SUPPORT_LISTEN
2217#ifdef AF_INET6
2218   sock_type = ROAR_SOCKET_TYPE_TCP6;
2219   if ( *sock_addr == '/' )
2220    sock_addr = ROAR_DEFAULT_HOST;
2221#else
2222    ROAR_ERR("No IPv6 support compiled in!");
2223    return 1;
2224#endif
2225#endif
2226
2227  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
2228#ifdef ROAR_SUPPORT_LISTEN
2229   // ignore this case as it is the default behavor.
2230   sock_type = ROAR_SOCKET_TYPE_UNIX;
2231#endif
2232
2233  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
2234#ifdef ROAR_SUPPORT_LISTEN
2235#ifdef ROAR_HAVE_LIBDNET
2236    port   = ROAR_DEFAULT_NUM;
2237    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
2238    sock_addr = decnethost;
2239    sock_type = ROAR_SOCKET_TYPE_DECNET;
2240#else
2241    ROAR_ERR("No DECnet support compiled in!");
2242    return 1;
2243#endif
2244#endif
2245  } else if ( strcmp(k, "--new-sock") == 0 ) {
2246#ifdef ROAR_SUPPORT_LISTEN
2247   if ( action == START || action == RESTART ) {
2248    if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
2249     ROAR_ERR("Can not open listen socket!");
2250     return 1;
2251    }
2252   }
2253#endif
2254
2255  } else if ( strcmp(k, "--slp") == 0 ) {
2256#ifdef ROAR_HAVE_LIBSLP
2257   reg_slp = 1;
2258#else
2259   ROAR_ERR("No OpenSLP support compiled in!");
2260   return 1;
2261#endif
2262
2263  } else if ( strcmp(k, "--x11") == 0 ) {
2264#ifdef ROAR_HAVE_LIBX11
2265   reg_x11 = 1;
2266#else
2267   ROAR_ERR("No X11 support compiled in!");
2268   return 1;
2269#endif
2270
2271
2272  } else if ( strcmp(k, "--jumbo-mtu") == 0 ) {
2273   _CKHAVEARGS(1);
2274   g_config->jumbo_mtu = atoi(argv[++i]);
2275
2276  } else if ( strcmp(k, "-G") == 0 ) {
2277   _CKHAVEARGS(1);
2278   sock_grp  = argv[++i];
2279  } else if ( strcmp(k, "-U") == 0 ) {
2280   _CKHAVEARGS(1);
2281   sock_user = argv[++i];
2282
2283  } else if ( strcmp(k, "--no-listen") == 0 ) {
2284#ifdef ROAR_SUPPORT_LISTEN
2285   sock_addr   = "";
2286   g_terminate = 1;
2287   g_no_listen = 1;
2288#endif
2289  } else if ( strcmp(k, "--client-fh") == 0 ) {
2290   _CKHAVEARGS(1);
2291   if ( clients_new_from_fh(atoi(argv[++i]), ROAR_PROTO_ROARAUDIO, ROAR_BYTEORDER_NETWORK, 1) == -1 ) {
2292    ROAR_ERR("main(*): Can not set client's fh");
2293    return 1;
2294   }
2295  } else if ( strcmp(k, "--close-fh") == 0 ) {
2296   _CKHAVEARGS(1);
2297#ifdef ROAR_HAVE_IO_POSIX
2298   close(atoi(argv[++i]));
2299#else
2300   i++;
2301   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
2302#endif
2303
2304  } else if ( strcmp(k, "--standby") == 0 ) {
2305   g_standby = 1;
2306  } else if ( strcmp(k, "--auto-standby") == 0 ) {
2307   g_autostandby = 1;
2308  } else {
2309   usage();
2310   return 1;
2311  }
2312
2313 }
2314#endif
2315
2316#ifdef ROAR_HAVE_MAIN_ARGS
2317 switch (action) {
2318  case START:
2319    // NO-OP.
2320   break;
2321  case RESTART:
2322    // NO-OP, done before.
2323   break;
2324  case STOP:
2325#ifdef ROAR_SUPPORT_LISTEN
2326    if ( restart_server(sock_addr, 0) == -1 ) {
2327     ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
2328     return 1;
2329    }
2330    return 0;
2331#else
2332    ROAR_ERR("--stop not supported");
2333    return 1;
2334#endif
2335   break;
2336  case SHUTDOWN:
2337#ifdef ROAR_SUPPORT_LISTEN
2338    if ( restart_server(sock_addr, 1) == -1 ) {
2339     ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
2340     return 1;
2341    }
2342    return 0;
2343#else
2344    ROAR_ERR("--shutdown not supported");
2345    return 1;
2346#endif
2347   break;
2348  case RESTART_RETRY:
2349#ifdef ROAR_SUPPORT_LISTEN
2350    if ( restart_server(sock_addr, 1) == -1 ) {
2351     ROAR_WARN("Can not terminate old server (again) (not running at %s?), tring to continue anyway", sock_addr);
2352     action = RESTART;
2353    }
2354#endif
2355   break;
2356 }
2357#endif
2358
2359#ifndef DEBUG
2360 // notify dbg if requested, if in DEBUG mode
2361 // do not able because it got enabled early.
2362 if ( g_verbose >= 4 )
2363  dbg_notify_cb_register();
2364#endif
2365
2366 if ( af_mode != AF_MODE_NONE ) {
2367  if ( add_authfile(af_file, af_type, af_mode, af_acclev) == -1 ) {
2368   ROAR_ERR("main(*): adding authfile '%s' failed!", af_file);
2369  }
2370 }
2371
2372#ifndef ROAR_WITHOUT_DCOMP_MIXER
2373 if ( m_drv != NULL ) {
2374  if ( hwmixer_add(m_drv, m_dev, m_opts, m_prim, m_count) == -1 ) {
2375   ROAR_ERR("main(*): adding mixer '%s' via '%s' failed!", m_dev, m_drv);
2376  }
2377 }
2378#endif
2379
2380#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2381 if ( s_drv != NULL || s_dev != NULL ) {
2382  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
2383   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
2384  }
2385 }
2386#endif
2387
2388 if ( output_add_default(o_drv, o_dev, o_opts, o_prim, o_count) == -1 ) {
2389  ROAR_ERR("Can not initialize default driver");
2390  return 1;
2391 }
2392
2393 ROAR_INFO("Server config: rate=%i, bits=%i, chans=%i", ROAR_DBG_INFO_NOTICE, sa.rate, sa.bits, sa.channels);
2394
2395 if ( waveform_init() == -1 ) {
2396  ROAR_ERR("Can not initialize Waveform subsystem");
2397  return 1;
2398 }
2399
2400#ifndef ROAR_WITHOUT_DCOMP_MIDI
2401 if ( midi_init() == -1 ) {
2402  ROAR_ERR("Can not initialize MIDI subsystem");
2403 }
2404#endif
2405
2406#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2407 if ( ssynth_init() == -1 ) {
2408  ROAR_ERR("Can not initialize ssynth subsystem");
2409 }
2410#endif
2411
2412#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2413 if ( light_init(light_channels) == -1 ) {
2414  ROAR_ERR("Can not initialize light control subsystem");
2415 }
2416#endif
2417
2418#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2419 if ( rdtcs_init() == -1 ) {
2420  ROAR_ERR("Can not initialize RDTCS subsystem");
2421 }
2422#endif
2423
2424 if ( plugins_init() == -1 ) {
2425  ROAR_ERR("Can not initialize plugins");
2426 }
2427
2428#ifdef ROAR_SUPPORT_LISTEN
2429 if ( !g_no_listen ) {
2430  if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
2431   ROAR_ERR("Can not open listen socket!");
2432   return 1;
2433  }
2434 }
2435#endif
2436
2437 if ( output_buffer_init(&sa) == -1 ) {
2438  ROAR_ERR("Can not init output buffer!");
2439  return 1;
2440 }
2441
2442 if ( samples_init() == -1 ) {
2443  ROAR_ERR("Can not init samples!");
2444  return 1;
2445 }
2446
2447 if ( check_listen() == -1 ) {
2448  ROAR_ERR("Can not check listen sockets. BAD.");
2449  return 1;
2450 }
2451
2452 // we should handle this on microcontrollers, too.
2453#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
2454 signal(SIGINT,  on_sig_int);
2455 signal(SIGTERM, on_sig_term);
2456 signal(SIGCHLD, on_sig_chld);
2457 signal(SIGUSR1, on_sig_usr1);
2458 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
2459#endif
2460
2461 if ( g_config->memlock_level == -1 ) {
2462  g_config->memlock_level = MEMLOCK_DEFAULT;
2463 }
2464
2465 if ( memlock_set_level(g_config->memlock_level) == -1 ) {
2466  ROAR_WARN("Can not set memory locking level to target level.");
2467 }
2468
2469 if ( realtime ) {
2470#ifdef DEBUG
2471  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compile without -DDEBUG");
2472#endif
2473
2474#ifdef ROAR_HAVE_NICE
2475  // this stupid error check is because type of returned data of nice() changed
2476  // too often. On some systems it may return 0/-1, on some new nice value or
2477  // mixed forms of both.
2478  errno = 0;
2479  (void)nice(-5*realtime); // -5 for each --realtime
2480  if ( errno ) {
2481   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2482  }
2483#else
2484  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2485#endif
2486/*
2487#ifdef __linux__
2488  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
2489   ROAR_WARN("Can not set io priority: %s", strerror(errno));
2490#endif
2491*/
2492 }
2493
2494#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
2495 if ( setids & R_SETGID ) {
2496  if ( sock_grp == NULL ) {
2497   ROAR_ERR("Can not set GID if no groupname is supplied");
2498   return 1;
2499  }
2500  if ( (grp = getgrnam(sock_grp)) == NULL ) {
2501   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
2502   return 1;
2503  }
2504  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
2505   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
2506  }
2507  if ( grp == NULL || setgid(grp->gr_gid) == -1 ) {
2508   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
2509  }
2510 }
2511#endif
2512
2513
2514 clients_set_pid(g_self_client, getpid());
2515#ifdef ROAR_HAVE_GETUID
2516 clients_set_uid(g_self_client, getuid());
2517#endif
2518#ifdef ROAR_HAVE_GETGID
2519 clients_set_gid(g_self_client, getgid());
2520#endif
2521 clients_get(g_self_client, &self);
2522
2523 if ( self == NULL ) {
2524  ROAR_ERR("Can not get self client!");
2525  return 1;
2526 }
2527
2528 strcpy(self->name, "RoarAudio daemon internal");
2529
2530 if ( roar_nnode_free(&(self->nnode)) == -1 )
2531  return 1;
2532
2533 // not fully correct but ok as workaorund
2534 // so tools assume that roard runs on the same machine as
2535 // they in case they use AF_UNIX:
2536 if ( roar_nnode_new(&(self->nnode), ROAR_SOCKET_TYPE_UNIX) == -1 )
2537  return 1;
2538
2539#if defined(ROAR_HAVE_FORK) || defined(ROAR_TARGET_WIN32)
2540 if ( daemon ) {
2541#ifdef ROAR_HAVE_SYSLOG
2542  roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
2543#else
2544  roar_debug_set_stderr_fh(-1);
2545#endif
2546
2547#ifdef ROAR_TARGET_WIN32
2548  FreeConsole();
2549#else
2550
2551  close(ROAR_STDIN );
2552  close(ROAR_STDOUT);
2553  close(ROAR_STDERR);
2554
2555  if ( fork() )
2556   ROAR_U_EXIT(0);
2557
2558#ifdef ROAR_HAVE_SETSID
2559  setsid();
2560#endif
2561  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
2562#endif
2563 }
2564#endif
2565
2566#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2567 // early test for UID as we need this for the pidfile and the setuid()
2568 if ( sock_user != NULL ) {
2569  if ( (pwd = getpwnam(sock_user)) == NULL ) {
2570   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
2571   return 1;
2572  }
2573 }
2574#endif
2575
2576 ROAR_INFO("Process ID: %i", ROAR_DBG_INFO_INFO, (int)getpid());
2577
2578#ifdef SUPPORT_PIDFILE
2579 if ( pidfile != NULL ) {
2580  if ( roar_vio_open_dstr_simple(&pidfile_vio, pidfile, O_WRONLY|O_CREAT) == -1 ) {
2581   ROAR_ERR("Can not write pidfile: %s", pidfile);
2582  } else {
2583   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
2584   roar_vio_close(&pidfile_vio);
2585  }
2586#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2587  if ( pwd != NULL || grp != NULL ) {
2588   if ( chown(pidfile, pwd != NULL ? pwd->pw_uid : -1, grp != NULL ? grp->gr_gid : -1) == -1 ) {
2589    ROAR_WARN("Can not change ownership of pidfile: %s: %s", pidfile, strerror(errno));
2590   }
2591  }
2592  if ( chmod(pidfile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1 ) {
2593   ROAR_WARN("Can not change permissions of pidfile: %s: %s", pidfile, strerror(errno));
2594  }
2595#endif
2596 }
2597#endif
2598
2599#ifdef ROAR_HAVE_CHROOT
2600 if (chrootdir) {
2601  if ( chroot(chrootdir) == -1 ) {
2602   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
2603   return 2;
2604  }
2605  if ( chdir("/") == -1 ) {
2606   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
2607   return 2;
2608  }
2609 }
2610#endif
2611
2612#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2613 if ( setids & R_SETUID ) {
2614  if ( sock_user == NULL ) {
2615   ROAR_ERR("Can not set UID if no username is supplied");
2616   return 1;
2617  }
2618  if ( pwd == NULL || setuid(pwd->pw_uid) == -1 ) {
2619   ROAR_ERR("Can not set UserID: %s", strerror(errno));
2620   return 3;
2621  }
2622#ifdef ROAR_HAVE_GETUID
2623  clients_set_uid(g_self_client, getuid());
2624#endif
2625 }
2626#endif
2627
2628 // setup auth:
2629#if defined(ROAR_HAVE_GETUID) && defined(ROAR_HAVE_GETGID)
2630 if ( auth_setup(none_acclev, trust_acclev, trust_root, getuid(), getgid()) == -1 ) {
2631#else
2632 if ( auth_setup(none_acclev, trust_acclev, trust_root, -1, -1) == -1 ) {
2633#endif
2634  ROAR_ERR("Can not set up auth. Bad.");
2635  alive = 0;
2636 }
2637
2638 // Register with OpenSLP:
2639#ifdef ROAR_HAVE_LIBSLP
2640 if ( reg_slp ) {
2641  register_slp(0, sock_addr);
2642 }
2643#endif
2644
2645#ifdef ROAR_HAVE_LIBX11
2646 if ( reg_x11 ) {
2647  register_x11(0, sock_addr);
2648 }
2649#endif
2650
2651 // update sync counter.
2652 streams_change_sync_num(-1, 0);
2653
2654 ROAR_INFO("Startup complet", ROAR_DBG_INFO_INFO);
2655
2656 // start main loop...
2657 ROAR_INFO("Entering main loop", ROAR_DBG_INFO_INFO);
2658 main_loop(&sa, sysclocksync);
2659 ROAR_INFO("Left main loop", ROAR_DBG_INFO_INFO);
2660
2661 // clean up.
2662 clean_quit_prep();
2663 output_buffer_free();
2664
2665 roar_notify_core_free(NULL);
2666
2667 ROAR_INFO("Shuting down complete", ROAR_DBG_INFO_INFO);
2668
2669#ifdef ROAR_HAVE_SYSTEM
2670 if ( g_config->scripts.post_shutdown != NULL )
2671  system(g_config->scripts.post_shutdown);
2672#endif
2673
2674 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
2675 return 0;
2676}
2677
2678void cleanup_listen_socket (int terminate) {
2679 int i;
2680
2681 ROAR_DBG("cleanup_listen_socket(terminate=%i) = (void)?", terminate);
2682
2683 ROAR_INFO("Cleaning up listen sockets", ROAR_DBG_INFO_INFO);
2684
2685 // Deregister from SLP:
2686#ifdef ROAR_HAVE_LIBSLP
2687 register_slp(1, NULL);
2688#endif
2689
2690#ifdef ROAR_HAVE_LIBX11
2691 register_x11(1, NULL);
2692#endif
2693
2694#ifdef ROAR_SUPPORT_LISTEN
2695 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
2696  if ( g_listen[i].used  ) {
2697   roar_vio_close(&(g_listen[i].sock));
2698
2699   g_listen[i].used = 0;
2700
2701#ifdef ROAR_HAVE_UNIX
2702   if ( server[i] != NULL )
2703    if ( *(server[i]) == '/' )
2704     unlink(server[i]);
2705#endif
2706  }
2707 }
2708
2709#endif
2710
2711 if ( terminate )
2712  g_terminate = 1;
2713}
2714
2715void clean_quit_prep (void) {
2716 cleanup_listen_socket(0);
2717
2718 plugins_free();
2719
2720#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2721 sources_free();
2722#endif
2723 streams_free();
2724 clients_free();
2725#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2726 ssynth_free();
2727#endif
2728#ifndef ROAR_WITHOUT_DCOMP_CB
2729 midi_cb_stop(); // stop console beep
2730#endif
2731#ifndef ROAR_WITHOUT_DCOMP_MIDI
2732 midi_free();
2733#endif
2734#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2735 light_free();
2736#endif
2737#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2738 rdtcs_free();
2739#endif
2740
2741 waveform_free();
2742
2743#ifdef SUPPORT_PIDFILE
2744 if ( pidfile != NULL )
2745  unlink(pidfile);
2746#endif
2747
2748 auth_free();
2749}
2750
2751void clean_quit (void) {
2752 ROAR_INFO("Shuting down", ROAR_DBG_INFO_INFO);
2753
2754 counters_print(ROAR_DEBUG_TYPE_INFO, 0);
2755
2756 clean_quit_prep();
2757// output_buffer_free();
2758
2759 roar_notify_core_free(NULL);
2760
2761 ROAR_INFO("Shuting down complete", ROAR_DBG_INFO_INFO);
2762
2763#ifdef ROAR_HAVE_SYSTEM
2764 if ( g_config->scripts.post_shutdown != NULL )
2765  system(g_config->scripts.post_shutdown);
2766#endif
2767
2768 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
2769 exit(0);
2770}
2771
2772//ll
Note: See TracBrowser for help on using the repository browser.