source: roaraudio/roard/roard.c @ 5586:5b82b3417705

Last change on this file since 5586:5b82b3417705 was 5586:5b82b3417705, checked in by phi, 12 years ago

general cleanup for -Wextra

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