source: roaraudio/roard/roard.c @ 2549:b127e2e3506c

Last change on this file since 2549:b127e2e3506c was 2549:b127e2e3506c, checked in by phi, 15 years ago

use lib functions

File size: 40.4 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
[1919]503   } else {
[2494]504    ROAR_ERR("add_output(*): unknown/unsupported subsystem '%s'", k);
[1991]505    error++;
506   }
507  // DMX:
508  } else if ( strcmp(k, "channel") == 0 ) {
509   channel  = atoi(v);
510   if ( channel < 0 || channel > 65535 ) {
511    ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel);
512    channel = -1;
513    error++;
[1919]514   }
[1991]515  } else if ( strcmp(k, "universe") == 0 ) {
516   universe = atoi(v);
517   if ( universe < 0 || universe > 255 ) {
518    ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe);
519    universe = -1;
520    error++;
521   }
522
[2115]523  } else if ( strcmp(k, "name") == 0 ) {
524   if ( streams_set_name(stream, v) == -1 ) {
525    ROAR_ERR("add_output(*): Can not set Stream name");
526    error++;
527   }
[1991]528
[1032]529  } else if ( strcmp(k, "meta") == 0 ) {
530   streams_set_flag(stream, ROAR_FLAG_META);
[1117]531  } else if ( strcmp(k, "sync") == 0 ) {
532   sync = 1;
[1221]533  } else if ( strcmp(k, "primary") == 0 ) {
534   prim = 1;
535
536  } else if ( strcmp(k, "cleanmeta") == 0 ) {
537   streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
538  } else if ( strcmp(k, "autoconf") == 0 ) {
[1531]539   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
[2413]540  } else if ( strcmp(k, "recsource") == 0 ) {
541   streams_set_flag(stream, ROAR_FLAG_RECSOURCE);
542  } else if ( strcmp(k, "passmixer") == 0 ) {
543   streams_set_flag(stream, ROAR_FLAG_PASSMIXER);
[938]544  } else {
545   ROAR_ERR("add_output(*): unknown option '%s'", k);
[1991]546   error++;
547  }
548
549  if ( error ) {
[938]550   streams_delete(stream);
[982]551   if ( prim ) alive = 0;
[1208]552#ifdef ROAR_DRIVER_CODEC
553   if ( to_free != NULL )
554    free(to_free);
555#endif
[938]556   return -1;
557  }
558
559  k = strtok(NULL, ",");
560 }
561
[2511]562 // set audio info...
563 switch (dir) {
564  case ROAR_DIR_LIGHT_OUT:
565    switch (s->info.codec) {
566     case ROAR_CODEC_DMX512:
567     case -1:
568       if ( s->info.rate == -1 )
569        s->info.rate = ROAR_OUTPUT_CFREQ;
570
571       s->info.channels =   0;
572       s->info.bits     =   8;
573       s->info.codec    = ROAR_CODEC_DMX512; // in case codec == -1
574      break;
575    }
576   break;
577  case ROAR_DIR_MIDI_OUT:
578    switch (s->info.codec) {
579     case ROAR_CODEC_MIDI:
580     case -1:
581       if ( s->info.rate == -1 )
582        s->info.rate    = ROAR_MIDI_TICKS_PER_BEAT;
583
584       s->info.channels = ROAR_MIDI_CHANNELS_DEFAULT;
585       s->info.bits     = ROAR_MIDI_BITS;
586       s->info.codec    = ROAR_CODEC_MIDI; // in case codec == -1
587      break;
588    }
589   break;
590  case ROAR_DIR_RAW_OUT:
591    if ( s->info.rate == -1 )
592     s->info.rate = 0;
593    if ( s->info.bits == -1 )
594     s->info.bits = 0;
595    if ( s->info.channels == -1 )
596     s->info.channels = 0;
597    if ( s->info.codec == -1 )
598     s->info.codec = 0;
599   break;
600 }
601
602 if ( s->info.rate == -1 )
603  s->info.rate = g_sa->rate;
604 if ( s->info.bits == -1 )
605  s->info.bits = g_sa->bits;
606 if ( s->info.channels == -1 )
607  s->info.channels = g_sa->channels;
608 if ( s->info.codec == -1 )
609  s->info.codec = g_sa->codec;
610
611 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);
612
[1919]613 if ( streams_set_dir(stream, dir, 1) == -1 ) {
614  streams_delete(stream);
615  return -1;
616 }
617
[1208]618#ifdef ROAR_DRIVER_CODEC
619 if ( to_free != NULL )
620  free(to_free);
621#endif
622
[2511]623 if ( s->info.codec == ROAR_CODEC_ALAW || s->info.codec == ROAR_CODEC_MULAW )
[941]624  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
625
[2511]626 ROAR_STREAM_SERVER(s)->codec_orgi = s->info.codec;
[933]627
[2364]628 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) {
[1581]629  ss->driver_id = -1; // don't close a driver not opened...
630  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
[933]631  streams_delete(stream);
[938]632  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]633  if ( prim ) alive = 0;
[933]634  return -1;
635 }
636
[1528]637 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here
638 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM,   s); // ignore errors here
639
[1221]640 if ( blocks != -1 )
641  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
642
[1522]643 if ( blocksize != -1 )
644  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
645
[1991]646 // TODO: we shoudld *really* check for errors here...
647 if ( channel != -1 ) {
648  tu16 = channel;
649  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16);
650 }
651 if ( universe != -1 ) {
652  tu16 = universe;
653  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16);
654 }
655
[1156]656 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
657
[938]658 streams_set_fh(stream, -1); // update some internal structures
659
[2087]660 if ( q > -1e6 ) {
661  ROAR_DBG("add_output(*): setting q=%f", q);
662  streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q);
663 }
664
[2490]665 client_stream_add(g_self_client, stream);
[933]666
[1200]667 if ( prim ) {
[961]668  streams_mark_primary(stream);
[1200]669  s->pos_rel_id = stream;
670 }
[961]671
[1120]672 if ( sync ) {
[1117]673  streams_set_flag(stream, ROAR_FLAG_SYNC);
[1120]674 } else {
675  streams_reset_flag(stream, ROAR_FLAG_SYNC);
676 }
[1117]677
[1926]678 if ( f_mmap )
679  streams_set_flag(stream, ROAR_FLAG_MMAP);
680
[2511]681 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]682 return 0;
[932]683}
684
[1993]685
686// SLP:
687void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
688 /* return the error code in the cookie */
689 *(SLPError*)cookie = errcode;
690}
691
692int register_slp (int unreg, char * sockname) {
693#ifdef ROAR_HAVE_LIBSLP
694 static int regged = 0;
695 static char * sn = NULL;
696 SLPError err;
697 SLPError callbackerr;
698 SLPHandle hslp;
699 char addr[1024];
700 char attr[1024] = "";
[2017]701 char * location;
[1993]702
703 if ( sockname != NULL )
704  sn = sockname;
705
[2016]706 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE "://%s", sn);
[1993]707
708 err = SLPOpen("en", SLP_FALSE, &hslp);
709
710 if (err != SLP_OK) {
711  ROAR_ERR("Error opening slp handle: Error #%i", err);
712  return -1;
713 }
714
715 if (!unreg) {
[2017]716
717  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
718   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
719   SLPClose(hslp);
720   return -1;
721  }
722
[1995]723  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
[2494]724#ifndef ROAR_WITHOUT_DCOMP_LIGHT
725                               "(light-channels=%i),"
726#endif
727                               "(location=%s)",
[1995]728           g_sa->rate, g_sa->channels, g_sa->bits,
[2494]729#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[2017]730           g_light_state.channels,
[2494]731#endif
[2017]732           location
[1995]733          );
734
[1993]735  /* Register a service with SLP */
736  err = SLPReg(hslp,
737               addr,
738               SLP_LIFETIME_MAXIMUM,
739               0,
740               attr,
741               SLP_TRUE,
742               register_slp_callback,
743               &callbackerr);
[1994]744  regged = 1;
745 } else if ( unreg && regged ) {
746  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
747  regged = 0;
748 } else {
749  SLPClose(hslp);
750  return -1;
751 }
[1993]752
[1994]753 /* err may contain an error code that occurred as the slp library    */
754 /* _prepared_ to make the call.                                     */
755 if ( err != SLP_OK ) {
756  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
757  return -1;
758 }
759
760 /* callbackerr may contain an error code (that was assigned through */
761 /* the callback cookie) that occurred as slp packets were sent on    */
762 /* the wire */
763 if (callbackerr != SLP_OK) {
764  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
765  return -1;
[1993]766 }
767
768 SLPClose(hslp);
769 return 0;
770#else
771 return -1;
772#endif
773}
774
775
776// MAIN:
777
[1503]778#ifdef ROAR_HAVE_MAIN_ARGS
[0]779int main (int argc, char * argv[]) {
[1503]780#else
781int main (void) {
782#endif
783#ifdef ROAR_HAVE_MAIN_ARGS
[0]784 int i;
785 char * k;
[1503]786#endif
[1494]787#ifdef ROAR_SUPPORT_LISTEN
[905]788 char user_sock[80]  = {0};
[1494]789#endif
[1609]790 struct roar_audio_info sa, max_sa;
791 struct roard_config config;
[1486]792#ifdef ROAR_HAVE_FORK
[905]793 int    daemon       = 0;
[1486]794#endif
[905]795 int    realtime     = 0;
796 int    sysclocksync = 0;
[1207]797 char * driver    = NULL;
798 char * device    = NULL;
[1503]799#ifdef ROAR_HAVE_MAIN_ARGS
[1207]800 char * opts      = NULL;
[1503]801#endif
[60]802// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
[1494]803#ifdef ROAR_SUPPORT_LISTEN
[2530]804 int    port       = ROAR_DEFAULT_PORT;
805 char * sock_addr  = NULL;
806 int    sock_proto = ROAR_PROTO_ROARAUDIO;
[1494]807#endif
[0]808 int               drvid;
[2486]809#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]810 char * s_drv     = "cf";
[1110]811 char * s_dev     = NULL;
[444]812 char * s_con     = NULL;
813 char * s_opt     = NULL;
814 int    s_prim    = 0;
[2486]815#endif
[1207]816 char * o_drv     = getenv("ROAR_DRIVER");
817 char * o_dev     = getenv("ROAR_DEVICE");
[932]818 char * o_opts    = NULL;
[961]819 int    o_prim    = 0;
[1145]820 int    o_count   = 0;
[2494]821#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]822 int    light_channels = LIGHT_CHANNELS_DEFAULT;
[2494]823#endif
[450]824 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
[444]825 char * sock_user = NULL;
[1494]826#ifdef ROAR_SUPPORT_LISTEN
[517]827 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
[1494]828#endif
[1993]829#ifdef ROAR_HAVE_LIBSLP
830 int    reg_slp   = 0;
831#endif
[1486]832#ifdef ROAR_HAVE_CHROOT
[444]833 char * chrootdir = NULL;
[1486]834#endif
835#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[446]836 struct group   * grp  = NULL;
[1486]837#endif
838#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[446]839 struct passwd  * pwd  = NULL;
[1486]840#endif
841#ifdef ROAR_HAVE_GETSERVBYNAME
[446]842 struct servent * serv = NULL;
[1486]843#endif
[0]844 DRIVER_USERDATA_T drvinst;
[39]845 struct roar_client * self = NULL;
[508]846#ifdef ROAR_HAVE_LIBDNET
847 char decnethost[80];
848#endif
[2104]849#ifdef SUPPORT_PIDFILE
850 struct roar_vio_calls pidfile_vio;
851#endif
[0]852
853 g_standby       =  0;
[920]854 g_autostandby   =  0;
[982]855 alive           =  1;
[1494]856#ifdef ROAR_SUPPORT_LISTEN
[1155]857 g_no_listen     =  0;
[1494]858#else
859 g_terminate     =  1;
860#endif
[0]861
862 sa.bits     = ROAR_BITS_DEFAULT;
863 sa.channels = ROAR_CHANNELS_DEFAULT;
864 sa.rate     = ROAR_RATE_DEFAULT;
865 sa.codec    = ROAR_CODEC_DEFAULT;
866
[1609]867 g_sa        = &sa;
868 g_max_sa    = &max_sa;
869
870 memcpy(g_max_sa, g_sa, sizeof(max_sa));
871
872 g_config = &config;
873
874 if ( init_config() == -1 ) {
875  ROAR_ERR("Can not init default config!");
876  return 1;
877 }
[0]878
[2530]879#ifdef ROAR_SUPPORT_LISTEN
880 if ( init_listening() == -1 ) {
881  ROAR_ERR("Can not init listening sockets!");
882  return 1;
883 }
884#endif
885
[2500]886#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1924]887 if ( midi_init_config() == -1 ) {
888  ROAR_ERR("Can not init MIDI config!");
889  return 1;
890 }
[2500]891#endif
[60]892
[2489]893#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]894 if ( ssynth_init_config() == -1 ) {
895  ROAR_ERR("Can not init ssynth config!");
896  return 1;
897 }
[2489]898#endif
[2451]899
[1494]900#ifdef ROAR_SUPPORT_LISTEN
[2530]901 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
[1753]902#ifdef ROAR_HAVE_GETUID
[1486]903 if ( getuid() != 0 && getenv("HOME") != NULL ) {
904  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
[2530]905  sock_addr = user_sock;
[60]906 }
[1753]907#endif
[60]908
[279]909 if ( getenv("ROAR_SERVER") != NULL )
[2530]910  sock_addr = getenv("ROAR_SERVER");
[1494]911#endif
[279]912
[63]913 if ( clients_init() == -1 ) {
914  ROAR_ERR("Can not init clients!");
915  return 1;
916 }
917
918 if ( streams_init() == -1 ) {
919  ROAR_ERR("Can not init streams!");
920  return 1;
921 }
922
[64]923 if ( (g_self_client = clients_new()) == -1 ) {
924  ROAR_ERR("Can not create self client!");
925  return 1;
926 }
927
[2485]928#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]929 if ( sources_init() == -1 ) {
930  ROAR_ERR("Can not init sources!");
931  return 1;
932 }
933
[64]934 if ( (sources_set_client(g_self_client)) == -1 ) {
935  ROAR_ERR("Can not init set source client!");
936  return 1;
937 }
[2485]938#endif
[64]939
[1503]940#ifdef ROAR_HAVE_MAIN_ARGS
[0]941 for (i = 1; i < argc; i++) {
942  k = argv[i];
943
944  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
945   usage();
946   return 0;
947
[2109]948  } else if ( strcmp(k, "--start") == 0 ) {
949   // this is a no op
[579]950  } else if ( strcmp(k, "--restart") == 0 ) {
[1494]951#ifdef ROAR_SUPPORT_LISTEN
[2530]952   if ( restart_server(sock_addr, 1) == -1 ) {
953    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", sock_addr);
[579]954   }
[1494]955#else
956   ROAR_ERR("--restart not supported");
957#endif
[2109]958  } else if ( strcmp(k, "--shutdown") == 0 ) {
959#ifdef ROAR_SUPPORT_LISTEN
[2530]960   if ( restart_server(sock_addr, 1) == -1 ) {
961    ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
[2109]962    return 1;
963   }
964   return 0;
965#else
966   ROAR_ERR("--shutdown not supported");
967   return 1;
968#endif
969  } else if ( strcmp(k, "--stop") == 0 ) {
970#ifdef ROAR_SUPPORT_LISTEN
[2530]971   if ( restart_server(sock_addr, 0) == -1 ) {
972    ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
[2109]973    return 1;
974   }
975   return 0;
976#else
977   ROAR_ERR("--stop not supported");
978   return 1;
979#endif
980
[579]981
[775]982  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
[1486]983#ifdef ROAR_HAVE_FORK
[775]984   daemon = 1;
[1486]985#else
986   ROAR_ERR("--daemon not supported");
987#endif
[71]988  } else if ( strcmp(k, "--terminate") == 0 ) {
989   g_terminate = 1;
[905]990  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
991   sysclocksync = 1000;
[275]992  } else if ( strcmp(k, "--realtime") == 0 ) {
[276]993   realtime++;
[444]994  } else if ( strcmp(k, "--chroot") == 0 ) {
[1486]995#ifdef ROAR_HAVE_CHROOT
[444]996   chrootdir = argv[++i];
[1486]997#else
998   ROAR_ERR("--chroot not supported");
999   i++;
1000#endif
[444]1001  } else if ( strcmp(k, "--setgid") == 0 ) {
[1486]1002#ifdef ROAR_HAVE_SETGID
[444]1003   setids |= R_SETGID;
[1486]1004#else
1005   ROAR_ERR("--setgid not supported");
1006#endif
[444]1007  } else if ( strcmp(k, "--setuid") == 0 ) {
[1486]1008#ifdef ROAR_HAVE_SETUID
[444]1009   setids |= R_SETUID;
[1486]1010#else
1011   ROAR_ERR("--setuid not supported");
1012#endif
[2017]1013  } else if ( strcmp(k, "--location") == 0 ) {
1014   g_config->location = argv[++i];
[2104]1015  } else if ( strcmp(k, "--pidfile") == 0 ) {
1016#ifdef SUPPORT_PIDFILE
1017   pidfile = argv[++i];
1018#else
1019   ROAR_ERR("--pidfile not supported");
1020   i++;
1021#endif
[68]1022
[280]1023  } else if ( strcmp(k, "--list-cf") == 0 ) {
1024   print_codecfilterlist();
1025   return 0;
1026
[0]1027  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
1028   sa.rate = atoi(argv[++i]);
1029  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
1030   sa.bits = atoi(argv[++i]);
1031  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
1032   sa.channels = atoi(argv[++i]);
1033
[2339]1034  } else if ( strcmp(k, "--stream-flags") == 0 ) {
1035   if ( update_stream_flags(argv[++i]) == -1 ) {
1036    ROAR_ERR("Can not set stream flags");
1037    return 1;
1038   }
1039
[0]1040  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
1041   driver = argv[++i];
1042   if ( strcmp(driver, "list") == 0 ) {
[974]1043    ROAR_WARN("The option is obsolete, use --list-driver!");
[0]1044    print_driverlist();
[973]1045    return 0;
[0]1046   }
1047  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
1048   device = argv[++i];
1049  } else if ( strcmp(k, "-dO") == 0 ) {
1050   opts = argv[++i];
[973]1051  } else if ( strcmp(k, "--list-driver") == 0 ) {
1052   print_driverlist();
1053   return 0;
[0]1054
[932]1055  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
1056   o_drv  = argv[++i];
1057  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
1058   o_dev  = argv[++i];
1059  } else if ( strcmp(k, "-oO") == 0 ) {
1060   o_opts = argv[++i];
[961]1061  } else if ( strcmp(k, "-oP") == 0 ) {
1062   o_prim = 1;
[932]1063  } else if ( strcmp(k, "-oN") == 0 ) {
[1145]1064   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1065    o_count++;
1066
[961]1067   o_drv  = o_dev = o_opts = NULL;
1068   o_prim = 0;
[932]1069
[0]1070  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
[2485]1071#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]1072   s_drv = argv[++i];
[2485]1073#else
1074   ROAR_ERR("main(*): No support for sources compiled in");
1075#endif
[550]1076  } else if ( strcmp(k, "-S") == 0 ) {
[2485]1077#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1078   s_dev = argv[++i];
[2485]1079#else
1080   ROAR_ERR("main(*): No support for sources compiled in");
1081#endif
[0]1082  } else if ( strcmp(k, "-sO") == 0 ) {
[2485]1083#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1084   s_opt = argv[++i];
[2485]1085#else
1086   ROAR_ERR("main(*): No support for sources compiled in");
1087#endif
[0]1088  } else if ( strcmp(k, "-sC") == 0 ) {
[2485]1089#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1090   s_con = argv[++i];
[2485]1091#else
1092   ROAR_ERR("main(*): No support for sources compiled in");
1093#endif
[0]1094  } else if ( strcmp(k, "-sP") == 0 ) {
[2485]1095#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1096   s_prim = 1;
[2485]1097#else
1098   ROAR_ERR("main(*): No support for sources compiled in");
1099#endif
[1110]1100  } else if ( strcmp(k, "-sN") == 0 ) {
[2485]1101#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1102   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1103    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1104   }
1105   s_opt = s_dev = s_con = NULL;
1106   s_drv = "cf";
1107   s_prim = 0;
[2485]1108#else
1109   ROAR_ERR("main(*): No support for sources compiled in");
1110#endif
[2270]1111  } else if ( strcmp(k, "--list-sources") == 0 ) {
[2485]1112#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[2270]1113   print_sourcelist();
1114   return 0;
[2485]1115#else
1116   ROAR_ERR("main(*): No support for sources compiled in");
[2504]1117   return 1;
[2485]1118#endif
[0]1119
[1923]1120  } else if ( strcmp(k, "--light-channels") == 0 ) {
[2494]1121#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]1122   light_channels = atoi(argv[++i]);
[2494]1123#else
1124   ROAR_WARN("main(*): no light subsystem compiled in");
1125#endif
[1923]1126
[1924]1127  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
[2487]1128#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]1129   midi_config.init_cb = 0;
[2487]1130#else
1131   // no warning here as this is the disable option
1132#endif
[2444]1133  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
[2487]1134#ifndef ROAR_WITHOUT_DCOMP_CB
[2444]1135   midi_config.init_cb = 1;
[2487]1136#else
1137   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1138#endif
[1924]1139  } else if ( strcmp(k, "--midi-console") == 0 ) {
[2487]1140#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]1141   midi_config.console_dev = argv[++i];
[2444]1142   midi_config.init_cb = 1;
[2487]1143#else
1144   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1145#endif
[1924]1146
[2451]1147  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
[2489]1148#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1149   ssynth_conf.enable = 1;
[2489]1150#else
1151   ROAR_ERR("main(*): No support for ssynth compiled in");
1152#endif
[2451]1153  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
[2489]1154#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1155   ssynth_conf.enable = 0;
[2489]1156#else
1157   // we can safely ignore the disable
1158#endif
[2451]1159
[0]1160  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
[447]1161   // This is only usefull in INET not UNIX mode.
[1494]1162#ifdef ROAR_SUPPORT_LISTEN
[2530]1163   if ( *sock_addr == '/' )
1164    sock_addr = ROAR_DEFAULT_HOST;
[447]1165
[446]1166   errno = 0;
1167   if ( (port = atoi(argv[++i])) < 1 ) {
[1486]1168#ifdef ROAR_HAVE_GETSERVBYNAME
[446]1169    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
1170     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
1171     return 1;
1172    }
1173    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
1174    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
1175            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
1176    port = ROAR_NET2HOST16(serv->s_port);
[1486]1177#else
1178    ROAR_ERR("invalite port number: %s", argv[i]);
1179    return 1;
1180#endif
[446]1181   }
[1494]1182#endif
[1115]1183  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
[1494]1184#ifdef ROAR_SUPPORT_LISTEN
[2530]1185   sock_addr = argv[++i];
1186#endif
1187
1188  } else if ( strcmp(k, "--proto") == 0 ) {
1189#ifdef ROAR_SUPPORT_LISTEN
[2549]1190   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
[2530]1191    ROAR_ERR("Unknown protocol: %s", argv[i]);
1192    return 1;
1193   }
[1494]1194#endif
[518]1195
[573]1196  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
[1494]1197#ifdef ROAR_SUPPORT_LISTEN
[518]1198   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
1199    sock_type = ROAR_SOCKET_TYPE_TCP;
1200
[2530]1201   if ( *sock_addr == '/' )
1202    sock_addr = ROAR_DEFAULT_HOST;
[1494]1203#endif
[518]1204
1205  } else if ( strcmp(k, "-4") == 0 ) {
[1494]1206#ifdef ROAR_SUPPORT_LISTEN
[517]1207   sock_type = ROAR_SOCKET_TYPE_TCP;
[2530]1208   if ( *sock_addr == '/' )
1209    sock_addr = ROAR_DEFAULT_HOST;
[1494]1210#endif
[518]1211  } else if ( strcmp(k, "-6") == 0 ) {
[1494]1212#ifdef ROAR_SUPPORT_LISTEN
[519]1213#ifdef PF_INET6
[518]1214   sock_type = ROAR_SOCKET_TYPE_TCP6;
[2530]1215   if ( *sock_addr == '/' )
1216    sock_addr = ROAR_DEFAULT_HOST;
[519]1217#else
1218    ROAR_ERR("No IPv6 support compiled in!");
1219    return 1;
1220#endif
[1494]1221#endif
[518]1222
[573]1223  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
[1494]1224#ifdef ROAR_SUPPORT_LISTEN
[62]1225   // ignore this case as it is the default behavor.
[517]1226   sock_type = ROAR_SOCKET_TYPE_UNIX;
[1494]1227#endif
[518]1228
[573]1229  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
[1494]1230#ifdef ROAR_SUPPORT_LISTEN
[508]1231#ifdef ROAR_HAVE_LIBDNET
1232    port   = ROAR_DEFAULT_NUM;
1233    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
[2530]1234    sock_addr = decnethost;
[517]1235    sock_type = ROAR_SOCKET_TYPE_DECNET;
[508]1236#else
1237    ROAR_ERR("No DECnet support compiled in!");
1238    return 1;
1239#endif
[1494]1240#endif
[2530]1241  } else if ( strcmp(k, "--new-sock") == 0 ) {
1242#ifdef ROAR_SUPPORT_LISTEN
1243   if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto) != 0 ) {
1244    ROAR_ERR("Can not open listen socket!");
1245    return 1;
1246   }
1247#endif
[518]1248
[1993]1249  } else if ( strcmp(k, "--slp") == 0 ) {
1250#ifdef ROAR_HAVE_LIBSLP
1251   reg_slp = 1;
1252#else
1253    ROAR_ERR("No OpenSLP support compiled in!");
1254    return 1;
1255#endif
1256
[60]1257  } else if ( strcmp(k, "-G") == 0 ) {
[444]1258   sock_grp  = argv[++i];
1259  } else if ( strcmp(k, "-U") == 0 ) {
1260   sock_user = argv[++i];
[0]1261
[68]1262  } else if ( strcmp(k, "--no-listen") == 0 ) {
[1494]1263#ifdef ROAR_SUPPORT_LISTEN
[2530]1264   sock_addr   = "";
[548]1265   g_terminate = 1;
[1155]1266   g_no_listen = 1;
[1494]1267#endif
[68]1268  } else if ( strcmp(k, "--client-fh") == 0 ) {
1269   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
1270    ROAR_ERR("main(*): Can not set client's fh");
1271    return 1;
1272   }
[501]1273  } else if ( strcmp(k, "--close-fh") == 0 ) {
[1486]1274#ifdef ROAR_HAVE_IO_POSIX
[501]1275   close(atoi(argv[++i]));
[1486]1276#else
1277   i++;
1278   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
1279#endif
[68]1280
[920]1281  } else if ( strcmp(k, "--standby") == 0 ) {
1282   g_standby = 1;
1283  } else if ( strcmp(k, "--auto-standby") == 0 ) {
1284   g_autostandby = 1;
[0]1285  } else {
1286   usage();
1287   return 1;
1288  }
1289
1290 }
[1503]1291#endif
[0]1292
[2485]1293#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1294 if ( s_dev != NULL ) {
1295  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1296   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1297  }
1298 }
[2485]1299#endif
[1110]1300
[1145]1301 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
[932]1302
[0]1303 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
1304
[2500]1305#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1819]1306 if ( midi_init() == -1 ) {
[281]1307  ROAR_ERR("Can not initialize MIDI subsystem");
[1819]1308 }
[2500]1309#endif
[1819]1310
[2489]1311#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1312 if ( ssynth_init() == -1 ) {
1313  ROAR_ERR("Can not initialize ssynth subsystem");
1314 }
[2489]1315#endif
[2451]1316
[2494]1317#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]1318 if ( light_init(light_channels) == -1 ) {
1319  ROAR_ERR("Can not initialize light control subsystem");
1320 }
[2494]1321#endif
[281]1322
[1494]1323#ifdef ROAR_SUPPORT_LISTEN
[2530]1324 if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto) != 0 ) {
1325  ROAR_ERR("Can not open listen socket!");
1326  return 1;
[60]1327 }
[1494]1328#endif
[60]1329
[0]1330 if ( output_buffer_init(&sa) == -1 ) {
1331  ROAR_ERR("Can not init output buffer!");
1332  return 1;
1333 }
1334
[1145]1335 if ( driver == NULL ) {
1336  driver = "null";
1337 } else {
1338  ROAR_WARN("Usage of old driver interface. use -o not -d!");
1339 }
1340
[0]1341 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
1342  ROAR_ERR("Can not open output driver!");
1343  return 1;
1344 }
1345
[44]1346 if ( samples_init() == -1 ) {
1347  ROAR_ERR("Can not init samples!");
1348  return 1;
1349 }
1350
1351
[1486]1352 // we should handle this on microcontrollers, too.
[1753]1353#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
[0]1354 signal(SIGINT,  on_sig_int);
[285]1355 signal(SIGCHLD, on_sig_chld);
[2112]1356 signal(SIGUSR1, on_sig_usr1);
[0]1357 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
[1486]1358#endif
[0]1359
[275]1360 if ( realtime ) {
[278]1361#ifdef DEBUG
1362  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
1363#endif
1364
[1486]1365#ifdef ROAR_HAVE_NICE
[275]1366  errno = 0;
[276]1367  nice(-5*realtime); // -5 for each --realtime
[1486]1368  if ( errno ) {
1369   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1370  }
1371#else
1372  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1373#endif
[277]1374/*
[276]1375#ifdef __linux__
[277]1376  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
1377   ROAR_WARN("Can not set io priority: %s", strerror(errno));
[276]1378#endif
[277]1379*/
[275]1380 }
1381
[1486]1382#ifdef ROAR_HAVE_SETGID
[444]1383 if ( setids & R_SETGID ) {
1384  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
1385   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
1386  }
[523]1387  if ( !grp || setgid(grp->gr_gid) == -1 ) {
[444]1388   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
1389  }
1390 }
[1486]1391#endif
[444]1392
[0]1393
[39]1394 clients_set_pid(g_self_client, getpid());
[1753]1395#ifdef ROAR_HAVE_GETUID
[440]1396 clients_set_uid(g_self_client, getuid());
[1753]1397#endif
1398#ifdef ROAR_HAVE_GETGID
[440]1399 clients_set_gid(g_self_client, getgid());
[1753]1400#endif
[39]1401 clients_get(g_self_client, &self);
[37]1402
[39]1403 if ( self == NULL ) {
1404  ROAR_ERR("Can not get self client!");
1405  return 1;
1406 }
1407
[775]1408 strcpy(self->name, "RoarAudio daemon internal");
[68]1409
[1486]1410#ifdef ROAR_HAVE_FORK
[775]1411 if ( daemon ) {
[68]1412  close(ROAR_STDIN );
1413  close(ROAR_STDOUT);
1414  close(ROAR_STDERR);
[1753]1415
[68]1416  if ( fork() )
[1486]1417   ROAR_U_EXIT(0);
[1753]1418
1419#ifdef ROAR_HAVE_SETSID
1420  setsid();
1421#endif
[1046]1422  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
[68]1423 }
[1486]1424#endif
[68]1425
[2104]1426#ifdef SUPPORT_PIDFILE
[2106]1427 if ( pidfile != NULL ) {
1428  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
1429   ROAR_ERR("Can not write pidfile: %s", pidfile);
1430  } else {
1431   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
1432   roar_vio_close(&pidfile_vio);
1433  }
[2104]1434 }
1435#endif
1436
[1486]1437#ifdef ROAR_HAVE_CHROOT
[444]1438 if (chrootdir) {
1439  if ( chroot(chrootdir) == -1 ) {
1440   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
1441   return 2;
1442  }
1443  if ( chdir("/") == -1 ) {
1444   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
1445   return 2;
1446  }
1447 }
[1486]1448#endif
[444]1449
[1486]1450#ifdef ROAR_HAVE_SETUID
[444]1451 if ( setids & R_SETUID ) {
1452  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
1453   ROAR_ERR("Can not set UserID: %s", strerror(errno));
1454   return 3;
1455  }
[1753]1456#ifdef ROAR_HAVE_GETUID
[444]1457  clients_set_uid(g_self_client, getuid());
[1753]1458#endif
[444]1459 }
[1486]1460#endif
[444]1461
[1993]1462 // Register with OpenSLP:
[2028]1463#ifdef ROAR_HAVE_LIBSLP
[1993]1464 if ( reg_slp ) {
[2530]1465  register_slp(0, sock_addr);
[1993]1466 }
[2028]1467#endif
[1993]1468
[0]1469 // start main loop...
[905]1470 main_loop(drvid, drvinst, &sa, sysclocksync);
[0]1471
1472 // clean up.
1473 clean_quit_prep();
1474 driver_close(drvinst, drvid);
1475 output_buffer_free();
1476
1477 return 0;
1478}
1479
[574]1480void cleanup_listen_socket (int terminate) {
[2530]1481 int i;
1482
[1993]1483 // Deregister from SLP:
[2028]1484#ifdef ROAR_HAVE_LIBSLP
[1993]1485 register_slp(1, NULL);
[2028]1486#endif
[580]1487
[1494]1488#ifdef ROAR_SUPPORT_LISTEN
[2530]1489 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
1490  if ( g_listen_socket[i] != -1 ) {
[1486]1491#ifdef ROAR_HAVE_IO_POSIX
[2530]1492   close(g_listen_socket[i]);
[1486]1493#endif // #else is useless because we are in void context.
[60]1494
[2530]1495   g_listen_socket[i] = -1;
[576]1496
[1486]1497#ifdef ROAR_HAVE_UNIX
[2530]1498   if ( server[i] != NULL )
1499    if ( *(server[i]) == '/' )
1500     unlink(server[i]);
[1486]1501#endif
[2530]1502  }
[580]1503 }
[60]1504
[1494]1505#endif
1506
[574]1507 if ( terminate )
1508  g_terminate = 1;
1509}
1510
1511void clean_quit_prep (void) {
1512 cleanup_listen_socket(0);
[60]1513
[2485]1514#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1515 sources_free();
[2485]1516#endif
[0]1517 streams_free();
1518 clients_free();
[2489]1519#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1520 ssynth_free();
[2489]1521#endif
[2487]1522#ifndef ROAR_WITHOUT_DCOMP_CB
[282]1523 midi_cb_stop(); // stop console beep
[2487]1524#endif
[2500]1525#ifndef ROAR_WITHOUT_DCOMP_MIDI
[281]1526 midi_free();
[2500]1527#endif
[2494]1528#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]1529 light_free();
[2494]1530#endif
[2104]1531
1532#ifdef SUPPORT_PIDFILE
1533 if ( pidfile != NULL )
1534  unlink(pidfile);
1535#endif
[0]1536}
1537
1538void clean_quit (void) {
1539 clean_quit_prep();
1540// driver_close(drvinst, drvid);
1541// output_buffer_free();
1542 exit(0);
1543}
1544
1545//ll
Note: See TracBrowser for help on using the repository browser.