source: roaraudio/roard/roard.c @ 4343:a67cbb88fbe0

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

some more debugging code

File size: 66.3 KB
RevLine 
[0]1//roard.c:
2
[668]3/*
[3358]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
[668]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[668]23 *
24 */
25
[0]26#include "roard.h"
27
[1494]28#ifdef ROAR_SUPPORT_LISTEN
[2530]29char * server[ROAR_MAX_LISTEN_SOCKETS];
[1494]30#endif
[60]31
[2104]32#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_FS_POSIX)
33#define SUPPORT_PIDFILE
34char * pidfile = NULL;
35#endif
36
[2530]37#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
[3374]38int    setids    = 0;
39#endif
40
41#ifdef ROAR_HAVE_LIBX11
42char * x11display = NULL;
[2530]43#endif
44
[4141]45int add_output (char * drv, char * dev, char * opts, int prim, int count);
46
[4324]47#ifdef DEBUG
48void dbg_notify_cb(struct roar_notify_core * core, struct roar_event * event, void * userdata) {
49 char buf[1024] = "";
[4338]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;
[4324]88
89 if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT ) {
[4338]90  snprintf(buf, sizeof(buf)-1, ".event_proxy=0x%.8x%s, ", (int)event->event_proxy, estr);
[4324]91  buf[sizeof(buf)-1] = 0;
92 }
93
[4338]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)",
[4324]95           core, event,
96           (int)event->flags,
97           (int)event->event,
[4338]98           (event->flags & ROAR_EVENT_FLAG_PROXYEVENT ? "" : estr),
[4324]99           buf,
100           event->emitter,
101           event->target,
102           event->target_type,
[4338]103           ttstr,
[4324]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
[1503]125#ifdef ROAR_HAVE_MAIN_ARGS
[0]126void usage (void) {
127 printf("Usage: roard [OPTIONS]...\n\n");
128
[68]129 printf("Misc Options:\n\n");
130 printf(
[775]131        " --daemon              - Bring the server into background after init\n"
[2970]132        " --verbose             - Be more verbose, can be used multiple times\n"
[71]133        " --terminate           - Terminate after last client quited\n"
[2109]134        " --start               - No op parameter (starting roard is default operation)\n"
[274]135        " --restart             - Trys to stop an old instance and start a new with new settings\n"
[2109]136        " --stop                - Stops a running roard (provide --pidfile!)\n"
137        " --shutdown            - Terminates a running roard (provide --pidfile!)\n"
[276]138        " --realtime            - Trys to get realtime priority,\n"
139        "                         give multible times for being more realtime\n"
[4245]140        " --memlock LEVEL       - Set default memory locking level to LEVEL\n"
[444]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"
[905]144        " --sysclocksync        - calculate exact sample rate using the system clock\n"
[2017]145        " --location  LOC       - Set lion readable location of server\n"
[2104]146#ifdef SUPPORT_PIDFILE
147        " --pidfile PIDFILE     - Write a pidfile at PIDFILE\n"
148#endif
[3688]149#ifdef ROAR_HAVE_SYSLOG
150        " --log-syslog          - Log Warnings, Errors, ... to syslog\n"
151#endif
[68]152       );
153
[3356]154 printf("\nPlugin Options:\n\n");
155 printf(
156        " --plugin-load FILE    - Load plugin FILE\n"
157       );
158
[68]159 printf("\nAudio Options:\n\n");
[0]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"
[3963]164        " --aiprofile  PROFILE  - Use the given audio profile\n"
[0]165       );
166
[2339]167 printf("\nStream Options:\n\n");
168 printf(
169        " --stream-flags D=F    - Set default flags for stream directions\n"
[4012]170        "                         D is the stream direction and F is a comma separated\n"
[2339]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
[932]175 printf("\nOutput Options:\n\n");
[974]176 printf(" -o  --odriver DRV     - Set the driver, use '--list-driver' to get a list\n");
[932]177 printf(" -O  --odevice DEV     - Set the device\n");
178 printf(" -oO OPTS              - Set output options\n");
179 printf(" -oN                   - Adds another output\n");
[961]180 printf(" -oP                   - Mark output as primary\n");
[4225]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
[932]186
[2485]187#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]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"
[1512]192        " -sN                   - Adds another source\n"
[0]193        " -sP                   - Make souce as primary\n"
194       );
[2270]195 printf(" --list-sources        - List all sources\n");
[2485]196#endif
[0]197
[280]198 printf("\nCodec Filter Options:\n\n");
199 printf(" --list-cf             - List all codec filter\n"
200       );
[0]201
[2500]202#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1923]203 printf("\nMIDI Options:\n\n");
[1924]204 printf(" --midi-no-console     - Disable console based MIDI synth\n"
[2444]205        " --midi-console-enable - Enables the console based MIDI synth\n"
[1924]206        " --midi-console DEV    - Set device for MIDI console\n"
[2489]207#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]208        " --ssynth-enable       - Enable simple software synth\n"
209        " --ssynth-disable      - Disable simple software synth\n"
[2489]210#endif
[1924]211       );
[2500]212#endif
[1923]213
[2494]214#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]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       );
[2494]219#endif
[1923]220
[2725]221#ifndef ROAR_WITHOUT_DCOMP_RDTCS
[3339]222 printf("\nRadio Data and Transmitter Control System Options:\n\n");
[2725]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
[3374]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
[0]239 printf("\nServer Options:\n\n");
240 printf(" -t  --tcp             - Use TCP listen socket\n"
[3992]241#ifdef ROAR_HAVE_UNIX
[0]242        " -u  --unix            - Use UNIX Domain listen socket (default)\n"
[3992]243#endif
[508]244#ifdef ROAR_HAVE_LIBDNET
245        " -n  --decnet          - use DECnet listen socket\n"
246#endif
[518]247        " -4                    - Use IPv4 connections (implies -t)\n"
[3837]248#ifdef AF_INET6
[518]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"
[4012]253        "                         this is normaly not useful.\n"
[518]254#endif
[0]255        " -p  --port            - TCP Port to bind to\n"
256        " -b  --bind            - IP/Hostname to bind to\n"
[1115]257        "     --sock            - Filename for UNIX Domain Socket\n"
[2530]258        "     --proto PROTO     - Use PROTO as protocol on Socket\n"
[3252]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"
[4012]263        "     --proto-chans C   - Set number of channels parameter for protocol\n"
[3954]264        "     --proto-aiprofile PROFILE\n"
265        "                       - Sets the audio profile for socket\n"
[3956]266        "     --proto-profile P - Set profile for listen socket\n"
[3127]267        "     --list-proto      - List supported protocols\n"
[3956]268        "     --list-profiles   - List supported profiles for --proto-profile\n"
[2530]269        "     --new-sock        - Parameters for new socket follows\n"
[1993]270#ifdef ROAR_HAVE_LIBSLP
271        "     --slp             - Enable OpenSLP support\n"
272#endif
[3373]273#ifdef ROAR_HAVE_LIBX11
274        "     --x11             - Enable X11 support\n"
275#endif
[3039]276        " --jumbo-mtu MTU       - Sets the MTU for Jumbo Packets\n"
[450]277        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: %s)\n"
[2737]278        "                         You need the permissions to change the GID\n"
[444]279        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
[2737]280        "                         You need the permissions to change the UID (normaly only root has)\n"
[548]281        " --no-listen           - Do not listen for new clients\n"
[4012]282        "                         (only useful for relaing, impleys --terminate)\n"
[274]283        " --client-fh           - Comunicate with a client over this handle\n"
[4012]284        "                         (only useful for relaing)\n"
[920]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",
[2755]288#ifdef ROAR_DEFAULT_SOCKGRP
[450]289        ROAR_DEFAULT_SOCKGRP
[2755]290#else
291        "(none)"
292#endif
[0]293       );
294// printf("\n Options:\n\n");
295 printf("\n");
296}
[3127]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
[3603]305#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
[3257]306 printf("  simple        WM LRX - PulseAudio simple protocol\n");
[3597]307#endif
[3684]308#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
309 printf("  rsound        W      - RSound emulation\n");
310#endif
[3981]311#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
312 printf("  rplay         W      - RPlay emulation\n");
313#endif
[3127]314}
315
[1503]316#endif
[0]317
[4101]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
[2109]335int restart_server (char * server, int terminate) {
[579]336 struct roar_connection con;
[2111]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
[4055]344 ROAR_INFO("restart_server(server='%s', terminate=%i): trying to restart server", ROAR_DBG_INFO_INFO, server, terminate);
345
[2111]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 ) {
[2113]357     ok = kill(pid, SIGUSR1);
[2111]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
[2109]372
[579]373 if ( roar_connect(&con, server) == -1 ) {
374  return -1;
375 }
376
[2109]377 if ( roar_terminate(&con, terminate) == -1 ) {
[579]378  return -1;
379 }
380
381 return roar_disconnect(&con);
382}
383
[444]384#define R_SETUID 1
385#define R_SETGID 2
386
[1609]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
[2291]400 g_config->streams[ROAR_DIR_PLAY    ].flags = ROAR_FLAG_META;
[2413]401 g_config->streams[ROAR_DIR_OUTPUT  ].flags = ROAR_FLAG_PASSMIXER;
[2264]402 g_config->streams[ROAR_DIR_FILTER  ].flags = ROAR_FLAG_SYNC;
[1899]403 g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC;
[2160]404 g_config->streams[ROAR_DIR_BIDIR   ].flags = ROAR_FLAG_ANTIECHO;
[1899]405
[2017]406 g_config->location = "***default***";
407
[4245]408 g_config->memlock_level = -1;
409
[1609]410 return 0;
411}
412
[2530]413#ifdef ROAR_SUPPORT_LISTEN
414int init_listening (void) {
415 int i;
416
[3128]417 memset(g_listen, 0, sizeof(g_listen));
418
[2530]419 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
[3128]420  g_listen[i].proto  = ROAR_PROTO_ROARAUDIO;
[2530]421  server[i]          = NULL;
422 }
423
424 return 0;
425}
426
[3731]427int get_listen(struct roard_listen ** sock, char *** sockname) {
[3730]428 int i;
429
430 if ( sock == NULL )
431  return -1;
432
433 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
[3802]434  if ( ! g_listen[i].used ) {
[3730]435   server[i] = NULL;
[3731]436   *sock = &(g_listen[i]);
[3730]437
[3733]438   if ( sockname != NULL )
[3730]439    *sockname = &(server[i]);
[3733]440
441   return 0;
[3730]442  }
443 }
444
445 return -1;
446}
447
[3956]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:
[3992]460#ifdef ROAR_HAVE_UNIX
[4058]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"},
[3992]467#endif
468#ifdef ROAR_HAVE_IPV4
[4058]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"},
[3992]475#endif
476#ifdef ROAR_HAVE_LIBDNET
[4058]477 {"roar-dnet",      ROAR_SOCKET_TYPE_DECNET, 0,                 ROAR_DEFAULT_LISTEN_OBJECT,
478                    ROAR_PROTO_ROARAUDIO, -1, NULL,
479                    "RoarAudio default DECnet"},
[3992]480#endif
481#ifdef ROAR_HAVE_UNIX
[4058]482 {"roar-abstract",  ROAR_SOCKET_TYPE_UNIX,   0,                 "+abstract",        ROAR_PROTO_ROARAUDIO, -1, NULL,
483                    "RoarAudio abstract namespace profile"},
[3992]484#endif
[3956]485
486 // EsounD:
[3992]487#if !defined(ROAR_WITHOUT_DCOMP_EMUL_ESD) && defined(ROAR_HAVE_H_ESD)
488#ifdef ROAR_HAVE_UNIX
[4058]489 {"esd-unix",       ROAR_SOCKET_TYPE_UNIX,   0,                 ROAR_DEFAULT_ESD_GSOCK,
490                    ROAR_PROTO_ESOUND,    -1, NULL,
491                    "EsounD default local profile"},
[3992]492#endif
493#ifdef ROAR_HAVE_IPV4
[4058]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"},
[3992]500#endif
501#endif
[3956]502
503 // RSound:
[3992]504#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
505#ifdef ROAR_HAVE_UNIX
[4058]506 {"rsound-unix",    ROAR_SOCKET_TYPE_UNIX,   0,                 ROAR_DEFAULT_RSOUND_GSOCK,
507                    ROAR_PROTO_RSOUND,    -1, NULL,
508                    "RSound default local profile"},
[3992]509#endif
510#ifdef ROAR_HAVE_IPV4
[4058]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"},
[3992]517#endif
518#ifdef ROAR_HAVE_LIBDNET
[4058]519 {"rsound-dnet",    ROAR_SOCKET_TYPE_DECNET, 0,                 ROAR_DEFAULT_RSOUND_OBJECT,
520                    ROAR_PROTO_RSOUND,    -1, NULL,
521                    "RSound DECnet profile"},
[3992]522#endif
523#endif
[3957]524
525 // PulseAudio Simple:
[3992]526#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
527#ifdef ROAR_HAVE_IPV4
[4058]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"},
[3992]534#endif
535#endif
[3987]536
537 // RPlay:
[3992]538#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
539#ifdef ROAR_HAVE_IPV4
[4058]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"},
[3992]546#endif
547#endif
[3987]548
549 // End of List:
[3956]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;
[3987]557 char port[8];
[3956]558
[3990]559 printf("Name           Type    Address          Port    Protocol  Dir        Audio Profile - Description\n");
[3956]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  }
[3987]572
573  if ( p->port ) {
[4049]574   snprintf(port, sizeof(port)-1, "%i", p->port);
575   port[sizeof(port)-1] = 0;
[3987]576  } else {
577   strcpy(port, "(none)");
578  }
579
580  printf("%-14s %-7s %-16s %-7s %-9s %-10s %-13s - %s\n",
[3956]581           p->name,
582           type,
[3987]583           p->sockaddr, port,
[3956]584           roar_proto2str(p->proto),
[3957]585           p->dir == -1 ? "(none)" : roar_dir2str(p->dir), p->aiprofile == NULL ? "(none)" : p->aiprofile,
[3956]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
[3959]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   }
[3956]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
[3252]626int add_listen (char * addr, int port, int sock_type, char * user, char * group, int proto, int dir, struct roar_audio_info * info) {
[2530]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;
[3907]637#ifdef ROAR_HAVE_UNIX
[2530]638 int    sock;
[3907]639#endif
[2530]640 int    i;
641
[4055]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
[2530]647 if ( *addr != 0 ) {
648  for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
[3802]649   if ( ! g_listen[i].used ) {
[2530]650    sockid = i;
651    break;
652   }
653  }
654
655  if ( sockid == -1 )
656   return -1;
657
[3128]658  g_listen[sockid].proto = proto;
[2530]659
660  ROAR_DBG("add_listen(*): proto=0x%.4x", proto);
661
[3802]662  if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) {
[2530]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);
[3040]671     ROAR_ERR("Can not open listen socket: Socket allready in use");
[2530]672     return 1;
673    } else {
674     unlink(addr);
[3802]675     if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) {
[3040]676      ROAR_ERR("Can not open listen socket: %s", strerror(errno));
[2530]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 {
[3041]688    ROAR_ERR("Can not open listen socket: %s", strerror(errno));
[2530]689    return 1;
690   }
691  }
692
693#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[2753]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   }
[2530]698  }
699#endif
700#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[2752]701  if ( user ) {
[2530]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 == '/' ) {
[2753]710   if ( grp || pwd ) {
711     if ( chown(addr, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 )
[2530]712      return 1;
[2753]713   }
[2530]714#ifdef ROAR_HAVE_GETUID
[2753]715   if ( grp ) {
[2530]716    if ( getuid() == 0 )
717     if ( chmod(addr, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
718      return 1;
[2753]719   }
[2530]720#endif
721  }
722#endif
723 }
724
[2736]725 // in case we opened the listening socket correctly.
[3256]726 if ( dir == -1 )
727  dir = ROAR_DIR_PLAY;
728
[3252]729 g_listen[sockid].inst.stpl.dir = dir;
730 memcpy(&(g_listen[sockid].inst.stpl.info), info, sizeof(struct roar_audio_info));
[3256]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
[3802]752 g_listen[sockid].used = 1;
753 server[sockid]        = addr;
754
[2530]755 return 0;
756}
757#endif
758
[2339]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) {
[2340]783   case '+': k++; op = ROAR_SET_FLAG;   break;
784   case '-': k++; op = ROAR_RESET_FLAG; break;
[2339]785   default:
[2340]786     op = ROAR_SET_FLAG;
[2339]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;
[2413]803  } else if ( !strcmp(k, "passmixer") ) {
804   flag = ROAR_FLAG_PASSMIXER;
[2339]805  } else {
806   return -1;
807  }
808
809  g_config->streams[dir].flags |= flag;
810
[2340]811  if ( op == ROAR_RESET_FLAG )
[2339]812   g_config->streams[dir].flags -= flag;
813 }
814
815 return 0;
816}
817
[4140]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) {
[4141]822 char * drvs[] = {
[4189]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
[4144]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:
[4140]837  NULL
838 };
839 int i;
840 int ret;
[4144]841 int _alive;
[4140]842
843 if ( drv != NULL )
844  return add_output(drv, dev, opts, prim, count);
845
846 if ( dev != NULL ) {
[4141]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);
[4140]848 }
849
850 for (i = 0; drvs[i] != NULL; i++) {
[4141]851  ROAR_INFO("add_default_output(*): trying driver %s", ROAR_DBG_INFO_INFO, drvs[i]);
[4144]852  _alive = alive; // save global alive setting
853
[4140]854  ret = add_output(drvs[i], dev, opts, prim, count);
855  if ( ret != -1 )
856   return ret;
[4144]857
858  alive = _alive; // restore global alive setting
[4141]859  ROAR_INFO("add_default_output(*): Driver %s faild to load", ROAR_DBG_INFO_VERBOSE, drvs[i]);
[4140]860 }
861
862 return -1;
863}
864#endif
865
[1145]866int add_output (char * drv, char * dev, char * opts, int prim, int count) {
[933]867 int stream;
868 struct roar_stream * s;
869 struct roar_stream_server * ss;
[938]870 char * k, * v;
[1208]871#ifdef ROAR_DRIVER_CODEC
872 char * to_free = NULL;
873#endif
[1926]874 int sync = 0, f_mmap = 0;
[1522]875 int32_t blocks = -1, blocksize = -1;
[1920]876 int dir = ROAR_DIR_OUTPUT;
[1991]877 int error = 0;
878 // DMX:
879 int32_t channel  = -1;
880 int32_t universe = -1;
881 uint16_t tu16;
[2085]882 float q = -32e6;
[933]883
[4055]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);
[933]885
[1145]886 if ( drv == NULL && count == 0 ) {
[4141]887#ifdef ROAR_DRIVER_DEFAULT
[1145]888  drv  = ROAR_DRIVER_DEFAULT;
889  prim = 1;
890  sync = 1;
[1208]891
892#ifdef ROAR_DRIVER_CODEC
893  if ( opts == NULL ) {
894   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
895  }
896#endif
[4141]897#else
898  ROAR_ERR("add_output(*): Can not find default driver");
899  return -1;
900#endif
[1145]901 }
902
[1227]903 if ( opts == NULL && count == 0 ) {
904  sync = 1;
905  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
906 }
907
[2785]908 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
909
[933]910 if ( (stream = streams_new()) == -1 ) {
[938]911  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]912  if ( prim ) alive = 0;
[933]913  return -1;
914 }
915
[2785]916 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
917
[933]918 streams_get(stream, &ss);
919 s = ROAR_STREAM(ss);
920
[2785]921 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
922
[2511]923 memset(&(s->info), 0xFF, sizeof(struct roar_audio_info)); // set everything to -1
[933]924
925 s->pos_rel_id = -1;
926// s->info.codec = codec;
927
[2785]928 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
929
[2788]930 if ( opts == NULL ) {
931  k = NULL;
932 } else {
933  k = strtok(opts, ",");
934 }
[2786]935
936 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s'): initial k='%s'(%p)", drv, dev, opts, k, k);
937
[938]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 ) {
[2511]953   if ( (s->info.codec = roar_str2codec(v)) == -1 ) {
[938]954    ROAR_ERR("add_output(*): unknown codec '%s'", v);
[1991]955    error++;
[938]956   }
[2085]957  } else if ( strcmp(k, "q") == 0 ) {
958   q = atof(v);
[1221]959  } else if ( strcmp(k, "blocks") == 0 ) {
960   blocks = atoi(v);
[1522]961  } else if ( strcmp(k, "blocksize") == 0 ) {
962   blocksize = atoi(v);
[1926]963  } else if ( strcmp(k, "mmap") == 0 ) {
964   f_mmap = 1;
[1919]965  } else if ( strcmp(k, "subsystem") == 0 ) {
966   if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) {
[1920]967    dir = ROAR_DIR_OUTPUT;
[2494]968#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1919]969   } else if ( !strcasecmp(v, "midi") ) {
970    dir = ROAR_DIR_MIDI_OUT;
[2494]971#endif
972#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1919]973   } else if ( !strcasecmp(v, "light") ) {
974    dir = ROAR_DIR_LIGHT_OUT;
[2494]975#endif
976#ifndef ROAR_WITHOUT_DCOMP_RAW
[2234]977   } else if ( !strcasecmp(v, "raw") ) {
978    dir = ROAR_DIR_RAW_OUT;
[2494]979#endif
[2681]980   } else if ( !strcasecmp(v, "complex") ) {
981    dir = ROAR_DIR_COMPLEX_OUT;
[1919]982   } else {
[2494]983    ROAR_ERR("add_output(*): unknown/unsupported subsystem '%s'", k);
[1991]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++;
[1919]993   }
[1991]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
[2115]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   }
[1991]1007
[1032]1008  } else if ( strcmp(k, "meta") == 0 ) {
1009   streams_set_flag(stream, ROAR_FLAG_META);
[1117]1010  } else if ( strcmp(k, "sync") == 0 ) {
1011   sync = 1;
[1221]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 ) {
[1531]1018   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
[2413]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);
[938]1023  } else {
1024   ROAR_ERR("add_output(*): unknown option '%s'", k);
[1991]1025   error++;
1026  }
1027
1028  if ( error ) {
[938]1029   streams_delete(stream);
[982]1030   if ( prim ) alive = 0;
[1208]1031#ifdef ROAR_DRIVER_CODEC
1032   if ( to_free != NULL )
1033    free(to_free);
1034#endif
[938]1035   return -1;
1036  }
1037
1038  k = strtok(NULL, ",");
1039 }
1040
[2785]1041 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
1042
[2511]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
[1919]1094 if ( streams_set_dir(stream, dir, 1) == -1 ) {
1095  streams_delete(stream);
1096  return -1;
1097 }
1098
[1208]1099#ifdef ROAR_DRIVER_CODEC
1100 if ( to_free != NULL )
1101  free(to_free);
1102#endif
1103
[2511]1104 if ( s->info.codec == ROAR_CODEC_ALAW || s->info.codec == ROAR_CODEC_MULAW )
[941]1105  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
1106
[2511]1107 ROAR_STREAM_SERVER(s)->codec_orgi = s->info.codec;
[933]1108
[2364]1109 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) {
[1581]1110  ss->driver_id = -1; // don't close a driver not opened...
1111  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
[933]1112  streams_delete(stream);
[938]1113  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]1114  if ( prim ) alive = 0;
[933]1115  return -1;
1116 }
1117
[1528]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
[1221]1121 if ( blocks != -1 )
1122  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
1123
[1522]1124 if ( blocksize != -1 )
1125  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
1126
[1991]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
[1156]1137 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
1138
[938]1139 streams_set_fh(stream, -1); // update some internal structures
1140
[2087]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
[2490]1146 client_stream_add(g_self_client, stream);
[933]1147
[1200]1148 if ( prim ) {
[961]1149  streams_mark_primary(stream);
[1200]1150  s->pos_rel_id = stream;
1151 }
[961]1152
[1120]1153 if ( sync ) {
[1117]1154  streams_set_flag(stream, ROAR_FLAG_SYNC);
[1120]1155 } else {
1156  streams_reset_flag(stream, ROAR_FLAG_SYNC);
1157 }
[1117]1158
[1926]1159 if ( f_mmap )
1160  streams_set_flag(stream, ROAR_FLAG_MMAP);
1161
[2511]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);
[933]1163 return 0;
[932]1164}
1165
[3373]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
[4103]1172 if ( (x11con = roar_x11_connect(x11display)) == NULL ) {
[4155]1173  ROAR_ERR("Can not connect to X11 server for %sregistering", unreg ? "un" : "");
[3373]1174  return -1;
[4103]1175 }
[3373]1176
1177 if ( unreg ) {
[4103]1178  if ( roar_x11_delete_prop(x11con, "ROAR_SERVER") == -1 ) {
[3373]1179   ret = -1;
[4103]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  }
[3373]1184 } else {
[4103]1185  if ( roar_x11_set_prop(x11con, "ROAR_SERVER", sockname) == -1 ) {
[3373]1186   ret = -1;
[4103]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  }
[3373]1191 }
1192
1193 roar_x11_disconnect(x11con);
1194
1195 return ret;
1196}
1197#endif
[1993]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] = "";
[2017]1214 char * location;
[1993]1215
1216 if ( sockname != NULL )
1217  sn = sockname;
1218
[3076]1219 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE_ROAR "://%s", sn);
[1993]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) {
[2017]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
[1995]1236  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
[2494]1237#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1238                               "(light-channels=%i),"
1239#endif
1240                               "(location=%s)",
[1995]1241           g_sa->rate, g_sa->channels, g_sa->bits,
[2494]1242#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[2017]1243           g_light_state.channels,
[2494]1244#endif
[2017]1245           location
[1995]1246          );
1247
[1993]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);
[1994]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 }
[1993]1265
[1994]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;
[1993]1279 }
1280
1281 SLPClose(hslp);
1282 return 0;
1283#else
1284 return -1;
1285#endif
1286}
1287
1288
1289// MAIN:
1290
[4053]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                       }
[4052]1295
[1503]1296#ifdef ROAR_HAVE_MAIN_ARGS
[0]1297int main (int argc, char * argv[]) {
[1503]1298#else
1299int main (void) {
1300#endif
1301#ifdef ROAR_HAVE_MAIN_ARGS
[0]1302 int i;
1303 char * k;
[1503]1304#endif
[2787]1305#if defined(ROAR_SUPPORT_LISTEN) && defined(ROAR_HAVE_GETUID)
[905]1306 char user_sock[80]  = {0};
[1494]1307#endif
[1609]1308 struct roar_audio_info sa, max_sa;
1309 struct roard_config config;
[1486]1310#ifdef ROAR_HAVE_FORK
[905]1311 int    daemon       = 0;
[1486]1312#endif
[905]1313 int    realtime     = 0;
1314 int    sysclocksync = 0;
[1207]1315 char * driver    = NULL;
1316 char * device    = NULL;
[1503]1317#ifdef ROAR_HAVE_MAIN_ARGS
[1207]1318 char * opts      = NULL;
[1503]1319#endif
[60]1320// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
[1494]1321#ifdef ROAR_SUPPORT_LISTEN
[2530]1322 int    port       = ROAR_DEFAULT_PORT;
1323 char * sock_addr  = NULL;
1324 int    sock_proto = ROAR_PROTO_ROARAUDIO;
[3252]1325 int    sock_dir   = -1;
1326 struct roar_audio_info sock_info = {0, 0, 0, 0};
[1494]1327#endif
[0]1328 int               drvid;
[2486]1329#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]1330 char * s_drv     = "cf";
[1110]1331 char * s_dev     = NULL;
[444]1332 char * s_con     = NULL;
1333 char * s_opt     = NULL;
1334 int    s_prim    = 0;
[2486]1335#endif
[1207]1336 char * o_drv     = getenv("ROAR_DRIVER");
1337 char * o_dev     = getenv("ROAR_DEVICE");
[932]1338 char * o_opts    = NULL;
[961]1339 int    o_prim    = 0;
[1145]1340 int    o_count   = 0;
[2494]1341#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]1342 int    light_channels = LIGHT_CHANNELS_DEFAULT;
[2494]1343#endif
[2751]1344#ifdef ROAR_DEFAULT_SOCKGRP
[450]1345 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
[2751]1346#else
1347 char * sock_grp  = NULL;
1348#endif
[444]1349 char * sock_user = NULL;
[1494]1350#ifdef ROAR_SUPPORT_LISTEN
[517]1351 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
[1494]1352#endif
[1993]1353#ifdef ROAR_HAVE_LIBSLP
1354 int    reg_slp   = 0;
1355#endif
[3373]1356#ifdef ROAR_HAVE_LIBX11
1357 int    reg_x11   = 0;
1358#endif
[1486]1359#ifdef ROAR_HAVE_CHROOT
[444]1360 char * chrootdir = NULL;
[1486]1361#endif
1362#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[446]1363 struct group   * grp  = NULL;
[1486]1364#endif
1365#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[446]1366 struct passwd  * pwd  = NULL;
[1486]1367#endif
1368#ifdef ROAR_HAVE_GETSERVBYNAME
[446]1369 struct servent * serv = NULL;
[1486]1370#endif
[0]1371 DRIVER_USERDATA_T drvinst;
[39]1372 struct roar_client * self = NULL;
[508]1373#ifdef ROAR_HAVE_LIBDNET
1374 char decnethost[80];
1375#endif
[2104]1376#ifdef SUPPORT_PIDFILE
1377 struct roar_vio_calls pidfile_vio;
1378#endif
[0]1379
[2784]1380 ROAR_DBG("main(*): starting roard...");
1381
[0]1382 g_standby       =  0;
[920]1383 g_autostandby   =  0;
[982]1384 alive           =  1;
[1494]1385#ifdef ROAR_SUPPORT_LISTEN
[1155]1386 g_no_listen     =  0;
[1494]1387#else
1388 g_terminate     =  1;
1389#endif
[0]1390
[2970]1391 g_verbose       = ROAR_DBG_INFO_NONE;
1392
[0]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
[1609]1398 g_sa        = &sa;
1399 g_max_sa    = &max_sa;
1400
1401 memcpy(g_max_sa, g_sa, sizeof(max_sa));
1402
[4101]1403 counters_init();
1404
[1609]1405 g_config = &config;
1406
1407 if ( init_config() == -1 ) {
1408  ROAR_ERR("Can not init default config!");
1409  return 1;
1410 }
[0]1411
[2968]1412 // load config
1413 roar_libroar_get_config();
1414
[4323]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
[4324]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
[2530]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
[2500]1438#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1924]1439 if ( midi_init_config() == -1 ) {
1440  ROAR_ERR("Can not init MIDI config!");
1441  return 1;
1442 }
[2500]1443#endif
[60]1444
[2489]1445#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1446 if ( ssynth_init_config() == -1 ) {
1447  ROAR_ERR("Can not init ssynth config!");
1448  return 1;
1449 }
[2489]1450#endif
[2451]1451
[2718]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
[3354]1459 if ( plugins_preinit() == -1 ) {
1460  ROAR_ERR("Can not pre-init plugins!");
1461  return 1;
1462 }
1463
[1494]1464#ifdef ROAR_SUPPORT_LISTEN
[2767]1465#ifndef ROAR_TARGET_WIN32
[2530]1466 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
[2767]1467#else
1468 sock_addr = ROAR_DEFAULT_HOST;
1469#endif
1470
[1753]1471#ifdef ROAR_HAVE_GETUID
[1486]1472 if ( getuid() != 0 && getenv("HOME") != NULL ) {
[3959]1473/*
[1486]1474  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
[3959]1475*/
1476  roar_env_render_path_r(user_sock, sizeof(user_sock), "~/" ROAR_DEFAULT_SOCK_USER);
[2530]1477  sock_addr = user_sock;
[3959]1478  ROAR_DBG("main(*): setting sock_addr='%s'", sock_addr);
[60]1479 }
[1753]1480#endif
[60]1481
[279]1482 if ( getenv("ROAR_SERVER") != NULL )
[2530]1483  sock_addr = getenv("ROAR_SERVER");
[1494]1484#endif
[279]1485
[63]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
[64]1496 if ( (g_self_client = clients_new()) == -1 ) {
1497  ROAR_ERR("Can not create self client!");
1498  return 1;
1499 }
1500
[2485]1501#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1502 if ( sources_init() == -1 ) {
1503  ROAR_ERR("Can not init sources!");
1504  return 1;
1505 }
1506
[64]1507 if ( (sources_set_client(g_self_client)) == -1 ) {
1508  ROAR_ERR("Can not init set source client!");
1509  return 1;
1510 }
[2485]1511#endif
[64]1512
[1503]1513#ifdef ROAR_HAVE_MAIN_ARGS
[0]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
[2109]1521  } else if ( strcmp(k, "--start") == 0 ) {
1522   // this is a no op
[579]1523  } else if ( strcmp(k, "--restart") == 0 ) {
[1494]1524#ifdef ROAR_SUPPORT_LISTEN
[2530]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);
[579]1527   }
[1494]1528#else
1529   ROAR_ERR("--restart not supported");
1530#endif
[2109]1531  } else if ( strcmp(k, "--shutdown") == 0 ) {
1532#ifdef ROAR_SUPPORT_LISTEN
[2530]1533   if ( restart_server(sock_addr, 1) == -1 ) {
1534    ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
[2109]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
[2530]1544   if ( restart_server(sock_addr, 0) == -1 ) {
1545    ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
[2109]1546    return 1;
1547   }
1548   return 0;
1549#else
1550   ROAR_ERR("--stop not supported");
1551   return 1;
1552#endif
1553
[579]1554
[775]1555  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
[1486]1556#ifdef ROAR_HAVE_FORK
[775]1557   daemon = 1;
[1486]1558#else
1559   ROAR_ERR("--daemon not supported");
1560#endif
[2970]1561  } else if ( strcmp(k, "--verbose") == 0 ) {
1562   g_verbose++;
[71]1563  } else if ( strcmp(k, "--terminate") == 0 ) {
1564   g_terminate = 1;
[905]1565  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1566   sysclocksync = 1000;
[275]1567  } else if ( strcmp(k, "--realtime") == 0 ) {
[276]1568   realtime++;
[4245]1569  } else if ( strcmp(k, "--memlock") == 0 ) {
1570   _CKHAVEARGS(1);
1571   g_config->memlock_level = memlock_str2level(argv[++i]);
[444]1572  } else if ( strcmp(k, "--chroot") == 0 ) {
[4052]1573   _CKHAVEARGS(1);
[1486]1574#ifdef ROAR_HAVE_CHROOT
[444]1575   chrootdir = argv[++i];
[1486]1576#else
1577   ROAR_ERR("--chroot not supported");
1578   i++;
1579#endif
[444]1580  } else if ( strcmp(k, "--setgid") == 0 ) {
[1486]1581#ifdef ROAR_HAVE_SETGID
[444]1582   setids |= R_SETGID;
[1486]1583#else
1584   ROAR_ERR("--setgid not supported");
1585#endif
[444]1586  } else if ( strcmp(k, "--setuid") == 0 ) {
[1486]1587#ifdef ROAR_HAVE_SETUID
[444]1588   setids |= R_SETUID;
[1486]1589#else
1590   ROAR_ERR("--setuid not supported");
1591#endif
[2017]1592  } else if ( strcmp(k, "--location") == 0 ) {
[4052]1593   _CKHAVEARGS(1);
[2017]1594   g_config->location = argv[++i];
[2104]1595  } else if ( strcmp(k, "--pidfile") == 0 ) {
[4052]1596   _CKHAVEARGS(1);
[2104]1597#ifdef SUPPORT_PIDFILE
1598   pidfile = argv[++i];
1599#else
1600   ROAR_ERR("--pidfile not supported");
1601   i++;
1602#endif
[3688]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
[68]1610
[3356]1611  } else if ( strcmp(k, "--plugin-load") == 0 ) {
[4052]1612   _CKHAVEARGS(1);
[3356]1613   if ( plugins_load(argv[++i]) == -1 ) {
1614    ROAR_ERR("Can not load plugin");
1615   }
1616
[280]1617  } else if ( strcmp(k, "--list-cf") == 0 ) {
1618   print_codecfilterlist();
1619   return 0;
1620
[0]1621  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
[4052]1622   _CKHAVEARGS(1);
[0]1623   sa.rate = atoi(argv[++i]);
1624  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
[4052]1625   _CKHAVEARGS(1);
[0]1626   sa.bits = atoi(argv[++i]);
1627  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
[4052]1628   _CKHAVEARGS(1);
[0]1629   sa.channels = atoi(argv[++i]);
1630
[3962]1631  } else if ( strcmp(k, "--aiprofile") == 0 ) {
[4052]1632   _CKHAVEARGS(1);
[3962]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
[2339]1639  } else if ( strcmp(k, "--stream-flags") == 0 ) {
[4052]1640   _CKHAVEARGS(1);
[2339]1641   if ( update_stream_flags(argv[++i]) == -1 ) {
1642    ROAR_ERR("Can not set stream flags");
1643    return 1;
1644   }
1645
[0]1646  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
[4052]1647   _CKHAVEARGS(1);
[0]1648   driver = argv[++i];
1649   if ( strcmp(driver, "list") == 0 ) {
[974]1650    ROAR_WARN("The option is obsolete, use --list-driver!");
[0]1651    print_driverlist();
[973]1652    return 0;
[0]1653   }
1654  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
[4052]1655   _CKHAVEARGS(1);
[0]1656   device = argv[++i];
1657  } else if ( strcmp(k, "-dO") == 0 ) {
[4052]1658   _CKHAVEARGS(1);
[0]1659   opts = argv[++i];
[973]1660  } else if ( strcmp(k, "--list-driver") == 0 ) {
1661   print_driverlist();
1662   return 0;
[0]1663
[932]1664  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
[4052]1665   _CKHAVEARGS(1);
[932]1666   o_drv  = argv[++i];
1667  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
[4052]1668   _CKHAVEARGS(1);
[932]1669   o_dev  = argv[++i];
1670  } else if ( strcmp(k, "-oO") == 0 ) {
[4052]1671   _CKHAVEARGS(1);
[932]1672   o_opts = argv[++i];
[961]1673  } else if ( strcmp(k, "-oP") == 0 ) {
1674   o_prim = 1;
[932]1675  } else if ( strcmp(k, "-oN") == 0 ) {
[1145]1676   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1677    o_count++;
1678
[961]1679   o_drv  = o_dev = o_opts = NULL;
1680   o_prim = 0;
[932]1681
[0]1682  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
[4052]1683   _CKHAVEARGS(1);
[2485]1684#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]1685   s_drv = argv[++i];
[2485]1686#else
1687   ROAR_ERR("main(*): No support for sources compiled in");
[4052]1688   i++;
[2485]1689#endif
[550]1690  } else if ( strcmp(k, "-S") == 0 ) {
[4052]1691   _CKHAVEARGS(1);
[2485]1692#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1693   s_dev = argv[++i];
[2485]1694#else
1695   ROAR_ERR("main(*): No support for sources compiled in");
[4052]1696   i++;
[2485]1697#endif
[0]1698  } else if ( strcmp(k, "-sO") == 0 ) {
[4052]1699   _CKHAVEARGS(1);
[2485]1700#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1701   s_opt = argv[++i];
[2485]1702#else
1703   ROAR_ERR("main(*): No support for sources compiled in");
[4052]1704   i++;
[2485]1705#endif
[0]1706  } else if ( strcmp(k, "-sC") == 0 ) {
[4052]1707   _CKHAVEARGS(1);
[2485]1708#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1709   s_con = argv[++i];
[2485]1710#else
1711   ROAR_ERR("main(*): No support for sources compiled in");
[4052]1712   i++;
[2485]1713#endif
[0]1714  } else if ( strcmp(k, "-sP") == 0 ) {
[2485]1715#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1716   s_prim = 1;
[2485]1717#else
1718   ROAR_ERR("main(*): No support for sources compiled in");
1719#endif
[1110]1720  } else if ( strcmp(k, "-sN") == 0 ) {
[2485]1721#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]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;
[2485]1728#else
1729   ROAR_ERR("main(*): No support for sources compiled in");
1730#endif
[2270]1731  } else if ( strcmp(k, "--list-sources") == 0 ) {
[2485]1732#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[2270]1733   print_sourcelist();
1734   return 0;
[2485]1735#else
1736   ROAR_ERR("main(*): No support for sources compiled in");
[2504]1737   return 1;
[2485]1738#endif
[0]1739
[1923]1740  } else if ( strcmp(k, "--light-channels") == 0 ) {
[4052]1741   _CKHAVEARGS(1);
[2494]1742#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]1743   light_channels = atoi(argv[++i]);
[2494]1744#else
1745   ROAR_WARN("main(*): no light subsystem compiled in");
[4052]1746   i++;
[2494]1747#endif
[1923]1748
[2725]1749  } else if ( strcmp(k, "--rds-pi") == 0 ) {
[4052]1750   _CKHAVEARGS(1);
[2725]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");
[4052]1755   i++;
[2725]1756#endif
[2728]1757  } else if ( strcmp(k, "--rds-ps") == 0 ) {
[4052]1758   _CKHAVEARGS(1);
[2728]1759#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1760   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
[2729]1761    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]);
[2728]1762    return 1;
1763   }
1764#else
1765   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
[4052]1766   i++;
[2728]1767#endif
1768  } else if ( strcmp(k, "--rds-pty") == 0 ) {
[4052]1769   _CKHAVEARGS(1);
[2728]1770#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1771   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
[2729]1772    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
[2728]1773    return 1;
1774   }
1775#else
1776   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
[4052]1777   i++;
[2728]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
[2725]1798
[1924]1799  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
[2487]1800#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]1801   midi_config.init_cb = 0;
[2487]1802#else
1803   // no warning here as this is the disable option
1804#endif
[2444]1805  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
[2487]1806#ifndef ROAR_WITHOUT_DCOMP_CB
[2444]1807   midi_config.init_cb = 1;
[2487]1808#else
1809   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1810#endif
[1924]1811  } else if ( strcmp(k, "--midi-console") == 0 ) {
[4052]1812   _CKHAVEARGS(1);
[2487]1813#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]1814   midi_config.console_dev = argv[++i];
[2444]1815   midi_config.init_cb = 1;
[2487]1816#else
1817   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
[4052]1818   i++;
[2487]1819#endif
[1924]1820
[2451]1821  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
[2489]1822#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1823   ssynth_conf.enable = 1;
[2489]1824#else
1825   ROAR_ERR("main(*): No support for ssynth compiled in");
1826#endif
[2451]1827  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
[2489]1828#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1829   ssynth_conf.enable = 0;
[2489]1830#else
1831   // we can safely ignore the disable
1832#endif
[2451]1833
[3374]1834  } else if ( strcmp(k, "--x11-display") == 0 || strcmp(k, "--display") == 0 ) {
[4052]1835   _CKHAVEARGS(1);
[3374]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
[0]1844  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
[4052]1845   _CKHAVEARGS(1);
[4012]1846   // This is only useful in INET not UNIX mode.
[1494]1847#ifdef ROAR_SUPPORT_LISTEN
[2530]1848   if ( *sock_addr == '/' )
1849    sock_addr = ROAR_DEFAULT_HOST;
[447]1850
[446]1851   errno = 0;
1852   if ( (port = atoi(argv[++i])) < 1 ) {
[1486]1853#ifdef ROAR_HAVE_GETSERVBYNAME
[446]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);
[1486]1862#else
1863    ROAR_ERR("invalite port number: %s", argv[i]);
1864    return 1;
1865#endif
[446]1866   }
[1494]1867#endif
[1115]1868  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
[4052]1869   _CKHAVEARGS(1);
[1494]1870#ifdef ROAR_SUPPORT_LISTEN
[2530]1871   sock_addr = argv[++i];
[4052]1872#else
1873   i++;
[2530]1874#endif
1875
1876  } else if ( strcmp(k, "--proto") == 0 ) {
1877#ifdef ROAR_SUPPORT_LISTEN
[2549]1878   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
[2530]1879    ROAR_ERR("Unknown protocol: %s", argv[i]);
1880    return 1;
1881   }
[1494]1882#endif
[3252]1883  } else if ( strcmp(k, "--proto-dir") == 0 ) {
[4052]1884   _CKHAVEARGS(1);
[3252]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   }
[4052]1890#else
1891   i++;
[3252]1892#endif
1893  } else if ( strcmp(k, "--proto-rate") == 0 ) {
[4052]1894   _CKHAVEARGS(1);
[3252]1895#ifdef ROAR_SUPPORT_LISTEN
1896   sock_info.rate = atoi(argv[++i]);
[4052]1897#else
1898   i++;
[3252]1899#endif
1900  } else if ( strcmp(k, "--proto-bits") == 0 ) {
[4052]1901   _CKHAVEARGS(1);
[3252]1902#ifdef ROAR_SUPPORT_LISTEN
1903   sock_info.bits = atoi(argv[++i]);
[4052]1904#else
1905   i++;
[3252]1906#endif
1907  } else if ( strcmp(k, "--proto-chans") == 0 ) {
[4052]1908   _CKHAVEARGS(1);
[3252]1909#ifdef ROAR_SUPPORT_LISTEN
1910   sock_info.channels = atoi(argv[++i]);
[4052]1911#else
1912   i++;
[3252]1913#endif
1914  } else if ( strcmp(k, "--proto-codec") == 0 ) {
[4052]1915   _CKHAVEARGS(1);
[3252]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   }
[4052]1921#else
1922   i++;
[3252]1923#endif
[3954]1924  } else if ( strcmp(k, "--proto-aiprofile") == 0 ) {
[4052]1925   _CKHAVEARGS(1);
[3954]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   }
[4052]1931#else
1932   i++;
[3954]1933#endif
[3956]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 ) {
[4052]1940   _CKHAVEARGS(1);
[3956]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   }
[4052]1946#else
1947   i++;
[3956]1948#endif
[3252]1949
1950
[3127]1951  } else if ( strcmp(k, "--list-proto") == 0 ) {
1952   list_proto();
1953   return 0;
[518]1954
[573]1955  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
[1494]1956#ifdef ROAR_SUPPORT_LISTEN
[518]1957   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
1958    sock_type = ROAR_SOCKET_TYPE_TCP;
1959
[2530]1960   if ( *sock_addr == '/' )
1961    sock_addr = ROAR_DEFAULT_HOST;
[1494]1962#endif
[518]1963
1964  } else if ( strcmp(k, "-4") == 0 ) {
[1494]1965#ifdef ROAR_SUPPORT_LISTEN
[517]1966   sock_type = ROAR_SOCKET_TYPE_TCP;
[2530]1967   if ( *sock_addr == '/' )
1968    sock_addr = ROAR_DEFAULT_HOST;
[1494]1969#endif
[518]1970  } else if ( strcmp(k, "-6") == 0 ) {
[1494]1971#ifdef ROAR_SUPPORT_LISTEN
[3837]1972#ifdef AF_INET6
[518]1973   sock_type = ROAR_SOCKET_TYPE_TCP6;
[2530]1974   if ( *sock_addr == '/' )
1975    sock_addr = ROAR_DEFAULT_HOST;
[519]1976#else
1977    ROAR_ERR("No IPv6 support compiled in!");
1978    return 1;
1979#endif
[1494]1980#endif
[518]1981
[573]1982  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
[1494]1983#ifdef ROAR_SUPPORT_LISTEN
[62]1984   // ignore this case as it is the default behavor.
[517]1985   sock_type = ROAR_SOCKET_TYPE_UNIX;
[1494]1986#endif
[518]1987
[573]1988  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
[1494]1989#ifdef ROAR_SUPPORT_LISTEN
[508]1990#ifdef ROAR_HAVE_LIBDNET
1991    port   = ROAR_DEFAULT_NUM;
1992    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
[2530]1993    sock_addr = decnethost;
[517]1994    sock_type = ROAR_SOCKET_TYPE_DECNET;
[508]1995#else
1996    ROAR_ERR("No DECnet support compiled in!");
1997    return 1;
1998#endif
[1494]1999#endif
[2530]2000  } else if ( strcmp(k, "--new-sock") == 0 ) {
2001#ifdef ROAR_SUPPORT_LISTEN
[3252]2002   if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
[2530]2003    ROAR_ERR("Can not open listen socket!");
2004    return 1;
2005   }
2006#endif
[518]2007
[1993]2008  } else if ( strcmp(k, "--slp") == 0 ) {
2009#ifdef ROAR_HAVE_LIBSLP
2010   reg_slp = 1;
2011#else
[3373]2012   ROAR_ERR("No OpenSLP support compiled in!");
2013   return 1;
[1993]2014#endif
2015
[3373]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
[3039]2025  } else if ( strcmp(k, "--jumbo-mtu") == 0 ) {
[4052]2026   _CKHAVEARGS(1);
[3039]2027   g_config->jumbo_mtu = atoi(argv[++i]);
2028
[60]2029  } else if ( strcmp(k, "-G") == 0 ) {
[4052]2030   _CKHAVEARGS(1);
[444]2031   sock_grp  = argv[++i];
2032  } else if ( strcmp(k, "-U") == 0 ) {
[4052]2033   _CKHAVEARGS(1);
[444]2034   sock_user = argv[++i];
[0]2035
[68]2036  } else if ( strcmp(k, "--no-listen") == 0 ) {
[1494]2037#ifdef ROAR_SUPPORT_LISTEN
[2530]2038   sock_addr   = "";
[548]2039   g_terminate = 1;
[1155]2040   g_no_listen = 1;
[1494]2041#endif
[68]2042  } else if ( strcmp(k, "--client-fh") == 0 ) {
[4052]2043   _CKHAVEARGS(1);
[3737]2044   if ( clients_new_from_fh(atoi(argv[++i]), ROAR_PROTO_ROARAUDIO, ROAR_BYTEORDER_NETWORK, 1) == -1 ) {
[68]2045    ROAR_ERR("main(*): Can not set client's fh");
2046    return 1;
2047   }
[501]2048  } else if ( strcmp(k, "--close-fh") == 0 ) {
[4052]2049   _CKHAVEARGS(1);
[1486]2050#ifdef ROAR_HAVE_IO_POSIX
[501]2051   close(atoi(argv[++i]));
[1486]2052#else
2053   i++;
2054   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
2055#endif
[68]2056
[920]2057  } else if ( strcmp(k, "--standby") == 0 ) {
2058   g_standby = 1;
2059  } else if ( strcmp(k, "--auto-standby") == 0 ) {
2060   g_autostandby = 1;
[0]2061  } else {
2062   usage();
2063   return 1;
2064  }
2065
2066 }
[1503]2067#endif
[0]2068
[2485]2069#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]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 }
[2485]2075#endif
[1110]2076
[4141]2077 add_default_output(o_drv, o_dev, o_opts, o_prim, o_count);
[932]2078
[4055]2079 ROAR_INFO("Server config: rate=%i, bits=%i, chans=%i", ROAR_DBG_INFO_NOTICE, sa.rate, sa.bits, sa.channels);
[0]2080
[2945]2081 if ( waveform_init() == -1 ) {
2082  ROAR_ERR("Can not initialize Waveform subsystem");
2083  return 1;
2084 }
2085
[2500]2086#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1819]2087 if ( midi_init() == -1 ) {
[281]2088  ROAR_ERR("Can not initialize MIDI subsystem");
[1819]2089 }
[2500]2090#endif
[1819]2091
[2489]2092#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]2093 if ( ssynth_init() == -1 ) {
2094  ROAR_ERR("Can not initialize ssynth subsystem");
2095 }
[2489]2096#endif
[2451]2097
[2494]2098#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]2099 if ( light_init(light_channels) == -1 ) {
2100  ROAR_ERR("Can not initialize light control subsystem");
2101 }
[2494]2102#endif
[281]2103
[2718]2104#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2105 if ( rdtcs_init() == -1 ) {
2106  ROAR_ERR("Can not initialize RDTCS subsystem");
2107 }
2108#endif
2109
[3354]2110 if ( plugins_init() == -1 ) {
2111  ROAR_ERR("Can not initialize plugins");
2112 }
2113
[1494]2114#ifdef ROAR_SUPPORT_LISTEN
[3760]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  }
[60]2120 }
[1494]2121#endif
[60]2122
[0]2123 if ( output_buffer_init(&sa) == -1 ) {
2124  ROAR_ERR("Can not init output buffer!");
2125  return 1;
2126 }
2127
[1145]2128 if ( driver == NULL ) {
2129  driver = "null";
2130 } else {
[4225]2131  ROAR_ERR("Usage of old driver interface. use -o not -d!");
2132  ROAR_WARN("-d will be removed within the next releases");
[1145]2133 }
2134
[0]2135 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
2136  ROAR_ERR("Can not open output driver!");
2137  return 1;
2138 }
2139
[44]2140 if ( samples_init() == -1 ) {
2141  ROAR_ERR("Can not init samples!");
2142  return 1;
2143 }
2144
2145
[1486]2146 // we should handle this on microcontrollers, too.
[1753]2147#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
[0]2148 signal(SIGINT,  on_sig_int);
[2732]2149 signal(SIGTERM, on_sig_term);
[285]2150 signal(SIGCHLD, on_sig_chld);
[2112]2151 signal(SIGUSR1, on_sig_usr1);
[0]2152 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
[1486]2153#endif
[0]2154
[4245]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
[275]2163 if ( realtime ) {
[278]2164#ifdef DEBUG
[4246]2165  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compile without -DDEBUG");
[278]2166#endif
2167
[1486]2168#ifdef ROAR_HAVE_NICE
[275]2169  errno = 0;
[276]2170  nice(-5*realtime); // -5 for each --realtime
[1486]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
[277]2177/*
[276]2178#ifdef __linux__
[277]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));
[276]2181#endif
[277]2182*/
[275]2183 }
2184
[2756]2185#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[444]2186 if ( setids & R_SETGID ) {
[2754]2187  if ( sock_grp == NULL ) {
2188   ROAR_ERR("Can not set GID if no groupname is supplied");
2189   return 1;
2190  }
[2584]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  }
[444]2195  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
2196   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
2197  }
[523]2198  if ( !grp || setgid(grp->gr_gid) == -1 ) {
[444]2199   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
2200  }
2201 }
[1486]2202#endif
[444]2203
[0]2204
[39]2205 clients_set_pid(g_self_client, getpid());
[1753]2206#ifdef ROAR_HAVE_GETUID
[440]2207 clients_set_uid(g_self_client, getuid());
[1753]2208#endif
2209#ifdef ROAR_HAVE_GETGID
[440]2210 clients_set_gid(g_self_client, getgid());
[1753]2211#endif
[39]2212 clients_get(g_self_client, &self);
[37]2213
[39]2214 if ( self == NULL ) {
2215  ROAR_ERR("Can not get self client!");
2216  return 1;
2217 }
2218
[775]2219 strcpy(self->name, "RoarAudio daemon internal");
[68]2220
[2815]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
[1486]2230#ifdef ROAR_HAVE_FORK
[775]2231 if ( daemon ) {
[3613]2232#ifdef ROAR_HAVE_SYSLOG
2233  roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
2234#else
[3609]2235  roar_debug_set_stderr_fh(-1);
[3613]2236#endif
[3609]2237
[68]2238  close(ROAR_STDIN );
2239  close(ROAR_STDOUT);
2240  close(ROAR_STDERR);
[1753]2241
[68]2242  if ( fork() )
[1486]2243   ROAR_U_EXIT(0);
[1753]2244
2245#ifdef ROAR_HAVE_SETSID
2246  setsid();
2247#endif
[1046]2248  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
[68]2249 }
[1486]2250#endif
[68]2251
[2757]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
[2104]2262#ifdef SUPPORT_PIDFILE
[2106]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  }
[2765]2270#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[2759]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  }
[2765]2279#endif
[2104]2280 }
2281#endif
2282
[1486]2283#ifdef ROAR_HAVE_CHROOT
[444]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 }
[1486]2294#endif
[444]2295
[2756]2296#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[444]2297 if ( setids & R_SETUID ) {
[2752]2298  if ( sock_user == NULL ) {
2299   ROAR_ERR("Can not set UID if no username is supplied");
2300   return 1;
2301  }
[444]2302  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
2303   ROAR_ERR("Can not set UserID: %s", strerror(errno));
2304   return 3;
2305  }
[1753]2306#ifdef ROAR_HAVE_GETUID
[444]2307  clients_set_uid(g_self_client, getuid());
[1753]2308#endif
[444]2309 }
[1486]2310#endif
[444]2311
[1993]2312 // Register with OpenSLP:
[2028]2313#ifdef ROAR_HAVE_LIBSLP
[1993]2314 if ( reg_slp ) {
[2530]2315  register_slp(0, sock_addr);
[1993]2316 }
[2028]2317#endif
[1993]2318
[3373]2319#ifdef ROAR_HAVE_LIBX11
2320 if ( reg_x11 ) {
2321  register_x11(0, sock_addr);
2322 }
2323#endif
2324
[4103]2325 ROAR_INFO("Startup complet", ROAR_DBG_INFO_INFO);
2326
[0]2327 // start main loop...
[4055]2328 ROAR_INFO("Entering main loop", ROAR_DBG_INFO_INFO);
[905]2329 main_loop(drvid, drvinst, &sa, sysclocksync);
[4055]2330 ROAR_INFO("Left main loop", ROAR_DBG_INFO_INFO);
[0]2331
2332 // clean up.
2333 clean_quit_prep();
2334 driver_close(drvinst, drvid);
2335 output_buffer_free();
2336
[4323]2337 roar_notify_core_free(NULL);
2338
[4055]2339 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
[0]2340 return 0;
2341}
2342
[574]2343void cleanup_listen_socket (int terminate) {
[2530]2344 int i;
2345
[4055]2346 ROAR_DBG("cleanup_listen_socket(terminate=%i) = (void)?", terminate);
2347
2348 ROAR_INFO("Cleaning up listen sockets", ROAR_DBG_INFO_INFO);
2349
[1993]2350 // Deregister from SLP:
[2028]2351#ifdef ROAR_HAVE_LIBSLP
[1993]2352 register_slp(1, NULL);
[2028]2353#endif
[580]2354
[3373]2355#ifdef ROAR_HAVE_LIBX11
2356 register_x11(1, NULL);
2357#endif
2358
[1494]2359#ifdef ROAR_SUPPORT_LISTEN
[2530]2360 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
[3802]2361  if ( g_listen[i].used  ) {
2362   roar_vio_close(&(g_listen[i].sock));
[60]2363
[3802]2364   g_listen[i].used = 0;
[576]2365
[1486]2366#ifdef ROAR_HAVE_UNIX
[2530]2367   if ( server[i] != NULL )
2368    if ( *(server[i]) == '/' )
2369     unlink(server[i]);
[1486]2370#endif
[2530]2371  }
[580]2372 }
[60]2373
[1494]2374#endif
2375
[574]2376 if ( terminate )
2377  g_terminate = 1;
2378}
2379
2380void clean_quit_prep (void) {
2381 cleanup_listen_socket(0);
[60]2382
[3354]2383 plugins_free();
2384
[2485]2385#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]2386 sources_free();
[2485]2387#endif
[0]2388 streams_free();
2389 clients_free();
[2489]2390#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]2391 ssynth_free();
[2489]2392#endif
[2487]2393#ifndef ROAR_WITHOUT_DCOMP_CB
[282]2394 midi_cb_stop(); // stop console beep
[2487]2395#endif
[2500]2396#ifndef ROAR_WITHOUT_DCOMP_MIDI
[281]2397 midi_free();
[2500]2398#endif
[2494]2399#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]2400 light_free();
[2494]2401#endif
[2718]2402#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2403 rdtcs_free();
2404#endif
[2104]2405
[2945]2406 waveform_free();
2407
[2104]2408#ifdef SUPPORT_PIDFILE
2409 if ( pidfile != NULL )
2410  unlink(pidfile);
2411#endif
[0]2412}
2413
2414void clean_quit (void) {
[4055]2415 ROAR_INFO("Shuting down", ROAR_DBG_INFO_INFO);
2416
[4101]2417 counters_print(ROAR_DEBUG_TYPE_INFO, 0);
2418
[0]2419 clean_quit_prep();
2420// driver_close(drvinst, drvid);
2421// output_buffer_free();
[4055]2422
[4323]2423 roar_notify_core_free(NULL);
2424
[4055]2425 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
[0]2426 exit(0);
2427}
2428
2429//ll
Note: See TracBrowser for help on using the repository browser.