source: roaraudio/roard/roard.c @ 2494:353c82155185

Last change on this file since 2494:353c82155185 was 2494:353c82155185, checked in by phi, 15 years ago

support to disable light subsystem and some parts of MIDI and raw

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