source: roaraudio/roard/roard.c @ 4816:f2d6a8e7c50c

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

set ROAR_FLAG_AUTOCONF and ROAR_FLAG_RECSOURCE on default output

File size: 82.0 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_UNKNOWN,   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 if ( !strcmp(k, "recsource") ) {
943   flag = ROAR_FLAG_RECSOURCE;
944  } else {
945   return -1;
946  }
947
948  g_config->streams[dir].flags |= flag;
949
950  if ( op == ROAR_RESET_FLAG )
951   g_config->streams[dir].flags -= flag;
952 }
953
954 return 0;
955}
956
957#ifdef ROAR_DRIVER_DEFAULT
958#define add_default_output add_output
959#else
960int add_default_output (char * drv, char * dev, char * opts, int prim, int count) {
961 char * drvs[] = {
962  // operating system depended things:
963#ifdef __OpenBSD__
964  /* OpenBSD use sndio natively, this check is discusses with upstream (See ML archive August 2010) */
965  "sndio",
966#endif
967  // native and pseudo-native interfaces:
968  "oss", "alsa", "sndio", "wmm",
969  // sound libs:
970  "ao", "portaudio",
971  // other sound systems:
972  "esd", "rsound", "pulsesimple", "roar",
973  // specal buildins:
974  "sysclock", "null",
975  // terminator:
976  NULL
977 };
978 int i;
979 int ret;
980 int _alive;
981
982 if ( drv != NULL )
983  return add_output(drv, dev, opts, prim, count);
984
985 if ( dev != NULL ) {
986  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);
987 }
988
989 for (i = 0; drvs[i] != NULL; i++) {
990  ROAR_INFO("add_default_output(*): trying driver %s", ROAR_DBG_INFO_INFO, drvs[i]);
991  _alive = alive; // save global alive setting
992
993  ret = add_output(drvs[i], dev, opts, prim, count);
994  if ( ret != -1 )
995   return ret;
996
997  alive = _alive; // restore global alive setting
998  ROAR_INFO("add_default_output(*): Driver %s faild to load", ROAR_DBG_INFO_VERBOSE, drvs[i]);
999 }
1000
1001 return -1;
1002}
1003#endif
1004
1005int add_output (char * drv, char * dev, char * opts, int prim, int count) {
1006 int stream;
1007 int default_flags;
1008 struct roar_stream * s;
1009 struct roar_stream_server * ss;
1010 char * k, * v;
1011#ifdef ROAR_DRIVER_CODEC
1012 char * to_free = NULL;
1013#endif
1014 int sync = 0, f_mmap = 0;
1015 int32_t blocks = -1, blocksize = -1;
1016 int dir = ROAR_DIR_OUTPUT;
1017 int error = 0;
1018 // DMX:
1019 int32_t channel  = -1;
1020 int32_t universe = -1;
1021 uint16_t tu16;
1022 float q = -32e6;
1023
1024 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);
1025
1026 if ( drv == NULL && count == 0 ) {
1027#ifdef ROAR_DRIVER_DEFAULT
1028  drv  = ROAR_DRIVER_DEFAULT;
1029  prim = 1;
1030  sync = 1;
1031
1032#ifdef ROAR_DRIVER_CODEC
1033  if ( opts == NULL ) {
1034   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
1035  }
1036#endif
1037#else
1038  ROAR_ERR("add_output(*): Can not find default driver");
1039  return -1;
1040#endif
1041 }
1042
1043 if ( opts == NULL && count == 0 ) {
1044  default_flags = ROAR_FLAG_AUTOCONF|ROAR_FLAG_RECSOURCE;
1045  sync = 1;
1046  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
1047 }
1048
1049 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
1050
1051 if ( (stream = streams_new()) == -1 ) {
1052  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
1053  if ( prim ) alive = 0;
1054  return -1;
1055 }
1056
1057 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
1058
1059 streams_get(stream, &ss);
1060 s = ROAR_STREAM(ss);
1061
1062 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
1063
1064 memset(&(s->info), 0xFF, sizeof(struct roar_audio_info)); // set everything to -1
1065
1066 s->pos_rel_id = -1;
1067// s->info.codec = codec;
1068
1069 // seting default flags:
1070 streams_set_flag(stream, default_flags);
1071
1072 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
1073
1074 if ( opts == NULL ) {
1075  k = NULL;
1076 } else {
1077  k = strtok(opts, ",");
1078 }
1079
1080 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s'): initial k='%s'(%p)", drv, dev, opts, k, k);
1081
1082 while (k != NULL) {
1083//  ROAR_WARN("add_output(*): opts: %s", k);
1084
1085  if ( (v = strstr(k, "=")) != NULL ) {
1086   *v++ = 0;
1087  }
1088
1089  ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v);
1090  if ( strcmp(k, "rate") == 0 ) {
1091   s->info.rate = atoi(v);
1092  } else if ( strcmp(k, "channels") == 0 ) {
1093   s->info.channels = atoi(v);
1094  } else if ( strcmp(k, "bits") == 0 ) {
1095   s->info.bits = atoi(v);
1096  } else if ( strcmp(k, "codec") == 0 ) {
1097   if ( (s->info.codec = roar_str2codec(v)) == -1 ) {
1098    ROAR_ERR("add_output(*): unknown codec '%s'", v);
1099    error++;
1100   }
1101  } else if ( strcmp(k, "q") == 0 ) {
1102   q = atof(v);
1103  } else if ( strcmp(k, "blocks") == 0 ) {
1104   blocks = atoi(v);
1105  } else if ( strcmp(k, "blocksize") == 0 ) {
1106   blocksize = atoi(v);
1107  } else if ( strcmp(k, "mmap") == 0 ) {
1108   f_mmap = 1;
1109  } else if ( strcmp(k, "subsystem") == 0 ) {
1110   if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) {
1111    dir = ROAR_DIR_OUTPUT;
1112#ifndef ROAR_WITHOUT_DCOMP_MIDI
1113   } else if ( !strcasecmp(v, "midi") ) {
1114    dir = ROAR_DIR_MIDI_OUT;
1115#endif
1116#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1117   } else if ( !strcasecmp(v, "light") ) {
1118    dir = ROAR_DIR_LIGHT_OUT;
1119#endif
1120#ifndef ROAR_WITHOUT_DCOMP_RAW
1121   } else if ( !strcasecmp(v, "raw") ) {
1122    dir = ROAR_DIR_RAW_OUT;
1123#endif
1124   } else if ( !strcasecmp(v, "complex") ) {
1125    dir = ROAR_DIR_COMPLEX_OUT;
1126   } else {
1127    ROAR_ERR("add_output(*): unknown/unsupported subsystem '%s'", k);
1128    error++;
1129   }
1130  // DMX:
1131  } else if ( strcmp(k, "channel") == 0 ) {
1132   channel  = atoi(v);
1133   if ( channel < 0 || channel > 65535 ) {
1134    ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel);
1135    channel = -1;
1136    error++;
1137   }
1138  } else if ( strcmp(k, "universe") == 0 ) {
1139   universe = atoi(v);
1140   if ( universe < 0 || universe > 255 ) {
1141    ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe);
1142    universe = -1;
1143    error++;
1144   }
1145
1146  } else if ( strcmp(k, "name") == 0 ) {
1147   if ( streams_set_name(stream, v) == -1 ) {
1148    ROAR_ERR("add_output(*): Can not set Stream name");
1149    error++;
1150   }
1151
1152  } else if ( strcmp(k, "meta") == 0 ) {
1153   streams_set_flag(stream, ROAR_FLAG_META);
1154  } else if ( strcmp(k, "sync") == 0 ) {
1155   sync = 1;
1156  } else if ( strcmp(k, "primary") == 0 ) {
1157   prim = 1;
1158
1159  } else if ( strcmp(k, "cleanmeta") == 0 ) {
1160   streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
1161  } else if ( strcmp(k, "autoconf") == 0 ) {
1162   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
1163  } else if ( strcmp(k, "recsource") == 0 ) {
1164   streams_set_flag(stream, ROAR_FLAG_RECSOURCE);
1165  } else if ( strcmp(k, "passmixer") == 0 ) {
1166   streams_set_flag(stream, ROAR_FLAG_PASSMIXER);
1167  } else if ( strcmp(k, "recsource") == 0 ) {
1168   streams_set_flag(stream, ROAR_FLAG_RECSOURCE);
1169  } else {
1170   ROAR_ERR("add_output(*): unknown option '%s'", k);
1171   error++;
1172  }
1173
1174  if ( error ) {
1175   streams_delete(stream);
1176   if ( prim ) alive = 0;
1177#ifdef ROAR_DRIVER_CODEC
1178   if ( to_free != NULL )
1179    free(to_free);
1180#endif
1181   return -1;
1182  }
1183
1184  k = strtok(NULL, ",");
1185 }
1186
1187 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
1188
1189 // set audio info...
1190 switch (dir) {
1191  case ROAR_DIR_LIGHT_OUT:
1192    switch (s->info.codec) {
1193     case ROAR_CODEC_DMX512:
1194     case -1:
1195       if ( s->info.rate == -1 )
1196        s->info.rate = ROAR_OUTPUT_CFREQ;
1197
1198       s->info.channels =   0;
1199       s->info.bits     =   8;
1200       s->info.codec    = ROAR_CODEC_DMX512; // in case codec == -1
1201      break;
1202    }
1203   break;
1204  case ROAR_DIR_MIDI_OUT:
1205    switch (s->info.codec) {
1206     case ROAR_CODEC_MIDI:
1207     case -1:
1208       if ( s->info.rate == -1 )
1209        s->info.rate    = ROAR_MIDI_TICKS_PER_BEAT;
1210
1211       s->info.channels = ROAR_MIDI_CHANNELS_DEFAULT;
1212       s->info.bits     = ROAR_MIDI_BITS;
1213       s->info.codec    = ROAR_CODEC_MIDI; // in case codec == -1
1214      break;
1215    }
1216   break;
1217  case ROAR_DIR_RAW_OUT:
1218    if ( s->info.rate == -1 )
1219     s->info.rate = 0;
1220    if ( s->info.bits == -1 )
1221     s->info.bits = 0;
1222    if ( s->info.channels == -1 )
1223     s->info.channels = 0;
1224    if ( s->info.codec == -1 )
1225     s->info.codec = 0;
1226   break;
1227 }
1228
1229 if ( s->info.rate == -1 )
1230  s->info.rate = g_sa->rate;
1231 if ( s->info.bits == -1 )
1232  s->info.bits = g_sa->bits;
1233 if ( s->info.channels == -1 )
1234  s->info.channels = g_sa->channels;
1235 if ( s->info.codec == -1 )
1236  s->info.codec = g_sa->codec;
1237
1238 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);
1239
1240 if ( streams_set_dir(stream, dir, 1) == -1 ) {
1241  streams_delete(stream);
1242  return -1;
1243 }
1244
1245#ifdef ROAR_DRIVER_CODEC
1246 if ( to_free != NULL )
1247  free(to_free);
1248#endif
1249
1250 if ( s->info.codec == ROAR_CODEC_ALAW || s->info.codec == ROAR_CODEC_MULAW )
1251  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
1252
1253 ROAR_STREAM_SERVER(s)->codec_orgi = s->info.codec;
1254
1255 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) {
1256  ss->driver_id = -1; // don't close a driver not opened...
1257  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
1258  streams_delete(stream);
1259  if ( prim ) alive = 0;
1260  ROAR_ERR("add_output(drv='%s', dev='%s', opts='%s'): can not open output driver.", drv, dev, opts);
1261  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
1262  return -1;
1263 }
1264
1265 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here
1266 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM,   s); // ignore errors here
1267
1268 if ( blocks != -1 )
1269  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
1270
1271 if ( blocksize != -1 )
1272  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
1273
1274 // TODO: we shoudld *really* check for errors here...
1275 if ( channel != -1 ) {
1276  tu16 = channel;
1277  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16);
1278 }
1279 if ( universe != -1 ) {
1280  tu16 = universe;
1281  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16);
1282 }
1283
1284 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
1285
1286 streams_set_fh(stream, -1); // update some internal structures
1287
1288 if ( q > -1e6 ) {
1289  ROAR_DBG("add_output(*): setting q=%f", q);
1290  streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q);
1291 }
1292
1293 client_stream_add(g_self_client, stream);
1294
1295 if ( prim ) {
1296  streams_mark_primary(stream);
1297  s->pos_rel_id = stream;
1298 }
1299
1300 if ( sync ) {
1301  streams_set_flag(stream, ROAR_FLAG_SYNC);
1302 } else {
1303  streams_reset_flag(stream, ROAR_FLAG_SYNC);
1304 }
1305
1306 if ( f_mmap )
1307  streams_set_flag(stream, ROAR_FLAG_MMAP);
1308
1309 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);
1310 return 0;
1311}
1312
1313#ifndef ROAR_WITHOUT_DCOMP_MIXER
1314int add_hwmixer (char * drv, char * dev, char * opts, int prim, int count) {
1315 char * basename = NULL;
1316 char * subnames = NULL;
1317 char * k, * v;
1318 int basestream = streams_new();
1319 int ret;
1320 int error = 0;
1321//int hwmixer_open(int basestream, char * drv, char * dev, int fh, char * basename, char * subnames) {
1322
1323 if ( basestream == -1 )
1324  return -1;
1325
1326 client_stream_add(g_self_client, basestream);
1327
1328 if ( opts == NULL ) {
1329  k = NULL;
1330 } else {
1331  k = strtok(opts, ",");
1332 }
1333
1334 while (k != NULL) {
1335//  ROAR_WARN("add_output(*): opts: %s", k);
1336
1337  if ( (v = strstr(k, "=")) != NULL ) {
1338   *v++ = 0;
1339  }
1340
1341  if ( strcmp(k, "primary") == 0 ) {
1342   prim = 1;
1343
1344  } else if ( strcmp(k, "name") == 0 ) {
1345   basename = v;
1346  } else if ( strcmp(k, "subs") == 0 ) {
1347   subnames = v;
1348
1349  } else if ( strcmp(k, "autoconf") == 0 ) {
1350   streams_set_flag(basestream, ROAR_FLAG_AUTOCONF);
1351  } else if ( strcmp(k, "passmixer") == 0 ) {
1352   streams_set_flag(basestream, ROAR_FLAG_PASSMIXER);
1353  } else {
1354   ROAR_ERR("add_hwmixer(*): unknown option '%s'", k);
1355   error++;
1356  }
1357
1358  if ( error ) {
1359   streams_delete(basestream);
1360   if ( prim ) alive = 0;
1361   return -1;
1362  }
1363
1364  k = strtok(NULL, ",");
1365 }
1366
1367 if ( prim ) {
1368  streams_mark_primary(basestream);
1369 }
1370
1371 ret = hwmixer_open(basestream, drv, dev, -1, basename, subnames);
1372
1373 if ( ret == -1 ) {
1374  streams_delete(basestream);
1375 }
1376
1377 return ret == -1 ? -1 : 0;
1378}
1379#endif
1380
1381int add_authfile (const char * file, const char * type, enum af_mode mode, enum roard_client_acclev acclev) {
1382 struct roar_authfile * authfile = NULL;
1383 struct roar_authfile_key *  key = NULL;
1384 int af_type = ROAR_AUTHFILE_TYPE_AUTO;
1385 void * keydata;
1386 int i;
1387
1388 if ( type == NULL ) {
1389  // noop.
1390 } else if ( !strcasecmp(type, "roar") ) {
1391  af_type = ROAR_AUTHFILE_TYPE_ROAR;
1392 } else if ( !strcasecmp(type, "roar") ) {
1393  af_type = ROAR_AUTHFILE_TYPE_ROAR;
1394 } else if ( !strcasecmp(type, "esd") ) {
1395  af_type = ROAR_AUTHFILE_TYPE_ESD;
1396 } else if ( !strcasecmp(type, "pulse") ) {
1397  af_type = ROAR_AUTHFILE_TYPE_PULSE;
1398 } else if ( !strcasecmp(type, "htpasswd") ) {
1399  af_type = ROAR_AUTHFILE_TYPE_HTPASSWD;
1400 } else if ( !strcasecmp(type, "xauth") ) {
1401  af_type = ROAR_AUTHFILE_TYPE_XAUTH;
1402 } else {
1403  ROAR_ERR("add_authfile(*): unknown authfile type '%s'.", type);
1404  return -1;
1405 }
1406
1407 if ( mode == AF_MODE_GEN && af_type == ROAR_AUTHFILE_TYPE_AUTO )
1408  af_type = ROAR_AUTHFILE_TYPE_ESD;
1409
1410 switch (mode) {
1411  case AF_MODE_NONE:
1412    return 0;
1413   break;
1414  case AF_MODE_GEN:
1415    switch (af_type) {
1416     case ROAR_AUTHFILE_TYPE_ESD:
1417       key = roar_authfile_key_new_random(ROAR_AUTH_T_COOKIE, 16, NULL);
1418      break;
1419     case ROAR_AUTHFILE_TYPE_PULSE:
1420       key = roar_authfile_key_new_random(ROAR_AUTH_T_COOKIE, 256, NULL);
1421      break;
1422     default:
1423       return -1;
1424      break;
1425    }
1426
1427    if ( key == NULL ) {
1428     ROAR_ERR("add_authfile(*): Can not generate key.");
1429     return -1;
1430    }
1431
1432    if ( (authfile = roar_authfile_open(af_type, file, 1, ROAR_AUTHFILE_VERSION_AUTO)) == NULL ) {
1433     roar_authfile_key_unref(key);
1434     return -1;
1435    }
1436
1437    if ( roar_authfile_add_key(authfile, key) == -1 ) {
1438     ROAR_WARN("add_authfile(*): Can not add key to authfile.");
1439    }
1440
1441    keydata = roar_mm_memdup(key->data, key->len);
1442
1443    if ( keydata == NULL ) {
1444     ROAR_WARN("add_authfile(*): Can not allocate memory for key.");
1445    } else if ( auth_addkey_cookie(acclev, keydata, key->len) == -1 ) {
1446     ROAR_WARN("add_authfile(*): Can not add key to internal key storage.");
1447    }
1448
1449    roar_authfile_key_unref(key);
1450    if ( roar_authfile_close(authfile) != 0 )
1451     return -1;
1452    return 0;
1453   break;
1454  case AF_MODE_LOAD:
1455    if ( (authfile = roar_authfile_open(af_type, file, 0, ROAR_AUTHFILE_VERSION_AUTO)) == NULL ) {
1456     return -1;
1457    }
1458
1459    for (i = 0; ; i++) {
1460     key = roar_authfile_lookup_key(authfile, ROAR_AUTH_T_AUTO, i, NULL);
1461     if ( key == NULL )
1462      break;
1463
1464     if ( key->type == ROAR_AUTH_T_COOKIE ) {
1465      keydata = roar_mm_memdup(key->data, key->len);
1466
1467      if ( keydata == NULL ) {
1468       ROAR_WARN("add_authfile(*): Can not allocate memory for key.");
1469      } else if ( auth_addkey_cookie(acclev, keydata, key->len) == -1 ) {
1470       ROAR_WARN("add_authfile(*): Can not add key to internal key storage.");
1471      }
1472     } else {
1473      ROAR_WARN("add_authfile(*): Unknown key type: %i", key->type);
1474     }
1475
1476     roar_authfile_key_unref(key);
1477    }
1478
1479    if ( roar_authfile_close(authfile) != 0 )
1480     return -1;
1481    return 0;
1482   break;
1483 }
1484
1485 return -1;
1486}
1487
1488// X11:
1489#ifdef ROAR_HAVE_LIBX11
1490int register_x11 (int unreg, char * sockname) {
1491 struct roar_x11_connection * x11con = NULL;
1492 int ret = 0;
1493
1494 if ( (x11con = roar_x11_connect(x11display)) == NULL ) {
1495  ROAR_ERR("Can not connect to X11 server for %sregistering", unreg ? "un" : "");
1496  return -1;
1497 }
1498
1499 if ( unreg ) {
1500  if ( roar_x11_delete_prop(x11con, "ROAR_SERVER") == -1 ) {
1501   ret = -1;
1502   ROAR_ERR("Error while unregistereing from X11", ROAR_DBG_INFO_INFO);
1503  } else {
1504   ROAR_INFO("Successfully unregistered from X11", ROAR_DBG_INFO_INFO);
1505  }
1506 } else {
1507  if ( roar_x11_set_prop(x11con, "ROAR_SERVER", sockname) == -1 ) {
1508   ret = -1;
1509   ROAR_ERR("Error while registereing to X11", ROAR_DBG_INFO_INFO);
1510  } else {
1511   ROAR_INFO("Successfully registered to X11", ROAR_DBG_INFO_INFO);
1512  }
1513 }
1514
1515 roar_x11_disconnect(x11con);
1516
1517 return ret;
1518}
1519#endif
1520
1521// SLP:
1522void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
1523 /* return the error code in the cookie */
1524 *(SLPError*)cookie = errcode;
1525}
1526
1527int register_slp (int unreg, char * sockname) {
1528#ifdef ROAR_HAVE_LIBSLP
1529 static int regged = 0;
1530 static char * sn = NULL;
1531 SLPError err;
1532 SLPError callbackerr;
1533 SLPHandle hslp;
1534 char addr[1024];
1535 char attr[1024] = "";
1536 char * location;
1537 char * description;
1538 char * standards;
1539
1540 if ( sockname != NULL )
1541  sn = sockname;
1542
1543 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE_ROAR "://%s", sn);
1544
1545 err = SLPOpen("en", SLP_FALSE, &hslp);
1546
1547 if (err != SLP_OK) {
1548  ROAR_ERR("Error opening slp handle: Error #%i", err);
1549  return -1;
1550 }
1551
1552 if (!unreg) {
1553
1554  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
1555   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
1556   SLPClose(hslp);
1557   return -1;
1558  }
1559
1560  if ( SLPEscape(g_config->description, &description, SLP_FALSE) != SLP_OK ) {
1561   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
1562   SLPClose(hslp);
1563   return -1;
1564  }
1565
1566  standards = stds_string();
1567
1568  if ( standards == NULL ) {
1569   if ( (standards = roar_mm_malloc(1)) == NULL ) {
1570    SLPClose(hslp);
1571    return -1;
1572   }
1573   standards[0] = 0;
1574  }
1575
1576  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
1577#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1578                               "(light-channels=%i),"
1579#endif
1580                               "(standards=%s),"
1581                               "(location=%s),(description=%s)",
1582           g_sa->rate, g_sa->channels, g_sa->bits,
1583#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1584           g_light_state.channels,
1585#endif
1586           standards,
1587           location, description
1588          );
1589
1590  roar_mm_free(standards);
1591
1592  /* Register a service with SLP */
1593  err = SLPReg(hslp,
1594               addr,
1595               SLP_LIFETIME_MAXIMUM,
1596               0,
1597               attr,
1598               SLP_TRUE,
1599               register_slp_callback,
1600               &callbackerr);
1601  regged = 1;
1602 } else if ( unreg && regged ) {
1603  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
1604  regged = 0;
1605 } else {
1606  SLPClose(hslp);
1607  return -1;
1608 }
1609
1610 /* err may contain an error code that occurred as the slp library    */
1611 /* _prepared_ to make the call.                                     */
1612 if ( err != SLP_OK ) {
1613  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
1614  return -1;
1615 }
1616
1617 /* callbackerr may contain an error code (that was assigned through */
1618 /* the callback cookie) that occurred as slp packets were sent on    */
1619 /* the wire */
1620 if (callbackerr != SLP_OK) {
1621  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
1622  return -1;
1623 }
1624
1625 SLPClose(hslp);
1626 return 0;
1627#else
1628 return -1;
1629#endif
1630}
1631
1632static int auth_setup (enum roard_client_acclev none_acclev,
1633                       enum roard_client_acclev trust_acclev, int trust_root, uid_t trust_uid, gid_t trust_gid) {
1634 union auth_typeunion * key;
1635 size_t i;
1636
1637 // Info:
1638 // if a authlevel is <= ACCLEV_IDENTED we do not need to add this to the keyring
1639 // as ACCLEV_IDENTED is already gained if IDENTIFY call was done correctly.
1640
1641 if ( !(none_acclev <= ACCLEV_IDENTED) ) {
1642  if ( auth_addkey_anonymous(none_acclev) == -1 )
1643   return -1;
1644 }
1645
1646 if ( !(trust_acclev <= ACCLEV_IDENTED) ) {
1647  if ( (key = auth_regkey_simple(ROAR_AUTH_T_TRUST, trust_acclev)) == NULL )
1648   return -1;
1649
1650  // zerosize all counters.
1651  memset(key, 0, sizeof(union auth_typeunion));
1652
1653  i = 0;
1654
1655  if ( trust_uid != -1 )
1656   key->trust.uids[i++] = trust_uid;
1657
1658#ifdef ROAR_ROOT_UID
1659  if ( trust_root )
1660   key->trust.uids[i++] = ROAR_ROOT_UID;
1661#endif
1662
1663  key->trust.uids_len = i;
1664
1665  i = 0;
1666
1667  if ( trust_gid != -1 )
1668   key->trust.gids[i++] = trust_gid;
1669
1670  key->trust.gids_len = i;
1671 }
1672
1673 return 0;
1674}
1675
1676
1677// MAIN:
1678
1679#define _CKHAVEARGS(x) if ( (i + (x)) >= argc ) { \
1680                        ROAR_ERR("Option %s requires more arguments. See --help for more details.", k); \
1681                        return 70; \
1682                       }
1683
1684#ifdef ROAR_HAVE_MAIN_ARGS
1685int main (int argc, char * argv[]) {
1686#else
1687int main (void) {
1688#endif
1689#ifdef ROAR_HAVE_MAIN_ARGS
1690 enum action action = START;
1691 int i;
1692 char * k;
1693#endif
1694#if defined(ROAR_SUPPORT_LISTEN) && defined(ROAR_HAVE_GETUID)
1695 char user_sock[80]  = {0};
1696#endif
1697 struct roar_audio_info sa, max_sa;
1698 struct roard_config config;
1699#ifdef ROAR_HAVE_FORK
1700 int    daemon       = 0;
1701#endif
1702 int    realtime     = 0;
1703 int    sysclocksync = 0;
1704// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
1705#ifdef ROAR_SUPPORT_LISTEN
1706 int    port       = ROAR_DEFAULT_PORT;
1707 char * sock_addr  = NULL;
1708 int    sock_proto = ROAR_PROTO_ROARAUDIO;
1709 int    sock_dir   = -1;
1710 struct roar_audio_info sock_info = {0, 0, 0, 0};
1711#endif
1712#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1713 char * s_drv     = "cf";
1714 char * s_dev     = NULL;
1715 char * s_con     = NULL;
1716 char * s_opt     = NULL;
1717 int    s_prim    = 0;
1718#endif
1719 char * o_drv     = getenv("ROAR_DRIVER");
1720 char * o_dev     = getenv("ROAR_DEVICE");
1721 char * o_opts    = NULL;
1722 int    o_prim    = 0;
1723 int    o_count   = 0;
1724#ifndef ROAR_WITHOUT_DCOMP_MIXER
1725 char * m_drv     = NULL;
1726 char * m_dev     = NULL;
1727 char * m_opts    = NULL;
1728 int    m_prim    = 0;
1729 int    m_count   = 0;
1730#endif
1731 enum roard_client_acclev none_acclev  = ACCLEV_ALL;
1732 enum roard_client_acclev trust_acclev = ACCLEV_ALL;
1733 int                      trust_root   = 1;
1734 char * af_file                        = NULL;
1735 char * af_type                        = NULL;
1736 enum af_mode af_mode                  = AF_MODE_NONE;
1737 enum roard_client_acclev af_acclev    = ACCLEV_ALL;
1738#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1739 int    light_channels = LIGHT_CHANNELS_DEFAULT;
1740#endif
1741#ifdef ROAR_DEFAULT_SOCKGRP
1742 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
1743#else
1744 char * sock_grp  = NULL;
1745#endif
1746 char * sock_user = NULL;
1747#ifdef ROAR_SUPPORT_LISTEN
1748 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
1749#endif
1750#ifdef ROAR_HAVE_LIBSLP
1751 int    reg_slp   = 0;
1752#endif
1753#ifdef ROAR_HAVE_LIBX11
1754 int    reg_x11   = 0;
1755#endif
1756#ifdef ROAR_HAVE_CHROOT
1757 char * chrootdir = NULL;
1758#endif
1759#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
1760 struct group   * grp  = NULL;
1761#endif
1762#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1763 struct passwd  * pwd  = NULL;
1764#endif
1765#ifdef ROAR_HAVE_GETSERVBYNAME
1766 struct servent * serv = NULL;
1767#endif
1768 struct roar_client * self = NULL;
1769#ifdef ROAR_HAVE_LIBDNET
1770 char decnethost[80];
1771#endif
1772#ifdef SUPPORT_PIDFILE
1773 struct roar_vio_calls pidfile_vio;
1774#endif
1775
1776 ROAR_DBG("main(*): starting roard...");
1777
1778 g_standby       =  0;
1779 g_autostandby   =  0;
1780 alive           =  1;
1781#ifdef ROAR_SUPPORT_LISTEN
1782 g_no_listen     =  0;
1783#else
1784 g_terminate     =  1;
1785#endif
1786
1787 g_verbose       = ROAR_DBG_INFO_NONE;
1788
1789 sa.bits     = ROAR_BITS_DEFAULT;
1790 sa.channels = ROAR_CHANNELS_DEFAULT;
1791 sa.rate     = ROAR_RATE_DEFAULT;
1792 sa.codec    = ROAR_CODEC_DEFAULT;
1793
1794 g_sa        = &sa;
1795 g_max_sa    = &max_sa;
1796
1797 memcpy(g_max_sa, g_sa, sizeof(max_sa));
1798
1799 counters_init();
1800
1801 g_config = &config;
1802
1803 if ( init_config() == -1 ) {
1804  ROAR_ERR("Can not init default config!");
1805  return 1;
1806 }
1807
1808 // load config
1809 roar_libroar_get_config();
1810
1811 // init notify core:
1812 // TODO: reconsider number of lists.
1813 if ( roar_notify_core_new_global(-1) == -1 ) {
1814  ROAR_ERR("Can not init notify core!");
1815  return 1;
1816 }
1817
1818 if ( roar_notify_core_register_proxy(NULL, roar_notify_proxy_std, NULL) == -1 ) {
1819  ROAR_ERR("Can not init notify core!");
1820  return 1;
1821 }
1822
1823#ifdef DEBUG
1824 // enable early in case we have DEBUG set.
1825 dbg_notify_cb_register();
1826#endif
1827
1828 if ( auth_init() == -1 ) {
1829  ROAR_ERR("Can not init auth subsystem!");
1830  return 1;
1831 }
1832
1833#ifdef ROAR_SUPPORT_LISTEN
1834 if ( init_listening() == -1 ) {
1835  ROAR_ERR("Can not init listening sockets!");
1836  return 1;
1837 }
1838#endif
1839
1840#ifndef ROAR_WITHOUT_DCOMP_MIDI
1841 if ( midi_init_config() == -1 ) {
1842  ROAR_ERR("Can not init MIDI config!");
1843  return 1;
1844 }
1845#endif
1846
1847#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1848 if ( ssynth_init_config() == -1 ) {
1849  ROAR_ERR("Can not init ssynth config!");
1850  return 1;
1851 }
1852#endif
1853
1854#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1855 if ( rdtcs_init_config() == -1 ) {
1856  ROAR_ERR("Can not init RDTCS config!");
1857  return 1;
1858 }
1859#endif
1860
1861 if ( plugins_preinit() == -1 ) {
1862  ROAR_ERR("Can not pre-init plugins!");
1863  return 1;
1864 }
1865
1866#ifdef ROAR_SUPPORT_LISTEN
1867#ifndef ROAR_TARGET_WIN32
1868 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
1869#else
1870 sock_addr = ROAR_DEFAULT_HOST;
1871#endif
1872
1873#ifdef ROAR_HAVE_GETUID
1874 if ( getuid() != 0 && getenv("HOME") != NULL ) {
1875/*
1876  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
1877*/
1878  roar_env_render_path_r(user_sock, sizeof(user_sock), "~/" ROAR_DEFAULT_SOCK_USER);
1879  sock_addr = user_sock;
1880  ROAR_DBG("main(*): setting sock_addr='%s'", sock_addr);
1881 }
1882#endif
1883
1884 if ( getenv("ROAR_SERVER") != NULL )
1885  sock_addr = getenv("ROAR_SERVER");
1886#endif
1887
1888 if ( clients_init() == -1 ) {
1889  ROAR_ERR("Can not init clients!");
1890  return 1;
1891 }
1892
1893 if ( streams_init() == -1 ) {
1894  ROAR_ERR("Can not init streams!");
1895  return 1;
1896 }
1897
1898 if ( (g_self_client = clients_new()) == -1 ) {
1899  ROAR_ERR("Can not create self client!");
1900  return 1;
1901 }
1902
1903#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1904 if ( sources_init() == -1 ) {
1905  ROAR_ERR("Can not init sources!");
1906  return 1;
1907 }
1908
1909 if ( (sources_set_client(g_self_client)) == -1 ) {
1910  ROAR_ERR("Can not init set source client!");
1911  return 1;
1912 }
1913#endif
1914
1915#ifdef ROAR_HAVE_MAIN_ARGS
1916 for (i = 1; i < argc; i++) {
1917  k = argv[i];
1918
1919  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
1920   usage();
1921   return 0;
1922
1923  } else if ( strcmp(k, "--start") == 0 ) {
1924   // this is a no op
1925   action = START;
1926  } else if ( strcmp(k, "--restart") == 0 ) {
1927   action = RESTART;
1928#ifdef ROAR_SUPPORT_LISTEN
1929   if ( restart_server(sock_addr, 1) == -1 ) {
1930    ROAR_WARN("Can not terminate old server (not running at %s?), will retry later.", sock_addr);
1931    action = RESTART_RETRY;
1932   }
1933#else
1934   ROAR_ERR("--restart not supported");
1935#endif
1936  } else if ( strcmp(k, "--shutdown") == 0 ) {
1937   action = SHUTDOWN;
1938  } else if ( strcmp(k, "--stop") == 0 ) {
1939   action = STOP;
1940
1941
1942  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
1943#ifdef ROAR_HAVE_FORK
1944   daemon = 1;
1945#else
1946   ROAR_ERR("--daemon not supported");
1947#endif
1948  } else if ( strcmp(k, "--verbose") == 0 ) {
1949   g_verbose++;
1950  } else if ( strcmp(k, "--terminate") == 0 ) {
1951   g_terminate = 1;
1952  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1953   sysclocksync = 1000;
1954  } else if ( strcmp(k, "--realtime") == 0 ) {
1955   realtime++;
1956  } else if ( strcmp(k, "--memlock") == 0 ) {
1957   _CKHAVEARGS(1);
1958   g_config->memlock_level = memlock_str2level(argv[++i]);
1959  } else if ( strcmp(k, "--chroot") == 0 ) {
1960   _CKHAVEARGS(1);
1961#ifdef ROAR_HAVE_CHROOT
1962   chrootdir = argv[++i];
1963#else
1964   ROAR_ERR("--chroot not supported");
1965   i++;
1966#endif
1967  } else if ( strcmp(k, "--setgid") == 0 ) {
1968#ifdef ROAR_HAVE_SETGID
1969   setids |= R_SETGID;
1970#else
1971   ROAR_ERR("--setgid not supported");
1972#endif
1973  } else if ( strcmp(k, "--setuid") == 0 ) {
1974#ifdef ROAR_HAVE_SETUID
1975   setids |= R_SETUID;
1976#else
1977   ROAR_ERR("--setuid not supported");
1978#endif
1979  } else if ( strcmp(k, "--location") == 0 ) {
1980   _CKHAVEARGS(1);
1981   g_config->location = argv[++i];
1982  } else if ( strcmp(k, "--description") == 0 ) {
1983   _CKHAVEARGS(1);
1984   g_config->description = argv[++i];
1985  } else if ( strcmp(k, "--pidfile") == 0 ) {
1986   _CKHAVEARGS(1);
1987#ifdef SUPPORT_PIDFILE
1988   pidfile = argv[++i];
1989#else
1990   ROAR_ERR("--pidfile not supported");
1991   i++;
1992#endif
1993  } else if ( strcmp(k, "--log-syslog") == 0 ) {
1994#ifdef ROAR_HAVE_SYSLOG
1995   roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
1996#else
1997   ROAR_ERR("--log-syslog not supported");
1998#endif
1999
2000
2001  } else if ( strcmp(k, "--plugin-load") == 0 ) {
2002   _CKHAVEARGS(1);
2003   if ( plugins_load(argv[++i]) == -1 ) {
2004    ROAR_ERR("Can not load plugin");
2005   }
2006
2007  } else if ( strcmp(k, "--guest-acclev") == 0 ) {
2008   _CKHAVEARGS(1);
2009   none_acclev = clients_str2acclev(argv[++i]);
2010  } else if ( strcmp(k, "--trust-acclev") == 0 ) {
2011   _CKHAVEARGS(1);
2012   trust_acclev = clients_str2acclev(argv[++i]);
2013  } else if ( strcmp(k, "--trust-root") == 0 ) {
2014   trust_root = 1;
2015  } else if ( strcmp(k, "--no-trust-root") == 0 ) {
2016   trust_root = 0;
2017
2018  } else if ( strcmp(k, "--authfile-gen") == 0 ) {
2019   _CKHAVEARGS(1);
2020   af_file = argv[++i];
2021   af_mode = AF_MODE_GEN;
2022  } else if ( strcmp(k, "--authfile-load") == 0 ) {
2023   _CKHAVEARGS(1);
2024   af_file = argv[++i];
2025   af_mode = AF_MODE_LOAD;
2026  } else if ( strcmp(k, "--authfile-type") == 0 ) {
2027   _CKHAVEARGS(1);
2028   af_type = argv[++i];
2029  } else if ( strcmp(k, "--authfile-acclev") == 0 ) {
2030   _CKHAVEARGS(1);
2031   af_acclev = clients_str2acclev(argv[++i]);
2032  } else if ( strcmp(k, "--new-authfile") == 0 ) {
2033   if ( af_mode != AF_MODE_NONE ) {
2034    if ( add_authfile(af_file, af_type, af_mode, af_acclev) == -1 ) {
2035     ROAR_ERR("main(*): adding authfile '%s' failed!", af_file);
2036    }
2037   }
2038   af_file   = NULL;
2039   af_type   = NULL;
2040   af_mode   = AF_MODE_NONE;
2041   af_acclev = ACCLEV_ALL;
2042
2043  } else if ( strcmp(k, "--list-cf") == 0 ) {
2044   print_codecfilterlist();
2045   return 0;
2046
2047  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
2048   _CKHAVEARGS(1);
2049   sa.rate = atoi(argv[++i]);
2050  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
2051   _CKHAVEARGS(1);
2052   sa.bits = atoi(argv[++i]);
2053  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
2054   _CKHAVEARGS(1);
2055   sa.channels = atoi(argv[++i]);
2056
2057  } else if ( strcmp(k, "--aiprofile") == 0 ) {
2058   _CKHAVEARGS(1);
2059   if ( roar_profile2info(&sa, argv[++i]) == -1 ) {
2060    ROAR_ERR("Unknown audio profile: %s", argv[i]);
2061    return 1;
2062   }
2063   sa.codec    = ROAR_CODEC_DEFAULT;
2064
2065  } else if ( strcmp(k, "--stream-flags") == 0 ) {
2066   _CKHAVEARGS(1);
2067   if ( update_stream_flags(argv[++i]) == -1 ) {
2068    ROAR_ERR("Can not set stream flags");
2069    return 1;
2070   }
2071
2072  } else if ( strcmp(k, "--list-driver") == 0 ) {
2073   print_driverlist();
2074   return 0;
2075
2076  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
2077   _CKHAVEARGS(1);
2078   o_drv  = argv[++i];
2079  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
2080   _CKHAVEARGS(1);
2081   o_dev  = argv[++i];
2082  } else if ( strcmp(k, "-oO") == 0 ) {
2083   _CKHAVEARGS(1);
2084   o_opts = argv[++i];
2085  } else if ( strcmp(k, "-oP") == 0 ) {
2086   o_prim = 1;
2087  } else if ( strcmp(k, "-oN") == 0 ) {
2088   if ( action == START || action == RESTART ) {
2089    if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
2090     o_count++;
2091
2092    o_drv  = o_dev = o_opts = NULL;
2093    o_prim = 0;
2094   }
2095
2096  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
2097   _CKHAVEARGS(1);
2098#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2099   s_drv = argv[++i];
2100#else
2101   ROAR_ERR("main(*): No support for sources compiled in");
2102   i++;
2103#endif
2104  } else if ( strcmp(k, "-S") == 0 ) {
2105   _CKHAVEARGS(1);
2106#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2107   s_dev = argv[++i];
2108#else
2109   ROAR_ERR("main(*): No support for sources compiled in");
2110   i++;
2111#endif
2112  } else if ( strcmp(k, "-sO") == 0 ) {
2113   _CKHAVEARGS(1);
2114#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2115   s_opt = argv[++i];
2116#else
2117   ROAR_ERR("main(*): No support for sources compiled in");
2118   i++;
2119#endif
2120  } else if ( strcmp(k, "-sC") == 0 ) {
2121   _CKHAVEARGS(1);
2122#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2123   s_con = argv[++i];
2124#else
2125   ROAR_ERR("main(*): No support for sources compiled in");
2126   i++;
2127#endif
2128  } else if ( strcmp(k, "-sP") == 0 ) {
2129#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2130   s_prim = 1;
2131#else
2132   ROAR_ERR("main(*): No support for sources compiled in");
2133#endif
2134  } else if ( strcmp(k, "-sN") == 0 ) {
2135#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2136   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
2137    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
2138   }
2139   s_opt = s_dev = s_con = NULL;
2140   s_drv = "cf";
2141   s_prim = 0;
2142#else
2143   ROAR_ERR("main(*): No support for sources compiled in");
2144#endif
2145  } else if ( strcmp(k, "--list-sources") == 0 ) {
2146#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2147   print_sourcelist();
2148   return 0;
2149#else
2150   ROAR_ERR("main(*): No support for sources compiled in");
2151   return 1;
2152#endif
2153
2154  } else if ( strcmp(k, "-m") == 0 || strcmp(k, "--mixer") == 0 ) {
2155   _CKHAVEARGS(1);
2156#ifndef ROAR_WITHOUT_DCOMP_MIXER
2157   m_drv  = argv[++i];
2158#else
2159   ROAR_ERR("main(*): No support for mixer compiled in");
2160   return 1;
2161#endif
2162  } else if ( strcmp(k, "-M") == 0 ) {
2163   _CKHAVEARGS(1);
2164#ifndef ROAR_WITHOUT_DCOMP_MIXER
2165   m_dev  = argv[++i];
2166#else
2167   ROAR_ERR("main(*): No support for mixer compiled in");
2168   return 1;
2169#endif
2170  } else if ( strcmp(k, "-mO") == 0 ) {
2171   _CKHAVEARGS(1);
2172#ifndef ROAR_WITHOUT_DCOMP_MIXER
2173   m_opts = argv[++i];
2174#else
2175   ROAR_ERR("main(*): No support for mixer compiled in");
2176   return 1;
2177#endif
2178  } else if ( strcmp(k, "-mP") == 0 ) {
2179#ifndef ROAR_WITHOUT_DCOMP_MIXER
2180   m_prim = 1;
2181#else
2182   ROAR_ERR("main(*): No support for mixer compiled in");
2183   return 1;
2184#endif
2185  } else if ( strcmp(k, "-mN") == 0 ) {
2186#ifndef ROAR_WITHOUT_DCOMP_MIXER
2187   if ( add_hwmixer(m_drv, m_dev, m_opts, m_prim, m_count) != -1 )
2188    m_count++;
2189
2190   m_drv  = o_dev = o_opts = NULL;
2191   m_prim = 0;
2192#else
2193   ROAR_ERR("main(*): No support for mixer compiled in");
2194   return 1;
2195#endif
2196  } else if ( strcmp(k, "--list-mixers") == 0 ) {
2197#ifndef ROAR_WITHOUT_DCOMP_MIXER
2198   print_hwmixerlist();
2199   return 0;
2200#else
2201   ROAR_ERR("main(*): No support for mixer compiled in");
2202   return 1;
2203#endif
2204
2205  } else if ( strcmp(k, "--light-channels") == 0 ) {
2206   _CKHAVEARGS(1);
2207#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2208   light_channels = atoi(argv[++i]);
2209#else
2210   ROAR_WARN("main(*): no light subsystem compiled in");
2211   i++;
2212#endif
2213
2214  } else if ( strcmp(k, "--rds-pi") == 0 ) {
2215   _CKHAVEARGS(1);
2216#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2217   g_rdtcs.rds.pi = atoi(argv[++i]);
2218#else
2219   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2220   i++;
2221#endif
2222  } else if ( strcmp(k, "--rds-ps") == 0 ) {
2223   _CKHAVEARGS(1);
2224#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2225   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
2226    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]);
2227    return 1;
2228   }
2229#else
2230   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2231   i++;
2232#endif
2233  } else if ( strcmp(k, "--rds-pty") == 0 ) {
2234   _CKHAVEARGS(1);
2235#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2236   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
2237    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
2238    return 1;
2239   }
2240#else
2241   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2242   i++;
2243#endif
2244  } else if ( strcmp(k, "--rds-tp") == 0 ) {
2245#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2246   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) {
2247    ROAR_ERR("Can not set RDS TP flag");
2248    return 1;
2249   }
2250#else
2251   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2252#endif
2253  } else if ( strcmp(k, "--rds-ct") == 0 ) {
2254#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2255   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) {
2256    ROAR_ERR("Can not set RDS CT flag");
2257    return 1;
2258   }
2259#else
2260   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2261#endif
2262
2263
2264  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
2265#ifndef ROAR_WITHOUT_DCOMP_CB
2266   midi_config.init_cb = 0;
2267#else
2268   // no warning here as this is the disable option
2269#endif
2270  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
2271#ifndef ROAR_WITHOUT_DCOMP_CB
2272   midi_config.init_cb = 1;
2273#else
2274   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
2275#endif
2276  } else if ( strcmp(k, "--midi-console") == 0 ) {
2277   _CKHAVEARGS(1);
2278#ifndef ROAR_WITHOUT_DCOMP_CB
2279   midi_config.console_dev = argv[++i];
2280   midi_config.init_cb = 1;
2281#else
2282   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
2283   i++;
2284#endif
2285
2286  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
2287#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2288   ssynth_conf.enable = 1;
2289#else
2290   ROAR_ERR("main(*): No support for ssynth compiled in");
2291#endif
2292  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
2293#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2294   ssynth_conf.enable = 0;
2295#else
2296   // we can safely ignore the disable
2297#endif
2298
2299  } else if ( strcmp(k, "--x11-display") == 0 || strcmp(k, "--display") == 0 ) {
2300   _CKHAVEARGS(1);
2301#ifdef ROAR_HAVE_LIBX11
2302   x11display = argv[++i];
2303#else
2304   ROAR_ERR("No X11 support compiled in!");
2305   return 1;
2306#endif
2307
2308
2309  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
2310   _CKHAVEARGS(1);
2311   // This is only useful in INET not UNIX mode.
2312#ifdef ROAR_SUPPORT_LISTEN
2313   if ( *sock_addr == '/' )
2314    sock_addr = ROAR_DEFAULT_HOST;
2315
2316   errno = 0;
2317   if ( (port = atoi(argv[++i])) < 1 ) {
2318#ifdef ROAR_HAVE_GETSERVBYNAME
2319    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
2320     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
2321     return 1;
2322    }
2323    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
2324    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
2325            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
2326    port = ROAR_NET2HOST16(serv->s_port);
2327#else
2328    ROAR_ERR("invalite port number: %s", argv[i]);
2329    return 1;
2330#endif
2331   }
2332#endif
2333  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
2334   _CKHAVEARGS(1);
2335#ifdef ROAR_SUPPORT_LISTEN
2336   sock_addr = argv[++i];
2337#else
2338   i++;
2339#endif
2340
2341  } else if ( strcmp(k, "--proto") == 0 ) {
2342#ifdef ROAR_SUPPORT_LISTEN
2343   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
2344    ROAR_ERR("Unknown protocol: %s", argv[i]);
2345    return 1;
2346   }
2347#endif
2348  } else if ( strcmp(k, "--proto-dir") == 0 ) {
2349   _CKHAVEARGS(1);
2350#ifdef ROAR_SUPPORT_LISTEN
2351   if ( (sock_dir = roar_str2dir(argv[++i])) == -1 ) {
2352    ROAR_ERR("Unknown stream direction: %s", argv[i]);
2353    return 1;
2354   }
2355#else
2356   i++;
2357#endif
2358  } else if ( strcmp(k, "--proto-rate") == 0 ) {
2359   _CKHAVEARGS(1);
2360#ifdef ROAR_SUPPORT_LISTEN
2361   sock_info.rate = atoi(argv[++i]);
2362#else
2363   i++;
2364#endif
2365  } else if ( strcmp(k, "--proto-bits") == 0 ) {
2366   _CKHAVEARGS(1);
2367#ifdef ROAR_SUPPORT_LISTEN
2368   sock_info.bits = atoi(argv[++i]);
2369#else
2370   i++;
2371#endif
2372  } else if ( strcmp(k, "--proto-chans") == 0 ) {
2373   _CKHAVEARGS(1);
2374#ifdef ROAR_SUPPORT_LISTEN
2375   sock_info.channels = atoi(argv[++i]);
2376#else
2377   i++;
2378#endif
2379  } else if ( strcmp(k, "--proto-codec") == 0 ) {
2380   _CKHAVEARGS(1);
2381#ifdef ROAR_SUPPORT_LISTEN
2382   if ( (sock_info.codec = roar_str2codec(argv[++i])) == -1 ) {
2383    ROAR_ERR("Unknown codec: %s", argv[i]);
2384    return 1;
2385   }
2386#else
2387   i++;
2388#endif
2389  } else if ( strcmp(k, "--proto-aiprofile") == 0 ) {
2390   _CKHAVEARGS(1);
2391#ifdef ROAR_SUPPORT_LISTEN
2392   if ( roar_profile2info(&sock_info, argv[++i]) == -1 ) {
2393    ROAR_ERR("Unknown audio profile: %s", argv[i]);
2394    return 1;
2395   }
2396#else
2397   i++;
2398#endif
2399  } else if ( strcmp(k, "--list-profiles") == 0 ) {
2400#ifdef ROAR_SUPPORT_LISTEN
2401   listen_listen_profiles();
2402   return 0;
2403#endif
2404  } else if ( strcmp(k, "--proto-profile") == 0 ) {
2405   _CKHAVEARGS(1);
2406#ifdef ROAR_SUPPORT_LISTEN
2407   if ( get_listen_profile(argv[++i], &port, &sock_addr, &sock_type, &sock_proto, &sock_dir, &sock_info) == -1 ) {
2408    ROAR_ERR("Unknown listen profile: %s", argv[i]);
2409    return 1;
2410   }
2411#else
2412   i++;
2413#endif
2414
2415
2416  } else if ( strcmp(k, "--list-proto") == 0 ) {
2417   list_proto();
2418   return 0;
2419
2420  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
2421#ifdef ROAR_SUPPORT_LISTEN
2422   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
2423    sock_type = ROAR_SOCKET_TYPE_TCP;
2424
2425   if ( *sock_addr == '/' )
2426    sock_addr = ROAR_DEFAULT_HOST;
2427#endif
2428
2429  } else if ( strcmp(k, "-4") == 0 ) {
2430#ifdef ROAR_SUPPORT_LISTEN
2431   sock_type = ROAR_SOCKET_TYPE_TCP;
2432   if ( *sock_addr == '/' )
2433    sock_addr = ROAR_DEFAULT_HOST;
2434#endif
2435  } else if ( strcmp(k, "-6") == 0 ) {
2436#ifdef ROAR_SUPPORT_LISTEN
2437#ifdef AF_INET6
2438   sock_type = ROAR_SOCKET_TYPE_TCP6;
2439   if ( *sock_addr == '/' )
2440    sock_addr = ROAR_DEFAULT_HOST;
2441#else
2442    ROAR_ERR("No IPv6 support compiled in!");
2443    return 1;
2444#endif
2445#endif
2446
2447  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
2448#ifdef ROAR_SUPPORT_LISTEN
2449   // ignore this case as it is the default behavor.
2450   sock_type = ROAR_SOCKET_TYPE_UNIX;
2451#endif
2452
2453  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
2454#ifdef ROAR_SUPPORT_LISTEN
2455#ifdef ROAR_HAVE_LIBDNET
2456    port   = ROAR_DEFAULT_NUM;
2457    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
2458    sock_addr = decnethost;
2459    sock_type = ROAR_SOCKET_TYPE_DECNET;
2460#else
2461    ROAR_ERR("No DECnet support compiled in!");
2462    return 1;
2463#endif
2464#endif
2465  } else if ( strcmp(k, "--new-sock") == 0 ) {
2466#ifdef ROAR_SUPPORT_LISTEN
2467   if ( action == START || action == RESTART ) {
2468    if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
2469     ROAR_ERR("Can not open listen socket!");
2470     return 1;
2471    }
2472   }
2473#endif
2474
2475  } else if ( strcmp(k, "--slp") == 0 ) {
2476#ifdef ROAR_HAVE_LIBSLP
2477   reg_slp = 1;
2478#else
2479   ROAR_ERR("No OpenSLP support compiled in!");
2480   return 1;
2481#endif
2482
2483  } else if ( strcmp(k, "--x11") == 0 ) {
2484#ifdef ROAR_HAVE_LIBX11
2485   reg_x11 = 1;
2486#else
2487   ROAR_ERR("No X11 support compiled in!");
2488   return 1;
2489#endif
2490
2491
2492  } else if ( strcmp(k, "--jumbo-mtu") == 0 ) {
2493   _CKHAVEARGS(1);
2494   g_config->jumbo_mtu = atoi(argv[++i]);
2495
2496  } else if ( strcmp(k, "-G") == 0 ) {
2497   _CKHAVEARGS(1);
2498   sock_grp  = argv[++i];
2499  } else if ( strcmp(k, "-U") == 0 ) {
2500   _CKHAVEARGS(1);
2501   sock_user = argv[++i];
2502
2503  } else if ( strcmp(k, "--no-listen") == 0 ) {
2504#ifdef ROAR_SUPPORT_LISTEN
2505   sock_addr   = "";
2506   g_terminate = 1;
2507   g_no_listen = 1;
2508#endif
2509  } else if ( strcmp(k, "--client-fh") == 0 ) {
2510   _CKHAVEARGS(1);
2511   if ( clients_new_from_fh(atoi(argv[++i]), ROAR_PROTO_ROARAUDIO, ROAR_BYTEORDER_NETWORK, 1) == -1 ) {
2512    ROAR_ERR("main(*): Can not set client's fh");
2513    return 1;
2514   }
2515  } else if ( strcmp(k, "--close-fh") == 0 ) {
2516   _CKHAVEARGS(1);
2517#ifdef ROAR_HAVE_IO_POSIX
2518   close(atoi(argv[++i]));
2519#else
2520   i++;
2521   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
2522#endif
2523
2524  } else if ( strcmp(k, "--standby") == 0 ) {
2525   g_standby = 1;
2526  } else if ( strcmp(k, "--auto-standby") == 0 ) {
2527   g_autostandby = 1;
2528  } else {
2529   usage();
2530   return 1;
2531  }
2532
2533 }
2534#endif
2535
2536#ifdef ROAR_HAVE_MAIN_ARGS
2537 switch (action) {
2538  case START:
2539    // NO-OP.
2540   break;
2541  case RESTART:
2542    // NO-OP, done before.
2543   break;
2544  case STOP:
2545#ifdef ROAR_SUPPORT_LISTEN
2546    if ( restart_server(sock_addr, 0) == -1 ) {
2547     ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
2548     return 1;
2549    }
2550    return 0;
2551#else
2552    ROAR_ERR("--stop not supported");
2553    return 1;
2554#endif
2555   break;
2556  case SHUTDOWN:
2557#ifdef ROAR_SUPPORT_LISTEN
2558    if ( restart_server(sock_addr, 1) == -1 ) {
2559     ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
2560     return 1;
2561    }
2562    return 0;
2563#else
2564    ROAR_ERR("--shutdown not supported");
2565    return 1;
2566#endif
2567   break;
2568  case RESTART_RETRY:
2569#ifdef ROAR_SUPPORT_LISTEN
2570    if ( restart_server(sock_addr, 1) == -1 ) {
2571     ROAR_WARN("Can not terminate old server (again) (not running at %s?), tring to continue anyway", sock_addr);
2572     action = RESTART;
2573    }
2574#endif
2575   break;
2576 }
2577#endif
2578
2579#ifndef DEBUG
2580 // notify dbg if requested, if in DEBUG mode
2581 // do not able because it got enabled early.
2582 if ( g_verbose >= 4 )
2583  dbg_notify_cb_register();
2584#endif
2585
2586 if ( af_mode != AF_MODE_NONE ) {
2587  if ( add_authfile(af_file, af_type, af_mode, af_acclev) == -1 ) {
2588   ROAR_ERR("main(*): adding authfile '%s' failed!", af_file);
2589  }
2590 }
2591
2592#ifndef ROAR_WITHOUT_DCOMP_MIXER
2593 if ( m_drv != NULL ) {
2594  if ( add_hwmixer(m_drv, m_dev, m_opts, m_prim, m_count) == -1 ) {
2595   ROAR_ERR("main(*): adding mixer '%s' via '%s' failed!", m_dev, m_drv);
2596  }
2597 }
2598#endif
2599
2600#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2601 if ( s_dev != NULL ) {
2602  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
2603   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
2604  }
2605 }
2606#endif
2607
2608 add_default_output(o_drv, o_dev, o_opts, o_prim, o_count);
2609
2610 ROAR_INFO("Server config: rate=%i, bits=%i, chans=%i", ROAR_DBG_INFO_NOTICE, sa.rate, sa.bits, sa.channels);
2611
2612 if ( waveform_init() == -1 ) {
2613  ROAR_ERR("Can not initialize Waveform subsystem");
2614  return 1;
2615 }
2616
2617#ifndef ROAR_WITHOUT_DCOMP_MIDI
2618 if ( midi_init() == -1 ) {
2619  ROAR_ERR("Can not initialize MIDI subsystem");
2620 }
2621#endif
2622
2623#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2624 if ( ssynth_init() == -1 ) {
2625  ROAR_ERR("Can not initialize ssynth subsystem");
2626 }
2627#endif
2628
2629#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2630 if ( light_init(light_channels) == -1 ) {
2631  ROAR_ERR("Can not initialize light control subsystem");
2632 }
2633#endif
2634
2635#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2636 if ( rdtcs_init() == -1 ) {
2637  ROAR_ERR("Can not initialize RDTCS subsystem");
2638 }
2639#endif
2640
2641 if ( plugins_init() == -1 ) {
2642  ROAR_ERR("Can not initialize plugins");
2643 }
2644
2645#ifdef ROAR_SUPPORT_LISTEN
2646 if ( !g_no_listen ) {
2647  if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
2648   ROAR_ERR("Can not open listen socket!");
2649   return 1;
2650  }
2651 }
2652#endif
2653
2654 if ( output_buffer_init(&sa) == -1 ) {
2655  ROAR_ERR("Can not init output buffer!");
2656  return 1;
2657 }
2658
2659 if ( samples_init() == -1 ) {
2660  ROAR_ERR("Can not init samples!");
2661  return 1;
2662 }
2663
2664
2665 // we should handle this on microcontrollers, too.
2666#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
2667 signal(SIGINT,  on_sig_int);
2668 signal(SIGTERM, on_sig_term);
2669 signal(SIGCHLD, on_sig_chld);
2670 signal(SIGUSR1, on_sig_usr1);
2671 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
2672#endif
2673
2674 if ( g_config->memlock_level == -1 ) {
2675  g_config->memlock_level = MEMLOCK_DEFAULT;
2676 }
2677
2678 if ( memlock_set_level(g_config->memlock_level) == -1 ) {
2679  ROAR_WARN("Can not set memory locking level to target level.");
2680 }
2681
2682 if ( realtime ) {
2683#ifdef DEBUG
2684  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compile without -DDEBUG");
2685#endif
2686
2687#ifdef ROAR_HAVE_NICE
2688  // this stupid error check is because type of returned data of nice() changed
2689  // too often. On some systems it may return 0/-1, on some new nice value or
2690  // mixed forms of both.
2691  errno = 0;
2692  (void)nice(-5*realtime); // -5 for each --realtime
2693  if ( errno ) {
2694   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2695  }
2696#else
2697  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2698#endif
2699/*
2700#ifdef __linux__
2701  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
2702   ROAR_WARN("Can not set io priority: %s", strerror(errno));
2703#endif
2704*/
2705 }
2706
2707#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
2708 if ( setids & R_SETGID ) {
2709  if ( sock_grp == NULL ) {
2710   ROAR_ERR("Can not set GID if no groupname is supplied");
2711   return 1;
2712  }
2713  if ( (grp = getgrnam(sock_grp)) == NULL ) {
2714   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
2715   return 1;
2716  }
2717  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
2718   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
2719  }
2720  if ( !grp || setgid(grp->gr_gid) == -1 ) {
2721   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
2722  }
2723 }
2724#endif
2725
2726
2727 clients_set_pid(g_self_client, getpid());
2728#ifdef ROAR_HAVE_GETUID
2729 clients_set_uid(g_self_client, getuid());
2730#endif
2731#ifdef ROAR_HAVE_GETGID
2732 clients_set_gid(g_self_client, getgid());
2733#endif
2734 clients_get(g_self_client, &self);
2735
2736 if ( self == NULL ) {
2737  ROAR_ERR("Can not get self client!");
2738  return 1;
2739 }
2740
2741 strcpy(self->name, "RoarAudio daemon internal");
2742
2743 if ( roar_nnode_free(&(self->nnode)) == -1 )
2744  return 1;
2745
2746 // not fully correct but ok as workaorund
2747 // so tools assume that roard runs on the same machine as
2748 // they in case they use AF_UNIX:
2749 if ( roar_nnode_new(&(self->nnode), ROAR_SOCKET_TYPE_UNIX) == -1 )
2750  return 1;
2751
2752#ifdef ROAR_HAVE_FORK
2753 if ( daemon ) {
2754#ifdef ROAR_HAVE_SYSLOG
2755  roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
2756#else
2757  roar_debug_set_stderr_fh(-1);
2758#endif
2759
2760  close(ROAR_STDIN );
2761  close(ROAR_STDOUT);
2762  close(ROAR_STDERR);
2763
2764  if ( fork() )
2765   ROAR_U_EXIT(0);
2766
2767#ifdef ROAR_HAVE_SETSID
2768  setsid();
2769#endif
2770  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
2771 }
2772#endif
2773
2774#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2775 // early test for UID as we need this for the pidfile and the setuid()
2776 if ( sock_user != NULL ) {
2777  if ( (pwd = getpwnam(sock_user)) == NULL ) {
2778   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
2779   return 1;
2780  }
2781 }
2782#endif
2783
2784 ROAR_INFO("Process ID: %i", ROAR_DBG_INFO_INFO, (int)getpid());
2785
2786#ifdef SUPPORT_PIDFILE
2787 if ( pidfile != NULL ) {
2788  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
2789   ROAR_ERR("Can not write pidfile: %s", pidfile);
2790  } else {
2791   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
2792   roar_vio_close(&pidfile_vio);
2793  }
2794#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2795  if ( pwd || grp ) {
2796   if ( chown(pidfile, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 ) {
2797    ROAR_WARN("Can not change ownership of pidfile: %s: %s", pidfile, strerror(errno));
2798   }
2799  }
2800  if ( chmod(pidfile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1 ) {
2801   ROAR_WARN("Can not change permissions of pidfile: %s: %s", pidfile, strerror(errno));
2802  }
2803#endif
2804 }
2805#endif
2806
2807#ifdef ROAR_HAVE_CHROOT
2808 if (chrootdir) {
2809  if ( chroot(chrootdir) == -1 ) {
2810   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
2811   return 2;
2812  }
2813  if ( chdir("/") == -1 ) {
2814   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
2815   return 2;
2816  }
2817 }
2818#endif
2819
2820#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2821 if ( setids & R_SETUID ) {
2822  if ( sock_user == NULL ) {
2823   ROAR_ERR("Can not set UID if no username is supplied");
2824   return 1;
2825  }
2826  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
2827   ROAR_ERR("Can not set UserID: %s", strerror(errno));
2828   return 3;
2829  }
2830#ifdef ROAR_HAVE_GETUID
2831  clients_set_uid(g_self_client, getuid());
2832#endif
2833 }
2834#endif
2835
2836 // setup auth:
2837#if defined(ROAR_HAVE_GETUID) && defined(ROAR_HAVE_GETGID)
2838 if ( auth_setup(none_acclev, trust_acclev, trust_root, getuid(), getgid()) == -1 ) {
2839#else
2840 if ( auth_setup(none_acclev, trust_acclev, trust_root, -1, -1) == -1 ) {
2841#endif
2842  ROAR_ERR("Can not set up auth. Bad.");
2843  alive = 0;
2844 }
2845
2846 // Register with OpenSLP:
2847#ifdef ROAR_HAVE_LIBSLP
2848 if ( reg_slp ) {
2849  register_slp(0, sock_addr);
2850 }
2851#endif
2852
2853#ifdef ROAR_HAVE_LIBX11
2854 if ( reg_x11 ) {
2855  register_x11(0, sock_addr);
2856 }
2857#endif
2858
2859 ROAR_INFO("Startup complet", ROAR_DBG_INFO_INFO);
2860
2861 // start main loop...
2862 ROAR_INFO("Entering main loop", ROAR_DBG_INFO_INFO);
2863 main_loop(&sa, sysclocksync);
2864 ROAR_INFO("Left main loop", ROAR_DBG_INFO_INFO);
2865
2866 // clean up.
2867 clean_quit_prep();
2868 output_buffer_free();
2869
2870 roar_notify_core_free(NULL);
2871
2872 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
2873 return 0;
2874}
2875
2876void cleanup_listen_socket (int terminate) {
2877 int i;
2878
2879 ROAR_DBG("cleanup_listen_socket(terminate=%i) = (void)?", terminate);
2880
2881 ROAR_INFO("Cleaning up listen sockets", ROAR_DBG_INFO_INFO);
2882
2883 // Deregister from SLP:
2884#ifdef ROAR_HAVE_LIBSLP
2885 register_slp(1, NULL);
2886#endif
2887
2888#ifdef ROAR_HAVE_LIBX11
2889 register_x11(1, NULL);
2890#endif
2891
2892#ifdef ROAR_SUPPORT_LISTEN
2893 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
2894  if ( g_listen[i].used  ) {
2895   roar_vio_close(&(g_listen[i].sock));
2896
2897   g_listen[i].used = 0;
2898
2899#ifdef ROAR_HAVE_UNIX
2900   if ( server[i] != NULL )
2901    if ( *(server[i]) == '/' )
2902     unlink(server[i]);
2903#endif
2904  }
2905 }
2906
2907#endif
2908
2909 if ( terminate )
2910  g_terminate = 1;
2911}
2912
2913void clean_quit_prep (void) {
2914 cleanup_listen_socket(0);
2915
2916 plugins_free();
2917
2918#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2919 sources_free();
2920#endif
2921 streams_free();
2922 clients_free();
2923#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2924 ssynth_free();
2925#endif
2926#ifndef ROAR_WITHOUT_DCOMP_CB
2927 midi_cb_stop(); // stop console beep
2928#endif
2929#ifndef ROAR_WITHOUT_DCOMP_MIDI
2930 midi_free();
2931#endif
2932#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2933 light_free();
2934#endif
2935#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2936 rdtcs_free();
2937#endif
2938
2939 waveform_free();
2940
2941#ifdef SUPPORT_PIDFILE
2942 if ( pidfile != NULL )
2943  unlink(pidfile);
2944#endif
2945
2946 auth_free();
2947}
2948
2949void clean_quit (void) {
2950 ROAR_INFO("Shuting down", ROAR_DBG_INFO_INFO);
2951
2952 counters_print(ROAR_DEBUG_TYPE_INFO, 0);
2953
2954 clean_quit_prep();
2955// output_buffer_free();
2956
2957 roar_notify_core_free(NULL);
2958
2959 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
2960 exit(0);
2961}
2962
2963//ll
Note: See TracBrowser for help on using the repository browser.