source: roaraudio/roard/roard.c @ 3731:53095de69773

Last change on this file since 3731:53095de69773 was 3731:53095de69773, checked in by phi, 14 years ago

corrected prototype

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