source: roaraudio/roard/roard.c @ 4690:989ca5f47929

Last change on this file since 4690:989ca5f47929 was 4690:989ca5f47929, checked in by phi, 13 years ago

Fixed small win32 bugs

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