source: roaraudio/roard/roard.c @ 4338:7f711956a126

Last change on this file since 4338:7f711956a126 was 4338:7f711956a126, checked in by phi, 14 years ago

some more debugging code

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