source: roaraudio/roard/roard.c @ 2681:359a85bcf5e9

Last change on this file since 2681:359a85bcf5e9 was 2681:359a85bcf5e9, checked in by phi, 15 years ago

added new subsystem complex

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