source: roaraudio/roard/roard.c @ 4703:be4d84be6f04

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

added basic gopher support

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