source: roaraudio/roard/roard.c @ 4324:08c67c699868

Last change on this file since 4324:08c67c699868 was 4324:08c67c699868, checked in by phi, 14 years ago

register notify proxy, added some debugging

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