source: roaraudio/roard/roard.c @ 2485:1cbb82cde915

Last change on this file since 2485:1cbb82cde915 was 2485:1cbb82cde915, checked in by phi, 15 years ago

make sources optional

File size: 35.6 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 char * s_drv     = "cf";
627 char * s_dev     = NULL;
628 char * s_con     = NULL;
629 char * s_opt     = NULL;
630 int    s_prim    = 0;
631 char * o_drv     = getenv("ROAR_DRIVER");
632 char * o_dev     = getenv("ROAR_DEVICE");
633 char * o_opts    = NULL;
634 int    o_prim    = 0;
635 int    o_count   = 0;
636 int    light_channels = LIGHT_CHANNELS_DEFAULT;
637 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
638 char * sock_user = NULL;
639#ifdef ROAR_SUPPORT_LISTEN
640 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
641#endif
642#ifdef ROAR_HAVE_LIBSLP
643 int    reg_slp   = 0;
644#endif
645#ifdef ROAR_HAVE_CHROOT
646 char * chrootdir = NULL;
647#endif
648#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
649 int    setids    = 0;
650#endif
651#ifdef ROAR_HAVE_UNIX
652 char * env_roar_proxy_backup;
653#endif
654#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
655 struct group   * grp  = NULL;
656#endif
657#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
658 struct passwd  * pwd  = NULL;
659#endif
660#ifdef ROAR_HAVE_GETSERVBYNAME
661 struct servent * serv = NULL;
662#endif
663 DRIVER_USERDATA_T drvinst;
664 struct roar_client * self = NULL;
665#ifdef ROAR_HAVE_LIBDNET
666 char decnethost[80];
667#endif
668#ifdef SUPPORT_PIDFILE
669 struct roar_vio_calls pidfile_vio;
670#endif
671
672 g_standby       =  0;
673 g_autostandby   =  0;
674 alive           =  1;
675#ifdef ROAR_SUPPORT_LISTEN
676 g_no_listen     =  0;
677 g_listen_socket = -1;
678#else
679 g_terminate     =  1;
680#endif
681
682 sa.bits     = ROAR_BITS_DEFAULT;
683 sa.channels = ROAR_CHANNELS_DEFAULT;
684 sa.rate     = ROAR_RATE_DEFAULT;
685 sa.codec    = ROAR_CODEC_DEFAULT;
686
687 g_sa        = &sa;
688 g_max_sa    = &max_sa;
689
690 memcpy(g_max_sa, g_sa, sizeof(max_sa));
691
692 g_config = &config;
693
694 if ( init_config() == -1 ) {
695  ROAR_ERR("Can not init default config!");
696  return 1;
697 }
698
699 if ( midi_init_config() == -1 ) {
700  ROAR_ERR("Can not init MIDI config!");
701  return 1;
702 }
703
704 if ( ssynth_init_config() == -1 ) {
705  ROAR_ERR("Can not init ssynth config!");
706  return 1;
707 }
708
709#ifdef ROAR_SUPPORT_LISTEN
710#ifdef ROAR_HAVE_GETUID
711 if ( getuid() != 0 && getenv("HOME") != NULL ) {
712  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
713  server = user_sock;
714 }
715#endif
716
717 if ( getenv("ROAR_SERVER") != NULL )
718  server = getenv("ROAR_SERVER");
719#endif
720
721 if ( clients_init() == -1 ) {
722  ROAR_ERR("Can not init clients!");
723  return 1;
724 }
725
726 if ( streams_init() == -1 ) {
727  ROAR_ERR("Can not init streams!");
728  return 1;
729 }
730
731 if ( (g_self_client = clients_new()) == -1 ) {
732  ROAR_ERR("Can not create self client!");
733  return 1;
734 }
735
736#ifndef ROAR_WITHOUT_DCOMP_SOURCES
737 if ( sources_init() == -1 ) {
738  ROAR_ERR("Can not init sources!");
739  return 1;
740 }
741
742 if ( (sources_set_client(g_self_client)) == -1 ) {
743  ROAR_ERR("Can not init set source client!");
744  return 1;
745 }
746#endif
747
748#ifdef ROAR_HAVE_MAIN_ARGS
749 for (i = 1; i < argc; i++) {
750  k = argv[i];
751
752  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
753   usage();
754   return 0;
755
756  } else if ( strcmp(k, "--start") == 0 ) {
757   // this is a no op
758  } else if ( strcmp(k, "--restart") == 0 ) {
759#ifdef ROAR_SUPPORT_LISTEN
760   if ( restart_server(server, 1) == -1 ) {
761    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server);
762   }
763#else
764   ROAR_ERR("--restart not supported");
765#endif
766  } else if ( strcmp(k, "--shutdown") == 0 ) {
767#ifdef ROAR_SUPPORT_LISTEN
768   if ( restart_server(server, 1) == -1 ) {
769    ROAR_WARN("Can not terminate old server (not running at %s?)", server);
770    return 1;
771   }
772   return 0;
773#else
774   ROAR_ERR("--shutdown not supported");
775   return 1;
776#endif
777  } else if ( strcmp(k, "--stop") == 0 ) {
778#ifdef ROAR_SUPPORT_LISTEN
779   if ( restart_server(server, 0) == -1 ) {
780    ROAR_WARN("Can not stop old server (not running at %s?)", server);
781    return 1;
782   }
783   return 0;
784#else
785   ROAR_ERR("--stop not supported");
786   return 1;
787#endif
788
789
790  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
791#ifdef ROAR_HAVE_FORK
792   daemon = 1;
793#else
794   ROAR_ERR("--daemon not supported");
795#endif
796  } else if ( strcmp(k, "--terminate") == 0 ) {
797   g_terminate = 1;
798  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
799   sysclocksync = 1000;
800  } else if ( strcmp(k, "--realtime") == 0 ) {
801   realtime++;
802  } else if ( strcmp(k, "--chroot") == 0 ) {
803#ifdef ROAR_HAVE_CHROOT
804   chrootdir = argv[++i];
805#else
806   ROAR_ERR("--chroot not supported");
807   i++;
808#endif
809  } else if ( strcmp(k, "--setgid") == 0 ) {
810#ifdef ROAR_HAVE_SETGID
811   setids |= R_SETGID;
812#else
813   ROAR_ERR("--setgid not supported");
814#endif
815  } else if ( strcmp(k, "--setuid") == 0 ) {
816#ifdef ROAR_HAVE_SETUID
817   setids |= R_SETUID;
818#else
819   ROAR_ERR("--setuid not supported");
820#endif
821  } else if ( strcmp(k, "--location") == 0 ) {
822   g_config->location = argv[++i];
823  } else if ( strcmp(k, "--pidfile") == 0 ) {
824#ifdef SUPPORT_PIDFILE
825   pidfile = argv[++i];
826#else
827   ROAR_ERR("--pidfile not supported");
828   i++;
829#endif
830
831  } else if ( strcmp(k, "--list-cf") == 0 ) {
832   print_codecfilterlist();
833   return 0;
834
835  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
836   sa.rate = atoi(argv[++i]);
837  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
838   sa.bits = atoi(argv[++i]);
839  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
840   sa.channels = atoi(argv[++i]);
841
842  } else if ( strcmp(k, "--stream-flags") == 0 ) {
843   if ( update_stream_flags(argv[++i]) == -1 ) {
844    ROAR_ERR("Can not set stream flags");
845    return 1;
846   }
847
848  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
849   driver = argv[++i];
850   if ( strcmp(driver, "list") == 0 ) {
851    ROAR_WARN("The option is obsolete, use --list-driver!");
852    print_driverlist();
853    return 0;
854   }
855  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
856   device = argv[++i];
857  } else if ( strcmp(k, "-dO") == 0 ) {
858   opts = argv[++i];
859  } else if ( strcmp(k, "--list-driver") == 0 ) {
860   print_driverlist();
861   return 0;
862
863  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
864   o_drv  = argv[++i];
865  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
866   o_dev  = argv[++i];
867  } else if ( strcmp(k, "-oO") == 0 ) {
868   o_opts = argv[++i];
869  } else if ( strcmp(k, "-oP") == 0 ) {
870   o_prim = 1;
871  } else if ( strcmp(k, "-oN") == 0 ) {
872   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
873    o_count++;
874
875   o_drv  = o_dev = o_opts = NULL;
876   o_prim = 0;
877
878  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
879#ifndef ROAR_WITHOUT_DCOMP_SOURCES
880   s_drv = argv[++i];
881#else
882   ROAR_ERR("main(*): No support for sources compiled in");
883#endif
884  } else if ( strcmp(k, "-S") == 0 ) {
885#ifndef ROAR_WITHOUT_DCOMP_SOURCES
886   s_dev = argv[++i];
887#else
888   ROAR_ERR("main(*): No support for sources compiled in");
889#endif
890  } else if ( strcmp(k, "-sO") == 0 ) {
891#ifndef ROAR_WITHOUT_DCOMP_SOURCES
892   s_opt = argv[++i];
893#else
894   ROAR_ERR("main(*): No support for sources compiled in");
895#endif
896  } else if ( strcmp(k, "-sC") == 0 ) {
897#ifndef ROAR_WITHOUT_DCOMP_SOURCES
898   s_con = argv[++i];
899#else
900   ROAR_ERR("main(*): No support for sources compiled in");
901#endif
902  } else if ( strcmp(k, "-sP") == 0 ) {
903#ifndef ROAR_WITHOUT_DCOMP_SOURCES
904   s_prim = 1;
905#else
906   ROAR_ERR("main(*): No support for sources compiled in");
907#endif
908  } else if ( strcmp(k, "-sN") == 0 ) {
909#ifndef ROAR_WITHOUT_DCOMP_SOURCES
910   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
911    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
912   }
913   s_opt = s_dev = s_con = NULL;
914   s_drv = "cf";
915   s_prim = 0;
916#else
917   ROAR_ERR("main(*): No support for sources compiled in");
918#endif
919  } else if ( strcmp(k, "--list-sources") == 0 ) {
920#ifndef ROAR_WITHOUT_DCOMP_SOURCES
921   print_sourcelist();
922   return 0;
923#else
924   ROAR_ERR("main(*): No support for sources compiled in");
925#endif
926
927  } else if ( strcmp(k, "--light-channels") == 0 ) {
928   light_channels = atoi(argv[++i]);
929
930  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
931   midi_config.init_cb = 0;
932  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
933   midi_config.init_cb = 1;
934  } else if ( strcmp(k, "--midi-console") == 0 ) {
935   midi_config.console_dev = argv[++i];
936   midi_config.init_cb = 1;
937
938  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
939   ssynth_conf.enable = 1;
940  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
941   ssynth_conf.enable = 0;
942
943  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
944   // This is only usefull in INET not UNIX mode.
945#ifdef ROAR_SUPPORT_LISTEN
946   if ( *server == '/' )
947    server = ROAR_DEFAULT_HOST;
948
949   errno = 0;
950   if ( (port = atoi(argv[++i])) < 1 ) {
951#ifdef ROAR_HAVE_GETSERVBYNAME
952    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
953     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
954     return 1;
955    }
956    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
957    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
958            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
959    port = ROAR_NET2HOST16(serv->s_port);
960#else
961    ROAR_ERR("invalite port number: %s", argv[i]);
962    return 1;
963#endif
964   }
965#endif
966  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
967#ifdef ROAR_SUPPORT_LISTEN
968   server = argv[++i];
969#endif
970
971  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
972#ifdef ROAR_SUPPORT_LISTEN
973   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
974    sock_type = ROAR_SOCKET_TYPE_TCP;
975
976   if ( *server == '/' )
977    server = ROAR_DEFAULT_HOST;
978#endif
979
980  } else if ( strcmp(k, "-4") == 0 ) {
981#ifdef ROAR_SUPPORT_LISTEN
982   sock_type = ROAR_SOCKET_TYPE_TCP;
983   if ( *server == '/' )
984    server = ROAR_DEFAULT_HOST;
985#endif
986  } else if ( strcmp(k, "-6") == 0 ) {
987#ifdef ROAR_SUPPORT_LISTEN
988#ifdef PF_INET6
989   sock_type = ROAR_SOCKET_TYPE_TCP6;
990   if ( *server == '/' )
991    server = ROAR_DEFAULT_HOST;
992#else
993    ROAR_ERR("No IPv6 support compiled in!");
994    return 1;
995#endif
996#endif
997
998  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
999#ifdef ROAR_SUPPORT_LISTEN
1000   // ignore this case as it is the default behavor.
1001   sock_type = ROAR_SOCKET_TYPE_UNIX;
1002#endif
1003
1004  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
1005#ifdef ROAR_SUPPORT_LISTEN
1006#ifdef ROAR_HAVE_LIBDNET
1007    port   = ROAR_DEFAULT_NUM;
1008    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
1009    server = decnethost;
1010    sock_type = ROAR_SOCKET_TYPE_DECNET;
1011#else
1012    ROAR_ERR("No DECnet support compiled in!");
1013    return 1;
1014#endif
1015#endif
1016
1017  } else if ( strcmp(k, "--slp") == 0 ) {
1018#ifdef ROAR_HAVE_LIBSLP
1019   reg_slp = 1;
1020#else
1021    ROAR_ERR("No OpenSLP support compiled in!");
1022    return 1;
1023#endif
1024
1025  } else if ( strcmp(k, "-G") == 0 ) {
1026   sock_grp  = argv[++i];
1027  } else if ( strcmp(k, "-U") == 0 ) {
1028   sock_user = argv[++i];
1029
1030  } else if ( strcmp(k, "--no-listen") == 0 ) {
1031#ifdef ROAR_SUPPORT_LISTEN
1032   server      = "";
1033   g_terminate = 1;
1034   g_no_listen = 1;
1035#endif
1036  } else if ( strcmp(k, "--client-fh") == 0 ) {
1037   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
1038    ROAR_ERR("main(*): Can not set client's fh");
1039    return 1;
1040   }
1041  } else if ( strcmp(k, "--close-fh") == 0 ) {
1042#ifdef ROAR_HAVE_IO_POSIX
1043   close(atoi(argv[++i]));
1044#else
1045   i++;
1046   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
1047#endif
1048
1049  } else if ( strcmp(k, "--standby") == 0 ) {
1050   g_standby = 1;
1051  } else if ( strcmp(k, "--auto-standby") == 0 ) {
1052   g_autostandby = 1;
1053  } else {
1054   usage();
1055   return 1;
1056  }
1057
1058 }
1059#endif
1060
1061#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1062 if ( s_dev != NULL ) {
1063  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1064   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1065  }
1066 }
1067#endif
1068
1069 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
1070
1071 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
1072
1073 if ( midi_init() == -1 ) {
1074  ROAR_ERR("Can not initialize MIDI subsystem");
1075 }
1076
1077 if ( ssynth_init() == -1 ) {
1078  ROAR_ERR("Can not initialize ssynth subsystem");
1079 }
1080
1081 if ( light_init(light_channels) == -1 ) {
1082  ROAR_ERR("Can not initialize light control subsystem");
1083 }
1084
1085#ifdef ROAR_SUPPORT_LISTEN
1086 if ( *server != 0 ) {
1087  if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
1088#ifdef ROAR_HAVE_UNIX
1089   if ( *server == '/' ) {
1090    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
1091     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
1092     unsetenv("ROAR_PROXY");
1093    }
1094    if ( (i = roar_socket_connect(server, port)) != -1 ) {
1095     close(i);
1096     ROAR_ERR("Can not open listen socket!");
1097     return 1;
1098    } else {
1099     unlink(server);
1100     if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
1101      ROAR_ERR("Can not open listen socket!");
1102      return 1;
1103     }
1104    }
1105    if ( env_roar_proxy_backup != NULL ) {
1106     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
1107     free(env_roar_proxy_backup);
1108    }
1109#else
1110   if (0) { // noop
1111#endif
1112   } else {
1113    ROAR_ERR("Can not open listen socket!");
1114    return 1;
1115   }
1116  }
1117
1118#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
1119  if ( (grp = getgrnam(sock_grp)) == NULL ) {
1120   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
1121  }
1122#endif
1123#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1124  if ( sock_user || (setids & R_SETUID) ) {
1125   if ( (pwd = getpwnam(sock_user)) == NULL ) {
1126    ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
1127   }
1128  }
1129#endif
1130
1131#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
1132  if ( *server == '/' ) {
1133   if ( grp ) {
1134    if ( pwd ) {
1135     if ( chown(server, pwd->pw_uid, grp->gr_gid) == -1 )
1136      return 1;
1137    } else {
1138     if ( chown(server, -1, grp->gr_gid) == -1 )
1139      return 1;
1140    }
1141#ifdef ROAR_HAVE_GETUID
1142    if ( getuid() == 0 )
1143     if ( chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
1144      return 1;
1145#endif
1146   }
1147  }
1148#endif
1149 }
1150#endif
1151
1152 if ( output_buffer_init(&sa) == -1 ) {
1153  ROAR_ERR("Can not init output buffer!");
1154  return 1;
1155 }
1156
1157 if ( driver == NULL ) {
1158  driver = "null";
1159 } else {
1160  ROAR_WARN("Usage of old driver interface. use -o not -d!");
1161 }
1162
1163 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
1164  ROAR_ERR("Can not open output driver!");
1165  return 1;
1166 }
1167
1168 if ( samples_init() == -1 ) {
1169  ROAR_ERR("Can not init samples!");
1170  return 1;
1171 }
1172
1173
1174 // we should handle this on microcontrollers, too.
1175#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
1176 signal(SIGINT,  on_sig_int);
1177 signal(SIGCHLD, on_sig_chld);
1178 signal(SIGUSR1, on_sig_usr1);
1179 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
1180#endif
1181
1182 if ( realtime ) {
1183#ifdef DEBUG
1184  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
1185#endif
1186
1187#ifdef ROAR_HAVE_NICE
1188  errno = 0;
1189  nice(-5*realtime); // -5 for each --realtime
1190  if ( errno ) {
1191   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1192  }
1193#else
1194  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1195#endif
1196/*
1197#ifdef __linux__
1198  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
1199   ROAR_WARN("Can not set io priority: %s", strerror(errno));
1200#endif
1201*/
1202 }
1203
1204#ifdef ROAR_HAVE_SETGID
1205 if ( setids & R_SETGID ) {
1206  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
1207   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
1208  }
1209  if ( !grp || setgid(grp->gr_gid) == -1 ) {
1210   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
1211  }
1212 }
1213#endif
1214
1215
1216 clients_set_pid(g_self_client, getpid());
1217#ifdef ROAR_HAVE_GETUID
1218 clients_set_uid(g_self_client, getuid());
1219#endif
1220#ifdef ROAR_HAVE_GETGID
1221 clients_set_gid(g_self_client, getgid());
1222#endif
1223 clients_get(g_self_client, &self);
1224
1225 if ( self == NULL ) {
1226  ROAR_ERR("Can not get self client!");
1227  return 1;
1228 }
1229
1230 strcpy(self->name, "RoarAudio daemon internal");
1231
1232#ifdef ROAR_HAVE_FORK
1233 if ( daemon ) {
1234  close(ROAR_STDIN );
1235  close(ROAR_STDOUT);
1236  close(ROAR_STDERR);
1237
1238  if ( fork() )
1239   ROAR_U_EXIT(0);
1240
1241#ifdef ROAR_HAVE_SETSID
1242  setsid();
1243#endif
1244  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
1245 }
1246#endif
1247
1248#ifdef SUPPORT_PIDFILE
1249 if ( pidfile != NULL ) {
1250  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
1251   ROAR_ERR("Can not write pidfile: %s", pidfile);
1252  } else {
1253   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
1254   roar_vio_close(&pidfile_vio);
1255  }
1256 }
1257#endif
1258
1259#ifdef ROAR_HAVE_CHROOT
1260 if (chrootdir) {
1261  if ( chroot(chrootdir) == -1 ) {
1262   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
1263   return 2;
1264  }
1265  if ( chdir("/") == -1 ) {
1266   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
1267   return 2;
1268  }
1269 }
1270#endif
1271
1272#ifdef ROAR_HAVE_SETUID
1273 if ( setids & R_SETUID ) {
1274  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
1275   ROAR_ERR("Can not set UserID: %s", strerror(errno));
1276   return 3;
1277  }
1278#ifdef ROAR_HAVE_GETUID
1279  clients_set_uid(g_self_client, getuid());
1280#endif
1281 }
1282#endif
1283
1284 // Register with OpenSLP:
1285#ifdef ROAR_HAVE_LIBSLP
1286 if ( reg_slp ) {
1287  register_slp(0, server);
1288 }
1289#endif
1290
1291 // start main loop...
1292 main_loop(drvid, drvinst, &sa, sysclocksync);
1293
1294 // clean up.
1295 clean_quit_prep();
1296 driver_close(drvinst, drvid);
1297 output_buffer_free();
1298
1299 return 0;
1300}
1301
1302void cleanup_listen_socket (int terminate) {
1303 // Deregister from SLP:
1304#ifdef ROAR_HAVE_LIBSLP
1305 register_slp(1, NULL);
1306#endif
1307
1308#ifdef ROAR_SUPPORT_LISTEN
1309 if ( g_listen_socket != -1 ) {
1310#ifdef ROAR_HAVE_IO_POSIX
1311  close(g_listen_socket);
1312#endif // #else is useless because we are in void context.
1313
1314  g_listen_socket = -1;
1315
1316#ifdef ROAR_HAVE_UNIX
1317  if ( *server == '/' )
1318   unlink(server);
1319#endif
1320 }
1321
1322#endif
1323
1324 if ( terminate )
1325  g_terminate = 1;
1326}
1327
1328void clean_quit_prep (void) {
1329 cleanup_listen_socket(0);
1330
1331#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1332 sources_free();
1333#endif
1334 streams_free();
1335 clients_free();
1336 ssynth_free();
1337 midi_cb_stop(); // stop console beep
1338 midi_free();
1339 light_free();
1340
1341#ifdef SUPPORT_PIDFILE
1342 if ( pidfile != NULL )
1343  unlink(pidfile);
1344#endif
1345}
1346
1347void clean_quit (void) {
1348 clean_quit_prep();
1349// driver_close(drvinst, drvid);
1350// output_buffer_free();
1351 exit(0);
1352}
1353
1354//ll
Note: See TracBrowser for help on using the repository browser.