source: roaraudio/roard/roard.c @ 3354:c0856cfec872

Last change on this file since 3354:c0856cfec872 was 3354:c0856cfec872, checked in by phi, 14 years ago

preinit, init, free plugins

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