source: roaraudio/roard/roard.c @ 5567:6ecf012d7063

Last change on this file since 5567:6ecf012d7063 was 5567:6ecf012d7063, checked in by phi, 12 years ago

roard now tries to auto load missing protocols as plugins (Closes: #275)

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