source: roaraudio/roard/roard.c @ 2970:624ab1461d6b

Last change on this file since 2970:624ab1461d6b was 2970:624ab1461d6b, checked in by phi, 15 years ago

added --verbose, configure new ROAR_INFO()

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