source: roaraudio/roard/roard.c @ 3539:1fd530767d7b

Last change on this file since 3539:1fd530767d7b was 3517:1a3218a3fc5b, checked in by phi, 14 years ago

updated license headers, FSF moved office

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