source: roaraudio/roard/roard.c @ 4790:c1073581d7c2

Last change on this file since 4790:c1073581d7c2 was 4790:c1073581d7c2, checked in by phi, 13 years ago

added support for long (> sizeof(mes.data)-4) cookies, fixed memory use-after-free bug in roard

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