source: roaraudio/roard/roard.c @ 3127:09663de087b8

Last change on this file since 3127:09663de087b8 was 3127:09663de087b8, checked in by phi, 14 years ago

added --list-proto

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