source: roaraudio/roard/roard.c @ 2681:359a85bcf5e9

Last change on this file since 2681:359a85bcf5e9 was 2681:359a85bcf5e9, checked in by phi, 15 years ago

added new subsystem complex

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