source: roaraudio/roard/roard.c @ 3688:7624aa24f3ef

Last change on this file since 3688:7624aa24f3ef was 3688:7624aa24f3ef, checked in by phi, 14 years ago

added --log-syslog to roard

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