source: roaraudio/roard/roard.c @ 3987:c7ff0c5e5e7b

Last change on this file since 3987:c7ff0c5e5e7b was 3987:c7ff0c5e5e7b, checked in by phi, 14 years ago

added profiles: roar-abstract, rplay-tcp, rplay-tcp-pub; ist port as (none) if non-tcp

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