source: roaraudio/roard/roard.c @ 5350:1e8e415b77c6

Last change on this file since 5350:1e8e415b77c6 was 5350:1e8e415b77c6, checked in by phi, 12 years ago

nicer table formating for --list-aiprofiles

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