source: roaraudio/roard/roard.c @ 5257:27194d561c16

Last change on this file since 5257:27194d561c16 was 5257:27194d561c16, checked in by phi, 12 years ago

fixed most warnings in roard

File size: 75.8 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(%lu) */", (long unsigned int)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(%lu) */", (long unsigned int)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(%lu) */", (long unsigned int)ROAR_NOTIFY_EVENT2OE(ev));
158    break;
159  }
160 } else if ( ROAR_NOTIFY_IS_USER(ev) ) {
161  snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_USER2EVENT(%lu) */", (long unsigned int)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 (const 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_dstr_simple(&fh, pidfile, O_RDONLY) ) {
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
961#ifdef ROAR_HAVE_MAIN_ARGS
962static void list_aiprofiles (void) {
963 struct roar_audio_info info;
964 const char * list[1024];
965 const char * mime;
966 ssize_t ret;
967 ssize_t i;
968
969 ret = roar_profiles_list(list, 1024, 0);
970
971 if ( ret == -1 )
972  return;
973
974 printf("  Name       Rate  Bits  Channels  Codec\n");
975 printf("---------------------------------------------\n");
976 for (i = 0; i < ret; i++) {
977  if ( roar_profile2info(&info, list[i]) == -1 ) {
978   printf("  %-10s --- unknown parameters ---\n", list[i]);
979   continue;
980  }
981
982  mime = roar_codec2mime(info.codec);
983
984  printf("  %-10s %5u %4u  %8u  %3u (%s%s%s%s)\n", list[i],
985            info.rate, info.bits, info.channels,
986            info.codec, roar_codec2str(info.codec),
987            info.codec == ROAR_CODEC_DEFAULT ? " native" : "",
988            mime == NULL ? "" : " mimetype:",
989            mime == NULL ? "" : mime
990        );
991 }
992}
993#endif
994
995int update_stream_flags (char * str) {
996 int    dir;
997 char * flags;
998 char * k;
999 int    op;
1000 int    flag;
1001
1002 if ( (flags = strstr(str, "=")) == NULL )
1003  return -1;
1004
1005 *flags = 0;
1006  flags++;
1007
1008 if ( (dir = roar_str2dir(str)) == -1 )
1009  return -1;
1010
1011 while (flags != NULL) {
1012  k = flags;
1013  flags = strstr(flags, ",");
1014
1015  if ( flags != NULL )
1016   *(flags++) = 0;
1017
1018  switch (*k) {
1019   case '+': k++; op = ROAR_SET_FLAG;   break;
1020   case '-': k++; op = ROAR_RESET_FLAG; break;
1021   default:
1022     op = ROAR_SET_FLAG;
1023  }
1024
1025  flag = 0;
1026
1027  if ( !strcmp(k, "sync") ) {
1028   flag = ROAR_FLAG_SYNC;
1029  } else if ( !strcmp(k, "meta") ) {
1030   flag = ROAR_FLAG_META;
1031  } else if ( !strcmp(k, "cleanmeta") ) {
1032   flag = ROAR_FLAG_CLEANMETA;
1033  } else if ( !strcmp(k, "pause") ) {
1034   flag = ROAR_FLAG_PAUSE;
1035  } else if ( !strcmp(k, "mute") ) {
1036   flag = ROAR_FLAG_MUTE;
1037  } else if ( !strcmp(k, "antiecho") ) {
1038   flag = ROAR_FLAG_ANTIECHO;
1039  } else if ( !strcmp(k, "passmixer") ) {
1040   flag = ROAR_FLAG_PASSMIXER;
1041  } else if ( !strcmp(k, "recsource") ) {
1042   flag = ROAR_FLAG_RECSOURCE;
1043  } else {
1044   return -1;
1045  }
1046
1047  g_config->streams[dir].flags |= flag;
1048
1049  if ( op == ROAR_RESET_FLAG )
1050   g_config->streams[dir].flags -= flag;
1051 }
1052
1053 return 0;
1054}
1055
1056int add_authfile (const char * file, const char * type, enum af_mode mode, enum roard_client_acclev acclev) {
1057 struct roar_authfile * authfile = NULL;
1058 struct roar_authfile_key *  key = NULL;
1059 int af_type = ROAR_AUTHFILE_TYPE_AUTO;
1060 void * keydata;
1061 int i;
1062
1063 if ( type == NULL ) {
1064  // noop.
1065 } else if ( !strcasecmp(type, "roar") ) {
1066  af_type = ROAR_AUTHFILE_TYPE_ROAR;
1067 } else if ( !strcasecmp(type, "roar") ) {
1068  af_type = ROAR_AUTHFILE_TYPE_ROAR;
1069 } else if ( !strcasecmp(type, "esd") ) {
1070  af_type = ROAR_AUTHFILE_TYPE_ESD;
1071 } else if ( !strcasecmp(type, "pulse") ) {
1072  af_type = ROAR_AUTHFILE_TYPE_PULSE;
1073 } else if ( !strcasecmp(type, "htpasswd") ) {
1074  af_type = ROAR_AUTHFILE_TYPE_HTPASSWD;
1075 } else if ( !strcasecmp(type, "xauth") ) {
1076  af_type = ROAR_AUTHFILE_TYPE_XAUTH;
1077 } else {
1078  ROAR_ERR("add_authfile(*): unknown authfile type '%s'.", type);
1079  return -1;
1080 }
1081
1082 if ( mode == AF_MODE_GEN && af_type == ROAR_AUTHFILE_TYPE_AUTO )
1083  af_type = ROAR_AUTHFILE_TYPE_ESD;
1084
1085 switch (mode) {
1086  case AF_MODE_NONE:
1087    return 0;
1088   break;
1089  case AF_MODE_GEN:
1090    switch (af_type) {
1091     case ROAR_AUTHFILE_TYPE_ESD:
1092       key = roar_authfile_key_new_random(ROAR_AUTH_T_COOKIE, 16, NULL);
1093      break;
1094     case ROAR_AUTHFILE_TYPE_PULSE:
1095       key = roar_authfile_key_new_random(ROAR_AUTH_T_COOKIE, 256, NULL);
1096      break;
1097     default:
1098       return -1;
1099      break;
1100    }
1101
1102    if ( key == NULL ) {
1103     ROAR_ERR("add_authfile(*): Can not generate key.");
1104     return -1;
1105    }
1106
1107    if ( (authfile = roar_authfile_open(af_type, file, 1, ROAR_AUTHFILE_VERSION_AUTO)) == NULL ) {
1108     roar_authfile_key_unref(key);
1109     return -1;
1110    }
1111
1112    if ( roar_authfile_add_key(authfile, key) == -1 ) {
1113     ROAR_WARN("add_authfile(*): Can not add key to authfile.");
1114    }
1115
1116    keydata = roar_mm_memdup(key->data, key->len);
1117
1118    if ( keydata == NULL ) {
1119     ROAR_WARN("add_authfile(*): Can not allocate memory for key.");
1120    } else if ( auth_addkey_cookie(acclev, keydata, key->len) == -1 ) {
1121     ROAR_WARN("add_authfile(*): Can not add key to internal key storage.");
1122    }
1123
1124    roar_authfile_key_unref(key);
1125    if ( roar_authfile_close(authfile) != 0 )
1126     return -1;
1127    return 0;
1128   break;
1129  case AF_MODE_LOAD:
1130    if ( (authfile = roar_authfile_open(af_type, file, 0, ROAR_AUTHFILE_VERSION_AUTO)) == NULL ) {
1131     return -1;
1132    }
1133
1134    for (i = 0; ; i++) {
1135     key = roar_authfile_lookup_key(authfile, ROAR_AUTH_T_AUTO, i, NULL);
1136     if ( key == NULL )
1137      break;
1138
1139     if ( key->type == ROAR_AUTH_T_COOKIE ) {
1140      keydata = roar_mm_memdup(key->data, key->len);
1141
1142      if ( keydata == NULL ) {
1143       ROAR_WARN("add_authfile(*): Can not allocate memory for key.");
1144      } else if ( auth_addkey_cookie(acclev, keydata, key->len) == -1 ) {
1145       ROAR_WARN("add_authfile(*): Can not add key to internal key storage.");
1146      }
1147     } else {
1148      ROAR_WARN("add_authfile(*): Unknown key type: %i", key->type);
1149     }
1150
1151     roar_authfile_key_unref(key);
1152    }
1153
1154    if ( roar_authfile_close(authfile) != 0 )
1155     return -1;
1156    return 0;
1157   break;
1158 }
1159
1160 return -1;
1161}
1162
1163// X11:
1164#ifdef ROAR_HAVE_LIBX11
1165int register_x11 (int unreg, char * sockname) {
1166 struct roar_x11_connection * x11con = NULL;
1167 int ret = 0;
1168
1169 if ( (x11con = roar_x11_connect(x11display)) == NULL ) {
1170  ROAR_ERR("Can not connect to X11 server for %sregistering", unreg ? "un" : "");
1171  return -1;
1172 }
1173
1174 if ( unreg ) {
1175  if ( roar_x11_delete_prop(x11con, "ROAR_SERVER") == -1 ) {
1176   ret = -1;
1177   ROAR_ERR("Error while unregistereing from X11", ROAR_DBG_INFO_INFO);
1178  } else {
1179   ROAR_INFO("Successfully unregistered from X11", ROAR_DBG_INFO_INFO);
1180  }
1181 } else {
1182  if ( roar_x11_set_prop(x11con, "ROAR_SERVER", sockname) == -1 ) {
1183   ret = -1;
1184   ROAR_ERR("Error while registereing to X11", ROAR_DBG_INFO_INFO);
1185  } else {
1186   ROAR_INFO("Successfully registered to X11", ROAR_DBG_INFO_INFO);
1187  }
1188 }
1189
1190 roar_x11_disconnect(x11con);
1191
1192 return ret;
1193}
1194#endif
1195
1196// SLP:
1197void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
1198 /* return the error code in the cookie */
1199 *(SLPError*)cookie = errcode;
1200}
1201
1202int register_slp (int unreg, char * sockname) {
1203#ifdef ROAR_HAVE_LIBSLP
1204 static int regged = 0;
1205 static char * sn = NULL;
1206 SLPError err;
1207 SLPError callbackerr;
1208 SLPHandle hslp;
1209 char addr[1024];
1210 char attr[1024] = "";
1211 char * location;
1212 char * description;
1213 char * standards;
1214
1215 if ( sockname != NULL )
1216  sn = sockname;
1217
1218 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE_ROAR "://%s", sn);
1219
1220 err = SLPOpen("en", SLP_FALSE, &hslp);
1221
1222 if (err != SLP_OK) {
1223  ROAR_ERR("Error opening slp handle: Error #%i", err);
1224  return -1;
1225 }
1226
1227 if (!unreg) {
1228
1229  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
1230   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
1231   SLPClose(hslp);
1232   return -1;
1233  }
1234
1235  if ( SLPEscape(g_config->description, &description, SLP_FALSE) != SLP_OK ) {
1236   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
1237   SLPClose(hslp);
1238   return -1;
1239  }
1240
1241  standards = stds_string();
1242
1243  if ( standards == NULL ) {
1244   if ( (standards = roar_mm_malloc(1)) == NULL ) {
1245    SLPClose(hslp);
1246    return -1;
1247   }
1248   standards[0] = 0;
1249  }
1250
1251  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
1252#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1253                               "(light-channels=%i),"
1254#endif
1255                               "(standards=%s),"
1256                               "(location=%s),(description=%s)",
1257           g_sa->rate, g_sa->channels, g_sa->bits,
1258#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1259           g_light_state.channels,
1260#endif
1261           standards,
1262           location, description
1263          );
1264
1265  roar_mm_free(standards);
1266
1267  /* Register a service with SLP */
1268  err = SLPReg(hslp,
1269               addr,
1270               SLP_LIFETIME_MAXIMUM,
1271               0,
1272               attr,
1273               SLP_TRUE,
1274               register_slp_callback,
1275               &callbackerr);
1276  regged = 1;
1277 } else if ( unreg && regged ) {
1278  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
1279  regged = 0;
1280 } else {
1281  SLPClose(hslp);
1282  return -1;
1283 }
1284
1285 /* err may contain an error code that occurred as the slp library    */
1286 /* _prepared_ to make the call.                                     */
1287 if ( err != SLP_OK ) {
1288  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
1289  return -1;
1290 }
1291
1292 /* callbackerr may contain an error code (that was assigned through */
1293 /* the callback cookie) that occurred as slp packets were sent on    */
1294 /* the wire */
1295 if (callbackerr != SLP_OK) {
1296  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
1297  return -1;
1298 }
1299
1300 SLPClose(hslp);
1301 return 0;
1302#else
1303 return -1;
1304#endif
1305}
1306
1307static int auth_setup (enum roard_client_acclev none_acclev,
1308                       enum roard_client_acclev trust_acclev, int trust_root, uid_t trust_uid, gid_t trust_gid) {
1309 union auth_typeunion * key;
1310 size_t i;
1311
1312 // Info:
1313 // if a authlevel is <= ACCLEV_IDENTED we do not need to add this to the keyring
1314 // as ACCLEV_IDENTED is already gained if IDENTIFY call was done correctly.
1315
1316 if ( !(none_acclev <= ACCLEV_IDENTED) ) {
1317  if ( auth_addkey_anonymous(none_acclev) == -1 )
1318   return -1;
1319 }
1320
1321 if ( !(trust_acclev <= ACCLEV_IDENTED) ) {
1322  if ( (key = auth_regkey_simple(ROAR_AUTH_T_TRUST, trust_acclev)) == NULL )
1323   return -1;
1324
1325  // zerosize all counters.
1326  memset(key, 0, sizeof(union auth_typeunion));
1327
1328  i = 0;
1329
1330  if ( trust_uid != -1 )
1331   key->trust.uids[i++] = trust_uid;
1332
1333#ifdef ROAR_ROOT_UID
1334  if ( trust_root )
1335   key->trust.uids[i++] = ROAR_ROOT_UID;
1336#endif
1337
1338  key->trust.uids_len = i;
1339
1340  i = 0;
1341
1342  if ( trust_gid != -1 )
1343   key->trust.gids[i++] = trust_gid;
1344
1345  key->trust.gids_len = i;
1346 }
1347
1348 return 0;
1349}
1350
1351
1352// MAIN:
1353
1354#define _CKHAVEARGS(x) if ( (i + (x)) >= argc ) { \
1355                        ROAR_ERR("Option %s requires more arguments. See --help for more details.", k); \
1356                        return 70; \
1357                       }
1358
1359#ifdef ROAR_HAVE_MAIN_ARGS
1360int main (int argc, char * argv[]) {
1361#else
1362int main (void) {
1363#endif
1364#ifdef ROAR_HAVE_MAIN_ARGS
1365 enum action action = START;
1366 int i;
1367 char * k;
1368 enum output_format print_format = FORMAT_NATIVE;
1369 const struct rolestack * rolestack;
1370#endif
1371#if defined(ROAR_SUPPORT_LISTEN) && defined(ROAR_HAVE_GETUID)
1372 char user_sock[80]  = {0};
1373#endif
1374 struct roar_audio_info sa, max_sa;
1375 struct roard_config config;
1376#if defined(ROAR_HAVE_FORK) || defined(ROAR_TARGET_WIN32)
1377 int    daemon       = 0;
1378#endif
1379 int    realtime     = 0;
1380 int    sysclocksync = 0;
1381// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
1382#ifdef ROAR_SUPPORT_LISTEN
1383 int    port       = ROAR_DEFAULT_PORT;
1384 char * sock_addr  = NULL;
1385 int    sock_proto = ROAR_PROTO_ROARAUDIO;
1386 int    sock_dir   = -1;
1387 struct roar_audio_info sock_info = {0, 0, 0, 0};
1388#endif
1389#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1390 char * s_drv     = NULL;
1391 char * s_dev     = NULL;
1392 char * s_con     = NULL;
1393 char * s_opt     = NULL;
1394 int    s_prim    = 0;
1395#endif
1396 char * o_drv     = getenv("ROAR_DRIVER");
1397 char * o_dev     = getenv("ROAR_DEVICE");
1398 char * o_opts    = NULL;
1399 int    o_prim    = 0;
1400 int    o_count   = 0;
1401#ifndef ROAR_WITHOUT_DCOMP_MIXER
1402 char * m_drv     = NULL;
1403 char * m_dev     = NULL;
1404 char * m_opts    = NULL;
1405 int    m_prim    = 0;
1406 int    m_count   = 0;
1407#endif
1408 enum roard_client_acclev none_acclev  = ACCLEV_ALL;
1409 enum roard_client_acclev trust_acclev = ACCLEV_ALL;
1410 int                      trust_root   = 1;
1411 char * af_file                        = NULL;
1412 char * af_type                        = NULL;
1413 enum af_mode af_mode                  = AF_MODE_NONE;
1414 enum roard_client_acclev af_acclev    = ACCLEV_ALL;
1415#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1416 uint32_t light_channels = LIGHT_CHANNELS_DEFAULT;
1417#endif
1418#ifdef ROAR_DEFAULT_SOCKGRP
1419 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
1420#else
1421 char * sock_grp  = NULL;
1422#endif
1423 char * sock_user = NULL;
1424#ifdef ROAR_SUPPORT_LISTEN
1425 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
1426#endif
1427#ifdef ROAR_HAVE_LIBSLP
1428 int    reg_slp   = 0;
1429#endif
1430#ifdef ROAR_HAVE_LIBX11
1431 int    reg_x11   = 0;
1432#endif
1433#ifdef ROAR_HAVE_CHROOT
1434 char * chrootdir = NULL;
1435#endif
1436#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
1437 struct group   * grp  = NULL;
1438#endif
1439#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1440 struct passwd  * pwd  = NULL;
1441#endif
1442#ifdef ROAR_HAVE_GETSERVBYNAME
1443 struct servent * serv = NULL;
1444#endif
1445 struct roar_client * self = NULL;
1446#ifdef ROAR_HAVE_LIBDNET
1447 char decnethost[80];
1448#endif
1449#ifdef SUPPORT_PIDFILE
1450 struct roar_vio_calls pidfile_vio;
1451#endif
1452
1453 ROAR_DBG("main(*): starting roard...");
1454
1455 g_standby       =  0;
1456 g_autostandby   =  0;
1457 alive           =  1;
1458#ifdef ROAR_SUPPORT_LISTEN
1459 g_no_listen     =  0;
1460#else
1461 g_terminate     =  1;
1462#endif
1463
1464 g_verbose       = ROAR_DBG_INFO_NONE;
1465
1466 _LIBROAR_IGNORE_RET(roar_profile2info(&sa, "default-server"));
1467
1468 g_sa        = &sa;
1469 g_max_sa    = &max_sa;
1470
1471 memcpy(g_max_sa, g_sa, sizeof(max_sa));
1472
1473 counters_init();
1474
1475 g_config = &config;
1476
1477 if ( init_config() == -1 ) {
1478  ROAR_ERR("Can not init default config!");
1479  return 1;
1480 }
1481
1482 // load config
1483 _LIBROAR_IGNORE_RET(roar_libroar_get_config());
1484
1485 // init notify core:
1486 // TODO: reconsider number of lists.
1487 if ( roar_notify_core_new_global(-1) == -1 ) {
1488  ROAR_ERR("Can not init notify core!");
1489  return 1;
1490 }
1491
1492 if ( roar_notify_core_register_proxy(NULL, roar_notify_proxy_std, NULL) == -1 ) {
1493  ROAR_ERR("Can not init notify core!");
1494  return 1;
1495 }
1496
1497#ifdef DEBUG
1498 // enable early in case we have DEBUG set.
1499 dbg_notify_cb_register();
1500#endif
1501
1502 rolestack_init();
1503
1504 if ( auth_init() == -1 ) {
1505  ROAR_ERR("Can not init auth subsystem!");
1506  return 1;
1507 }
1508
1509#ifdef ROAR_SUPPORT_LISTEN
1510 if ( init_listening() == -1 ) {
1511  ROAR_ERR("Can not init listening sockets!");
1512  return 1;
1513 }
1514#endif
1515
1516#ifndef ROAR_WITHOUT_DCOMP_MIDI
1517 if ( midi_init_config() == -1 ) {
1518  ROAR_ERR("Can not init MIDI config!");
1519  return 1;
1520 }
1521#endif
1522
1523#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1524 if ( ssynth_init_config() == -1 ) {
1525  ROAR_ERR("Can not init ssynth config!");
1526  return 1;
1527 }
1528#endif
1529
1530#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1531 if ( rdtcs_init_config() == -1 ) {
1532  ROAR_ERR("Can not init RDTCS config!");
1533  return 1;
1534 }
1535#endif
1536
1537 if ( plugins_preinit() == -1 ) {
1538  ROAR_ERR("Can not pre-init plugins!");
1539  return 1;
1540 }
1541
1542#ifdef ROAR_SUPPORT_LISTEN
1543#ifndef ROAR_TARGET_WIN32
1544 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
1545#else
1546 sock_addr = ROAR_DEFAULT_HOST;
1547#endif
1548
1549#ifdef ROAR_HAVE_GETUID
1550 if ( getuid() != 0 && getenv("HOME") != NULL ) {
1551/*
1552  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
1553*/
1554  roar_env_render_path_r(user_sock, sizeof(user_sock), "~/" ROAR_DEFAULT_SOCK_USER);
1555  sock_addr = user_sock;
1556  ROAR_DBG("main(*): setting sock_addr='%s'", sock_addr);
1557 }
1558#endif
1559
1560 if ( getenv("ROAR_SERVER") != NULL )
1561  sock_addr = getenv("ROAR_SERVER");
1562#endif
1563
1564 if ( clients_init() == -1 ) {
1565  ROAR_ERR("Can not init clients!");
1566  return 1;
1567 }
1568
1569 if ( streams_init() == -1 ) {
1570  ROAR_ERR("Can not init streams!");
1571  return 1;
1572 }
1573
1574 if ( (g_self_client = clients_new()) == -1 ) {
1575  ROAR_ERR("Can not create self client!");
1576  return 1;
1577 }
1578
1579#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1580 if ( sources_init() == -1 ) {
1581  ROAR_ERR("Can not init sources!");
1582  return 1;
1583 }
1584
1585 if ( (sources_set_client(g_self_client)) == -1 ) {
1586  ROAR_ERR("Can not init set source client!");
1587  return 1;
1588 }
1589#endif
1590
1591#ifdef ROAR_HAVE_MAIN_ARGS
1592 for (i = 1; i < argc; i++) {
1593  k = argv[i];
1594
1595  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
1596   usage();
1597   return 0;
1598
1599  } else if ( strcmp(k, "--start") == 0 ) {
1600   // this is a no op
1601   action = START;
1602  } else if ( strcmp(k, "--restart") == 0 ) {
1603   action = RESTART;
1604#ifdef ROAR_SUPPORT_LISTEN
1605   if ( restart_server(sock_addr, 1) == -1 ) {
1606    ROAR_WARN("Can not terminate old server (not running at %s?), will retry later.", sock_addr);
1607    action = RESTART_RETRY;
1608   }
1609#else
1610   ROAR_ERR("--restart not supported");
1611#endif
1612  } else if ( strcmp(k, "--shutdown") == 0 ) {
1613   action = SHUTDOWN;
1614  } else if ( strcmp(k, "--stop") == 0 ) {
1615   action = STOP;
1616
1617
1618  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
1619#if defined(ROAR_HAVE_FORK) || defined(ROAR_TARGET_WIN32)
1620   daemon = 1;
1621#else
1622   ROAR_ERR("--daemon not supported");
1623#endif
1624  } else if ( strcmp(k, "--verbose") == 0 ) {
1625   g_verbose++;
1626
1627  } else if ( strcmp(k, "--print-format") == 0 ) {
1628   k = argv[++i];
1629   if ( !strcasecmp(k, "native") ) {
1630    print_format = FORMAT_NATIVE;
1631   } else if ( !strcasecmp(k, "wiki") ) {
1632    print_format = FORMAT_WIKI;
1633   } else if ( !strcasecmp(k, "csv") ) {
1634    print_format = FORMAT_CSV;
1635   } else {
1636    ROAR_WARN("Unknown print format: %s", k);
1637   }
1638
1639  } else if ( strcmp(k, "--print-format") == 0 ) {
1640   _CKHAVEARGS(1);
1641
1642  } else if ( strcmp(k, "--terminate") == 0 ) {
1643   g_terminate = 1;
1644  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1645   sysclocksync = 1000;
1646  } else if ( strcmp(k, "--realtime") == 0 ) {
1647   realtime++;
1648  } else if ( strcmp(k, "--memlock") == 0 ) {
1649   _CKHAVEARGS(1);
1650   g_config->memlock_level = memlock_str2level(argv[++i]);
1651  } else if ( strcmp(k, "--chroot") == 0 ) {
1652   _CKHAVEARGS(1);
1653#ifdef ROAR_HAVE_CHROOT
1654   chrootdir = argv[++i];
1655#else
1656   ROAR_ERR("--chroot not supported");
1657   i++;
1658#endif
1659  } else if ( strcmp(k, "--setgid") == 0 ) {
1660#ifdef ROAR_HAVE_SETGID
1661   setids |= R_SETGID;
1662#else
1663   ROAR_ERR("--setgid not supported");
1664#endif
1665  } else if ( strcmp(k, "--setuid") == 0 ) {
1666#ifdef ROAR_HAVE_SETUID
1667   setids |= R_SETUID;
1668#else
1669   ROAR_ERR("--setuid not supported");
1670#endif
1671  } else if ( strcmp(k, "--location") == 0 ) {
1672   _CKHAVEARGS(1);
1673   g_config->location = argv[++i];
1674  } else if ( strcmp(k, "--description") == 0 ) {
1675   _CKHAVEARGS(1);
1676   g_config->description = argv[++i];
1677  } else if ( strcmp(k, "--pidfile") == 0 ) {
1678   _CKHAVEARGS(1);
1679#ifdef SUPPORT_PIDFILE
1680   pidfile = argv[++i];
1681#else
1682   ROAR_ERR("--pidfile not supported");
1683   i++;
1684#endif
1685  } else if ( strcmp(k, "--log-syslog") == 0 ) {
1686#ifdef ROAR_HAVE_SYSLOG
1687   roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
1688#else
1689   ROAR_ERR("--log-syslog not supported");
1690#endif
1691  } else if ( strcmp(k, "--script-postdown") == 0 ) {
1692   _CKHAVEARGS(1);
1693#ifdef ROAR_HAVE_SYSTEM
1694   g_config->scripts.post_shutdown = argv[++i];
1695#else
1696   ROAR_ERR("--script-postdown not supported");
1697   i++;
1698#endif
1699
1700
1701  } else if ( strcmp(k, "--plugin-load") == 0 ) {
1702   _CKHAVEARGS(1);
1703   if ( plugins_load(argv[++i]) == -1 ) {
1704    ROAR_ERR("Can not load plugin");
1705   }
1706
1707  } else if ( strcmp(k, "--guest-acclev") == 0 ) {
1708   _CKHAVEARGS(1);
1709   none_acclev = clients_str2acclev(argv[++i]);
1710   if ( none_acclev == -1 ) {
1711    ROAR_ERR("Invalid access level: %s", argv[i]);
1712    return 1;
1713   }
1714  } else if ( strcmp(k, "--trust-acclev") == 0 ) {
1715   _CKHAVEARGS(1);
1716   trust_acclev = clients_str2acclev(argv[++i]);
1717   if ( trust_acclev == -1 ) {
1718    ROAR_ERR("Invalid access level: %s", argv[i]);
1719    return 1;
1720   }
1721  } else if ( strcmp(k, "--trust-root") == 0 ) {
1722   trust_root = 1;
1723  } else if ( strcmp(k, "--no-trust-root") == 0 ) {
1724   trust_root = 0;
1725
1726  } else if ( strcmp(k, "--authfile-gen") == 0 ) {
1727   _CKHAVEARGS(1);
1728   af_file = argv[++i];
1729   af_mode = AF_MODE_GEN;
1730  } else if ( strcmp(k, "--authfile-load") == 0 ) {
1731   _CKHAVEARGS(1);
1732   af_file = argv[++i];
1733   af_mode = AF_MODE_LOAD;
1734  } else if ( strcmp(k, "--authfile-type") == 0 ) {
1735   _CKHAVEARGS(1);
1736   af_type = argv[++i];
1737  } else if ( strcmp(k, "--authfile-acclev") == 0 ) {
1738   _CKHAVEARGS(1);
1739   af_acclev = clients_str2acclev(argv[++i]);
1740  } else if ( strcmp(k, "--new-authfile") == 0 ) {
1741   if ( af_mode != AF_MODE_NONE ) {
1742    if ( add_authfile(af_file, af_type, af_mode, af_acclev) == -1 ) {
1743     ROAR_ERR("main(*): adding authfile '%s' failed!", af_file);
1744    }
1745   }
1746   af_file   = NULL;
1747   af_type   = NULL;
1748   af_mode   = AF_MODE_NONE;
1749   af_acclev = ACCLEV_ALL;
1750
1751  } else if ( strcmp(k, "--list-cf") == 0 ) {
1752   print_codecfilterlist();
1753   return 0;
1754
1755  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
1756   _CKHAVEARGS(1);
1757   sa.rate = atoi(argv[++i]);
1758  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
1759   _CKHAVEARGS(1);
1760   sa.bits = atoi(argv[++i]);
1761  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
1762   _CKHAVEARGS(1);
1763   sa.channels = atoi(argv[++i]);
1764
1765  } else if ( strcmp(k, "--aiprofile") == 0 ) {
1766   _CKHAVEARGS(1);
1767   if ( roar_profile2info(&sa, argv[++i]) == -1 ) {
1768    ROAR_ERR("Unknown audio profile: %s", argv[i]);
1769    return 1;
1770   }
1771   sa.codec    = ROAR_CODEC_DEFAULT;
1772
1773  } else if ( strcmp(k, "--stream-flags") == 0 ) {
1774   _CKHAVEARGS(1);
1775   if ( update_stream_flags(argv[++i]) == -1 ) {
1776    ROAR_ERR("Can not set stream flags");
1777    return 1;
1778   }
1779
1780  } else if ( strcmp(k, "--list-rolestack") == 0 ) {
1781   print_rolestack();
1782   return 0;
1783  } else if ( strcmp(k, "--rolestack-push") == 0 ) {
1784   _CKHAVEARGS(1);
1785  if ( (rolestack = rolestack_parse(argv[++i])) == NULL ) {
1786   ROAR_ERR("Can not parse rolestack request: %s", roar_error2str(roar_error));
1787  } else {
1788   if ( rolestack_push(rolestack) == -1 ) {
1789    ROAR_ERR("Can not push request to rolestack: %s", roar_error2str(roar_error));
1790   }
1791  }
1792
1793  } else if ( strcmp(k, "--list-aiprofiles") == 0 ) {
1794   list_aiprofiles();
1795   return 0;
1796
1797  } else if ( strcmp(k, "--list-driver") == 0 ) {
1798   print_driverlist(print_format);
1799   return 0;
1800
1801  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
1802   _CKHAVEARGS(1);
1803   o_drv  = argv[++i];
1804  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
1805   _CKHAVEARGS(1);
1806   o_dev  = argv[++i];
1807  } else if ( strcmp(k, "-oO") == 0 ) {
1808   _CKHAVEARGS(1);
1809   o_opts = argv[++i];
1810  } else if ( strcmp(k, "-oP") == 0 ) {
1811   o_prim = 1;
1812  } else if ( strcmp(k, "-oN") == 0 ) {
1813   if ( action == START || action == RESTART ) {
1814    if ( output_add(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1815     o_count++;
1816
1817    o_drv  = o_dev = o_opts = NULL;
1818    o_prim = 0;
1819   }
1820
1821  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
1822   _CKHAVEARGS(1);
1823#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1824   s_drv = argv[++i];
1825#else
1826   ROAR_ERR("main(*): No support for sources compiled in");
1827   i++;
1828#endif
1829  } else if ( strcmp(k, "-S") == 0 ) {
1830   _CKHAVEARGS(1);
1831#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1832   s_dev = argv[++i];
1833#else
1834   ROAR_ERR("main(*): No support for sources compiled in");
1835   i++;
1836#endif
1837  } else if ( strcmp(k, "-sO") == 0 ) {
1838   _CKHAVEARGS(1);
1839#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1840   s_opt = argv[++i];
1841#else
1842   ROAR_ERR("main(*): No support for sources compiled in");
1843   i++;
1844#endif
1845  } else if ( strcmp(k, "-sC") == 0 ) {
1846   _CKHAVEARGS(1);
1847#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1848   s_con = argv[++i];
1849#else
1850   ROAR_ERR("main(*): No support for sources compiled in");
1851   i++;
1852#endif
1853  } else if ( strcmp(k, "-sP") == 0 ) {
1854#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1855   s_prim = 1;
1856#else
1857   ROAR_ERR("main(*): No support for sources compiled in");
1858#endif
1859  } else if ( strcmp(k, "-sN") == 0 ) {
1860#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1861   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1862    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1863   }
1864   s_opt = s_dev = s_con = NULL;
1865   s_drv = NULL;
1866   s_prim = 0;
1867#else
1868   ROAR_ERR("main(*): No support for sources compiled in");
1869#endif
1870  } else if ( strcmp(k, "--list-sources") == 0 ) {
1871#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1872   print_sourcelist();
1873   return 0;
1874#else
1875   ROAR_ERR("main(*): No support for sources compiled in");
1876   return 1;
1877#endif
1878
1879  } else if ( strcmp(k, "-m") == 0 || strcmp(k, "--mixer") == 0 ) {
1880   _CKHAVEARGS(1);
1881#ifndef ROAR_WITHOUT_DCOMP_MIXER
1882   m_drv  = argv[++i];
1883#else
1884   ROAR_ERR("main(*): No support for mixer compiled in");
1885   return 1;
1886#endif
1887  } else if ( strcmp(k, "-M") == 0 ) {
1888   _CKHAVEARGS(1);
1889#ifndef ROAR_WITHOUT_DCOMP_MIXER
1890   m_dev  = argv[++i];
1891#else
1892   ROAR_ERR("main(*): No support for mixer compiled in");
1893   return 1;
1894#endif
1895  } else if ( strcmp(k, "-mO") == 0 ) {
1896   _CKHAVEARGS(1);
1897#ifndef ROAR_WITHOUT_DCOMP_MIXER
1898   m_opts = argv[++i];
1899#else
1900   ROAR_ERR("main(*): No support for mixer compiled in");
1901   return 1;
1902#endif
1903  } else if ( strcmp(k, "-mP") == 0 ) {
1904#ifndef ROAR_WITHOUT_DCOMP_MIXER
1905   m_prim = 1;
1906#else
1907   ROAR_ERR("main(*): No support for mixer compiled in");
1908   return 1;
1909#endif
1910  } else if ( strcmp(k, "-mN") == 0 ) {
1911#ifndef ROAR_WITHOUT_DCOMP_MIXER
1912   if ( hwmixer_add(m_drv, m_dev, m_opts, m_prim, m_count) != -1 )
1913    m_count++;
1914
1915   m_drv  = o_dev = o_opts = NULL;
1916   m_prim = 0;
1917#else
1918   ROAR_ERR("main(*): No support for mixer compiled in");
1919   return 1;
1920#endif
1921  } else if ( strcmp(k, "--list-mixers") == 0 ) {
1922#ifndef ROAR_WITHOUT_DCOMP_MIXER
1923   print_hwmixerlist();
1924   return 0;
1925#else
1926   ROAR_ERR("main(*): No support for mixer compiled in");
1927   return 1;
1928#endif
1929
1930  } else if ( strcmp(k, "--light-channels") == 0 ) {
1931   _CKHAVEARGS(1);
1932#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1933   light_channels = atoi(argv[++i]);
1934#else
1935   ROAR_WARN("main(*): no light subsystem compiled in");
1936   i++;
1937#endif
1938
1939  } else if ( strcmp(k, "--rds-pi") == 0 ) {
1940   _CKHAVEARGS(1);
1941#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1942   g_rdtcs.rds.pi = atoi(argv[++i]);
1943#else
1944   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1945   i++;
1946#endif
1947  } else if ( strcmp(k, "--rds-ps") == 0 ) {
1948   _CKHAVEARGS(1);
1949#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1950   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
1951    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]);
1952    return 1;
1953   }
1954#else
1955   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1956   i++;
1957#endif
1958  } else if ( strcmp(k, "--rds-pty") == 0 ) {
1959   _CKHAVEARGS(1);
1960#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1961   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
1962    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
1963    return 1;
1964   }
1965#else
1966   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1967   i++;
1968#endif
1969  } else if ( strcmp(k, "--rds-tp") == 0 ) {
1970#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1971   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) {
1972    ROAR_ERR("Can not set RDS TP flag");
1973    return 1;
1974   }
1975#else
1976   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1977#endif
1978  } else if ( strcmp(k, "--rds-ct") == 0 ) {
1979#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1980   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) {
1981    ROAR_ERR("Can not set RDS CT flag");
1982    return 1;
1983   }
1984#else
1985   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1986#endif
1987
1988
1989  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
1990#ifndef ROAR_WITHOUT_DCOMP_CB
1991   midi_config.init_cb = 0;
1992#else
1993   // no warning here as this is the disable option
1994#endif
1995  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
1996#ifndef ROAR_WITHOUT_DCOMP_CB
1997   midi_config.init_cb = 1;
1998#else
1999   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
2000#endif
2001  } else if ( strcmp(k, "--midi-console") == 0 ) {
2002   _CKHAVEARGS(1);
2003#ifndef ROAR_WITHOUT_DCOMP_CB
2004   midi_config.console_dev = argv[++i];
2005   midi_config.init_cb = 1;
2006#else
2007   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
2008   i++;
2009#endif
2010
2011  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
2012#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2013   ssynth_conf.enable = 1;
2014#else
2015   ROAR_ERR("main(*): No support for ssynth compiled in");
2016#endif
2017  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
2018#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2019   ssynth_conf.enable = 0;
2020#else
2021   // we can safely ignore the disable
2022#endif
2023
2024  } else if ( strcmp(k, "--x11-display") == 0 || strcmp(k, "--display") == 0 ) {
2025   _CKHAVEARGS(1);
2026#ifdef ROAR_HAVE_LIBX11
2027   x11display = argv[++i];
2028#else
2029   ROAR_ERR("No X11 support compiled in!");
2030   return 1;
2031#endif
2032
2033
2034  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
2035   _CKHAVEARGS(1);
2036   // This is only useful in INET not UNIX mode.
2037#ifdef ROAR_SUPPORT_LISTEN
2038   if ( *sock_addr == '/' )
2039    sock_addr = ROAR_DEFAULT_HOST;
2040
2041   errno = 0;
2042   if ( (port = atoi(argv[++i])) < 1 ) {
2043#ifdef ROAR_HAVE_GETSERVBYNAME
2044    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
2045     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
2046     return 1;
2047    }
2048    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
2049    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
2050            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
2051    port = ROAR_NET2HOST16(serv->s_port);
2052#else
2053    ROAR_ERR("invalite port number: %s", argv[i]);
2054    return 1;
2055#endif
2056   }
2057#endif
2058  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
2059   _CKHAVEARGS(1);
2060#ifdef ROAR_SUPPORT_LISTEN
2061   sock_addr = argv[++i];
2062#else
2063   i++;
2064#endif
2065
2066  } else if ( strcmp(k, "--proto") == 0 ) {
2067#ifdef ROAR_SUPPORT_LISTEN
2068   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
2069    ROAR_ERR("Unknown protocol: %s", argv[i]);
2070    return 1;
2071   }
2072#endif
2073  } else if ( strcmp(k, "--proto-dir") == 0 ) {
2074   _CKHAVEARGS(1);
2075#ifdef ROAR_SUPPORT_LISTEN
2076   if ( (sock_dir = roar_str2dir(argv[++i])) == -1 ) {
2077    ROAR_ERR("Unknown stream direction: %s", argv[i]);
2078    return 1;
2079   }
2080#else
2081   i++;
2082#endif
2083  } else if ( strcmp(k, "--proto-rate") == 0 ) {
2084   _CKHAVEARGS(1);
2085#ifdef ROAR_SUPPORT_LISTEN
2086   sock_info.rate = atoi(argv[++i]);
2087#else
2088   i++;
2089#endif
2090  } else if ( strcmp(k, "--proto-bits") == 0 ) {
2091   _CKHAVEARGS(1);
2092#ifdef ROAR_SUPPORT_LISTEN
2093   sock_info.bits = atoi(argv[++i]);
2094#else
2095   i++;
2096#endif
2097  } else if ( strcmp(k, "--proto-chans") == 0 ) {
2098   _CKHAVEARGS(1);
2099#ifdef ROAR_SUPPORT_LISTEN
2100   sock_info.channels = atoi(argv[++i]);
2101#else
2102   i++;
2103#endif
2104  } else if ( strcmp(k, "--proto-codec") == 0 ) {
2105   _CKHAVEARGS(1);
2106#ifdef ROAR_SUPPORT_LISTEN
2107   if ( (sock_info.codec = roar_str2codec(argv[++i])) == -1 ) {
2108    ROAR_ERR("Unknown codec: %s", argv[i]);
2109    return 1;
2110   }
2111#else
2112   i++;
2113#endif
2114  } else if ( strcmp(k, "--proto-aiprofile") == 0 ) {
2115   _CKHAVEARGS(1);
2116#ifdef ROAR_SUPPORT_LISTEN
2117   if ( roar_profile2info(&sock_info, argv[++i]) == -1 ) {
2118    ROAR_ERR("Unknown audio profile: %s", argv[i]);
2119    return 1;
2120   }
2121#else
2122   i++;
2123#endif
2124  } else if ( strcmp(k, "--list-profiles") == 0 ) {
2125#ifdef ROAR_SUPPORT_LISTEN
2126   listen_listen_profiles();
2127   return 0;
2128#endif
2129  } else if ( strcmp(k, "--proto-profile") == 0 ) {
2130   _CKHAVEARGS(1);
2131#ifdef ROAR_SUPPORT_LISTEN
2132   if ( get_listen_profile(argv[++i], &port, &sock_addr, &sock_type, &sock_proto, &sock_dir, &sock_info) == -1 ) {
2133    ROAR_ERR("Unknown listen profile: %s", argv[i]);
2134    return 1;
2135   }
2136#else
2137   i++;
2138#endif
2139
2140
2141  } else if ( strcmp(k, "--list-proto") == 0 ) {
2142   list_proto();
2143   return 0;
2144
2145  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
2146#ifdef ROAR_SUPPORT_LISTEN
2147   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
2148    sock_type = ROAR_SOCKET_TYPE_TCP;
2149
2150   if ( *sock_addr == '/' )
2151    sock_addr = ROAR_DEFAULT_HOST;
2152#endif
2153
2154  } else if ( strcmp(k, "-4") == 0 ) {
2155#ifdef ROAR_SUPPORT_LISTEN
2156   sock_type = ROAR_SOCKET_TYPE_TCP;
2157   if ( *sock_addr == '/' )
2158    sock_addr = ROAR_DEFAULT_HOST;
2159#endif
2160  } else if ( strcmp(k, "-6") == 0 ) {
2161#ifdef ROAR_SUPPORT_LISTEN
2162#ifdef AF_INET6
2163   sock_type = ROAR_SOCKET_TYPE_TCP6;
2164   if ( *sock_addr == '/' )
2165    sock_addr = ROAR_DEFAULT_HOST;
2166#else
2167    ROAR_ERR("No IPv6 support compiled in!");
2168    return 1;
2169#endif
2170#endif
2171
2172  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
2173#ifdef ROAR_SUPPORT_LISTEN
2174   // ignore this case as it is the default behavor.
2175   sock_type = ROAR_SOCKET_TYPE_UNIX;
2176#endif
2177
2178  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
2179#ifdef ROAR_SUPPORT_LISTEN
2180#ifdef ROAR_HAVE_LIBDNET
2181    port   = ROAR_DEFAULT_NUM;
2182    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
2183    sock_addr = decnethost;
2184    sock_type = ROAR_SOCKET_TYPE_DECNET;
2185#else
2186    ROAR_ERR("No DECnet support compiled in!");
2187    return 1;
2188#endif
2189#endif
2190  } else if ( strcmp(k, "--new-sock") == 0 ) {
2191#ifdef ROAR_SUPPORT_LISTEN
2192   if ( action == START || action == RESTART ) {
2193    if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
2194     ROAR_ERR("Can not open listen socket!");
2195     return 1;
2196    }
2197   }
2198#endif
2199
2200  } else if ( strcmp(k, "--slp") == 0 ) {
2201#ifdef ROAR_HAVE_LIBSLP
2202   reg_slp = 1;
2203#else
2204   ROAR_ERR("No OpenSLP support compiled in!");
2205   return 1;
2206#endif
2207
2208  } else if ( strcmp(k, "--x11") == 0 ) {
2209#ifdef ROAR_HAVE_LIBX11
2210   reg_x11 = 1;
2211#else
2212   ROAR_ERR("No X11 support compiled in!");
2213   return 1;
2214#endif
2215
2216
2217  } else if ( strcmp(k, "--jumbo-mtu") == 0 ) {
2218   _CKHAVEARGS(1);
2219   g_config->jumbo_mtu = atoi(argv[++i]);
2220
2221  } else if ( strcmp(k, "-G") == 0 ) {
2222   _CKHAVEARGS(1);
2223   sock_grp  = argv[++i];
2224  } else if ( strcmp(k, "-U") == 0 ) {
2225   _CKHAVEARGS(1);
2226   sock_user = argv[++i];
2227
2228  } else if ( strcmp(k, "--no-listen") == 0 ) {
2229#ifdef ROAR_SUPPORT_LISTEN
2230   sock_addr   = "";
2231   g_terminate = 1;
2232   g_no_listen = 1;
2233#endif
2234  } else if ( strcmp(k, "--client-fh") == 0 ) {
2235   _CKHAVEARGS(1);
2236   if ( clients_new_from_fh(atoi(argv[++i]), ROAR_PROTO_ROARAUDIO, ROAR_BYTEORDER_NETWORK, 1) == -1 ) {
2237    ROAR_ERR("main(*): Can not set client's fh");
2238    return 1;
2239   }
2240  } else if ( strcmp(k, "--close-fh") == 0 ) {
2241   _CKHAVEARGS(1);
2242#ifdef ROAR_HAVE_IO_POSIX
2243   close(atoi(argv[++i]));
2244#else
2245   i++;
2246   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
2247#endif
2248
2249  } else if ( strcmp(k, "--standby") == 0 ) {
2250   g_standby = 1;
2251  } else if ( strcmp(k, "--auto-standby") == 0 ) {
2252   g_autostandby = 1;
2253  } else {
2254   usage();
2255   return 1;
2256  }
2257
2258 }
2259#endif
2260
2261#ifdef ROAR_HAVE_MAIN_ARGS
2262 switch (action) {
2263  case START:
2264    // NO-OP.
2265   break;
2266  case RESTART:
2267    // NO-OP, done before.
2268   break;
2269  case STOP:
2270#ifdef ROAR_SUPPORT_LISTEN
2271    if ( restart_server(sock_addr, 0) == -1 ) {
2272     ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
2273     return 1;
2274    }
2275    return 0;
2276#else
2277    ROAR_ERR("--stop not supported");
2278    return 1;
2279#endif
2280   break;
2281  case SHUTDOWN:
2282#ifdef ROAR_SUPPORT_LISTEN
2283    if ( restart_server(sock_addr, 1) == -1 ) {
2284     ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
2285     return 1;
2286    }
2287    return 0;
2288#else
2289    ROAR_ERR("--shutdown not supported");
2290    return 1;
2291#endif
2292   break;
2293  case RESTART_RETRY:
2294#ifdef ROAR_SUPPORT_LISTEN
2295    if ( restart_server(sock_addr, 1) == -1 ) {
2296     ROAR_WARN("Can not terminate old server (again) (not running at %s?), tring to continue anyway", sock_addr);
2297     action = RESTART;
2298    }
2299#endif
2300   break;
2301 }
2302#endif
2303
2304#ifndef DEBUG
2305 // notify dbg if requested, if in DEBUG mode
2306 // do not able because it got enabled early.
2307 if ( g_verbose >= 4 )
2308  dbg_notify_cb_register();
2309#endif
2310
2311 if ( af_mode != AF_MODE_NONE ) {
2312  if ( add_authfile(af_file, af_type, af_mode, af_acclev) == -1 ) {
2313   ROAR_ERR("main(*): adding authfile '%s' failed!", af_file);
2314  }
2315 }
2316
2317#ifndef ROAR_WITHOUT_DCOMP_MIXER
2318 if ( m_drv != NULL ) {
2319  if ( hwmixer_add(m_drv, m_dev, m_opts, m_prim, m_count) == -1 ) {
2320   ROAR_ERR("main(*): adding mixer '%s' via '%s' failed!", m_dev, m_drv);
2321  }
2322 }
2323#endif
2324
2325#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2326 if ( s_drv != NULL || s_dev != NULL ) {
2327  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
2328   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
2329  }
2330 }
2331#endif
2332
2333 if ( output_add_default(o_drv, o_dev, o_opts, o_prim, o_count) == -1 ) {
2334  ROAR_ERR("Can not initialize default driver");
2335  return 1;
2336 }
2337
2338 ROAR_INFO("Server config: rate=%i, bits=%i, chans=%i", ROAR_DBG_INFO_NOTICE, sa.rate, sa.bits, sa.channels);
2339
2340 if ( waveform_init() == -1 ) {
2341  ROAR_ERR("Can not initialize Waveform subsystem");
2342  return 1;
2343 }
2344
2345#ifndef ROAR_WITHOUT_DCOMP_MIDI
2346 if ( midi_init() == -1 ) {
2347  ROAR_ERR("Can not initialize MIDI subsystem");
2348 }
2349#endif
2350
2351#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2352 if ( ssynth_init() == -1 ) {
2353  ROAR_ERR("Can not initialize ssynth subsystem");
2354 }
2355#endif
2356
2357#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2358 if ( light_init(light_channels) == -1 ) {
2359  ROAR_ERR("Can not initialize light control subsystem");
2360 }
2361#endif
2362
2363#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2364 if ( rdtcs_init() == -1 ) {
2365  ROAR_ERR("Can not initialize RDTCS subsystem");
2366 }
2367#endif
2368
2369 if ( plugins_init() == -1 ) {
2370  ROAR_ERR("Can not initialize plugins");
2371 }
2372
2373#ifdef ROAR_SUPPORT_LISTEN
2374 if ( !g_no_listen ) {
2375  if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
2376   ROAR_ERR("Can not open listen socket!");
2377   return 1;
2378  }
2379 }
2380#endif
2381
2382 if ( output_buffer_init(&sa) == -1 ) {
2383  ROAR_ERR("Can not init output buffer!");
2384  return 1;
2385 }
2386
2387 if ( samples_init() == -1 ) {
2388  ROAR_ERR("Can not init samples!");
2389  return 1;
2390 }
2391
2392
2393 // we should handle this on microcontrollers, too.
2394#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
2395 signal(SIGINT,  on_sig_int);
2396 signal(SIGTERM, on_sig_term);
2397 signal(SIGCHLD, on_sig_chld);
2398 signal(SIGUSR1, on_sig_usr1);
2399 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
2400#endif
2401
2402 if ( g_config->memlock_level == -1 ) {
2403  g_config->memlock_level = MEMLOCK_DEFAULT;
2404 }
2405
2406 if ( memlock_set_level(g_config->memlock_level) == -1 ) {
2407  ROAR_WARN("Can not set memory locking level to target level.");
2408 }
2409
2410 if ( realtime ) {
2411#ifdef DEBUG
2412  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compile without -DDEBUG");
2413#endif
2414
2415#ifdef ROAR_HAVE_NICE
2416  // this stupid error check is because type of returned data of nice() changed
2417  // too often. On some systems it may return 0/-1, on some new nice value or
2418  // mixed forms of both.
2419  errno = 0;
2420  (void)nice(-5*realtime); // -5 for each --realtime
2421  if ( errno ) {
2422   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2423  }
2424#else
2425  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2426#endif
2427/*
2428#ifdef __linux__
2429  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
2430   ROAR_WARN("Can not set io priority: %s", strerror(errno));
2431#endif
2432*/
2433 }
2434
2435#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
2436 if ( setids & R_SETGID ) {
2437  if ( sock_grp == NULL ) {
2438   ROAR_ERR("Can not set GID if no groupname is supplied");
2439   return 1;
2440  }
2441  if ( (grp = getgrnam(sock_grp)) == NULL ) {
2442   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
2443   return 1;
2444  }
2445  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
2446   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
2447  }
2448  if ( grp == NULL || setgid(grp->gr_gid) == -1 ) {
2449   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
2450  }
2451 }
2452#endif
2453
2454
2455 clients_set_pid(g_self_client, getpid());
2456#ifdef ROAR_HAVE_GETUID
2457 clients_set_uid(g_self_client, getuid());
2458#endif
2459#ifdef ROAR_HAVE_GETGID
2460 clients_set_gid(g_self_client, getgid());
2461#endif
2462 clients_get(g_self_client, &self);
2463
2464 if ( self == NULL ) {
2465  ROAR_ERR("Can not get self client!");
2466  return 1;
2467 }
2468
2469 strcpy(self->name, "RoarAudio daemon internal");
2470
2471 if ( roar_nnode_free(&(self->nnode)) == -1 )
2472  return 1;
2473
2474 // not fully correct but ok as workaorund
2475 // so tools assume that roard runs on the same machine as
2476 // they in case they use AF_UNIX:
2477 if ( roar_nnode_new(&(self->nnode), ROAR_SOCKET_TYPE_UNIX) == -1 )
2478  return 1;
2479
2480#if defined(ROAR_HAVE_FORK) || defined(ROAR_TARGET_WIN32)
2481 if ( daemon ) {
2482#ifdef ROAR_HAVE_SYSLOG
2483  roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
2484#else
2485  roar_debug_set_stderr_fh(-1);
2486#endif
2487
2488#ifdef ROAR_TARGET_WIN32
2489  FreeConsole();
2490#else
2491
2492  close(ROAR_STDIN );
2493  close(ROAR_STDOUT);
2494  close(ROAR_STDERR);
2495
2496  if ( fork() )
2497   ROAR_U_EXIT(0);
2498
2499#ifdef ROAR_HAVE_SETSID
2500  setsid();
2501#endif
2502  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
2503#endif
2504 }
2505#endif
2506
2507#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2508 // early test for UID as we need this for the pidfile and the setuid()
2509 if ( sock_user != NULL ) {
2510  if ( (pwd = getpwnam(sock_user)) == NULL ) {
2511   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
2512   return 1;
2513  }
2514 }
2515#endif
2516
2517 ROAR_INFO("Process ID: %i", ROAR_DBG_INFO_INFO, (int)getpid());
2518
2519#ifdef SUPPORT_PIDFILE
2520 if ( pidfile != NULL ) {
2521  if ( roar_vio_open_dstr_simple(&pidfile_vio, pidfile, O_WRONLY|O_CREAT) == -1 ) {
2522   ROAR_ERR("Can not write pidfile: %s", pidfile);
2523  } else {
2524   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
2525   roar_vio_close(&pidfile_vio);
2526  }
2527#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2528  if ( pwd != NULL || grp != NULL ) {
2529   if ( chown(pidfile, pwd != NULL ? pwd->pw_uid : -1, grp != NULL ? grp->gr_gid : -1) == -1 ) {
2530    ROAR_WARN("Can not change ownership of pidfile: %s: %s", pidfile, strerror(errno));
2531   }
2532  }
2533  if ( chmod(pidfile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1 ) {
2534   ROAR_WARN("Can not change permissions of pidfile: %s: %s", pidfile, strerror(errno));
2535  }
2536#endif
2537 }
2538#endif
2539
2540#ifdef ROAR_HAVE_CHROOT
2541 if (chrootdir) {
2542  if ( chroot(chrootdir) == -1 ) {
2543   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
2544   return 2;
2545  }
2546  if ( chdir("/") == -1 ) {
2547   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
2548   return 2;
2549  }
2550 }
2551#endif
2552
2553#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2554 if ( setids & R_SETUID ) {
2555  if ( sock_user == NULL ) {
2556   ROAR_ERR("Can not set UID if no username is supplied");
2557   return 1;
2558  }
2559  if ( pwd == NULL || setuid(pwd->pw_uid) == -1 ) {
2560   ROAR_ERR("Can not set UserID: %s", strerror(errno));
2561   return 3;
2562  }
2563#ifdef ROAR_HAVE_GETUID
2564  clients_set_uid(g_self_client, getuid());
2565#endif
2566 }
2567#endif
2568
2569 // setup auth:
2570#if defined(ROAR_HAVE_GETUID) && defined(ROAR_HAVE_GETGID)
2571 if ( auth_setup(none_acclev, trust_acclev, trust_root, getuid(), getgid()) == -1 ) {
2572#else
2573 if ( auth_setup(none_acclev, trust_acclev, trust_root, -1, -1) == -1 ) {
2574#endif
2575  ROAR_ERR("Can not set up auth. Bad.");
2576  alive = 0;
2577 }
2578
2579 // Register with OpenSLP:
2580#ifdef ROAR_HAVE_LIBSLP
2581 if ( reg_slp ) {
2582  register_slp(0, sock_addr);
2583 }
2584#endif
2585
2586#ifdef ROAR_HAVE_LIBX11
2587 if ( reg_x11 ) {
2588  register_x11(0, sock_addr);
2589 }
2590#endif
2591
2592 // update sync counter.
2593 streams_change_sync_num(-1, 0);
2594
2595 ROAR_INFO("Startup complet", ROAR_DBG_INFO_INFO);
2596
2597 // start main loop...
2598 ROAR_INFO("Entering main loop", ROAR_DBG_INFO_INFO);
2599 main_loop(&sa, sysclocksync);
2600 ROAR_INFO("Left main loop", ROAR_DBG_INFO_INFO);
2601
2602 // clean up.
2603 clean_quit_prep();
2604 output_buffer_free();
2605
2606 roar_notify_core_free(NULL);
2607
2608 ROAR_INFO("Shuting down complete", ROAR_DBG_INFO_INFO);
2609
2610#ifdef ROAR_HAVE_SYSTEM
2611 if ( g_config->scripts.post_shutdown != NULL )
2612  system(g_config->scripts.post_shutdown);
2613#endif
2614
2615 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
2616 return 0;
2617}
2618
2619void cleanup_listen_socket (int terminate) {
2620 int i;
2621
2622 ROAR_DBG("cleanup_listen_socket(terminate=%i) = (void)?", terminate);
2623
2624 ROAR_INFO("Cleaning up listen sockets", ROAR_DBG_INFO_INFO);
2625
2626 // Deregister from SLP:
2627#ifdef ROAR_HAVE_LIBSLP
2628 register_slp(1, NULL);
2629#endif
2630
2631#ifdef ROAR_HAVE_LIBX11
2632 register_x11(1, NULL);
2633#endif
2634
2635#ifdef ROAR_SUPPORT_LISTEN
2636 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
2637  if ( g_listen[i].used  ) {
2638   roar_vio_close(&(g_listen[i].sock));
2639
2640   g_listen[i].used = 0;
2641
2642#ifdef ROAR_HAVE_UNIX
2643   if ( server[i] != NULL )
2644    if ( *(server[i]) == '/' )
2645     unlink(server[i]);
2646#endif
2647  }
2648 }
2649
2650#endif
2651
2652 if ( terminate )
2653  g_terminate = 1;
2654}
2655
2656void clean_quit_prep (void) {
2657 cleanup_listen_socket(0);
2658
2659 plugins_free();
2660
2661#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2662 sources_free();
2663#endif
2664 streams_free();
2665 clients_free();
2666#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2667 ssynth_free();
2668#endif
2669#ifndef ROAR_WITHOUT_DCOMP_CB
2670 midi_cb_stop(); // stop console beep
2671#endif
2672#ifndef ROAR_WITHOUT_DCOMP_MIDI
2673 midi_free();
2674#endif
2675#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2676 light_free();
2677#endif
2678#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2679 rdtcs_free();
2680#endif
2681
2682 waveform_free();
2683
2684#ifdef SUPPORT_PIDFILE
2685 if ( pidfile != NULL )
2686  unlink(pidfile);
2687#endif
2688
2689 auth_free();
2690}
2691
2692void clean_quit (void) {
2693 ROAR_INFO("Shuting down", ROAR_DBG_INFO_INFO);
2694
2695 counters_print(ROAR_DEBUG_TYPE_INFO, 0);
2696
2697 clean_quit_prep();
2698// output_buffer_free();
2699
2700 roar_notify_core_free(NULL);
2701
2702 ROAR_INFO("Shuting down complete", ROAR_DBG_INFO_INFO);
2703
2704#ifdef ROAR_HAVE_SYSTEM
2705 if ( g_config->scripts.post_shutdown != NULL )
2706  system(g_config->scripts.post_shutdown);
2707#endif
2708
2709 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
2710 exit(0);
2711}
2712
2713//ll
Note: See TracBrowser for help on using the repository browser.