source: roaraudio/roard/roard.c @ 3932:04a795c3f5c1

Last change on this file since 3932:04a795c3f5c1 was 3907:3146f952e764, checked in by phi, 14 years ago

only declare vars we use

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