source: roaraudio/roard/roard.c @ 4710:41d6f4b03f6f

Last change on this file since 4710:41d6f4b03f6f was 4710:41d6f4b03f6f, checked in by phi, 13 years ago

added profiles for gopher

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