source: roaraudio/roard/roard.c @ 2751:6cc76359d728

Last change on this file since 2751:6cc76359d728 was 2751:6cc76359d728, checked in by phi, 15 years ago

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