source: roaraudio/roard/roard.c @ 3802:958585351e28

Last change on this file since 3802:958585351e28 was 3802:958585351e28, checked in by phi, 14 years ago

converted listen socket handling mostly to VIOs

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