source: roaraudio/roard/roard.c @ 2725:acd2b121bd92

Last change on this file since 2725:acd2b121bd92 was 2725:acd2b121bd92, checked in by phi, 15 years ago

added --rds-pi, list of other options

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