source: roaraudio/roard/roard.c @ 2486:f4ec482727d6

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

disable unused vars

File size: 35.7 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   midi_config.init_cb = 0;
934  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
935   midi_config.init_cb = 1;
936  } else if ( strcmp(k, "--midi-console") == 0 ) {
937   midi_config.console_dev = argv[++i];
938   midi_config.init_cb = 1;
939
940  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
941   ssynth_conf.enable = 1;
942  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
943   ssynth_conf.enable = 0;
944
945  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
946   // This is only usefull in INET not UNIX mode.
947#ifdef ROAR_SUPPORT_LISTEN
948   if ( *server == '/' )
949    server = ROAR_DEFAULT_HOST;
950
951   errno = 0;
952   if ( (port = atoi(argv[++i])) < 1 ) {
953#ifdef ROAR_HAVE_GETSERVBYNAME
954    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
955     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
956     return 1;
957    }
958    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
959    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
960            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
961    port = ROAR_NET2HOST16(serv->s_port);
962#else
963    ROAR_ERR("invalite port number: %s", argv[i]);
964    return 1;
965#endif
966   }
967#endif
968  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
969#ifdef ROAR_SUPPORT_LISTEN
970   server = argv[++i];
971#endif
972
973  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
974#ifdef ROAR_SUPPORT_LISTEN
975   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
976    sock_type = ROAR_SOCKET_TYPE_TCP;
977
978   if ( *server == '/' )
979    server = ROAR_DEFAULT_HOST;
980#endif
981
982  } else if ( strcmp(k, "-4") == 0 ) {
983#ifdef ROAR_SUPPORT_LISTEN
984   sock_type = ROAR_SOCKET_TYPE_TCP;
985   if ( *server == '/' )
986    server = ROAR_DEFAULT_HOST;
987#endif
988  } else if ( strcmp(k, "-6") == 0 ) {
989#ifdef ROAR_SUPPORT_LISTEN
990#ifdef PF_INET6
991   sock_type = ROAR_SOCKET_TYPE_TCP6;
992   if ( *server == '/' )
993    server = ROAR_DEFAULT_HOST;
994#else
995    ROAR_ERR("No IPv6 support compiled in!");
996    return 1;
997#endif
998#endif
999
1000  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
1001#ifdef ROAR_SUPPORT_LISTEN
1002   // ignore this case as it is the default behavor.
1003   sock_type = ROAR_SOCKET_TYPE_UNIX;
1004#endif
1005
1006  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
1007#ifdef ROAR_SUPPORT_LISTEN
1008#ifdef ROAR_HAVE_LIBDNET
1009    port   = ROAR_DEFAULT_NUM;
1010    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
1011    server = decnethost;
1012    sock_type = ROAR_SOCKET_TYPE_DECNET;
1013#else
1014    ROAR_ERR("No DECnet support compiled in!");
1015    return 1;
1016#endif
1017#endif
1018
1019  } else if ( strcmp(k, "--slp") == 0 ) {
1020#ifdef ROAR_HAVE_LIBSLP
1021   reg_slp = 1;
1022#else
1023    ROAR_ERR("No OpenSLP support compiled in!");
1024    return 1;
1025#endif
1026
1027  } else if ( strcmp(k, "-G") == 0 ) {
1028   sock_grp  = argv[++i];
1029  } else if ( strcmp(k, "-U") == 0 ) {
1030   sock_user = argv[++i];
1031
1032  } else if ( strcmp(k, "--no-listen") == 0 ) {
1033#ifdef ROAR_SUPPORT_LISTEN
1034   server      = "";
1035   g_terminate = 1;
1036   g_no_listen = 1;
1037#endif
1038  } else if ( strcmp(k, "--client-fh") == 0 ) {
1039   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
1040    ROAR_ERR("main(*): Can not set client's fh");
1041    return 1;
1042   }
1043  } else if ( strcmp(k, "--close-fh") == 0 ) {
1044#ifdef ROAR_HAVE_IO_POSIX
1045   close(atoi(argv[++i]));
1046#else
1047   i++;
1048   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
1049#endif
1050
1051  } else if ( strcmp(k, "--standby") == 0 ) {
1052   g_standby = 1;
1053  } else if ( strcmp(k, "--auto-standby") == 0 ) {
1054   g_autostandby = 1;
1055  } else {
1056   usage();
1057   return 1;
1058  }
1059
1060 }
1061#endif
1062
1063#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1064 if ( s_dev != NULL ) {
1065  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1066   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1067  }
1068 }
1069#endif
1070
1071 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
1072
1073 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
1074
1075 if ( midi_init() == -1 ) {
1076  ROAR_ERR("Can not initialize MIDI subsystem");
1077 }
1078
1079 if ( ssynth_init() == -1 ) {
1080  ROAR_ERR("Can not initialize ssynth subsystem");
1081 }
1082
1083 if ( light_init(light_channels) == -1 ) {
1084  ROAR_ERR("Can not initialize light control subsystem");
1085 }
1086
1087#ifdef ROAR_SUPPORT_LISTEN
1088 if ( *server != 0 ) {
1089  if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
1090#ifdef ROAR_HAVE_UNIX
1091   if ( *server == '/' ) {
1092    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
1093     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
1094     unsetenv("ROAR_PROXY");
1095    }
1096    if ( (i = roar_socket_connect(server, port)) != -1 ) {
1097     close(i);
1098     ROAR_ERR("Can not open listen socket!");
1099     return 1;
1100    } else {
1101     unlink(server);
1102     if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
1103      ROAR_ERR("Can not open listen socket!");
1104      return 1;
1105     }
1106    }
1107    if ( env_roar_proxy_backup != NULL ) {
1108     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
1109     free(env_roar_proxy_backup);
1110    }
1111#else
1112   if (0) { // noop
1113#endif
1114   } else {
1115    ROAR_ERR("Can not open listen socket!");
1116    return 1;
1117   }
1118  }
1119
1120#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
1121  if ( (grp = getgrnam(sock_grp)) == NULL ) {
1122   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
1123  }
1124#endif
1125#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1126  if ( sock_user || (setids & R_SETUID) ) {
1127   if ( (pwd = getpwnam(sock_user)) == NULL ) {
1128    ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
1129   }
1130  }
1131#endif
1132
1133#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
1134  if ( *server == '/' ) {
1135   if ( grp ) {
1136    if ( pwd ) {
1137     if ( chown(server, pwd->pw_uid, grp->gr_gid) == -1 )
1138      return 1;
1139    } else {
1140     if ( chown(server, -1, grp->gr_gid) == -1 )
1141      return 1;
1142    }
1143#ifdef ROAR_HAVE_GETUID
1144    if ( getuid() == 0 )
1145     if ( chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
1146      return 1;
1147#endif
1148   }
1149  }
1150#endif
1151 }
1152#endif
1153
1154 if ( output_buffer_init(&sa) == -1 ) {
1155  ROAR_ERR("Can not init output buffer!");
1156  return 1;
1157 }
1158
1159 if ( driver == NULL ) {
1160  driver = "null";
1161 } else {
1162  ROAR_WARN("Usage of old driver interface. use -o not -d!");
1163 }
1164
1165 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
1166  ROAR_ERR("Can not open output driver!");
1167  return 1;
1168 }
1169
1170 if ( samples_init() == -1 ) {
1171  ROAR_ERR("Can not init samples!");
1172  return 1;
1173 }
1174
1175
1176 // we should handle this on microcontrollers, too.
1177#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
1178 signal(SIGINT,  on_sig_int);
1179 signal(SIGCHLD, on_sig_chld);
1180 signal(SIGUSR1, on_sig_usr1);
1181 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
1182#endif
1183
1184 if ( realtime ) {
1185#ifdef DEBUG
1186  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
1187#endif
1188
1189#ifdef ROAR_HAVE_NICE
1190  errno = 0;
1191  nice(-5*realtime); // -5 for each --realtime
1192  if ( errno ) {
1193   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1194  }
1195#else
1196  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1197#endif
1198/*
1199#ifdef __linux__
1200  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
1201   ROAR_WARN("Can not set io priority: %s", strerror(errno));
1202#endif
1203*/
1204 }
1205
1206#ifdef ROAR_HAVE_SETGID
1207 if ( setids & R_SETGID ) {
1208  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
1209   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
1210  }
1211  if ( !grp || setgid(grp->gr_gid) == -1 ) {
1212   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
1213  }
1214 }
1215#endif
1216
1217
1218 clients_set_pid(g_self_client, getpid());
1219#ifdef ROAR_HAVE_GETUID
1220 clients_set_uid(g_self_client, getuid());
1221#endif
1222#ifdef ROAR_HAVE_GETGID
1223 clients_set_gid(g_self_client, getgid());
1224#endif
1225 clients_get(g_self_client, &self);
1226
1227 if ( self == NULL ) {
1228  ROAR_ERR("Can not get self client!");
1229  return 1;
1230 }
1231
1232 strcpy(self->name, "RoarAudio daemon internal");
1233
1234#ifdef ROAR_HAVE_FORK
1235 if ( daemon ) {
1236  close(ROAR_STDIN );
1237  close(ROAR_STDOUT);
1238  close(ROAR_STDERR);
1239
1240  if ( fork() )
1241   ROAR_U_EXIT(0);
1242
1243#ifdef ROAR_HAVE_SETSID
1244  setsid();
1245#endif
1246  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
1247 }
1248#endif
1249
1250#ifdef SUPPORT_PIDFILE
1251 if ( pidfile != NULL ) {
1252  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
1253   ROAR_ERR("Can not write pidfile: %s", pidfile);
1254  } else {
1255   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
1256   roar_vio_close(&pidfile_vio);
1257  }
1258 }
1259#endif
1260
1261#ifdef ROAR_HAVE_CHROOT
1262 if (chrootdir) {
1263  if ( chroot(chrootdir) == -1 ) {
1264   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
1265   return 2;
1266  }
1267  if ( chdir("/") == -1 ) {
1268   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
1269   return 2;
1270  }
1271 }
1272#endif
1273
1274#ifdef ROAR_HAVE_SETUID
1275 if ( setids & R_SETUID ) {
1276  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
1277   ROAR_ERR("Can not set UserID: %s", strerror(errno));
1278   return 3;
1279  }
1280#ifdef ROAR_HAVE_GETUID
1281  clients_set_uid(g_self_client, getuid());
1282#endif
1283 }
1284#endif
1285
1286 // Register with OpenSLP:
1287#ifdef ROAR_HAVE_LIBSLP
1288 if ( reg_slp ) {
1289  register_slp(0, server);
1290 }
1291#endif
1292
1293 // start main loop...
1294 main_loop(drvid, drvinst, &sa, sysclocksync);
1295
1296 // clean up.
1297 clean_quit_prep();
1298 driver_close(drvinst, drvid);
1299 output_buffer_free();
1300
1301 return 0;
1302}
1303
1304void cleanup_listen_socket (int terminate) {
1305 // Deregister from SLP:
1306#ifdef ROAR_HAVE_LIBSLP
1307 register_slp(1, NULL);
1308#endif
1309
1310#ifdef ROAR_SUPPORT_LISTEN
1311 if ( g_listen_socket != -1 ) {
1312#ifdef ROAR_HAVE_IO_POSIX
1313  close(g_listen_socket);
1314#endif // #else is useless because we are in void context.
1315
1316  g_listen_socket = -1;
1317
1318#ifdef ROAR_HAVE_UNIX
1319  if ( *server == '/' )
1320   unlink(server);
1321#endif
1322 }
1323
1324#endif
1325
1326 if ( terminate )
1327  g_terminate = 1;
1328}
1329
1330void clean_quit_prep (void) {
1331 cleanup_listen_socket(0);
1332
1333#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1334 sources_free();
1335#endif
1336 streams_free();
1337 clients_free();
1338 ssynth_free();
1339 midi_cb_stop(); // stop console beep
1340 midi_free();
1341 light_free();
1342
1343#ifdef SUPPORT_PIDFILE
1344 if ( pidfile != NULL )
1345  unlink(pidfile);
1346#endif
1347}
1348
1349void clean_quit (void) {
1350 clean_quit_prep();
1351// driver_close(drvinst, drvid);
1352// output_buffer_free();
1353 exit(0);
1354}
1355
1356//ll
Note: See TracBrowser for help on using the repository browser.