source: roaraudio/roard/roard.c @ 3685:cab6f9a5c647

Last change on this file since 3685:cab6f9a5c647 was 3684:69134bcda4c2, checked in by phi, 14 years ago

added most of the rsound interface

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