source: roaraudio/roard/roard.c @ 4805:f4f72e985020

Last change on this file since 4805:f4f72e985020 was 4792:a9cb74d9acb5, checked in by phi, 13 years ago

added support to roard to load authfiles

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