source: roaraudio/roard/roard.c @ 4846:58d1d4cb7602

Last change on this file since 4846:58d1d4cb7602 was 4846:58d1d4cb7602, checked in by phi, 13 years ago

Auto select a stream if no stream has sync flag. (Closes: #30)

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