source: roaraudio/roard/roard.c @ 4442:2d3a1ad3edcc

Last change on this file since 4442:2d3a1ad3edcc was 4442:2d3a1ad3edcc, checked in by phi, 13 years ago

make config default string symbolic, do not use it for server_info.

File size: 71.2 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
1397
1398// MAIN:
1399
1400#define _CKHAVEARGS(x) if ( (i + (x)) >= argc ) { \
1401                        ROAR_ERR("Option %s requires more arguments. See --help for more details.", k); \
1402                        return 70; \
1403                       }
1404
1405#ifdef ROAR_HAVE_MAIN_ARGS
1406int main (int argc, char * argv[]) {
1407#else
1408int main (void) {
1409#endif
1410#ifdef ROAR_HAVE_MAIN_ARGS
1411 int i;
1412 char * k;
1413#endif
1414#if defined(ROAR_SUPPORT_LISTEN) && defined(ROAR_HAVE_GETUID)
1415 char user_sock[80]  = {0};
1416#endif
1417 struct roar_audio_info sa, max_sa;
1418 struct roard_config config;
1419#ifdef ROAR_HAVE_FORK
1420 int    daemon       = 0;
1421#endif
1422 int    realtime     = 0;
1423 int    sysclocksync = 0;
1424 char * driver    = NULL;
1425 char * device    = NULL;
1426#ifdef ROAR_HAVE_MAIN_ARGS
1427 char * opts      = NULL;
1428#endif
1429// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
1430#ifdef ROAR_SUPPORT_LISTEN
1431 int    port       = ROAR_DEFAULT_PORT;
1432 char * sock_addr  = NULL;
1433 int    sock_proto = ROAR_PROTO_ROARAUDIO;
1434 int    sock_dir   = -1;
1435 struct roar_audio_info sock_info = {0, 0, 0, 0};
1436#endif
1437 int               drvid;
1438#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1439 char * s_drv     = "cf";
1440 char * s_dev     = NULL;
1441 char * s_con     = NULL;
1442 char * s_opt     = NULL;
1443 int    s_prim    = 0;
1444#endif
1445 char * o_drv     = getenv("ROAR_DRIVER");
1446 char * o_dev     = getenv("ROAR_DEVICE");
1447 char * o_opts    = NULL;
1448 int    o_prim    = 0;
1449 int    o_count   = 0;
1450#ifndef ROAR_WITHOUT_DCOMP_MIXER
1451 char * m_drv     = NULL;
1452 char * m_dev     = NULL;
1453 char * m_opts    = NULL;
1454 int    m_prim    = 0;
1455 int    m_count   = 0;
1456#endif
1457#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1458 int    light_channels = LIGHT_CHANNELS_DEFAULT;
1459#endif
1460#ifdef ROAR_DEFAULT_SOCKGRP
1461 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
1462#else
1463 char * sock_grp  = NULL;
1464#endif
1465 char * sock_user = NULL;
1466#ifdef ROAR_SUPPORT_LISTEN
1467 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
1468#endif
1469#ifdef ROAR_HAVE_LIBSLP
1470 int    reg_slp   = 0;
1471#endif
1472#ifdef ROAR_HAVE_LIBX11
1473 int    reg_x11   = 0;
1474#endif
1475#ifdef ROAR_HAVE_CHROOT
1476 char * chrootdir = NULL;
1477#endif
1478#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
1479 struct group   * grp  = NULL;
1480#endif
1481#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1482 struct passwd  * pwd  = NULL;
1483#endif
1484#ifdef ROAR_HAVE_GETSERVBYNAME
1485 struct servent * serv = NULL;
1486#endif
1487 DRIVER_USERDATA_T drvinst;
1488 struct roar_client * self = NULL;
1489#ifdef ROAR_HAVE_LIBDNET
1490 char decnethost[80];
1491#endif
1492#ifdef SUPPORT_PIDFILE
1493 struct roar_vio_calls pidfile_vio;
1494#endif
1495
1496 ROAR_DBG("main(*): starting roard...");
1497
1498 g_standby       =  0;
1499 g_autostandby   =  0;
1500 alive           =  1;
1501#ifdef ROAR_SUPPORT_LISTEN
1502 g_no_listen     =  0;
1503#else
1504 g_terminate     =  1;
1505#endif
1506
1507 g_verbose       = ROAR_DBG_INFO_NONE;
1508
1509 sa.bits     = ROAR_BITS_DEFAULT;
1510 sa.channels = ROAR_CHANNELS_DEFAULT;
1511 sa.rate     = ROAR_RATE_DEFAULT;
1512 sa.codec    = ROAR_CODEC_DEFAULT;
1513
1514 g_sa        = &sa;
1515 g_max_sa    = &max_sa;
1516
1517 memcpy(g_max_sa, g_sa, sizeof(max_sa));
1518
1519 counters_init();
1520
1521 g_config = &config;
1522
1523 if ( init_config() == -1 ) {
1524  ROAR_ERR("Can not init default config!");
1525  return 1;
1526 }
1527
1528 // load config
1529 roar_libroar_get_config();
1530
1531 // init notify core:
1532 // TODO: reconsider number of lists.
1533 if ( roar_notify_core_new_global(-1) == -1 ) {
1534  ROAR_ERR("Can not init notify core!");
1535  return 1;
1536 }
1537
1538 if ( roar_notify_core_register_proxy(NULL, roar_notify_proxy_std, NULL) == -1 ) {
1539  ROAR_ERR("Can not init notify core!");
1540  return 1;
1541 }
1542
1543#ifdef DEBUG
1544 // enable early in case we have DEBUG set.
1545 dbg_notify_cb_register();
1546#endif
1547
1548#ifdef ROAR_SUPPORT_LISTEN
1549 if ( init_listening() == -1 ) {
1550  ROAR_ERR("Can not init listening sockets!");
1551  return 1;
1552 }
1553#endif
1554
1555#ifndef ROAR_WITHOUT_DCOMP_MIDI
1556 if ( midi_init_config() == -1 ) {
1557  ROAR_ERR("Can not init MIDI config!");
1558  return 1;
1559 }
1560#endif
1561
1562#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1563 if ( ssynth_init_config() == -1 ) {
1564  ROAR_ERR("Can not init ssynth config!");
1565  return 1;
1566 }
1567#endif
1568
1569#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1570 if ( rdtcs_init_config() == -1 ) {
1571  ROAR_ERR("Can not init RDTCS config!");
1572  return 1;
1573 }
1574#endif
1575
1576 if ( plugins_preinit() == -1 ) {
1577  ROAR_ERR("Can not pre-init plugins!");
1578  return 1;
1579 }
1580
1581#ifdef ROAR_SUPPORT_LISTEN
1582#ifndef ROAR_TARGET_WIN32
1583 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
1584#else
1585 sock_addr = ROAR_DEFAULT_HOST;
1586#endif
1587
1588#ifdef ROAR_HAVE_GETUID
1589 if ( getuid() != 0 && getenv("HOME") != NULL ) {
1590/*
1591  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
1592*/
1593  roar_env_render_path_r(user_sock, sizeof(user_sock), "~/" ROAR_DEFAULT_SOCK_USER);
1594  sock_addr = user_sock;
1595  ROAR_DBG("main(*): setting sock_addr='%s'", sock_addr);
1596 }
1597#endif
1598
1599 if ( getenv("ROAR_SERVER") != NULL )
1600  sock_addr = getenv("ROAR_SERVER");
1601#endif
1602
1603 if ( clients_init() == -1 ) {
1604  ROAR_ERR("Can not init clients!");
1605  return 1;
1606 }
1607
1608 if ( streams_init() == -1 ) {
1609  ROAR_ERR("Can not init streams!");
1610  return 1;
1611 }
1612
1613 if ( (g_self_client = clients_new()) == -1 ) {
1614  ROAR_ERR("Can not create self client!");
1615  return 1;
1616 }
1617
1618#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1619 if ( sources_init() == -1 ) {
1620  ROAR_ERR("Can not init sources!");
1621  return 1;
1622 }
1623
1624 if ( (sources_set_client(g_self_client)) == -1 ) {
1625  ROAR_ERR("Can not init set source client!");
1626  return 1;
1627 }
1628#endif
1629
1630#ifdef ROAR_HAVE_MAIN_ARGS
1631 for (i = 1; i < argc; i++) {
1632  k = argv[i];
1633
1634  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
1635   usage();
1636   return 0;
1637
1638  } else if ( strcmp(k, "--start") == 0 ) {
1639   // this is a no op
1640  } else if ( strcmp(k, "--restart") == 0 ) {
1641#ifdef ROAR_SUPPORT_LISTEN
1642   if ( restart_server(sock_addr, 1) == -1 ) {
1643    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", sock_addr);
1644   }
1645#else
1646   ROAR_ERR("--restart not supported");
1647#endif
1648  } else if ( strcmp(k, "--shutdown") == 0 ) {
1649#ifdef ROAR_SUPPORT_LISTEN
1650   if ( restart_server(sock_addr, 1) == -1 ) {
1651    ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
1652    return 1;
1653   }
1654   return 0;
1655#else
1656   ROAR_ERR("--shutdown not supported");
1657   return 1;
1658#endif
1659  } else if ( strcmp(k, "--stop") == 0 ) {
1660#ifdef ROAR_SUPPORT_LISTEN
1661   if ( restart_server(sock_addr, 0) == -1 ) {
1662    ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
1663    return 1;
1664   }
1665   return 0;
1666#else
1667   ROAR_ERR("--stop not supported");
1668   return 1;
1669#endif
1670
1671
1672  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
1673#ifdef ROAR_HAVE_FORK
1674   daemon = 1;
1675#else
1676   ROAR_ERR("--daemon not supported");
1677#endif
1678  } else if ( strcmp(k, "--verbose") == 0 ) {
1679   g_verbose++;
1680  } else if ( strcmp(k, "--terminate") == 0 ) {
1681   g_terminate = 1;
1682  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1683   sysclocksync = 1000;
1684  } else if ( strcmp(k, "--realtime") == 0 ) {
1685   realtime++;
1686  } else if ( strcmp(k, "--memlock") == 0 ) {
1687   _CKHAVEARGS(1);
1688   g_config->memlock_level = memlock_str2level(argv[++i]);
1689  } else if ( strcmp(k, "--chroot") == 0 ) {
1690   _CKHAVEARGS(1);
1691#ifdef ROAR_HAVE_CHROOT
1692   chrootdir = argv[++i];
1693#else
1694   ROAR_ERR("--chroot not supported");
1695   i++;
1696#endif
1697  } else if ( strcmp(k, "--setgid") == 0 ) {
1698#ifdef ROAR_HAVE_SETGID
1699   setids |= R_SETGID;
1700#else
1701   ROAR_ERR("--setgid not supported");
1702#endif
1703  } else if ( strcmp(k, "--setuid") == 0 ) {
1704#ifdef ROAR_HAVE_SETUID
1705   setids |= R_SETUID;
1706#else
1707   ROAR_ERR("--setuid not supported");
1708#endif
1709  } else if ( strcmp(k, "--location") == 0 ) {
1710   _CKHAVEARGS(1);
1711   g_config->location = argv[++i];
1712  } else if ( strcmp(k, "--description") == 0 ) {
1713   _CKHAVEARGS(1);
1714   g_config->description = argv[++i];
1715  } else if ( strcmp(k, "--pidfile") == 0 ) {
1716   _CKHAVEARGS(1);
1717#ifdef SUPPORT_PIDFILE
1718   pidfile = argv[++i];
1719#else
1720   ROAR_ERR("--pidfile not supported");
1721   i++;
1722#endif
1723  } else if ( strcmp(k, "--log-syslog") == 0 ) {
1724#ifdef ROAR_HAVE_SYSLOG
1725   roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
1726#else
1727   ROAR_ERR("--log-syslog not supported");
1728#endif
1729
1730
1731  } else if ( strcmp(k, "--plugin-load") == 0 ) {
1732   _CKHAVEARGS(1);
1733   if ( plugins_load(argv[++i]) == -1 ) {
1734    ROAR_ERR("Can not load plugin");
1735   }
1736
1737  } else if ( strcmp(k, "--list-cf") == 0 ) {
1738   print_codecfilterlist();
1739   return 0;
1740
1741  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
1742   _CKHAVEARGS(1);
1743   sa.rate = atoi(argv[++i]);
1744  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
1745   _CKHAVEARGS(1);
1746   sa.bits = atoi(argv[++i]);
1747  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
1748   _CKHAVEARGS(1);
1749   sa.channels = atoi(argv[++i]);
1750
1751  } else if ( strcmp(k, "--aiprofile") == 0 ) {
1752   _CKHAVEARGS(1);
1753   if ( roar_profile2info(&sa, argv[++i]) == -1 ) {
1754    ROAR_ERR("Unknown audio profile: %s", argv[i]);
1755    return 1;
1756   }
1757   sa.codec    = ROAR_CODEC_DEFAULT;
1758
1759  } else if ( strcmp(k, "--stream-flags") == 0 ) {
1760   _CKHAVEARGS(1);
1761   if ( update_stream_flags(argv[++i]) == -1 ) {
1762    ROAR_ERR("Can not set stream flags");
1763    return 1;
1764   }
1765
1766  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
1767   _CKHAVEARGS(1);
1768   driver = argv[++i];
1769   if ( strcmp(driver, "list") == 0 ) {
1770    ROAR_WARN("The option is obsolete, use --list-driver!");
1771    print_driverlist();
1772    return 0;
1773   }
1774  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
1775   _CKHAVEARGS(1);
1776   device = argv[++i];
1777  } else if ( strcmp(k, "-dO") == 0 ) {
1778   _CKHAVEARGS(1);
1779   opts = argv[++i];
1780  } else if ( strcmp(k, "--list-driver") == 0 ) {
1781   print_driverlist();
1782   return 0;
1783
1784  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
1785   _CKHAVEARGS(1);
1786   o_drv  = argv[++i];
1787  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
1788   _CKHAVEARGS(1);
1789   o_dev  = argv[++i];
1790  } else if ( strcmp(k, "-oO") == 0 ) {
1791   _CKHAVEARGS(1);
1792   o_opts = argv[++i];
1793  } else if ( strcmp(k, "-oP") == 0 ) {
1794   o_prim = 1;
1795  } else if ( strcmp(k, "-oN") == 0 ) {
1796   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1797    o_count++;
1798
1799   o_drv  = o_dev = o_opts = NULL;
1800   o_prim = 0;
1801
1802  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
1803   _CKHAVEARGS(1);
1804#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1805   s_drv = argv[++i];
1806#else
1807   ROAR_ERR("main(*): No support for sources compiled in");
1808   i++;
1809#endif
1810  } else if ( strcmp(k, "-S") == 0 ) {
1811   _CKHAVEARGS(1);
1812#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1813   s_dev = argv[++i];
1814#else
1815   ROAR_ERR("main(*): No support for sources compiled in");
1816   i++;
1817#endif
1818  } else if ( strcmp(k, "-sO") == 0 ) {
1819   _CKHAVEARGS(1);
1820#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1821   s_opt = argv[++i];
1822#else
1823   ROAR_ERR("main(*): No support for sources compiled in");
1824   i++;
1825#endif
1826  } else if ( strcmp(k, "-sC") == 0 ) {
1827   _CKHAVEARGS(1);
1828#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1829   s_con = argv[++i];
1830#else
1831   ROAR_ERR("main(*): No support for sources compiled in");
1832   i++;
1833#endif
1834  } else if ( strcmp(k, "-sP") == 0 ) {
1835#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1836   s_prim = 1;
1837#else
1838   ROAR_ERR("main(*): No support for sources compiled in");
1839#endif
1840  } else if ( strcmp(k, "-sN") == 0 ) {
1841#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1842   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1843    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1844   }
1845   s_opt = s_dev = s_con = NULL;
1846   s_drv = "cf";
1847   s_prim = 0;
1848#else
1849   ROAR_ERR("main(*): No support for sources compiled in");
1850#endif
1851  } else if ( strcmp(k, "--list-sources") == 0 ) {
1852#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1853   print_sourcelist();
1854   return 0;
1855#else
1856   ROAR_ERR("main(*): No support for sources compiled in");
1857   return 1;
1858#endif
1859
1860  } else if ( strcmp(k, "-m") == 0 || strcmp(k, "--mixer") == 0 ) {
1861   _CKHAVEARGS(1);
1862#ifndef ROAR_WITHOUT_DCOMP_MIXER
1863   m_drv  = argv[++i];
1864#else
1865   ROAR_ERR("main(*): No support for mixer compiled in");
1866   return 1;
1867#endif
1868  } else if ( strcmp(k, "-M") == 0 ) {
1869   _CKHAVEARGS(1);
1870#ifndef ROAR_WITHOUT_DCOMP_MIXER
1871   m_dev  = argv[++i];
1872#else
1873   ROAR_ERR("main(*): No support for mixer compiled in");
1874   return 1;
1875#endif
1876  } else if ( strcmp(k, "-mO") == 0 ) {
1877   _CKHAVEARGS(1);
1878#ifndef ROAR_WITHOUT_DCOMP_MIXER
1879   m_opts = argv[++i];
1880#else
1881   ROAR_ERR("main(*): No support for mixer compiled in");
1882   return 1;
1883#endif
1884  } else if ( strcmp(k, "-mP") == 0 ) {
1885#ifndef ROAR_WITHOUT_DCOMP_MIXER
1886   m_prim = 1;
1887#else
1888   ROAR_ERR("main(*): No support for mixer compiled in");
1889   return 1;
1890#endif
1891  } else if ( strcmp(k, "-mN") == 0 ) {
1892#ifndef ROAR_WITHOUT_DCOMP_MIXER
1893   if ( add_hwmixer(m_drv, m_dev, m_opts, m_prim, m_count) != -1 )
1894    m_count++;
1895
1896   m_drv  = o_dev = o_opts = NULL;
1897   m_prim = 0;
1898#else
1899   ROAR_ERR("main(*): No support for mixer compiled in");
1900   return 1;
1901#endif
1902  } else if ( strcmp(k, "--list-mixers") == 0 ) {
1903#ifndef ROAR_WITHOUT_DCOMP_MIXER
1904   print_hwmixerlist();
1905   return 0;
1906#else
1907   ROAR_ERR("main(*): No support for mixer compiled in");
1908   return 1;
1909#endif
1910
1911  } else if ( strcmp(k, "--light-channels") == 0 ) {
1912   _CKHAVEARGS(1);
1913#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1914   light_channels = atoi(argv[++i]);
1915#else
1916   ROAR_WARN("main(*): no light subsystem compiled in");
1917   i++;
1918#endif
1919
1920  } else if ( strcmp(k, "--rds-pi") == 0 ) {
1921   _CKHAVEARGS(1);
1922#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1923   g_rdtcs.rds.pi = atoi(argv[++i]);
1924#else
1925   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1926   i++;
1927#endif
1928  } else if ( strcmp(k, "--rds-ps") == 0 ) {
1929   _CKHAVEARGS(1);
1930#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1931   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
1932    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]);
1933    return 1;
1934   }
1935#else
1936   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1937   i++;
1938#endif
1939  } else if ( strcmp(k, "--rds-pty") == 0 ) {
1940   _CKHAVEARGS(1);
1941#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1942   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
1943    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
1944    return 1;
1945   }
1946#else
1947   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1948   i++;
1949#endif
1950  } else if ( strcmp(k, "--rds-tp") == 0 ) {
1951#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1952   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) {
1953    ROAR_ERR("Can not set RDS TP flag");
1954    return 1;
1955   }
1956#else
1957   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1958#endif
1959  } else if ( strcmp(k, "--rds-ct") == 0 ) {
1960#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1961   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) {
1962    ROAR_ERR("Can not set RDS CT flag");
1963    return 1;
1964   }
1965#else
1966   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1967#endif
1968
1969
1970  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
1971#ifndef ROAR_WITHOUT_DCOMP_CB
1972   midi_config.init_cb = 0;
1973#else
1974   // no warning here as this is the disable option
1975#endif
1976  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
1977#ifndef ROAR_WITHOUT_DCOMP_CB
1978   midi_config.init_cb = 1;
1979#else
1980   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1981#endif
1982  } else if ( strcmp(k, "--midi-console") == 0 ) {
1983   _CKHAVEARGS(1);
1984#ifndef ROAR_WITHOUT_DCOMP_CB
1985   midi_config.console_dev = argv[++i];
1986   midi_config.init_cb = 1;
1987#else
1988   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1989   i++;
1990#endif
1991
1992  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
1993#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1994   ssynth_conf.enable = 1;
1995#else
1996   ROAR_ERR("main(*): No support for ssynth compiled in");
1997#endif
1998  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
1999#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2000   ssynth_conf.enable = 0;
2001#else
2002   // we can safely ignore the disable
2003#endif
2004
2005  } else if ( strcmp(k, "--x11-display") == 0 || strcmp(k, "--display") == 0 ) {
2006   _CKHAVEARGS(1);
2007#ifdef ROAR_HAVE_LIBX11
2008   x11display = argv[++i];
2009#else
2010   ROAR_ERR("No X11 support compiled in!");
2011   return 1;
2012#endif
2013
2014
2015  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
2016   _CKHAVEARGS(1);
2017   // This is only useful in INET not UNIX mode.
2018#ifdef ROAR_SUPPORT_LISTEN
2019   if ( *sock_addr == '/' )
2020    sock_addr = ROAR_DEFAULT_HOST;
2021
2022   errno = 0;
2023   if ( (port = atoi(argv[++i])) < 1 ) {
2024#ifdef ROAR_HAVE_GETSERVBYNAME
2025    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
2026     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
2027     return 1;
2028    }
2029    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
2030    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
2031            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
2032    port = ROAR_NET2HOST16(serv->s_port);
2033#else
2034    ROAR_ERR("invalite port number: %s", argv[i]);
2035    return 1;
2036#endif
2037   }
2038#endif
2039  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
2040   _CKHAVEARGS(1);
2041#ifdef ROAR_SUPPORT_LISTEN
2042   sock_addr = argv[++i];
2043#else
2044   i++;
2045#endif
2046
2047  } else if ( strcmp(k, "--proto") == 0 ) {
2048#ifdef ROAR_SUPPORT_LISTEN
2049   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
2050    ROAR_ERR("Unknown protocol: %s", argv[i]);
2051    return 1;
2052   }
2053#endif
2054  } else if ( strcmp(k, "--proto-dir") == 0 ) {
2055   _CKHAVEARGS(1);
2056#ifdef ROAR_SUPPORT_LISTEN
2057   if ( (sock_dir = roar_str2dir(argv[++i])) == -1 ) {
2058    ROAR_ERR("Unknown stream direction: %s", argv[i]);
2059    return 1;
2060   }
2061#else
2062   i++;
2063#endif
2064  } else if ( strcmp(k, "--proto-rate") == 0 ) {
2065   _CKHAVEARGS(1);
2066#ifdef ROAR_SUPPORT_LISTEN
2067   sock_info.rate = atoi(argv[++i]);
2068#else
2069   i++;
2070#endif
2071  } else if ( strcmp(k, "--proto-bits") == 0 ) {
2072   _CKHAVEARGS(1);
2073#ifdef ROAR_SUPPORT_LISTEN
2074   sock_info.bits = atoi(argv[++i]);
2075#else
2076   i++;
2077#endif
2078  } else if ( strcmp(k, "--proto-chans") == 0 ) {
2079   _CKHAVEARGS(1);
2080#ifdef ROAR_SUPPORT_LISTEN
2081   sock_info.channels = atoi(argv[++i]);
2082#else
2083   i++;
2084#endif
2085  } else if ( strcmp(k, "--proto-codec") == 0 ) {
2086   _CKHAVEARGS(1);
2087#ifdef ROAR_SUPPORT_LISTEN
2088   if ( (sock_info.codec = roar_str2codec(argv[++i])) == -1 ) {
2089    ROAR_ERR("Unknown codec: %s", argv[i]);
2090    return 1;
2091   }
2092#else
2093   i++;
2094#endif
2095  } else if ( strcmp(k, "--proto-aiprofile") == 0 ) {
2096   _CKHAVEARGS(1);
2097#ifdef ROAR_SUPPORT_LISTEN
2098   if ( roar_profile2info(&sock_info, argv[++i]) == -1 ) {
2099    ROAR_ERR("Unknown audio profile: %s", argv[i]);
2100    return 1;
2101   }
2102#else
2103   i++;
2104#endif
2105  } else if ( strcmp(k, "--list-profiles") == 0 ) {
2106#ifdef ROAR_SUPPORT_LISTEN
2107   listen_listen_profiles();
2108   return 0;
2109#endif
2110  } else if ( strcmp(k, "--proto-profile") == 0 ) {
2111   _CKHAVEARGS(1);
2112#ifdef ROAR_SUPPORT_LISTEN
2113   if ( get_listen_profile(argv[++i], &port, &sock_addr, &sock_type, &sock_proto, &sock_dir, &sock_info) == -1 ) {
2114    ROAR_ERR("Unknown listen profile: %s", argv[i]);
2115    return 1;
2116   }
2117#else
2118   i++;
2119#endif
2120
2121
2122  } else if ( strcmp(k, "--list-proto") == 0 ) {
2123   list_proto();
2124   return 0;
2125
2126  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
2127#ifdef ROAR_SUPPORT_LISTEN
2128   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
2129    sock_type = ROAR_SOCKET_TYPE_TCP;
2130
2131   if ( *sock_addr == '/' )
2132    sock_addr = ROAR_DEFAULT_HOST;
2133#endif
2134
2135  } else if ( strcmp(k, "-4") == 0 ) {
2136#ifdef ROAR_SUPPORT_LISTEN
2137   sock_type = ROAR_SOCKET_TYPE_TCP;
2138   if ( *sock_addr == '/' )
2139    sock_addr = ROAR_DEFAULT_HOST;
2140#endif
2141  } else if ( strcmp(k, "-6") == 0 ) {
2142#ifdef ROAR_SUPPORT_LISTEN
2143#ifdef AF_INET6
2144   sock_type = ROAR_SOCKET_TYPE_TCP6;
2145   if ( *sock_addr == '/' )
2146    sock_addr = ROAR_DEFAULT_HOST;
2147#else
2148    ROAR_ERR("No IPv6 support compiled in!");
2149    return 1;
2150#endif
2151#endif
2152
2153  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
2154#ifdef ROAR_SUPPORT_LISTEN
2155   // ignore this case as it is the default behavor.
2156   sock_type = ROAR_SOCKET_TYPE_UNIX;
2157#endif
2158
2159  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
2160#ifdef ROAR_SUPPORT_LISTEN
2161#ifdef ROAR_HAVE_LIBDNET
2162    port   = ROAR_DEFAULT_NUM;
2163    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
2164    sock_addr = decnethost;
2165    sock_type = ROAR_SOCKET_TYPE_DECNET;
2166#else
2167    ROAR_ERR("No DECnet support compiled in!");
2168    return 1;
2169#endif
2170#endif
2171  } else if ( strcmp(k, "--new-sock") == 0 ) {
2172#ifdef ROAR_SUPPORT_LISTEN
2173   if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
2174    ROAR_ERR("Can not open listen socket!");
2175    return 1;
2176   }
2177#endif
2178
2179  } else if ( strcmp(k, "--slp") == 0 ) {
2180#ifdef ROAR_HAVE_LIBSLP
2181   reg_slp = 1;
2182#else
2183   ROAR_ERR("No OpenSLP support compiled in!");
2184   return 1;
2185#endif
2186
2187  } else if ( strcmp(k, "--x11") == 0 ) {
2188#ifdef ROAR_HAVE_LIBX11
2189   reg_x11 = 1;
2190#else
2191   ROAR_ERR("No X11 support compiled in!");
2192   return 1;
2193#endif
2194
2195
2196  } else if ( strcmp(k, "--jumbo-mtu") == 0 ) {
2197   _CKHAVEARGS(1);
2198   g_config->jumbo_mtu = atoi(argv[++i]);
2199
2200  } else if ( strcmp(k, "-G") == 0 ) {
2201   _CKHAVEARGS(1);
2202   sock_grp  = argv[++i];
2203  } else if ( strcmp(k, "-U") == 0 ) {
2204   _CKHAVEARGS(1);
2205   sock_user = argv[++i];
2206
2207  } else if ( strcmp(k, "--no-listen") == 0 ) {
2208#ifdef ROAR_SUPPORT_LISTEN
2209   sock_addr   = "";
2210   g_terminate = 1;
2211   g_no_listen = 1;
2212#endif
2213  } else if ( strcmp(k, "--client-fh") == 0 ) {
2214   _CKHAVEARGS(1);
2215   if ( clients_new_from_fh(atoi(argv[++i]), ROAR_PROTO_ROARAUDIO, ROAR_BYTEORDER_NETWORK, 1) == -1 ) {
2216    ROAR_ERR("main(*): Can not set client's fh");
2217    return 1;
2218   }
2219  } else if ( strcmp(k, "--close-fh") == 0 ) {
2220   _CKHAVEARGS(1);
2221#ifdef ROAR_HAVE_IO_POSIX
2222   close(atoi(argv[++i]));
2223#else
2224   i++;
2225   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
2226#endif
2227
2228  } else if ( strcmp(k, "--standby") == 0 ) {
2229   g_standby = 1;
2230  } else if ( strcmp(k, "--auto-standby") == 0 ) {
2231   g_autostandby = 1;
2232  } else {
2233   usage();
2234   return 1;
2235  }
2236
2237 }
2238#endif
2239
2240#ifndef DEBUG
2241 // notify dbg if requested, if in DEBUG mode
2242 // do not able because it got enabled early.
2243 if ( g_verbose >= 4 )
2244  dbg_notify_cb_register();
2245#endif
2246
2247#ifndef ROAR_WITHOUT_DCOMP_MIXER
2248 if ( m_drv != NULL ) {
2249  if ( add_hwmixer(m_drv, m_dev, m_opts, m_prim, m_count) == -1 ) {
2250   ROAR_ERR("main(*): adding mixer '%s' via '%s' failed!", m_dev, m_drv);
2251  }
2252 }
2253#endif
2254
2255#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2256 if ( s_dev != NULL ) {
2257  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
2258   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
2259  }
2260 }
2261#endif
2262
2263 add_default_output(o_drv, o_dev, o_opts, o_prim, o_count);
2264
2265 ROAR_INFO("Server config: rate=%i, bits=%i, chans=%i", ROAR_DBG_INFO_NOTICE, sa.rate, sa.bits, sa.channels);
2266
2267 if ( waveform_init() == -1 ) {
2268  ROAR_ERR("Can not initialize Waveform subsystem");
2269  return 1;
2270 }
2271
2272#ifndef ROAR_WITHOUT_DCOMP_MIDI
2273 if ( midi_init() == -1 ) {
2274  ROAR_ERR("Can not initialize MIDI subsystem");
2275 }
2276#endif
2277
2278#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2279 if ( ssynth_init() == -1 ) {
2280  ROAR_ERR("Can not initialize ssynth subsystem");
2281 }
2282#endif
2283
2284#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2285 if ( light_init(light_channels) == -1 ) {
2286  ROAR_ERR("Can not initialize light control subsystem");
2287 }
2288#endif
2289
2290#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2291 if ( rdtcs_init() == -1 ) {
2292  ROAR_ERR("Can not initialize RDTCS subsystem");
2293 }
2294#endif
2295
2296 if ( plugins_init() == -1 ) {
2297  ROAR_ERR("Can not initialize plugins");
2298 }
2299
2300#ifdef ROAR_SUPPORT_LISTEN
2301 if ( !g_no_listen ) {
2302  if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
2303   ROAR_ERR("Can not open listen socket!");
2304   return 1;
2305  }
2306 }
2307#endif
2308
2309 if ( output_buffer_init(&sa) == -1 ) {
2310  ROAR_ERR("Can not init output buffer!");
2311  return 1;
2312 }
2313
2314 if ( driver == NULL ) {
2315  driver = "null";
2316 } else {
2317  ROAR_ERR("Usage of old driver interface. use -o not -d!");
2318  ROAR_WARN("-d will be removed within the next releases");
2319 }
2320
2321 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
2322  ROAR_ERR("Can not open output driver!");
2323  return 1;
2324 }
2325
2326 if ( samples_init() == -1 ) {
2327  ROAR_ERR("Can not init samples!");
2328  return 1;
2329 }
2330
2331
2332 // we should handle this on microcontrollers, too.
2333#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
2334 signal(SIGINT,  on_sig_int);
2335 signal(SIGTERM, on_sig_term);
2336 signal(SIGCHLD, on_sig_chld);
2337 signal(SIGUSR1, on_sig_usr1);
2338 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
2339#endif
2340
2341 if ( g_config->memlock_level == -1 ) {
2342  g_config->memlock_level = MEMLOCK_DEFAULT;
2343 }
2344
2345 if ( memlock_set_level(g_config->memlock_level) == -1 ) {
2346  ROAR_WARN("Can not set memory locking level to target level.");
2347 }
2348
2349 if ( realtime ) {
2350#ifdef DEBUG
2351  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compile without -DDEBUG");
2352#endif
2353
2354#ifdef ROAR_HAVE_NICE
2355  errno = 0;
2356  nice(-5*realtime); // -5 for each --realtime
2357  if ( errno ) {
2358   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2359  }
2360#else
2361  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2362#endif
2363/*
2364#ifdef __linux__
2365  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
2366   ROAR_WARN("Can not set io priority: %s", strerror(errno));
2367#endif
2368*/
2369 }
2370
2371#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
2372 if ( setids & R_SETGID ) {
2373  if ( sock_grp == NULL ) {
2374   ROAR_ERR("Can not set GID if no groupname is supplied");
2375   return 1;
2376  }
2377  if ( (grp = getgrnam(sock_grp)) == NULL ) {
2378   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
2379   return 1;
2380  }
2381  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
2382   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
2383  }
2384  if ( !grp || setgid(grp->gr_gid) == -1 ) {
2385   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
2386  }
2387 }
2388#endif
2389
2390
2391 clients_set_pid(g_self_client, getpid());
2392#ifdef ROAR_HAVE_GETUID
2393 clients_set_uid(g_self_client, getuid());
2394#endif
2395#ifdef ROAR_HAVE_GETGID
2396 clients_set_gid(g_self_client, getgid());
2397#endif
2398 clients_get(g_self_client, &self);
2399
2400 if ( self == NULL ) {
2401  ROAR_ERR("Can not get self client!");
2402  return 1;
2403 }
2404
2405 strcpy(self->name, "RoarAudio daemon internal");
2406
2407 if ( roar_nnode_free(&(self->nnode)) == -1 )
2408  return 1;
2409
2410 // not fully correct but ok as workaorund
2411 // so tools assume that roard runs on the same machine as
2412 // they in case they use AF_UNIX:
2413 if ( roar_nnode_new(&(self->nnode), ROAR_SOCKET_TYPE_UNIX) == -1 )
2414  return 1;
2415
2416#ifdef ROAR_HAVE_FORK
2417 if ( daemon ) {
2418#ifdef ROAR_HAVE_SYSLOG
2419  roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
2420#else
2421  roar_debug_set_stderr_fh(-1);
2422#endif
2423
2424  close(ROAR_STDIN );
2425  close(ROAR_STDOUT);
2426  close(ROAR_STDERR);
2427
2428  if ( fork() )
2429   ROAR_U_EXIT(0);
2430
2431#ifdef ROAR_HAVE_SETSID
2432  setsid();
2433#endif
2434  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
2435 }
2436#endif
2437
2438#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2439 // early test for UID as we need this for the pidfile and the setuid()
2440 if ( sock_user != NULL ) {
2441  if ( (pwd = getpwnam(sock_user)) == NULL ) {
2442   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
2443   return 1;
2444  }
2445 }
2446#endif
2447
2448 ROAR_INFO("Process ID: %i", ROAR_DBG_INFO_INFO, (int)getpid());
2449
2450#ifdef SUPPORT_PIDFILE
2451 if ( pidfile != NULL ) {
2452  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
2453   ROAR_ERR("Can not write pidfile: %s", pidfile);
2454  } else {
2455   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
2456   roar_vio_close(&pidfile_vio);
2457  }
2458#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2459  if ( pwd || grp ) {
2460   if ( chown(pidfile, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 ) {
2461    ROAR_WARN("Can not change ownership of pidfile: %s: %s", pidfile, strerror(errno));
2462   }
2463  }
2464  if ( chmod(pidfile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1 ) {
2465   ROAR_WARN("Can not change permissions of pidfile: %s: %s", pidfile, strerror(errno));
2466  }
2467#endif
2468 }
2469#endif
2470
2471#ifdef ROAR_HAVE_CHROOT
2472 if (chrootdir) {
2473  if ( chroot(chrootdir) == -1 ) {
2474   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
2475   return 2;
2476  }
2477  if ( chdir("/") == -1 ) {
2478   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
2479   return 2;
2480  }
2481 }
2482#endif
2483
2484#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2485 if ( setids & R_SETUID ) {
2486  if ( sock_user == NULL ) {
2487   ROAR_ERR("Can not set UID if no username is supplied");
2488   return 1;
2489  }
2490  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
2491   ROAR_ERR("Can not set UserID: %s", strerror(errno));
2492   return 3;
2493  }
2494#ifdef ROAR_HAVE_GETUID
2495  clients_set_uid(g_self_client, getuid());
2496#endif
2497 }
2498#endif
2499
2500 // Register with OpenSLP:
2501#ifdef ROAR_HAVE_LIBSLP
2502 if ( reg_slp ) {
2503  register_slp(0, sock_addr);
2504 }
2505#endif
2506
2507#ifdef ROAR_HAVE_LIBX11
2508 if ( reg_x11 ) {
2509  register_x11(0, sock_addr);
2510 }
2511#endif
2512
2513 ROAR_INFO("Startup complet", ROAR_DBG_INFO_INFO);
2514
2515 // start main loop...
2516 ROAR_INFO("Entering main loop", ROAR_DBG_INFO_INFO);
2517 main_loop(drvid, drvinst, &sa, sysclocksync);
2518 ROAR_INFO("Left main loop", ROAR_DBG_INFO_INFO);
2519
2520 // clean up.
2521 clean_quit_prep();
2522 driver_close(drvinst, drvid);
2523 output_buffer_free();
2524
2525 roar_notify_core_free(NULL);
2526
2527 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
2528 return 0;
2529}
2530
2531void cleanup_listen_socket (int terminate) {
2532 int i;
2533
2534 ROAR_DBG("cleanup_listen_socket(terminate=%i) = (void)?", terminate);
2535
2536 ROAR_INFO("Cleaning up listen sockets", ROAR_DBG_INFO_INFO);
2537
2538 // Deregister from SLP:
2539#ifdef ROAR_HAVE_LIBSLP
2540 register_slp(1, NULL);
2541#endif
2542
2543#ifdef ROAR_HAVE_LIBX11
2544 register_x11(1, NULL);
2545#endif
2546
2547#ifdef ROAR_SUPPORT_LISTEN
2548 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
2549  if ( g_listen[i].used  ) {
2550   roar_vio_close(&(g_listen[i].sock));
2551
2552   g_listen[i].used = 0;
2553
2554#ifdef ROAR_HAVE_UNIX
2555   if ( server[i] != NULL )
2556    if ( *(server[i]) == '/' )
2557     unlink(server[i]);
2558#endif
2559  }
2560 }
2561
2562#endif
2563
2564 if ( terminate )
2565  g_terminate = 1;
2566}
2567
2568void clean_quit_prep (void) {
2569 cleanup_listen_socket(0);
2570
2571 plugins_free();
2572
2573#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2574 sources_free();
2575#endif
2576 streams_free();
2577 clients_free();
2578#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2579 ssynth_free();
2580#endif
2581#ifndef ROAR_WITHOUT_DCOMP_CB
2582 midi_cb_stop(); // stop console beep
2583#endif
2584#ifndef ROAR_WITHOUT_DCOMP_MIDI
2585 midi_free();
2586#endif
2587#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2588 light_free();
2589#endif
2590#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2591 rdtcs_free();
2592#endif
2593
2594 waveform_free();
2595
2596#ifdef SUPPORT_PIDFILE
2597 if ( pidfile != NULL )
2598  unlink(pidfile);
2599#endif
2600}
2601
2602void clean_quit (void) {
2603 ROAR_INFO("Shuting down", ROAR_DBG_INFO_INFO);
2604
2605 counters_print(ROAR_DEBUG_TYPE_INFO, 0);
2606
2607 clean_quit_prep();
2608// driver_close(drvinst, drvid);
2609// output_buffer_free();
2610
2611 roar_notify_core_free(NULL);
2612
2613 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
2614 exit(0);
2615}
2616
2617//ll
Note: See TracBrowser for help on using the repository browser.