source: roaraudio/roard/roard.c @ 4246:fda6cde65064

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

typo

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