source: roaraudio/roard/roard.c @ 4850:fe9aec03046c

Last change on this file since 4850:fe9aec03046c was 4848:0b3d312d4253, checked in by phi, 13 years ago

updated add_driver() to check the args and fix segfauls if user gave incorrect parameters

File size: 83.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;
[4848]984 char * _opts;
[4140]985
986 if ( drv != NULL )
987  return add_output(drv, dev, opts, prim, count);
988
989 if ( dev != NULL ) {
[4141]990  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]991 }
992
993 for (i = 0; drvs[i] != NULL; i++) {
[4141]994  ROAR_INFO("add_default_output(*): trying driver %s", ROAR_DBG_INFO_INFO, drvs[i]);
[4144]995  _alive = alive; // save global alive setting
996
[4848]997  if ( opts == NULL ) {
998   _opts = NULL;
999  } else {
1000   _opts = roar_mm_strdup(opts);
1001  }
1002
1003  ret = add_output(drvs[i], dev, _opts, prim, count);
1004
1005  if ( _opts != NULL )
1006   roar_mm_free(_opts);
1007
[4140]1008  if ( ret != -1 )
1009   return ret;
[4144]1010
1011  alive = _alive; // restore global alive setting
[4141]1012  ROAR_INFO("add_default_output(*): Driver %s faild to load", ROAR_DBG_INFO_VERBOSE, drvs[i]);
[4140]1013 }
1014
1015 return -1;
1016}
1017#endif
1018
[4848]1019#define _CKPARAM(n) if ( n (v == NULL) ) { \
1020                     ROAR_WARN("add_output(drv='%s', dev='%s', opts=..., prim=%i, count=%i): " \
1021                               "Parameter '%s' expects %s parameter.", \
1022                               drv, dev, prim, count, k, (n 1) ? "a" : "no"); \
1023                     error++; \
1024                    } else
1025
[1145]1026int add_output (char * drv, char * dev, char * opts, int prim, int count) {
[933]1027 int stream;
[4816]1028 int default_flags;
[933]1029 struct roar_stream * s;
1030 struct roar_stream_server * ss;
[938]1031 char * k, * v;
[1208]1032#ifdef ROAR_DRIVER_CODEC
1033 char * to_free = NULL;
1034#endif
[1926]1035 int sync = 0, f_mmap = 0;
[1522]1036 int32_t blocks = -1, blocksize = -1;
[1920]1037 int dir = ROAR_DIR_OUTPUT;
[1991]1038 int error = 0;
1039 // DMX:
1040 int32_t channel  = -1;
1041 int32_t universe = -1;
1042 uint16_t tu16;
[2085]1043 float q = -32e6;
[933]1044
[4055]1045 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]1046
[1145]1047 if ( drv == NULL && count == 0 ) {
[4141]1048#ifdef ROAR_DRIVER_DEFAULT
[1145]1049  drv  = ROAR_DRIVER_DEFAULT;
1050  prim = 1;
1051  sync = 1;
[1208]1052
1053#ifdef ROAR_DRIVER_CODEC
1054  if ( opts == NULL ) {
1055   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
1056  }
1057#endif
[4141]1058#else
1059  ROAR_ERR("add_output(*): Can not find default driver");
1060  return -1;
1061#endif
[1145]1062 }
1063
[1227]1064 if ( opts == NULL && count == 0 ) {
[4816]1065  default_flags = ROAR_FLAG_AUTOCONF|ROAR_FLAG_RECSOURCE;
[1227]1066  sync = 1;
1067  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
1068 }
1069
[2785]1070 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
1071
[933]1072 if ( (stream = streams_new()) == -1 ) {
[938]1073  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]1074  if ( prim ) alive = 0;
[933]1075  return -1;
1076 }
1077
[2785]1078 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
1079
[933]1080 streams_get(stream, &ss);
1081 s = ROAR_STREAM(ss);
1082
[2785]1083 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
1084
[2511]1085 memset(&(s->info), 0xFF, sizeof(struct roar_audio_info)); // set everything to -1
[933]1086
1087 s->pos_rel_id = -1;
1088// s->info.codec = codec;
1089
[4816]1090 // seting default flags:
1091 streams_set_flag(stream, default_flags);
1092
[2785]1093 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
1094
[2788]1095 if ( opts == NULL ) {
1096  k = NULL;
1097 } else {
1098  k = strtok(opts, ",");
1099 }
[2786]1100
1101 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s'): initial k='%s'(%p)", drv, dev, opts, k, k);
1102
[938]1103 while (k != NULL) {
1104//  ROAR_WARN("add_output(*): opts: %s", k);
1105
1106  if ( (v = strstr(k, "=")) != NULL ) {
1107   *v++ = 0;
1108  }
1109
1110  ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v);
1111  if ( strcmp(k, "rate") == 0 ) {
[4848]1112   _CKPARAM()
1113    s->info.rate = atoi(v);
[938]1114  } else if ( strcmp(k, "channels") == 0 ) {
[4848]1115   _CKPARAM()
1116    s->info.channels = atoi(v);
[938]1117  } else if ( strcmp(k, "bits") == 0 ) {
[4848]1118   _CKPARAM()
1119    s->info.bits = atoi(v);
[938]1120  } else if ( strcmp(k, "codec") == 0 ) {
[4848]1121   _CKPARAM()
1122    if ( (s->info.codec = roar_str2codec(v)) == -1 ) {
1123     ROAR_ERR("add_output(*): unknown codec '%s'", v);
1124     error++;
1125    }
[2085]1126  } else if ( strcmp(k, "q") == 0 ) {
[4848]1127   _CKPARAM()
1128    q = atof(v);
[1221]1129  } else if ( strcmp(k, "blocks") == 0 ) {
[4848]1130   _CKPARAM()
1131    blocks = atoi(v);
[1522]1132  } else if ( strcmp(k, "blocksize") == 0 ) {
[4848]1133   _CKPARAM()
1134    blocksize = atoi(v);
[1926]1135  } else if ( strcmp(k, "mmap") == 0 ) {
[4848]1136   _CKPARAM(!)
1137    f_mmap = 1;
[1919]1138  } else if ( strcmp(k, "subsystem") == 0 ) {
[4848]1139   _CKPARAM() {
1140    if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) {
1141     dir = ROAR_DIR_OUTPUT;
[2494]1142#ifndef ROAR_WITHOUT_DCOMP_MIDI
[4848]1143    } else if ( !strcasecmp(v, "midi") ) {
1144     dir = ROAR_DIR_MIDI_OUT;
[2494]1145#endif
1146#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[4848]1147    } else if ( !strcasecmp(v, "light") ) {
1148     dir = ROAR_DIR_LIGHT_OUT;
[2494]1149#endif
1150#ifndef ROAR_WITHOUT_DCOMP_RAW
[4848]1151    } else if ( !strcasecmp(v, "raw") ) {
1152     dir = ROAR_DIR_RAW_OUT;
[2494]1153#endif
[4848]1154    } else if ( !strcasecmp(v, "complex") ) {
1155     dir = ROAR_DIR_COMPLEX_OUT;
1156    } else {
1157     ROAR_ERR("add_output(*): unknown/unsupported subsystem '%s'", k);
1158     error++;
1159    }
[1991]1160   }
1161  // DMX:
1162  } else if ( strcmp(k, "channel") == 0 ) {
[4848]1163   _CKPARAM() {
1164    channel  = atoi(v);
1165    if ( channel < 0 || channel > 65535 ) {
1166     ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel);
1167     channel = -1;
1168     error++;
1169    }
[1919]1170   }
[1991]1171  } else if ( strcmp(k, "universe") == 0 ) {
[4848]1172   _CKPARAM() {
1173    universe = atoi(v);
1174    if ( universe < 0 || universe > 255 ) {
1175     ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe);
1176     universe = -1;
1177     error++;
1178    }
[1991]1179   }
1180
[2115]1181  } else if ( strcmp(k, "name") == 0 ) {
[4848]1182   _CKPARAM() {
1183    if ( streams_set_name(stream, v) == -1 ) {
1184     ROAR_ERR("add_output(*): Can not set Stream name");
1185     error++;
1186    }
[2115]1187   }
[1991]1188
[1032]1189  } else if ( strcmp(k, "meta") == 0 ) {
[4848]1190   _CKPARAM(!)
1191    streams_set_flag(stream, ROAR_FLAG_META);
[1117]1192  } else if ( strcmp(k, "sync") == 0 ) {
[4848]1193   _CKPARAM(!)
1194    sync = 1;
[1221]1195  } else if ( strcmp(k, "primary") == 0 ) {
[4848]1196   _CKPARAM(!)
1197    prim = 1;
[1221]1198
1199  } else if ( strcmp(k, "cleanmeta") == 0 ) {
[4848]1200   _CKPARAM(!)
1201    streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
[1221]1202  } else if ( strcmp(k, "autoconf") == 0 ) {
[4848]1203   _CKPARAM(!)
1204    streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
[2413]1205  } else if ( strcmp(k, "recsource") == 0 ) {
[4848]1206   _CKPARAM(!)
1207    streams_set_flag(stream, ROAR_FLAG_RECSOURCE);
[2413]1208  } else if ( strcmp(k, "passmixer") == 0 ) {
[4848]1209   _CKPARAM(!)
1210    streams_set_flag(stream, ROAR_FLAG_PASSMIXER);
[4815]1211  } else if ( strcmp(k, "recsource") == 0 ) {
[4848]1212   _CKPARAM(!)
1213    streams_set_flag(stream, ROAR_FLAG_RECSOURCE);
[938]1214  } else {
1215   ROAR_ERR("add_output(*): unknown option '%s'", k);
[1991]1216   error++;
1217  }
1218
1219  if ( error ) {
[938]1220   streams_delete(stream);
[982]1221   if ( prim ) alive = 0;
[1208]1222#ifdef ROAR_DRIVER_CODEC
1223   if ( to_free != NULL )
1224    free(to_free);
1225#endif
[938]1226   return -1;
1227  }
1228
1229  k = strtok(NULL, ",");
1230 }
1231
[2785]1232 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
1233
[2511]1234 // set audio info...
1235 switch (dir) {
1236  case ROAR_DIR_LIGHT_OUT:
1237    switch (s->info.codec) {
1238     case ROAR_CODEC_DMX512:
1239     case -1:
1240       if ( s->info.rate == -1 )
1241        s->info.rate = ROAR_OUTPUT_CFREQ;
1242
1243       s->info.channels =   0;
1244       s->info.bits     =   8;
1245       s->info.codec    = ROAR_CODEC_DMX512; // in case codec == -1
1246      break;
1247    }
1248   break;
1249  case ROAR_DIR_MIDI_OUT:
1250    switch (s->info.codec) {
1251     case ROAR_CODEC_MIDI:
1252     case -1:
1253       if ( s->info.rate == -1 )
1254        s->info.rate    = ROAR_MIDI_TICKS_PER_BEAT;
1255
1256       s->info.channels = ROAR_MIDI_CHANNELS_DEFAULT;
1257       s->info.bits     = ROAR_MIDI_BITS;
1258       s->info.codec    = ROAR_CODEC_MIDI; // in case codec == -1
1259      break;
1260    }
1261   break;
1262  case ROAR_DIR_RAW_OUT:
1263    if ( s->info.rate == -1 )
1264     s->info.rate = 0;
1265    if ( s->info.bits == -1 )
1266     s->info.bits = 0;
1267    if ( s->info.channels == -1 )
1268     s->info.channels = 0;
1269    if ( s->info.codec == -1 )
1270     s->info.codec = 0;
1271   break;
1272 }
1273
1274 if ( s->info.rate == -1 )
1275  s->info.rate = g_sa->rate;
1276 if ( s->info.bits == -1 )
1277  s->info.bits = g_sa->bits;
1278 if ( s->info.channels == -1 )
1279  s->info.channels = g_sa->channels;
1280 if ( s->info.codec == -1 )
1281  s->info.codec = g_sa->codec;
1282
1283 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);
1284
[1919]1285 if ( streams_set_dir(stream, dir, 1) == -1 ) {
1286  streams_delete(stream);
1287  return -1;
1288 }
1289
[1208]1290#ifdef ROAR_DRIVER_CODEC
1291 if ( to_free != NULL )
1292  free(to_free);
1293#endif
1294
[2511]1295 if ( s->info.codec == ROAR_CODEC_ALAW || s->info.codec == ROAR_CODEC_MULAW )
[941]1296  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
1297
[2511]1298 ROAR_STREAM_SERVER(s)->codec_orgi = s->info.codec;
[933]1299
[2364]1300 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) {
[1581]1301  ss->driver_id = -1; // don't close a driver not opened...
1302  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
[933]1303  streams_delete(stream);
[4741]1304  if ( prim ) alive = 0;
1305  ROAR_ERR("add_output(drv='%s', dev='%s', opts='%s'): can not open output driver.", drv, dev, opts);
[938]1306  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[933]1307  return -1;
1308 }
1309
[1528]1310 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here
1311 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM,   s); // ignore errors here
1312
[1221]1313 if ( blocks != -1 )
1314  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
1315
[1522]1316 if ( blocksize != -1 )
1317  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
1318
[1991]1319 // TODO: we shoudld *really* check for errors here...
1320 if ( channel != -1 ) {
1321  tu16 = channel;
1322  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16);
1323 }
1324 if ( universe != -1 ) {
1325  tu16 = universe;
1326  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16);
1327 }
1328
[1156]1329 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
1330
[938]1331 streams_set_fh(stream, -1); // update some internal structures
1332
[2087]1333 if ( q > -1e6 ) {
1334  ROAR_DBG("add_output(*): setting q=%f", q);
1335  streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q);
1336 }
1337
[2490]1338 client_stream_add(g_self_client, stream);
[933]1339
[1200]1340 if ( prim ) {
[961]1341  streams_mark_primary(stream);
[1200]1342  s->pos_rel_id = stream;
1343 }
[961]1344
[1120]1345 if ( sync ) {
[1117]1346  streams_set_flag(stream, ROAR_FLAG_SYNC);
[1120]1347 } else {
1348  streams_reset_flag(stream, ROAR_FLAG_SYNC);
1349 }
[1117]1350
[1926]1351 if ( f_mmap )
1352  streams_set_flag(stream, ROAR_FLAG_MMAP);
1353
[2511]1354 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]1355 return 0;
[932]1356}
1357
[4358]1358#ifndef ROAR_WITHOUT_DCOMP_MIXER
1359int add_hwmixer (char * drv, char * dev, char * opts, int prim, int count) {
[4381]1360 char * basename = NULL;
1361 char * subnames = NULL;
1362 char * k, * v;
[4360]1363 int basestream = streams_new();
1364 int ret;
[4381]1365 int error = 0;
[4360]1366//int hwmixer_open(int basestream, char * drv, char * dev, int fh, char * basename, char * subnames) {
1367
[4369]1368 if ( basestream == -1 )
1369  return -1;
1370
[4382]1371 client_stream_add(g_self_client, basestream);
[4369]1372
[4381]1373 if ( opts == NULL ) {
1374  k = NULL;
1375 } else {
1376  k = strtok(opts, ",");
1377 }
1378
1379 while (k != NULL) {
1380//  ROAR_WARN("add_output(*): opts: %s", k);
1381
1382  if ( (v = strstr(k, "=")) != NULL ) {
1383   *v++ = 0;
1384  }
1385
1386  if ( strcmp(k, "primary") == 0 ) {
1387   prim = 1;
1388
1389  } else if ( strcmp(k, "name") == 0 ) {
1390   basename = v;
1391  } else if ( strcmp(k, "subs") == 0 ) {
1392   subnames = v;
1393
1394  } else if ( strcmp(k, "autoconf") == 0 ) {
1395   streams_set_flag(basestream, ROAR_FLAG_AUTOCONF);
1396  } else if ( strcmp(k, "passmixer") == 0 ) {
1397   streams_set_flag(basestream, ROAR_FLAG_PASSMIXER);
1398  } else {
1399   ROAR_ERR("add_hwmixer(*): unknown option '%s'", k);
1400   error++;
1401  }
1402
1403  if ( error ) {
1404   streams_delete(basestream);
1405   if ( prim ) alive = 0;
1406   return -1;
1407  }
1408
1409  k = strtok(NULL, ",");
1410 }
1411
[4360]1412 if ( prim ) {
1413  streams_mark_primary(basestream);
1414 }
1415
[4381]1416 ret = hwmixer_open(basestream, drv, dev, -1, basename, subnames);
[4360]1417
1418 if ( ret == -1 ) {
1419  streams_delete(basestream);
1420 }
1421
1422 return ret == -1 ? -1 : 0;
[4358]1423}
1424#endif
1425
[4788]1426int add_authfile (const char * file, const char * type, enum af_mode mode, enum roard_client_acclev acclev) {
1427 struct roar_authfile * authfile = NULL;
1428 struct roar_authfile_key *  key = NULL;
1429 int af_type = ROAR_AUTHFILE_TYPE_AUTO;
[4790]1430 void * keydata;
[4792]1431 int i;
[4788]1432
1433 if ( type == NULL ) {
1434  // noop.
1435 } else if ( !strcasecmp(type, "roar") ) {
1436  af_type = ROAR_AUTHFILE_TYPE_ROAR;
1437 } else if ( !strcasecmp(type, "roar") ) {
1438  af_type = ROAR_AUTHFILE_TYPE_ROAR;
1439 } else if ( !strcasecmp(type, "esd") ) {
1440  af_type = ROAR_AUTHFILE_TYPE_ESD;
1441 } else if ( !strcasecmp(type, "pulse") ) {
1442  af_type = ROAR_AUTHFILE_TYPE_PULSE;
1443 } else if ( !strcasecmp(type, "htpasswd") ) {
1444  af_type = ROAR_AUTHFILE_TYPE_HTPASSWD;
1445 } else if ( !strcasecmp(type, "xauth") ) {
1446  af_type = ROAR_AUTHFILE_TYPE_XAUTH;
1447 } else {
1448  ROAR_ERR("add_authfile(*): unknown authfile type '%s'.", type);
1449  return -1;
1450 }
1451
1452 if ( mode == AF_MODE_GEN && af_type == ROAR_AUTHFILE_TYPE_AUTO )
1453  af_type = ROAR_AUTHFILE_TYPE_ESD;
1454
1455 switch (mode) {
1456  case AF_MODE_NONE:
1457    return 0;
1458   break;
1459  case AF_MODE_GEN:
1460    switch (af_type) {
1461     case ROAR_AUTHFILE_TYPE_ESD:
1462       key = roar_authfile_key_new_random(ROAR_AUTH_T_COOKIE, 16, NULL);
1463      break;
1464     case ROAR_AUTHFILE_TYPE_PULSE:
1465       key = roar_authfile_key_new_random(ROAR_AUTH_T_COOKIE, 256, NULL);
1466      break;
1467     default:
1468       return -1;
1469      break;
1470    }
1471
1472    if ( key == NULL ) {
1473     ROAR_ERR("add_authfile(*): Can not generate key.");
1474     return -1;
1475    }
1476
1477    if ( (authfile = roar_authfile_open(af_type, file, 1, ROAR_AUTHFILE_VERSION_AUTO)) == NULL ) {
1478     roar_authfile_key_unref(key);
1479     return -1;
1480    }
1481
1482    if ( roar_authfile_add_key(authfile, key) == -1 ) {
1483     ROAR_WARN("add_authfile(*): Can not add key to authfile.");
1484    }
1485
[4790]1486    keydata = roar_mm_memdup(key->data, key->len);
1487
1488    if ( keydata == NULL ) {
1489     ROAR_WARN("add_authfile(*): Can not allocate memory for key.");
1490    } else if ( auth_addkey_cookie(acclev, keydata, key->len) == -1 ) {
[4788]1491     ROAR_WARN("add_authfile(*): Can not add key to internal key storage.");
1492    }
1493
1494    roar_authfile_key_unref(key);
1495    if ( roar_authfile_close(authfile) != 0 )
1496     return -1;
1497    return 0;
1498   break;
[4792]1499  case AF_MODE_LOAD:
1500    if ( (authfile = roar_authfile_open(af_type, file, 0, ROAR_AUTHFILE_VERSION_AUTO)) == NULL ) {
1501     return -1;
1502    }
1503
1504    for (i = 0; ; i++) {
1505     key = roar_authfile_lookup_key(authfile, ROAR_AUTH_T_AUTO, i, NULL);
1506     if ( key == NULL )
1507      break;
1508
1509     if ( key->type == ROAR_AUTH_T_COOKIE ) {
1510      keydata = roar_mm_memdup(key->data, key->len);
1511
1512      if ( keydata == NULL ) {
1513       ROAR_WARN("add_authfile(*): Can not allocate memory for key.");
1514      } else if ( auth_addkey_cookie(acclev, keydata, key->len) == -1 ) {
1515       ROAR_WARN("add_authfile(*): Can not add key to internal key storage.");
1516      }
1517     } else {
1518      ROAR_WARN("add_authfile(*): Unknown key type: %i", key->type);
1519     }
1520
1521     roar_authfile_key_unref(key);
1522    }
1523
1524    if ( roar_authfile_close(authfile) != 0 )
1525     return -1;
1526    return 0;
1527   break;
[4788]1528 }
1529
1530 return -1;
1531}
1532
[3373]1533// X11:
1534#ifdef ROAR_HAVE_LIBX11
1535int register_x11 (int unreg, char * sockname) {
1536 struct roar_x11_connection * x11con = NULL;
1537 int ret = 0;
1538
[4103]1539 if ( (x11con = roar_x11_connect(x11display)) == NULL ) {
[4155]1540  ROAR_ERR("Can not connect to X11 server for %sregistering", unreg ? "un" : "");
[3373]1541  return -1;
[4103]1542 }
[3373]1543
1544 if ( unreg ) {
[4103]1545  if ( roar_x11_delete_prop(x11con, "ROAR_SERVER") == -1 ) {
[3373]1546   ret = -1;
[4103]1547   ROAR_ERR("Error while unregistereing from X11", ROAR_DBG_INFO_INFO);
1548  } else {
1549   ROAR_INFO("Successfully unregistered from X11", ROAR_DBG_INFO_INFO);
1550  }
[3373]1551 } else {
[4103]1552  if ( roar_x11_set_prop(x11con, "ROAR_SERVER", sockname) == -1 ) {
[3373]1553   ret = -1;
[4103]1554   ROAR_ERR("Error while registereing to X11", ROAR_DBG_INFO_INFO);
1555  } else {
1556   ROAR_INFO("Successfully registered to X11", ROAR_DBG_INFO_INFO);
1557  }
[3373]1558 }
1559
1560 roar_x11_disconnect(x11con);
1561
1562 return ret;
1563}
1564#endif
[1993]1565
1566// SLP:
1567void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
1568 /* return the error code in the cookie */
1569 *(SLPError*)cookie = errcode;
1570}
1571
1572int register_slp (int unreg, char * sockname) {
1573#ifdef ROAR_HAVE_LIBSLP
1574 static int regged = 0;
1575 static char * sn = NULL;
1576 SLPError err;
1577 SLPError callbackerr;
1578 SLPHandle hslp;
1579 char addr[1024];
1580 char attr[1024] = "";
[2017]1581 char * location;
[4421]1582 char * description;
[4577]1583 char * standards;
[1993]1584
1585 if ( sockname != NULL )
1586  sn = sockname;
1587
[3076]1588 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE_ROAR "://%s", sn);
[1993]1589
1590 err = SLPOpen("en", SLP_FALSE, &hslp);
1591
1592 if (err != SLP_OK) {
1593  ROAR_ERR("Error opening slp handle: Error #%i", err);
1594  return -1;
1595 }
1596
1597 if (!unreg) {
[2017]1598
1599  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
1600   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
1601   SLPClose(hslp);
1602   return -1;
1603  }
1604
[4421]1605  if ( SLPEscape(g_config->description, &description, SLP_FALSE) != SLP_OK ) {
1606   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
1607   SLPClose(hslp);
1608   return -1;
1609  }
1610
[4577]1611  standards = stds_string();
1612
1613  if ( standards == NULL ) {
1614   if ( (standards = roar_mm_malloc(1)) == NULL ) {
1615    SLPClose(hslp);
1616    return -1;
1617   }
1618   standards[0] = 0;
1619  }
1620
[1995]1621  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
[2494]1622#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1623                               "(light-channels=%i),"
1624#endif
[4577]1625                               "(standards=%s),"
[4421]1626                               "(location=%s),(description=%s)",
[1995]1627           g_sa->rate, g_sa->channels, g_sa->bits,
[2494]1628#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[2017]1629           g_light_state.channels,
[2494]1630#endif
[4577]1631           standards,
[4421]1632           location, description
[1995]1633          );
1634
[4577]1635  roar_mm_free(standards);
1636
[1993]1637  /* Register a service with SLP */
1638  err = SLPReg(hslp,
1639               addr,
1640               SLP_LIFETIME_MAXIMUM,
1641               0,
1642               attr,
1643               SLP_TRUE,
1644               register_slp_callback,
1645               &callbackerr);
[1994]1646  regged = 1;
1647 } else if ( unreg && regged ) {
1648  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
1649  regged = 0;
1650 } else {
1651  SLPClose(hslp);
1652  return -1;
1653 }
[1993]1654
[1994]1655 /* err may contain an error code that occurred as the slp library    */
1656 /* _prepared_ to make the call.                                     */
1657 if ( err != SLP_OK ) {
1658  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
1659  return -1;
1660 }
1661
1662 /* callbackerr may contain an error code (that was assigned through */
1663 /* the callback cookie) that occurred as slp packets were sent on    */
1664 /* the wire */
1665 if (callbackerr != SLP_OK) {
1666  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
1667  return -1;
[1993]1668 }
1669
1670 SLPClose(hslp);
1671 return 0;
1672#else
1673 return -1;
1674#endif
1675}
1676
[4481]1677static int auth_setup (enum roard_client_acclev none_acclev,
1678                       enum roard_client_acclev trust_acclev, int trust_root, uid_t trust_uid, gid_t trust_gid) {
1679 union auth_typeunion * key;
1680 size_t i;
1681
[4482]1682 // Info:
1683 // if a authlevel is <= ACCLEV_IDENTED we do not need to add this to the keyring
1684 // as ACCLEV_IDENTED is already gained if IDENTIFY call was done correctly.
[4481]1685
[4482]1686 if ( !(none_acclev <= ACCLEV_IDENTED) ) {
1687  if ( auth_addkey_anonymous(none_acclev) == -1 )
1688   return -1;
1689 }
[4481]1690
[4482]1691 if ( !(trust_acclev <= ACCLEV_IDENTED) ) {
1692  if ( (key = auth_regkey_simple(ROAR_AUTH_T_TRUST, trust_acclev)) == NULL )
1693   return -1;
[4481]1694
[4482]1695  // zerosize all counters.
1696  memset(key, 0, sizeof(union auth_typeunion));
[4481]1697
[4482]1698  i = 0;
1699
1700  if ( trust_uid != -1 )
1701   key->trust.uids[i++] = trust_uid;
[4481]1702
1703#ifdef ROAR_ROOT_UID
[4482]1704  if ( trust_root )
1705   key->trust.uids[i++] = ROAR_ROOT_UID;
[4481]1706#endif
1707
[4482]1708  key->trust.uids_len = i;
[4481]1709
[4482]1710  i = 0;
[4481]1711
[4482]1712  if ( trust_gid != -1 )
1713   key->trust.gids[i++] = trust_gid;
[4481]1714
[4482]1715  key->trust.gids_len = i;
1716 }
[4481]1717
1718 return 0;
1719}
1720
[1993]1721
1722// MAIN:
1723
[4053]1724#define _CKHAVEARGS(x) if ( (i + (x)) >= argc ) { \
1725                        ROAR_ERR("Option %s requires more arguments. See --help for more details.", k); \
1726                        return 70; \
1727                       }
[4052]1728
[1503]1729#ifdef ROAR_HAVE_MAIN_ARGS
[0]1730int main (int argc, char * argv[]) {
[1503]1731#else
1732int main (void) {
1733#endif
1734#ifdef ROAR_HAVE_MAIN_ARGS
[4722]1735 enum action action = START;
[0]1736 int i;
1737 char * k;
[1503]1738#endif
[2787]1739#if defined(ROAR_SUPPORT_LISTEN) && defined(ROAR_HAVE_GETUID)
[905]1740 char user_sock[80]  = {0};
[1494]1741#endif
[1609]1742 struct roar_audio_info sa, max_sa;
1743 struct roard_config config;
[1486]1744#ifdef ROAR_HAVE_FORK
[905]1745 int    daemon       = 0;
[1486]1746#endif
[905]1747 int    realtime     = 0;
1748 int    sysclocksync = 0;
[60]1749// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
[1494]1750#ifdef ROAR_SUPPORT_LISTEN
[2530]1751 int    port       = ROAR_DEFAULT_PORT;
1752 char * sock_addr  = NULL;
1753 int    sock_proto = ROAR_PROTO_ROARAUDIO;
[3252]1754 int    sock_dir   = -1;
1755 struct roar_audio_info sock_info = {0, 0, 0, 0};
[1494]1756#endif
[2486]1757#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]1758 char * s_drv     = "cf";
[1110]1759 char * s_dev     = NULL;
[444]1760 char * s_con     = NULL;
1761 char * s_opt     = NULL;
1762 int    s_prim    = 0;
[2486]1763#endif
[1207]1764 char * o_drv     = getenv("ROAR_DRIVER");
1765 char * o_dev     = getenv("ROAR_DEVICE");
[932]1766 char * o_opts    = NULL;
[961]1767 int    o_prim    = 0;
[1145]1768 int    o_count   = 0;
[4358]1769#ifndef ROAR_WITHOUT_DCOMP_MIXER
1770 char * m_drv     = NULL;
1771 char * m_dev     = NULL;
1772 char * m_opts    = NULL;
1773 int    m_prim    = 0;
1774 int    m_count   = 0;
1775#endif
[4481]1776 enum roard_client_acclev none_acclev  = ACCLEV_ALL;
1777 enum roard_client_acclev trust_acclev = ACCLEV_ALL;
1778 int                      trust_root   = 1;
[4788]1779 char * af_file                        = NULL;
1780 char * af_type                        = NULL;
1781 enum af_mode af_mode                  = AF_MODE_NONE;
1782 enum roard_client_acclev af_acclev    = ACCLEV_ALL;
[2494]1783#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]1784 int    light_channels = LIGHT_CHANNELS_DEFAULT;
[2494]1785#endif
[2751]1786#ifdef ROAR_DEFAULT_SOCKGRP
[450]1787 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
[2751]1788#else
1789 char * sock_grp  = NULL;
1790#endif
[444]1791 char * sock_user = NULL;
[1494]1792#ifdef ROAR_SUPPORT_LISTEN
[517]1793 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
[1494]1794#endif
[1993]1795#ifdef ROAR_HAVE_LIBSLP
1796 int    reg_slp   = 0;
1797#endif
[3373]1798#ifdef ROAR_HAVE_LIBX11
1799 int    reg_x11   = 0;
1800#endif
[1486]1801#ifdef ROAR_HAVE_CHROOT
[444]1802 char * chrootdir = NULL;
[1486]1803#endif
1804#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[446]1805 struct group   * grp  = NULL;
[1486]1806#endif
1807#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[446]1808 struct passwd  * pwd  = NULL;
[1486]1809#endif
1810#ifdef ROAR_HAVE_GETSERVBYNAME
[446]1811 struct servent * serv = NULL;
[1486]1812#endif
[39]1813 struct roar_client * self = NULL;
[508]1814#ifdef ROAR_HAVE_LIBDNET
1815 char decnethost[80];
1816#endif
[2104]1817#ifdef SUPPORT_PIDFILE
1818 struct roar_vio_calls pidfile_vio;
1819#endif
[0]1820
[2784]1821 ROAR_DBG("main(*): starting roard...");
1822
[0]1823 g_standby       =  0;
[920]1824 g_autostandby   =  0;
[982]1825 alive           =  1;
[1494]1826#ifdef ROAR_SUPPORT_LISTEN
[1155]1827 g_no_listen     =  0;
[1494]1828#else
1829 g_terminate     =  1;
1830#endif
[0]1831
[2970]1832 g_verbose       = ROAR_DBG_INFO_NONE;
1833
[0]1834 sa.bits     = ROAR_BITS_DEFAULT;
1835 sa.channels = ROAR_CHANNELS_DEFAULT;
1836 sa.rate     = ROAR_RATE_DEFAULT;
1837 sa.codec    = ROAR_CODEC_DEFAULT;
1838
[1609]1839 g_sa        = &sa;
1840 g_max_sa    = &max_sa;
1841
1842 memcpy(g_max_sa, g_sa, sizeof(max_sa));
1843
[4101]1844 counters_init();
1845
[1609]1846 g_config = &config;
1847
1848 if ( init_config() == -1 ) {
1849  ROAR_ERR("Can not init default config!");
1850  return 1;
1851 }
[0]1852
[2968]1853 // load config
1854 roar_libroar_get_config();
1855
[4323]1856 // init notify core:
1857 // TODO: reconsider number of lists.
1858 if ( roar_notify_core_new_global(-1) == -1 ) {
1859  ROAR_ERR("Can not init notify core!");
1860  return 1;
1861 }
1862
[4324]1863 if ( roar_notify_core_register_proxy(NULL, roar_notify_proxy_std, NULL) == -1 ) {
1864  ROAR_ERR("Can not init notify core!");
1865  return 1;
1866 }
1867
1868#ifdef DEBUG
[4418]1869 // enable early in case we have DEBUG set.
[4324]1870 dbg_notify_cb_register();
1871#endif
1872
[4469]1873 if ( auth_init() == -1 ) {
1874  ROAR_ERR("Can not init auth subsystem!");
1875  return 1;
1876 }
1877
[2530]1878#ifdef ROAR_SUPPORT_LISTEN
1879 if ( init_listening() == -1 ) {
1880  ROAR_ERR("Can not init listening sockets!");
1881  return 1;
1882 }
1883#endif
1884
[2500]1885#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1924]1886 if ( midi_init_config() == -1 ) {
1887  ROAR_ERR("Can not init MIDI config!");
1888  return 1;
1889 }
[2500]1890#endif
[60]1891
[2489]1892#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1893 if ( ssynth_init_config() == -1 ) {
1894  ROAR_ERR("Can not init ssynth config!");
1895  return 1;
1896 }
[2489]1897#endif
[2451]1898
[2718]1899#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1900 if ( rdtcs_init_config() == -1 ) {
1901  ROAR_ERR("Can not init RDTCS config!");
1902  return 1;
1903 }
1904#endif
1905
[3354]1906 if ( plugins_preinit() == -1 ) {
1907  ROAR_ERR("Can not pre-init plugins!");
1908  return 1;
1909 }
1910
[1494]1911#ifdef ROAR_SUPPORT_LISTEN
[2767]1912#ifndef ROAR_TARGET_WIN32
[2530]1913 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
[2767]1914#else
1915 sock_addr = ROAR_DEFAULT_HOST;
1916#endif
1917
[1753]1918#ifdef ROAR_HAVE_GETUID
[1486]1919 if ( getuid() != 0 && getenv("HOME") != NULL ) {
[3959]1920/*
[1486]1921  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
[3959]1922*/
1923  roar_env_render_path_r(user_sock, sizeof(user_sock), "~/" ROAR_DEFAULT_SOCK_USER);
[2530]1924  sock_addr = user_sock;
[3959]1925  ROAR_DBG("main(*): setting sock_addr='%s'", sock_addr);
[60]1926 }
[1753]1927#endif
[60]1928
[279]1929 if ( getenv("ROAR_SERVER") != NULL )
[2530]1930  sock_addr = getenv("ROAR_SERVER");
[1494]1931#endif
[279]1932
[63]1933 if ( clients_init() == -1 ) {
1934  ROAR_ERR("Can not init clients!");
1935  return 1;
1936 }
1937
1938 if ( streams_init() == -1 ) {
1939  ROAR_ERR("Can not init streams!");
1940  return 1;
1941 }
1942
[64]1943 if ( (g_self_client = clients_new()) == -1 ) {
1944  ROAR_ERR("Can not create self client!");
1945  return 1;
1946 }
1947
[2485]1948#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1949 if ( sources_init() == -1 ) {
1950  ROAR_ERR("Can not init sources!");
1951  return 1;
1952 }
1953
[64]1954 if ( (sources_set_client(g_self_client)) == -1 ) {
1955  ROAR_ERR("Can not init set source client!");
1956  return 1;
1957 }
[2485]1958#endif
[64]1959
[1503]1960#ifdef ROAR_HAVE_MAIN_ARGS
[0]1961 for (i = 1; i < argc; i++) {
1962  k = argv[i];
1963
1964  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
1965   usage();
1966   return 0;
1967
[2109]1968  } else if ( strcmp(k, "--start") == 0 ) {
1969   // this is a no op
[4722]1970   action = START;
[579]1971  } else if ( strcmp(k, "--restart") == 0 ) {
[4722]1972   action = RESTART;
[1494]1973#ifdef ROAR_SUPPORT_LISTEN
[2530]1974   if ( restart_server(sock_addr, 1) == -1 ) {
[4722]1975    ROAR_WARN("Can not terminate old server (not running at %s?), will retry later.", sock_addr);
1976    action = RESTART_RETRY;
[579]1977   }
[1494]1978#else
1979   ROAR_ERR("--restart not supported");
1980#endif
[2109]1981  } else if ( strcmp(k, "--shutdown") == 0 ) {
[4722]1982   action = SHUTDOWN;
[2109]1983  } else if ( strcmp(k, "--stop") == 0 ) {
[4722]1984   action = STOP;
[2109]1985
[579]1986
[775]1987  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
[1486]1988#ifdef ROAR_HAVE_FORK
[775]1989   daemon = 1;
[1486]1990#else
1991   ROAR_ERR("--daemon not supported");
1992#endif
[2970]1993  } else if ( strcmp(k, "--verbose") == 0 ) {
1994   g_verbose++;
[71]1995  } else if ( strcmp(k, "--terminate") == 0 ) {
1996   g_terminate = 1;
[905]1997  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1998   sysclocksync = 1000;
[275]1999  } else if ( strcmp(k, "--realtime") == 0 ) {
[276]2000   realtime++;
[4245]2001  } else if ( strcmp(k, "--memlock") == 0 ) {
2002   _CKHAVEARGS(1);
2003   g_config->memlock_level = memlock_str2level(argv[++i]);
[444]2004  } else if ( strcmp(k, "--chroot") == 0 ) {
[4052]2005   _CKHAVEARGS(1);
[1486]2006#ifdef ROAR_HAVE_CHROOT
[444]2007   chrootdir = argv[++i];
[1486]2008#else
2009   ROAR_ERR("--chroot not supported");
2010   i++;
2011#endif
[444]2012  } else if ( strcmp(k, "--setgid") == 0 ) {
[1486]2013#ifdef ROAR_HAVE_SETGID
[444]2014   setids |= R_SETGID;
[1486]2015#else
2016   ROAR_ERR("--setgid not supported");
2017#endif
[444]2018  } else if ( strcmp(k, "--setuid") == 0 ) {
[1486]2019#ifdef ROAR_HAVE_SETUID
[444]2020   setids |= R_SETUID;
[1486]2021#else
2022   ROAR_ERR("--setuid not supported");
2023#endif
[2017]2024  } else if ( strcmp(k, "--location") == 0 ) {
[4052]2025   _CKHAVEARGS(1);
[2017]2026   g_config->location = argv[++i];
[4421]2027  } else if ( strcmp(k, "--description") == 0 ) {
2028   _CKHAVEARGS(1);
2029   g_config->description = argv[++i];
[2104]2030  } else if ( strcmp(k, "--pidfile") == 0 ) {
[4052]2031   _CKHAVEARGS(1);
[2104]2032#ifdef SUPPORT_PIDFILE
2033   pidfile = argv[++i];
2034#else
2035   ROAR_ERR("--pidfile not supported");
2036   i++;
2037#endif
[3688]2038  } else if ( strcmp(k, "--log-syslog") == 0 ) {
2039#ifdef ROAR_HAVE_SYSLOG
2040   roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
2041#else
2042   ROAR_ERR("--log-syslog not supported");
2043#endif
2044
[68]2045
[3356]2046  } else if ( strcmp(k, "--plugin-load") == 0 ) {
[4052]2047   _CKHAVEARGS(1);
[3356]2048   if ( plugins_load(argv[++i]) == -1 ) {
2049    ROAR_ERR("Can not load plugin");
2050   }
2051
[4482]2052  } else if ( strcmp(k, "--guest-acclev") == 0 ) {
2053   _CKHAVEARGS(1);
2054   none_acclev = clients_str2acclev(argv[++i]);
2055  } else if ( strcmp(k, "--trust-acclev") == 0 ) {
2056   _CKHAVEARGS(1);
2057   trust_acclev = clients_str2acclev(argv[++i]);
2058  } else if ( strcmp(k, "--trust-root") == 0 ) {
2059   trust_root = 1;
2060  } else if ( strcmp(k, "--no-trust-root") == 0 ) {
2061   trust_root = 0;
2062
[4788]2063  } else if ( strcmp(k, "--authfile-gen") == 0 ) {
2064   _CKHAVEARGS(1);
2065   af_file = argv[++i];
2066   af_mode = AF_MODE_GEN;
2067  } else if ( strcmp(k, "--authfile-load") == 0 ) {
2068   _CKHAVEARGS(1);
2069   af_file = argv[++i];
2070   af_mode = AF_MODE_LOAD;
2071  } else if ( strcmp(k, "--authfile-type") == 0 ) {
2072   _CKHAVEARGS(1);
2073   af_type = argv[++i];
2074  } else if ( strcmp(k, "--authfile-acclev") == 0 ) {
2075   _CKHAVEARGS(1);
2076   af_acclev = clients_str2acclev(argv[++i]);
[4789]2077  } else if ( strcmp(k, "--new-authfile") == 0 ) {
2078   if ( af_mode != AF_MODE_NONE ) {
2079    if ( add_authfile(af_file, af_type, af_mode, af_acclev) == -1 ) {
2080     ROAR_ERR("main(*): adding authfile '%s' failed!", af_file);
2081    }
2082   }
2083   af_file   = NULL;
2084   af_type   = NULL;
2085   af_mode   = AF_MODE_NONE;
2086   af_acclev = ACCLEV_ALL;
[4788]2087
[280]2088  } else if ( strcmp(k, "--list-cf") == 0 ) {
2089   print_codecfilterlist();
2090   return 0;
2091
[0]2092  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
[4052]2093   _CKHAVEARGS(1);
[0]2094   sa.rate = atoi(argv[++i]);
2095  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
[4052]2096   _CKHAVEARGS(1);
[0]2097   sa.bits = atoi(argv[++i]);
2098  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
[4052]2099   _CKHAVEARGS(1);
[0]2100   sa.channels = atoi(argv[++i]);
2101
[3962]2102  } else if ( strcmp(k, "--aiprofile") == 0 ) {
[4052]2103   _CKHAVEARGS(1);
[3962]2104   if ( roar_profile2info(&sa, argv[++i]) == -1 ) {
2105    ROAR_ERR("Unknown audio profile: %s", argv[i]);
2106    return 1;
2107   }
2108   sa.codec    = ROAR_CODEC_DEFAULT;
2109
[2339]2110  } else if ( strcmp(k, "--stream-flags") == 0 ) {
[4052]2111   _CKHAVEARGS(1);
[2339]2112   if ( update_stream_flags(argv[++i]) == -1 ) {
2113    ROAR_ERR("Can not set stream flags");
2114    return 1;
2115   }
2116
[973]2117  } else if ( strcmp(k, "--list-driver") == 0 ) {
2118   print_driverlist();
2119   return 0;
[0]2120
[932]2121  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
[4052]2122   _CKHAVEARGS(1);
[932]2123   o_drv  = argv[++i];
2124  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
[4052]2125   _CKHAVEARGS(1);
[932]2126   o_dev  = argv[++i];
2127  } else if ( strcmp(k, "-oO") == 0 ) {
[4052]2128   _CKHAVEARGS(1);
[932]2129   o_opts = argv[++i];
[961]2130  } else if ( strcmp(k, "-oP") == 0 ) {
2131   o_prim = 1;
[932]2132  } else if ( strcmp(k, "-oN") == 0 ) {
[4722]2133   if ( action == START || action == RESTART ) {
2134    if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
2135     o_count++;
[1145]2136
[4722]2137    o_drv  = o_dev = o_opts = NULL;
2138    o_prim = 0;
2139   }
[932]2140
[0]2141  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
[4052]2142   _CKHAVEARGS(1);
[2485]2143#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]2144   s_drv = argv[++i];
[2485]2145#else
2146   ROAR_ERR("main(*): No support for sources compiled in");
[4052]2147   i++;
[2485]2148#endif
[550]2149  } else if ( strcmp(k, "-S") == 0 ) {
[4052]2150   _CKHAVEARGS(1);
[2485]2151#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]2152   s_dev = argv[++i];
[2485]2153#else
2154   ROAR_ERR("main(*): No support for sources compiled in");
[4052]2155   i++;
[2485]2156#endif
[0]2157  } else if ( strcmp(k, "-sO") == 0 ) {
[4052]2158   _CKHAVEARGS(1);
[2485]2159#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]2160   s_opt = argv[++i];
[2485]2161#else
2162   ROAR_ERR("main(*): No support for sources compiled in");
[4052]2163   i++;
[2485]2164#endif
[0]2165  } else if ( strcmp(k, "-sC") == 0 ) {
[4052]2166   _CKHAVEARGS(1);
[2485]2167#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]2168   s_con = argv[++i];
[2485]2169#else
2170   ROAR_ERR("main(*): No support for sources compiled in");
[4052]2171   i++;
[2485]2172#endif
[0]2173  } else if ( strcmp(k, "-sP") == 0 ) {
[2485]2174#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]2175   s_prim = 1;
[2485]2176#else
2177   ROAR_ERR("main(*): No support for sources compiled in");
2178#endif
[1110]2179  } else if ( strcmp(k, "-sN") == 0 ) {
[2485]2180#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]2181   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
2182    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
2183   }
2184   s_opt = s_dev = s_con = NULL;
2185   s_drv = "cf";
2186   s_prim = 0;
[2485]2187#else
2188   ROAR_ERR("main(*): No support for sources compiled in");
2189#endif
[2270]2190  } else if ( strcmp(k, "--list-sources") == 0 ) {
[2485]2191#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[2270]2192   print_sourcelist();
2193   return 0;
[2485]2194#else
2195   ROAR_ERR("main(*): No support for sources compiled in");
[2504]2196   return 1;
[2485]2197#endif
[0]2198
[4358]2199  } else if ( strcmp(k, "-m") == 0 || strcmp(k, "--mixer") == 0 ) {
2200   _CKHAVEARGS(1);
2201#ifndef ROAR_WITHOUT_DCOMP_MIXER
2202   m_drv  = argv[++i];
2203#else
2204   ROAR_ERR("main(*): No support for mixer compiled in");
2205   return 1;
2206#endif
2207  } else if ( strcmp(k, "-M") == 0 ) {
2208   _CKHAVEARGS(1);
2209#ifndef ROAR_WITHOUT_DCOMP_MIXER
2210   m_dev  = argv[++i];
2211#else
2212   ROAR_ERR("main(*): No support for mixer compiled in");
2213   return 1;
2214#endif
2215  } else if ( strcmp(k, "-mO") == 0 ) {
2216   _CKHAVEARGS(1);
2217#ifndef ROAR_WITHOUT_DCOMP_MIXER
2218   m_opts = argv[++i];
2219#else
2220   ROAR_ERR("main(*): No support for mixer compiled in");
2221   return 1;
2222#endif
2223  } else if ( strcmp(k, "-mP") == 0 ) {
2224#ifndef ROAR_WITHOUT_DCOMP_MIXER
2225   m_prim = 1;
2226#else
2227   ROAR_ERR("main(*): No support for mixer compiled in");
2228   return 1;
2229#endif
2230  } else if ( strcmp(k, "-mN") == 0 ) {
2231#ifndef ROAR_WITHOUT_DCOMP_MIXER
2232   if ( add_hwmixer(m_drv, m_dev, m_opts, m_prim, m_count) != -1 )
2233    m_count++;
2234
2235   m_drv  = o_dev = o_opts = NULL;
2236   m_prim = 0;
2237#else
2238   ROAR_ERR("main(*): No support for mixer compiled in");
2239   return 1;
2240#endif
[4360]2241  } else if ( strcmp(k, "--list-mixers") == 0 ) {
2242#ifndef ROAR_WITHOUT_DCOMP_MIXER
2243   print_hwmixerlist();
2244   return 0;
2245#else
2246   ROAR_ERR("main(*): No support for mixer compiled in");
2247   return 1;
2248#endif
[4358]2249
[1923]2250  } else if ( strcmp(k, "--light-channels") == 0 ) {
[4052]2251   _CKHAVEARGS(1);
[2494]2252#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]2253   light_channels = atoi(argv[++i]);
[2494]2254#else
2255   ROAR_WARN("main(*): no light subsystem compiled in");
[4052]2256   i++;
[2494]2257#endif
[1923]2258
[2725]2259  } else if ( strcmp(k, "--rds-pi") == 0 ) {
[4052]2260   _CKHAVEARGS(1);
[2725]2261#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2262   g_rdtcs.rds.pi = atoi(argv[++i]);
2263#else
2264   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
[4052]2265   i++;
[2725]2266#endif
[2728]2267  } else if ( strcmp(k, "--rds-ps") == 0 ) {
[4052]2268   _CKHAVEARGS(1);
[2728]2269#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2270   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
[2729]2271    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]);
[2728]2272    return 1;
2273   }
2274#else
2275   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
[4052]2276   i++;
[2728]2277#endif
2278  } else if ( strcmp(k, "--rds-pty") == 0 ) {
[4052]2279   _CKHAVEARGS(1);
[2728]2280#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2281   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
[2729]2282    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
[2728]2283    return 1;
2284   }
2285#else
2286   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
[4052]2287   i++;
[2728]2288#endif
2289  } else if ( strcmp(k, "--rds-tp") == 0 ) {
2290#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2291   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) {
2292    ROAR_ERR("Can not set RDS TP flag");
2293    return 1;
2294   }
2295#else
2296   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2297#endif
2298  } else if ( strcmp(k, "--rds-ct") == 0 ) {
2299#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2300   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) {
2301    ROAR_ERR("Can not set RDS CT flag");
2302    return 1;
2303   }
2304#else
2305   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
2306#endif
2307
[2725]2308
[1924]2309  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
[2487]2310#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]2311   midi_config.init_cb = 0;
[2487]2312#else
2313   // no warning here as this is the disable option
2314#endif
[2444]2315  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
[2487]2316#ifndef ROAR_WITHOUT_DCOMP_CB
[2444]2317   midi_config.init_cb = 1;
[2487]2318#else
2319   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
2320#endif
[1924]2321  } else if ( strcmp(k, "--midi-console") == 0 ) {
[4052]2322   _CKHAVEARGS(1);
[2487]2323#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]2324   midi_config.console_dev = argv[++i];
[2444]2325   midi_config.init_cb = 1;
[2487]2326#else
2327   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
[4052]2328   i++;
[2487]2329#endif
[1924]2330
[2451]2331  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
[2489]2332#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]2333   ssynth_conf.enable = 1;
[2489]2334#else
2335   ROAR_ERR("main(*): No support for ssynth compiled in");
2336#endif
[2451]2337  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
[2489]2338#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]2339   ssynth_conf.enable = 0;
[2489]2340#else
2341   // we can safely ignore the disable
2342#endif
[2451]2343
[3374]2344  } else if ( strcmp(k, "--x11-display") == 0 || strcmp(k, "--display") == 0 ) {
[4052]2345   _CKHAVEARGS(1);
[3374]2346#ifdef ROAR_HAVE_LIBX11
2347   x11display = argv[++i];
2348#else
2349   ROAR_ERR("No X11 support compiled in!");
2350   return 1;
2351#endif
2352
2353
[0]2354  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
[4052]2355   _CKHAVEARGS(1);
[4012]2356   // This is only useful in INET not UNIX mode.
[1494]2357#ifdef ROAR_SUPPORT_LISTEN
[2530]2358   if ( *sock_addr == '/' )
2359    sock_addr = ROAR_DEFAULT_HOST;
[447]2360
[446]2361   errno = 0;
2362   if ( (port = atoi(argv[++i])) < 1 ) {
[1486]2363#ifdef ROAR_HAVE_GETSERVBYNAME
[446]2364    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
2365     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
2366     return 1;
2367    }
2368    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
2369    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
2370            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
2371    port = ROAR_NET2HOST16(serv->s_port);
[1486]2372#else
2373    ROAR_ERR("invalite port number: %s", argv[i]);
2374    return 1;
2375#endif
[446]2376   }
[1494]2377#endif
[1115]2378  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
[4052]2379   _CKHAVEARGS(1);
[1494]2380#ifdef ROAR_SUPPORT_LISTEN
[2530]2381   sock_addr = argv[++i];
[4052]2382#else
2383   i++;
[2530]2384#endif
2385
2386  } else if ( strcmp(k, "--proto") == 0 ) {
2387#ifdef ROAR_SUPPORT_LISTEN
[2549]2388   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
[2530]2389    ROAR_ERR("Unknown protocol: %s", argv[i]);
2390    return 1;
2391   }
[1494]2392#endif
[3252]2393  } else if ( strcmp(k, "--proto-dir") == 0 ) {
[4052]2394   _CKHAVEARGS(1);
[3252]2395#ifdef ROAR_SUPPORT_LISTEN
2396   if ( (sock_dir = roar_str2dir(argv[++i])) == -1 ) {
2397    ROAR_ERR("Unknown stream direction: %s", argv[i]);
2398    return 1;
2399   }
[4052]2400#else
2401   i++;
[3252]2402#endif
2403  } else if ( strcmp(k, "--proto-rate") == 0 ) {
[4052]2404   _CKHAVEARGS(1);
[3252]2405#ifdef ROAR_SUPPORT_LISTEN
2406   sock_info.rate = atoi(argv[++i]);
[4052]2407#else
2408   i++;
[3252]2409#endif
2410  } else if ( strcmp(k, "--proto-bits") == 0 ) {
[4052]2411   _CKHAVEARGS(1);
[3252]2412#ifdef ROAR_SUPPORT_LISTEN
2413   sock_info.bits = atoi(argv[++i]);
[4052]2414#else
2415   i++;
[3252]2416#endif
2417  } else if ( strcmp(k, "--proto-chans") == 0 ) {
[4052]2418   _CKHAVEARGS(1);
[3252]2419#ifdef ROAR_SUPPORT_LISTEN
2420   sock_info.channels = atoi(argv[++i]);
[4052]2421#else
2422   i++;
[3252]2423#endif
2424  } else if ( strcmp(k, "--proto-codec") == 0 ) {
[4052]2425   _CKHAVEARGS(1);
[3252]2426#ifdef ROAR_SUPPORT_LISTEN
2427   if ( (sock_info.codec = roar_str2codec(argv[++i])) == -1 ) {
2428    ROAR_ERR("Unknown codec: %s", argv[i]);
2429    return 1;
2430   }
[4052]2431#else
2432   i++;
[3252]2433#endif
[3954]2434  } else if ( strcmp(k, "--proto-aiprofile") == 0 ) {
[4052]2435   _CKHAVEARGS(1);
[3954]2436#ifdef ROAR_SUPPORT_LISTEN
2437   if ( roar_profile2info(&sock_info, argv[++i]) == -1 ) {
2438    ROAR_ERR("Unknown audio profile: %s", argv[i]);
2439    return 1;
2440   }
[4052]2441#else
2442   i++;
[3954]2443#endif
[3956]2444  } else if ( strcmp(k, "--list-profiles") == 0 ) {
2445#ifdef ROAR_SUPPORT_LISTEN
2446   listen_listen_profiles();
2447   return 0;
2448#endif
2449  } else if ( strcmp(k, "--proto-profile") == 0 ) {
[4052]2450   _CKHAVEARGS(1);
[3956]2451#ifdef ROAR_SUPPORT_LISTEN
2452   if ( get_listen_profile(argv[++i], &port, &sock_addr, &sock_type, &sock_proto, &sock_dir, &sock_info) == -1 ) {
2453    ROAR_ERR("Unknown listen profile: %s", argv[i]);
2454    return 1;
2455   }
[4052]2456#else
2457   i++;
[3956]2458#endif
[3252]2459
2460
[3127]2461  } else if ( strcmp(k, "--list-proto") == 0 ) {
2462   list_proto();
2463   return 0;
[518]2464
[573]2465  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
[1494]2466#ifdef ROAR_SUPPORT_LISTEN
[518]2467   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
2468    sock_type = ROAR_SOCKET_TYPE_TCP;
2469
[2530]2470   if ( *sock_addr == '/' )
2471    sock_addr = ROAR_DEFAULT_HOST;
[1494]2472#endif
[518]2473
2474  } else if ( strcmp(k, "-4") == 0 ) {
[1494]2475#ifdef ROAR_SUPPORT_LISTEN
[517]2476   sock_type = ROAR_SOCKET_TYPE_TCP;
[2530]2477   if ( *sock_addr == '/' )
2478    sock_addr = ROAR_DEFAULT_HOST;
[1494]2479#endif
[518]2480  } else if ( strcmp(k, "-6") == 0 ) {
[1494]2481#ifdef ROAR_SUPPORT_LISTEN
[3837]2482#ifdef AF_INET6
[518]2483   sock_type = ROAR_SOCKET_TYPE_TCP6;
[2530]2484   if ( *sock_addr == '/' )
2485    sock_addr = ROAR_DEFAULT_HOST;
[519]2486#else
2487    ROAR_ERR("No IPv6 support compiled in!");
2488    return 1;
2489#endif
[1494]2490#endif
[518]2491
[573]2492  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
[1494]2493#ifdef ROAR_SUPPORT_LISTEN
[62]2494   // ignore this case as it is the default behavor.
[517]2495   sock_type = ROAR_SOCKET_TYPE_UNIX;
[1494]2496#endif
[518]2497
[573]2498  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
[1494]2499#ifdef ROAR_SUPPORT_LISTEN
[508]2500#ifdef ROAR_HAVE_LIBDNET
2501    port   = ROAR_DEFAULT_NUM;
2502    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
[2530]2503    sock_addr = decnethost;
[517]2504    sock_type = ROAR_SOCKET_TYPE_DECNET;
[508]2505#else
2506    ROAR_ERR("No DECnet support compiled in!");
2507    return 1;
2508#endif
[1494]2509#endif
[2530]2510  } else if ( strcmp(k, "--new-sock") == 0 ) {
2511#ifdef ROAR_SUPPORT_LISTEN
[4722]2512   if ( action == START || action == RESTART ) {
2513    if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
2514     ROAR_ERR("Can not open listen socket!");
2515     return 1;
2516    }
[2530]2517   }
2518#endif
[518]2519
[1993]2520  } else if ( strcmp(k, "--slp") == 0 ) {
2521#ifdef ROAR_HAVE_LIBSLP
2522   reg_slp = 1;
2523#else
[3373]2524   ROAR_ERR("No OpenSLP support compiled in!");
2525   return 1;
[1993]2526#endif
2527
[3373]2528  } else if ( strcmp(k, "--x11") == 0 ) {
2529#ifdef ROAR_HAVE_LIBX11
2530   reg_x11 = 1;
2531#else
2532   ROAR_ERR("No X11 support compiled in!");
2533   return 1;
2534#endif
2535
2536
[3039]2537  } else if ( strcmp(k, "--jumbo-mtu") == 0 ) {
[4052]2538   _CKHAVEARGS(1);
[3039]2539   g_config->jumbo_mtu = atoi(argv[++i]);
2540
[60]2541  } else if ( strcmp(k, "-G") == 0 ) {
[4052]2542   _CKHAVEARGS(1);
[444]2543   sock_grp  = argv[++i];
2544  } else if ( strcmp(k, "-U") == 0 ) {
[4052]2545   _CKHAVEARGS(1);
[444]2546   sock_user = argv[++i];
[0]2547
[68]2548  } else if ( strcmp(k, "--no-listen") == 0 ) {
[1494]2549#ifdef ROAR_SUPPORT_LISTEN
[2530]2550   sock_addr   = "";
[548]2551   g_terminate = 1;
[1155]2552   g_no_listen = 1;
[1494]2553#endif
[68]2554  } else if ( strcmp(k, "--client-fh") == 0 ) {
[4052]2555   _CKHAVEARGS(1);
[3737]2556   if ( clients_new_from_fh(atoi(argv[++i]), ROAR_PROTO_ROARAUDIO, ROAR_BYTEORDER_NETWORK, 1) == -1 ) {
[68]2557    ROAR_ERR("main(*): Can not set client's fh");
2558    return 1;
2559   }
[501]2560  } else if ( strcmp(k, "--close-fh") == 0 ) {
[4052]2561   _CKHAVEARGS(1);
[1486]2562#ifdef ROAR_HAVE_IO_POSIX
[501]2563   close(atoi(argv[++i]));
[1486]2564#else
2565   i++;
2566   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
2567#endif
[68]2568
[920]2569  } else if ( strcmp(k, "--standby") == 0 ) {
2570   g_standby = 1;
2571  } else if ( strcmp(k, "--auto-standby") == 0 ) {
2572   g_autostandby = 1;
[0]2573  } else {
2574   usage();
2575   return 1;
2576  }
2577
2578 }
[1503]2579#endif
[0]2580
[4722]2581#ifdef ROAR_HAVE_MAIN_ARGS
2582 switch (action) {
2583  case START:
2584    // NO-OP.
2585   break;
2586  case RESTART:
2587    // NO-OP, done before.
2588   break;
2589  case STOP:
2590#ifdef ROAR_SUPPORT_LISTEN
2591    if ( restart_server(sock_addr, 0) == -1 ) {
2592     ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
2593     return 1;
2594    }
2595    return 0;
2596#else
2597    ROAR_ERR("--stop not supported");
2598    return 1;
2599#endif
2600   break;
2601  case SHUTDOWN:
2602#ifdef ROAR_SUPPORT_LISTEN
2603    if ( restart_server(sock_addr, 1) == -1 ) {
2604     ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
2605     return 1;
2606    }
2607    return 0;
2608#else
2609    ROAR_ERR("--shutdown not supported");
2610    return 1;
2611#endif
2612   break;
2613  case RESTART_RETRY:
2614#ifdef ROAR_SUPPORT_LISTEN
2615    if ( restart_server(sock_addr, 1) == -1 ) {
2616     ROAR_WARN("Can not terminate old server (again) (not running at %s?), tring to continue anyway", sock_addr);
2617     action = RESTART;
2618    }
2619#endif
2620   break;
2621 }
2622#endif
2623
[4418]2624#ifndef DEBUG
2625 // notify dbg if requested, if in DEBUG mode
2626 // do not able because it got enabled early.
2627 if ( g_verbose >= 4 )
2628  dbg_notify_cb_register();
2629#endif
2630
[4788]2631 if ( af_mode != AF_MODE_NONE ) {
2632  if ( add_authfile(af_file, af_type, af_mode, af_acclev) == -1 ) {
2633   ROAR_ERR("main(*): adding authfile '%s' failed!", af_file);
2634  }
2635 }
2636
[4358]2637#ifndef ROAR_WITHOUT_DCOMP_MIXER
2638 if ( m_drv != NULL ) {
2639  if ( add_hwmixer(m_drv, m_dev, m_opts, m_prim, m_count) == -1 ) {
2640   ROAR_ERR("main(*): adding mixer '%s' via '%s' failed!", m_dev, m_drv);
2641  }
2642 }
2643#endif
2644
[2485]2645#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]2646 if ( s_dev != NULL ) {
2647  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
2648   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
2649  }
2650 }
[2485]2651#endif
[1110]2652
[4848]2653 if ( add_default_output(o_drv, o_dev, o_opts, o_prim, o_count) == -1 ) {
2654  ROAR_ERR("Can not initialize default driver");
2655  return 1;
2656 }
[932]2657
[4055]2658 ROAR_INFO("Server config: rate=%i, bits=%i, chans=%i", ROAR_DBG_INFO_NOTICE, sa.rate, sa.bits, sa.channels);
[0]2659
[2945]2660 if ( waveform_init() == -1 ) {
2661  ROAR_ERR("Can not initialize Waveform subsystem");
2662  return 1;
2663 }
2664
[2500]2665#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1819]2666 if ( midi_init() == -1 ) {
[281]2667  ROAR_ERR("Can not initialize MIDI subsystem");
[1819]2668 }
[2500]2669#endif
[1819]2670
[2489]2671#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]2672 if ( ssynth_init() == -1 ) {
2673  ROAR_ERR("Can not initialize ssynth subsystem");
2674 }
[2489]2675#endif
[2451]2676
[2494]2677#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]2678 if ( light_init(light_channels) == -1 ) {
2679  ROAR_ERR("Can not initialize light control subsystem");
2680 }
[2494]2681#endif
[281]2682
[2718]2683#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2684 if ( rdtcs_init() == -1 ) {
2685  ROAR_ERR("Can not initialize RDTCS subsystem");
2686 }
2687#endif
2688
[3354]2689 if ( plugins_init() == -1 ) {
2690  ROAR_ERR("Can not initialize plugins");
2691 }
2692
[1494]2693#ifdef ROAR_SUPPORT_LISTEN
[3760]2694 if ( !g_no_listen ) {
2695  if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
2696   ROAR_ERR("Can not open listen socket!");
2697   return 1;
2698  }
[60]2699 }
[1494]2700#endif
[60]2701
[0]2702 if ( output_buffer_init(&sa) == -1 ) {
2703  ROAR_ERR("Can not init output buffer!");
2704  return 1;
2705 }
2706
[44]2707 if ( samples_init() == -1 ) {
2708  ROAR_ERR("Can not init samples!");
2709  return 1;
2710 }
2711
2712
[1486]2713 // we should handle this on microcontrollers, too.
[1753]2714#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
[0]2715 signal(SIGINT,  on_sig_int);
[2732]2716 signal(SIGTERM, on_sig_term);
[285]2717 signal(SIGCHLD, on_sig_chld);
[2112]2718 signal(SIGUSR1, on_sig_usr1);
[0]2719 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
[1486]2720#endif
[0]2721
[4245]2722 if ( g_config->memlock_level == -1 ) {
2723  g_config->memlock_level = MEMLOCK_DEFAULT;
2724 }
2725
2726 if ( memlock_set_level(g_config->memlock_level) == -1 ) {
2727  ROAR_WARN("Can not set memory locking level to target level.");
2728 }
2729
[275]2730 if ( realtime ) {
[278]2731#ifdef DEBUG
[4246]2732  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compile without -DDEBUG");
[278]2733#endif
2734
[1486]2735#ifdef ROAR_HAVE_NICE
[4739]2736  // this stupid error check is because type of returned data of nice() changed
2737  // too often. On some systems it may return 0/-1, on some new nice value or
2738  // mixed forms of both.
[275]2739  errno = 0;
[4739]2740  (void)nice(-5*realtime); // -5 for each --realtime
[1486]2741  if ( errno ) {
2742   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2743  }
2744#else
2745  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2746#endif
[277]2747/*
[276]2748#ifdef __linux__
[277]2749  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
2750   ROAR_WARN("Can not set io priority: %s", strerror(errno));
[276]2751#endif
[277]2752*/
[275]2753 }
2754
[2756]2755#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[444]2756 if ( setids & R_SETGID ) {
[2754]2757  if ( sock_grp == NULL ) {
2758   ROAR_ERR("Can not set GID if no groupname is supplied");
2759   return 1;
2760  }
[2584]2761  if ( (grp = getgrnam(sock_grp)) == NULL ) {
2762   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
2763   return 1;
2764  }
[444]2765  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
2766   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
2767  }
[523]2768  if ( !grp || setgid(grp->gr_gid) == -1 ) {
[444]2769   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
2770  }
2771 }
[1486]2772#endif
[444]2773
[0]2774
[39]2775 clients_set_pid(g_self_client, getpid());
[1753]2776#ifdef ROAR_HAVE_GETUID
[440]2777 clients_set_uid(g_self_client, getuid());
[1753]2778#endif
2779#ifdef ROAR_HAVE_GETGID
[440]2780 clients_set_gid(g_self_client, getgid());
[1753]2781#endif
[39]2782 clients_get(g_self_client, &self);
[37]2783
[39]2784 if ( self == NULL ) {
2785  ROAR_ERR("Can not get self client!");
2786  return 1;
2787 }
2788
[775]2789 strcpy(self->name, "RoarAudio daemon internal");
[68]2790
[2815]2791 if ( roar_nnode_free(&(self->nnode)) == -1 )
2792  return 1;
2793
2794 // not fully correct but ok as workaorund
2795 // so tools assume that roard runs on the same machine as
2796 // they in case they use AF_UNIX:
2797 if ( roar_nnode_new(&(self->nnode), ROAR_SOCKET_TYPE_UNIX) == -1 )
2798  return 1;
2799
[1486]2800#ifdef ROAR_HAVE_FORK
[775]2801 if ( daemon ) {
[3613]2802#ifdef ROAR_HAVE_SYSLOG
2803  roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
2804#else
[3609]2805  roar_debug_set_stderr_fh(-1);
[3613]2806#endif
[3609]2807
[68]2808  close(ROAR_STDIN );
2809  close(ROAR_STDOUT);
2810  close(ROAR_STDERR);
[1753]2811
[68]2812  if ( fork() )
[1486]2813   ROAR_U_EXIT(0);
[1753]2814
2815#ifdef ROAR_HAVE_SETSID
2816  setsid();
2817#endif
[1046]2818  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
[68]2819 }
[1486]2820#endif
[68]2821
[2757]2822#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2823 // early test for UID as we need this for the pidfile and the setuid()
2824 if ( sock_user != NULL ) {
2825  if ( (pwd = getpwnam(sock_user)) == NULL ) {
2826   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
2827   return 1;
2828  }
2829 }
2830#endif
2831
[4423]2832 ROAR_INFO("Process ID: %i", ROAR_DBG_INFO_INFO, (int)getpid());
2833
[2104]2834#ifdef SUPPORT_PIDFILE
[2106]2835 if ( pidfile != NULL ) {
2836  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
2837   ROAR_ERR("Can not write pidfile: %s", pidfile);
2838  } else {
2839   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
2840   roar_vio_close(&pidfile_vio);
2841  }
[2765]2842#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[2759]2843  if ( pwd || grp ) {
2844   if ( chown(pidfile, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 ) {
2845    ROAR_WARN("Can not change ownership of pidfile: %s: %s", pidfile, strerror(errno));
2846   }
2847  }
2848  if ( chmod(pidfile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1 ) {
2849   ROAR_WARN("Can not change permissions of pidfile: %s: %s", pidfile, strerror(errno));
2850  }
[2765]2851#endif
[2104]2852 }
2853#endif
2854
[1486]2855#ifdef ROAR_HAVE_CHROOT
[444]2856 if (chrootdir) {
2857  if ( chroot(chrootdir) == -1 ) {
2858   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
2859   return 2;
2860  }
2861  if ( chdir("/") == -1 ) {
2862   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
2863   return 2;
2864  }
2865 }
[1486]2866#endif
[444]2867
[2756]2868#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[444]2869 if ( setids & R_SETUID ) {
[2752]2870  if ( sock_user == NULL ) {
2871   ROAR_ERR("Can not set UID if no username is supplied");
2872   return 1;
2873  }
[444]2874  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
2875   ROAR_ERR("Can not set UserID: %s", strerror(errno));
2876   return 3;
2877  }
[1753]2878#ifdef ROAR_HAVE_GETUID
[444]2879  clients_set_uid(g_self_client, getuid());
[1753]2880#endif
[444]2881 }
[1486]2882#endif
[444]2883
[4481]2884 // setup auth:
[4690]2885#if defined(ROAR_HAVE_GETUID) && defined(ROAR_HAVE_GETGID)
[4481]2886 if ( auth_setup(none_acclev, trust_acclev, trust_root, getuid(), getgid()) == -1 ) {
[4690]2887#else
2888 if ( auth_setup(none_acclev, trust_acclev, trust_root, -1, -1) == -1 ) {
2889#endif
[4481]2890  ROAR_ERR("Can not set up auth. Bad.");
2891  alive = 0;
2892 }
2893
[1993]2894 // Register with OpenSLP:
[2028]2895#ifdef ROAR_HAVE_LIBSLP
[1993]2896 if ( reg_slp ) {
[2530]2897  register_slp(0, sock_addr);
[1993]2898 }
[2028]2899#endif
[1993]2900
[3373]2901#ifdef ROAR_HAVE_LIBX11
2902 if ( reg_x11 ) {
2903  register_x11(0, sock_addr);
2904 }
2905#endif
2906
[4846]2907 // update sync counter.
2908 streams_change_sync_num(-1, 0);
2909
[4103]2910 ROAR_INFO("Startup complet", ROAR_DBG_INFO_INFO);
2911
[0]2912 // start main loop...
[4055]2913 ROAR_INFO("Entering main loop", ROAR_DBG_INFO_INFO);
[4811]2914 main_loop(&sa, sysclocksync);
[4055]2915 ROAR_INFO("Left main loop", ROAR_DBG_INFO_INFO);
[0]2916
2917 // clean up.
2918 clean_quit_prep();
2919 output_buffer_free();
2920
[4323]2921 roar_notify_core_free(NULL);
2922
[4055]2923 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
[0]2924 return 0;
2925}
2926
[574]2927void cleanup_listen_socket (int terminate) {
[2530]2928 int i;
2929
[4055]2930 ROAR_DBG("cleanup_listen_socket(terminate=%i) = (void)?", terminate);
2931
2932 ROAR_INFO("Cleaning up listen sockets", ROAR_DBG_INFO_INFO);
2933
[1993]2934 // Deregister from SLP:
[2028]2935#ifdef ROAR_HAVE_LIBSLP
[1993]2936 register_slp(1, NULL);
[2028]2937#endif
[580]2938
[3373]2939#ifdef ROAR_HAVE_LIBX11
2940 register_x11(1, NULL);
2941#endif
2942
[1494]2943#ifdef ROAR_SUPPORT_LISTEN
[2530]2944 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
[3802]2945  if ( g_listen[i].used  ) {
2946   roar_vio_close(&(g_listen[i].sock));
[60]2947
[3802]2948   g_listen[i].used = 0;
[576]2949
[1486]2950#ifdef ROAR_HAVE_UNIX
[2530]2951   if ( server[i] != NULL )
2952    if ( *(server[i]) == '/' )
2953     unlink(server[i]);
[1486]2954#endif
[2530]2955  }
[580]2956 }
[60]2957
[1494]2958#endif
2959
[574]2960 if ( terminate )
2961  g_terminate = 1;
2962}
2963
2964void clean_quit_prep (void) {
2965 cleanup_listen_socket(0);
[60]2966
[3354]2967 plugins_free();
2968
[2485]2969#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]2970 sources_free();
[2485]2971#endif
[0]2972 streams_free();
2973 clients_free();
[2489]2974#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]2975 ssynth_free();
[2489]2976#endif
[2487]2977#ifndef ROAR_WITHOUT_DCOMP_CB
[282]2978 midi_cb_stop(); // stop console beep
[2487]2979#endif
[2500]2980#ifndef ROAR_WITHOUT_DCOMP_MIDI
[281]2981 midi_free();
[2500]2982#endif
[2494]2983#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]2984 light_free();
[2494]2985#endif
[2718]2986#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2987 rdtcs_free();
2988#endif
[2104]2989
[2945]2990 waveform_free();
2991
[2104]2992#ifdef SUPPORT_PIDFILE
2993 if ( pidfile != NULL )
2994  unlink(pidfile);
2995#endif
[4469]2996
2997 auth_free();
[0]2998}
2999
3000void clean_quit (void) {
[4055]3001 ROAR_INFO("Shuting down", ROAR_DBG_INFO_INFO);
3002
[4101]3003 counters_print(ROAR_DEBUG_TYPE_INFO, 0);
3004
[0]3005 clean_quit_prep();
3006// output_buffer_free();
[4055]3007
[4323]3008 roar_notify_core_free(NULL);
3009
[4055]3010 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
[0]3011 exit(0);
3012}
3013
3014//ll
Note: See TracBrowser for help on using the repository browser.