source: roaraudio/roard/roard.c @ 4760:50a0fc5a9f65

Last change on this file since 4760:50a0fc5a9f65 was 4760:50a0fc5a9f65, checked in by phi, 13 years ago

added some nice IPv6 profiles

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