source: roaraudio/roard/roard.c @ 3358:7f9d211148e0

Last change on this file since 3358:7f9d211148e0 was 3358:7f9d211148e0, checked in by phi, 14 years ago

updated (C) statements

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