source: roaraudio/roard/roard.c @ 5319:3ceafc779654

Last change on this file since 5319:3ceafc779654 was 5287:4dd383ee8658, checked in by phi, 12 years ago

avoid declared but unused var on no-main-args platforms

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