source: roaraudio/roard/roard.c @ 2737:5d3d02d2b38b

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

typo

File size: 42.9 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 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
839 char * sock_user = NULL;
840#ifdef ROAR_SUPPORT_LISTEN
841 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
842#endif
843#ifdef ROAR_HAVE_LIBSLP
844 int    reg_slp   = 0;
845#endif
846#ifdef ROAR_HAVE_CHROOT
847 char * chrootdir = NULL;
848#endif
849#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
850 struct group   * grp  = NULL;
851#endif
852#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
853 struct passwd  * pwd  = NULL;
854#endif
855#ifdef ROAR_HAVE_GETSERVBYNAME
856 struct servent * serv = NULL;
857#endif
858 DRIVER_USERDATA_T drvinst;
859 struct roar_client * self = NULL;
860#ifdef ROAR_HAVE_LIBDNET
861 char decnethost[80];
862#endif
863#ifdef SUPPORT_PIDFILE
864 struct roar_vio_calls pidfile_vio;
865#endif
866
867 g_standby       =  0;
868 g_autostandby   =  0;
869 alive           =  1;
870#ifdef ROAR_SUPPORT_LISTEN
871 g_no_listen     =  0;
872#else
873 g_terminate     =  1;
874#endif
875
876 sa.bits     = ROAR_BITS_DEFAULT;
877 sa.channels = ROAR_CHANNELS_DEFAULT;
878 sa.rate     = ROAR_RATE_DEFAULT;
879 sa.codec    = ROAR_CODEC_DEFAULT;
880
881 g_sa        = &sa;
882 g_max_sa    = &max_sa;
883
884 memcpy(g_max_sa, g_sa, sizeof(max_sa));
885
886 g_config = &config;
887
888 if ( init_config() == -1 ) {
889  ROAR_ERR("Can not init default config!");
890  return 1;
891 }
892
893#ifdef ROAR_SUPPORT_LISTEN
894 if ( init_listening() == -1 ) {
895  ROAR_ERR("Can not init listening sockets!");
896  return 1;
897 }
898#endif
899
900#ifndef ROAR_WITHOUT_DCOMP_MIDI
901 if ( midi_init_config() == -1 ) {
902  ROAR_ERR("Can not init MIDI config!");
903  return 1;
904 }
905#endif
906
907#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
908 if ( ssynth_init_config() == -1 ) {
909  ROAR_ERR("Can not init ssynth config!");
910  return 1;
911 }
912#endif
913
914#ifndef ROAR_WITHOUT_DCOMP_RDTCS
915 if ( rdtcs_init_config() == -1 ) {
916  ROAR_ERR("Can not init RDTCS config!");
917  return 1;
918 }
919#endif
920
921#ifdef ROAR_SUPPORT_LISTEN
922 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
923#ifdef ROAR_HAVE_GETUID
924 if ( getuid() != 0 && getenv("HOME") != NULL ) {
925  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
926  sock_addr = user_sock;
927 }
928#endif
929
930 if ( getenv("ROAR_SERVER") != NULL )
931  sock_addr = getenv("ROAR_SERVER");
932#endif
933
934 if ( clients_init() == -1 ) {
935  ROAR_ERR("Can not init clients!");
936  return 1;
937 }
938
939 if ( streams_init() == -1 ) {
940  ROAR_ERR("Can not init streams!");
941  return 1;
942 }
943
944 if ( (g_self_client = clients_new()) == -1 ) {
945  ROAR_ERR("Can not create self client!");
946  return 1;
947 }
948
949#ifndef ROAR_WITHOUT_DCOMP_SOURCES
950 if ( sources_init() == -1 ) {
951  ROAR_ERR("Can not init sources!");
952  return 1;
953 }
954
955 if ( (sources_set_client(g_self_client)) == -1 ) {
956  ROAR_ERR("Can not init set source client!");
957  return 1;
958 }
959#endif
960
961#ifdef ROAR_HAVE_MAIN_ARGS
962 for (i = 1; i < argc; i++) {
963  k = argv[i];
964
965  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
966   usage();
967   return 0;
968
969  } else if ( strcmp(k, "--start") == 0 ) {
970   // this is a no op
971  } else if ( strcmp(k, "--restart") == 0 ) {
972#ifdef ROAR_SUPPORT_LISTEN
973   if ( restart_server(sock_addr, 1) == -1 ) {
974    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", sock_addr);
975   }
976#else
977   ROAR_ERR("--restart not supported");
978#endif
979  } else if ( strcmp(k, "--shutdown") == 0 ) {
980#ifdef ROAR_SUPPORT_LISTEN
981   if ( restart_server(sock_addr, 1) == -1 ) {
982    ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
983    return 1;
984   }
985   return 0;
986#else
987   ROAR_ERR("--shutdown not supported");
988   return 1;
989#endif
990  } else if ( strcmp(k, "--stop") == 0 ) {
991#ifdef ROAR_SUPPORT_LISTEN
992   if ( restart_server(sock_addr, 0) == -1 ) {
993    ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
994    return 1;
995   }
996   return 0;
997#else
998   ROAR_ERR("--stop not supported");
999   return 1;
1000#endif
1001
1002
1003  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
1004#ifdef ROAR_HAVE_FORK
1005   daemon = 1;
1006#else
1007   ROAR_ERR("--daemon not supported");
1008#endif
1009  } else if ( strcmp(k, "--terminate") == 0 ) {
1010   g_terminate = 1;
1011  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1012   sysclocksync = 1000;
1013  } else if ( strcmp(k, "--realtime") == 0 ) {
1014   realtime++;
1015  } else if ( strcmp(k, "--chroot") == 0 ) {
1016#ifdef ROAR_HAVE_CHROOT
1017   chrootdir = argv[++i];
1018#else
1019   ROAR_ERR("--chroot not supported");
1020   i++;
1021#endif
1022  } else if ( strcmp(k, "--setgid") == 0 ) {
1023#ifdef ROAR_HAVE_SETGID
1024   setids |= R_SETGID;
1025#else
1026   ROAR_ERR("--setgid not supported");
1027#endif
1028  } else if ( strcmp(k, "--setuid") == 0 ) {
1029#ifdef ROAR_HAVE_SETUID
1030   setids |= R_SETUID;
1031#else
1032   ROAR_ERR("--setuid not supported");
1033#endif
1034  } else if ( strcmp(k, "--location") == 0 ) {
1035   g_config->location = argv[++i];
1036  } else if ( strcmp(k, "--pidfile") == 0 ) {
1037#ifdef SUPPORT_PIDFILE
1038   pidfile = argv[++i];
1039#else
1040   ROAR_ERR("--pidfile not supported");
1041   i++;
1042#endif
1043
1044  } else if ( strcmp(k, "--list-cf") == 0 ) {
1045   print_codecfilterlist();
1046   return 0;
1047
1048  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
1049   sa.rate = atoi(argv[++i]);
1050  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
1051   sa.bits = atoi(argv[++i]);
1052  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
1053   sa.channels = atoi(argv[++i]);
1054
1055  } else if ( strcmp(k, "--stream-flags") == 0 ) {
1056   if ( update_stream_flags(argv[++i]) == -1 ) {
1057    ROAR_ERR("Can not set stream flags");
1058    return 1;
1059   }
1060
1061  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
1062   driver = argv[++i];
1063   if ( strcmp(driver, "list") == 0 ) {
1064    ROAR_WARN("The option is obsolete, use --list-driver!");
1065    print_driverlist();
1066    return 0;
1067   }
1068  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
1069   device = argv[++i];
1070  } else if ( strcmp(k, "-dO") == 0 ) {
1071   opts = argv[++i];
1072  } else if ( strcmp(k, "--list-driver") == 0 ) {
1073   print_driverlist();
1074   return 0;
1075
1076  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
1077   o_drv  = argv[++i];
1078  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
1079   o_dev  = argv[++i];
1080  } else if ( strcmp(k, "-oO") == 0 ) {
1081   o_opts = argv[++i];
1082  } else if ( strcmp(k, "-oP") == 0 ) {
1083   o_prim = 1;
1084  } else if ( strcmp(k, "-oN") == 0 ) {
1085   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1086    o_count++;
1087
1088   o_drv  = o_dev = o_opts = NULL;
1089   o_prim = 0;
1090
1091  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
1092#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1093   s_drv = argv[++i];
1094#else
1095   ROAR_ERR("main(*): No support for sources compiled in");
1096#endif
1097  } else if ( strcmp(k, "-S") == 0 ) {
1098#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1099   s_dev = argv[++i];
1100#else
1101   ROAR_ERR("main(*): No support for sources compiled in");
1102#endif
1103  } else if ( strcmp(k, "-sO") == 0 ) {
1104#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1105   s_opt = argv[++i];
1106#else
1107   ROAR_ERR("main(*): No support for sources compiled in");
1108#endif
1109  } else if ( strcmp(k, "-sC") == 0 ) {
1110#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1111   s_con = argv[++i];
1112#else
1113   ROAR_ERR("main(*): No support for sources compiled in");
1114#endif
1115  } else if ( strcmp(k, "-sP") == 0 ) {
1116#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1117   s_prim = 1;
1118#else
1119   ROAR_ERR("main(*): No support for sources compiled in");
1120#endif
1121  } else if ( strcmp(k, "-sN") == 0 ) {
1122#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1123   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1124    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1125   }
1126   s_opt = s_dev = s_con = NULL;
1127   s_drv = "cf";
1128   s_prim = 0;
1129#else
1130   ROAR_ERR("main(*): No support for sources compiled in");
1131#endif
1132  } else if ( strcmp(k, "--list-sources") == 0 ) {
1133#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1134   print_sourcelist();
1135   return 0;
1136#else
1137   ROAR_ERR("main(*): No support for sources compiled in");
1138   return 1;
1139#endif
1140
1141  } else if ( strcmp(k, "--light-channels") == 0 ) {
1142#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1143   light_channels = atoi(argv[++i]);
1144#else
1145   ROAR_WARN("main(*): no light subsystem compiled in");
1146#endif
1147
1148  } else if ( strcmp(k, "--rds-pi") == 0 ) {
1149#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1150   g_rdtcs.rds.pi = atoi(argv[++i]);
1151#else
1152   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1153#endif
1154  } else if ( strcmp(k, "--rds-ps") == 0 ) {
1155#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1156   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
1157    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]);
1158    return 1;
1159   }
1160#else
1161   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1162#endif
1163  } else if ( strcmp(k, "--rds-pty") == 0 ) {
1164#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1165   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
1166    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
1167    return 1;
1168   }
1169#else
1170   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1171#endif
1172  } else if ( strcmp(k, "--rds-tp") == 0 ) {
1173#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1174   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) {
1175    ROAR_ERR("Can not set RDS TP flag");
1176    return 1;
1177   }
1178#else
1179   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1180#endif
1181  } else if ( strcmp(k, "--rds-ct") == 0 ) {
1182#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1183   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) {
1184    ROAR_ERR("Can not set RDS CT flag");
1185    return 1;
1186   }
1187#else
1188   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1189#endif
1190
1191
1192  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
1193#ifndef ROAR_WITHOUT_DCOMP_CB
1194   midi_config.init_cb = 0;
1195#else
1196   // no warning here as this is the disable option
1197#endif
1198  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
1199#ifndef ROAR_WITHOUT_DCOMP_CB
1200   midi_config.init_cb = 1;
1201#else
1202   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1203#endif
1204  } else if ( strcmp(k, "--midi-console") == 0 ) {
1205#ifndef ROAR_WITHOUT_DCOMP_CB
1206   midi_config.console_dev = argv[++i];
1207   midi_config.init_cb = 1;
1208#else
1209   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1210#endif
1211
1212  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
1213#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1214   ssynth_conf.enable = 1;
1215#else
1216   ROAR_ERR("main(*): No support for ssynth compiled in");
1217#endif
1218  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
1219#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1220   ssynth_conf.enable = 0;
1221#else
1222   // we can safely ignore the disable
1223#endif
1224
1225  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
1226   // This is only usefull in INET not UNIX mode.
1227#ifdef ROAR_SUPPORT_LISTEN
1228   if ( *sock_addr == '/' )
1229    sock_addr = ROAR_DEFAULT_HOST;
1230
1231   errno = 0;
1232   if ( (port = atoi(argv[++i])) < 1 ) {
1233#ifdef ROAR_HAVE_GETSERVBYNAME
1234    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
1235     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
1236     return 1;
1237    }
1238    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
1239    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
1240            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
1241    port = ROAR_NET2HOST16(serv->s_port);
1242#else
1243    ROAR_ERR("invalite port number: %s", argv[i]);
1244    return 1;
1245#endif
1246   }
1247#endif
1248  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
1249#ifdef ROAR_SUPPORT_LISTEN
1250   sock_addr = argv[++i];
1251#endif
1252
1253  } else if ( strcmp(k, "--proto") == 0 ) {
1254#ifdef ROAR_SUPPORT_LISTEN
1255   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
1256    ROAR_ERR("Unknown protocol: %s", argv[i]);
1257    return 1;
1258   }
1259#endif
1260
1261  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
1262#ifdef ROAR_SUPPORT_LISTEN
1263   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
1264    sock_type = ROAR_SOCKET_TYPE_TCP;
1265
1266   if ( *sock_addr == '/' )
1267    sock_addr = ROAR_DEFAULT_HOST;
1268#endif
1269
1270  } else if ( strcmp(k, "-4") == 0 ) {
1271#ifdef ROAR_SUPPORT_LISTEN
1272   sock_type = ROAR_SOCKET_TYPE_TCP;
1273   if ( *sock_addr == '/' )
1274    sock_addr = ROAR_DEFAULT_HOST;
1275#endif
1276  } else if ( strcmp(k, "-6") == 0 ) {
1277#ifdef ROAR_SUPPORT_LISTEN
1278#ifdef PF_INET6
1279   sock_type = ROAR_SOCKET_TYPE_TCP6;
1280   if ( *sock_addr == '/' )
1281    sock_addr = ROAR_DEFAULT_HOST;
1282#else
1283    ROAR_ERR("No IPv6 support compiled in!");
1284    return 1;
1285#endif
1286#endif
1287
1288  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
1289#ifdef ROAR_SUPPORT_LISTEN
1290   // ignore this case as it is the default behavor.
1291   sock_type = ROAR_SOCKET_TYPE_UNIX;
1292#endif
1293
1294  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
1295#ifdef ROAR_SUPPORT_LISTEN
1296#ifdef ROAR_HAVE_LIBDNET
1297    port   = ROAR_DEFAULT_NUM;
1298    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
1299    sock_addr = decnethost;
1300    sock_type = ROAR_SOCKET_TYPE_DECNET;
1301#else
1302    ROAR_ERR("No DECnet support compiled in!");
1303    return 1;
1304#endif
1305#endif
1306  } else if ( strcmp(k, "--new-sock") == 0 ) {
1307#ifdef ROAR_SUPPORT_LISTEN
1308   if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto) != 0 ) {
1309    ROAR_ERR("Can not open listen socket!");
1310    return 1;
1311   }
1312#endif
1313
1314  } else if ( strcmp(k, "--slp") == 0 ) {
1315#ifdef ROAR_HAVE_LIBSLP
1316   reg_slp = 1;
1317#else
1318    ROAR_ERR("No OpenSLP support compiled in!");
1319    return 1;
1320#endif
1321
1322  } else if ( strcmp(k, "-G") == 0 ) {
1323   sock_grp  = argv[++i];
1324  } else if ( strcmp(k, "-U") == 0 ) {
1325   sock_user = argv[++i];
1326
1327  } else if ( strcmp(k, "--no-listen") == 0 ) {
1328#ifdef ROAR_SUPPORT_LISTEN
1329   sock_addr   = "";
1330   g_terminate = 1;
1331   g_no_listen = 1;
1332#endif
1333  } else if ( strcmp(k, "--client-fh") == 0 ) {
1334   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
1335    ROAR_ERR("main(*): Can not set client's fh");
1336    return 1;
1337   }
1338  } else if ( strcmp(k, "--close-fh") == 0 ) {
1339#ifdef ROAR_HAVE_IO_POSIX
1340   close(atoi(argv[++i]));
1341#else
1342   i++;
1343   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
1344#endif
1345
1346  } else if ( strcmp(k, "--standby") == 0 ) {
1347   g_standby = 1;
1348  } else if ( strcmp(k, "--auto-standby") == 0 ) {
1349   g_autostandby = 1;
1350  } else {
1351   usage();
1352   return 1;
1353  }
1354
1355 }
1356#endif
1357
1358#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1359 if ( s_dev != NULL ) {
1360  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1361   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1362  }
1363 }
1364#endif
1365
1366 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
1367
1368 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
1369
1370#ifndef ROAR_WITHOUT_DCOMP_MIDI
1371 if ( midi_init() == -1 ) {
1372  ROAR_ERR("Can not initialize MIDI subsystem");
1373 }
1374#endif
1375
1376#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1377 if ( ssynth_init() == -1 ) {
1378  ROAR_ERR("Can not initialize ssynth subsystem");
1379 }
1380#endif
1381
1382#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1383 if ( light_init(light_channels) == -1 ) {
1384  ROAR_ERR("Can not initialize light control subsystem");
1385 }
1386#endif
1387
1388#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1389 if ( rdtcs_init() == -1 ) {
1390  ROAR_ERR("Can not initialize RDTCS subsystem");
1391 }
1392#endif
1393
1394#ifdef ROAR_SUPPORT_LISTEN
1395 if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto) != 0 ) {
1396  ROAR_ERR("Can not open listen socket!");
1397  return 1;
1398 }
1399#endif
1400
1401 if ( output_buffer_init(&sa) == -1 ) {
1402  ROAR_ERR("Can not init output buffer!");
1403  return 1;
1404 }
1405
1406 if ( driver == NULL ) {
1407  driver = "null";
1408 } else {
1409  ROAR_WARN("Usage of old driver interface. use -o not -d!");
1410 }
1411
1412 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
1413  ROAR_ERR("Can not open output driver!");
1414  return 1;
1415 }
1416
1417 if ( samples_init() == -1 ) {
1418  ROAR_ERR("Can not init samples!");
1419  return 1;
1420 }
1421
1422
1423 // we should handle this on microcontrollers, too.
1424#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
1425 signal(SIGINT,  on_sig_int);
1426 signal(SIGTERM, on_sig_term);
1427 signal(SIGCHLD, on_sig_chld);
1428 signal(SIGUSR1, on_sig_usr1);
1429 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
1430#endif
1431
1432 if ( realtime ) {
1433#ifdef DEBUG
1434  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
1435#endif
1436
1437#ifdef ROAR_HAVE_NICE
1438  errno = 0;
1439  nice(-5*realtime); // -5 for each --realtime
1440  if ( errno ) {
1441   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1442  }
1443#else
1444  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1445#endif
1446/*
1447#ifdef __linux__
1448  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
1449   ROAR_WARN("Can not set io priority: %s", strerror(errno));
1450#endif
1451*/
1452 }
1453
1454#ifdef ROAR_HAVE_SETGID
1455 if ( setids & R_SETGID ) {
1456  if ( (grp = getgrnam(sock_grp)) == NULL ) {
1457   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
1458   return 1;
1459  }
1460  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
1461   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
1462  }
1463  if ( !grp || setgid(grp->gr_gid) == -1 ) {
1464   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
1465  }
1466 }
1467#endif
1468
1469
1470 clients_set_pid(g_self_client, getpid());
1471#ifdef ROAR_HAVE_GETUID
1472 clients_set_uid(g_self_client, getuid());
1473#endif
1474#ifdef ROAR_HAVE_GETGID
1475 clients_set_gid(g_self_client, getgid());
1476#endif
1477 clients_get(g_self_client, &self);
1478
1479 if ( self == NULL ) {
1480  ROAR_ERR("Can not get self client!");
1481  return 1;
1482 }
1483
1484 strcpy(self->name, "RoarAudio daemon internal");
1485
1486#ifdef ROAR_HAVE_FORK
1487 if ( daemon ) {
1488  close(ROAR_STDIN );
1489  close(ROAR_STDOUT);
1490  close(ROAR_STDERR);
1491
1492  if ( fork() )
1493   ROAR_U_EXIT(0);
1494
1495#ifdef ROAR_HAVE_SETSID
1496  setsid();
1497#endif
1498  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
1499 }
1500#endif
1501
1502#ifdef SUPPORT_PIDFILE
1503 if ( pidfile != NULL ) {
1504  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
1505   ROAR_ERR("Can not write pidfile: %s", pidfile);
1506  } else {
1507   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
1508   roar_vio_close(&pidfile_vio);
1509  }
1510 }
1511#endif
1512
1513#ifdef ROAR_HAVE_CHROOT
1514 if (chrootdir) {
1515  if ( chroot(chrootdir) == -1 ) {
1516   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
1517   return 2;
1518  }
1519  if ( chdir("/") == -1 ) {
1520   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
1521   return 2;
1522  }
1523 }
1524#endif
1525
1526#ifdef ROAR_HAVE_SETUID
1527 if ( setids & R_SETUID ) {
1528  if ( (pwd = getpwnam(sock_user)) == NULL ) {
1529   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
1530   return 1;
1531  }
1532  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
1533   ROAR_ERR("Can not set UserID: %s", strerror(errno));
1534   return 3;
1535  }
1536#ifdef ROAR_HAVE_GETUID
1537  clients_set_uid(g_self_client, getuid());
1538#endif
1539 }
1540#endif
1541
1542 // Register with OpenSLP:
1543#ifdef ROAR_HAVE_LIBSLP
1544 if ( reg_slp ) {
1545  register_slp(0, sock_addr);
1546 }
1547#endif
1548
1549 // start main loop...
1550 main_loop(drvid, drvinst, &sa, sysclocksync);
1551
1552 // clean up.
1553 clean_quit_prep();
1554 driver_close(drvinst, drvid);
1555 output_buffer_free();
1556
1557 return 0;
1558}
1559
1560void cleanup_listen_socket (int terminate) {
1561 int i;
1562
1563 // Deregister from SLP:
1564#ifdef ROAR_HAVE_LIBSLP
1565 register_slp(1, NULL);
1566#endif
1567
1568#ifdef ROAR_SUPPORT_LISTEN
1569 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
1570  if ( g_listen_socket[i] != -1 ) {
1571#ifdef ROAR_HAVE_IO_POSIX
1572   close(g_listen_socket[i]);
1573#endif // #else is useless because we are in void context.
1574
1575   g_listen_socket[i] = -1;
1576
1577#ifdef ROAR_HAVE_UNIX
1578   if ( server[i] != NULL )
1579    if ( *(server[i]) == '/' )
1580     unlink(server[i]);
1581#endif
1582  }
1583 }
1584
1585#endif
1586
1587 if ( terminate )
1588  g_terminate = 1;
1589}
1590
1591void clean_quit_prep (void) {
1592 cleanup_listen_socket(0);
1593
1594#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1595 sources_free();
1596#endif
1597 streams_free();
1598 clients_free();
1599#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1600 ssynth_free();
1601#endif
1602#ifndef ROAR_WITHOUT_DCOMP_CB
1603 midi_cb_stop(); // stop console beep
1604#endif
1605#ifndef ROAR_WITHOUT_DCOMP_MIDI
1606 midi_free();
1607#endif
1608#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1609 light_free();
1610#endif
1611#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1612 rdtcs_free();
1613#endif
1614
1615#ifdef SUPPORT_PIDFILE
1616 if ( pidfile != NULL )
1617  unlink(pidfile);
1618#endif
1619}
1620
1621void clean_quit (void) {
1622 clean_quit_prep();
1623// driver_close(drvinst, drvid);
1624// output_buffer_free();
1625 exit(0);
1626}
1627
1628//ll
Note: See TracBrowser for help on using the repository browser.