source: roaraudio/roard/roard.c @ 4741:2bafcf09484a

Last change on this file since 4741:2bafcf09484a was 4741:2bafcf09484a, checked in by phi, 13 years ago

added error message for opening non existing driver, thanks to dirk|

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