source: roaraudio/roard/roard.c @ 5651:b0e163908378

Last change on this file since 5651:b0e163908378 was 5651:b0e163908378, checked in by phi, 12 years ago

Added compiler checks for printf() like format strings.

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