source: roaraudio/roard/roard.c @ 5275:811818eb5b81

Last change on this file since 5275:811818eb5b81 was 5275:811818eb5b81, checked in by phi, 12 years ago

Improved plugin loader a lot (Closes: #190)

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