source: roaraudio/roard/roard.c @ 2785:2171e2547d63

Last change on this file since 2785:2171e2547d63 was 2785:2171e2547d63, checked in by phi, 15 years ago

more debug lions

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