source: roaraudio/roard/roard.c @ 4703:be4d84be6f04

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

added basic gopher support

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