source: roaraudio/roard/roard.c @ 2487:10020ba41793

Last change on this file since 2487:10020ba41793 was 2487:10020ba41793, checked in by phi, 15 years ago

support to disable CB completly

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