source: roaraudio/roard/roard.c @ 4577:e9b3b3b521dd

Last change on this file since 4577:e9b3b3b521dd was 4577:e9b3b3b521dd, checked in by phi, 13 years ago

Announce standards via OpenSLP (Closes: #5)

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