source: roaraudio/roard/roard.c @ 4418:0234c5231754

Last change on this file since 4418:0234c5231754 was 4418:0234c5231754, checked in by phi, 14 years ago

make notify debug code runtime activatable.

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