source: roaraudio/roard/roard.c @ 2787:c44a97701300

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

only define user_sock var if we need it

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