source: roaraudio/roard/roard.c @ 5592:da9a9bb6ece0

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

addded --list-plugins

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