source: roaraudio/roard/roard.c @ 4760:50a0fc5a9f65

Last change on this file since 4760:50a0fc5a9f65 was 4760:50a0fc5a9f65, checked in by phi, 13 years ago

added some nice IPv6 profiles

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