source: roaraudio/roard/roard.c @ 2500:5e481907a8c0

Last change on this file since 2500:5e481907a8c0 was 2500:5e481907a8c0, checked in by phi, 15 years ago

support to disable MIDI subsystem completly

File size: 37.0 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
[938]301 int codec;
[1926]302 int sync = 0, f_mmap = 0;
[1522]303 int32_t blocks = -1, blocksize = -1;
[1920]304 int dir = ROAR_DIR_OUTPUT;
[1991]305 int error = 0;
306 // DMX:
307 int32_t channel  = -1;
308 int32_t universe = -1;
309 uint16_t tu16;
[2085]310 float q = -32e6;
[933]311
[938]312 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
[933]313
[1145]314 if ( drv == NULL && count == 0 ) {
315  drv  = ROAR_DRIVER_DEFAULT;
316  prim = 1;
317  sync = 1;
[1208]318
319#ifdef ROAR_DRIVER_CODEC
320  if ( opts == NULL ) {
321   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
322  }
323#endif
[1145]324 }
325
[1227]326 if ( opts == NULL && count == 0 ) {
327  sync = 1;
328  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
329 }
330
[933]331 if ( (stream = streams_new()) == -1 ) {
[938]332  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]333  if ( prim ) alive = 0;
[933]334  return -1;
335 }
336
337 streams_get(stream, &ss);
338 s = ROAR_STREAM(ss);
339
340 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
341
342 s->pos_rel_id = -1;
343// s->info.codec = codec;
344
[938]345 codec = s->info.codec;
346
347 k = strtok(opts, ",");
348 while (k != NULL) {
349//  ROAR_WARN("add_output(*): opts: %s", k);
350
351  if ( (v = strstr(k, "=")) != NULL ) {
352   *v++ = 0;
353  }
354
355  ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v);
356  if ( strcmp(k, "rate") == 0 ) {
357   s->info.rate = atoi(v);
358  } else if ( strcmp(k, "channels") == 0 ) {
359   s->info.channels = atoi(v);
360  } else if ( strcmp(k, "bits") == 0 ) {
361   s->info.bits = atoi(v);
362  } else if ( strcmp(k, "codec") == 0 ) {
363   if ( (codec = roar_str2codec(v)) == -1 ) {
364    ROAR_ERR("add_output(*): unknown codec '%s'", v);
[1991]365    error++;
[938]366   }
[2085]367  } else if ( strcmp(k, "q") == 0 ) {
368   q = atof(v);
[1221]369  } else if ( strcmp(k, "blocks") == 0 ) {
370   blocks = atoi(v);
[1522]371  } else if ( strcmp(k, "blocksize") == 0 ) {
372   blocksize = atoi(v);
[1926]373  } else if ( strcmp(k, "mmap") == 0 ) {
374   f_mmap = 1;
[1919]375  } else if ( strcmp(k, "subsystem") == 0 ) {
376   if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) {
[1920]377    dir = ROAR_DIR_OUTPUT;
[2494]378#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1919]379   } else if ( !strcasecmp(v, "midi") ) {
380    dir = ROAR_DIR_MIDI_OUT;
[2494]381#endif
382#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1919]383   } else if ( !strcasecmp(v, "light") ) {
384    dir = ROAR_DIR_LIGHT_OUT;
[2494]385#endif
386#ifndef ROAR_WITHOUT_DCOMP_RAW
[2234]387   } else if ( !strcasecmp(v, "raw") ) {
388    dir = ROAR_DIR_RAW_OUT;
[2494]389#endif
[1919]390   } else {
[2494]391    ROAR_ERR("add_output(*): unknown/unsupported subsystem '%s'", k);
[1991]392    error++;
393   }
394  // DMX:
395  } else if ( strcmp(k, "channel") == 0 ) {
396   channel  = atoi(v);
397   if ( channel < 0 || channel > 65535 ) {
398    ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel);
399    channel = -1;
400    error++;
[1919]401   }
[1991]402  } else if ( strcmp(k, "universe") == 0 ) {
403   universe = atoi(v);
404   if ( universe < 0 || universe > 255 ) {
405    ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe);
406    universe = -1;
407    error++;
408   }
409
[2115]410  } else if ( strcmp(k, "name") == 0 ) {
411   if ( streams_set_name(stream, v) == -1 ) {
412    ROAR_ERR("add_output(*): Can not set Stream name");
413    error++;
414   }
[1991]415
[1032]416  } else if ( strcmp(k, "meta") == 0 ) {
417   streams_set_flag(stream, ROAR_FLAG_META);
[1117]418  } else if ( strcmp(k, "sync") == 0 ) {
419   sync = 1;
[1221]420  } else if ( strcmp(k, "primary") == 0 ) {
421   prim = 1;
422
423  } else if ( strcmp(k, "cleanmeta") == 0 ) {
424   streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
425  } else if ( strcmp(k, "autoconf") == 0 ) {
[1531]426   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
[2413]427  } else if ( strcmp(k, "recsource") == 0 ) {
428   streams_set_flag(stream, ROAR_FLAG_RECSOURCE);
429  } else if ( strcmp(k, "passmixer") == 0 ) {
430   streams_set_flag(stream, ROAR_FLAG_PASSMIXER);
[938]431  } else {
432   ROAR_ERR("add_output(*): unknown option '%s'", k);
[1991]433   error++;
434  }
435
436  if ( error ) {
[938]437   streams_delete(stream);
[982]438   if ( prim ) alive = 0;
[1208]439#ifdef ROAR_DRIVER_CODEC
440   if ( to_free != NULL )
441    free(to_free);
442#endif
[938]443   return -1;
444  }
445
446  k = strtok(NULL, ",");
447 }
448
[1919]449 if ( streams_set_dir(stream, dir, 1) == -1 ) {
450  streams_delete(stream);
451  return -1;
452 }
453
[1208]454#ifdef ROAR_DRIVER_CODEC
455 if ( to_free != NULL )
456  free(to_free);
457#endif
458
[941]459 if ( codec == ROAR_CODEC_ALAW || codec == ROAR_CODEC_MULAW )
460  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
461
[938]462 s->info.codec = codec;
463 ROAR_STREAM_SERVER(s)->codec_orgi = codec;
[933]464
[2364]465 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) {
[1581]466  ss->driver_id = -1; // don't close a driver not opened...
467  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
[933]468  streams_delete(stream);
[938]469  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]470  if ( prim ) alive = 0;
[933]471  return -1;
472 }
473
[1528]474 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here
475 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM,   s); // ignore errors here
476
[1221]477 if ( blocks != -1 )
478  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
479
[1522]480 if ( blocksize != -1 )
481  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
482
[1991]483 // TODO: we shoudld *really* check for errors here...
484 if ( channel != -1 ) {
485  tu16 = channel;
486  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16);
487 }
488 if ( universe != -1 ) {
489  tu16 = universe;
490  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16);
491 }
492
[1156]493 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
494
[938]495 streams_set_fh(stream, -1); // update some internal structures
496
[2087]497 if ( q > -1e6 ) {
498  ROAR_DBG("add_output(*): setting q=%f", q);
499  streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q);
500 }
501
[2490]502 client_stream_add(g_self_client, stream);
[933]503
[1200]504 if ( prim ) {
[961]505  streams_mark_primary(stream);
[1200]506  s->pos_rel_id = stream;
507 }
[961]508
[1120]509 if ( sync ) {
[1117]510  streams_set_flag(stream, ROAR_FLAG_SYNC);
[1120]511 } else {
512  streams_reset_flag(stream, ROAR_FLAG_SYNC);
513 }
[1117]514
[1926]515 if ( f_mmap )
516  streams_set_flag(stream, ROAR_FLAG_MMAP);
517
[933]518 return 0;
[932]519}
520
[1993]521
522// SLP:
523void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
524 /* return the error code in the cookie */
525 *(SLPError*)cookie = errcode;
526}
527
528int register_slp (int unreg, char * sockname) {
529#ifdef ROAR_HAVE_LIBSLP
530 static int regged = 0;
531 static char * sn = NULL;
532 SLPError err;
533 SLPError callbackerr;
534 SLPHandle hslp;
535 char addr[1024];
536 char attr[1024] = "";
[2017]537 char * location;
[1993]538
539 if ( sockname != NULL )
540  sn = sockname;
541
[2016]542 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE "://%s", sn);
[1993]543
544 err = SLPOpen("en", SLP_FALSE, &hslp);
545
546 if (err != SLP_OK) {
547  ROAR_ERR("Error opening slp handle: Error #%i", err);
548  return -1;
549 }
550
551 if (!unreg) {
[2017]552
553  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
554   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
555   SLPClose(hslp);
556   return -1;
557  }
558
[1995]559  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
[2494]560#ifndef ROAR_WITHOUT_DCOMP_LIGHT
561                               "(light-channels=%i),"
562#endif
563                               "(location=%s)",
[1995]564           g_sa->rate, g_sa->channels, g_sa->bits,
[2494]565#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[2017]566           g_light_state.channels,
[2494]567#endif
[2017]568           location
[1995]569          );
570
[1993]571  /* Register a service with SLP */
572  err = SLPReg(hslp,
573               addr,
574               SLP_LIFETIME_MAXIMUM,
575               0,
576               attr,
577               SLP_TRUE,
578               register_slp_callback,
579               &callbackerr);
[1994]580  regged = 1;
581 } else if ( unreg && regged ) {
582  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
583  regged = 0;
584 } else {
585  SLPClose(hslp);
586  return -1;
587 }
[1993]588
[1994]589 /* err may contain an error code that occurred as the slp library    */
590 /* _prepared_ to make the call.                                     */
591 if ( err != SLP_OK ) {
592  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
593  return -1;
594 }
595
596 /* callbackerr may contain an error code (that was assigned through */
597 /* the callback cookie) that occurred as slp packets were sent on    */
598 /* the wire */
599 if (callbackerr != SLP_OK) {
600  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
601  return -1;
[1993]602 }
603
604 SLPClose(hslp);
605 return 0;
606#else
607 return -1;
608#endif
609}
610
611
612// MAIN:
613
[1503]614#ifdef ROAR_HAVE_MAIN_ARGS
[0]615int main (int argc, char * argv[]) {
[1503]616#else
617int main (void) {
618#endif
619#ifdef ROAR_HAVE_MAIN_ARGS
[0]620 int i;
621 char * k;
[1503]622#endif
[1494]623#ifdef ROAR_SUPPORT_LISTEN
[905]624 char user_sock[80]  = {0};
[1494]625#endif
[1609]626 struct roar_audio_info sa, max_sa;
627 struct roard_config config;
[1486]628#ifdef ROAR_HAVE_FORK
[905]629 int    daemon       = 0;
[1486]630#endif
[905]631 int    realtime     = 0;
632 int    sysclocksync = 0;
[1207]633 char * driver    = NULL;
634 char * device    = NULL;
[1503]635#ifdef ROAR_HAVE_MAIN_ARGS
[1207]636 char * opts      = NULL;
[1503]637#endif
[60]638// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
[1494]639#ifdef ROAR_SUPPORT_LISTEN
[1207]640 int      port    = ROAR_DEFAULT_PORT;
[1494]641#endif
[0]642 int               drvid;
[2486]643#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]644 char * s_drv     = "cf";
[1110]645 char * s_dev     = NULL;
[444]646 char * s_con     = NULL;
647 char * s_opt     = NULL;
648 int    s_prim    = 0;
[2486]649#endif
[1207]650 char * o_drv     = getenv("ROAR_DRIVER");
651 char * o_dev     = getenv("ROAR_DEVICE");
[932]652 char * o_opts    = NULL;
[961]653 int    o_prim    = 0;
[1145]654 int    o_count   = 0;
[2494]655#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]656 int    light_channels = LIGHT_CHANNELS_DEFAULT;
[2494]657#endif
[450]658 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
[444]659 char * sock_user = NULL;
[1494]660#ifdef ROAR_SUPPORT_LISTEN
[517]661 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
[1494]662#endif
[1993]663#ifdef ROAR_HAVE_LIBSLP
664 int    reg_slp   = 0;
665#endif
[1486]666#ifdef ROAR_HAVE_CHROOT
[444]667 char * chrootdir = NULL;
[1486]668#endif
669#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
[444]670 int    setids    = 0;
[1486]671#endif
672#ifdef ROAR_HAVE_UNIX
[1011]673 char * env_roar_proxy_backup;
[1486]674#endif
675#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[446]676 struct group   * grp  = NULL;
[1486]677#endif
678#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[446]679 struct passwd  * pwd  = NULL;
[1486]680#endif
681#ifdef ROAR_HAVE_GETSERVBYNAME
[446]682 struct servent * serv = NULL;
[1486]683#endif
[0]684 DRIVER_USERDATA_T drvinst;
[39]685 struct roar_client * self = NULL;
[508]686#ifdef ROAR_HAVE_LIBDNET
687 char decnethost[80];
688#endif
[2104]689#ifdef SUPPORT_PIDFILE
690 struct roar_vio_calls pidfile_vio;
691#endif
[0]692
693 g_standby       =  0;
[920]694 g_autostandby   =  0;
[982]695 alive           =  1;
[1494]696#ifdef ROAR_SUPPORT_LISTEN
[1155]697 g_no_listen     =  0;
[1494]698 g_listen_socket = -1;
699#else
700 g_terminate     =  1;
701#endif
[0]702
703 sa.bits     = ROAR_BITS_DEFAULT;
704 sa.channels = ROAR_CHANNELS_DEFAULT;
705 sa.rate     = ROAR_RATE_DEFAULT;
706 sa.codec    = ROAR_CODEC_DEFAULT;
707
[1609]708 g_sa        = &sa;
709 g_max_sa    = &max_sa;
710
711 memcpy(g_max_sa, g_sa, sizeof(max_sa));
712
713 g_config = &config;
714
715 if ( init_config() == -1 ) {
716  ROAR_ERR("Can not init default config!");
717  return 1;
718 }
[0]719
[2500]720#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1924]721 if ( midi_init_config() == -1 ) {
722  ROAR_ERR("Can not init MIDI config!");
723  return 1;
724 }
[2500]725#endif
[60]726
[2489]727#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]728 if ( ssynth_init_config() == -1 ) {
729  ROAR_ERR("Can not init ssynth config!");
730  return 1;
731 }
[2489]732#endif
[2451]733
[1494]734#ifdef ROAR_SUPPORT_LISTEN
[1753]735#ifdef ROAR_HAVE_GETUID
[1486]736 if ( getuid() != 0 && getenv("HOME") != NULL ) {
737  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
[60]738  server = user_sock;
739 }
[1753]740#endif
[60]741
[279]742 if ( getenv("ROAR_SERVER") != NULL )
743  server = getenv("ROAR_SERVER");
[1494]744#endif
[279]745
[63]746 if ( clients_init() == -1 ) {
747  ROAR_ERR("Can not init clients!");
748  return 1;
749 }
750
751 if ( streams_init() == -1 ) {
752  ROAR_ERR("Can not init streams!");
753  return 1;
754 }
755
[64]756 if ( (g_self_client = clients_new()) == -1 ) {
757  ROAR_ERR("Can not create self client!");
758  return 1;
759 }
760
[2485]761#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]762 if ( sources_init() == -1 ) {
763  ROAR_ERR("Can not init sources!");
764  return 1;
765 }
766
[64]767 if ( (sources_set_client(g_self_client)) == -1 ) {
768  ROAR_ERR("Can not init set source client!");
769  return 1;
770 }
[2485]771#endif
[64]772
[1503]773#ifdef ROAR_HAVE_MAIN_ARGS
[0]774 for (i = 1; i < argc; i++) {
775  k = argv[i];
776
777  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
778   usage();
779   return 0;
780
[2109]781  } else if ( strcmp(k, "--start") == 0 ) {
782   // this is a no op
[579]783  } else if ( strcmp(k, "--restart") == 0 ) {
[1494]784#ifdef ROAR_SUPPORT_LISTEN
[2109]785   if ( restart_server(server, 1) == -1 ) {
[579]786    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server);
787   }
[1494]788#else
789   ROAR_ERR("--restart not supported");
790#endif
[2109]791  } else if ( strcmp(k, "--shutdown") == 0 ) {
792#ifdef ROAR_SUPPORT_LISTEN
793   if ( restart_server(server, 1) == -1 ) {
794    ROAR_WARN("Can not terminate old server (not running at %s?)", server);
795    return 1;
796   }
797   return 0;
798#else
799   ROAR_ERR("--shutdown not supported");
800   return 1;
801#endif
802  } else if ( strcmp(k, "--stop") == 0 ) {
803#ifdef ROAR_SUPPORT_LISTEN
[2111]804   if ( restart_server(server, 0) == -1 ) {
[2109]805    ROAR_WARN("Can not stop old server (not running at %s?)", server);
806    return 1;
807   }
808   return 0;
809#else
810   ROAR_ERR("--stop not supported");
811   return 1;
812#endif
813
[579]814
[775]815  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
[1486]816#ifdef ROAR_HAVE_FORK
[775]817   daemon = 1;
[1486]818#else
819   ROAR_ERR("--daemon not supported");
820#endif
[71]821  } else if ( strcmp(k, "--terminate") == 0 ) {
822   g_terminate = 1;
[905]823  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
824   sysclocksync = 1000;
[275]825  } else if ( strcmp(k, "--realtime") == 0 ) {
[276]826   realtime++;
[444]827  } else if ( strcmp(k, "--chroot") == 0 ) {
[1486]828#ifdef ROAR_HAVE_CHROOT
[444]829   chrootdir = argv[++i];
[1486]830#else
831   ROAR_ERR("--chroot not supported");
832   i++;
833#endif
[444]834  } else if ( strcmp(k, "--setgid") == 0 ) {
[1486]835#ifdef ROAR_HAVE_SETGID
[444]836   setids |= R_SETGID;
[1486]837#else
838   ROAR_ERR("--setgid not supported");
839#endif
[444]840  } else if ( strcmp(k, "--setuid") == 0 ) {
[1486]841#ifdef ROAR_HAVE_SETUID
[444]842   setids |= R_SETUID;
[1486]843#else
844   ROAR_ERR("--setuid not supported");
845#endif
[2017]846  } else if ( strcmp(k, "--location") == 0 ) {
847   g_config->location = argv[++i];
[2104]848  } else if ( strcmp(k, "--pidfile") == 0 ) {
849#ifdef SUPPORT_PIDFILE
850   pidfile = argv[++i];
851#else
852   ROAR_ERR("--pidfile not supported");
853   i++;
854#endif
[68]855
[280]856  } else if ( strcmp(k, "--list-cf") == 0 ) {
857   print_codecfilterlist();
858   return 0;
859
[0]860  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
861   sa.rate = atoi(argv[++i]);
862  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
863   sa.bits = atoi(argv[++i]);
864  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
865   sa.channels = atoi(argv[++i]);
866
[2339]867  } else if ( strcmp(k, "--stream-flags") == 0 ) {
868   if ( update_stream_flags(argv[++i]) == -1 ) {
869    ROAR_ERR("Can not set stream flags");
870    return 1;
871   }
872
[0]873  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
874   driver = argv[++i];
875   if ( strcmp(driver, "list") == 0 ) {
[974]876    ROAR_WARN("The option is obsolete, use --list-driver!");
[0]877    print_driverlist();
[973]878    return 0;
[0]879   }
880  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
881   device = argv[++i];
882  } else if ( strcmp(k, "-dO") == 0 ) {
883   opts = argv[++i];
[973]884  } else if ( strcmp(k, "--list-driver") == 0 ) {
885   print_driverlist();
886   return 0;
[0]887
[932]888  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
889   o_drv  = argv[++i];
890  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
891   o_dev  = argv[++i];
892  } else if ( strcmp(k, "-oO") == 0 ) {
893   o_opts = argv[++i];
[961]894  } else if ( strcmp(k, "-oP") == 0 ) {
895   o_prim = 1;
[932]896  } else if ( strcmp(k, "-oN") == 0 ) {
[1145]897   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
898    o_count++;
899
[961]900   o_drv  = o_dev = o_opts = NULL;
901   o_prim = 0;
[932]902
[0]903  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
[2485]904#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]905   s_drv = argv[++i];
[2485]906#else
907   ROAR_ERR("main(*): No support for sources compiled in");
908#endif
[550]909  } else if ( strcmp(k, "-S") == 0 ) {
[2485]910#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]911   s_dev = argv[++i];
[2485]912#else
913   ROAR_ERR("main(*): No support for sources compiled in");
914#endif
[0]915  } else if ( strcmp(k, "-sO") == 0 ) {
[2485]916#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]917   s_opt = argv[++i];
[2485]918#else
919   ROAR_ERR("main(*): No support for sources compiled in");
920#endif
[0]921  } else if ( strcmp(k, "-sC") == 0 ) {
[2485]922#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]923   s_con = argv[++i];
[2485]924#else
925   ROAR_ERR("main(*): No support for sources compiled in");
926#endif
[0]927  } else if ( strcmp(k, "-sP") == 0 ) {
[2485]928#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]929   s_prim = 1;
[2485]930#else
931   ROAR_ERR("main(*): No support for sources compiled in");
932#endif
[1110]933  } else if ( strcmp(k, "-sN") == 0 ) {
[2485]934#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]935   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
936    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
937   }
938   s_opt = s_dev = s_con = NULL;
939   s_drv = "cf";
940   s_prim = 0;
[2485]941#else
942   ROAR_ERR("main(*): No support for sources compiled in");
943#endif
[2270]944  } else if ( strcmp(k, "--list-sources") == 0 ) {
[2485]945#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[2270]946   print_sourcelist();
947   return 0;
[2485]948#else
949   ROAR_ERR("main(*): No support for sources compiled in");
950#endif
[0]951
[1923]952  } else if ( strcmp(k, "--light-channels") == 0 ) {
[2494]953#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]954   light_channels = atoi(argv[++i]);
[2494]955#else
956   ROAR_WARN("main(*): no light subsystem compiled in");
957#endif
[1923]958
[1924]959  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
[2487]960#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]961   midi_config.init_cb = 0;
[2487]962#else
963   // no warning here as this is the disable option
964#endif
[2444]965  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
[2487]966#ifndef ROAR_WITHOUT_DCOMP_CB
[2444]967   midi_config.init_cb = 1;
[2487]968#else
969   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
970#endif
[1924]971  } else if ( strcmp(k, "--midi-console") == 0 ) {
[2487]972#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]973   midi_config.console_dev = argv[++i];
[2444]974   midi_config.init_cb = 1;
[2487]975#else
976   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
977#endif
[1924]978
[2451]979  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
[2489]980#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]981   ssynth_conf.enable = 1;
[2489]982#else
983   ROAR_ERR("main(*): No support for ssynth compiled in");
984#endif
[2451]985  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
[2489]986#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]987   ssynth_conf.enable = 0;
[2489]988#else
989   // we can safely ignore the disable
990#endif
[2451]991
[0]992  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
[447]993   // This is only usefull in INET not UNIX mode.
[1494]994#ifdef ROAR_SUPPORT_LISTEN
[447]995   if ( *server == '/' )
996    server = ROAR_DEFAULT_HOST;
997
[446]998   errno = 0;
999   if ( (port = atoi(argv[++i])) < 1 ) {
[1486]1000#ifdef ROAR_HAVE_GETSERVBYNAME
[446]1001    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
1002     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
1003     return 1;
1004    }
1005    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
1006    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
1007            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
1008    port = ROAR_NET2HOST16(serv->s_port);
[1486]1009#else
1010    ROAR_ERR("invalite port number: %s", argv[i]);
1011    return 1;
1012#endif
[446]1013   }
[1494]1014#endif
[1115]1015  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
[1494]1016#ifdef ROAR_SUPPORT_LISTEN
[0]1017   server = argv[++i];
[1494]1018#endif
[518]1019
[573]1020  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
[1494]1021#ifdef ROAR_SUPPORT_LISTEN
[518]1022   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
1023    sock_type = ROAR_SOCKET_TYPE_TCP;
1024
1025   if ( *server == '/' )
1026    server = ROAR_DEFAULT_HOST;
[1494]1027#endif
[518]1028
1029  } else if ( strcmp(k, "-4") == 0 ) {
[1494]1030#ifdef ROAR_SUPPORT_LISTEN
[517]1031   sock_type = ROAR_SOCKET_TYPE_TCP;
[447]1032   if ( *server == '/' )
1033    server = ROAR_DEFAULT_HOST;
[1494]1034#endif
[518]1035  } else if ( strcmp(k, "-6") == 0 ) {
[1494]1036#ifdef ROAR_SUPPORT_LISTEN
[519]1037#ifdef PF_INET6
[518]1038   sock_type = ROAR_SOCKET_TYPE_TCP6;
1039   if ( *server == '/' )
1040    server = ROAR_DEFAULT_HOST;
[519]1041#else
1042    ROAR_ERR("No IPv6 support compiled in!");
1043    return 1;
1044#endif
[1494]1045#endif
[518]1046
[573]1047  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
[1494]1048#ifdef ROAR_SUPPORT_LISTEN
[62]1049   // ignore this case as it is the default behavor.
[517]1050   sock_type = ROAR_SOCKET_TYPE_UNIX;
[1494]1051#endif
[518]1052
[573]1053  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
[1494]1054#ifdef ROAR_SUPPORT_LISTEN
[508]1055#ifdef ROAR_HAVE_LIBDNET
1056    port   = ROAR_DEFAULT_NUM;
1057    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
1058    server = decnethost;
[517]1059    sock_type = ROAR_SOCKET_TYPE_DECNET;
[508]1060#else
1061    ROAR_ERR("No DECnet support compiled in!");
1062    return 1;
1063#endif
[1494]1064#endif
[518]1065
[1993]1066  } else if ( strcmp(k, "--slp") == 0 ) {
1067#ifdef ROAR_HAVE_LIBSLP
1068   reg_slp = 1;
1069#else
1070    ROAR_ERR("No OpenSLP support compiled in!");
1071    return 1;
1072#endif
1073
[60]1074  } else if ( strcmp(k, "-G") == 0 ) {
[444]1075   sock_grp  = argv[++i];
1076  } else if ( strcmp(k, "-U") == 0 ) {
1077   sock_user = argv[++i];
[0]1078
[68]1079  } else if ( strcmp(k, "--no-listen") == 0 ) {
[1494]1080#ifdef ROAR_SUPPORT_LISTEN
[1787]1081   server      = "";
[548]1082   g_terminate = 1;
[1155]1083   g_no_listen = 1;
[1494]1084#endif
[68]1085  } else if ( strcmp(k, "--client-fh") == 0 ) {
1086   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
1087    ROAR_ERR("main(*): Can not set client's fh");
1088    return 1;
1089   }
[501]1090  } else if ( strcmp(k, "--close-fh") == 0 ) {
[1486]1091#ifdef ROAR_HAVE_IO_POSIX
[501]1092   close(atoi(argv[++i]));
[1486]1093#else
1094   i++;
1095   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
1096#endif
[68]1097
[920]1098  } else if ( strcmp(k, "--standby") == 0 ) {
1099   g_standby = 1;
1100  } else if ( strcmp(k, "--auto-standby") == 0 ) {
1101   g_autostandby = 1;
[0]1102  } else {
1103   usage();
1104   return 1;
1105  }
1106
1107 }
[1503]1108#endif
[0]1109
[2485]1110#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1111 if ( s_dev != NULL ) {
1112  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1113   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1114  }
1115 }
[2485]1116#endif
[1110]1117
[1145]1118 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
[932]1119
[0]1120 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
1121
[2500]1122#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1819]1123 if ( midi_init() == -1 ) {
[281]1124  ROAR_ERR("Can not initialize MIDI subsystem");
[1819]1125 }
[2500]1126#endif
[1819]1127
[2489]1128#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1129 if ( ssynth_init() == -1 ) {
1130  ROAR_ERR("Can not initialize ssynth subsystem");
1131 }
[2489]1132#endif
[2451]1133
[2494]1134#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]1135 if ( light_init(light_channels) == -1 ) {
1136  ROAR_ERR("Can not initialize light control subsystem");
1137 }
[2494]1138#endif
[281]1139
[1494]1140#ifdef ROAR_SUPPORT_LISTEN
[68]1141 if ( *server != 0 ) {
[517]1142  if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
[1486]1143#ifdef ROAR_HAVE_UNIX
[286]1144   if ( *server == '/' ) {
[1011]1145    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
1146     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
1147     unsetenv("ROAR_PROXY");
1148    }
[286]1149    if ( (i = roar_socket_connect(server, port)) != -1 ) {
1150     close(i);
1151     ROAR_ERR("Can not open listen socket!");
1152     return 1;
1153    } else {
1154     unlink(server);
[517]1155     if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
[286]1156      ROAR_ERR("Can not open listen socket!");
1157      return 1;
1158     }
1159    }
[1011]1160    if ( env_roar_proxy_backup != NULL ) {
1161     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
1162     free(env_roar_proxy_backup);
1163    }
[1486]1164#else
1165   if (0) { // noop
1166#endif
[286]1167   } else {
1168    ROAR_ERR("Can not open listen socket!");
1169    return 1;
1170   }
[68]1171  }
[0]1172
[1486]1173#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[523]1174  if ( (grp = getgrnam(sock_grp)) == NULL ) {
1175   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
1176  }
[1486]1177#endif
1178#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[523]1179  if ( sock_user || (setids & R_SETUID) ) {
1180   if ( (pwd = getpwnam(sock_user)) == NULL ) {
1181    ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
1182   }
1183  }
[1486]1184#endif
[523]1185
[1516]1186#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
[68]1187  if ( *server == '/' ) {
[523]1188   if ( grp ) {
[444]1189    if ( pwd ) {
[1691]1190     if ( chown(server, pwd->pw_uid, grp->gr_gid) == -1 )
1191      return 1;
[444]1192    } else {
[1691]1193     if ( chown(server, -1, grp->gr_gid) == -1 )
1194      return 1;
[444]1195    }
[1753]1196#ifdef ROAR_HAVE_GETUID
[68]1197    if ( getuid() == 0 )
[1691]1198     if ( chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
1199      return 1;
[1753]1200#endif
[68]1201   }
[60]1202  }
[1486]1203#endif
[60]1204 }
[1494]1205#endif
[60]1206
[0]1207 if ( output_buffer_init(&sa) == -1 ) {
1208  ROAR_ERR("Can not init output buffer!");
1209  return 1;
1210 }
1211
[1145]1212 if ( driver == NULL ) {
1213  driver = "null";
1214 } else {
1215  ROAR_WARN("Usage of old driver interface. use -o not -d!");
1216 }
1217
[0]1218 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
1219  ROAR_ERR("Can not open output driver!");
1220  return 1;
1221 }
1222
[44]1223 if ( samples_init() == -1 ) {
1224  ROAR_ERR("Can not init samples!");
1225  return 1;
1226 }
1227
1228
[1486]1229 // we should handle this on microcontrollers, too.
[1753]1230#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
[0]1231 signal(SIGINT,  on_sig_int);
[285]1232 signal(SIGCHLD, on_sig_chld);
[2112]1233 signal(SIGUSR1, on_sig_usr1);
[0]1234 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
[1486]1235#endif
[0]1236
[275]1237 if ( realtime ) {
[278]1238#ifdef DEBUG
1239  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
1240#endif
1241
[1486]1242#ifdef ROAR_HAVE_NICE
[275]1243  errno = 0;
[276]1244  nice(-5*realtime); // -5 for each --realtime
[1486]1245  if ( errno ) {
1246   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1247  }
1248#else
1249  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1250#endif
[277]1251/*
[276]1252#ifdef __linux__
[277]1253  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
1254   ROAR_WARN("Can not set io priority: %s", strerror(errno));
[276]1255#endif
[277]1256*/
[275]1257 }
1258
[1486]1259#ifdef ROAR_HAVE_SETGID
[444]1260 if ( setids & R_SETGID ) {
1261  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
1262   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
1263  }
[523]1264  if ( !grp || setgid(grp->gr_gid) == -1 ) {
[444]1265   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
1266  }
1267 }
[1486]1268#endif
[444]1269
[0]1270
[39]1271 clients_set_pid(g_self_client, getpid());
[1753]1272#ifdef ROAR_HAVE_GETUID
[440]1273 clients_set_uid(g_self_client, getuid());
[1753]1274#endif
1275#ifdef ROAR_HAVE_GETGID
[440]1276 clients_set_gid(g_self_client, getgid());
[1753]1277#endif
[39]1278 clients_get(g_self_client, &self);
[37]1279
[39]1280 if ( self == NULL ) {
1281  ROAR_ERR("Can not get self client!");
1282  return 1;
1283 }
1284
[775]1285 strcpy(self->name, "RoarAudio daemon internal");
[68]1286
[1486]1287#ifdef ROAR_HAVE_FORK
[775]1288 if ( daemon ) {
[68]1289  close(ROAR_STDIN );
1290  close(ROAR_STDOUT);
1291  close(ROAR_STDERR);
[1753]1292
[68]1293  if ( fork() )
[1486]1294   ROAR_U_EXIT(0);
[1753]1295
1296#ifdef ROAR_HAVE_SETSID
1297  setsid();
1298#endif
[1046]1299  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
[68]1300 }
[1486]1301#endif
[68]1302
[2104]1303#ifdef SUPPORT_PIDFILE
[2106]1304 if ( pidfile != NULL ) {
1305  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
1306   ROAR_ERR("Can not write pidfile: %s", pidfile);
1307  } else {
1308   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
1309   roar_vio_close(&pidfile_vio);
1310  }
[2104]1311 }
1312#endif
1313
[1486]1314#ifdef ROAR_HAVE_CHROOT
[444]1315 if (chrootdir) {
1316  if ( chroot(chrootdir) == -1 ) {
1317   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
1318   return 2;
1319  }
1320  if ( chdir("/") == -1 ) {
1321   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
1322   return 2;
1323  }
1324 }
[1486]1325#endif
[444]1326
[1486]1327#ifdef ROAR_HAVE_SETUID
[444]1328 if ( setids & R_SETUID ) {
1329  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
1330   ROAR_ERR("Can not set UserID: %s", strerror(errno));
1331   return 3;
1332  }
[1753]1333#ifdef ROAR_HAVE_GETUID
[444]1334  clients_set_uid(g_self_client, getuid());
[1753]1335#endif
[444]1336 }
[1486]1337#endif
[444]1338
[1993]1339 // Register with OpenSLP:
[2028]1340#ifdef ROAR_HAVE_LIBSLP
[1993]1341 if ( reg_slp ) {
1342  register_slp(0, server);
1343 }
[2028]1344#endif
[1993]1345
[0]1346 // start main loop...
[905]1347 main_loop(drvid, drvinst, &sa, sysclocksync);
[0]1348
1349 // clean up.
1350 clean_quit_prep();
1351 driver_close(drvinst, drvid);
1352 output_buffer_free();
1353
1354 return 0;
1355}
1356
[574]1357void cleanup_listen_socket (int terminate) {
[1993]1358 // Deregister from SLP:
[2028]1359#ifdef ROAR_HAVE_LIBSLP
[1993]1360 register_slp(1, NULL);
[2028]1361#endif
[580]1362
[1494]1363#ifdef ROAR_SUPPORT_LISTEN
[580]1364 if ( g_listen_socket != -1 ) {
[1486]1365#ifdef ROAR_HAVE_IO_POSIX
[580]1366  close(g_listen_socket);
[1486]1367#endif // #else is useless because we are in void context.
[60]1368
[580]1369  g_listen_socket = -1;
[576]1370
[1486]1371#ifdef ROAR_HAVE_UNIX
[580]1372  if ( *server == '/' )
1373   unlink(server);
[1486]1374#endif
[580]1375 }
[60]1376
[1494]1377#endif
1378
[574]1379 if ( terminate )
1380  g_terminate = 1;
1381}
1382
1383void clean_quit_prep (void) {
1384 cleanup_listen_socket(0);
[60]1385
[2485]1386#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1387 sources_free();
[2485]1388#endif
[0]1389 streams_free();
1390 clients_free();
[2489]1391#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1392 ssynth_free();
[2489]1393#endif
[2487]1394#ifndef ROAR_WITHOUT_DCOMP_CB
[282]1395 midi_cb_stop(); // stop console beep
[2487]1396#endif
[2500]1397#ifndef ROAR_WITHOUT_DCOMP_MIDI
[281]1398 midi_free();
[2500]1399#endif
[2494]1400#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]1401 light_free();
[2494]1402#endif
[2104]1403
1404#ifdef SUPPORT_PIDFILE
1405 if ( pidfile != NULL )
1406  unlink(pidfile);
1407#endif
[0]1408}
1409
1410void clean_quit (void) {
1411 clean_quit_prep();
1412// driver_close(drvinst, drvid);
1413// output_buffer_free();
1414 exit(0);
1415}
1416
1417//ll
Note: See TracBrowser for help on using the repository browser.