source: roaraudio/roard/roard.c @ 3959:56b27f189c07

Last change on this file since 3959:56b27f189c07 was 3959:56b27f189c07, checked in by phi, 14 years ago

support for home-relative paths

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