source: roaraudio/roard/roard.c @ 4739:605b086a1801

Last change on this file since 4739:605b086a1801 was 4739:605b086a1801, checked in by phi, 13 years ago

some work to avoid compiler warnings

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