source: roaraudio/roard/roard.c @ 4481:0a8cc5415d9b

Last change on this file since 4481:0a8cc5415d9b was 4481:0a8cc5415d9b, checked in by phi, 14 years ago

prepair stuff so we can set up auth stuff in main()

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