source: roaraudio/roard/roard.c @ 3688:7624aa24f3ef

Last change on this file since 3688:7624aa24f3ef was 3688:7624aa24f3ef, checked in by phi, 14 years ago

added --log-syslog to roard

File size: 50.3 KB
Line 
1//roard.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28#ifdef ROAR_SUPPORT_LISTEN
29char * server[ROAR_MAX_LISTEN_SOCKETS];
30#endif
31
32#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_FS_POSIX)
33#define SUPPORT_PIDFILE
34char * pidfile = NULL;
35#endif
36
37#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
38int    setids    = 0;
39#endif
40
41#ifdef ROAR_HAVE_LIBX11
42char * x11display = NULL;
43#endif
44
45#ifdef ROAR_HAVE_MAIN_ARGS
46void usage (void) {
47 printf("Usage: roard [OPTIONS]...\n\n");
48
49 printf("Misc Options:\n\n");
50 printf(
51        " --daemon              - Bring the server into background after init\n"
52        " --verbose             - Be more verbose, can be used multiple times\n"
53        " --terminate           - Terminate after last client quited\n"
54        " --start               - No op parameter (starting roard is default operation)\n"
55        " --restart             - Trys to stop an old instance and start a new with new settings\n"
56        " --stop                - Stops a running roard (provide --pidfile!)\n"
57        " --shutdown            - Terminates a running roard (provide --pidfile!)\n"
58        " --realtime            - Trys to get realtime priority,\n"
59        "                         give multible times for being more realtime\n"
60        " --chroot DIR          - chroots to the given dir\n"
61        " --setgid              - GroupID to the audio group as specified via -G\n"
62        " --setuid              - UserID to the audio user as specified via -U\n"
63        " --sysclocksync        - calculate exact sample rate using the system clock\n"
64        " --location  LOC       - Set lion readable location of server\n"
65#ifdef SUPPORT_PIDFILE
66        " --pidfile PIDFILE     - Write a pidfile at PIDFILE\n"
67#endif
68#ifdef ROAR_HAVE_SYSLOG
69        " --log-syslog          - Log Warnings, Errors, ... to syslog\n"
70#endif
71       );
72
73 printf("\nPlugin Options:\n\n");
74 printf(
75        " --plugin-load FILE    - Load plugin FILE\n"
76       );
77
78 printf("\nAudio Options:\n\n");
79 printf(
80        " -R  --rate   RATE     - Set server rate\n"
81        " -B  --bits   BITS     - Set server bits\n"
82        " -C  --chans  CHANNELS - Set server channels\n"
83       );
84
85 printf("\nStream Options:\n\n");
86 printf(
87        " --stream-flags D=F    - Set default flags for stream directions\n"
88        "                         D is the stream direction and F is a comma seperated\n"
89        "                         list of flags in form +flag or -flag to set or unset\n"
90        "                         a flag as default or remove it from the default\n"
91       );
92
93 printf("\nDriver Options: (obsolete, do not use, Use Ouput Options)\n\n");
94 printf(" -d  --driver DRV      - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT);
95 printf(" -D  --device DEV      - Set the device\n");
96 printf(" -dO OPTS              - Set output options\n");
97 printf(" --list-driver         - List all drivers\n");
98
99 printf("\nOutput Options:\n\n");
100 printf(" -o  --odriver DRV     - Set the driver, use '--list-driver' to get a list\n");
101 printf(" -O  --odevice DEV     - Set the device\n");
102 printf(" -oO OPTS              - Set output options\n");
103 printf(" -oN                   - Adds another output\n");
104 printf(" -oP                   - Mark output as primary\n");
105
106#ifndef ROAR_WITHOUT_DCOMP_SOURCES
107 printf("\nSource Options:\n\n");
108 printf(" -s  --source DRV      - Use DRV as input driver\n"
109        " -S           DEV      - Use DEV as input device\n"
110        " -sO          OPTS     - Use OPTS as input options\n"
111        " -sN                   - Adds another source\n"
112        " -sP                   - Make souce as primary\n"
113       );
114 printf(" --list-sources        - List all sources\n");
115#endif
116
117 printf("\nCodec Filter Options:\n\n");
118 printf(" --list-cf             - List all codec filter\n"
119       );
120
121#ifndef ROAR_WITHOUT_DCOMP_MIDI
122 printf("\nMIDI Options:\n\n");
123 printf(" --midi-no-console     - Disable console based MIDI synth\n"
124        " --midi-console-enable - Enables the console based MIDI synth\n"
125        " --midi-console DEV    - Set device for MIDI console\n"
126#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
127        " --ssynth-enable       - Enable simple software synth\n"
128        " --ssynth-disable      - Disable simple software synth\n"
129#endif
130       );
131#endif
132
133#ifndef ROAR_WITHOUT_DCOMP_LIGHT
134 printf("\nLight Control Options:\n\n");
135 printf(" --light-channels NUM  - Sets the number of channels for Light control (default: %i)\n",
136                                  LIGHT_CHANNELS_DEFAULT
137       );
138#endif
139
140#ifndef ROAR_WITHOUT_DCOMP_RDTCS
141 printf("\nRadio Data and Transmitter Control System Options:\n\n");
142 printf(" --rds-pi   PI         - Sets the RDS Programme Identification (PI)\n"
143        " --rds-ps   PS         - Sets the RDS Programme Service Name (PS)\n"
144        " --rds-pty  PTY        - Sets the RDS Programme Type (PTY)\n"
145        " --rds-tp              - Sets the RDS Traffic Programme (TP) flag\n"
146        " --rds-ct              - Enables sending of RDS Clock Time (CT)\n"
147       );
148#endif
149
150#ifdef ROAR_HAVE_LIBX11
151 printf("\nX11 Options:\n\n");
152 printf(
153        " --x11-display DISPLAY - Set display for X11\n"
154        " --display DISPLAY     - Set display for X11\n"
155       );
156#endif
157
158 printf("\nServer Options:\n\n");
159 printf(" -t  --tcp             - Use TCP listen socket\n"
160        " -u  --unix            - Use UNIX Domain listen socket (default)\n"
161#ifdef ROAR_HAVE_LIBDNET
162        " -n  --decnet          - use DECnet listen socket\n"
163#endif
164        " -4                    - Use IPv4 connections (implies -t)\n"
165#ifdef PF_INET6
166        " -6                    - Use IPv6 connections (implies -t)\n"
167#endif
168#ifdef IPV6_ADDRFORM
169        " -64                   - Try to downgrade sockets from IPv6 into IPv4,\n"
170        "                         this is normaly not usefull.\n"
171#endif
172        " -p  --port            - TCP Port to bind to\n"
173        " -b  --bind            - IP/Hostname to bind to\n"
174        "     --sock            - Filename for UNIX Domain Socket\n"
175        "     --proto PROTO     - Use PROTO as protocol on Socket\n"
176        "     --proto-dir DIR   - Set direction parameter for protocol\n"
177        "     --proto-rate RATE - Set sample rate parameter for protocol\n"
178        "     --proto-bits BITS - Set bits per sample parameter for protocol\n"
179        "     --proto-codec E   - Set codec parameter for protocol\n"
180        "     --proto-chans C   - Set number of channels paramter for protocol\n"
181        "     --list-proto      - List supported protocols\n"
182        "     --new-sock        - Parameters for new socket follows\n"
183#ifdef ROAR_HAVE_LIBSLP
184        "     --slp             - Enable OpenSLP support\n"
185#endif
186#ifdef ROAR_HAVE_LIBX11
187        "     --x11             - Enable X11 support\n"
188#endif
189        " --jumbo-mtu MTU       - Sets the MTU for Jumbo Packets\n"
190        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: %s)\n"
191        "                         You need the permissions to change the GID\n"
192        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
193        "                         You need the permissions to change the UID (normaly only root has)\n"
194        " --no-listen           - Do not listen for new clients\n"
195        "                         (only usefull for relaing, impleys --terminate)\n"
196        " --client-fh           - Comunicate with a client over this handle\n"
197        "                         (only usefull for relaing)\n"
198        " --close-fh            - Closes the given fh\n"
199        " --standby             - Start in standby state\n"
200        " --auto-standby        - Automatical goes into standby if there are no streams\n",
201#ifdef ROAR_DEFAULT_SOCKGRP
202        ROAR_DEFAULT_SOCKGRP
203#else
204        "(none)"
205#endif
206       );
207// printf("\n Options:\n\n");
208 printf("\n");
209}
210
211static void list_proto (void) {
212 printf("  Protocol Flag Subsys - Description\n");
213 printf("------------------------------------------------------\n");
214 printf("  roar          WM LRX - RoarAudio native protocol\n");
215#if !defined(ROAR_WITHOUT_DCOMP_EMUL_ESD) && defined(ROAR_HAVE_H_ESD)
216 printf("  esd           W      - EsounD emulation\n");
217#endif
218#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
219 printf("  simple        WM LRX - PulseAudio simple protocol\n");
220#endif
221#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
222 printf("  rsound        W      - RSound emulation\n");
223#endif
224}
225
226#endif
227
228int restart_server (char * server, int terminate) {
229 struct roar_connection con;
230#ifdef ROAR_HAVE_KILL
231 char buf[80];
232 ssize_t l;
233 struct roar_vio_calls fh;
234 pid_t pid;
235 int ok;
236
237 if ( pidfile != NULL ) {
238  if ( roar_vio_open_file(&fh, pidfile, O_RDONLY, 0644) == -1 ) {
239   ROAR_WARN("restart_server(*): Can not read pidfile: %s", pidfile);
240  } else {
241   l = roar_vio_read(&fh, buf, 80);
242   roar_vio_close(&fh);
243   if ( l > 0 ) {
244    buf[l-1] = 0;
245    buf[79]  = 0;
246    pid = atoi(buf);
247    if ( terminate ) {
248     ok = kill(pid, SIGUSR1);
249    } else {
250     ok = kill(pid, SIGINT);
251    }
252    if ( ok == 0 ) {
253     return 0;
254    } else {
255     ROAR_WARN("restart_server(*): Can not kill roard by pidfile");
256    }
257   } else {
258    ROAR_WARN("restart_server(*): Can not find a PID in the pidfile");
259   }
260  }
261 }
262#endif
263
264 if ( roar_connect(&con, server) == -1 ) {
265  return -1;
266 }
267
268 if ( roar_terminate(&con, terminate) == -1 ) {
269  return -1;
270 }
271
272 return roar_disconnect(&con);
273}
274
275#define R_SETUID 1
276#define R_SETGID 2
277
278int init_config (void) {
279 int i;
280
281 memset(g_config, 0, sizeof(struct roard_config));
282
283 for (i = 0; i < ROAR_DIR_DIRIDS; i++) {
284  g_config->streams[i].mixer_channels = 1;
285  g_config->streams[i].mixer.rpg_mul  = 1;
286  g_config->streams[i].mixer.rpg_div  = 1;
287  g_config->streams[i].mixer.scale    = 65535;
288  g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale;
289 }
290
291 g_config->streams[ROAR_DIR_PLAY    ].flags = ROAR_FLAG_META;
292 g_config->streams[ROAR_DIR_OUTPUT  ].flags = ROAR_FLAG_PASSMIXER;
293 g_config->streams[ROAR_DIR_FILTER  ].flags = ROAR_FLAG_SYNC;
294 g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC;
295 g_config->streams[ROAR_DIR_BIDIR   ].flags = ROAR_FLAG_ANTIECHO;
296
297 g_config->location = "***default***";
298
299 return 0;
300}
301
302#ifdef ROAR_SUPPORT_LISTEN
303int init_listening (void) {
304 int i;
305
306 memset(g_listen, 0, sizeof(g_listen));
307
308 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
309  g_listen[i].socket = -1;
310  g_listen[i].proto  = ROAR_PROTO_ROARAUDIO;
311  server[i]          = NULL;
312 }
313
314 return 0;
315}
316
317int add_listen (char * addr, int port, int sock_type, char * user, char * group, int proto, int dir, struct roar_audio_info * info) {
318#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
319 struct group   * grp  = NULL;
320#endif
321#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
322 struct passwd  * pwd  = NULL;
323#endif
324#ifdef ROAR_HAVE_UNIX
325 char * env_roar_proxy_backup;
326#endif
327 int    sockid = -1;
328 int    sock;
329 int    i;
330
331 if ( *addr != 0 ) {
332  for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
333   if ( g_listen[i].socket == -1 ) {
334    sockid = i;
335    break;
336   }
337  }
338
339  if ( sockid == -1 )
340   return -1;
341
342  g_listen[sockid].proto = proto;
343
344  ROAR_DBG("add_listen(*): proto=0x%.4x", proto);
345
346  if ( (g_listen[sockid].socket = roar_socket_listen(sock_type, addr, port)) == -1 ) {
347#ifdef ROAR_HAVE_UNIX
348   if ( *addr == '/' ) {
349    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
350     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
351     unsetenv("ROAR_PROXY");
352    }
353    if ( (sock = roar_socket_connect(addr, port)) != -1 ) {
354     close(sock);
355     ROAR_ERR("Can not open listen socket: Socket allready in use");
356     return 1;
357    } else {
358     unlink(addr);
359     if ( (g_listen[sockid].socket = roar_socket_listen(sock_type, addr, port)) == -1 ) {
360      ROAR_ERR("Can not open listen socket: %s", strerror(errno));
361      return 1;
362     }
363    }
364    if ( env_roar_proxy_backup != NULL ) {
365     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
366     free(env_roar_proxy_backup);
367    }
368#else
369   if (0) { // noop
370#endif
371   } else {
372    ROAR_ERR("Can not open listen socket: %s", strerror(errno));
373    return 1;
374   }
375  }
376
377#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
378  if ( group != NULL ) {
379   if ( (grp = getgrnam(group)) == NULL ) {
380    ROAR_ERR("Can not get GID for group %s: %s", group, strerror(errno));
381   }
382  }
383#endif
384#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
385  if ( user ) {
386   if ( (pwd = getpwnam(user)) == NULL ) {
387    ROAR_ERR("Can not get UID for user %s: %s", user, strerror(errno));
388   }
389  }
390#endif
391
392#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
393  if ( *addr == '/' ) {
394   if ( grp || pwd ) {
395     if ( chown(addr, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 )
396      return 1;
397   }
398#ifdef ROAR_HAVE_GETUID
399   if ( grp ) {
400    if ( getuid() == 0 )
401     if ( chmod(addr, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
402      return 1;
403   }
404#endif
405  }
406#endif
407 }
408
409 // in case we opened the listening socket correctly.
410 if ( dir == -1 )
411  dir = ROAR_DIR_PLAY;
412
413 g_listen[sockid].inst.stpl.dir = dir;
414 memcpy(&(g_listen[sockid].inst.stpl.info), info, sizeof(struct roar_audio_info));
415
416 switch (dir) {
417  case ROAR_DIR_PLAY:
418  case ROAR_DIR_RECORD:
419  case ROAR_DIR_MONITOR:
420  case ROAR_DIR_FILTER:
421  case ROAR_DIR_BIDIR:
422    if ( !g_listen[sockid].inst.stpl.info.rate )
423     g_listen[sockid].inst.stpl.info.rate = g_sa->rate;
424
425    if ( !g_listen[sockid].inst.stpl.info.bits )
426     g_listen[sockid].inst.stpl.info.bits = g_sa->bits;
427
428    if ( !g_listen[sockid].inst.stpl.info.channels )
429     g_listen[sockid].inst.stpl.info.channels = g_sa->channels;
430
431    if ( !g_listen[sockid].inst.stpl.info.codec )
432     g_listen[sockid].inst.stpl.info.codec = g_sa->codec;
433   break;
434 }
435
436 server[sockid] = addr;
437 return 0;
438}
439#endif
440
441int update_stream_flags (char * str) {
442 int    dir;
443 char * flags;
444 char * k;
445 int    op;
446 int    flag;
447
448 if ( (flags = strstr(str, "=")) == NULL )
449  return -1;
450
451 *flags = 0;
452  flags++;
453
454 if ( (dir = roar_str2dir(str)) == -1 )
455  return -1;
456
457 while (flags != NULL) {
458  k = flags;
459  flags = strstr(flags, ",");
460
461  if ( flags != NULL )
462   *(flags++) = 0;
463
464  switch (*k) {
465   case '+': k++; op = ROAR_SET_FLAG;   break;
466   case '-': k++; op = ROAR_RESET_FLAG; break;
467   default:
468     op = ROAR_SET_FLAG;
469  }
470
471  flag = 0;
472
473  if ( !strcmp(k, "sync") ) {
474   flag = ROAR_FLAG_SYNC;
475  } else if ( !strcmp(k, "meta") ) {
476   flag = ROAR_FLAG_META;
477  } else if ( !strcmp(k, "cleanmeta") ) {
478   flag = ROAR_FLAG_CLEANMETA;
479  } else if ( !strcmp(k, "pause") ) {
480   flag = ROAR_FLAG_PAUSE;
481  } else if ( !strcmp(k, "mute") ) {
482   flag = ROAR_FLAG_MUTE;
483  } else if ( !strcmp(k, "antiecho") ) {
484   flag = ROAR_FLAG_ANTIECHO;
485  } else if ( !strcmp(k, "passmixer") ) {
486   flag = ROAR_FLAG_PASSMIXER;
487  } else {
488   return -1;
489  }
490
491  g_config->streams[dir].flags |= flag;
492
493  if ( op == ROAR_RESET_FLAG )
494   g_config->streams[dir].flags -= flag;
495 }
496
497 return 0;
498}
499
500int add_output (char * drv, char * dev, char * opts, int prim, int count) {
501 int stream;
502 struct roar_stream * s;
503 struct roar_stream_server * ss;
504 char * k, * v;
505#ifdef ROAR_DRIVER_CODEC
506 char * to_free = NULL;
507#endif
508 int sync = 0, f_mmap = 0;
509 int32_t blocks = -1, blocksize = -1;
510 int dir = ROAR_DIR_OUTPUT;
511 int error = 0;
512 // DMX:
513 int32_t channel  = -1;
514 int32_t universe = -1;
515 uint16_t tu16;
516 float q = -32e6;
517
518 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
519
520 if ( drv == NULL && count == 0 ) {
521  drv  = ROAR_DRIVER_DEFAULT;
522  prim = 1;
523  sync = 1;
524
525#ifdef ROAR_DRIVER_CODEC
526  if ( opts == NULL ) {
527   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
528  }
529#endif
530 }
531
532 if ( opts == NULL && count == 0 ) {
533  sync = 1;
534  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
535 }
536
537 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
538
539 if ( (stream = streams_new()) == -1 ) {
540  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
541  if ( prim ) alive = 0;
542  return -1;
543 }
544
545 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
546
547 streams_get(stream, &ss);
548 s = ROAR_STREAM(ss);
549
550 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
551
552 memset(&(s->info), 0xFF, sizeof(struct roar_audio_info)); // set everything to -1
553
554 s->pos_rel_id = -1;
555// s->info.codec = codec;
556
557 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
558
559 if ( opts == NULL ) {
560  k = NULL;
561 } else {
562  k = strtok(opts, ",");
563 }
564
565 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s'): initial k='%s'(%p)", drv, dev, opts, k, k);
566
567 while (k != NULL) {
568//  ROAR_WARN("add_output(*): opts: %s", k);
569
570  if ( (v = strstr(k, "=")) != NULL ) {
571   *v++ = 0;
572  }
573
574  ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v);
575  if ( strcmp(k, "rate") == 0 ) {
576   s->info.rate = atoi(v);
577  } else if ( strcmp(k, "channels") == 0 ) {
578   s->info.channels = atoi(v);
579  } else if ( strcmp(k, "bits") == 0 ) {
580   s->info.bits = atoi(v);
581  } else if ( strcmp(k, "codec") == 0 ) {
582   if ( (s->info.codec = roar_str2codec(v)) == -1 ) {
583    ROAR_ERR("add_output(*): unknown codec '%s'", v);
584    error++;
585   }
586  } else if ( strcmp(k, "q") == 0 ) {
587   q = atof(v);
588  } else if ( strcmp(k, "blocks") == 0 ) {
589   blocks = atoi(v);
590  } else if ( strcmp(k, "blocksize") == 0 ) {
591   blocksize = atoi(v);
592  } else if ( strcmp(k, "mmap") == 0 ) {
593   f_mmap = 1;
594  } else if ( strcmp(k, "subsystem") == 0 ) {
595   if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) {
596    dir = ROAR_DIR_OUTPUT;
597#ifndef ROAR_WITHOUT_DCOMP_MIDI
598   } else if ( !strcasecmp(v, "midi") ) {
599    dir = ROAR_DIR_MIDI_OUT;
600#endif
601#ifndef ROAR_WITHOUT_DCOMP_LIGHT
602   } else if ( !strcasecmp(v, "light") ) {
603    dir = ROAR_DIR_LIGHT_OUT;
604#endif
605#ifndef ROAR_WITHOUT_DCOMP_RAW
606   } else if ( !strcasecmp(v, "raw") ) {
607    dir = ROAR_DIR_RAW_OUT;
608#endif
609   } else if ( !strcasecmp(v, "complex") ) {
610    dir = ROAR_DIR_COMPLEX_OUT;
611   } else {
612    ROAR_ERR("add_output(*): unknown/unsupported subsystem '%s'", k);
613    error++;
614   }
615  // DMX:
616  } else if ( strcmp(k, "channel") == 0 ) {
617   channel  = atoi(v);
618   if ( channel < 0 || channel > 65535 ) {
619    ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel);
620    channel = -1;
621    error++;
622   }
623  } else if ( strcmp(k, "universe") == 0 ) {
624   universe = atoi(v);
625   if ( universe < 0 || universe > 255 ) {
626    ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe);
627    universe = -1;
628    error++;
629   }
630
631  } else if ( strcmp(k, "name") == 0 ) {
632   if ( streams_set_name(stream, v) == -1 ) {
633    ROAR_ERR("add_output(*): Can not set Stream name");
634    error++;
635   }
636
637  } else if ( strcmp(k, "meta") == 0 ) {
638   streams_set_flag(stream, ROAR_FLAG_META);
639  } else if ( strcmp(k, "sync") == 0 ) {
640   sync = 1;
641  } else if ( strcmp(k, "primary") == 0 ) {
642   prim = 1;
643
644  } else if ( strcmp(k, "cleanmeta") == 0 ) {
645   streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
646  } else if ( strcmp(k, "autoconf") == 0 ) {
647   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
648  } else if ( strcmp(k, "recsource") == 0 ) {
649   streams_set_flag(stream, ROAR_FLAG_RECSOURCE);
650  } else if ( strcmp(k, "passmixer") == 0 ) {
651   streams_set_flag(stream, ROAR_FLAG_PASSMIXER);
652  } else {
653   ROAR_ERR("add_output(*): unknown option '%s'", k);
654   error++;
655  }
656
657  if ( error ) {
658   streams_delete(stream);
659   if ( prim ) alive = 0;
660#ifdef ROAR_DRIVER_CODEC
661   if ( to_free != NULL )
662    free(to_free);
663#endif
664   return -1;
665  }
666
667  k = strtok(NULL, ",");
668 }
669
670 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
671
672 // set audio info...
673 switch (dir) {
674  case ROAR_DIR_LIGHT_OUT:
675    switch (s->info.codec) {
676     case ROAR_CODEC_DMX512:
677     case -1:
678       if ( s->info.rate == -1 )
679        s->info.rate = ROAR_OUTPUT_CFREQ;
680
681       s->info.channels =   0;
682       s->info.bits     =   8;
683       s->info.codec    = ROAR_CODEC_DMX512; // in case codec == -1
684      break;
685    }
686   break;
687  case ROAR_DIR_MIDI_OUT:
688    switch (s->info.codec) {
689     case ROAR_CODEC_MIDI:
690     case -1:
691       if ( s->info.rate == -1 )
692        s->info.rate    = ROAR_MIDI_TICKS_PER_BEAT;
693
694       s->info.channels = ROAR_MIDI_CHANNELS_DEFAULT;
695       s->info.bits     = ROAR_MIDI_BITS;
696       s->info.codec    = ROAR_CODEC_MIDI; // in case codec == -1
697      break;
698    }
699   break;
700  case ROAR_DIR_RAW_OUT:
701    if ( s->info.rate == -1 )
702     s->info.rate = 0;
703    if ( s->info.bits == -1 )
704     s->info.bits = 0;
705    if ( s->info.channels == -1 )
706     s->info.channels = 0;
707    if ( s->info.codec == -1 )
708     s->info.codec = 0;
709   break;
710 }
711
712 if ( s->info.rate == -1 )
713  s->info.rate = g_sa->rate;
714 if ( s->info.bits == -1 )
715  s->info.bits = g_sa->bits;
716 if ( s->info.channels == -1 )
717  s->info.channels = g_sa->channels;
718 if ( s->info.codec == -1 )
719  s->info.codec = g_sa->codec;
720
721 ROAR_DBG("add_output(*): s->info = {.rate=%i, .bits=%i, .channels=%i, .codec=%i}", s->info.rate, s->info.bits, s->info.channels, s->info.codec);
722
723 if ( streams_set_dir(stream, dir, 1) == -1 ) {
724  streams_delete(stream);
725  return -1;
726 }
727
728#ifdef ROAR_DRIVER_CODEC
729 if ( to_free != NULL )
730  free(to_free);
731#endif
732
733 if ( s->info.codec == ROAR_CODEC_ALAW || s->info.codec == ROAR_CODEC_MULAW )
734  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
735
736 ROAR_STREAM_SERVER(s)->codec_orgi = s->info.codec;
737
738 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) {
739  ss->driver_id = -1; // don't close a driver not opened...
740  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
741  streams_delete(stream);
742  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
743  if ( prim ) alive = 0;
744  return -1;
745 }
746
747 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here
748 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM,   s); // ignore errors here
749
750 if ( blocks != -1 )
751  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
752
753 if ( blocksize != -1 )
754  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
755
756 // TODO: we shoudld *really* check for errors here...
757 if ( channel != -1 ) {
758  tu16 = channel;
759  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16);
760 }
761 if ( universe != -1 ) {
762  tu16 = universe;
763  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16);
764 }
765
766 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
767
768 streams_set_fh(stream, -1); // update some internal structures
769
770 if ( q > -1e6 ) {
771  ROAR_DBG("add_output(*): setting q=%f", q);
772  streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q);
773 }
774
775 client_stream_add(g_self_client, stream);
776
777 if ( prim ) {
778  streams_mark_primary(stream);
779  s->pos_rel_id = stream;
780 }
781
782 if ( sync ) {
783  streams_set_flag(stream, ROAR_FLAG_SYNC);
784 } else {
785  streams_reset_flag(stream, ROAR_FLAG_SYNC);
786 }
787
788 if ( f_mmap )
789  streams_set_flag(stream, ROAR_FLAG_MMAP);
790
791 ROAR_DBG("add_output(*): s->info = {.rate=%i, .bits=%i, .channels=%i, .codec=%i}", s->info.rate, s->info.bits, s->info.channels, s->info.codec);
792 return 0;
793}
794
795// X11:
796#ifdef ROAR_HAVE_LIBX11
797int register_x11 (int unreg, char * sockname) {
798 struct roar_x11_connection * x11con = NULL;
799 int ret = 0;
800
801 if ( (x11con = roar_x11_connect(x11display)) == NULL )
802  return -1;
803
804 if ( unreg ) {
805  if ( roar_x11_delete_prop(x11con, "ROAR_SERVER") == -1 )
806   ret = -1;
807 } else {
808  if ( roar_x11_set_prop(x11con, "ROAR_SERVER", sockname) == -1 )
809   ret = -1;
810 }
811
812 roar_x11_disconnect(x11con);
813
814 return ret;
815}
816#endif
817
818// SLP:
819void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
820 /* return the error code in the cookie */
821 *(SLPError*)cookie = errcode;
822}
823
824int register_slp (int unreg, char * sockname) {
825#ifdef ROAR_HAVE_LIBSLP
826 static int regged = 0;
827 static char * sn = NULL;
828 SLPError err;
829 SLPError callbackerr;
830 SLPHandle hslp;
831 char addr[1024];
832 char attr[1024] = "";
833 char * location;
834
835 if ( sockname != NULL )
836  sn = sockname;
837
838 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE_ROAR "://%s", sn);
839
840 err = SLPOpen("en", SLP_FALSE, &hslp);
841
842 if (err != SLP_OK) {
843  ROAR_ERR("Error opening slp handle: Error #%i", err);
844  return -1;
845 }
846
847 if (!unreg) {
848
849  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
850   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
851   SLPClose(hslp);
852   return -1;
853  }
854
855  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
856#ifndef ROAR_WITHOUT_DCOMP_LIGHT
857                               "(light-channels=%i),"
858#endif
859                               "(location=%s)",
860           g_sa->rate, g_sa->channels, g_sa->bits,
861#ifndef ROAR_WITHOUT_DCOMP_LIGHT
862           g_light_state.channels,
863#endif
864           location
865          );
866
867  /* Register a service with SLP */
868  err = SLPReg(hslp,
869               addr,
870               SLP_LIFETIME_MAXIMUM,
871               0,
872               attr,
873               SLP_TRUE,
874               register_slp_callback,
875               &callbackerr);
876  regged = 1;
877 } else if ( unreg && regged ) {
878  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
879  regged = 0;
880 } else {
881  SLPClose(hslp);
882  return -1;
883 }
884
885 /* err may contain an error code that occurred as the slp library    */
886 /* _prepared_ to make the call.                                     */
887 if ( err != SLP_OK ) {
888  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
889  return -1;
890 }
891
892 /* callbackerr may contain an error code (that was assigned through */
893 /* the callback cookie) that occurred as slp packets were sent on    */
894 /* the wire */
895 if (callbackerr != SLP_OK) {
896  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
897  return -1;
898 }
899
900 SLPClose(hslp);
901 return 0;
902#else
903 return -1;
904#endif
905}
906
907
908// MAIN:
909
910#ifdef ROAR_HAVE_MAIN_ARGS
911int main (int argc, char * argv[]) {
912#else
913int main (void) {
914#endif
915#ifdef ROAR_HAVE_MAIN_ARGS
916 int i;
917 char * k;
918#endif
919#if defined(ROAR_SUPPORT_LISTEN) && defined(ROAR_HAVE_GETUID)
920 char user_sock[80]  = {0};
921#endif
922 struct roar_audio_info sa, max_sa;
923 struct roard_config config;
924#ifdef ROAR_HAVE_FORK
925 int    daemon       = 0;
926#endif
927 int    realtime     = 0;
928 int    sysclocksync = 0;
929 char * driver    = NULL;
930 char * device    = NULL;
931#ifdef ROAR_HAVE_MAIN_ARGS
932 char * opts      = NULL;
933#endif
934// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
935#ifdef ROAR_SUPPORT_LISTEN
936 int    port       = ROAR_DEFAULT_PORT;
937 char * sock_addr  = NULL;
938 int    sock_proto = ROAR_PROTO_ROARAUDIO;
939 int    sock_dir   = -1;
940 struct roar_audio_info sock_info = {0, 0, 0, 0};
941#endif
942 int               drvid;
943#ifndef ROAR_WITHOUT_DCOMP_SOURCES
944 char * s_drv     = "cf";
945 char * s_dev     = NULL;
946 char * s_con     = NULL;
947 char * s_opt     = NULL;
948 int    s_prim    = 0;
949#endif
950 char * o_drv     = getenv("ROAR_DRIVER");
951 char * o_dev     = getenv("ROAR_DEVICE");
952 char * o_opts    = NULL;
953 int    o_prim    = 0;
954 int    o_count   = 0;
955#ifndef ROAR_WITHOUT_DCOMP_LIGHT
956 int    light_channels = LIGHT_CHANNELS_DEFAULT;
957#endif
958#ifdef ROAR_DEFAULT_SOCKGRP
959 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
960#else
961 char * sock_grp  = NULL;
962#endif
963 char * sock_user = NULL;
964#ifdef ROAR_SUPPORT_LISTEN
965 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
966#endif
967#ifdef ROAR_HAVE_LIBSLP
968 int    reg_slp   = 0;
969#endif
970#ifdef ROAR_HAVE_LIBX11
971 int    reg_x11   = 0;
972#endif
973#ifdef ROAR_HAVE_CHROOT
974 char * chrootdir = NULL;
975#endif
976#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
977 struct group   * grp  = NULL;
978#endif
979#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
980 struct passwd  * pwd  = NULL;
981#endif
982#ifdef ROAR_HAVE_GETSERVBYNAME
983 struct servent * serv = NULL;
984#endif
985 DRIVER_USERDATA_T drvinst;
986 struct roar_client * self = NULL;
987#ifdef ROAR_HAVE_LIBDNET
988 char decnethost[80];
989#endif
990#ifdef SUPPORT_PIDFILE
991 struct roar_vio_calls pidfile_vio;
992#endif
993
994 ROAR_DBG("main(*): starting roard...");
995
996 g_standby       =  0;
997 g_autostandby   =  0;
998 alive           =  1;
999#ifdef ROAR_SUPPORT_LISTEN
1000 g_no_listen     =  0;
1001#else
1002 g_terminate     =  1;
1003#endif
1004
1005 g_verbose       = ROAR_DBG_INFO_NONE;
1006
1007 sa.bits     = ROAR_BITS_DEFAULT;
1008 sa.channels = ROAR_CHANNELS_DEFAULT;
1009 sa.rate     = ROAR_RATE_DEFAULT;
1010 sa.codec    = ROAR_CODEC_DEFAULT;
1011
1012 g_sa        = &sa;
1013 g_max_sa    = &max_sa;
1014
1015 memcpy(g_max_sa, g_sa, sizeof(max_sa));
1016
1017 g_config = &config;
1018
1019 if ( init_config() == -1 ) {
1020  ROAR_ERR("Can not init default config!");
1021  return 1;
1022 }
1023
1024 // load config
1025 roar_libroar_get_config();
1026
1027#ifdef ROAR_SUPPORT_LISTEN
1028 if ( init_listening() == -1 ) {
1029  ROAR_ERR("Can not init listening sockets!");
1030  return 1;
1031 }
1032#endif
1033
1034#ifndef ROAR_WITHOUT_DCOMP_MIDI
1035 if ( midi_init_config() == -1 ) {
1036  ROAR_ERR("Can not init MIDI config!");
1037  return 1;
1038 }
1039#endif
1040
1041#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1042 if ( ssynth_init_config() == -1 ) {
1043  ROAR_ERR("Can not init ssynth config!");
1044  return 1;
1045 }
1046#endif
1047
1048#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1049 if ( rdtcs_init_config() == -1 ) {
1050  ROAR_ERR("Can not init RDTCS config!");
1051  return 1;
1052 }
1053#endif
1054
1055 if ( plugins_preinit() == -1 ) {
1056  ROAR_ERR("Can not pre-init plugins!");
1057  return 1;
1058 }
1059
1060#ifdef ROAR_SUPPORT_LISTEN
1061#ifndef ROAR_TARGET_WIN32
1062 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
1063#else
1064 sock_addr = ROAR_DEFAULT_HOST;
1065#endif
1066
1067#ifdef ROAR_HAVE_GETUID
1068 if ( getuid() != 0 && getenv("HOME") != NULL ) {
1069  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
1070  sock_addr = user_sock;
1071 }
1072#endif
1073
1074 if ( getenv("ROAR_SERVER") != NULL )
1075  sock_addr = getenv("ROAR_SERVER");
1076#endif
1077
1078 if ( clients_init() == -1 ) {
1079  ROAR_ERR("Can not init clients!");
1080  return 1;
1081 }
1082
1083 if ( streams_init() == -1 ) {
1084  ROAR_ERR("Can not init streams!");
1085  return 1;
1086 }
1087
1088 if ( (g_self_client = clients_new()) == -1 ) {
1089  ROAR_ERR("Can not create self client!");
1090  return 1;
1091 }
1092
1093#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1094 if ( sources_init() == -1 ) {
1095  ROAR_ERR("Can not init sources!");
1096  return 1;
1097 }
1098
1099 if ( (sources_set_client(g_self_client)) == -1 ) {
1100  ROAR_ERR("Can not init set source client!");
1101  return 1;
1102 }
1103#endif
1104
1105#ifdef ROAR_HAVE_MAIN_ARGS
1106 for (i = 1; i < argc; i++) {
1107  k = argv[i];
1108
1109  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
1110   usage();
1111   return 0;
1112
1113  } else if ( strcmp(k, "--start") == 0 ) {
1114   // this is a no op
1115  } else if ( strcmp(k, "--restart") == 0 ) {
1116#ifdef ROAR_SUPPORT_LISTEN
1117   if ( restart_server(sock_addr, 1) == -1 ) {
1118    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", sock_addr);
1119   }
1120#else
1121   ROAR_ERR("--restart not supported");
1122#endif
1123  } else if ( strcmp(k, "--shutdown") == 0 ) {
1124#ifdef ROAR_SUPPORT_LISTEN
1125   if ( restart_server(sock_addr, 1) == -1 ) {
1126    ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
1127    return 1;
1128   }
1129   return 0;
1130#else
1131   ROAR_ERR("--shutdown not supported");
1132   return 1;
1133#endif
1134  } else if ( strcmp(k, "--stop") == 0 ) {
1135#ifdef ROAR_SUPPORT_LISTEN
1136   if ( restart_server(sock_addr, 0) == -1 ) {
1137    ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
1138    return 1;
1139   }
1140   return 0;
1141#else
1142   ROAR_ERR("--stop not supported");
1143   return 1;
1144#endif
1145
1146
1147  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
1148#ifdef ROAR_HAVE_FORK
1149   daemon = 1;
1150#else
1151   ROAR_ERR("--daemon not supported");
1152#endif
1153  } else if ( strcmp(k, "--verbose") == 0 ) {
1154   g_verbose++;
1155  } else if ( strcmp(k, "--terminate") == 0 ) {
1156   g_terminate = 1;
1157  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1158   sysclocksync = 1000;
1159  } else if ( strcmp(k, "--realtime") == 0 ) {
1160   realtime++;
1161  } else if ( strcmp(k, "--chroot") == 0 ) {
1162#ifdef ROAR_HAVE_CHROOT
1163   chrootdir = argv[++i];
1164#else
1165   ROAR_ERR("--chroot not supported");
1166   i++;
1167#endif
1168  } else if ( strcmp(k, "--setgid") == 0 ) {
1169#ifdef ROAR_HAVE_SETGID
1170   setids |= R_SETGID;
1171#else
1172   ROAR_ERR("--setgid not supported");
1173#endif
1174  } else if ( strcmp(k, "--setuid") == 0 ) {
1175#ifdef ROAR_HAVE_SETUID
1176   setids |= R_SETUID;
1177#else
1178   ROAR_ERR("--setuid not supported");
1179#endif
1180  } else if ( strcmp(k, "--location") == 0 ) {
1181   g_config->location = argv[++i];
1182  } else if ( strcmp(k, "--pidfile") == 0 ) {
1183#ifdef SUPPORT_PIDFILE
1184   pidfile = argv[++i];
1185#else
1186   ROAR_ERR("--pidfile not supported");
1187   i++;
1188#endif
1189  } else if ( strcmp(k, "--log-syslog") == 0 ) {
1190#ifdef ROAR_HAVE_SYSLOG
1191   roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
1192#else
1193   ROAR_ERR("--log-syslog not supported");
1194#endif
1195
1196
1197  } else if ( strcmp(k, "--plugin-load") == 0 ) {
1198   if ( plugins_load(argv[++i]) == -1 ) {
1199    ROAR_ERR("Can not load plugin");
1200   }
1201
1202  } else if ( strcmp(k, "--list-cf") == 0 ) {
1203   print_codecfilterlist();
1204   return 0;
1205
1206  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
1207   sa.rate = atoi(argv[++i]);
1208  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
1209   sa.bits = atoi(argv[++i]);
1210  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
1211   sa.channels = atoi(argv[++i]);
1212
1213  } else if ( strcmp(k, "--stream-flags") == 0 ) {
1214   if ( update_stream_flags(argv[++i]) == -1 ) {
1215    ROAR_ERR("Can not set stream flags");
1216    return 1;
1217   }
1218
1219  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
1220   driver = argv[++i];
1221   if ( strcmp(driver, "list") == 0 ) {
1222    ROAR_WARN("The option is obsolete, use --list-driver!");
1223    print_driverlist();
1224    return 0;
1225   }
1226  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
1227   device = argv[++i];
1228  } else if ( strcmp(k, "-dO") == 0 ) {
1229   opts = argv[++i];
1230  } else if ( strcmp(k, "--list-driver") == 0 ) {
1231   print_driverlist();
1232   return 0;
1233
1234  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
1235   o_drv  = argv[++i];
1236  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
1237   o_dev  = argv[++i];
1238  } else if ( strcmp(k, "-oO") == 0 ) {
1239   o_opts = argv[++i];
1240  } else if ( strcmp(k, "-oP") == 0 ) {
1241   o_prim = 1;
1242  } else if ( strcmp(k, "-oN") == 0 ) {
1243   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1244    o_count++;
1245
1246   o_drv  = o_dev = o_opts = NULL;
1247   o_prim = 0;
1248
1249  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
1250#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1251   s_drv = argv[++i];
1252#else
1253   ROAR_ERR("main(*): No support for sources compiled in");
1254#endif
1255  } else if ( strcmp(k, "-S") == 0 ) {
1256#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1257   s_dev = argv[++i];
1258#else
1259   ROAR_ERR("main(*): No support for sources compiled in");
1260#endif
1261  } else if ( strcmp(k, "-sO") == 0 ) {
1262#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1263   s_opt = argv[++i];
1264#else
1265   ROAR_ERR("main(*): No support for sources compiled in");
1266#endif
1267  } else if ( strcmp(k, "-sC") == 0 ) {
1268#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1269   s_con = argv[++i];
1270#else
1271   ROAR_ERR("main(*): No support for sources compiled in");
1272#endif
1273  } else if ( strcmp(k, "-sP") == 0 ) {
1274#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1275   s_prim = 1;
1276#else
1277   ROAR_ERR("main(*): No support for sources compiled in");
1278#endif
1279  } else if ( strcmp(k, "-sN") == 0 ) {
1280#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1281   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1282    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1283   }
1284   s_opt = s_dev = s_con = NULL;
1285   s_drv = "cf";
1286   s_prim = 0;
1287#else
1288   ROAR_ERR("main(*): No support for sources compiled in");
1289#endif
1290  } else if ( strcmp(k, "--list-sources") == 0 ) {
1291#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1292   print_sourcelist();
1293   return 0;
1294#else
1295   ROAR_ERR("main(*): No support for sources compiled in");
1296   return 1;
1297#endif
1298
1299  } else if ( strcmp(k, "--light-channels") == 0 ) {
1300#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1301   light_channels = atoi(argv[++i]);
1302#else
1303   ROAR_WARN("main(*): no light subsystem compiled in");
1304#endif
1305
1306  } else if ( strcmp(k, "--rds-pi") == 0 ) {
1307#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1308   g_rdtcs.rds.pi = atoi(argv[++i]);
1309#else
1310   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1311#endif
1312  } else if ( strcmp(k, "--rds-ps") == 0 ) {
1313#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1314   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
1315    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]);
1316    return 1;
1317   }
1318#else
1319   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1320#endif
1321  } else if ( strcmp(k, "--rds-pty") == 0 ) {
1322#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1323   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
1324    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
1325    return 1;
1326   }
1327#else
1328   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1329#endif
1330  } else if ( strcmp(k, "--rds-tp") == 0 ) {
1331#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1332   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) {
1333    ROAR_ERR("Can not set RDS TP flag");
1334    return 1;
1335   }
1336#else
1337   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1338#endif
1339  } else if ( strcmp(k, "--rds-ct") == 0 ) {
1340#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1341   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) {
1342    ROAR_ERR("Can not set RDS CT flag");
1343    return 1;
1344   }
1345#else
1346   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1347#endif
1348
1349
1350  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
1351#ifndef ROAR_WITHOUT_DCOMP_CB
1352   midi_config.init_cb = 0;
1353#else
1354   // no warning here as this is the disable option
1355#endif
1356  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
1357#ifndef ROAR_WITHOUT_DCOMP_CB
1358   midi_config.init_cb = 1;
1359#else
1360   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1361#endif
1362  } else if ( strcmp(k, "--midi-console") == 0 ) {
1363#ifndef ROAR_WITHOUT_DCOMP_CB
1364   midi_config.console_dev = argv[++i];
1365   midi_config.init_cb = 1;
1366#else
1367   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1368#endif
1369
1370  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
1371#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1372   ssynth_conf.enable = 1;
1373#else
1374   ROAR_ERR("main(*): No support for ssynth compiled in");
1375#endif
1376  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
1377#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1378   ssynth_conf.enable = 0;
1379#else
1380   // we can safely ignore the disable
1381#endif
1382
1383  } else if ( strcmp(k, "--x11-display") == 0 || strcmp(k, "--display") == 0 ) {
1384#ifdef ROAR_HAVE_LIBX11
1385   x11display = argv[++i];
1386#else
1387   ROAR_ERR("No X11 support compiled in!");
1388   return 1;
1389#endif
1390
1391
1392  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
1393   // This is only usefull in INET not UNIX mode.
1394#ifdef ROAR_SUPPORT_LISTEN
1395   if ( *sock_addr == '/' )
1396    sock_addr = ROAR_DEFAULT_HOST;
1397
1398   errno = 0;
1399   if ( (port = atoi(argv[++i])) < 1 ) {
1400#ifdef ROAR_HAVE_GETSERVBYNAME
1401    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
1402     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
1403     return 1;
1404    }
1405    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
1406    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
1407            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
1408    port = ROAR_NET2HOST16(serv->s_port);
1409#else
1410    ROAR_ERR("invalite port number: %s", argv[i]);
1411    return 1;
1412#endif
1413   }
1414#endif
1415  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
1416#ifdef ROAR_SUPPORT_LISTEN
1417   sock_addr = argv[++i];
1418#endif
1419
1420  } else if ( strcmp(k, "--proto") == 0 ) {
1421#ifdef ROAR_SUPPORT_LISTEN
1422   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
1423    ROAR_ERR("Unknown protocol: %s", argv[i]);
1424    return 1;
1425   }
1426#endif
1427  } else if ( strcmp(k, "--proto-dir") == 0 ) {
1428#ifdef ROAR_SUPPORT_LISTEN
1429   if ( (sock_dir = roar_str2dir(argv[++i])) == -1 ) {
1430    ROAR_ERR("Unknown stream direction: %s", argv[i]);
1431    return 1;
1432   }
1433#endif
1434  } else if ( strcmp(k, "--proto-rate") == 0 ) {
1435#ifdef ROAR_SUPPORT_LISTEN
1436   sock_info.rate = atoi(argv[++i]);
1437#endif
1438  } else if ( strcmp(k, "--proto-bits") == 0 ) {
1439#ifdef ROAR_SUPPORT_LISTEN
1440   sock_info.bits = atoi(argv[++i]);
1441#endif
1442  } else if ( strcmp(k, "--proto-chans") == 0 ) {
1443#ifdef ROAR_SUPPORT_LISTEN
1444   sock_info.channels = atoi(argv[++i]);
1445#endif
1446  } else if ( strcmp(k, "--proto-codec") == 0 ) {
1447#ifdef ROAR_SUPPORT_LISTEN
1448   if ( (sock_info.codec = roar_str2codec(argv[++i])) == -1 ) {
1449    ROAR_ERR("Unknown codec: %s", argv[i]);
1450    return 1;
1451   }
1452#endif
1453
1454
1455  } else if ( strcmp(k, "--list-proto") == 0 ) {
1456   list_proto();
1457   return 0;
1458
1459  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
1460#ifdef ROAR_SUPPORT_LISTEN
1461   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
1462    sock_type = ROAR_SOCKET_TYPE_TCP;
1463
1464   if ( *sock_addr == '/' )
1465    sock_addr = ROAR_DEFAULT_HOST;
1466#endif
1467
1468  } else if ( strcmp(k, "-4") == 0 ) {
1469#ifdef ROAR_SUPPORT_LISTEN
1470   sock_type = ROAR_SOCKET_TYPE_TCP;
1471   if ( *sock_addr == '/' )
1472    sock_addr = ROAR_DEFAULT_HOST;
1473#endif
1474  } else if ( strcmp(k, "-6") == 0 ) {
1475#ifdef ROAR_SUPPORT_LISTEN
1476#ifdef PF_INET6
1477   sock_type = ROAR_SOCKET_TYPE_TCP6;
1478   if ( *sock_addr == '/' )
1479    sock_addr = ROAR_DEFAULT_HOST;
1480#else
1481    ROAR_ERR("No IPv6 support compiled in!");
1482    return 1;
1483#endif
1484#endif
1485
1486  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
1487#ifdef ROAR_SUPPORT_LISTEN
1488   // ignore this case as it is the default behavor.
1489   sock_type = ROAR_SOCKET_TYPE_UNIX;
1490#endif
1491
1492  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
1493#ifdef ROAR_SUPPORT_LISTEN
1494#ifdef ROAR_HAVE_LIBDNET
1495    port   = ROAR_DEFAULT_NUM;
1496    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
1497    sock_addr = decnethost;
1498    sock_type = ROAR_SOCKET_TYPE_DECNET;
1499#else
1500    ROAR_ERR("No DECnet support compiled in!");
1501    return 1;
1502#endif
1503#endif
1504  } else if ( strcmp(k, "--new-sock") == 0 ) {
1505#ifdef ROAR_SUPPORT_LISTEN
1506   if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
1507    ROAR_ERR("Can not open listen socket!");
1508    return 1;
1509   }
1510#endif
1511
1512  } else if ( strcmp(k, "--slp") == 0 ) {
1513#ifdef ROAR_HAVE_LIBSLP
1514   reg_slp = 1;
1515#else
1516   ROAR_ERR("No OpenSLP support compiled in!");
1517   return 1;
1518#endif
1519
1520  } else if ( strcmp(k, "--x11") == 0 ) {
1521#ifdef ROAR_HAVE_LIBX11
1522   reg_x11 = 1;
1523#else
1524   ROAR_ERR("No X11 support compiled in!");
1525   return 1;
1526#endif
1527
1528
1529  } else if ( strcmp(k, "--jumbo-mtu") == 0 ) {
1530   g_config->jumbo_mtu = atoi(argv[++i]);
1531
1532  } else if ( strcmp(k, "-G") == 0 ) {
1533   sock_grp  = argv[++i];
1534  } else if ( strcmp(k, "-U") == 0 ) {
1535   sock_user = argv[++i];
1536
1537  } else if ( strcmp(k, "--no-listen") == 0 ) {
1538#ifdef ROAR_SUPPORT_LISTEN
1539   sock_addr   = "";
1540   g_terminate = 1;
1541   g_no_listen = 1;
1542#endif
1543  } else if ( strcmp(k, "--client-fh") == 0 ) {
1544   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
1545    ROAR_ERR("main(*): Can not set client's fh");
1546    return 1;
1547   }
1548  } else if ( strcmp(k, "--close-fh") == 0 ) {
1549#ifdef ROAR_HAVE_IO_POSIX
1550   close(atoi(argv[++i]));
1551#else
1552   i++;
1553   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
1554#endif
1555
1556  } else if ( strcmp(k, "--standby") == 0 ) {
1557   g_standby = 1;
1558  } else if ( strcmp(k, "--auto-standby") == 0 ) {
1559   g_autostandby = 1;
1560  } else {
1561   usage();
1562   return 1;
1563  }
1564
1565 }
1566#endif
1567
1568#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1569 if ( s_dev != NULL ) {
1570  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1571   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1572  }
1573 }
1574#endif
1575
1576 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
1577
1578 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
1579
1580 if ( waveform_init() == -1 ) {
1581  ROAR_ERR("Can not initialize Waveform subsystem");
1582  return 1;
1583 }
1584
1585#ifndef ROAR_WITHOUT_DCOMP_MIDI
1586 if ( midi_init() == -1 ) {
1587  ROAR_ERR("Can not initialize MIDI subsystem");
1588 }
1589#endif
1590
1591#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1592 if ( ssynth_init() == -1 ) {
1593  ROAR_ERR("Can not initialize ssynth subsystem");
1594 }
1595#endif
1596
1597#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1598 if ( light_init(light_channels) == -1 ) {
1599  ROAR_ERR("Can not initialize light control subsystem");
1600 }
1601#endif
1602
1603#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1604 if ( rdtcs_init() == -1 ) {
1605  ROAR_ERR("Can not initialize RDTCS subsystem");
1606 }
1607#endif
1608
1609 if ( plugins_init() == -1 ) {
1610  ROAR_ERR("Can not initialize plugins");
1611 }
1612
1613#ifdef ROAR_SUPPORT_LISTEN
1614 if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
1615  ROAR_ERR("Can not open listen socket!");
1616  return 1;
1617 }
1618#endif
1619
1620 if ( output_buffer_init(&sa) == -1 ) {
1621  ROAR_ERR("Can not init output buffer!");
1622  return 1;
1623 }
1624
1625 if ( driver == NULL ) {
1626  driver = "null";
1627 } else {
1628  ROAR_WARN("Usage of old driver interface. use -o not -d!");
1629 }
1630
1631 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
1632  ROAR_ERR("Can not open output driver!");
1633  return 1;
1634 }
1635
1636 if ( samples_init() == -1 ) {
1637  ROAR_ERR("Can not init samples!");
1638  return 1;
1639 }
1640
1641
1642 // we should handle this on microcontrollers, too.
1643#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
1644 signal(SIGINT,  on_sig_int);
1645 signal(SIGTERM, on_sig_term);
1646 signal(SIGCHLD, on_sig_chld);
1647 signal(SIGUSR1, on_sig_usr1);
1648 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
1649#endif
1650
1651 if ( realtime ) {
1652#ifdef DEBUG
1653  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
1654#endif
1655
1656#ifdef ROAR_HAVE_NICE
1657  errno = 0;
1658  nice(-5*realtime); // -5 for each --realtime
1659  if ( errno ) {
1660   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1661  }
1662#else
1663  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1664#endif
1665/*
1666#ifdef __linux__
1667  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
1668   ROAR_WARN("Can not set io priority: %s", strerror(errno));
1669#endif
1670*/
1671 }
1672
1673#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
1674 if ( setids & R_SETGID ) {
1675  if ( sock_grp == NULL ) {
1676   ROAR_ERR("Can not set GID if no groupname is supplied");
1677   return 1;
1678  }
1679  if ( (grp = getgrnam(sock_grp)) == NULL ) {
1680   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
1681   return 1;
1682  }
1683  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
1684   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
1685  }
1686  if ( !grp || setgid(grp->gr_gid) == -1 ) {
1687   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
1688  }
1689 }
1690#endif
1691
1692
1693 clients_set_pid(g_self_client, getpid());
1694#ifdef ROAR_HAVE_GETUID
1695 clients_set_uid(g_self_client, getuid());
1696#endif
1697#ifdef ROAR_HAVE_GETGID
1698 clients_set_gid(g_self_client, getgid());
1699#endif
1700 clients_get(g_self_client, &self);
1701
1702 if ( self == NULL ) {
1703  ROAR_ERR("Can not get self client!");
1704  return 1;
1705 }
1706
1707 strcpy(self->name, "RoarAudio daemon internal");
1708
1709 if ( roar_nnode_free(&(self->nnode)) == -1 )
1710  return 1;
1711
1712 // not fully correct but ok as workaorund
1713 // so tools assume that roard runs on the same machine as
1714 // they in case they use AF_UNIX:
1715 if ( roar_nnode_new(&(self->nnode), ROAR_SOCKET_TYPE_UNIX) == -1 )
1716  return 1;
1717
1718#ifdef ROAR_HAVE_FORK
1719 if ( daemon ) {
1720#ifdef ROAR_HAVE_SYSLOG
1721  roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
1722#else
1723  roar_debug_set_stderr_fh(-1);
1724#endif
1725
1726  close(ROAR_STDIN );
1727  close(ROAR_STDOUT);
1728  close(ROAR_STDERR);
1729
1730  if ( fork() )
1731   ROAR_U_EXIT(0);
1732
1733#ifdef ROAR_HAVE_SETSID
1734  setsid();
1735#endif
1736  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
1737 }
1738#endif
1739
1740#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1741 // early test for UID as we need this for the pidfile and the setuid()
1742 if ( sock_user != NULL ) {
1743  if ( (pwd = getpwnam(sock_user)) == NULL ) {
1744   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
1745   return 1;
1746  }
1747 }
1748#endif
1749
1750#ifdef SUPPORT_PIDFILE
1751 if ( pidfile != NULL ) {
1752  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
1753   ROAR_ERR("Can not write pidfile: %s", pidfile);
1754  } else {
1755   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
1756   roar_vio_close(&pidfile_vio);
1757  }
1758#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1759  if ( pwd || grp ) {
1760   if ( chown(pidfile, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 ) {
1761    ROAR_WARN("Can not change ownership of pidfile: %s: %s", pidfile, strerror(errno));
1762   }
1763  }
1764  if ( chmod(pidfile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1 ) {
1765   ROAR_WARN("Can not change permissions of pidfile: %s: %s", pidfile, strerror(errno));
1766  }
1767#endif
1768 }
1769#endif
1770
1771#ifdef ROAR_HAVE_CHROOT
1772 if (chrootdir) {
1773  if ( chroot(chrootdir) == -1 ) {
1774   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
1775   return 2;
1776  }
1777  if ( chdir("/") == -1 ) {
1778   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
1779   return 2;
1780  }
1781 }
1782#endif
1783
1784#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1785 if ( setids & R_SETUID ) {
1786  if ( sock_user == NULL ) {
1787   ROAR_ERR("Can not set UID if no username is supplied");
1788   return 1;
1789  }
1790  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
1791   ROAR_ERR("Can not set UserID: %s", strerror(errno));
1792   return 3;
1793  }
1794#ifdef ROAR_HAVE_GETUID
1795  clients_set_uid(g_self_client, getuid());
1796#endif
1797 }
1798#endif
1799
1800 // Register with OpenSLP:
1801#ifdef ROAR_HAVE_LIBSLP
1802 if ( reg_slp ) {
1803  register_slp(0, sock_addr);
1804 }
1805#endif
1806
1807#ifdef ROAR_HAVE_LIBX11
1808 if ( reg_x11 ) {
1809  register_x11(0, sock_addr);
1810 }
1811#endif
1812
1813 // start main loop...
1814 main_loop(drvid, drvinst, &sa, sysclocksync);
1815
1816 // clean up.
1817 clean_quit_prep();
1818 driver_close(drvinst, drvid);
1819 output_buffer_free();
1820
1821 return 0;
1822}
1823
1824void cleanup_listen_socket (int terminate) {
1825 int i;
1826
1827 // Deregister from SLP:
1828#ifdef ROAR_HAVE_LIBSLP
1829 register_slp(1, NULL);
1830#endif
1831
1832#ifdef ROAR_HAVE_LIBX11
1833 register_x11(1, NULL);
1834#endif
1835
1836#ifdef ROAR_SUPPORT_LISTEN
1837 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
1838  if ( g_listen[i].socket != -1 ) {
1839#ifdef ROAR_HAVE_IO_POSIX
1840   close(g_listen[i].socket);
1841#endif // #else is useless because we are in void context.
1842
1843   g_listen[i].socket = -1;
1844
1845#ifdef ROAR_HAVE_UNIX
1846   if ( server[i] != NULL )
1847    if ( *(server[i]) == '/' )
1848     unlink(server[i]);
1849#endif
1850  }
1851 }
1852
1853#endif
1854
1855 if ( terminate )
1856  g_terminate = 1;
1857}
1858
1859void clean_quit_prep (void) {
1860 cleanup_listen_socket(0);
1861
1862 plugins_free();
1863
1864#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1865 sources_free();
1866#endif
1867 streams_free();
1868 clients_free();
1869#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1870 ssynth_free();
1871#endif
1872#ifndef ROAR_WITHOUT_DCOMP_CB
1873 midi_cb_stop(); // stop console beep
1874#endif
1875#ifndef ROAR_WITHOUT_DCOMP_MIDI
1876 midi_free();
1877#endif
1878#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1879 light_free();
1880#endif
1881#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1882 rdtcs_free();
1883#endif
1884
1885 waveform_free();
1886
1887#ifdef SUPPORT_PIDFILE
1888 if ( pidfile != NULL )
1889  unlink(pidfile);
1890#endif
1891}
1892
1893void clean_quit (void) {
1894 clean_quit_prep();
1895// driver_close(drvinst, drvid);
1896// output_buffer_free();
1897 exit(0);
1898}
1899
1900//ll
Note: See TracBrowser for help on using the repository browser.