source: roaraudio/roard/roard.c @ 2524:fb139fa9dfb9

Last change on this file since 2524:fb139fa9dfb9 was 2511:e5f52519866f, checked in by phi, 15 years ago

much better defaulting for diffrent subsystems

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