source: roaraudio/roard/roard.c @ 2727:f95c3e9cc1c8

Last change on this file since 2727:f95c3e9cc1c8 was 2725:acd2b121bd92, checked in by phi, 15 years ago

added --rds-pi, list of other options

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