source: roaraudio/roard/roard.c @ 4842:d135b43f4dab

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

emit ROAR_OE_STREAM_META_UPDATE events on meta data change

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