source: roaraudio/roard/roard.c @ 2530:921f7ac0be31

Last change on this file since 2530:921f7ac0be31 was 2530:921f7ac0be31, checked in by phi, 15 years ago

added support for multi homed server

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