source: roaraudio/roard/roard.c @ 4858:ef003b336588

Last change on this file since 4858:ef003b336588 was 4852:166a9cc557c9, checked in by phi, 13 years ago

moved a lot stuff out of roard.c

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