source: roaraudio/roard/roard.c @ 5619:16fe78ec277c

Last change on this file since 5619:16fe78ec277c was 5619:16fe78ec277c, checked in by phi, 12 years ago

Implemented watchdog (Closes: #291)

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