source: roaraudio/roard/roard.c @ 2753:4ba2efb7920b

Last change on this file since 2753:4ba2efb7920b was 2753:4ba2efb7920b, checked in by phi, 15 years ago

make add_listen() independed of audio group

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