source: roaraudio/roard/roard.c @ 2539:616cc31e245a

Last change on this file since 2539:616cc31e245a was 2530:921f7ac0be31, checked in by phi, 15 years ago

added support for multi homed server

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