source: roaraudio/roard/roard.c @ 5954:de6e7e247a3d

Last change on this file since 5954:de6e7e247a3d was 5954:de6e7e247a3d, checked in by phi, 10 years ago

added error message in case of unknown roard option

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