source: roaraudio/roard/roard.c @ 3733:78a938545a68

Last change on this file since 3733:78a938545a68 was 3733:78a938545a68, checked in by phi, 14 years ago

return correct error value

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