source: roaraudio/roard/roard.c @ 4423:94456e48fb68

Last change on this file since 4423:94456e48fb68 was 4423:94456e48fb68, checked in by phi, 14 years ago

display PID at g_verbose >= 2

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