source: roaraudio/roard/roard.c @ 3041:5455976f2e01

Last change on this file since 3041:5455976f2e01 was 3041:5455976f2e01, checked in by phi, 14 years ago

better error messages even on non-unix systems

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