source: roaraudio/roard/roard.c @ 5194:b40015b16ff2

Last change on this file since 5194:b40015b16ff2 was 5194:b40015b16ff2, checked in by phi, 13 years ago

make more use of /extern/

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