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

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

support to disable MIDI subsystem completly

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