source: roaraudio/roard/roard.c @ 3957:68855c6ae89c

Last change on this file since 3957:68855c6ae89c was 3957:68855c6ae89c, checked in by phi, 14 years ago

nicer display, added pas-*-tcp

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