source: roaraudio/roard/roard.c @ 2783:b83359ac8107

Last change on this file since 2783:b83359ac8107 was 2767:4dd870914ef3, checked in by phi, 15 years ago

default on win32 is IP

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