source: roaraudio/roard/roard.c @ 5593:c6f715635b44

Last change on this file since 5593:c6f715635b44 was 5592:da9a9bb6ece0, checked in by phi, 12 years ago

addded --list-plugins

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