source: roaraudio/roard/roard.c @ 2511:e5f52519866f

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

much better defaulting for diffrent subsystems

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