source: roaraudio/roard/roard.c @ 5754:b23d79c13370

Last change on this file since 5754:b23d79c13370 was 5754:b23d79c13370, checked in by phi, 11 years ago

avoid getenv() and use more portable roar_env_get(). Also improved security as we enfore const now on bufferes obtained from the env.

File size: 79.6 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 const 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, const 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, const 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    if ( roar_env_render_path_r(buf, sizeof(buf), p->sockaddr) == -1 )
801     continue;
802   } else {
803    strncpy(buf, p->sockaddr, sizeof(buf));
804   }
805   *sockaddr = buf;
806   *type     = p->type;
807
808   *proto    = p->proto;
809   *dir      = p->dir;
810
811   if ( p->aiprofile != NULL ) {
812    if ( roar_profile2info(info, p->aiprofile) == -1 ) {
813     ROAR_ERR("Unknown audio profile: %s", p->aiprofile);
814     return -1;
815    }
816   }
817   return 0;
818  }
819 }
820
821 return -1;
822}
823#endif
824
825int add_listen (const char * addr, int port, int sock_type, char * user, char * group, int proto, int dir, struct roar_audio_info * info) {
826#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
827 struct group   * grp  = NULL;
828#endif
829#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
830 struct passwd  * pwd  = NULL;
831#endif
832#ifdef ROAR_HAVE_UNIX
833 char * env_roar_proxy_backup;
834#endif
835 int    sockid = -1;
836#ifdef ROAR_HAVE_UNIX
837 int    sock;
838#endif
839 int    i;
840
841 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",
842            ROAR_DBG_INFO_INFO,
843            addr, port, sock_type, user, group, roar_proto2str(proto), proto, roar_dir2str(dir), dir,
844            info->rate, info->bits, info->channels, roar_codec2str(info->codec), info->codec);
845
846 if ( *addr != 0 ) {
847  for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
848   if ( ! g_listen[i].used ) {
849    sockid = i;
850    break;
851   }
852  }
853
854  if ( sockid == -1 )
855   return -1;
856
857  g_listen[sockid].proto = proto;
858
859  ROAR_DBG("add_listen(*): proto=0x%.4x", proto);
860
861  if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) {
862#ifdef ROAR_HAVE_UNIX
863   if ( *addr == '/' ) {
864    if ( (env_roar_proxy_backup = (char*)roar_env_get("ROAR_PROXY")) != NULL ) {
865     env_roar_proxy_backup = roar_mm_strdup(env_roar_proxy_backup);
866     unsetenv("ROAR_PROXY");
867    }
868    if ( (sock = roar_socket_connect(ROAR_SOCKET_TYPE_UNKNOWN, addr, port)) != -1 ) {
869     close(sock);
870     ROAR_ERR("Can not open listen socket: Socket allready in use");
871     return -1;
872    } else {
873     unlink(addr);
874     if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) {
875      ROAR_ERR("Can not open listen socket: %s", strerror(errno));
876      return -1;
877     }
878    }
879    if ( env_roar_proxy_backup != NULL ) {
880     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
881     roar_mm_free(env_roar_proxy_backup);
882    }
883#else
884   if (0) { // noop
885#endif
886   } else {
887    ROAR_ERR("Can not open listen socket: %s", strerror(errno));
888    return -1;
889   }
890  }
891
892  ROAR_DBG("add_listen(*) = ?");
893
894#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
895  if ( group != NULL ) {
896   if ( (grp = getgrnam(group)) == NULL ) {
897    ROAR_ERR("Can not get GID for group %s: %s", group, strerror(errno));
898   }
899  }
900#endif
901
902  ROAR_DBG("add_listen(*) = ?");
903
904#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
905  if ( user != NULL ) {
906   if ( (pwd = getpwnam(user)) == NULL ) {
907    ROAR_ERR("Can not get UID for user %s: %s", user, strerror(errno));
908   }
909  }
910#endif
911
912  ROAR_DBG("add_listen(*) = ?");
913
914#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
915  if ( *addr == '/' ) {
916   if ( grp != NULL || pwd != NULL ) {
917     if ( chown(addr, pwd != NULL ? pwd->pw_uid : (uid_t)-1, grp != NULL ? grp->gr_gid : (gid_t)-1) == -1 )
918      return -1;
919   }
920#ifdef ROAR_HAVE_GETUID
921   if ( grp != NULL ) {
922    if ( getuid() == 0 )
923     if ( chmod(addr, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
924      return -1;
925   }
926#endif
927  }
928#endif
929 }
930
931 ROAR_DBG("add_listen(*) = ?");
932
933 // in case we opened the listening socket correctly.
934 if ( dir == -1 )
935  dir = ROAR_DIR_PLAY;
936
937 g_listen[sockid].inst.stpl.dir = dir;
938 memcpy(&(g_listen[sockid].inst.stpl.info), info, sizeof(struct roar_audio_info));
939
940 switch (dir) {
941  case ROAR_DIR_PLAY:
942  case ROAR_DIR_RECORD:
943  case ROAR_DIR_MONITOR:
944  case ROAR_DIR_FILTER:
945  case ROAR_DIR_BIDIR:
946  case ROAR_DIR_RECPLAY:
947    if ( !g_listen[sockid].inst.stpl.info.rate )
948     g_listen[sockid].inst.stpl.info.rate = g_sa->rate;
949
950    if ( !g_listen[sockid].inst.stpl.info.bits )
951     g_listen[sockid].inst.stpl.info.bits = g_sa->bits;
952
953    if ( !g_listen[sockid].inst.stpl.info.channels )
954     g_listen[sockid].inst.stpl.info.channels = g_sa->channels;
955
956    if ( !g_listen[sockid].inst.stpl.info.codec )
957     g_listen[sockid].inst.stpl.info.codec = g_sa->codec;
958   break;
959 }
960
961 g_listen[sockid].used = 1;
962 server[sockid]        = addr;
963
964 return 0;
965}
966
967static int check_listen(void) {
968 const struct roard_proto_handle * proto;
969 const char * protoname;
970 char buffer[80];
971 size_t i, j;
972
973 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
974  if ( g_listen[i].used ) {
975   proto = clients_get_protohandle(g_listen[i].proto);
976   if ( proto != NULL )
977    continue;
978   protoname = roar_proto2str(g_listen[i].proto);
979   if ( protoname == NULL ) {
980    ROAR_ERR("check_listen(void): protocol %i is unknown (protocol ID not assigned?).", g_listen[i].proto);
981   } else {
982    ROAR_DBG("check_listen(void): Unknown protocol %s(%i) is used.", protoname, g_listen[i].proto);
983    snprintf(buffer, sizeof(buffer), "protocol-%s", protoname);
984    for (j = 0; buffer[j]; j++)
985     buffer[j] = tolower(buffer[j]);
986
987    ROAR_DBG("check_listen(void): Trying to load plugin \"%s\"", buffer);
988    if ( plugins_load(buffer, NULL) == -1 ) {
989     ROAR_WARN("check_listen(void): unabled to load plugin: %s: %s", buffer, roar_errorstring);
990    }
991   }
992
993   // recheck:
994   proto = clients_get_protohandle(g_listen[i].proto);
995   if ( proto == NULL ) {
996    ROAR_WARN("check_listen(void): Protocol %s(%i) is still unknown. Deleting listen socket.", protoname, g_listen[i].proto);
997    roar_vio_close(&(g_listen[i].sock));
998#ifdef ROAR_HAVE_UNIX
999    if ( server[i] != NULL )
1000     if ( server[i][0] == '/' )
1001      unlink(server[i]);
1002#endif
1003    g_listen[i].used = 0;
1004    server[i] = NULL;
1005   }
1006  }
1007 }
1008
1009 return 0;
1010}
1011#endif
1012
1013#ifdef ROAR_HAVE_MAIN_ARGS
1014static void list_aiprofiles (void) {
1015 struct roar_audio_info info;
1016 const char * list[1024];
1017 const char * mime;
1018 ssize_t ret;
1019 ssize_t i;
1020
1021 ret = roar_profiles_list(list, 1024, 0);
1022
1023 if ( ret == -1 )
1024  return;
1025
1026 printf("  Name             Rate  Bits  Channels  Codec\n");
1027 printf("-----------------------------------------------------------------------------\n");
1028 for (i = 0; i < ret; i++) {
1029  if ( roar_profile2info(&info, list[i]) == -1 ) {
1030   printf("  %-10s --- unknown parameters ---\n", list[i]);
1031   continue;
1032  }
1033
1034  mime = roar_codec2mime(info.codec);
1035
1036  printf("  %-16s %5u %4u  %8u  %3u (%s%s%s%s)\n", list[i],
1037            info.rate, info.bits, info.channels,
1038            info.codec, roar_codec2str(info.codec),
1039            info.codec == ROAR_CODEC_DEFAULT ? " native" : "",
1040            mime == NULL ? "" : " mimetype:",
1041            mime == NULL ? "" : mime
1042        );
1043 }
1044}
1045#endif
1046
1047int update_stream_flags (char * str) {
1048 int    dir;
1049 char * flags;
1050 char * k;
1051 int    op;
1052 int    flag;
1053
1054 if ( (flags = strstr(str, "=")) == NULL )
1055  return -1;
1056
1057 *flags = 0;
1058  flags++;
1059
1060 if ( (dir = roar_str2dir(str)) == -1 )
1061  return -1;
1062
1063 while (flags != NULL) {
1064  k = flags;
1065  flags = strstr(flags, ",");
1066
1067  if ( flags != NULL )
1068   *(flags++) = 0;
1069
1070  switch (*k) {
1071   case '+': k++; op = ROAR_SET_FLAG;   break;
1072   case '-': k++; op = ROAR_RESET_FLAG; break;
1073   default:
1074     op = ROAR_SET_FLAG;
1075  }
1076
1077  flag = 0;
1078
1079  if ( !strcmp(k, "sync") ) {
1080   flag = ROAR_FLAG_SYNC;
1081  } else if ( !strcmp(k, "meta") ) {
1082   flag = ROAR_FLAG_META;
1083  } else if ( !strcmp(k, "cleanmeta") ) {
1084   flag = ROAR_FLAG_CLEANMETA;
1085  } else if ( !strcmp(k, "pause") ) {
1086   flag = ROAR_FLAG_PAUSE;
1087  } else if ( !strcmp(k, "mute") ) {
1088   flag = ROAR_FLAG_MUTE;
1089  } else if ( !strcmp(k, "antiecho") ) {
1090   flag = ROAR_FLAG_ANTIECHO;
1091  } else if ( !strcmp(k, "passmixer") ) {
1092   flag = ROAR_FLAG_PASSMIXER;
1093  } else if ( !strcmp(k, "recsource") ) {
1094   flag = ROAR_FLAG_RECSOURCE;
1095  } else {
1096   return -1;
1097  }
1098
1099  g_config->streams[dir].flags |= flag;
1100
1101  if ( op == ROAR_RESET_FLAG )
1102   g_config->streams[dir].flags -= flag;
1103 }
1104
1105 return 0;
1106}
1107
1108int add_authfile (const char * file, const char * type, enum af_mode mode, enum roard_client_acclev acclev) {
1109 struct roar_authfile * authfile = NULL;
1110 struct roar_authfile_key *  key = NULL;
1111 int af_type = ROAR_AUTHFILE_TYPE_AUTO;
1112 void * keydata;
1113 int i;
1114
1115 if ( type == NULL ) {
1116  // noop.
1117 } else if ( !strcasecmp(type, "roar") ) {
1118  af_type = ROAR_AUTHFILE_TYPE_ROAR;
1119 } else if ( !strcasecmp(type, "roar") ) {
1120  af_type = ROAR_AUTHFILE_TYPE_ROAR;
1121 } else if ( !strcasecmp(type, "esd") ) {
1122  af_type = ROAR_AUTHFILE_TYPE_ESD;
1123 } else if ( !strcasecmp(type, "pulse") ) {
1124  af_type = ROAR_AUTHFILE_TYPE_PULSE;
1125 } else if ( !strcasecmp(type, "htpasswd") ) {
1126  af_type = ROAR_AUTHFILE_TYPE_HTPASSWD;
1127 } else if ( !strcasecmp(type, "xauth") ) {
1128  af_type = ROAR_AUTHFILE_TYPE_XAUTH;
1129 } else {
1130  ROAR_ERR("add_authfile(*): unknown authfile type '%s'.", type);
1131  return -1;
1132 }
1133
1134 if ( mode == AF_MODE_GEN && af_type == ROAR_AUTHFILE_TYPE_AUTO )
1135  af_type = ROAR_AUTHFILE_TYPE_ESD;
1136
1137 switch (mode) {
1138  case AF_MODE_NONE:
1139    return 0;
1140   break;
1141  case AF_MODE_GEN:
1142    switch (af_type) {
1143     case ROAR_AUTHFILE_TYPE_ESD:
1144       key = roar_authfile_key_new_random(ROAR_AUTH_T_COOKIE, 16, NULL);
1145      break;
1146     case ROAR_AUTHFILE_TYPE_PULSE:
1147       key = roar_authfile_key_new_random(ROAR_AUTH_T_COOKIE, 256, NULL);
1148      break;
1149     default:
1150       return -1;
1151      break;
1152    }
1153
1154    if ( key == NULL ) {
1155     ROAR_ERR("add_authfile(*): Can not generate key.");
1156     return -1;
1157    }
1158
1159    if ( (authfile = roar_authfile_open(af_type, file, 1, ROAR_AUTHFILE_VERSION_AUTO)) == NULL ) {
1160     roar_authfile_key_unref(key);
1161     return -1;
1162    }
1163
1164    if ( roar_authfile_add_key(authfile, key) == -1 ) {
1165     ROAR_WARN("add_authfile(*): Can not add key to authfile.");
1166    }
1167
1168    keydata = roar_mm_memdup(key->data, key->len);
1169
1170    if ( keydata == NULL ) {
1171     ROAR_WARN("add_authfile(*): Can not allocate memory for key.");
1172    } else if ( auth_addkey_cookie(acclev, keydata, key->len) == -1 ) {
1173     ROAR_WARN("add_authfile(*): Can not add key to internal key storage.");
1174    }
1175
1176    roar_authfile_key_unref(key);
1177    if ( roar_authfile_close(authfile) != 0 )
1178     return -1;
1179    return 0;
1180   break;
1181  case AF_MODE_LOAD:
1182    if ( (authfile = roar_authfile_open(af_type, file, 0, ROAR_AUTHFILE_VERSION_AUTO)) == NULL ) {
1183     return -1;
1184    }
1185
1186    for (i = 0; ; i++) {
1187     key = roar_authfile_lookup_key(authfile, ROAR_AUTH_T_AUTO, i, NULL);
1188     if ( key == NULL )
1189      break;
1190
1191     if ( key->type == ROAR_AUTH_T_COOKIE ) {
1192      keydata = roar_mm_memdup(key->data, key->len);
1193
1194      if ( keydata == NULL ) {
1195       ROAR_WARN("add_authfile(*): Can not allocate memory for key.");
1196      } else if ( auth_addkey_cookie(acclev, keydata, key->len) == -1 ) {
1197       ROAR_WARN("add_authfile(*): Can not add key to internal key storage.");
1198      }
1199     } else {
1200      ROAR_WARN("add_authfile(*): Unknown key type: %i", key->type);
1201     }
1202
1203     roar_authfile_key_unref(key);
1204    }
1205
1206    if ( roar_authfile_close(authfile) != 0 )
1207     return -1;
1208    return 0;
1209   break;
1210 }
1211
1212 return -1;
1213}
1214
1215// X11:
1216#ifdef ROAR_HAVE_LIBX11
1217int register_x11 (int unreg, const char * sockname) {
1218 struct roar_x11_connection * x11con = NULL;
1219 int ret = 0;
1220
1221 if ( (x11con = roar_x11_connect(x11display)) == NULL ) {
1222  ROAR_ERR("Can not connect to X11 server for %sregistering", unreg ? "un" : "");
1223  return -1;
1224 }
1225
1226 if ( unreg ) {
1227  if ( roar_x11_delete_prop(x11con, "ROAR_SERVER") == -1 ) {
1228   ret = -1;
1229   ROAR_ERR("Error while unregistereing from X11");
1230  } else {
1231   ROAR_INFO("Successfully unregistered from X11", ROAR_DBG_INFO_INFO);
1232  }
1233 } else {
1234  if ( roar_x11_set_prop(x11con, "ROAR_SERVER", sockname) == -1 ) {
1235   ret = -1;
1236   ROAR_ERR("Error while registereing to X11");
1237  } else {
1238   ROAR_INFO("Successfully registered to X11", ROAR_DBG_INFO_INFO);
1239  }
1240 }
1241
1242 roar_x11_disconnect(x11con);
1243
1244 return ret;
1245}
1246#endif
1247
1248// SLP:
1249void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
1250 (void)hslp;
1251 /* return the error code in the cookie */
1252 *(SLPError*)cookie = errcode;
1253}
1254
1255int register_slp (int unreg, const char * sockname) {
1256#ifdef ROAR_HAVE_LIBSLP
1257 static int regged = 0;
1258 static const char * sn = NULL;
1259 SLPError err;
1260 SLPError callbackerr;
1261 SLPHandle hslp;
1262 char addr[1024];
1263 char attr[1024] = "";
1264 char * location;
1265 char * description;
1266 char * standards;
1267
1268 if ( sockname != NULL )
1269  sn = sockname;
1270
1271 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE_ROAR "://%s", sn);
1272
1273 err = SLPOpen("en", SLP_FALSE, &hslp);
1274
1275 if (err != SLP_OK) {
1276  ROAR_ERR("Error opening slp handle: Error #%i", err);
1277  return -1;
1278 }
1279
1280 if (!unreg) {
1281
1282  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
1283   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
1284   SLPClose(hslp);
1285   return -1;
1286  }
1287
1288  if ( SLPEscape(g_config->description, &description, SLP_FALSE) != SLP_OK ) {
1289   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
1290   SLPClose(hslp);
1291   return -1;
1292  }
1293
1294  standards = stds_string();
1295
1296  if ( standards == NULL ) {
1297   if ( (standards = roar_mm_malloc(1)) == NULL ) {
1298    SLPClose(hslp);
1299    return -1;
1300   }
1301   standards[0] = 0;
1302  }
1303
1304  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
1305#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1306                               "(light-channels=%i),"
1307#endif
1308                               "(standards=%s),"
1309                               "(location=%s),(description=%s)",
1310           g_sa->rate, g_sa->channels, g_sa->bits,
1311#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1312           g_light_state.channels,
1313#endif
1314           standards,
1315           location, description
1316          );
1317
1318  roar_mm_free(standards);
1319
1320  /* Register a service with SLP */
1321  err = SLPReg(hslp,
1322               addr,
1323               SLP_LIFETIME_MAXIMUM,
1324               0,
1325               attr,
1326               SLP_TRUE,
1327               register_slp_callback,
1328               &callbackerr);
1329  regged = 1;
1330 } else if ( unreg && regged ) {
1331  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
1332  regged = 0;
1333 } else {
1334  SLPClose(hslp);
1335  return -1;
1336 }
1337
1338 /* err may contain an error code that occurred as the slp library    */
1339 /* _prepared_ to make the call.                                     */
1340 if ( err != SLP_OK ) {
1341  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
1342  return -1;
1343 }
1344
1345 /* callbackerr may contain an error code (that was assigned through */
1346 /* the callback cookie) that occurred as slp packets were sent on    */
1347 /* the wire */
1348 if (callbackerr != SLP_OK) {
1349  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
1350  return -1;
1351 }
1352
1353 SLPClose(hslp);
1354 return 0;
1355#else
1356 return -1;
1357#endif
1358}
1359
1360static int auth_setup (enum roard_client_acclev none_acclev,
1361                       enum roard_client_acclev trust_acclev, int trust_root, uid_t trust_uid, gid_t trust_gid) {
1362 union auth_typeunion * key;
1363 size_t i;
1364
1365 // Info:
1366 // if a authlevel is <= ACCLEV_IDENTED we do not need to add this to the keyring
1367 // as ACCLEV_IDENTED is already gained if IDENTIFY call was done correctly.
1368
1369 if ( !(none_acclev <= ACCLEV_IDENTED) ) {
1370  if ( auth_addkey_anonymous(none_acclev) == -1 )
1371   return -1;
1372 }
1373
1374 if ( !(trust_acclev <= ACCLEV_IDENTED) ) {
1375  if ( (key = auth_regkey_simple(ROAR_AUTH_T_TRUST, trust_acclev)) == NULL )
1376   return -1;
1377
1378  // zerosize all counters.
1379  memset(key, 0, sizeof(union auth_typeunion));
1380
1381  i = 0;
1382
1383  if ( trust_uid != (uid_t)-1 )
1384   key->trust.uids[i++] = trust_uid;
1385
1386#ifdef ROAR_ROOT_UID
1387  if ( trust_root )
1388   key->trust.uids[i++] = ROAR_ROOT_UID;
1389#endif
1390
1391  key->trust.uids_len = i;
1392
1393  i = 0;
1394
1395  if ( trust_gid != (gid_t)-1 )
1396   key->trust.gids[i++] = trust_gid;
1397
1398  key->trust.gids_len = i;
1399 }
1400
1401 return 0;
1402}
1403
1404
1405// MAIN:
1406
1407#define _CKHAVEARGS(x) if ( (i + (x)) >= argc ) { \
1408                        ROAR_ERR("Option %s requires more arguments. See --help for more details.", k); \
1409                        return 70; \
1410                       }
1411
1412#ifdef ROAR_HAVE_MAIN_ARGS
1413int main (int argc, char * argv[]) {
1414#else
1415int main (void) {
1416#endif
1417#ifdef ROAR_HAVE_MAIN_ARGS
1418 enum metaaction metaaction = MA_ACTION;
1419 enum action action = START;
1420 int i;
1421 char * k;
1422 enum output_format print_format = FORMAT_NATIVE;
1423 const struct rolestack * rolestack;
1424 const char * plugin_args = NULL;
1425#endif
1426#if defined(ROAR_SUPPORT_LISTEN) && defined(ROAR_HAVE_GETUID)
1427 char user_sock[80]  = {0};
1428#endif
1429 struct roar_audio_info sa, max_sa;
1430 struct roard_config config;
1431#if defined(ROAR_HAVE_FORK) || defined(ROAR_TARGET_WIN32)
1432 int    daemon       = 0;
1433#endif
1434 int_least32_t watchdog_time = 0; // -1: default, 0: disabled, > 0: user set time.
1435 int    realtime     = 0;
1436 int    sysclocksync = 0;
1437// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
1438#ifdef ROAR_SUPPORT_LISTEN
1439 int    port       = ROAR_DEFAULT_PORT;
1440 const char * sock_addr  = NULL;
1441 int    sock_proto = ROAR_PROTO_ROARAUDIO;
1442 int    sock_dir   = -1;
1443 struct roar_audio_info sock_info = {0, 0, 0, 0};
1444#endif
1445#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1446 char * s_drv     = NULL;
1447 char * s_dev     = NULL;
1448 char * s_con     = NULL;
1449 char * s_opt     = NULL;
1450 int    s_prim    = 0;
1451#endif
1452 char * o_drv     = NULL;
1453 char * o_dev     = NULL;
1454 char * o_opts    = NULL;
1455 int    o_prim    = 0;
1456 int    o_count   = 0;
1457#ifndef ROAR_WITHOUT_DCOMP_MIXER
1458 char * m_drv     = NULL;
1459 char * m_dev     = NULL;
1460 char * m_opts    = NULL;
1461 int    m_prim    = 0;
1462 int    m_count   = 0;
1463#endif
1464 enum roard_client_acclev none_acclev  = ACCLEV_ALL;
1465 enum roard_client_acclev trust_acclev = ACCLEV_ALL;
1466 int                      trust_root   = 1;
1467 char * af_file                        = NULL;
1468 char * af_type                        = NULL;
1469 enum af_mode af_mode                  = AF_MODE_NONE;
1470 enum roard_client_acclev af_acclev    = ACCLEV_ALL;
1471#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1472 uint32_t light_channels = LIGHT_CHANNELS_DEFAULT;
1473#endif
1474#ifdef ROAR_DEFAULT_SOCKGRP
1475 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
1476#else
1477 char * sock_grp  = NULL;
1478#endif
1479 char * sock_user = NULL;
1480#ifdef ROAR_SUPPORT_LISTEN
1481 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
1482#endif
1483#ifdef ROAR_HAVE_LIBSLP
1484 int    reg_slp   = 0;
1485#endif
1486#ifdef ROAR_HAVE_LIBX11
1487 int    reg_x11   = 0;
1488#endif
1489#ifdef ROAR_HAVE_CHROOT
1490 char * chrootdir = NULL;
1491#endif
1492#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
1493 struct group   * grp  = NULL;
1494#endif
1495#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1496 struct passwd  * pwd  = NULL;
1497#endif
1498#ifdef ROAR_HAVE_GETSERVBYNAME
1499 struct servent * serv = NULL;
1500#endif
1501 struct roar_client * self = NULL;
1502#ifdef ROAR_HAVE_LIBDNET
1503 char decnethost[80];
1504#endif
1505#ifdef SUPPORT_PIDFILE
1506 struct roar_vio_calls pidfile_vio;
1507#endif
1508
1509 ROAR_DBG("main(*): starting roard...");
1510
1511// NOTE: those to lions can generate a memory leak. The OS will clean that up on exit.
1512 if ( roar_env_get("ROAR_DRIVER") != NULL )
1513  o_drv = roar_mm_strdup(roar_env_get("ROAR_DRIVER"));
1514 if ( roar_env_get("ROAR_DEVICE") != NULL )
1515  o_dev = roar_mm_strdup(roar_env_get("ROAR_DEVICE"));
1516
1517 g_standby       =  0;
1518 g_autostandby   =  0;
1519 alive           =  1;
1520#ifdef ROAR_SUPPORT_LISTEN
1521 g_no_listen     =  0;
1522#else
1523 g_terminate     =  1;
1524#endif
1525
1526 g_verbose       = ROAR_DBG_INFO_NONE;
1527
1528 _LIBROAR_IGNORE_RET(roar_profile2info(&sa, "default-server"));
1529
1530 g_sa        = &sa;
1531 g_max_sa    = &max_sa;
1532
1533 memcpy(g_max_sa, g_sa, sizeof(max_sa));
1534
1535 counters_init();
1536
1537 g_config = &config;
1538
1539 if ( init_config() == -1 ) {
1540  ROAR_ERR("Can not init default config!");
1541  return 1;
1542 }
1543
1544 // load config
1545 _LIBROAR_IGNORE_RET(roar_libroar_get_config());
1546
1547 // init notify core:
1548 // TODO: reconsider number of lists.
1549 if ( roar_notify_core_new_global(-1) == -1 ) {
1550  ROAR_ERR("Can not init notify core!");
1551  return 1;
1552 }
1553
1554 if ( roar_notify_core_register_proxy(NULL, roar_notify_proxy_std, NULL) == -1 ) {
1555  ROAR_ERR("Can not init notify core!");
1556  return 1;
1557 }
1558
1559#ifdef DEBUG
1560 // enable early in case we have DEBUG set.
1561 dbg_notify_cb_register();
1562#endif
1563
1564 rolestack_init();
1565
1566 if ( auth_init() == -1 ) {
1567  ROAR_ERR("Can not init auth subsystem!");
1568  return 1;
1569 }
1570
1571#ifdef ROAR_SUPPORT_LISTEN
1572 if ( init_listening() == -1 ) {
1573  ROAR_ERR("Can not init listening sockets!");
1574  return 1;
1575 }
1576#endif
1577
1578#ifndef ROAR_WITHOUT_DCOMP_MIDI
1579 if ( midi_init_config() == -1 ) {
1580  ROAR_ERR("Can not init MIDI config!");
1581  return 1;
1582 }
1583#endif
1584
1585#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1586 if ( ssynth_init_config() == -1 ) {
1587  ROAR_ERR("Can not init ssynth config!");
1588  return 1;
1589 }
1590#endif
1591
1592#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1593 if ( rdtcs_init_config() == -1 ) {
1594  ROAR_ERR("Can not init RDTCS config!");
1595  return 1;
1596 }
1597#endif
1598
1599 if ( plugins_preinit() == -1 ) {
1600  ROAR_ERR("Can not pre-init plugins!");
1601  return 1;
1602 }
1603
1604#ifdef ROAR_SUPPORT_LISTEN
1605#ifndef ROAR_TARGET_WIN32
1606 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
1607#else
1608 sock_addr = ROAR_DEFAULT_HOST;
1609#endif
1610
1611#ifdef ROAR_HAVE_GETUID
1612 if ( getuid() != 0 && roar_env_get("HOME") != NULL ) {
1613/*
1614  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
1615*/
1616  if ( roar_env_render_path_r(user_sock, sizeof(user_sock), "~/" ROAR_DEFAULT_SOCK_USER) == -1 ) {
1617   strncpy(user_sock, "/NX-SOCK", sizeof(user_sock));
1618  }
1619  sock_addr = user_sock;
1620  ROAR_DBG("main(*): setting sock_addr='%s'", sock_addr);
1621 }
1622#endif
1623
1624 if ( roar_env_get("ROAR_SERVER") != NULL )
1625  sock_addr = roar_env_get("ROAR_SERVER");
1626#endif
1627
1628 if ( clients_init() == -1 ) {
1629  ROAR_ERR("Can not init clients!");
1630  return 1;
1631 }
1632
1633 if ( streams_init() == -1 ) {
1634  ROAR_ERR("Can not init streams!");
1635  return 1;
1636 }
1637
1638 if ( (g_self_client = clients_new()) == -1 ) {
1639  ROAR_ERR("Can not create self client!");
1640  return 1;
1641 }
1642
1643#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1644 if ( sources_init() == -1 ) {
1645  ROAR_ERR("Can not init sources!");
1646  return 1;
1647 }
1648
1649 if ( (sources_set_client(g_self_client)) == -1 ) {
1650  ROAR_ERR("Can not init set source client!");
1651  return 1;
1652 }
1653#endif
1654
1655#ifdef ROAR_HAVE_MAIN_ARGS
1656 for (i = 1; i < argc; i++) {
1657  k = argv[i];
1658
1659  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
1660   usage();
1661   return 0;
1662
1663  } else if ( strcmp(k, "--start") == 0 ) {
1664   // this is a no op
1665   action = START;
1666  } else if ( strcmp(k, "--restart") == 0 ) {
1667   action = RESTART;
1668#ifdef ROAR_SUPPORT_LISTEN
1669   if ( restart_server(sock_addr, 1) == -1 ) {
1670    ROAR_WARN("Can not terminate old server (not running at %s?), will retry later.", sock_addr);
1671    action = RESTART_RETRY;
1672   }
1673#else
1674   ROAR_ERR("--restart not supported");
1675#endif
1676  } else if ( strcmp(k, "--shutdown") == 0 ) {
1677   action = SHUTDOWN;
1678  } else if ( strcmp(k, "--stop") == 0 ) {
1679   action = STOP;
1680
1681
1682  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
1683#if defined(ROAR_HAVE_FORK) || defined(ROAR_TARGET_WIN32)
1684   daemon = 1;
1685#else
1686   ROAR_ERR("--daemon not supported");
1687#endif
1688  } else if ( strcmp(k, "--verbose") == 0 ) {
1689   g_verbose++;
1690
1691  } else if ( strcmp(k, "--print-format") == 0 ) {
1692   k = argv[++i];
1693   if ( !strcasecmp(k, "native") ) {
1694    print_format = FORMAT_NATIVE;
1695   } else if ( !strcasecmp(k, "wiki") ) {
1696    print_format = FORMAT_WIKI;
1697   } else if ( !strcasecmp(k, "csv") ) {
1698    print_format = FORMAT_CSV;
1699   } else {
1700    ROAR_WARN("Unknown print format: %s", k);
1701   }
1702
1703  } else if ( strcmp(k, "--print-format") == 0 ) {
1704   _CKHAVEARGS(1);
1705
1706  } else if ( strcmp(k, "--terminate") == 0 ) {
1707   g_terminate = 1;
1708  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1709   sysclocksync = 1000;
1710  } else if ( strcmp(k, "--realtime") == 0 ) {
1711   realtime++;
1712  } else if ( strcmp(k, "--memlock") == 0 ) {
1713   _CKHAVEARGS(1);
1714   g_config->memlock_level = memlock_str2level(argv[++i]);
1715  } else if ( strcmp(k, "--watchdog") == 0 ) {
1716   if ( watchdog_time == 0 )
1717    watchdog_time = -1;
1718  } else if ( strcmp(k, "--watchdog-time") == 0 ) {
1719   _CKHAVEARGS(1);
1720    watchdog_time = atoi(argv[++i]);
1721  } else if ( strcmp(k, "--chroot") == 0 ) {
1722   _CKHAVEARGS(1);
1723#ifdef ROAR_HAVE_CHROOT
1724   chrootdir = argv[++i];
1725#else
1726   ROAR_ERR("--chroot not supported");
1727   i++;
1728#endif
1729  } else if ( strcmp(k, "--setgid") == 0 ) {
1730#ifdef ROAR_HAVE_SETGID
1731   setids |= R_SETGID;
1732#else
1733   ROAR_ERR("--setgid not supported");
1734#endif
1735  } else if ( strcmp(k, "--setuid") == 0 ) {
1736#ifdef ROAR_HAVE_SETUID
1737   setids |= R_SETUID;
1738#else
1739   ROAR_ERR("--setuid not supported");
1740#endif
1741  } else if ( strcmp(k, "--location") == 0 ) {
1742   _CKHAVEARGS(1);
1743   g_config->location = argv[++i];
1744  } else if ( strcmp(k, "--description") == 0 ) {
1745   _CKHAVEARGS(1);
1746   g_config->description = argv[++i];
1747  } else if ( strcmp(k, "--contact") == 0 ) {
1748   _CKHAVEARGS(1);
1749   g_config->contact = argv[++i];
1750  } else if ( strcmp(k, "--serial") == 0 ) {
1751   _CKHAVEARGS(1);
1752   g_config->serial = argv[++i];
1753  } else if ( strcmp(k, "--uiurl") == 0 ) {
1754   _CKHAVEARGS(1);
1755   g_config->uiurl = argv[++i];
1756  } else if ( strcmp(k, "--pidfile") == 0 ) {
1757   _CKHAVEARGS(1);
1758#ifdef SUPPORT_PIDFILE
1759   pidfile = argv[++i];
1760#else
1761   ROAR_ERR("--pidfile not supported");
1762   i++;
1763#endif
1764  } else if ( strcmp(k, "--log-syslog") == 0 ) {
1765#ifdef ROAR_HAVE_SYSLOG
1766   roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
1767#else
1768   ROAR_ERR("--log-syslog not supported");
1769#endif
1770  } else if ( strcmp(k, "--script-postdown") == 0 ) {
1771   _CKHAVEARGS(1);
1772#ifdef ROAR_HAVE_SYSTEM
1773   g_config->scripts.post_shutdown = argv[++i];
1774#else
1775   ROAR_ERR("--script-postdown not supported");
1776   i++;
1777#endif
1778
1779
1780  } else if ( strcmp(k, "--plugin-load") == 0 ) {
1781   _CKHAVEARGS(1);
1782   if ( plugins_load(argv[++i], plugin_args) == -1 ) {
1783    ROAR_ERR("Can not load plugin");
1784   }
1785   plugin_args = NULL;
1786  } else if ( strcmp(k, "--plugin-args") == 0 ) {
1787   _CKHAVEARGS(1);
1788   plugin_args = argv[++i];
1789  } else if ( strcmp(k, "--list-plugins") == 0 ) {
1790   metaaction = MA_LIST_PLUGIN;
1791
1792  } else if ( strcmp(k, "--guest-acclev") == 0 ) {
1793   _CKHAVEARGS(1);
1794   none_acclev = clients_str2acclev(argv[++i]);
1795   if ( none_acclev == -1 ) {
1796    ROAR_ERR("Invalid access level: %s", argv[i]);
1797    return 1;
1798   }
1799  } else if ( strcmp(k, "--trust-acclev") == 0 ) {
1800   _CKHAVEARGS(1);
1801   trust_acclev = clients_str2acclev(argv[++i]);
1802   if ( trust_acclev == -1 ) {
1803    ROAR_ERR("Invalid access level: %s", argv[i]);
1804    return 1;
1805   }
1806  } else if ( strcmp(k, "--trust-root") == 0 ) {
1807   trust_root = 1;
1808  } else if ( strcmp(k, "--no-trust-root") == 0 ) {
1809   trust_root = 0;
1810
1811  } else if ( strcmp(k, "--authfile-gen") == 0 ) {
1812   _CKHAVEARGS(1);
1813   af_file = argv[++i];
1814   af_mode = AF_MODE_GEN;
1815  } else if ( strcmp(k, "--authfile-load") == 0 ) {
1816   _CKHAVEARGS(1);
1817   af_file = argv[++i];
1818   af_mode = AF_MODE_LOAD;
1819  } else if ( strcmp(k, "--authfile-type") == 0 ) {
1820   _CKHAVEARGS(1);
1821   af_type = argv[++i];
1822  } else if ( strcmp(k, "--authfile-acclev") == 0 ) {
1823   _CKHAVEARGS(1);
1824   af_acclev = clients_str2acclev(argv[++i]);
1825  } else if ( strcmp(k, "--new-authfile") == 0 ) {
1826   if ( af_mode != AF_MODE_NONE ) {
1827    if ( add_authfile(af_file, af_type, af_mode, af_acclev) == -1 ) {
1828     ROAR_ERR("main(*): adding authfile '%s' failed!", af_file);
1829    }
1830   }
1831   af_file   = NULL;
1832   af_type   = NULL;
1833   af_mode   = AF_MODE_NONE;
1834   af_acclev = ACCLEV_ALL;
1835
1836  } else if ( strcmp(k, "--list-cf") == 0 ) {
1837   print_codecfilterlist();
1838   return 0;
1839
1840  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
1841   _CKHAVEARGS(1);
1842   sa.rate = atoi(argv[++i]);
1843  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
1844   _CKHAVEARGS(1);
1845   sa.bits = atoi(argv[++i]);
1846  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
1847   _CKHAVEARGS(1);
1848   sa.channels = atoi(argv[++i]);
1849
1850  } else if ( strcmp(k, "--aiprofile") == 0 ) {
1851   _CKHAVEARGS(1);
1852   if ( roar_profile2info(&sa, argv[++i]) == -1 ) {
1853    ROAR_ERR("Unknown audio profile: %s", argv[i]);
1854    return 1;
1855   }
1856   sa.codec    = ROAR_CODEC_DEFAULT;
1857
1858  } else if ( strcmp(k, "--stream-flags") == 0 ) {
1859   _CKHAVEARGS(1);
1860   if ( update_stream_flags(argv[++i]) == -1 ) {
1861    ROAR_ERR("Can not set stream flags");
1862    return 1;
1863   }
1864
1865  } else if ( strcmp(k, "--list-rolestack") == 0 ) {
1866   print_rolestack();
1867   return 0;
1868  } else if ( strcmp(k, "--rolestack-push") == 0 ) {
1869   _CKHAVEARGS(1);
1870  if ( (rolestack = rolestack_parse(argv[++i])) == NULL ) {
1871   ROAR_ERR("Can not parse rolestack request: %s", roar_error2str(roar_error));
1872  } else {
1873   if ( rolestack_push(rolestack) == -1 ) {
1874    ROAR_ERR("Can not push request to rolestack: %s", roar_error2str(roar_error));
1875   }
1876  }
1877
1878  } else if ( strcmp(k, "--list-aiprofiles") == 0 ) {
1879   list_aiprofiles();
1880   return 0;
1881
1882  } else if ( strcmp(k, "--list-driver") == 0 ) {
1883   print_driverlist(print_format);
1884   return 0;
1885
1886  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
1887   _CKHAVEARGS(1);
1888   o_drv  = argv[++i];
1889  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
1890   _CKHAVEARGS(1);
1891   o_dev  = argv[++i];
1892  } else if ( strcmp(k, "-oO") == 0 ) {
1893   _CKHAVEARGS(1);
1894   o_opts = argv[++i];
1895  } else if ( strcmp(k, "-oP") == 0 ) {
1896   o_prim = 1;
1897  } else if ( strcmp(k, "-oN") == 0 ) {
1898   if ( action == START || action == RESTART ) {
1899    if ( output_add(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1900     o_count++;
1901
1902    o_drv  = o_dev = o_opts = NULL;
1903    o_prim = 0;
1904   }
1905
1906  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
1907   _CKHAVEARGS(1);
1908#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1909   s_drv = argv[++i];
1910#else
1911   ROAR_ERR("main(*): No support for sources compiled in");
1912   i++;
1913#endif
1914  } else if ( strcmp(k, "-S") == 0 ) {
1915   _CKHAVEARGS(1);
1916#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1917   s_dev = argv[++i];
1918#else
1919   ROAR_ERR("main(*): No support for sources compiled in");
1920   i++;
1921#endif
1922  } else if ( strcmp(k, "-sO") == 0 ) {
1923   _CKHAVEARGS(1);
1924#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1925   s_opt = argv[++i];
1926#else
1927   ROAR_ERR("main(*): No support for sources compiled in");
1928   i++;
1929#endif
1930  } else if ( strcmp(k, "-sC") == 0 ) {
1931   _CKHAVEARGS(1);
1932#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1933   s_con = argv[++i];
1934#else
1935   ROAR_ERR("main(*): No support for sources compiled in");
1936   i++;
1937#endif
1938  } else if ( strcmp(k, "-sP") == 0 ) {
1939#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1940   s_prim = 1;
1941#else
1942   ROAR_ERR("main(*): No support for sources compiled in");
1943#endif
1944  } else if ( strcmp(k, "-sN") == 0 ) {
1945#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1946   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1947    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1948   }
1949   s_opt = s_dev = s_con = NULL;
1950   s_drv = NULL;
1951   s_prim = 0;
1952#else
1953   ROAR_ERR("main(*): No support for sources compiled in");
1954#endif
1955  } else if ( strcmp(k, "--list-sources") == 0 ) {
1956#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1957   print_sourcelist();
1958   return 0;
1959#else
1960   ROAR_ERR("main(*): No support for sources compiled in");
1961   return 1;
1962#endif
1963
1964  } else if ( strcmp(k, "-m") == 0 || strcmp(k, "--mixer") == 0 ) {
1965   _CKHAVEARGS(1);
1966#ifndef ROAR_WITHOUT_DCOMP_MIXER
1967   m_drv  = argv[++i];
1968#else
1969   ROAR_ERR("main(*): No support for mixer compiled in");
1970   return 1;
1971#endif
1972  } else if ( strcmp(k, "-M") == 0 ) {
1973   _CKHAVEARGS(1);
1974#ifndef ROAR_WITHOUT_DCOMP_MIXER
1975   m_dev  = argv[++i];
1976#else
1977   ROAR_ERR("main(*): No support for mixer compiled in");
1978   return 1;
1979#endif
1980  } else if ( strcmp(k, "-mO") == 0 ) {
1981   _CKHAVEARGS(1);
1982#ifndef ROAR_WITHOUT_DCOMP_MIXER
1983   m_opts = argv[++i];
1984#else
1985   ROAR_ERR("main(*): No support for mixer compiled in");
1986   return 1;
1987#endif
1988  } else if ( strcmp(k, "-mP") == 0 ) {
1989#ifndef ROAR_WITHOUT_DCOMP_MIXER
1990   m_prim = 1;
1991#else
1992   ROAR_ERR("main(*): No support for mixer compiled in");
1993   return 1;
1994#endif
1995  } else if ( strcmp(k, "-mN") == 0 ) {
1996#ifndef ROAR_WITHOUT_DCOMP_MIXER
1997   if ( hwmixer_add(m_drv, m_dev, m_opts, m_prim, m_count) != -1 )
1998    m_count++;
1999
2000   m_drv  = o_dev = o_opts = NULL;
2001   m_prim = 0;
2002#else
2003   ROAR_ERR("main(*): No support for mixer compiled in");
2004   return 1;
2005#endif
2006  } else if ( strcmp(k, "--list-mixers") == 0 ) {
2007#ifndef ROAR_WITHOUT_DCOMP_MIXER
2008   print_hwmixerlist();
2009   return 0;
2010#else
2011   ROAR_ERR("main(*): No support for mixer compiled in");
2012   return 1;
2013#endif
2014
2015  } else if ( strcmp(k, "--light-channels") == 0 ) {
2016   _CKHAVEARGS(1);
2017#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2018   light_channels = atoi(argv[++i]);
2019#else
2020   ROAR_WARN("main(*): no light subsystem compiled in");
2021   i++;
2022#endif
2023
2024  } else if ( strcmp(k, "--rds-pi") == 0 ) {
2025   _CKHAVEARGS(1);
2026#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2027   g_rdtcs.rds.pi = atoi(argv[++i]);
2028#else
2029   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2030   i++;
2031#endif
2032  } else if ( strcmp(k, "--rds-ps") == 0 ) {
2033   _CKHAVEARGS(1);
2034#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2035   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
2036    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]);
2037    return 1;
2038   }
2039#else
2040   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2041   i++;
2042#endif
2043  } else if ( strcmp(k, "--rds-pty") == 0 ) {
2044   _CKHAVEARGS(1);
2045#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2046   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
2047    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
2048    return 1;
2049   }
2050#else
2051   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2052   i++;
2053#endif
2054  } else if ( strcmp(k, "--rds-tp") == 0 ) {
2055#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2056   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) {
2057    ROAR_ERR("Can not set RDS TP flag");
2058    return 1;
2059   }
2060#else
2061   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2062#endif
2063  } else if ( strcmp(k, "--rds-ct") == 0 ) {
2064#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2065   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) {
2066    ROAR_ERR("Can not set RDS CT flag");
2067    return 1;
2068   }
2069#else
2070   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2071#endif
2072
2073
2074  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
2075#ifndef ROAR_WITHOUT_DCOMP_CB
2076   midi_config.init_cb = 0;
2077#else
2078   // no warning here as this is the disable option
2079#endif
2080  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
2081#ifndef ROAR_WITHOUT_DCOMP_CB
2082   midi_config.init_cb = 1;
2083#else
2084   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
2085#endif
2086  } else if ( strcmp(k, "--midi-console") == 0 ) {
2087   _CKHAVEARGS(1);
2088#ifndef ROAR_WITHOUT_DCOMP_CB
2089   midi_config.console_dev = argv[++i];
2090   midi_config.init_cb = 1;
2091#else
2092   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
2093   i++;
2094#endif
2095
2096  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
2097#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2098   ssynth_conf.enable = 1;
2099#else
2100   ROAR_ERR("main(*): No support for ssynth compiled in");
2101#endif
2102  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
2103#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2104   ssynth_conf.enable = 0;
2105#else
2106   // we can safely ignore the disable
2107#endif
2108
2109  } else if ( strcmp(k, "--x11-display") == 0 || strcmp(k, "--display") == 0 ) {
2110   _CKHAVEARGS(1);
2111#ifdef ROAR_HAVE_LIBX11
2112   x11display = argv[++i];
2113#else
2114   ROAR_ERR("No X11 support compiled in!");
2115   return 1;
2116#endif
2117
2118
2119  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
2120   _CKHAVEARGS(1);
2121   // This is only useful in INET not UNIX mode.
2122#ifdef ROAR_SUPPORT_LISTEN
2123   if ( *sock_addr == '/' )
2124    sock_addr = ROAR_DEFAULT_HOST;
2125
2126   errno = 0;
2127   if ( (port = atoi(argv[++i])) < 1 ) {
2128#ifdef ROAR_HAVE_GETSERVBYNAME
2129    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
2130     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
2131     return 1;
2132    }
2133    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
2134    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
2135            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
2136    port = ROAR_NET2HOST16(serv->s_port);
2137#else
2138    ROAR_ERR("invalite port number: %s", argv[i]);
2139    return 1;
2140#endif
2141   }
2142#endif
2143  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
2144   _CKHAVEARGS(1);
2145#ifdef ROAR_SUPPORT_LISTEN
2146   sock_addr = argv[++i];
2147#else
2148   i++;
2149#endif
2150
2151  } else if ( strcmp(k, "--proto") == 0 ) {
2152#ifdef ROAR_SUPPORT_LISTEN
2153   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
2154    ROAR_ERR("Unknown protocol: %s", argv[i]);
2155    return 1;
2156   }
2157#endif
2158  } else if ( strcmp(k, "--proto-dir") == 0 ) {
2159   _CKHAVEARGS(1);
2160#ifdef ROAR_SUPPORT_LISTEN
2161   if ( (sock_dir = roar_str2dir(argv[++i])) == -1 ) {
2162    ROAR_ERR("Unknown stream direction: %s", argv[i]);
2163    return 1;
2164   }
2165#else
2166   i++;
2167#endif
2168  } else if ( strcmp(k, "--proto-rate") == 0 ) {
2169   _CKHAVEARGS(1);
2170#ifdef ROAR_SUPPORT_LISTEN
2171   sock_info.rate = atoi(argv[++i]);
2172#else
2173   i++;
2174#endif
2175  } else if ( strcmp(k, "--proto-bits") == 0 ) {
2176   _CKHAVEARGS(1);
2177#ifdef ROAR_SUPPORT_LISTEN
2178   sock_info.bits = atoi(argv[++i]);
2179#else
2180   i++;
2181#endif
2182  } else if ( strcmp(k, "--proto-chans") == 0 ) {
2183   _CKHAVEARGS(1);
2184#ifdef ROAR_SUPPORT_LISTEN
2185   sock_info.channels = atoi(argv[++i]);
2186#else
2187   i++;
2188#endif
2189  } else if ( strcmp(k, "--proto-codec") == 0 ) {
2190   _CKHAVEARGS(1);
2191#ifdef ROAR_SUPPORT_LISTEN
2192   if ( (sock_info.codec = roar_str2codec(argv[++i])) == -1 ) {
2193    ROAR_ERR("Unknown codec: %s", argv[i]);
2194    return 1;
2195   }
2196#else
2197   i++;
2198#endif
2199  } else if ( strcmp(k, "--proto-aiprofile") == 0 ) {
2200   _CKHAVEARGS(1);
2201#ifdef ROAR_SUPPORT_LISTEN
2202   if ( roar_profile2info(&sock_info, argv[++i]) == -1 ) {
2203    ROAR_ERR("Unknown audio profile: %s", argv[i]);
2204    return 1;
2205   }
2206#else
2207   i++;
2208#endif
2209  } else if ( strcmp(k, "--list-profiles") == 0 ) {
2210#ifdef ROAR_SUPPORT_LISTEN
2211   listen_listen_profiles();
2212   return 0;
2213#endif
2214  } else if ( strcmp(k, "--proto-profile") == 0 ) {
2215   _CKHAVEARGS(1);
2216#ifdef ROAR_SUPPORT_LISTEN
2217   if ( get_listen_profile(argv[++i], &port, &sock_addr, &sock_type, &sock_proto, &sock_dir, &sock_info) == -1 ) {
2218    ROAR_ERR("Unknown listen profile: %s", argv[i]);
2219    return 1;
2220   }
2221#else
2222   i++;
2223#endif
2224
2225
2226  } else if ( strcmp(k, "--list-proto") == 0 ) {
2227   metaaction = MA_LIST_PROTO;
2228
2229  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
2230#ifdef ROAR_SUPPORT_LISTEN
2231   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
2232    sock_type = ROAR_SOCKET_TYPE_TCP;
2233
2234   if ( *sock_addr == '/' )
2235    sock_addr = ROAR_DEFAULT_HOST;
2236#endif
2237
2238  } else if ( strcmp(k, "-4") == 0 ) {
2239#ifdef ROAR_SUPPORT_LISTEN
2240   sock_type = ROAR_SOCKET_TYPE_TCP;
2241   if ( *sock_addr == '/' )
2242    sock_addr = ROAR_DEFAULT_HOST;
2243#endif
2244  } else if ( strcmp(k, "-6") == 0 ) {
2245#ifdef ROAR_SUPPORT_LISTEN
2246#ifdef AF_INET6
2247   sock_type = ROAR_SOCKET_TYPE_TCP6;
2248   if ( *sock_addr == '/' )
2249    sock_addr = ROAR_DEFAULT_HOST;
2250#else
2251    ROAR_ERR("No IPv6 support compiled in!");
2252    return 1;
2253#endif
2254#endif
2255
2256  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
2257#ifdef ROAR_SUPPORT_LISTEN
2258   // ignore this case as it is the default behavor.
2259   sock_type = ROAR_SOCKET_TYPE_UNIX;
2260#endif
2261
2262  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
2263#ifdef ROAR_SUPPORT_LISTEN
2264#ifdef ROAR_HAVE_LIBDNET
2265    port   = ROAR_DEFAULT_NUM;
2266    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
2267    sock_addr = decnethost;
2268    sock_type = ROAR_SOCKET_TYPE_DECNET;
2269#else
2270    ROAR_ERR("No DECnet support compiled in!");
2271    return 1;
2272#endif
2273#endif
2274  } else if ( strcmp(k, "--new-sock") == 0 ) {
2275#ifdef ROAR_SUPPORT_LISTEN
2276   if ( action == START || action == RESTART ) {
2277    if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
2278     ROAR_ERR("Can not open listen socket!");
2279     return 1;
2280    }
2281   }
2282#endif
2283
2284  } else if ( strcmp(k, "--slp") == 0 ) {
2285#ifdef ROAR_HAVE_LIBSLP
2286   reg_slp = 1;
2287#else
2288   ROAR_ERR("No OpenSLP support compiled in!");
2289   return 1;
2290#endif
2291
2292  } else if ( strcmp(k, "--x11") == 0 ) {
2293#ifdef ROAR_HAVE_LIBX11
2294   reg_x11 = 1;
2295#else
2296   ROAR_ERR("No X11 support compiled in!");
2297   return 1;
2298#endif
2299
2300
2301  } else if ( strcmp(k, "--jumbo-mtu") == 0 ) {
2302   _CKHAVEARGS(1);
2303   g_config->jumbo_mtu = atoi(argv[++i]);
2304
2305  } else if ( strcmp(k, "-G") == 0 ) {
2306   _CKHAVEARGS(1);
2307   sock_grp  = argv[++i];
2308  } else if ( strcmp(k, "-U") == 0 ) {
2309   _CKHAVEARGS(1);
2310   sock_user = argv[++i];
2311
2312  } else if ( strcmp(k, "--no-listen") == 0 ) {
2313#ifdef ROAR_SUPPORT_LISTEN
2314   sock_addr   = "";
2315   g_terminate = 1;
2316   g_no_listen = 1;
2317#endif
2318  } else if ( strcmp(k, "--client-fh") == 0 ) {
2319   _CKHAVEARGS(1);
2320   if ( clients_new_from_fh(atoi(argv[++i]), ROAR_PROTO_ROARAUDIO, ROAR_BYTEORDER_NETWORK, 1) == -1 ) {
2321    ROAR_ERR("main(*): Can not set client's fh");
2322    return 1;
2323   }
2324  } else if ( strcmp(k, "--close-fh") == 0 ) {
2325   _CKHAVEARGS(1);
2326#ifdef ROAR_HAVE_IO_POSIX
2327   close(atoi(argv[++i]));
2328#else
2329   i++;
2330   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
2331#endif
2332
2333  } else if ( strcmp(k, "--standby") == 0 ) {
2334   g_standby = 1;
2335  } else if ( strcmp(k, "--auto-standby") == 0 ) {
2336   g_autostandby = 1;
2337  } else {
2338   usage();
2339   return 1;
2340  }
2341
2342 }
2343#endif
2344
2345#ifdef ROAR_HAVE_MAIN_ARGS
2346 if ( metaaction != MA_ACTION )
2347  action = START;
2348
2349 switch (action) {
2350  case START:
2351    // NO-OP.
2352   break;
2353  case RESTART:
2354    // NO-OP, done before.
2355   break;
2356  case STOP:
2357#ifdef ROAR_SUPPORT_LISTEN
2358    if ( restart_server(sock_addr, 0) == -1 ) {
2359     ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
2360     return 1;
2361    }
2362    return 0;
2363#else
2364    ROAR_ERR("--stop not supported");
2365    return 1;
2366#endif
2367   break;
2368  case SHUTDOWN:
2369#ifdef ROAR_SUPPORT_LISTEN
2370    if ( restart_server(sock_addr, 1) == -1 ) {
2371     ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
2372     return 1;
2373    }
2374    return 0;
2375#else
2376    ROAR_ERR("--shutdown not supported");
2377    return 1;
2378#endif
2379   break;
2380  case RESTART_RETRY:
2381#ifdef ROAR_SUPPORT_LISTEN
2382    if ( restart_server(sock_addr, 1) == -1 ) {
2383     ROAR_WARN("Can not terminate old server (again) (not running at %s?), tring to continue anyway", sock_addr);
2384     action = RESTART;
2385    }
2386#endif
2387   break;
2388 }
2389#endif
2390
2391#ifndef DEBUG
2392 // notify dbg if requested, if in DEBUG mode
2393 // do not able because it got enabled early.
2394 if ( g_verbose >= 4 )
2395  dbg_notify_cb_register();
2396#endif
2397
2398 if ( af_mode != AF_MODE_NONE ) {
2399  if ( add_authfile(af_file, af_type, af_mode, af_acclev) == -1 ) {
2400   ROAR_ERR("main(*): adding authfile '%s' failed!", af_file);
2401  }
2402 }
2403
2404#ifndef ROAR_WITHOUT_DCOMP_MIXER
2405 if ( m_drv != NULL ) {
2406  if ( hwmixer_add(m_drv, m_dev, m_opts, m_prim, m_count) == -1 ) {
2407   ROAR_ERR("main(*): adding mixer '%s' via '%s' failed!", m_dev, m_drv);
2408  }
2409 }
2410#endif
2411
2412#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2413 if ( s_drv != NULL || s_dev != NULL ) {
2414  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
2415   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
2416  }
2417 }
2418#endif
2419
2420 if ( output_add_default(o_drv, o_dev, o_opts, o_prim, o_count) == -1 ) {
2421  ROAR_ERR("Can not initialize default driver");
2422  return 1;
2423 }
2424
2425 ROAR_INFO("Server config: rate=%i, bits=%i, chans=%i", ROAR_DBG_INFO_NOTICE, sa.rate, sa.bits, sa.channels);
2426
2427 if ( waveform_init() == -1 ) {
2428  ROAR_ERR("Can not initialize Waveform subsystem");
2429  return 1;
2430 }
2431
2432#ifndef ROAR_WITHOUT_DCOMP_MIDI
2433 if ( midi_init() == -1 ) {
2434  ROAR_ERR("Can not initialize MIDI subsystem");
2435 }
2436#endif
2437
2438#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2439 if ( ssynth_init() == -1 ) {
2440  ROAR_ERR("Can not initialize ssynth subsystem");
2441 }
2442#endif
2443
2444#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2445 if ( light_init(light_channels) == -1 ) {
2446  ROAR_ERR("Can not initialize light control subsystem");
2447 }
2448#endif
2449
2450#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2451 if ( rdtcs_init() == -1 ) {
2452  ROAR_ERR("Can not initialize RDTCS subsystem");
2453 }
2454#endif
2455
2456 if ( plugins_init() == -1 ) {
2457  ROAR_ERR("Can not initialize plugins");
2458 }
2459
2460#ifdef ROAR_SUPPORT_LISTEN
2461 if ( !g_no_listen ) {
2462  if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
2463   ROAR_ERR("Can not open listen socket!");
2464   return 1;
2465  }
2466 }
2467#endif
2468
2469 if ( output_buffer_init(&sa) == -1 ) {
2470  ROAR_ERR("Can not init output buffer!");
2471  return 1;
2472 }
2473
2474 if ( samples_init() == -1 ) {
2475  ROAR_ERR("Can not init samples!");
2476  return 1;
2477 }
2478
2479 if ( check_listen() == -1 ) {
2480  ROAR_ERR("Can not check listen sockets. BAD.");
2481  return 1;
2482 }
2483
2484#ifdef ROAR_HAVE_MAIN_ARGS
2485 // check meta action now as everything is set up.
2486 switch (metaaction) {
2487  case MA_ACTION: /* noop */; break;
2488  case MA_LIST_PROTO:
2489    print_protolist(print_format);
2490    return 0;
2491   break;
2492  case MA_LIST_PLUGIN:
2493    print_pluginlist(print_format);
2494    return 0;
2495   break;
2496 }
2497#endif
2498
2499 // we should handle this on microcontrollers, too.
2500#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
2501 signal(SIGINT,  on_sig_int);
2502 signal(SIGTERM, on_sig_term);
2503 signal(SIGCHLD, on_sig_chld);
2504 signal(SIGUSR1, on_sig_usr1);
2505 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
2506#endif
2507
2508 if ( g_config->memlock_level == -1 ) {
2509  g_config->memlock_level = MEMLOCK_DEFAULT;
2510  // do not print warning in case of failure and useage of default level.
2511  memlock_set_level(g_config->memlock_level);
2512 } else {
2513  if ( memlock_set_level(g_config->memlock_level) == -1 ) {
2514   ROAR_WARN("Can not set memory locking level to target level.");
2515  }
2516 }
2517
2518 if ( realtime ) {
2519#ifdef DEBUG
2520  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compile without -DDEBUG");
2521#endif
2522
2523#ifdef ROAR_HAVE_NICE
2524  // this stupid error check is because type of returned data of nice() changed
2525  // too often. On some systems it may return 0/-1, on some new nice value or
2526  // mixed forms of both.
2527  errno = 0;
2528  (void)nice(-5*realtime); // -5 for each --realtime
2529  if ( errno ) {
2530   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2531  }
2532#else
2533  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2534#endif
2535/*
2536#ifdef __linux__
2537  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
2538   ROAR_WARN("Can not set io priority: %s", strerror(errno));
2539#endif
2540*/
2541 }
2542
2543#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
2544 if ( setids & R_SETGID ) {
2545  if ( sock_grp == NULL ) {
2546   ROAR_ERR("Can not set GID if no groupname is supplied");
2547   return 1;
2548  }
2549  if ( (grp = getgrnam(sock_grp)) == NULL ) {
2550   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
2551   return 1;
2552  }
2553  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
2554   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
2555  }
2556  if ( grp == NULL || setgid(grp->gr_gid) == -1 ) {
2557   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
2558  }
2559 }
2560#endif
2561
2562
2563 clients_set_pid(g_self_client, getpid());
2564#ifdef ROAR_HAVE_GETUID
2565 clients_set_uid(g_self_client, getuid());
2566#endif
2567#ifdef ROAR_HAVE_GETGID
2568 clients_set_gid(g_self_client, getgid());
2569#endif
2570 clients_get(g_self_client, &self);
2571
2572 if ( self == NULL ) {
2573  ROAR_ERR("Can not get self client!");
2574  return 1;
2575 }
2576
2577 strcpy(self->name, "RoarAudio daemon internal");
2578
2579 if ( roar_nnode_free(&(self->nnode)) == -1 )
2580  return 1;
2581
2582 // not fully correct but ok as workaorund
2583 // so tools assume that roard runs on the same machine as
2584 // they in case they use AF_UNIX:
2585 if ( roar_nnode_new(&(self->nnode), ROAR_SOCKET_TYPE_UNIX) == -1 )
2586  return 1;
2587
2588#if defined(ROAR_HAVE_FORK) || defined(ROAR_TARGET_WIN32)
2589 if ( daemon ) {
2590#ifdef ROAR_HAVE_SYSLOG
2591  roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
2592#else
2593  roar_debug_set_stderr_fh(-1);
2594#endif
2595
2596#ifdef ROAR_TARGET_WIN32
2597  FreeConsole();
2598#else
2599
2600  close(ROAR_STDIN );
2601  close(ROAR_STDOUT);
2602  close(ROAR_STDERR);
2603
2604  if ( roar_fork(NULL) )
2605   ROAR_U_EXIT(0);
2606
2607#ifdef ROAR_HAVE_SETSID
2608  setsid();
2609#endif
2610  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
2611#endif
2612 }
2613#endif
2614
2615 if ( watchdog_time ) {
2616  if ( watchdog_time == -1 )
2617   watchdog_time = 5000;
2618  roar_watchdog_start(ROAR_WATCHDOG_CONF_DEFAULTS, watchdog_time, NULL);
2619 }
2620
2621#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2622 // early test for UID as we need this for the pidfile and the setuid()
2623 if ( sock_user != NULL ) {
2624  if ( (pwd = getpwnam(sock_user)) == NULL ) {
2625   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
2626   return 1;
2627  }
2628 }
2629#endif
2630
2631 ROAR_INFO("Process ID: %i", ROAR_DBG_INFO_INFO, (int)getpid());
2632
2633#ifdef SUPPORT_PIDFILE
2634 if ( pidfile != NULL ) {
2635  if ( roar_vio_open_dstr_simple(&pidfile_vio, pidfile, O_WRONLY|O_CREAT) == -1 ) {
2636   ROAR_ERR("Can not write pidfile: %s", pidfile);
2637  } else {
2638   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
2639   roar_vio_close(&pidfile_vio);
2640  }
2641#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2642  if ( pwd != NULL || grp != NULL ) {
2643   if ( chown(pidfile, pwd != NULL ? pwd->pw_uid : (uid_t)-1, grp != NULL ? grp->gr_gid : (gid_t)-1) == -1 ) {
2644    ROAR_WARN("Can not change ownership of pidfile: %s: %s", pidfile, strerror(errno));
2645   }
2646  }
2647  if ( chmod(pidfile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1 ) {
2648   ROAR_WARN("Can not change permissions of pidfile: %s: %s", pidfile, strerror(errno));
2649  }
2650#endif
2651 }
2652#endif
2653
2654#ifdef ROAR_HAVE_CHROOT
2655 if (chrootdir) {
2656  if ( chroot(chrootdir) == -1 ) {
2657   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
2658   return 2;
2659  }
2660  if ( chdir("/") == -1 ) {
2661   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
2662   return 2;
2663  }
2664 }
2665#endif
2666
2667#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2668 if ( setids & R_SETUID ) {
2669  if ( sock_user == NULL ) {
2670   ROAR_ERR("Can not set UID if no username is supplied");
2671   return 1;
2672  }
2673  if ( pwd == NULL || setuid(pwd->pw_uid) == -1 ) {
2674   ROAR_ERR("Can not set UserID: %s", strerror(errno));
2675   return 3;
2676  }
2677#ifdef ROAR_HAVE_GETUID
2678  clients_set_uid(g_self_client, getuid());
2679#endif
2680 }
2681#endif
2682
2683 // setup auth:
2684#if defined(ROAR_HAVE_GETUID) && defined(ROAR_HAVE_GETGID)
2685 if ( auth_setup(none_acclev, trust_acclev, trust_root, getuid(), getgid()) == -1 ) {
2686#else
2687 if ( auth_setup(none_acclev, trust_acclev, trust_root, -1, -1) == -1 ) {
2688#endif
2689  ROAR_ERR("Can not set up auth. Bad.");
2690  alive = 0;
2691 }
2692
2693 // Register with OpenSLP:
2694#ifdef ROAR_HAVE_LIBSLP
2695 if ( reg_slp ) {
2696  register_slp(0, sock_addr);
2697 }
2698#endif
2699
2700#ifdef ROAR_HAVE_LIBX11
2701 if ( reg_x11 ) {
2702  register_x11(0, sock_addr);
2703 }
2704#endif
2705
2706 // update sync counter.
2707 streams_change_sync_num(-1, 0);
2708
2709 ROAR_INFO("Startup complet", ROAR_DBG_INFO_INFO);
2710
2711 // start main loop...
2712 ROAR_INFO("Entering main loop", ROAR_DBG_INFO_INFO);
2713 main_loop(&sa, sysclocksync);
2714 ROAR_INFO("Left main loop", ROAR_DBG_INFO_INFO);
2715
2716 // clean up.
2717 clean_quit_prep();
2718 output_buffer_free();
2719
2720 roar_notify_core_free(NULL);
2721
2722 ROAR_INFO("Shuting down complete", ROAR_DBG_INFO_INFO);
2723
2724#ifdef ROAR_HAVE_SYSTEM
2725 if ( g_config->scripts.post_shutdown != NULL )
2726  system(g_config->scripts.post_shutdown);
2727#endif
2728
2729 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
2730 return 0;
2731}
2732
2733void cleanup_listen_socket (int terminate) {
2734 int i;
2735
2736 ROAR_DBG("cleanup_listen_socket(terminate=%i) = (void)?", terminate);
2737
2738 ROAR_INFO("Cleaning up listen sockets", ROAR_DBG_INFO_INFO);
2739
2740 // Deregister from SLP:
2741#ifdef ROAR_HAVE_LIBSLP
2742 register_slp(1, NULL);
2743#endif
2744
2745#ifdef ROAR_HAVE_LIBX11
2746 register_x11(1, NULL);
2747#endif
2748
2749#ifdef ROAR_SUPPORT_LISTEN
2750 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
2751  if ( g_listen[i].used  ) {
2752   roar_vio_close(&(g_listen[i].sock));
2753
2754   g_listen[i].used = 0;
2755
2756#ifdef ROAR_HAVE_UNIX
2757   if ( server[i] != NULL )
2758    if ( *(server[i]) == '/' )
2759     unlink(server[i]);
2760#endif
2761  }
2762 }
2763
2764#endif
2765
2766 if ( terminate )
2767  g_terminate = 1;
2768}
2769
2770void clean_quit_prep (void) {
2771 cleanup_listen_socket(0);
2772
2773 plugins_free();
2774
2775#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2776 sources_free();
2777#endif
2778 streams_free();
2779 clients_free();
2780#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2781 ssynth_free();
2782#endif
2783#ifndef ROAR_WITHOUT_DCOMP_CB
2784 midi_cb_stop(); // stop console beep
2785#endif
2786#ifndef ROAR_WITHOUT_DCOMP_MIDI
2787 midi_free();
2788#endif
2789#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2790 light_free();
2791#endif
2792#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2793 rdtcs_free();
2794#endif
2795
2796 waveform_free();
2797
2798#ifdef SUPPORT_PIDFILE
2799 if ( pidfile != NULL )
2800  unlink(pidfile);
2801#endif
2802
2803 auth_free();
2804}
2805
2806void clean_quit (void) {
2807 ROAR_INFO("Shuting down", ROAR_DBG_INFO_INFO);
2808
2809 counters_print(ROAR_DEBUG_TYPE_INFO, 0);
2810
2811 clean_quit_prep();
2812// output_buffer_free();
2813
2814 roar_notify_core_free(NULL);
2815
2816 ROAR_INFO("Shuting down complete", ROAR_DBG_INFO_INFO);
2817
2818#ifdef ROAR_HAVE_SYSTEM
2819 if ( g_config->scripts.post_shutdown != NULL )
2820  system(g_config->scripts.post_shutdown);
2821#endif
2822
2823 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
2824 exit(0);
2825}
2826
2827//ll
Note: See TracBrowser for help on using the repository browser.