source: roaraudio/roard/roard.c @ 3008:edd998caccdc

Last change on this file since 3008:edd998caccdc was 2970:624ab1461d6b, checked in by phi, 15 years ago

added --verbose, configure new ROAR_INFO()

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