source: roaraudio/roard/roard.c @ 3971:370e1268ac0e

Last change on this file since 3971:370e1268ac0e was 3966:7076f996366a, checked in by phi, 14 years ago

added rsound-unix profile

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