source: roaraudio/roard/roard.c @ 5381:430b1d26e12d

Last change on this file since 5381:430b1d26e12d was 5381:430b1d26e12d, checked in by phi, 12 years ago

updated copyright years

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