source: roaraudio/roard/roard.c @ 2718:bba8be1f4d19

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

added stubs for init and uninit of RDTCS

File size: 41.0 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
[2718]902#ifndef ROAR_WITHOUT_DCOMP_RDTCS
903 if ( rdtcs_init_config() == -1 ) {
904  ROAR_ERR("Can not init RDTCS config!");
905  return 1;
906 }
907#endif
908
[1494]909#ifdef ROAR_SUPPORT_LISTEN
[2530]910 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
[1753]911#ifdef ROAR_HAVE_GETUID
[1486]912 if ( getuid() != 0 && getenv("HOME") != NULL ) {
913  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
[2530]914  sock_addr = user_sock;
[60]915 }
[1753]916#endif
[60]917
[279]918 if ( getenv("ROAR_SERVER") != NULL )
[2530]919  sock_addr = getenv("ROAR_SERVER");
[1494]920#endif
[279]921
[63]922 if ( clients_init() == -1 ) {
923  ROAR_ERR("Can not init clients!");
924  return 1;
925 }
926
927 if ( streams_init() == -1 ) {
928  ROAR_ERR("Can not init streams!");
929  return 1;
930 }
931
[64]932 if ( (g_self_client = clients_new()) == -1 ) {
933  ROAR_ERR("Can not create self client!");
934  return 1;
935 }
936
[2485]937#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]938 if ( sources_init() == -1 ) {
939  ROAR_ERR("Can not init sources!");
940  return 1;
941 }
942
[64]943 if ( (sources_set_client(g_self_client)) == -1 ) {
944  ROAR_ERR("Can not init set source client!");
945  return 1;
946 }
[2485]947#endif
[64]948
[1503]949#ifdef ROAR_HAVE_MAIN_ARGS
[0]950 for (i = 1; i < argc; i++) {
951  k = argv[i];
952
953  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
954   usage();
955   return 0;
956
[2109]957  } else if ( strcmp(k, "--start") == 0 ) {
958   // this is a no op
[579]959  } else if ( strcmp(k, "--restart") == 0 ) {
[1494]960#ifdef ROAR_SUPPORT_LISTEN
[2530]961   if ( restart_server(sock_addr, 1) == -1 ) {
962    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", sock_addr);
[579]963   }
[1494]964#else
965   ROAR_ERR("--restart not supported");
966#endif
[2109]967  } else if ( strcmp(k, "--shutdown") == 0 ) {
968#ifdef ROAR_SUPPORT_LISTEN
[2530]969   if ( restart_server(sock_addr, 1) == -1 ) {
970    ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
[2109]971    return 1;
972   }
973   return 0;
974#else
975   ROAR_ERR("--shutdown not supported");
976   return 1;
977#endif
978  } else if ( strcmp(k, "--stop") == 0 ) {
979#ifdef ROAR_SUPPORT_LISTEN
[2530]980   if ( restart_server(sock_addr, 0) == -1 ) {
981    ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
[2109]982    return 1;
983   }
984   return 0;
985#else
986   ROAR_ERR("--stop not supported");
987   return 1;
988#endif
989
[579]990
[775]991  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
[1486]992#ifdef ROAR_HAVE_FORK
[775]993   daemon = 1;
[1486]994#else
995   ROAR_ERR("--daemon not supported");
996#endif
[71]997  } else if ( strcmp(k, "--terminate") == 0 ) {
998   g_terminate = 1;
[905]999  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1000   sysclocksync = 1000;
[275]1001  } else if ( strcmp(k, "--realtime") == 0 ) {
[276]1002   realtime++;
[444]1003  } else if ( strcmp(k, "--chroot") == 0 ) {
[1486]1004#ifdef ROAR_HAVE_CHROOT
[444]1005   chrootdir = argv[++i];
[1486]1006#else
1007   ROAR_ERR("--chroot not supported");
1008   i++;
1009#endif
[444]1010  } else if ( strcmp(k, "--setgid") == 0 ) {
[1486]1011#ifdef ROAR_HAVE_SETGID
[444]1012   setids |= R_SETGID;
[1486]1013#else
1014   ROAR_ERR("--setgid not supported");
1015#endif
[444]1016  } else if ( strcmp(k, "--setuid") == 0 ) {
[1486]1017#ifdef ROAR_HAVE_SETUID
[444]1018   setids |= R_SETUID;
[1486]1019#else
1020   ROAR_ERR("--setuid not supported");
1021#endif
[2017]1022  } else if ( strcmp(k, "--location") == 0 ) {
1023   g_config->location = argv[++i];
[2104]1024  } else if ( strcmp(k, "--pidfile") == 0 ) {
1025#ifdef SUPPORT_PIDFILE
1026   pidfile = argv[++i];
1027#else
1028   ROAR_ERR("--pidfile not supported");
1029   i++;
1030#endif
[68]1031
[280]1032  } else if ( strcmp(k, "--list-cf") == 0 ) {
1033   print_codecfilterlist();
1034   return 0;
1035
[0]1036  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
1037   sa.rate = atoi(argv[++i]);
1038  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
1039   sa.bits = atoi(argv[++i]);
1040  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
1041   sa.channels = atoi(argv[++i]);
1042
[2339]1043  } else if ( strcmp(k, "--stream-flags") == 0 ) {
1044   if ( update_stream_flags(argv[++i]) == -1 ) {
1045    ROAR_ERR("Can not set stream flags");
1046    return 1;
1047   }
1048
[0]1049  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
1050   driver = argv[++i];
1051   if ( strcmp(driver, "list") == 0 ) {
[974]1052    ROAR_WARN("The option is obsolete, use --list-driver!");
[0]1053    print_driverlist();
[973]1054    return 0;
[0]1055   }
1056  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
1057   device = argv[++i];
1058  } else if ( strcmp(k, "-dO") == 0 ) {
1059   opts = argv[++i];
[973]1060  } else if ( strcmp(k, "--list-driver") == 0 ) {
1061   print_driverlist();
1062   return 0;
[0]1063
[932]1064  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
1065   o_drv  = argv[++i];
1066  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
1067   o_dev  = argv[++i];
1068  } else if ( strcmp(k, "-oO") == 0 ) {
1069   o_opts = argv[++i];
[961]1070  } else if ( strcmp(k, "-oP") == 0 ) {
1071   o_prim = 1;
[932]1072  } else if ( strcmp(k, "-oN") == 0 ) {
[1145]1073   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1074    o_count++;
1075
[961]1076   o_drv  = o_dev = o_opts = NULL;
1077   o_prim = 0;
[932]1078
[0]1079  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
[2485]1080#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]1081   s_drv = argv[++i];
[2485]1082#else
1083   ROAR_ERR("main(*): No support for sources compiled in");
1084#endif
[550]1085  } else if ( strcmp(k, "-S") == 0 ) {
[2485]1086#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1087   s_dev = argv[++i];
[2485]1088#else
1089   ROAR_ERR("main(*): No support for sources compiled in");
1090#endif
[0]1091  } else if ( strcmp(k, "-sO") == 0 ) {
[2485]1092#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1093   s_opt = argv[++i];
[2485]1094#else
1095   ROAR_ERR("main(*): No support for sources compiled in");
1096#endif
[0]1097  } else if ( strcmp(k, "-sC") == 0 ) {
[2485]1098#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1099   s_con = argv[++i];
[2485]1100#else
1101   ROAR_ERR("main(*): No support for sources compiled in");
1102#endif
[0]1103  } else if ( strcmp(k, "-sP") == 0 ) {
[2485]1104#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1105   s_prim = 1;
[2485]1106#else
1107   ROAR_ERR("main(*): No support for sources compiled in");
1108#endif
[1110]1109  } else if ( strcmp(k, "-sN") == 0 ) {
[2485]1110#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1111   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1112    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1113   }
1114   s_opt = s_dev = s_con = NULL;
1115   s_drv = "cf";
1116   s_prim = 0;
[2485]1117#else
1118   ROAR_ERR("main(*): No support for sources compiled in");
1119#endif
[2270]1120  } else if ( strcmp(k, "--list-sources") == 0 ) {
[2485]1121#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[2270]1122   print_sourcelist();
1123   return 0;
[2485]1124#else
1125   ROAR_ERR("main(*): No support for sources compiled in");
[2504]1126   return 1;
[2485]1127#endif
[0]1128
[1923]1129  } else if ( strcmp(k, "--light-channels") == 0 ) {
[2494]1130#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]1131   light_channels = atoi(argv[++i]);
[2494]1132#else
1133   ROAR_WARN("main(*): no light subsystem compiled in");
1134#endif
[1923]1135
[1924]1136  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
[2487]1137#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]1138   midi_config.init_cb = 0;
[2487]1139#else
1140   // no warning here as this is the disable option
1141#endif
[2444]1142  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
[2487]1143#ifndef ROAR_WITHOUT_DCOMP_CB
[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  } else if ( strcmp(k, "--midi-console") == 0 ) {
[2487]1149#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]1150   midi_config.console_dev = argv[++i];
[2444]1151   midi_config.init_cb = 1;
[2487]1152#else
1153   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1154#endif
[1924]1155
[2451]1156  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
[2489]1157#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1158   ssynth_conf.enable = 1;
[2489]1159#else
1160   ROAR_ERR("main(*): No support for ssynth compiled in");
1161#endif
[2451]1162  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
[2489]1163#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1164   ssynth_conf.enable = 0;
[2489]1165#else
1166   // we can safely ignore the disable
1167#endif
[2451]1168
[0]1169  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
[447]1170   // This is only usefull in INET not UNIX mode.
[1494]1171#ifdef ROAR_SUPPORT_LISTEN
[2530]1172   if ( *sock_addr == '/' )
1173    sock_addr = ROAR_DEFAULT_HOST;
[447]1174
[446]1175   errno = 0;
1176   if ( (port = atoi(argv[++i])) < 1 ) {
[1486]1177#ifdef ROAR_HAVE_GETSERVBYNAME
[446]1178    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
1179     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
1180     return 1;
1181    }
1182    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
1183    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
1184            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
1185    port = ROAR_NET2HOST16(serv->s_port);
[1486]1186#else
1187    ROAR_ERR("invalite port number: %s", argv[i]);
1188    return 1;
1189#endif
[446]1190   }
[1494]1191#endif
[1115]1192  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
[1494]1193#ifdef ROAR_SUPPORT_LISTEN
[2530]1194   sock_addr = argv[++i];
1195#endif
1196
1197  } else if ( strcmp(k, "--proto") == 0 ) {
1198#ifdef ROAR_SUPPORT_LISTEN
[2549]1199   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
[2530]1200    ROAR_ERR("Unknown protocol: %s", argv[i]);
1201    return 1;
1202   }
[1494]1203#endif
[518]1204
[573]1205  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
[1494]1206#ifdef ROAR_SUPPORT_LISTEN
[518]1207   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
1208    sock_type = ROAR_SOCKET_TYPE_TCP;
1209
[2530]1210   if ( *sock_addr == '/' )
1211    sock_addr = ROAR_DEFAULT_HOST;
[1494]1212#endif
[518]1213
1214  } else if ( strcmp(k, "-4") == 0 ) {
[1494]1215#ifdef ROAR_SUPPORT_LISTEN
[517]1216   sock_type = ROAR_SOCKET_TYPE_TCP;
[2530]1217   if ( *sock_addr == '/' )
1218    sock_addr = ROAR_DEFAULT_HOST;
[1494]1219#endif
[518]1220  } else if ( strcmp(k, "-6") == 0 ) {
[1494]1221#ifdef ROAR_SUPPORT_LISTEN
[519]1222#ifdef PF_INET6
[518]1223   sock_type = ROAR_SOCKET_TYPE_TCP6;
[2530]1224   if ( *sock_addr == '/' )
1225    sock_addr = ROAR_DEFAULT_HOST;
[519]1226#else
1227    ROAR_ERR("No IPv6 support compiled in!");
1228    return 1;
1229#endif
[1494]1230#endif
[518]1231
[573]1232  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
[1494]1233#ifdef ROAR_SUPPORT_LISTEN
[62]1234   // ignore this case as it is the default behavor.
[517]1235   sock_type = ROAR_SOCKET_TYPE_UNIX;
[1494]1236#endif
[518]1237
[573]1238  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
[1494]1239#ifdef ROAR_SUPPORT_LISTEN
[508]1240#ifdef ROAR_HAVE_LIBDNET
1241    port   = ROAR_DEFAULT_NUM;
1242    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
[2530]1243    sock_addr = decnethost;
[517]1244    sock_type = ROAR_SOCKET_TYPE_DECNET;
[508]1245#else
1246    ROAR_ERR("No DECnet support compiled in!");
1247    return 1;
1248#endif
[1494]1249#endif
[2530]1250  } else if ( strcmp(k, "--new-sock") == 0 ) {
1251#ifdef ROAR_SUPPORT_LISTEN
1252   if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto) != 0 ) {
1253    ROAR_ERR("Can not open listen socket!");
1254    return 1;
1255   }
1256#endif
[518]1257
[1993]1258  } else if ( strcmp(k, "--slp") == 0 ) {
1259#ifdef ROAR_HAVE_LIBSLP
1260   reg_slp = 1;
1261#else
1262    ROAR_ERR("No OpenSLP support compiled in!");
1263    return 1;
1264#endif
1265
[60]1266  } else if ( strcmp(k, "-G") == 0 ) {
[444]1267   sock_grp  = argv[++i];
1268  } else if ( strcmp(k, "-U") == 0 ) {
1269   sock_user = argv[++i];
[0]1270
[68]1271  } else if ( strcmp(k, "--no-listen") == 0 ) {
[1494]1272#ifdef ROAR_SUPPORT_LISTEN
[2530]1273   sock_addr   = "";
[548]1274   g_terminate = 1;
[1155]1275   g_no_listen = 1;
[1494]1276#endif
[68]1277  } else if ( strcmp(k, "--client-fh") == 0 ) {
1278   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
1279    ROAR_ERR("main(*): Can not set client's fh");
1280    return 1;
1281   }
[501]1282  } else if ( strcmp(k, "--close-fh") == 0 ) {
[1486]1283#ifdef ROAR_HAVE_IO_POSIX
[501]1284   close(atoi(argv[++i]));
[1486]1285#else
1286   i++;
1287   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
1288#endif
[68]1289
[920]1290  } else if ( strcmp(k, "--standby") == 0 ) {
1291   g_standby = 1;
1292  } else if ( strcmp(k, "--auto-standby") == 0 ) {
1293   g_autostandby = 1;
[0]1294  } else {
1295   usage();
1296   return 1;
1297  }
1298
1299 }
[1503]1300#endif
[0]1301
[2485]1302#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1303 if ( s_dev != NULL ) {
1304  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1305   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1306  }
1307 }
[2485]1308#endif
[1110]1309
[1145]1310 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
[932]1311
[0]1312 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
1313
[2500]1314#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1819]1315 if ( midi_init() == -1 ) {
[281]1316  ROAR_ERR("Can not initialize MIDI subsystem");
[1819]1317 }
[2500]1318#endif
[1819]1319
[2489]1320#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1321 if ( ssynth_init() == -1 ) {
1322  ROAR_ERR("Can not initialize ssynth subsystem");
1323 }
[2489]1324#endif
[2451]1325
[2494]1326#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]1327 if ( light_init(light_channels) == -1 ) {
1328  ROAR_ERR("Can not initialize light control subsystem");
1329 }
[2494]1330#endif
[281]1331
[2718]1332#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1333 if ( rdtcs_init() == -1 ) {
1334  ROAR_ERR("Can not initialize RDTCS subsystem");
1335 }
1336#endif
1337
[1494]1338#ifdef ROAR_SUPPORT_LISTEN
[2530]1339 if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto) != 0 ) {
1340  ROAR_ERR("Can not open listen socket!");
1341  return 1;
[60]1342 }
[1494]1343#endif
[60]1344
[0]1345 if ( output_buffer_init(&sa) == -1 ) {
1346  ROAR_ERR("Can not init output buffer!");
1347  return 1;
1348 }
1349
[1145]1350 if ( driver == NULL ) {
1351  driver = "null";
1352 } else {
1353  ROAR_WARN("Usage of old driver interface. use -o not -d!");
1354 }
1355
[0]1356 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
1357  ROAR_ERR("Can not open output driver!");
1358  return 1;
1359 }
1360
[44]1361 if ( samples_init() == -1 ) {
1362  ROAR_ERR("Can not init samples!");
1363  return 1;
1364 }
1365
1366
[1486]1367 // we should handle this on microcontrollers, too.
[1753]1368#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
[0]1369 signal(SIGINT,  on_sig_int);
[285]1370 signal(SIGCHLD, on_sig_chld);
[2112]1371 signal(SIGUSR1, on_sig_usr1);
[0]1372 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
[1486]1373#endif
[0]1374
[275]1375 if ( realtime ) {
[278]1376#ifdef DEBUG
1377  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
1378#endif
1379
[1486]1380#ifdef ROAR_HAVE_NICE
[275]1381  errno = 0;
[276]1382  nice(-5*realtime); // -5 for each --realtime
[1486]1383  if ( errno ) {
1384   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1385  }
1386#else
1387  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1388#endif
[277]1389/*
[276]1390#ifdef __linux__
[277]1391  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
1392   ROAR_WARN("Can not set io priority: %s", strerror(errno));
[276]1393#endif
[277]1394*/
[275]1395 }
1396
[1486]1397#ifdef ROAR_HAVE_SETGID
[444]1398 if ( setids & R_SETGID ) {
[2584]1399  if ( (grp = getgrnam(sock_grp)) == NULL ) {
1400   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
1401   return 1;
1402  }
[444]1403  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
1404   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
1405  }
[523]1406  if ( !grp || setgid(grp->gr_gid) == -1 ) {
[444]1407   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
1408  }
1409 }
[1486]1410#endif
[444]1411
[0]1412
[39]1413 clients_set_pid(g_self_client, getpid());
[1753]1414#ifdef ROAR_HAVE_GETUID
[440]1415 clients_set_uid(g_self_client, getuid());
[1753]1416#endif
1417#ifdef ROAR_HAVE_GETGID
[440]1418 clients_set_gid(g_self_client, getgid());
[1753]1419#endif
[39]1420 clients_get(g_self_client, &self);
[37]1421
[39]1422 if ( self == NULL ) {
1423  ROAR_ERR("Can not get self client!");
1424  return 1;
1425 }
1426
[775]1427 strcpy(self->name, "RoarAudio daemon internal");
[68]1428
[1486]1429#ifdef ROAR_HAVE_FORK
[775]1430 if ( daemon ) {
[68]1431  close(ROAR_STDIN );
1432  close(ROAR_STDOUT);
1433  close(ROAR_STDERR);
[1753]1434
[68]1435  if ( fork() )
[1486]1436   ROAR_U_EXIT(0);
[1753]1437
1438#ifdef ROAR_HAVE_SETSID
1439  setsid();
1440#endif
[1046]1441  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
[68]1442 }
[1486]1443#endif
[68]1444
[2104]1445#ifdef SUPPORT_PIDFILE
[2106]1446 if ( pidfile != NULL ) {
1447  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
1448   ROAR_ERR("Can not write pidfile: %s", pidfile);
1449  } else {
1450   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
1451   roar_vio_close(&pidfile_vio);
1452  }
[2104]1453 }
1454#endif
1455
[1486]1456#ifdef ROAR_HAVE_CHROOT
[444]1457 if (chrootdir) {
1458  if ( chroot(chrootdir) == -1 ) {
1459   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
1460   return 2;
1461  }
1462  if ( chdir("/") == -1 ) {
1463   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
1464   return 2;
1465  }
1466 }
[1486]1467#endif
[444]1468
[1486]1469#ifdef ROAR_HAVE_SETUID
[444]1470 if ( setids & R_SETUID ) {
[2584]1471  if ( (pwd = getpwnam(sock_user)) == NULL ) {
1472   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
1473   return 1;
1474  }
[444]1475  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
1476   ROAR_ERR("Can not set UserID: %s", strerror(errno));
1477   return 3;
1478  }
[1753]1479#ifdef ROAR_HAVE_GETUID
[444]1480  clients_set_uid(g_self_client, getuid());
[1753]1481#endif
[444]1482 }
[1486]1483#endif
[444]1484
[1993]1485 // Register with OpenSLP:
[2028]1486#ifdef ROAR_HAVE_LIBSLP
[1993]1487 if ( reg_slp ) {
[2530]1488  register_slp(0, sock_addr);
[1993]1489 }
[2028]1490#endif
[1993]1491
[0]1492 // start main loop...
[905]1493 main_loop(drvid, drvinst, &sa, sysclocksync);
[0]1494
1495 // clean up.
1496 clean_quit_prep();
1497 driver_close(drvinst, drvid);
1498 output_buffer_free();
1499
1500 return 0;
1501}
1502
[574]1503void cleanup_listen_socket (int terminate) {
[2530]1504 int i;
1505
[1993]1506 // Deregister from SLP:
[2028]1507#ifdef ROAR_HAVE_LIBSLP
[1993]1508 register_slp(1, NULL);
[2028]1509#endif
[580]1510
[1494]1511#ifdef ROAR_SUPPORT_LISTEN
[2530]1512 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
1513  if ( g_listen_socket[i] != -1 ) {
[1486]1514#ifdef ROAR_HAVE_IO_POSIX
[2530]1515   close(g_listen_socket[i]);
[1486]1516#endif // #else is useless because we are in void context.
[60]1517
[2530]1518   g_listen_socket[i] = -1;
[576]1519
[1486]1520#ifdef ROAR_HAVE_UNIX
[2530]1521   if ( server[i] != NULL )
1522    if ( *(server[i]) == '/' )
1523     unlink(server[i]);
[1486]1524#endif
[2530]1525  }
[580]1526 }
[60]1527
[1494]1528#endif
1529
[574]1530 if ( terminate )
1531  g_terminate = 1;
1532}
1533
1534void clean_quit_prep (void) {
1535 cleanup_listen_socket(0);
[60]1536
[2485]1537#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1538 sources_free();
[2485]1539#endif
[0]1540 streams_free();
1541 clients_free();
[2489]1542#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1543 ssynth_free();
[2489]1544#endif
[2487]1545#ifndef ROAR_WITHOUT_DCOMP_CB
[282]1546 midi_cb_stop(); // stop console beep
[2487]1547#endif
[2500]1548#ifndef ROAR_WITHOUT_DCOMP_MIDI
[281]1549 midi_free();
[2500]1550#endif
[2494]1551#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]1552 light_free();
[2494]1553#endif
[2718]1554#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1555 rdtcs_free();
1556#endif
[2104]1557
1558#ifdef SUPPORT_PIDFILE
1559 if ( pidfile != NULL )
1560  unlink(pidfile);
1561#endif
[0]1562}
1563
1564void clean_quit (void) {
1565 clean_quit_prep();
1566// driver_close(drvinst, drvid);
1567// output_buffer_free();
1568 exit(0);
1569}
1570
1571//ll
Note: See TracBrowser for help on using the repository browser.