source: roaraudio/roard/roard.c @ 5651:b0e163908378

Last change on this file since 5651:b0e163908378 was 5651:b0e163908378, checked in by phi, 12 years ago

Added compiler checks for printf() like format strings.

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