source: roaraudio/roard/roard.c @ 2364:4d2d1aa717b1

Last change on this file since 2364:4d2d1aa717b1 was 2364:4d2d1aa717b1, checked in by phi, 15 years ago

added argument ss of vio open driver...

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