source: roaraudio/roard/roard.c @ 3963:4c89fe99987b

Last change on this file since 3963:4c89fe99987b was 3963:4c89fe99987b, checked in by phi, 14 years ago

corrected spacing

File size: 56.1 KB
Line 
1//roard.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
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
28#ifdef ROAR_SUPPORT_LISTEN
29char * server[ROAR_MAX_LISTEN_SOCKETS];
30#endif
31
32#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_FS_POSIX)
33#define SUPPORT_PIDFILE
34char * pidfile = NULL;
35#endif
36
37#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
38int    setids    = 0;
39#endif
40
41#ifdef ROAR_HAVE_LIBX11
42char * x11display = NULL;
43#endif
44
45#ifdef ROAR_HAVE_MAIN_ARGS
46void usage (void) {
47 printf("Usage: roard [OPTIONS]...\n\n");
48
49 printf("Misc Options:\n\n");
50 printf(
51        " --daemon              - Bring the server into background after init\n"
52        " --verbose             - Be more verbose, can be used multiple times\n"
53        " --terminate           - Terminate after last client quited\n"
54        " --start               - No op parameter (starting roard is default operation)\n"
55        " --restart             - Trys to stop an old instance and start a new with new settings\n"
56        " --stop                - Stops a running roard (provide --pidfile!)\n"
57        " --shutdown            - Terminates a running roard (provide --pidfile!)\n"
58        " --realtime            - Trys to get realtime priority,\n"
59        "                         give multible times for being more realtime\n"
60        " --chroot DIR          - chroots to the given dir\n"
61        " --setgid              - GroupID to the audio group as specified via -G\n"
62        " --setuid              - UserID to the audio user as specified via -U\n"
63        " --sysclocksync        - calculate exact sample rate using the system clock\n"
64        " --location  LOC       - Set lion readable location of server\n"
65#ifdef SUPPORT_PIDFILE
66        " --pidfile PIDFILE     - Write a pidfile at PIDFILE\n"
67#endif
68#ifdef ROAR_HAVE_SYSLOG
69        " --log-syslog          - Log Warnings, Errors, ... to syslog\n"
70#endif
71       );
72
73 printf("\nPlugin Options:\n\n");
74 printf(
75        " --plugin-load FILE    - Load plugin FILE\n"
76       );
77
78 printf("\nAudio Options:\n\n");
79 printf(
80        " -R  --rate   RATE     - Set server rate\n"
81        " -B  --bits   BITS     - Set server bits\n"
82        " -C  --chans  CHANNELS - Set server channels\n"
83        " --aiprofile  PROFILE  - Use the given audio profile\n"
84       );
85
86 printf("\nStream Options:\n\n");
87 printf(
88        " --stream-flags D=F    - Set default flags for stream directions\n"
89        "                         D is the stream direction and F is a comma seperated\n"
90        "                         list of flags in form +flag or -flag to set or unset\n"
91        "                         a flag as default or remove it from the default\n"
92       );
93
94 printf("\nDriver Options: (obsolete, do not use, Use Ouput Options)\n\n");
95 printf(" -d  --driver DRV      - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT);
96 printf(" -D  --device DEV      - Set the device\n");
97 printf(" -dO OPTS              - Set output options\n");
98 printf(" --list-driver         - List all drivers\n");
99
100 printf("\nOutput Options:\n\n");
101 printf(" -o  --odriver DRV     - Set the driver, use '--list-driver' to get a list\n");
102 printf(" -O  --odevice DEV     - Set the device\n");
103 printf(" -oO OPTS              - Set output options\n");
104 printf(" -oN                   - Adds another output\n");
105 printf(" -oP                   - Mark output as primary\n");
106
107#ifndef ROAR_WITHOUT_DCOMP_SOURCES
108 printf("\nSource Options:\n\n");
109 printf(" -s  --source DRV      - Use DRV as input driver\n"
110        " -S           DEV      - Use DEV as input device\n"
111        " -sO          OPTS     - Use OPTS as input options\n"
112        " -sN                   - Adds another source\n"
113        " -sP                   - Make souce as primary\n"
114       );
115 printf(" --list-sources        - List all sources\n");
116#endif
117
118 printf("\nCodec Filter Options:\n\n");
119 printf(" --list-cf             - List all codec filter\n"
120       );
121
122#ifndef ROAR_WITHOUT_DCOMP_MIDI
123 printf("\nMIDI Options:\n\n");
124 printf(" --midi-no-console     - Disable console based MIDI synth\n"
125        " --midi-console-enable - Enables the console based MIDI synth\n"
126        " --midi-console DEV    - Set device for MIDI console\n"
127#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
128        " --ssynth-enable       - Enable simple software synth\n"
129        " --ssynth-disable      - Disable simple software synth\n"
130#endif
131       );
132#endif
133
134#ifndef ROAR_WITHOUT_DCOMP_LIGHT
135 printf("\nLight Control Options:\n\n");
136 printf(" --light-channels NUM  - Sets the number of channels for Light control (default: %i)\n",
137                                  LIGHT_CHANNELS_DEFAULT
138       );
139#endif
140
141#ifndef ROAR_WITHOUT_DCOMP_RDTCS
142 printf("\nRadio Data and Transmitter Control System Options:\n\n");
143 printf(" --rds-pi   PI         - Sets the RDS Programme Identification (PI)\n"
144        " --rds-ps   PS         - Sets the RDS Programme Service Name (PS)\n"
145        " --rds-pty  PTY        - Sets the RDS Programme Type (PTY)\n"
146        " --rds-tp              - Sets the RDS Traffic Programme (TP) flag\n"
147        " --rds-ct              - Enables sending of RDS Clock Time (CT)\n"
148       );
149#endif
150
151#ifdef ROAR_HAVE_LIBX11
152 printf("\nX11 Options:\n\n");
153 printf(
154        " --x11-display DISPLAY - Set display for X11\n"
155        " --display DISPLAY     - Set display for X11\n"
156       );
157#endif
158
159 printf("\nServer Options:\n\n");
160 printf(" -t  --tcp             - Use TCP listen socket\n"
161        " -u  --unix            - Use UNIX Domain listen socket (default)\n"
162#ifdef ROAR_HAVE_LIBDNET
163        " -n  --decnet          - use DECnet listen socket\n"
164#endif
165        " -4                    - Use IPv4 connections (implies -t)\n"
166#ifdef AF_INET6
167        " -6                    - Use IPv6 connections (implies -t)\n"
168#endif
169#ifdef IPV6_ADDRFORM
170        " -64                   - Try to downgrade sockets from IPv6 into IPv4,\n"
171        "                         this is normaly not usefull.\n"
172#endif
173        " -p  --port            - TCP Port to bind to\n"
174        " -b  --bind            - IP/Hostname to bind to\n"
175        "     --sock            - Filename for UNIX Domain Socket\n"
176        "     --proto PROTO     - Use PROTO as protocol on Socket\n"
177        "     --proto-dir DIR   - Set direction parameter for protocol\n"
178        "     --proto-rate RATE - Set sample rate parameter for protocol\n"
179        "     --proto-bits BITS - Set bits per sample parameter for protocol\n"
180        "     --proto-codec E   - Set codec parameter for protocol\n"
181        "     --proto-chans C   - Set number of channels paramter for protocol\n"
182        "     --proto-aiprofile PROFILE\n"
183        "                       - Sets the audio profile for socket\n"
184        "     --proto-profile P - Set profile for listen socket\n"
185        "     --list-proto      - List supported protocols\n"
186        "     --list-profiles   - List supported profiles for --proto-profile\n"
187        "     --new-sock        - Parameters for new socket follows\n"
188#ifdef ROAR_HAVE_LIBSLP
189        "     --slp             - Enable OpenSLP support\n"
190#endif
191#ifdef ROAR_HAVE_LIBX11
192        "     --x11             - Enable X11 support\n"
193#endif
194        " --jumbo-mtu MTU       - Sets the MTU for Jumbo Packets\n"
195        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: %s)\n"
196        "                         You need the permissions to change the GID\n"
197        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
198        "                         You need the permissions to change the UID (normaly only root has)\n"
199        " --no-listen           - Do not listen for new clients\n"
200        "                         (only usefull for relaing, impleys --terminate)\n"
201        " --client-fh           - Comunicate with a client over this handle\n"
202        "                         (only usefull for relaing)\n"
203        " --close-fh            - Closes the given fh\n"
204        " --standby             - Start in standby state\n"
205        " --auto-standby        - Automatical goes into standby if there are no streams\n",
206#ifdef ROAR_DEFAULT_SOCKGRP
207        ROAR_DEFAULT_SOCKGRP
208#else
209        "(none)"
210#endif
211       );
212// printf("\n Options:\n\n");
213 printf("\n");
214}
215
216static void list_proto (void) {
217 printf("  Protocol Flag Subsys - Description\n");
218 printf("------------------------------------------------------\n");
219 printf("  roar          WM LRX - RoarAudio native protocol\n");
220#if !defined(ROAR_WITHOUT_DCOMP_EMUL_ESD) && defined(ROAR_HAVE_H_ESD)
221 printf("  esd           W      - EsounD emulation\n");
222#endif
223#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
224 printf("  simple        WM LRX - PulseAudio simple protocol\n");
225#endif
226#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
227 printf("  rsound        W      - RSound emulation\n");
228#endif
229}
230
231#endif
232
233int restart_server (char * server, int terminate) {
234 struct roar_connection con;
235#ifdef ROAR_HAVE_KILL
236 char buf[80];
237 ssize_t l;
238 struct roar_vio_calls fh;
239 pid_t pid;
240 int ok;
241
242 if ( pidfile != NULL ) {
243  if ( roar_vio_open_file(&fh, pidfile, O_RDONLY, 0644) == -1 ) {
244   ROAR_WARN("restart_server(*): Can not read pidfile: %s", pidfile);
245  } else {
246   l = roar_vio_read(&fh, buf, 80);
247   roar_vio_close(&fh);
248   if ( l > 0 ) {
249    buf[l-1] = 0;
250    buf[79]  = 0;
251    pid = atoi(buf);
252    if ( terminate ) {
253     ok = kill(pid, SIGUSR1);
254    } else {
255     ok = kill(pid, SIGINT);
256    }
257    if ( ok == 0 ) {
258     return 0;
259    } else {
260     ROAR_WARN("restart_server(*): Can not kill roard by pidfile");
261    }
262   } else {
263    ROAR_WARN("restart_server(*): Can not find a PID in the pidfile");
264   }
265  }
266 }
267#endif
268
269 if ( roar_connect(&con, server) == -1 ) {
270  return -1;
271 }
272
273 if ( roar_terminate(&con, terminate) == -1 ) {
274  return -1;
275 }
276
277 return roar_disconnect(&con);
278}
279
280#define R_SETUID 1
281#define R_SETGID 2
282
283int init_config (void) {
284 int i;
285
286 memset(g_config, 0, sizeof(struct roard_config));
287
288 for (i = 0; i < ROAR_DIR_DIRIDS; i++) {
289  g_config->streams[i].mixer_channels = 1;
290  g_config->streams[i].mixer.rpg_mul  = 1;
291  g_config->streams[i].mixer.rpg_div  = 1;
292  g_config->streams[i].mixer.scale    = 65535;
293  g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale;
294 }
295
296 g_config->streams[ROAR_DIR_PLAY    ].flags = ROAR_FLAG_META;
297 g_config->streams[ROAR_DIR_OUTPUT  ].flags = ROAR_FLAG_PASSMIXER;
298 g_config->streams[ROAR_DIR_FILTER  ].flags = ROAR_FLAG_SYNC;
299 g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC;
300 g_config->streams[ROAR_DIR_BIDIR   ].flags = ROAR_FLAG_ANTIECHO;
301
302 g_config->location = "***default***";
303
304 return 0;
305}
306
307#ifdef ROAR_SUPPORT_LISTEN
308int init_listening (void) {
309 int i;
310
311 memset(g_listen, 0, sizeof(g_listen));
312
313 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
314  g_listen[i].proto  = ROAR_PROTO_ROARAUDIO;
315  server[i]          = NULL;
316 }
317
318 return 0;
319}
320
321int get_listen(struct roard_listen ** sock, char *** sockname) {
322 int i;
323
324 if ( sock == NULL )
325  return -1;
326
327 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
328  if ( ! g_listen[i].used ) {
329   server[i] = NULL;
330   *sock = &(g_listen[i]);
331
332   if ( sockname != NULL )
333    *sockname = &(server[i]);
334
335   return 0;
336  }
337 }
338
339 return -1;
340}
341
342#ifdef ROAR_SUPPORT_LISTEN
343static struct _listen_profile {
344 const char * name;
345 int          type;
346 int          port;
347 const char * sockaddr;
348 int          proto;
349 int          dir;
350 const char * aiprofile;
351 const char * desc;
352} _g_listen_profiles[] = {
353 // TODO: convert port numbers into consts!
354
355 // RoarAudio:
356 {"roar-usock",     ROAR_SOCKET_TYPE_UNIX,   0,                 "~/.roar",          ROAR_PROTO_ROARAUDIO, -1, NULL, NULL},
357 {"roar-gsock",     ROAR_SOCKET_TYPE_UNIX,   0,                 "/tmp/roar",        ROAR_PROTO_ROARAUDIO, -1, NULL, NULL},
358 {"roar-tcp",       ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_PORT, "localhost",        ROAR_PROTO_ROARAUDIO, -1, NULL, NULL},
359 {"roar-tcp-pub",   ROAR_SOCKET_TYPE_TCP,    ROAR_DEFAULT_PORT, "0.0.0.0",          ROAR_PROTO_ROARAUDIO, -1, NULL, NULL},
360 {"roar-dnet",      ROAR_SOCKET_TYPE_DECNET, 0,                 "::roar",           ROAR_PROTO_ROARAUDIO, -1, NULL, NULL},
361
362 // EsounD:
363 {"esd-unix",       ROAR_SOCKET_TYPE_UNIX,   0,                 "/tmp/.esd/socket", ROAR_PROTO_ESOUND,    -1, NULL, NULL},
364 {"esd-tcp",        ROAR_SOCKET_TYPE_TCP,    16001,             "localhost",        ROAR_PROTO_ESOUND,    -1, NULL, NULL},
365 {"esd-tcp-pub",    ROAR_SOCKET_TYPE_TCP,    16001,             "0.0.0.0",          ROAR_PROTO_ESOUND,    -1, NULL, NULL},
366
367 // RSound:
368 {"rsound-tcp",     ROAR_SOCKET_TYPE_TCP,    12345,             "localhost",        ROAR_PROTO_RSOUND,    -1, NULL, NULL},
369 {"rsound-tcp-pub", ROAR_SOCKET_TYPE_TCP,    12345,             "0.0.0.0",          ROAR_PROTO_RSOUND,    -1, NULL, NULL},
370 {"rsound-dnet",    ROAR_SOCKET_TYPE_DECNET, 0,                 "::rsound",         ROAR_PROTO_RSOUND,    -1, NULL, NULL},
371
372 // PulseAudio Simple:
373 {"pas-play-tcp",   ROAR_SOCKET_TYPE_TCP,    4712,              "0.0.0.0",          ROAR_PROTO_SIMPLE,
374                                                                                    ROAR_DIR_PLAY, "default",
375                                                                                    NULL},
376 {"pas-mon-tcp",    ROAR_SOCKET_TYPE_TCP,    4712,              "0.0.0.0",          ROAR_PROTO_SIMPLE,
377                                                                                    ROAR_DIR_MONITOR, "default",
378                                                                                    NULL},
379 {NULL, -1, -1, NULL, -1, -1, NULL, NULL}
380};
381
382void listen_listen_profiles (void) {
383 struct _listen_profile * p;
384 char * type;
385 int i;
386
387 printf("Name           Type    Addrress         Port    Protocol  Dir        Audio Profile - Description\n");
388 printf("------------------------------------------------------------------------------------------------\n");
389      //roar-tcp-pub 0.0.0.0       16002   RoarAudio unknown    (null) - (null)
390
391 for (i = 0; (p = &(_g_listen_profiles[i]))->name != NULL; i++) {
392  switch (p->type) {
393   case ROAR_SOCKET_TYPE_UNIX:   type = "UNIX";   break;
394   case ROAR_SOCKET_TYPE_TCP:    type = "TCP";    break;
395   case ROAR_SOCKET_TYPE_DECNET: type = "DECnet"; break;
396   default:
397     type = "unknown";
398    break;
399  }
400  printf("%-14s %-7s %-16s %-7i %-9s %-10s %-13s - %s\n",
401           p->name,
402           type,
403           p->sockaddr, p->port,
404           roar_proto2str(p->proto),
405           p->dir == -1 ? "(none)" : roar_dir2str(p->dir), p->aiprofile == NULL ? "(none)" : p->aiprofile,
406           p->desc == NULL ? "" : p->desc);
407 }
408}
409
410int get_listen_profile (const char * name,
411                        int * port, char ** sockaddr, int * type,
412                        int * proto,
413                        int * dir, struct roar_audio_info * info) {
414 static char buf[1024];
415 struct _listen_profile * p;
416 int i;
417
418 for (i = 0; (p = &(_g_listen_profiles[i]))->name != NULL; i++) {
419  if ( !strcasecmp(p->name, name) ) {
420   *port     = p->port;
421
422   if ( p->type == ROAR_SOCKET_TYPE_UNIX && p->sockaddr[0] != '+' ) {
423    roar_env_render_path_r(buf, sizeof(buf), p->sockaddr);
424   } else {
425    strncpy(buf, p->sockaddr, sizeof(buf));
426   }
427   *sockaddr = buf;
428
429   *proto    = p->proto;
430   *dir      = p->dir;
431
432   if ( p->aiprofile != NULL ) {
433    if ( roar_profile2info(info, p->aiprofile) == -1 ) {
434     ROAR_ERR("Unknown audio profile: %s", p->aiprofile);
435     return -1;
436    }
437   }
438   return 0;
439  }
440 }
441
442 return -1;
443}
444#endif
445
446int add_listen (char * addr, int port, int sock_type, char * user, char * group, int proto, int dir, struct roar_audio_info * info) {
447#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
448 struct group   * grp  = NULL;
449#endif
450#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
451 struct passwd  * pwd  = NULL;
452#endif
453#ifdef ROAR_HAVE_UNIX
454 char * env_roar_proxy_backup;
455#endif
456 int    sockid = -1;
457#ifdef ROAR_HAVE_UNIX
458 int    sock;
459#endif
460 int    i;
461
462 if ( *addr != 0 ) {
463  for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
464   if ( ! g_listen[i].used ) {
465    sockid = i;
466    break;
467   }
468  }
469
470  if ( sockid == -1 )
471   return -1;
472
473  g_listen[sockid].proto = proto;
474
475  ROAR_DBG("add_listen(*): proto=0x%.4x", proto);
476
477  if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) {
478#ifdef ROAR_HAVE_UNIX
479   if ( *addr == '/' ) {
480    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
481     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
482     unsetenv("ROAR_PROXY");
483    }
484    if ( (sock = roar_socket_connect(addr, port)) != -1 ) {
485     close(sock);
486     ROAR_ERR("Can not open listen socket: Socket allready in use");
487     return 1;
488    } else {
489     unlink(addr);
490     if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) {
491      ROAR_ERR("Can not open listen socket: %s", strerror(errno));
492      return 1;
493     }
494    }
495    if ( env_roar_proxy_backup != NULL ) {
496     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
497     free(env_roar_proxy_backup);
498    }
499#else
500   if (0) { // noop
501#endif
502   } else {
503    ROAR_ERR("Can not open listen socket: %s", strerror(errno));
504    return 1;
505   }
506  }
507
508#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
509  if ( group != NULL ) {
510   if ( (grp = getgrnam(group)) == NULL ) {
511    ROAR_ERR("Can not get GID for group %s: %s", group, strerror(errno));
512   }
513  }
514#endif
515#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
516  if ( user ) {
517   if ( (pwd = getpwnam(user)) == NULL ) {
518    ROAR_ERR("Can not get UID for user %s: %s", user, strerror(errno));
519   }
520  }
521#endif
522
523#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
524  if ( *addr == '/' ) {
525   if ( grp || pwd ) {
526     if ( chown(addr, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 )
527      return 1;
528   }
529#ifdef ROAR_HAVE_GETUID
530   if ( grp ) {
531    if ( getuid() == 0 )
532     if ( chmod(addr, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
533      return 1;
534   }
535#endif
536  }
537#endif
538 }
539
540 // in case we opened the listening socket correctly.
541 if ( dir == -1 )
542  dir = ROAR_DIR_PLAY;
543
544 g_listen[sockid].inst.stpl.dir = dir;
545 memcpy(&(g_listen[sockid].inst.stpl.info), info, sizeof(struct roar_audio_info));
546
547 switch (dir) {
548  case ROAR_DIR_PLAY:
549  case ROAR_DIR_RECORD:
550  case ROAR_DIR_MONITOR:
551  case ROAR_DIR_FILTER:
552  case ROAR_DIR_BIDIR:
553    if ( !g_listen[sockid].inst.stpl.info.rate )
554     g_listen[sockid].inst.stpl.info.rate = g_sa->rate;
555
556    if ( !g_listen[sockid].inst.stpl.info.bits )
557     g_listen[sockid].inst.stpl.info.bits = g_sa->bits;
558
559    if ( !g_listen[sockid].inst.stpl.info.channels )
560     g_listen[sockid].inst.stpl.info.channels = g_sa->channels;
561
562    if ( !g_listen[sockid].inst.stpl.info.codec )
563     g_listen[sockid].inst.stpl.info.codec = g_sa->codec;
564   break;
565 }
566
567 g_listen[sockid].used = 1;
568 server[sockid]        = addr;
569
570 return 0;
571}
572#endif
573
574int update_stream_flags (char * str) {
575 int    dir;
576 char * flags;
577 char * k;
578 int    op;
579 int    flag;
580
581 if ( (flags = strstr(str, "=")) == NULL )
582  return -1;
583
584 *flags = 0;
585  flags++;
586
587 if ( (dir = roar_str2dir(str)) == -1 )
588  return -1;
589
590 while (flags != NULL) {
591  k = flags;
592  flags = strstr(flags, ",");
593
594  if ( flags != NULL )
595   *(flags++) = 0;
596
597  switch (*k) {
598   case '+': k++; op = ROAR_SET_FLAG;   break;
599   case '-': k++; op = ROAR_RESET_FLAG; break;
600   default:
601     op = ROAR_SET_FLAG;
602  }
603
604  flag = 0;
605
606  if ( !strcmp(k, "sync") ) {
607   flag = ROAR_FLAG_SYNC;
608  } else if ( !strcmp(k, "meta") ) {
609   flag = ROAR_FLAG_META;
610  } else if ( !strcmp(k, "cleanmeta") ) {
611   flag = ROAR_FLAG_CLEANMETA;
612  } else if ( !strcmp(k, "pause") ) {
613   flag = ROAR_FLAG_PAUSE;
614  } else if ( !strcmp(k, "mute") ) {
615   flag = ROAR_FLAG_MUTE;
616  } else if ( !strcmp(k, "antiecho") ) {
617   flag = ROAR_FLAG_ANTIECHO;
618  } else if ( !strcmp(k, "passmixer") ) {
619   flag = ROAR_FLAG_PASSMIXER;
620  } else {
621   return -1;
622  }
623
624  g_config->streams[dir].flags |= flag;
625
626  if ( op == ROAR_RESET_FLAG )
627   g_config->streams[dir].flags -= flag;
628 }
629
630 return 0;
631}
632
633int add_output (char * drv, char * dev, char * opts, int prim, int count) {
634 int stream;
635 struct roar_stream * s;
636 struct roar_stream_server * ss;
637 char * k, * v;
638#ifdef ROAR_DRIVER_CODEC
639 char * to_free = NULL;
640#endif
641 int sync = 0, f_mmap = 0;
642 int32_t blocks = -1, blocksize = -1;
643 int dir = ROAR_DIR_OUTPUT;
644 int error = 0;
645 // DMX:
646 int32_t channel  = -1;
647 int32_t universe = -1;
648 uint16_t tu16;
649 float q = -32e6;
650
651 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
652
653 if ( drv == NULL && count == 0 ) {
654  drv  = ROAR_DRIVER_DEFAULT;
655  prim = 1;
656  sync = 1;
657
658#ifdef ROAR_DRIVER_CODEC
659  if ( opts == NULL ) {
660   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
661  }
662#endif
663 }
664
665 if ( opts == NULL && count == 0 ) {
666  sync = 1;
667  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
668 }
669
670 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
671
672 if ( (stream = streams_new()) == -1 ) {
673  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
674  if ( prim ) alive = 0;
675  return -1;
676 }
677
678 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
679
680 streams_get(stream, &ss);
681 s = ROAR_STREAM(ss);
682
683 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
684
685 memset(&(s->info), 0xFF, sizeof(struct roar_audio_info)); // set everything to -1
686
687 s->pos_rel_id = -1;
688// s->info.codec = codec;
689
690 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
691
692 if ( opts == NULL ) {
693  k = NULL;
694 } else {
695  k = strtok(opts, ",");
696 }
697
698 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s'): initial k='%s'(%p)", drv, dev, opts, k, k);
699
700 while (k != NULL) {
701//  ROAR_WARN("add_output(*): opts: %s", k);
702
703  if ( (v = strstr(k, "=")) != NULL ) {
704   *v++ = 0;
705  }
706
707  ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v);
708  if ( strcmp(k, "rate") == 0 ) {
709   s->info.rate = atoi(v);
710  } else if ( strcmp(k, "channels") == 0 ) {
711   s->info.channels = atoi(v);
712  } else if ( strcmp(k, "bits") == 0 ) {
713   s->info.bits = atoi(v);
714  } else if ( strcmp(k, "codec") == 0 ) {
715   if ( (s->info.codec = roar_str2codec(v)) == -1 ) {
716    ROAR_ERR("add_output(*): unknown codec '%s'", v);
717    error++;
718   }
719  } else if ( strcmp(k, "q") == 0 ) {
720   q = atof(v);
721  } else if ( strcmp(k, "blocks") == 0 ) {
722   blocks = atoi(v);
723  } else if ( strcmp(k, "blocksize") == 0 ) {
724   blocksize = atoi(v);
725  } else if ( strcmp(k, "mmap") == 0 ) {
726   f_mmap = 1;
727  } else if ( strcmp(k, "subsystem") == 0 ) {
728   if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) {
729    dir = ROAR_DIR_OUTPUT;
730#ifndef ROAR_WITHOUT_DCOMP_MIDI
731   } else if ( !strcasecmp(v, "midi") ) {
732    dir = ROAR_DIR_MIDI_OUT;
733#endif
734#ifndef ROAR_WITHOUT_DCOMP_LIGHT
735   } else if ( !strcasecmp(v, "light") ) {
736    dir = ROAR_DIR_LIGHT_OUT;
737#endif
738#ifndef ROAR_WITHOUT_DCOMP_RAW
739   } else if ( !strcasecmp(v, "raw") ) {
740    dir = ROAR_DIR_RAW_OUT;
741#endif
742   } else if ( !strcasecmp(v, "complex") ) {
743    dir = ROAR_DIR_COMPLEX_OUT;
744   } else {
745    ROAR_ERR("add_output(*): unknown/unsupported subsystem '%s'", k);
746    error++;
747   }
748  // DMX:
749  } else if ( strcmp(k, "channel") == 0 ) {
750   channel  = atoi(v);
751   if ( channel < 0 || channel > 65535 ) {
752    ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel);
753    channel = -1;
754    error++;
755   }
756  } else if ( strcmp(k, "universe") == 0 ) {
757   universe = atoi(v);
758   if ( universe < 0 || universe > 255 ) {
759    ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe);
760    universe = -1;
761    error++;
762   }
763
764  } else if ( strcmp(k, "name") == 0 ) {
765   if ( streams_set_name(stream, v) == -1 ) {
766    ROAR_ERR("add_output(*): Can not set Stream name");
767    error++;
768   }
769
770  } else if ( strcmp(k, "meta") == 0 ) {
771   streams_set_flag(stream, ROAR_FLAG_META);
772  } else if ( strcmp(k, "sync") == 0 ) {
773   sync = 1;
774  } else if ( strcmp(k, "primary") == 0 ) {
775   prim = 1;
776
777  } else if ( strcmp(k, "cleanmeta") == 0 ) {
778   streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
779  } else if ( strcmp(k, "autoconf") == 0 ) {
780   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
781  } else if ( strcmp(k, "recsource") == 0 ) {
782   streams_set_flag(stream, ROAR_FLAG_RECSOURCE);
783  } else if ( strcmp(k, "passmixer") == 0 ) {
784   streams_set_flag(stream, ROAR_FLAG_PASSMIXER);
785  } else {
786   ROAR_ERR("add_output(*): unknown option '%s'", k);
787   error++;
788  }
789
790  if ( error ) {
791   streams_delete(stream);
792   if ( prim ) alive = 0;
793#ifdef ROAR_DRIVER_CODEC
794   if ( to_free != NULL )
795    free(to_free);
796#endif
797   return -1;
798  }
799
800  k = strtok(NULL, ",");
801 }
802
803 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
804
805 // set audio info...
806 switch (dir) {
807  case ROAR_DIR_LIGHT_OUT:
808    switch (s->info.codec) {
809     case ROAR_CODEC_DMX512:
810     case -1:
811       if ( s->info.rate == -1 )
812        s->info.rate = ROAR_OUTPUT_CFREQ;
813
814       s->info.channels =   0;
815       s->info.bits     =   8;
816       s->info.codec    = ROAR_CODEC_DMX512; // in case codec == -1
817      break;
818    }
819   break;
820  case ROAR_DIR_MIDI_OUT:
821    switch (s->info.codec) {
822     case ROAR_CODEC_MIDI:
823     case -1:
824       if ( s->info.rate == -1 )
825        s->info.rate    = ROAR_MIDI_TICKS_PER_BEAT;
826
827       s->info.channels = ROAR_MIDI_CHANNELS_DEFAULT;
828       s->info.bits     = ROAR_MIDI_BITS;
829       s->info.codec    = ROAR_CODEC_MIDI; // in case codec == -1
830      break;
831    }
832   break;
833  case ROAR_DIR_RAW_OUT:
834    if ( s->info.rate == -1 )
835     s->info.rate = 0;
836    if ( s->info.bits == -1 )
837     s->info.bits = 0;
838    if ( s->info.channels == -1 )
839     s->info.channels = 0;
840    if ( s->info.codec == -1 )
841     s->info.codec = 0;
842   break;
843 }
844
845 if ( s->info.rate == -1 )
846  s->info.rate = g_sa->rate;
847 if ( s->info.bits == -1 )
848  s->info.bits = g_sa->bits;
849 if ( s->info.channels == -1 )
850  s->info.channels = g_sa->channels;
851 if ( s->info.codec == -1 )
852  s->info.codec = g_sa->codec;
853
854 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);
855
856 if ( streams_set_dir(stream, dir, 1) == -1 ) {
857  streams_delete(stream);
858  return -1;
859 }
860
861#ifdef ROAR_DRIVER_CODEC
862 if ( to_free != NULL )
863  free(to_free);
864#endif
865
866 if ( s->info.codec == ROAR_CODEC_ALAW || s->info.codec == ROAR_CODEC_MULAW )
867  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
868
869 ROAR_STREAM_SERVER(s)->codec_orgi = s->info.codec;
870
871 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) {
872  ss->driver_id = -1; // don't close a driver not opened...
873  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
874  streams_delete(stream);
875  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
876  if ( prim ) alive = 0;
877  return -1;
878 }
879
880 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here
881 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM,   s); // ignore errors here
882
883 if ( blocks != -1 )
884  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
885
886 if ( blocksize != -1 )
887  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
888
889 // TODO: we shoudld *really* check for errors here...
890 if ( channel != -1 ) {
891  tu16 = channel;
892  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16);
893 }
894 if ( universe != -1 ) {
895  tu16 = universe;
896  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16);
897 }
898
899 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
900
901 streams_set_fh(stream, -1); // update some internal structures
902
903 if ( q > -1e6 ) {
904  ROAR_DBG("add_output(*): setting q=%f", q);
905  streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q);
906 }
907
908 client_stream_add(g_self_client, stream);
909
910 if ( prim ) {
911  streams_mark_primary(stream);
912  s->pos_rel_id = stream;
913 }
914
915 if ( sync ) {
916  streams_set_flag(stream, ROAR_FLAG_SYNC);
917 } else {
918  streams_reset_flag(stream, ROAR_FLAG_SYNC);
919 }
920
921 if ( f_mmap )
922  streams_set_flag(stream, ROAR_FLAG_MMAP);
923
924 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);
925 return 0;
926}
927
928// X11:
929#ifdef ROAR_HAVE_LIBX11
930int register_x11 (int unreg, char * sockname) {
931 struct roar_x11_connection * x11con = NULL;
932 int ret = 0;
933
934 if ( (x11con = roar_x11_connect(x11display)) == NULL )
935  return -1;
936
937 if ( unreg ) {
938  if ( roar_x11_delete_prop(x11con, "ROAR_SERVER") == -1 )
939   ret = -1;
940 } else {
941  if ( roar_x11_set_prop(x11con, "ROAR_SERVER", sockname) == -1 )
942   ret = -1;
943 }
944
945 roar_x11_disconnect(x11con);
946
947 return ret;
948}
949#endif
950
951// SLP:
952void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
953 /* return the error code in the cookie */
954 *(SLPError*)cookie = errcode;
955}
956
957int register_slp (int unreg, char * sockname) {
958#ifdef ROAR_HAVE_LIBSLP
959 static int regged = 0;
960 static char * sn = NULL;
961 SLPError err;
962 SLPError callbackerr;
963 SLPHandle hslp;
964 char addr[1024];
965 char attr[1024] = "";
966 char * location;
967
968 if ( sockname != NULL )
969  sn = sockname;
970
971 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE_ROAR "://%s", sn);
972
973 err = SLPOpen("en", SLP_FALSE, &hslp);
974
975 if (err != SLP_OK) {
976  ROAR_ERR("Error opening slp handle: Error #%i", err);
977  return -1;
978 }
979
980 if (!unreg) {
981
982  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
983   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
984   SLPClose(hslp);
985   return -1;
986  }
987
988  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
989#ifndef ROAR_WITHOUT_DCOMP_LIGHT
990                               "(light-channels=%i),"
991#endif
992                               "(location=%s)",
993           g_sa->rate, g_sa->channels, g_sa->bits,
994#ifndef ROAR_WITHOUT_DCOMP_LIGHT
995           g_light_state.channels,
996#endif
997           location
998          );
999
1000  /* Register a service with SLP */
1001  err = SLPReg(hslp,
1002               addr,
1003               SLP_LIFETIME_MAXIMUM,
1004               0,
1005               attr,
1006               SLP_TRUE,
1007               register_slp_callback,
1008               &callbackerr);
1009  regged = 1;
1010 } else if ( unreg && regged ) {
1011  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
1012  regged = 0;
1013 } else {
1014  SLPClose(hslp);
1015  return -1;
1016 }
1017
1018 /* err may contain an error code that occurred as the slp library    */
1019 /* _prepared_ to make the call.                                     */
1020 if ( err != SLP_OK ) {
1021  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
1022  return -1;
1023 }
1024
1025 /* callbackerr may contain an error code (that was assigned through */
1026 /* the callback cookie) that occurred as slp packets were sent on    */
1027 /* the wire */
1028 if (callbackerr != SLP_OK) {
1029  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
1030  return -1;
1031 }
1032
1033 SLPClose(hslp);
1034 return 0;
1035#else
1036 return -1;
1037#endif
1038}
1039
1040
1041// MAIN:
1042
1043#ifdef ROAR_HAVE_MAIN_ARGS
1044int main (int argc, char * argv[]) {
1045#else
1046int main (void) {
1047#endif
1048#ifdef ROAR_HAVE_MAIN_ARGS
1049 int i;
1050 char * k;
1051#endif
1052#if defined(ROAR_SUPPORT_LISTEN) && defined(ROAR_HAVE_GETUID)
1053 char user_sock[80]  = {0};
1054#endif
1055 struct roar_audio_info sa, max_sa;
1056 struct roard_config config;
1057#ifdef ROAR_HAVE_FORK
1058 int    daemon       = 0;
1059#endif
1060 int    realtime     = 0;
1061 int    sysclocksync = 0;
1062 char * driver    = NULL;
1063 char * device    = NULL;
1064#ifdef ROAR_HAVE_MAIN_ARGS
1065 char * opts      = NULL;
1066#endif
1067// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
1068#ifdef ROAR_SUPPORT_LISTEN
1069 int    port       = ROAR_DEFAULT_PORT;
1070 char * sock_addr  = NULL;
1071 int    sock_proto = ROAR_PROTO_ROARAUDIO;
1072 int    sock_dir   = -1;
1073 struct roar_audio_info sock_info = {0, 0, 0, 0};
1074#endif
1075 int               drvid;
1076#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1077 char * s_drv     = "cf";
1078 char * s_dev     = NULL;
1079 char * s_con     = NULL;
1080 char * s_opt     = NULL;
1081 int    s_prim    = 0;
1082#endif
1083 char * o_drv     = getenv("ROAR_DRIVER");
1084 char * o_dev     = getenv("ROAR_DEVICE");
1085 char * o_opts    = NULL;
1086 int    o_prim    = 0;
1087 int    o_count   = 0;
1088#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1089 int    light_channels = LIGHT_CHANNELS_DEFAULT;
1090#endif
1091#ifdef ROAR_DEFAULT_SOCKGRP
1092 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
1093#else
1094 char * sock_grp  = NULL;
1095#endif
1096 char * sock_user = NULL;
1097#ifdef ROAR_SUPPORT_LISTEN
1098 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
1099#endif
1100#ifdef ROAR_HAVE_LIBSLP
1101 int    reg_slp   = 0;
1102#endif
1103#ifdef ROAR_HAVE_LIBX11
1104 int    reg_x11   = 0;
1105#endif
1106#ifdef ROAR_HAVE_CHROOT
1107 char * chrootdir = NULL;
1108#endif
1109#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
1110 struct group   * grp  = NULL;
1111#endif
1112#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1113 struct passwd  * pwd  = NULL;
1114#endif
1115#ifdef ROAR_HAVE_GETSERVBYNAME
1116 struct servent * serv = NULL;
1117#endif
1118 DRIVER_USERDATA_T drvinst;
1119 struct roar_client * self = NULL;
1120#ifdef ROAR_HAVE_LIBDNET
1121 char decnethost[80];
1122#endif
1123#ifdef SUPPORT_PIDFILE
1124 struct roar_vio_calls pidfile_vio;
1125#endif
1126
1127 ROAR_DBG("main(*): starting roard...");
1128
1129 g_standby       =  0;
1130 g_autostandby   =  0;
1131 alive           =  1;
1132#ifdef ROAR_SUPPORT_LISTEN
1133 g_no_listen     =  0;
1134#else
1135 g_terminate     =  1;
1136#endif
1137
1138 g_verbose       = ROAR_DBG_INFO_NONE;
1139
1140 sa.bits     = ROAR_BITS_DEFAULT;
1141 sa.channels = ROAR_CHANNELS_DEFAULT;
1142 sa.rate     = ROAR_RATE_DEFAULT;
1143 sa.codec    = ROAR_CODEC_DEFAULT;
1144
1145 g_sa        = &sa;
1146 g_max_sa    = &max_sa;
1147
1148 memcpy(g_max_sa, g_sa, sizeof(max_sa));
1149
1150 g_config = &config;
1151
1152 if ( init_config() == -1 ) {
1153  ROAR_ERR("Can not init default config!");
1154  return 1;
1155 }
1156
1157 // load config
1158 roar_libroar_get_config();
1159
1160#ifdef ROAR_SUPPORT_LISTEN
1161 if ( init_listening() == -1 ) {
1162  ROAR_ERR("Can not init listening sockets!");
1163  return 1;
1164 }
1165#endif
1166
1167#ifndef ROAR_WITHOUT_DCOMP_MIDI
1168 if ( midi_init_config() == -1 ) {
1169  ROAR_ERR("Can not init MIDI config!");
1170  return 1;
1171 }
1172#endif
1173
1174#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1175 if ( ssynth_init_config() == -1 ) {
1176  ROAR_ERR("Can not init ssynth config!");
1177  return 1;
1178 }
1179#endif
1180
1181#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1182 if ( rdtcs_init_config() == -1 ) {
1183  ROAR_ERR("Can not init RDTCS config!");
1184  return 1;
1185 }
1186#endif
1187
1188 if ( plugins_preinit() == -1 ) {
1189  ROAR_ERR("Can not pre-init plugins!");
1190  return 1;
1191 }
1192
1193#ifdef ROAR_SUPPORT_LISTEN
1194#ifndef ROAR_TARGET_WIN32
1195 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
1196#else
1197 sock_addr = ROAR_DEFAULT_HOST;
1198#endif
1199
1200#ifdef ROAR_HAVE_GETUID
1201 if ( getuid() != 0 && getenv("HOME") != NULL ) {
1202/*
1203  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
1204*/
1205  roar_env_render_path_r(user_sock, sizeof(user_sock), "~/" ROAR_DEFAULT_SOCK_USER);
1206  sock_addr = user_sock;
1207  ROAR_DBG("main(*): setting sock_addr='%s'", sock_addr);
1208 }
1209#endif
1210
1211 if ( getenv("ROAR_SERVER") != NULL )
1212  sock_addr = getenv("ROAR_SERVER");
1213#endif
1214
1215 if ( clients_init() == -1 ) {
1216  ROAR_ERR("Can not init clients!");
1217  return 1;
1218 }
1219
1220 if ( streams_init() == -1 ) {
1221  ROAR_ERR("Can not init streams!");
1222  return 1;
1223 }
1224
1225 if ( (g_self_client = clients_new()) == -1 ) {
1226  ROAR_ERR("Can not create self client!");
1227  return 1;
1228 }
1229
1230#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1231 if ( sources_init() == -1 ) {
1232  ROAR_ERR("Can not init sources!");
1233  return 1;
1234 }
1235
1236 if ( (sources_set_client(g_self_client)) == -1 ) {
1237  ROAR_ERR("Can not init set source client!");
1238  return 1;
1239 }
1240#endif
1241
1242#ifdef ROAR_HAVE_MAIN_ARGS
1243 for (i = 1; i < argc; i++) {
1244  k = argv[i];
1245
1246  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
1247   usage();
1248   return 0;
1249
1250  } else if ( strcmp(k, "--start") == 0 ) {
1251   // this is a no op
1252  } else if ( strcmp(k, "--restart") == 0 ) {
1253#ifdef ROAR_SUPPORT_LISTEN
1254   if ( restart_server(sock_addr, 1) == -1 ) {
1255    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", sock_addr);
1256   }
1257#else
1258   ROAR_ERR("--restart not supported");
1259#endif
1260  } else if ( strcmp(k, "--shutdown") == 0 ) {
1261#ifdef ROAR_SUPPORT_LISTEN
1262   if ( restart_server(sock_addr, 1) == -1 ) {
1263    ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
1264    return 1;
1265   }
1266   return 0;
1267#else
1268   ROAR_ERR("--shutdown not supported");
1269   return 1;
1270#endif
1271  } else if ( strcmp(k, "--stop") == 0 ) {
1272#ifdef ROAR_SUPPORT_LISTEN
1273   if ( restart_server(sock_addr, 0) == -1 ) {
1274    ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
1275    return 1;
1276   }
1277   return 0;
1278#else
1279   ROAR_ERR("--stop not supported");
1280   return 1;
1281#endif
1282
1283
1284  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
1285#ifdef ROAR_HAVE_FORK
1286   daemon = 1;
1287#else
1288   ROAR_ERR("--daemon not supported");
1289#endif
1290  } else if ( strcmp(k, "--verbose") == 0 ) {
1291   g_verbose++;
1292  } else if ( strcmp(k, "--terminate") == 0 ) {
1293   g_terminate = 1;
1294  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1295   sysclocksync = 1000;
1296  } else if ( strcmp(k, "--realtime") == 0 ) {
1297   realtime++;
1298  } else if ( strcmp(k, "--chroot") == 0 ) {
1299#ifdef ROAR_HAVE_CHROOT
1300   chrootdir = argv[++i];
1301#else
1302   ROAR_ERR("--chroot not supported");
1303   i++;
1304#endif
1305  } else if ( strcmp(k, "--setgid") == 0 ) {
1306#ifdef ROAR_HAVE_SETGID
1307   setids |= R_SETGID;
1308#else
1309   ROAR_ERR("--setgid not supported");
1310#endif
1311  } else if ( strcmp(k, "--setuid") == 0 ) {
1312#ifdef ROAR_HAVE_SETUID
1313   setids |= R_SETUID;
1314#else
1315   ROAR_ERR("--setuid not supported");
1316#endif
1317  } else if ( strcmp(k, "--location") == 0 ) {
1318   g_config->location = argv[++i];
1319  } else if ( strcmp(k, "--pidfile") == 0 ) {
1320#ifdef SUPPORT_PIDFILE
1321   pidfile = argv[++i];
1322#else
1323   ROAR_ERR("--pidfile not supported");
1324   i++;
1325#endif
1326  } else if ( strcmp(k, "--log-syslog") == 0 ) {
1327#ifdef ROAR_HAVE_SYSLOG
1328   roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
1329#else
1330   ROAR_ERR("--log-syslog not supported");
1331#endif
1332
1333
1334  } else if ( strcmp(k, "--plugin-load") == 0 ) {
1335   if ( plugins_load(argv[++i]) == -1 ) {
1336    ROAR_ERR("Can not load plugin");
1337   }
1338
1339  } else if ( strcmp(k, "--list-cf") == 0 ) {
1340   print_codecfilterlist();
1341   return 0;
1342
1343  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
1344   sa.rate = atoi(argv[++i]);
1345  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
1346   sa.bits = atoi(argv[++i]);
1347  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
1348   sa.channels = atoi(argv[++i]);
1349
1350  } else if ( strcmp(k, "--aiprofile") == 0 ) {
1351   if ( roar_profile2info(&sa, argv[++i]) == -1 ) {
1352    ROAR_ERR("Unknown audio profile: %s", argv[i]);
1353    return 1;
1354   }
1355   sa.codec    = ROAR_CODEC_DEFAULT;
1356
1357  } else if ( strcmp(k, "--stream-flags") == 0 ) {
1358   if ( update_stream_flags(argv[++i]) == -1 ) {
1359    ROAR_ERR("Can not set stream flags");
1360    return 1;
1361   }
1362
1363  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
1364   driver = argv[++i];
1365   if ( strcmp(driver, "list") == 0 ) {
1366    ROAR_WARN("The option is obsolete, use --list-driver!");
1367    print_driverlist();
1368    return 0;
1369   }
1370  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
1371   device = argv[++i];
1372  } else if ( strcmp(k, "-dO") == 0 ) {
1373   opts = argv[++i];
1374  } else if ( strcmp(k, "--list-driver") == 0 ) {
1375   print_driverlist();
1376   return 0;
1377
1378  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
1379   o_drv  = argv[++i];
1380  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
1381   o_dev  = argv[++i];
1382  } else if ( strcmp(k, "-oO") == 0 ) {
1383   o_opts = argv[++i];
1384  } else if ( strcmp(k, "-oP") == 0 ) {
1385   o_prim = 1;
1386  } else if ( strcmp(k, "-oN") == 0 ) {
1387   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1388    o_count++;
1389
1390   o_drv  = o_dev = o_opts = NULL;
1391   o_prim = 0;
1392
1393  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
1394#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1395   s_drv = argv[++i];
1396#else
1397   ROAR_ERR("main(*): No support for sources compiled in");
1398#endif
1399  } else if ( strcmp(k, "-S") == 0 ) {
1400#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1401   s_dev = argv[++i];
1402#else
1403   ROAR_ERR("main(*): No support for sources compiled in");
1404#endif
1405  } else if ( strcmp(k, "-sO") == 0 ) {
1406#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1407   s_opt = argv[++i];
1408#else
1409   ROAR_ERR("main(*): No support for sources compiled in");
1410#endif
1411  } else if ( strcmp(k, "-sC") == 0 ) {
1412#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1413   s_con = argv[++i];
1414#else
1415   ROAR_ERR("main(*): No support for sources compiled in");
1416#endif
1417  } else if ( strcmp(k, "-sP") == 0 ) {
1418#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1419   s_prim = 1;
1420#else
1421   ROAR_ERR("main(*): No support for sources compiled in");
1422#endif
1423  } else if ( strcmp(k, "-sN") == 0 ) {
1424#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1425   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1426    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1427   }
1428   s_opt = s_dev = s_con = NULL;
1429   s_drv = "cf";
1430   s_prim = 0;
1431#else
1432   ROAR_ERR("main(*): No support for sources compiled in");
1433#endif
1434  } else if ( strcmp(k, "--list-sources") == 0 ) {
1435#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1436   print_sourcelist();
1437   return 0;
1438#else
1439   ROAR_ERR("main(*): No support for sources compiled in");
1440   return 1;
1441#endif
1442
1443  } else if ( strcmp(k, "--light-channels") == 0 ) {
1444#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1445   light_channels = atoi(argv[++i]);
1446#else
1447   ROAR_WARN("main(*): no light subsystem compiled in");
1448#endif
1449
1450  } else if ( strcmp(k, "--rds-pi") == 0 ) {
1451#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1452   g_rdtcs.rds.pi = atoi(argv[++i]);
1453#else
1454   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1455#endif
1456  } else if ( strcmp(k, "--rds-ps") == 0 ) {
1457#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1458   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
1459    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]);
1460    return 1;
1461   }
1462#else
1463   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1464#endif
1465  } else if ( strcmp(k, "--rds-pty") == 0 ) {
1466#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1467   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
1468    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
1469    return 1;
1470   }
1471#else
1472   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1473#endif
1474  } else if ( strcmp(k, "--rds-tp") == 0 ) {
1475#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1476   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) {
1477    ROAR_ERR("Can not set RDS TP flag");
1478    return 1;
1479   }
1480#else
1481   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1482#endif
1483  } else if ( strcmp(k, "--rds-ct") == 0 ) {
1484#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1485   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) {
1486    ROAR_ERR("Can not set RDS CT flag");
1487    return 1;
1488   }
1489#else
1490   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1491#endif
1492
1493
1494  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
1495#ifndef ROAR_WITHOUT_DCOMP_CB
1496   midi_config.init_cb = 0;
1497#else
1498   // no warning here as this is the disable option
1499#endif
1500  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
1501#ifndef ROAR_WITHOUT_DCOMP_CB
1502   midi_config.init_cb = 1;
1503#else
1504   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1505#endif
1506  } else if ( strcmp(k, "--midi-console") == 0 ) {
1507#ifndef ROAR_WITHOUT_DCOMP_CB
1508   midi_config.console_dev = argv[++i];
1509   midi_config.init_cb = 1;
1510#else
1511   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1512#endif
1513
1514  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
1515#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1516   ssynth_conf.enable = 1;
1517#else
1518   ROAR_ERR("main(*): No support for ssynth compiled in");
1519#endif
1520  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
1521#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1522   ssynth_conf.enable = 0;
1523#else
1524   // we can safely ignore the disable
1525#endif
1526
1527  } else if ( strcmp(k, "--x11-display") == 0 || strcmp(k, "--display") == 0 ) {
1528#ifdef ROAR_HAVE_LIBX11
1529   x11display = argv[++i];
1530#else
1531   ROAR_ERR("No X11 support compiled in!");
1532   return 1;
1533#endif
1534
1535
1536  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
1537   // This is only usefull in INET not UNIX mode.
1538#ifdef ROAR_SUPPORT_LISTEN
1539   if ( *sock_addr == '/' )
1540    sock_addr = ROAR_DEFAULT_HOST;
1541
1542   errno = 0;
1543   if ( (port = atoi(argv[++i])) < 1 ) {
1544#ifdef ROAR_HAVE_GETSERVBYNAME
1545    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
1546     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
1547     return 1;
1548    }
1549    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
1550    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
1551            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
1552    port = ROAR_NET2HOST16(serv->s_port);
1553#else
1554    ROAR_ERR("invalite port number: %s", argv[i]);
1555    return 1;
1556#endif
1557   }
1558#endif
1559  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
1560#ifdef ROAR_SUPPORT_LISTEN
1561   sock_addr = argv[++i];
1562#endif
1563
1564  } else if ( strcmp(k, "--proto") == 0 ) {
1565#ifdef ROAR_SUPPORT_LISTEN
1566   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
1567    ROAR_ERR("Unknown protocol: %s", argv[i]);
1568    return 1;
1569   }
1570#endif
1571  } else if ( strcmp(k, "--proto-dir") == 0 ) {
1572#ifdef ROAR_SUPPORT_LISTEN
1573   if ( (sock_dir = roar_str2dir(argv[++i])) == -1 ) {
1574    ROAR_ERR("Unknown stream direction: %s", argv[i]);
1575    return 1;
1576   }
1577#endif
1578  } else if ( strcmp(k, "--proto-rate") == 0 ) {
1579#ifdef ROAR_SUPPORT_LISTEN
1580   sock_info.rate = atoi(argv[++i]);
1581#endif
1582  } else if ( strcmp(k, "--proto-bits") == 0 ) {
1583#ifdef ROAR_SUPPORT_LISTEN
1584   sock_info.bits = atoi(argv[++i]);
1585#endif
1586  } else if ( strcmp(k, "--proto-chans") == 0 ) {
1587#ifdef ROAR_SUPPORT_LISTEN
1588   sock_info.channels = atoi(argv[++i]);
1589#endif
1590  } else if ( strcmp(k, "--proto-codec") == 0 ) {
1591#ifdef ROAR_SUPPORT_LISTEN
1592   if ( (sock_info.codec = roar_str2codec(argv[++i])) == -1 ) {
1593    ROAR_ERR("Unknown codec: %s", argv[i]);
1594    return 1;
1595   }
1596#endif
1597  } else if ( strcmp(k, "--proto-aiprofile") == 0 ) {
1598#ifdef ROAR_SUPPORT_LISTEN
1599   if ( roar_profile2info(&sock_info, argv[++i]) == -1 ) {
1600    ROAR_ERR("Unknown audio profile: %s", argv[i]);
1601    return 1;
1602   }
1603#endif
1604  } else if ( strcmp(k, "--list-profiles") == 0 ) {
1605#ifdef ROAR_SUPPORT_LISTEN
1606   listen_listen_profiles();
1607   return 0;
1608#endif
1609  } else if ( strcmp(k, "--proto-profile") == 0 ) {
1610#ifdef ROAR_SUPPORT_LISTEN
1611   if ( get_listen_profile(argv[++i], &port, &sock_addr, &sock_type, &sock_proto, &sock_dir, &sock_info) == -1 ) {
1612    ROAR_ERR("Unknown listen profile: %s", argv[i]);
1613    return 1;
1614   }
1615#endif
1616
1617
1618  } else if ( strcmp(k, "--list-proto") == 0 ) {
1619   list_proto();
1620   return 0;
1621
1622  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
1623#ifdef ROAR_SUPPORT_LISTEN
1624   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
1625    sock_type = ROAR_SOCKET_TYPE_TCP;
1626
1627   if ( *sock_addr == '/' )
1628    sock_addr = ROAR_DEFAULT_HOST;
1629#endif
1630
1631  } else if ( strcmp(k, "-4") == 0 ) {
1632#ifdef ROAR_SUPPORT_LISTEN
1633   sock_type = ROAR_SOCKET_TYPE_TCP;
1634   if ( *sock_addr == '/' )
1635    sock_addr = ROAR_DEFAULT_HOST;
1636#endif
1637  } else if ( strcmp(k, "-6") == 0 ) {
1638#ifdef ROAR_SUPPORT_LISTEN
1639#ifdef AF_INET6
1640   sock_type = ROAR_SOCKET_TYPE_TCP6;
1641   if ( *sock_addr == '/' )
1642    sock_addr = ROAR_DEFAULT_HOST;
1643#else
1644    ROAR_ERR("No IPv6 support compiled in!");
1645    return 1;
1646#endif
1647#endif
1648
1649  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
1650#ifdef ROAR_SUPPORT_LISTEN
1651   // ignore this case as it is the default behavor.
1652   sock_type = ROAR_SOCKET_TYPE_UNIX;
1653#endif
1654
1655  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
1656#ifdef ROAR_SUPPORT_LISTEN
1657#ifdef ROAR_HAVE_LIBDNET
1658    port   = ROAR_DEFAULT_NUM;
1659    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
1660    sock_addr = decnethost;
1661    sock_type = ROAR_SOCKET_TYPE_DECNET;
1662#else
1663    ROAR_ERR("No DECnet support compiled in!");
1664    return 1;
1665#endif
1666#endif
1667  } else if ( strcmp(k, "--new-sock") == 0 ) {
1668#ifdef ROAR_SUPPORT_LISTEN
1669   if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
1670    ROAR_ERR("Can not open listen socket!");
1671    return 1;
1672   }
1673#endif
1674
1675  } else if ( strcmp(k, "--slp") == 0 ) {
1676#ifdef ROAR_HAVE_LIBSLP
1677   reg_slp = 1;
1678#else
1679   ROAR_ERR("No OpenSLP support compiled in!");
1680   return 1;
1681#endif
1682
1683  } else if ( strcmp(k, "--x11") == 0 ) {
1684#ifdef ROAR_HAVE_LIBX11
1685   reg_x11 = 1;
1686#else
1687   ROAR_ERR("No X11 support compiled in!");
1688   return 1;
1689#endif
1690
1691
1692  } else if ( strcmp(k, "--jumbo-mtu") == 0 ) {
1693   g_config->jumbo_mtu = atoi(argv[++i]);
1694
1695  } else if ( strcmp(k, "-G") == 0 ) {
1696   sock_grp  = argv[++i];
1697  } else if ( strcmp(k, "-U") == 0 ) {
1698   sock_user = argv[++i];
1699
1700  } else if ( strcmp(k, "--no-listen") == 0 ) {
1701#ifdef ROAR_SUPPORT_LISTEN
1702   sock_addr   = "";
1703   g_terminate = 1;
1704   g_no_listen = 1;
1705#endif
1706  } else if ( strcmp(k, "--client-fh") == 0 ) {
1707   if ( clients_new_from_fh(atoi(argv[++i]), ROAR_PROTO_ROARAUDIO, ROAR_BYTEORDER_NETWORK, 1) == -1 ) {
1708    ROAR_ERR("main(*): Can not set client's fh");
1709    return 1;
1710   }
1711  } else if ( strcmp(k, "--close-fh") == 0 ) {
1712#ifdef ROAR_HAVE_IO_POSIX
1713   close(atoi(argv[++i]));
1714#else
1715   i++;
1716   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
1717#endif
1718
1719  } else if ( strcmp(k, "--standby") == 0 ) {
1720   g_standby = 1;
1721  } else if ( strcmp(k, "--auto-standby") == 0 ) {
1722   g_autostandby = 1;
1723  } else {
1724   usage();
1725   return 1;
1726  }
1727
1728 }
1729#endif
1730
1731#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1732 if ( s_dev != NULL ) {
1733  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1734   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1735  }
1736 }
1737#endif
1738
1739 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
1740
1741 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
1742
1743 if ( waveform_init() == -1 ) {
1744  ROAR_ERR("Can not initialize Waveform subsystem");
1745  return 1;
1746 }
1747
1748#ifndef ROAR_WITHOUT_DCOMP_MIDI
1749 if ( midi_init() == -1 ) {
1750  ROAR_ERR("Can not initialize MIDI subsystem");
1751 }
1752#endif
1753
1754#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1755 if ( ssynth_init() == -1 ) {
1756  ROAR_ERR("Can not initialize ssynth subsystem");
1757 }
1758#endif
1759
1760#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1761 if ( light_init(light_channels) == -1 ) {
1762  ROAR_ERR("Can not initialize light control subsystem");
1763 }
1764#endif
1765
1766#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1767 if ( rdtcs_init() == -1 ) {
1768  ROAR_ERR("Can not initialize RDTCS subsystem");
1769 }
1770#endif
1771
1772 if ( plugins_init() == -1 ) {
1773  ROAR_ERR("Can not initialize plugins");
1774 }
1775
1776#ifdef ROAR_SUPPORT_LISTEN
1777 if ( !g_no_listen ) {
1778  if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
1779   ROAR_ERR("Can not open listen socket!");
1780   return 1;
1781  }
1782 }
1783#endif
1784
1785 if ( output_buffer_init(&sa) == -1 ) {
1786  ROAR_ERR("Can not init output buffer!");
1787  return 1;
1788 }
1789
1790 if ( driver == NULL ) {
1791  driver = "null";
1792 } else {
1793  ROAR_WARN("Usage of old driver interface. use -o not -d!");
1794 }
1795
1796 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
1797  ROAR_ERR("Can not open output driver!");
1798  return 1;
1799 }
1800
1801 if ( samples_init() == -1 ) {
1802  ROAR_ERR("Can not init samples!");
1803  return 1;
1804 }
1805
1806
1807 // we should handle this on microcontrollers, too.
1808#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
1809 signal(SIGINT,  on_sig_int);
1810 signal(SIGTERM, on_sig_term);
1811 signal(SIGCHLD, on_sig_chld);
1812 signal(SIGUSR1, on_sig_usr1);
1813 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
1814#endif
1815
1816 if ( realtime ) {
1817#ifdef DEBUG
1818  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
1819#endif
1820
1821#ifdef ROAR_HAVE_NICE
1822  errno = 0;
1823  nice(-5*realtime); // -5 for each --realtime
1824  if ( errno ) {
1825   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1826  }
1827#else
1828  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1829#endif
1830/*
1831#ifdef __linux__
1832  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
1833   ROAR_WARN("Can not set io priority: %s", strerror(errno));
1834#endif
1835*/
1836 }
1837
1838#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
1839 if ( setids & R_SETGID ) {
1840  if ( sock_grp == NULL ) {
1841   ROAR_ERR("Can not set GID if no groupname is supplied");
1842   return 1;
1843  }
1844  if ( (grp = getgrnam(sock_grp)) == NULL ) {
1845   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
1846   return 1;
1847  }
1848  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
1849   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
1850  }
1851  if ( !grp || setgid(grp->gr_gid) == -1 ) {
1852   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
1853  }
1854 }
1855#endif
1856
1857
1858 clients_set_pid(g_self_client, getpid());
1859#ifdef ROAR_HAVE_GETUID
1860 clients_set_uid(g_self_client, getuid());
1861#endif
1862#ifdef ROAR_HAVE_GETGID
1863 clients_set_gid(g_self_client, getgid());
1864#endif
1865 clients_get(g_self_client, &self);
1866
1867 if ( self == NULL ) {
1868  ROAR_ERR("Can not get self client!");
1869  return 1;
1870 }
1871
1872 strcpy(self->name, "RoarAudio daemon internal");
1873
1874 if ( roar_nnode_free(&(self->nnode)) == -1 )
1875  return 1;
1876
1877 // not fully correct but ok as workaorund
1878 // so tools assume that roard runs on the same machine as
1879 // they in case they use AF_UNIX:
1880 if ( roar_nnode_new(&(self->nnode), ROAR_SOCKET_TYPE_UNIX) == -1 )
1881  return 1;
1882
1883#ifdef ROAR_HAVE_FORK
1884 if ( daemon ) {
1885#ifdef ROAR_HAVE_SYSLOG
1886  roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
1887#else
1888  roar_debug_set_stderr_fh(-1);
1889#endif
1890
1891  close(ROAR_STDIN );
1892  close(ROAR_STDOUT);
1893  close(ROAR_STDERR);
1894
1895  if ( fork() )
1896   ROAR_U_EXIT(0);
1897
1898#ifdef ROAR_HAVE_SETSID
1899  setsid();
1900#endif
1901  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
1902 }
1903#endif
1904
1905#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1906 // early test for UID as we need this for the pidfile and the setuid()
1907 if ( sock_user != NULL ) {
1908  if ( (pwd = getpwnam(sock_user)) == NULL ) {
1909   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
1910   return 1;
1911  }
1912 }
1913#endif
1914
1915#ifdef SUPPORT_PIDFILE
1916 if ( pidfile != NULL ) {
1917  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
1918   ROAR_ERR("Can not write pidfile: %s", pidfile);
1919  } else {
1920   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
1921   roar_vio_close(&pidfile_vio);
1922  }
1923#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1924  if ( pwd || grp ) {
1925   if ( chown(pidfile, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 ) {
1926    ROAR_WARN("Can not change ownership of pidfile: %s: %s", pidfile, strerror(errno));
1927   }
1928  }
1929  if ( chmod(pidfile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1 ) {
1930   ROAR_WARN("Can not change permissions of pidfile: %s: %s", pidfile, strerror(errno));
1931  }
1932#endif
1933 }
1934#endif
1935
1936#ifdef ROAR_HAVE_CHROOT
1937 if (chrootdir) {
1938  if ( chroot(chrootdir) == -1 ) {
1939   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
1940   return 2;
1941  }
1942  if ( chdir("/") == -1 ) {
1943   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
1944   return 2;
1945  }
1946 }
1947#endif
1948
1949#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1950 if ( setids & R_SETUID ) {
1951  if ( sock_user == NULL ) {
1952   ROAR_ERR("Can not set UID if no username is supplied");
1953   return 1;
1954  }
1955  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
1956   ROAR_ERR("Can not set UserID: %s", strerror(errno));
1957   return 3;
1958  }
1959#ifdef ROAR_HAVE_GETUID
1960  clients_set_uid(g_self_client, getuid());
1961#endif
1962 }
1963#endif
1964
1965 // Register with OpenSLP:
1966#ifdef ROAR_HAVE_LIBSLP
1967 if ( reg_slp ) {
1968  register_slp(0, sock_addr);
1969 }
1970#endif
1971
1972#ifdef ROAR_HAVE_LIBX11
1973 if ( reg_x11 ) {
1974  register_x11(0, sock_addr);
1975 }
1976#endif
1977
1978 // start main loop...
1979 main_loop(drvid, drvinst, &sa, sysclocksync);
1980
1981 // clean up.
1982 clean_quit_prep();
1983 driver_close(drvinst, drvid);
1984 output_buffer_free();
1985
1986 return 0;
1987}
1988
1989void cleanup_listen_socket (int terminate) {
1990 int i;
1991
1992 // Deregister from SLP:
1993#ifdef ROAR_HAVE_LIBSLP
1994 register_slp(1, NULL);
1995#endif
1996
1997#ifdef ROAR_HAVE_LIBX11
1998 register_x11(1, NULL);
1999#endif
2000
2001#ifdef ROAR_SUPPORT_LISTEN
2002 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
2003  if ( g_listen[i].used  ) {
2004   roar_vio_close(&(g_listen[i].sock));
2005
2006   g_listen[i].used = 0;
2007
2008#ifdef ROAR_HAVE_UNIX
2009   if ( server[i] != NULL )
2010    if ( *(server[i]) == '/' )
2011     unlink(server[i]);
2012#endif
2013  }
2014 }
2015
2016#endif
2017
2018 if ( terminate )
2019  g_terminate = 1;
2020}
2021
2022void clean_quit_prep (void) {
2023 cleanup_listen_socket(0);
2024
2025 plugins_free();
2026
2027#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2028 sources_free();
2029#endif
2030 streams_free();
2031 clients_free();
2032#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2033 ssynth_free();
2034#endif
2035#ifndef ROAR_WITHOUT_DCOMP_CB
2036 midi_cb_stop(); // stop console beep
2037#endif
2038#ifndef ROAR_WITHOUT_DCOMP_MIDI
2039 midi_free();
2040#endif
2041#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2042 light_free();
2043#endif
2044#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2045 rdtcs_free();
2046#endif
2047
2048 waveform_free();
2049
2050#ifdef SUPPORT_PIDFILE
2051 if ( pidfile != NULL )
2052  unlink(pidfile);
2053#endif
2054}
2055
2056void clean_quit (void) {
2057 clean_quit_prep();
2058// driver_close(drvinst, drvid);
2059// output_buffer_free();
2060 exit(0);
2061}
2062
2063//ll
Note: See TracBrowser for help on using the repository browser.