source: roaraudio/roard/roard.c @ 3963:4c89fe99987b

Last change on this file since 3963:4c89fe99987b was 3963:4c89fe99987b, checked in by phi, 14 years ago

corrected spacing

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