source: roaraudio/roard/roard.c @ 2728:5d36ed1176f2

Last change on this file since 2728:5d36ed1176f2 was 2728:5d36ed1176f2, checked in by phi, 15 years ago

added --rds-* from TODO list

File size: 42.8 KB
Line 
1//roard.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "roard.h"
26
27#ifdef ROAR_SUPPORT_LISTEN
28char * server[ROAR_MAX_LISTEN_SOCKETS];
29#endif
30
31#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_FS_POSIX)
32#define SUPPORT_PIDFILE
33char * pidfile = NULL;
34#endif
35
36#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
37 int    setids    = 0;
38#endif
39
40#ifdef ROAR_HAVE_MAIN_ARGS
41void usage (void) {
42 printf("Usage: roard [OPTIONS]...\n\n");
43
44 printf("Misc Options:\n\n");
45 printf(
46        " --daemon              - Bring the server into background after init\n"
47        " --terminate           - Terminate after last client quited\n"
48        " --start               - No op parameter (starting roard is default operation)\n"
49        " --restart             - Trys to stop an old instance and start a new with new settings\n"
50        " --stop                - Stops a running roard (provide --pidfile!)\n"
51        " --shutdown            - Terminates a running roard (provide --pidfile!)\n"
52        " --realtime            - Trys to get realtime priority,\n"
53        "                         give multible times for being more realtime\n"
54        " --chroot DIR          - chroots to the given dir\n"
55        " --setgid              - GroupID to the audio group as specified via -G\n"
56        " --setuid              - UserID to the audio user as specified via -U\n"
57        " --sysclocksync        - calculate exact sample rate using the system clock\n"
58        " --location  LOC       - Set lion readable location of server\n"
59#ifdef SUPPORT_PIDFILE
60        " --pidfile PIDFILE     - Write a pidfile at PIDFILE\n"
61#endif
62       );
63
64 printf("\nAudio Options:\n\n");
65 printf(
66        " -R  --rate   RATE     - Set server rate\n"
67        " -B  --bits   BITS     - Set server bits\n"
68        " -C  --chans  CHANNELS - Set server channels\n"
69       );
70
71 printf("\nStream Options:\n\n");
72 printf(
73        " --stream-flags D=F    - Set default flags for stream directions\n"
74        "                         D is the stream direction and F is a comma seperated\n"
75        "                         list of flags in form +flag or -flag to set or unset\n"
76        "                         a flag as default or remove it from the default\n"
77       );
78
79 printf("\nDriver Options: (obsolete, do not use, Use Ouput Options)\n\n");
80 printf(" -d  --driver DRV      - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT);
81 printf(" -D  --device DEV      - Set the device\n");
82 printf(" -dO OPTS              - Set output options\n");
83 printf(" --list-driver         - List all drivers\n");
84
85 printf("\nOutput Options:\n\n");
86 printf(" -o  --odriver DRV     - Set the driver, use '--list-driver' to get a list\n");
87 printf(" -O  --odevice DEV     - Set the device\n");
88 printf(" -oO OPTS              - Set output options\n");
89 printf(" -oN                   - Adds another output\n");
90 printf(" -oP                   - Mark output as primary\n");
91
92#ifndef ROAR_WITHOUT_DCOMP_SOURCES
93 printf("\nSource Options:\n\n");
94 printf(" -s  --source DRV      - Use DRV as input driver\n"
95        " -S           DEV      - Use DEV as input device\n"
96        " -sO          OPTS     - Use OPTS as input options\n"
97        " -sN                   - Adds another source\n"
98        " -sP                   - Make souce as primary\n"
99       );
100 printf(" --list-sources        - List all sources\n");
101#endif
102
103 printf("\nCodec Filter Options:\n\n");
104 printf(" --list-cf             - List all codec filter\n"
105       );
106
107#ifndef ROAR_WITHOUT_DCOMP_MIDI
108 printf("\nMIDI Options:\n\n");
109 printf(" --midi-no-console     - Disable console based MIDI synth\n"
110        " --midi-console-enable - Enables the console based MIDI synth\n"
111        " --midi-console DEV    - Set device for MIDI console\n"
112#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
113        " --ssynth-enable       - Enable simple software synth\n"
114        " --ssynth-disable      - Disable simple software synth\n"
115#endif
116       );
117#endif
118
119#ifndef ROAR_WITHOUT_DCOMP_LIGHT
120 printf("\nLight Control Options:\n\n");
121 printf(" --light-channels NUM  - Sets the number of channels for Light control (default: %i)\n",
122                                  LIGHT_CHANNELS_DEFAULT
123       );
124#endif
125
126#ifndef ROAR_WITHOUT_DCOMP_RDTCS
127 printf("\nRadio Date and Transmitter Control System Options:\n\n");
128 printf(" --rds-pi   PI         - Sets the RDS Programme Identification (PI)\n"
129        " --rds-ps   PS         - Sets the RDS Programme Service Name (PS)\n"
130        " --rds-pty  PTY        - Sets the RDS Programme Type (PTY)\n"
131        " --rds-tp              - Sets the RDS Traffic Programme (TP) flag\n"
132        " --rds-ct              - Enables sending of RDS Clock Time (CT)\n"
133       );
134#endif
135
136 printf("\nServer Options:\n\n");
137 printf(" -t  --tcp             - Use TCP listen socket\n"
138        " -u  --unix            - Use UNIX Domain listen socket (default)\n"
139#ifdef ROAR_HAVE_LIBDNET
140        " -n  --decnet          - use DECnet listen socket\n"
141#endif
142        " -4                    - Use IPv4 connections (implies -t)\n"
143#ifdef PF_INET6
144        " -6                    - Use IPv6 connections (implies -t)\n"
145#endif
146#ifdef IPV6_ADDRFORM
147        " -64                   - Try to downgrade sockets from IPv6 into IPv4,\n"
148        "                         this is normaly not usefull.\n"
149#endif
150        " -p  --port            - TCP Port to bind to\n"
151        " -b  --bind            - IP/Hostname to bind to\n"
152        "     --sock            - Filename for UNIX Domain Socket\n"
153        "     --proto PROTO     - Use PROTO as protocol on Socket\n"
154        "     --new-sock        - Parameters for new socket follows\n"
155#ifdef ROAR_HAVE_LIBSLP
156        "     --slp             - Enable OpenSLP support\n"
157#endif
158        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: %s)\n"
159        "                         You need the permittions to change the GID\n"
160        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
161        "                         You need the permittions to change the UID (normaly only root has)\n"
162        " --no-listen           - Do not listen for new clients\n"
163        "                         (only usefull for relaing, impleys --terminate)\n"
164        " --client-fh           - Comunicate with a client over this handle\n"
165        "                         (only usefull for relaing)\n"
166        " --close-fh            - Closes the given fh\n"
167        " --standby             - Start in standby state\n"
168        " --auto-standby        - Automatical goes into standby if there are no streams\n",
169        ROAR_DEFAULT_SOCKGRP
170       );
171// printf("\n Options:\n\n");
172 printf("\n");
173}
174#endif
175
176int restart_server (char * server, int terminate) {
177 struct roar_connection con;
178#ifdef ROAR_HAVE_KILL
179 char buf[80];
180 ssize_t l;
181 struct roar_vio_calls fh;
182 pid_t pid;
183 int ok;
184
185 if ( pidfile != NULL ) {
186  if ( roar_vio_open_file(&fh, pidfile, O_RDONLY, 0644) == -1 ) {
187   ROAR_WARN("restart_server(*): Can not read pidfile: %s", pidfile);
188  } else {
189   l = roar_vio_read(&fh, buf, 80);
190   roar_vio_close(&fh);
191   if ( l > 0 ) {
192    buf[l-1] = 0;
193    buf[79]  = 0;
194    pid = atoi(buf);
195    if ( terminate ) {
196     ok = kill(pid, SIGUSR1);
197    } else {
198     ok = kill(pid, SIGINT);
199    }
200    if ( ok == 0 ) {
201     return 0;
202    } else {
203     ROAR_WARN("restart_server(*): Can not kill roard by pidfile");
204    }
205   } else {
206    ROAR_WARN("restart_server(*): Can not find a PID in the pidfile");
207   }
208  }
209 }
210#endif
211
212 if ( roar_connect(&con, server) == -1 ) {
213  return -1;
214 }
215
216 if ( roar_terminate(&con, terminate) == -1 ) {
217  return -1;
218 }
219
220 return roar_disconnect(&con);
221}
222
223#define R_SETUID 1
224#define R_SETGID 2
225
226int init_config (void) {
227 int i;
228
229 memset(g_config, 0, sizeof(struct roard_config));
230
231 for (i = 0; i < ROAR_DIR_DIRIDS; i++) {
232  g_config->streams[i].mixer_channels = 1;
233  g_config->streams[i].mixer.rpg_mul  = 1;
234  g_config->streams[i].mixer.rpg_div  = 1;
235  g_config->streams[i].mixer.scale    = 65535;
236  g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale;
237 }
238
239 g_config->streams[ROAR_DIR_PLAY    ].flags = ROAR_FLAG_META;
240 g_config->streams[ROAR_DIR_OUTPUT  ].flags = ROAR_FLAG_PASSMIXER;
241 g_config->streams[ROAR_DIR_FILTER  ].flags = ROAR_FLAG_SYNC;
242 g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC;
243 g_config->streams[ROAR_DIR_BIDIR   ].flags = ROAR_FLAG_ANTIECHO;
244
245 g_config->location = "***default***";
246
247 return 0;
248}
249
250#ifdef ROAR_SUPPORT_LISTEN
251int init_listening (void) {
252 int i;
253
254 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
255  g_listen_socket[i] = -1;
256  g_listen_proto[i]  = ROAR_PROTO_ROARAUDIO;
257  server[i]          = NULL;
258 }
259
260 return 0;
261}
262
263int add_listen (char * addr, int port, int sock_type, char * user, char * group, int proto) {
264#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
265 struct group   * grp  = NULL;
266#endif
267#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
268 struct passwd  * pwd  = NULL;
269#endif
270#ifdef ROAR_HAVE_UNIX
271 char * env_roar_proxy_backup;
272#endif
273 int    sockid = -1;
274 int    sock;
275 int    i;
276
277 if ( *addr != 0 ) {
278  for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
279   if ( g_listen_socket[i] == -1 ) {
280    sockid = i;
281    break;
282   }
283  }
284
285  if ( sockid == -1 )
286   return -1;
287
288  g_listen_proto[sockid] = proto;
289
290  ROAR_DBG("add_listen(*): proto=0x%.4x", proto);
291
292  if ( (g_listen_socket[sockid] = roar_socket_listen(sock_type, addr, port)) == -1 ) {
293#ifdef ROAR_HAVE_UNIX
294   if ( *addr == '/' ) {
295    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
296     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
297     unsetenv("ROAR_PROXY");
298    }
299    if ( (sock = roar_socket_connect(addr, port)) != -1 ) {
300     close(sock);
301     ROAR_ERR("Can not open listen socket!");
302     return 1;
303    } else {
304     unlink(addr);
305     if ( (g_listen_socket[sockid] = roar_socket_listen(sock_type, addr, port)) == -1 ) {
306      ROAR_ERR("Can not open listen socket!");
307      return 1;
308     }
309    }
310    if ( env_roar_proxy_backup != NULL ) {
311     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
312     free(env_roar_proxy_backup);
313    }
314#else
315   if (0) { // noop
316#endif
317   } else {
318    ROAR_ERR("Can not open listen socket!");
319    return 1;
320   }
321  }
322
323#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
324  if ( (grp = getgrnam(group)) == NULL ) {
325   ROAR_ERR("Can not get GID for group %s: %s", group, strerror(errno));
326  }
327#endif
328#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
329  if ( user || (setids & R_SETUID) ) {
330   if ( (pwd = getpwnam(user)) == NULL ) {
331    ROAR_ERR("Can not get UID for user %s: %s", user, strerror(errno));
332   }
333  }
334#endif
335
336#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
337  if ( *addr == '/' ) {
338   if ( grp ) {
339    if ( pwd ) {
340     if ( chown(addr, pwd->pw_uid, grp->gr_gid) == -1 )
341      return 1;
342    } else {
343     if ( chown(addr, -1, grp->gr_gid) == -1 )
344      return 1;
345    }
346#ifdef ROAR_HAVE_GETUID
347    if ( getuid() == 0 )
348     if ( chmod(addr, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
349      return 1;
350#endif
351   }
352  }
353#endif
354 }
355
356 return 0;
357}
358#endif
359
360int update_stream_flags (char * str) {
361 int    dir;
362 char * flags;
363 char * k;
364 int    op;
365 int    flag;
366
367 if ( (flags = strstr(str, "=")) == NULL )
368  return -1;
369
370 *flags = 0;
371  flags++;
372
373 if ( (dir = roar_str2dir(str)) == -1 )
374  return -1;
375
376 while (flags != NULL) {
377  k = flags;
378  flags = strstr(flags, ",");
379
380  if ( flags != NULL )
381   *(flags++) = 0;
382
383  switch (*k) {
384   case '+': k++; op = ROAR_SET_FLAG;   break;
385   case '-': k++; op = ROAR_RESET_FLAG; break;
386   default:
387     op = ROAR_SET_FLAG;
388  }
389
390  flag = 0;
391
392  if ( !strcmp(k, "sync") ) {
393   flag = ROAR_FLAG_SYNC;
394  } else if ( !strcmp(k, "meta") ) {
395   flag = ROAR_FLAG_META;
396  } else if ( !strcmp(k, "cleanmeta") ) {
397   flag = ROAR_FLAG_CLEANMETA;
398  } else if ( !strcmp(k, "pause") ) {
399   flag = ROAR_FLAG_PAUSE;
400  } else if ( !strcmp(k, "mute") ) {
401   flag = ROAR_FLAG_MUTE;
402  } else if ( !strcmp(k, "antiecho") ) {
403   flag = ROAR_FLAG_ANTIECHO;
404  } else if ( !strcmp(k, "passmixer") ) {
405   flag = ROAR_FLAG_PASSMIXER;
406  } else {
407   return -1;
408  }
409
410  g_config->streams[dir].flags |= flag;
411
412  if ( op == ROAR_RESET_FLAG )
413   g_config->streams[dir].flags -= flag;
414 }
415
416 return 0;
417}
418
419int add_output (char * drv, char * dev, char * opts, int prim, int count) {
420 int stream;
421 struct roar_stream * s;
422 struct roar_stream_server * ss;
423 char * k, * v;
424#ifdef ROAR_DRIVER_CODEC
425 char * to_free = NULL;
426#endif
427 int sync = 0, f_mmap = 0;
428 int32_t blocks = -1, blocksize = -1;
429 int dir = ROAR_DIR_OUTPUT;
430 int error = 0;
431 // DMX:
432 int32_t channel  = -1;
433 int32_t universe = -1;
434 uint16_t tu16;
435 float q = -32e6;
436
437 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
438
439 if ( drv == NULL && count == 0 ) {
440  drv  = ROAR_DRIVER_DEFAULT;
441  prim = 1;
442  sync = 1;
443
444#ifdef ROAR_DRIVER_CODEC
445  if ( opts == NULL ) {
446   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
447  }
448#endif
449 }
450
451 if ( opts == NULL && count == 0 ) {
452  sync = 1;
453  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
454 }
455
456 if ( (stream = streams_new()) == -1 ) {
457  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
458  if ( prim ) alive = 0;
459  return -1;
460 }
461
462 streams_get(stream, &ss);
463 s = ROAR_STREAM(ss);
464
465 memset(&(s->info), 0xFF, sizeof(struct roar_audio_info)); // set everything to -1
466
467 s->pos_rel_id = -1;
468// s->info.codec = codec;
469
470 k = strtok(opts, ",");
471 while (k != NULL) {
472//  ROAR_WARN("add_output(*): opts: %s", k);
473
474  if ( (v = strstr(k, "=")) != NULL ) {
475   *v++ = 0;
476  }
477
478  ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v);
479  if ( strcmp(k, "rate") == 0 ) {
480   s->info.rate = atoi(v);
481  } else if ( strcmp(k, "channels") == 0 ) {
482   s->info.channels = atoi(v);
483  } else if ( strcmp(k, "bits") == 0 ) {
484   s->info.bits = atoi(v);
485  } else if ( strcmp(k, "codec") == 0 ) {
486   if ( (s->info.codec = roar_str2codec(v)) == -1 ) {
487    ROAR_ERR("add_output(*): unknown codec '%s'", v);
488    error++;
489   }
490  } else if ( strcmp(k, "q") == 0 ) {
491   q = atof(v);
492  } else if ( strcmp(k, "blocks") == 0 ) {
493   blocks = atoi(v);
494  } else if ( strcmp(k, "blocksize") == 0 ) {
495   blocksize = atoi(v);
496  } else if ( strcmp(k, "mmap") == 0 ) {
497   f_mmap = 1;
498  } else if ( strcmp(k, "subsystem") == 0 ) {
499   if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) {
500    dir = ROAR_DIR_OUTPUT;
501#ifndef ROAR_WITHOUT_DCOMP_MIDI
502   } else if ( !strcasecmp(v, "midi") ) {
503    dir = ROAR_DIR_MIDI_OUT;
504#endif
505#ifndef ROAR_WITHOUT_DCOMP_LIGHT
506   } else if ( !strcasecmp(v, "light") ) {
507    dir = ROAR_DIR_LIGHT_OUT;
508#endif
509#ifndef ROAR_WITHOUT_DCOMP_RAW
510   } else if ( !strcasecmp(v, "raw") ) {
511    dir = ROAR_DIR_RAW_OUT;
512#endif
513   } else if ( !strcasecmp(v, "complex") ) {
514    dir = ROAR_DIR_COMPLEX_OUT;
515   } else {
516    ROAR_ERR("add_output(*): unknown/unsupported subsystem '%s'", k);
517    error++;
518   }
519  // DMX:
520  } else if ( strcmp(k, "channel") == 0 ) {
521   channel  = atoi(v);
522   if ( channel < 0 || channel > 65535 ) {
523    ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel);
524    channel = -1;
525    error++;
526   }
527  } else if ( strcmp(k, "universe") == 0 ) {
528   universe = atoi(v);
529   if ( universe < 0 || universe > 255 ) {
530    ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe);
531    universe = -1;
532    error++;
533   }
534
535  } else if ( strcmp(k, "name") == 0 ) {
536   if ( streams_set_name(stream, v) == -1 ) {
537    ROAR_ERR("add_output(*): Can not set Stream name");
538    error++;
539   }
540
541  } else if ( strcmp(k, "meta") == 0 ) {
542   streams_set_flag(stream, ROAR_FLAG_META);
543  } else if ( strcmp(k, "sync") == 0 ) {
544   sync = 1;
545  } else if ( strcmp(k, "primary") == 0 ) {
546   prim = 1;
547
548  } else if ( strcmp(k, "cleanmeta") == 0 ) {
549   streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
550  } else if ( strcmp(k, "autoconf") == 0 ) {
551   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
552  } else if ( strcmp(k, "recsource") == 0 ) {
553   streams_set_flag(stream, ROAR_FLAG_RECSOURCE);
554  } else if ( strcmp(k, "passmixer") == 0 ) {
555   streams_set_flag(stream, ROAR_FLAG_PASSMIXER);
556  } else {
557   ROAR_ERR("add_output(*): unknown option '%s'", k);
558   error++;
559  }
560
561  if ( error ) {
562   streams_delete(stream);
563   if ( prim ) alive = 0;
564#ifdef ROAR_DRIVER_CODEC
565   if ( to_free != NULL )
566    free(to_free);
567#endif
568   return -1;
569  }
570
571  k = strtok(NULL, ",");
572 }
573
574 // set audio info...
575 switch (dir) {
576  case ROAR_DIR_LIGHT_OUT:
577    switch (s->info.codec) {
578     case ROAR_CODEC_DMX512:
579     case -1:
580       if ( s->info.rate == -1 )
581        s->info.rate = ROAR_OUTPUT_CFREQ;
582
583       s->info.channels =   0;
584       s->info.bits     =   8;
585       s->info.codec    = ROAR_CODEC_DMX512; // in case codec == -1
586      break;
587    }
588   break;
589  case ROAR_DIR_MIDI_OUT:
590    switch (s->info.codec) {
591     case ROAR_CODEC_MIDI:
592     case -1:
593       if ( s->info.rate == -1 )
594        s->info.rate    = ROAR_MIDI_TICKS_PER_BEAT;
595
596       s->info.channels = ROAR_MIDI_CHANNELS_DEFAULT;
597       s->info.bits     = ROAR_MIDI_BITS;
598       s->info.codec    = ROAR_CODEC_MIDI; // in case codec == -1
599      break;
600    }
601   break;
602  case ROAR_DIR_RAW_OUT:
603    if ( s->info.rate == -1 )
604     s->info.rate = 0;
605    if ( s->info.bits == -1 )
606     s->info.bits = 0;
607    if ( s->info.channels == -1 )
608     s->info.channels = 0;
609    if ( s->info.codec == -1 )
610     s->info.codec = 0;
611   break;
612 }
613
614 if ( s->info.rate == -1 )
615  s->info.rate = g_sa->rate;
616 if ( s->info.bits == -1 )
617  s->info.bits = g_sa->bits;
618 if ( s->info.channels == -1 )
619  s->info.channels = g_sa->channels;
620 if ( s->info.codec == -1 )
621  s->info.codec = g_sa->codec;
622
623 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);
624
625 if ( streams_set_dir(stream, dir, 1) == -1 ) {
626  streams_delete(stream);
627  return -1;
628 }
629
630#ifdef ROAR_DRIVER_CODEC
631 if ( to_free != NULL )
632  free(to_free);
633#endif
634
635 if ( s->info.codec == ROAR_CODEC_ALAW || s->info.codec == ROAR_CODEC_MULAW )
636  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
637
638 ROAR_STREAM_SERVER(s)->codec_orgi = s->info.codec;
639
640 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) {
641  ss->driver_id = -1; // don't close a driver not opened...
642  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
643  streams_delete(stream);
644  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
645  if ( prim ) alive = 0;
646  return -1;
647 }
648
649 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here
650 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM,   s); // ignore errors here
651
652 if ( blocks != -1 )
653  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
654
655 if ( blocksize != -1 )
656  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
657
658 // TODO: we shoudld *really* check for errors here...
659 if ( channel != -1 ) {
660  tu16 = channel;
661  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16);
662 }
663 if ( universe != -1 ) {
664  tu16 = universe;
665  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16);
666 }
667
668 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
669
670 streams_set_fh(stream, -1); // update some internal structures
671
672 if ( q > -1e6 ) {
673  ROAR_DBG("add_output(*): setting q=%f", q);
674  streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q);
675 }
676
677 client_stream_add(g_self_client, stream);
678
679 if ( prim ) {
680  streams_mark_primary(stream);
681  s->pos_rel_id = stream;
682 }
683
684 if ( sync ) {
685  streams_set_flag(stream, ROAR_FLAG_SYNC);
686 } else {
687  streams_reset_flag(stream, ROAR_FLAG_SYNC);
688 }
689
690 if ( f_mmap )
691  streams_set_flag(stream, ROAR_FLAG_MMAP);
692
693 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);
694 return 0;
695}
696
697
698// SLP:
699void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
700 /* return the error code in the cookie */
701 *(SLPError*)cookie = errcode;
702}
703
704int register_slp (int unreg, char * sockname) {
705#ifdef ROAR_HAVE_LIBSLP
706 static int regged = 0;
707 static char * sn = NULL;
708 SLPError err;
709 SLPError callbackerr;
710 SLPHandle hslp;
711 char addr[1024];
712 char attr[1024] = "";
713 char * location;
714
715 if ( sockname != NULL )
716  sn = sockname;
717
718 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE "://%s", sn);
719
720 err = SLPOpen("en", SLP_FALSE, &hslp);
721
722 if (err != SLP_OK) {
723  ROAR_ERR("Error opening slp handle: Error #%i", err);
724  return -1;
725 }
726
727 if (!unreg) {
728
729  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
730   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
731   SLPClose(hslp);
732   return -1;
733  }
734
735  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
736#ifndef ROAR_WITHOUT_DCOMP_LIGHT
737                               "(light-channels=%i),"
738#endif
739                               "(location=%s)",
740           g_sa->rate, g_sa->channels, g_sa->bits,
741#ifndef ROAR_WITHOUT_DCOMP_LIGHT
742           g_light_state.channels,
743#endif
744           location
745          );
746
747  /* Register a service with SLP */
748  err = SLPReg(hslp,
749               addr,
750               SLP_LIFETIME_MAXIMUM,
751               0,
752               attr,
753               SLP_TRUE,
754               register_slp_callback,
755               &callbackerr);
756  regged = 1;
757 } else if ( unreg && regged ) {
758  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
759  regged = 0;
760 } else {
761  SLPClose(hslp);
762  return -1;
763 }
764
765 /* err may contain an error code that occurred as the slp library    */
766 /* _prepared_ to make the call.                                     */
767 if ( err != SLP_OK ) {
768  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
769  return -1;
770 }
771
772 /* callbackerr may contain an error code (that was assigned through */
773 /* the callback cookie) that occurred as slp packets were sent on    */
774 /* the wire */
775 if (callbackerr != SLP_OK) {
776  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
777  return -1;
778 }
779
780 SLPClose(hslp);
781 return 0;
782#else
783 return -1;
784#endif
785}
786
787
788// MAIN:
789
790#ifdef ROAR_HAVE_MAIN_ARGS
791int main (int argc, char * argv[]) {
792#else
793int main (void) {
794#endif
795#ifdef ROAR_HAVE_MAIN_ARGS
796 int i;
797 char * k;
798#endif
799#ifdef ROAR_SUPPORT_LISTEN
800 char user_sock[80]  = {0};
801#endif
802 struct roar_audio_info sa, max_sa;
803 struct roard_config config;
804#ifdef ROAR_HAVE_FORK
805 int    daemon       = 0;
806#endif
807 int    realtime     = 0;
808 int    sysclocksync = 0;
809 char * driver    = NULL;
810 char * device    = NULL;
811#ifdef ROAR_HAVE_MAIN_ARGS
812 char * opts      = NULL;
813#endif
814// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
815#ifdef ROAR_SUPPORT_LISTEN
816 int    port       = ROAR_DEFAULT_PORT;
817 char * sock_addr  = NULL;
818 int    sock_proto = ROAR_PROTO_ROARAUDIO;
819#endif
820 int               drvid;
821#ifndef ROAR_WITHOUT_DCOMP_SOURCES
822 char * s_drv     = "cf";
823 char * s_dev     = NULL;
824 char * s_con     = NULL;
825 char * s_opt     = NULL;
826 int    s_prim    = 0;
827#endif
828 char * o_drv     = getenv("ROAR_DRIVER");
829 char * o_dev     = getenv("ROAR_DEVICE");
830 char * o_opts    = NULL;
831 int    o_prim    = 0;
832 int    o_count   = 0;
833#ifndef ROAR_WITHOUT_DCOMP_LIGHT
834 int    light_channels = LIGHT_CHANNELS_DEFAULT;
835#endif
836 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
837 char * sock_user = NULL;
838#ifdef ROAR_SUPPORT_LISTEN
839 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
840#endif
841#ifdef ROAR_HAVE_LIBSLP
842 int    reg_slp   = 0;
843#endif
844#ifdef ROAR_HAVE_CHROOT
845 char * chrootdir = NULL;
846#endif
847#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
848 struct group   * grp  = NULL;
849#endif
850#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
851 struct passwd  * pwd  = NULL;
852#endif
853#ifdef ROAR_HAVE_GETSERVBYNAME
854 struct servent * serv = NULL;
855#endif
856 DRIVER_USERDATA_T drvinst;
857 struct roar_client * self = NULL;
858#ifdef ROAR_HAVE_LIBDNET
859 char decnethost[80];
860#endif
861#ifdef SUPPORT_PIDFILE
862 struct roar_vio_calls pidfile_vio;
863#endif
864
865 g_standby       =  0;
866 g_autostandby   =  0;
867 alive           =  1;
868#ifdef ROAR_SUPPORT_LISTEN
869 g_no_listen     =  0;
870#else
871 g_terminate     =  1;
872#endif
873
874 sa.bits     = ROAR_BITS_DEFAULT;
875 sa.channels = ROAR_CHANNELS_DEFAULT;
876 sa.rate     = ROAR_RATE_DEFAULT;
877 sa.codec    = ROAR_CODEC_DEFAULT;
878
879 g_sa        = &sa;
880 g_max_sa    = &max_sa;
881
882 memcpy(g_max_sa, g_sa, sizeof(max_sa));
883
884 g_config = &config;
885
886 if ( init_config() == -1 ) {
887  ROAR_ERR("Can not init default config!");
888  return 1;
889 }
890
891#ifdef ROAR_SUPPORT_LISTEN
892 if ( init_listening() == -1 ) {
893  ROAR_ERR("Can not init listening sockets!");
894  return 1;
895 }
896#endif
897
898#ifndef ROAR_WITHOUT_DCOMP_MIDI
899 if ( midi_init_config() == -1 ) {
900  ROAR_ERR("Can not init MIDI config!");
901  return 1;
902 }
903#endif
904
905#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
906 if ( ssynth_init_config() == -1 ) {
907  ROAR_ERR("Can not init ssynth config!");
908  return 1;
909 }
910#endif
911
912#ifndef ROAR_WITHOUT_DCOMP_RDTCS
913 if ( rdtcs_init_config() == -1 ) {
914  ROAR_ERR("Can not init RDTCS config!");
915  return 1;
916 }
917#endif
918
919#ifdef ROAR_SUPPORT_LISTEN
920 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
921#ifdef ROAR_HAVE_GETUID
922 if ( getuid() != 0 && getenv("HOME") != NULL ) {
923  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
924  sock_addr = user_sock;
925 }
926#endif
927
928 if ( getenv("ROAR_SERVER") != NULL )
929  sock_addr = getenv("ROAR_SERVER");
930#endif
931
932 if ( clients_init() == -1 ) {
933  ROAR_ERR("Can not init clients!");
934  return 1;
935 }
936
937 if ( streams_init() == -1 ) {
938  ROAR_ERR("Can not init streams!");
939  return 1;
940 }
941
942 if ( (g_self_client = clients_new()) == -1 ) {
943  ROAR_ERR("Can not create self client!");
944  return 1;
945 }
946
947#ifndef ROAR_WITHOUT_DCOMP_SOURCES
948 if ( sources_init() == -1 ) {
949  ROAR_ERR("Can not init sources!");
950  return 1;
951 }
952
953 if ( (sources_set_client(g_self_client)) == -1 ) {
954  ROAR_ERR("Can not init set source client!");
955  return 1;
956 }
957#endif
958
959#ifdef ROAR_HAVE_MAIN_ARGS
960 for (i = 1; i < argc; i++) {
961  k = argv[i];
962
963  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
964   usage();
965   return 0;
966
967  } else if ( strcmp(k, "--start") == 0 ) {
968   // this is a no op
969  } else if ( strcmp(k, "--restart") == 0 ) {
970#ifdef ROAR_SUPPORT_LISTEN
971   if ( restart_server(sock_addr, 1) == -1 ) {
972    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", sock_addr);
973   }
974#else
975   ROAR_ERR("--restart not supported");
976#endif
977  } else if ( strcmp(k, "--shutdown") == 0 ) {
978#ifdef ROAR_SUPPORT_LISTEN
979   if ( restart_server(sock_addr, 1) == -1 ) {
980    ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
981    return 1;
982   }
983   return 0;
984#else
985   ROAR_ERR("--shutdown not supported");
986   return 1;
987#endif
988  } else if ( strcmp(k, "--stop") == 0 ) {
989#ifdef ROAR_SUPPORT_LISTEN
990   if ( restart_server(sock_addr, 0) == -1 ) {
991    ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
992    return 1;
993   }
994   return 0;
995#else
996   ROAR_ERR("--stop not supported");
997   return 1;
998#endif
999
1000
1001  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
1002#ifdef ROAR_HAVE_FORK
1003   daemon = 1;
1004#else
1005   ROAR_ERR("--daemon not supported");
1006#endif
1007  } else if ( strcmp(k, "--terminate") == 0 ) {
1008   g_terminate = 1;
1009  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1010   sysclocksync = 1000;
1011  } else if ( strcmp(k, "--realtime") == 0 ) {
1012   realtime++;
1013  } else if ( strcmp(k, "--chroot") == 0 ) {
1014#ifdef ROAR_HAVE_CHROOT
1015   chrootdir = argv[++i];
1016#else
1017   ROAR_ERR("--chroot not supported");
1018   i++;
1019#endif
1020  } else if ( strcmp(k, "--setgid") == 0 ) {
1021#ifdef ROAR_HAVE_SETGID
1022   setids |= R_SETGID;
1023#else
1024   ROAR_ERR("--setgid not supported");
1025#endif
1026  } else if ( strcmp(k, "--setuid") == 0 ) {
1027#ifdef ROAR_HAVE_SETUID
1028   setids |= R_SETUID;
1029#else
1030   ROAR_ERR("--setuid not supported");
1031#endif
1032  } else if ( strcmp(k, "--location") == 0 ) {
1033   g_config->location = argv[++i];
1034  } else if ( strcmp(k, "--pidfile") == 0 ) {
1035#ifdef SUPPORT_PIDFILE
1036   pidfile = argv[++i];
1037#else
1038   ROAR_ERR("--pidfile not supported");
1039   i++;
1040#endif
1041
1042  } else if ( strcmp(k, "--list-cf") == 0 ) {
1043   print_codecfilterlist();
1044   return 0;
1045
1046  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
1047   sa.rate = atoi(argv[++i]);
1048  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
1049   sa.bits = atoi(argv[++i]);
1050  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
1051   sa.channels = atoi(argv[++i]);
1052
1053  } else if ( strcmp(k, "--stream-flags") == 0 ) {
1054   if ( update_stream_flags(argv[++i]) == -1 ) {
1055    ROAR_ERR("Can not set stream flags");
1056    return 1;
1057   }
1058
1059  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
1060   driver = argv[++i];
1061   if ( strcmp(driver, "list") == 0 ) {
1062    ROAR_WARN("The option is obsolete, use --list-driver!");
1063    print_driverlist();
1064    return 0;
1065   }
1066  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
1067   device = argv[++i];
1068  } else if ( strcmp(k, "-dO") == 0 ) {
1069   opts = argv[++i];
1070  } else if ( strcmp(k, "--list-driver") == 0 ) {
1071   print_driverlist();
1072   return 0;
1073
1074  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
1075   o_drv  = argv[++i];
1076  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
1077   o_dev  = argv[++i];
1078  } else if ( strcmp(k, "-oO") == 0 ) {
1079   o_opts = argv[++i];
1080  } else if ( strcmp(k, "-oP") == 0 ) {
1081   o_prim = 1;
1082  } else if ( strcmp(k, "-oN") == 0 ) {
1083   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1084    o_count++;
1085
1086   o_drv  = o_dev = o_opts = NULL;
1087   o_prim = 0;
1088
1089  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
1090#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1091   s_drv = argv[++i];
1092#else
1093   ROAR_ERR("main(*): No support for sources compiled in");
1094#endif
1095  } else if ( strcmp(k, "-S") == 0 ) {
1096#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1097   s_dev = argv[++i];
1098#else
1099   ROAR_ERR("main(*): No support for sources compiled in");
1100#endif
1101  } else if ( strcmp(k, "-sO") == 0 ) {
1102#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1103   s_opt = argv[++i];
1104#else
1105   ROAR_ERR("main(*): No support for sources compiled in");
1106#endif
1107  } else if ( strcmp(k, "-sC") == 0 ) {
1108#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1109   s_con = argv[++i];
1110#else
1111   ROAR_ERR("main(*): No support for sources compiled in");
1112#endif
1113  } else if ( strcmp(k, "-sP") == 0 ) {
1114#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1115   s_prim = 1;
1116#else
1117   ROAR_ERR("main(*): No support for sources compiled in");
1118#endif
1119  } else if ( strcmp(k, "-sN") == 0 ) {
1120#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1121   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1122    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1123   }
1124   s_opt = s_dev = s_con = NULL;
1125   s_drv = "cf";
1126   s_prim = 0;
1127#else
1128   ROAR_ERR("main(*): No support for sources compiled in");
1129#endif
1130  } else if ( strcmp(k, "--list-sources") == 0 ) {
1131#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1132   print_sourcelist();
1133   return 0;
1134#else
1135   ROAR_ERR("main(*): No support for sources compiled in");
1136   return 1;
1137#endif
1138
1139  } else if ( strcmp(k, "--light-channels") == 0 ) {
1140#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1141   light_channels = atoi(argv[++i]);
1142#else
1143   ROAR_WARN("main(*): no light subsystem compiled in");
1144#endif
1145
1146  } else if ( strcmp(k, "--rds-pi") == 0 ) {
1147#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1148   g_rdtcs.rds.pi = atoi(argv[++i]);
1149#else
1150   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1151#endif
1152  } else if ( strcmp(k, "--rds-ps") == 0 ) {
1153#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1154   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
1155    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", arvg[i]);
1156    return 1;
1157   }
1158#else
1159   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1160#endif
1161  } else if ( strcmp(k, "--rds-pty") == 0 ) {
1162#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1163   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
1164    ROAR_ERR("Can not set RDS PTY to '%s'", arvg[i]);
1165    return 1;
1166   }
1167#else
1168   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1169#endif
1170  } else if ( strcmp(k, "--rds-tp") == 0 ) {
1171#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1172   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) {
1173    ROAR_ERR("Can not set RDS TP flag");
1174    return 1;
1175   }
1176#else
1177   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1178#endif
1179  } else if ( strcmp(k, "--rds-ct") == 0 ) {
1180#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1181   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) {
1182    ROAR_ERR("Can not set RDS CT flag");
1183    return 1;
1184   }
1185#else
1186   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1187#endif
1188
1189
1190  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
1191#ifndef ROAR_WITHOUT_DCOMP_CB
1192   midi_config.init_cb = 0;
1193#else
1194   // no warning here as this is the disable option
1195#endif
1196  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
1197#ifndef ROAR_WITHOUT_DCOMP_CB
1198   midi_config.init_cb = 1;
1199#else
1200   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1201#endif
1202  } else if ( strcmp(k, "--midi-console") == 0 ) {
1203#ifndef ROAR_WITHOUT_DCOMP_CB
1204   midi_config.console_dev = argv[++i];
1205   midi_config.init_cb = 1;
1206#else
1207   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1208#endif
1209
1210  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
1211#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1212   ssynth_conf.enable = 1;
1213#else
1214   ROAR_ERR("main(*): No support for ssynth compiled in");
1215#endif
1216  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
1217#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1218   ssynth_conf.enable = 0;
1219#else
1220   // we can safely ignore the disable
1221#endif
1222
1223  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
1224   // This is only usefull in INET not UNIX mode.
1225#ifdef ROAR_SUPPORT_LISTEN
1226   if ( *sock_addr == '/' )
1227    sock_addr = ROAR_DEFAULT_HOST;
1228
1229   errno = 0;
1230   if ( (port = atoi(argv[++i])) < 1 ) {
1231#ifdef ROAR_HAVE_GETSERVBYNAME
1232    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
1233     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
1234     return 1;
1235    }
1236    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
1237    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
1238            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
1239    port = ROAR_NET2HOST16(serv->s_port);
1240#else
1241    ROAR_ERR("invalite port number: %s", argv[i]);
1242    return 1;
1243#endif
1244   }
1245#endif
1246  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
1247#ifdef ROAR_SUPPORT_LISTEN
1248   sock_addr = argv[++i];
1249#endif
1250
1251  } else if ( strcmp(k, "--proto") == 0 ) {
1252#ifdef ROAR_SUPPORT_LISTEN
1253   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
1254    ROAR_ERR("Unknown protocol: %s", argv[i]);
1255    return 1;
1256   }
1257#endif
1258
1259  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
1260#ifdef ROAR_SUPPORT_LISTEN
1261   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
1262    sock_type = ROAR_SOCKET_TYPE_TCP;
1263
1264   if ( *sock_addr == '/' )
1265    sock_addr = ROAR_DEFAULT_HOST;
1266#endif
1267
1268  } else if ( strcmp(k, "-4") == 0 ) {
1269#ifdef ROAR_SUPPORT_LISTEN
1270   sock_type = ROAR_SOCKET_TYPE_TCP;
1271   if ( *sock_addr == '/' )
1272    sock_addr = ROAR_DEFAULT_HOST;
1273#endif
1274  } else if ( strcmp(k, "-6") == 0 ) {
1275#ifdef ROAR_SUPPORT_LISTEN
1276#ifdef PF_INET6
1277   sock_type = ROAR_SOCKET_TYPE_TCP6;
1278   if ( *sock_addr == '/' )
1279    sock_addr = ROAR_DEFAULT_HOST;
1280#else
1281    ROAR_ERR("No IPv6 support compiled in!");
1282    return 1;
1283#endif
1284#endif
1285
1286  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
1287#ifdef ROAR_SUPPORT_LISTEN
1288   // ignore this case as it is the default behavor.
1289   sock_type = ROAR_SOCKET_TYPE_UNIX;
1290#endif
1291
1292  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
1293#ifdef ROAR_SUPPORT_LISTEN
1294#ifdef ROAR_HAVE_LIBDNET
1295    port   = ROAR_DEFAULT_NUM;
1296    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
1297    sock_addr = decnethost;
1298    sock_type = ROAR_SOCKET_TYPE_DECNET;
1299#else
1300    ROAR_ERR("No DECnet support compiled in!");
1301    return 1;
1302#endif
1303#endif
1304  } else if ( strcmp(k, "--new-sock") == 0 ) {
1305#ifdef ROAR_SUPPORT_LISTEN
1306   if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto) != 0 ) {
1307    ROAR_ERR("Can not open listen socket!");
1308    return 1;
1309   }
1310#endif
1311
1312  } else if ( strcmp(k, "--slp") == 0 ) {
1313#ifdef ROAR_HAVE_LIBSLP
1314   reg_slp = 1;
1315#else
1316    ROAR_ERR("No OpenSLP support compiled in!");
1317    return 1;
1318#endif
1319
1320  } else if ( strcmp(k, "-G") == 0 ) {
1321   sock_grp  = argv[++i];
1322  } else if ( strcmp(k, "-U") == 0 ) {
1323   sock_user = argv[++i];
1324
1325  } else if ( strcmp(k, "--no-listen") == 0 ) {
1326#ifdef ROAR_SUPPORT_LISTEN
1327   sock_addr   = "";
1328   g_terminate = 1;
1329   g_no_listen = 1;
1330#endif
1331  } else if ( strcmp(k, "--client-fh") == 0 ) {
1332   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
1333    ROAR_ERR("main(*): Can not set client's fh");
1334    return 1;
1335   }
1336  } else if ( strcmp(k, "--close-fh") == 0 ) {
1337#ifdef ROAR_HAVE_IO_POSIX
1338   close(atoi(argv[++i]));
1339#else
1340   i++;
1341   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
1342#endif
1343
1344  } else if ( strcmp(k, "--standby") == 0 ) {
1345   g_standby = 1;
1346  } else if ( strcmp(k, "--auto-standby") == 0 ) {
1347   g_autostandby = 1;
1348  } else {
1349   usage();
1350   return 1;
1351  }
1352
1353 }
1354#endif
1355
1356#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1357 if ( s_dev != NULL ) {
1358  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1359   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1360  }
1361 }
1362#endif
1363
1364 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
1365
1366 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
1367
1368#ifndef ROAR_WITHOUT_DCOMP_MIDI
1369 if ( midi_init() == -1 ) {
1370  ROAR_ERR("Can not initialize MIDI subsystem");
1371 }
1372#endif
1373
1374#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1375 if ( ssynth_init() == -1 ) {
1376  ROAR_ERR("Can not initialize ssynth subsystem");
1377 }
1378#endif
1379
1380#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1381 if ( light_init(light_channels) == -1 ) {
1382  ROAR_ERR("Can not initialize light control subsystem");
1383 }
1384#endif
1385
1386#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1387 if ( rdtcs_init() == -1 ) {
1388  ROAR_ERR("Can not initialize RDTCS subsystem");
1389 }
1390#endif
1391
1392#ifdef ROAR_SUPPORT_LISTEN
1393 if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto) != 0 ) {
1394  ROAR_ERR("Can not open listen socket!");
1395  return 1;
1396 }
1397#endif
1398
1399 if ( output_buffer_init(&sa) == -1 ) {
1400  ROAR_ERR("Can not init output buffer!");
1401  return 1;
1402 }
1403
1404 if ( driver == NULL ) {
1405  driver = "null";
1406 } else {
1407  ROAR_WARN("Usage of old driver interface. use -o not -d!");
1408 }
1409
1410 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
1411  ROAR_ERR("Can not open output driver!");
1412  return 1;
1413 }
1414
1415 if ( samples_init() == -1 ) {
1416  ROAR_ERR("Can not init samples!");
1417  return 1;
1418 }
1419
1420
1421 // we should handle this on microcontrollers, too.
1422#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
1423 signal(SIGINT,  on_sig_int);
1424 signal(SIGCHLD, on_sig_chld);
1425 signal(SIGUSR1, on_sig_usr1);
1426 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
1427#endif
1428
1429 if ( realtime ) {
1430#ifdef DEBUG
1431  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
1432#endif
1433
1434#ifdef ROAR_HAVE_NICE
1435  errno = 0;
1436  nice(-5*realtime); // -5 for each --realtime
1437  if ( errno ) {
1438   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1439  }
1440#else
1441  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1442#endif
1443/*
1444#ifdef __linux__
1445  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
1446   ROAR_WARN("Can not set io priority: %s", strerror(errno));
1447#endif
1448*/
1449 }
1450
1451#ifdef ROAR_HAVE_SETGID
1452 if ( setids & R_SETGID ) {
1453  if ( (grp = getgrnam(sock_grp)) == NULL ) {
1454   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
1455   return 1;
1456  }
1457  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
1458   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
1459  }
1460  if ( !grp || setgid(grp->gr_gid) == -1 ) {
1461   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
1462  }
1463 }
1464#endif
1465
1466
1467 clients_set_pid(g_self_client, getpid());
1468#ifdef ROAR_HAVE_GETUID
1469 clients_set_uid(g_self_client, getuid());
1470#endif
1471#ifdef ROAR_HAVE_GETGID
1472 clients_set_gid(g_self_client, getgid());
1473#endif
1474 clients_get(g_self_client, &self);
1475
1476 if ( self == NULL ) {
1477  ROAR_ERR("Can not get self client!");
1478  return 1;
1479 }
1480
1481 strcpy(self->name, "RoarAudio daemon internal");
1482
1483#ifdef ROAR_HAVE_FORK
1484 if ( daemon ) {
1485  close(ROAR_STDIN );
1486  close(ROAR_STDOUT);
1487  close(ROAR_STDERR);
1488
1489  if ( fork() )
1490   ROAR_U_EXIT(0);
1491
1492#ifdef ROAR_HAVE_SETSID
1493  setsid();
1494#endif
1495  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
1496 }
1497#endif
1498
1499#ifdef SUPPORT_PIDFILE
1500 if ( pidfile != NULL ) {
1501  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
1502   ROAR_ERR("Can not write pidfile: %s", pidfile);
1503  } else {
1504   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
1505   roar_vio_close(&pidfile_vio);
1506  }
1507 }
1508#endif
1509
1510#ifdef ROAR_HAVE_CHROOT
1511 if (chrootdir) {
1512  if ( chroot(chrootdir) == -1 ) {
1513   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
1514   return 2;
1515  }
1516  if ( chdir("/") == -1 ) {
1517   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
1518   return 2;
1519  }
1520 }
1521#endif
1522
1523#ifdef ROAR_HAVE_SETUID
1524 if ( setids & R_SETUID ) {
1525  if ( (pwd = getpwnam(sock_user)) == NULL ) {
1526   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
1527   return 1;
1528  }
1529  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
1530   ROAR_ERR("Can not set UserID: %s", strerror(errno));
1531   return 3;
1532  }
1533#ifdef ROAR_HAVE_GETUID
1534  clients_set_uid(g_self_client, getuid());
1535#endif
1536 }
1537#endif
1538
1539 // Register with OpenSLP:
1540#ifdef ROAR_HAVE_LIBSLP
1541 if ( reg_slp ) {
1542  register_slp(0, sock_addr);
1543 }
1544#endif
1545
1546 // start main loop...
1547 main_loop(drvid, drvinst, &sa, sysclocksync);
1548
1549 // clean up.
1550 clean_quit_prep();
1551 driver_close(drvinst, drvid);
1552 output_buffer_free();
1553
1554 return 0;
1555}
1556
1557void cleanup_listen_socket (int terminate) {
1558 int i;
1559
1560 // Deregister from SLP:
1561#ifdef ROAR_HAVE_LIBSLP
1562 register_slp(1, NULL);
1563#endif
1564
1565#ifdef ROAR_SUPPORT_LISTEN
1566 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
1567  if ( g_listen_socket[i] != -1 ) {
1568#ifdef ROAR_HAVE_IO_POSIX
1569   close(g_listen_socket[i]);
1570#endif // #else is useless because we are in void context.
1571
1572   g_listen_socket[i] = -1;
1573
1574#ifdef ROAR_HAVE_UNIX
1575   if ( server[i] != NULL )
1576    if ( *(server[i]) == '/' )
1577     unlink(server[i]);
1578#endif
1579  }
1580 }
1581
1582#endif
1583
1584 if ( terminate )
1585  g_terminate = 1;
1586}
1587
1588void clean_quit_prep (void) {
1589 cleanup_listen_socket(0);
1590
1591#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1592 sources_free();
1593#endif
1594 streams_free();
1595 clients_free();
1596#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1597 ssynth_free();
1598#endif
1599#ifndef ROAR_WITHOUT_DCOMP_CB
1600 midi_cb_stop(); // stop console beep
1601#endif
1602#ifndef ROAR_WITHOUT_DCOMP_MIDI
1603 midi_free();
1604#endif
1605#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1606 light_free();
1607#endif
1608#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1609 rdtcs_free();
1610#endif
1611
1612#ifdef SUPPORT_PIDFILE
1613 if ( pidfile != NULL )
1614  unlink(pidfile);
1615#endif
1616}
1617
1618void clean_quit (void) {
1619 clean_quit_prep();
1620// driver_close(drvinst, drvid);
1621// output_buffer_free();
1622 exit(0);
1623}
1624
1625//ll
Note: See TracBrowser for help on using the repository browser.