source: roaraudio/roard/roard.c @ 3256:781ecf0a7bcf

Last change on this file since 3256:781ecf0a7bcf was 3256:781ecf0a7bcf, checked in by phi, 14 years ago

some defaults

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