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
Line 
1//roard.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
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
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28#ifdef ROAR_SUPPORT_LISTEN
29char * server[ROAR_MAX_LISTEN_SOCKETS];
30#endif
31
32#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_FS_POSIX)
33#define SUPPORT_PIDFILE
34char * pidfile = NULL;
35#endif
36
37#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
38int    setids    = 0;
39#endif
40
41#ifdef ROAR_HAVE_LIBX11
42char * x11display = NULL;
43#endif
44
45int add_output (char * drv, char * dev, char * opts, int prim, int count);
46
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
85#ifdef ROAR_HAVE_MAIN_ARGS
86void usage (void) {
87 printf("Usage: roard [OPTIONS]...\n\n");
88
89 printf("Misc Options:\n\n");
90 printf(
91        " --daemon              - Bring the server into background after init\n"
92        " --verbose             - Be more verbose, can be used multiple times\n"
93        " --terminate           - Terminate after last client quited\n"
94        " --start               - No op parameter (starting roard is default operation)\n"
95        " --restart             - Trys to stop an old instance and start a new with new settings\n"
96        " --stop                - Stops a running roard (provide --pidfile!)\n"
97        " --shutdown            - Terminates a running roard (provide --pidfile!)\n"
98        " --realtime            - Trys to get realtime priority,\n"
99        "                         give multible times for being more realtime\n"
100        " --memlock LEVEL       - Set default memory locking level to LEVEL\n"
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"
104        " --sysclocksync        - calculate exact sample rate using the system clock\n"
105        " --location  LOC       - Set lion readable location of server\n"
106#ifdef SUPPORT_PIDFILE
107        " --pidfile PIDFILE     - Write a pidfile at PIDFILE\n"
108#endif
109#ifdef ROAR_HAVE_SYSLOG
110        " --log-syslog          - Log Warnings, Errors, ... to syslog\n"
111#endif
112       );
113
114 printf("\nPlugin Options:\n\n");
115 printf(
116        " --plugin-load FILE    - Load plugin FILE\n"
117       );
118
119 printf("\nAudio Options:\n\n");
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"
124        " --aiprofile  PROFILE  - Use the given audio profile\n"
125       );
126
127 printf("\nStream Options:\n\n");
128 printf(
129        " --stream-flags D=F    - Set default flags for stream directions\n"
130        "                         D is the stream direction and F is a comma separated\n"
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
135 printf("\nOutput Options:\n\n");
136 printf(" -o  --odriver DRV     - Set the driver, use '--list-driver' to get a list\n");
137 printf(" -O  --odevice DEV     - Set the device\n");
138 printf(" -oO OPTS              - Set output options\n");
139 printf(" -oN                   - Adds another output\n");
140 printf(" -oP                   - Mark output as primary\n");
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
146
147#ifndef ROAR_WITHOUT_DCOMP_SOURCES
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"
152        " -sN                   - Adds another source\n"
153        " -sP                   - Make souce as primary\n"
154       );
155 printf(" --list-sources        - List all sources\n");
156#endif
157
158 printf("\nCodec Filter Options:\n\n");
159 printf(" --list-cf             - List all codec filter\n"
160       );
161
162#ifndef ROAR_WITHOUT_DCOMP_MIDI
163 printf("\nMIDI Options:\n\n");
164 printf(" --midi-no-console     - Disable console based MIDI synth\n"
165        " --midi-console-enable - Enables the console based MIDI synth\n"
166        " --midi-console DEV    - Set device for MIDI console\n"
167#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
168        " --ssynth-enable       - Enable simple software synth\n"
169        " --ssynth-disable      - Disable simple software synth\n"
170#endif
171       );
172#endif
173
174#ifndef ROAR_WITHOUT_DCOMP_LIGHT
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       );
179#endif
180
181#ifndef ROAR_WITHOUT_DCOMP_RDTCS
182 printf("\nRadio Data and Transmitter Control System Options:\n\n");
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
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
199 printf("\nServer Options:\n\n");
200 printf(" -t  --tcp             - Use TCP listen socket\n"
201#ifdef ROAR_HAVE_UNIX
202        " -u  --unix            - Use UNIX Domain listen socket (default)\n"
203#endif
204#ifdef ROAR_HAVE_LIBDNET
205        " -n  --decnet          - use DECnet listen socket\n"
206#endif
207        " -4                    - Use IPv4 connections (implies -t)\n"
208#ifdef AF_INET6
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"
213        "                         this is normaly not useful.\n"
214#endif
215        " -p  --port            - TCP Port to bind to\n"
216        " -b  --bind            - IP/Hostname to bind to\n"
217        "     --sock            - Filename for UNIX Domain Socket\n"
218        "     --proto PROTO     - Use PROTO as protocol on Socket\n"
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"
223        "     --proto-chans C   - Set number of channels parameter for protocol\n"
224        "     --proto-aiprofile PROFILE\n"
225        "                       - Sets the audio profile for socket\n"
226        "     --proto-profile P - Set profile for listen socket\n"
227        "     --list-proto      - List supported protocols\n"
228        "     --list-profiles   - List supported profiles for --proto-profile\n"
229        "     --new-sock        - Parameters for new socket follows\n"
230#ifdef ROAR_HAVE_LIBSLP
231        "     --slp             - Enable OpenSLP support\n"
232#endif
233#ifdef ROAR_HAVE_LIBX11
234        "     --x11             - Enable X11 support\n"
235#endif
236        " --jumbo-mtu MTU       - Sets the MTU for Jumbo Packets\n"
237        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: %s)\n"
238        "                         You need the permissions to change the GID\n"
239        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
240        "                         You need the permissions to change the UID (normaly only root has)\n"
241        " --no-listen           - Do not listen for new clients\n"
242        "                         (only useful for relaing, impleys --terminate)\n"
243        " --client-fh           - Comunicate with a client over this handle\n"
244        "                         (only useful for relaing)\n"
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",
248#ifdef ROAR_DEFAULT_SOCKGRP
249        ROAR_DEFAULT_SOCKGRP
250#else
251        "(none)"
252#endif
253       );
254// printf("\n Options:\n\n");
255 printf("\n");
256}
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
265#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
266 printf("  simple        WM LRX - PulseAudio simple protocol\n");
267#endif
268#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
269 printf("  rsound        W      - RSound emulation\n");
270#endif
271#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
272 printf("  rplay         W      - RPlay emulation\n");
273#endif
274}
275
276#endif
277
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
295int restart_server (char * server, int terminate) {
296 struct roar_connection con;
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
304 ROAR_INFO("restart_server(server='%s', terminate=%i): trying to restart server", ROAR_DBG_INFO_INFO, server, terminate);
305
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 ) {
317     ok = kill(pid, SIGUSR1);
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
332
333 if ( roar_connect(&con, server) == -1 ) {
334  return -1;
335 }
336
337 if ( roar_terminate(&con, terminate) == -1 ) {
338  return -1;
339 }
340
341 return roar_disconnect(&con);
342}
343
344#define R_SETUID 1
345#define R_SETGID 2
346
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
360 g_config->streams[ROAR_DIR_PLAY    ].flags = ROAR_FLAG_META;
361 g_config->streams[ROAR_DIR_OUTPUT  ].flags = ROAR_FLAG_PASSMIXER;
362 g_config->streams[ROAR_DIR_FILTER  ].flags = ROAR_FLAG_SYNC;
363 g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC;
364 g_config->streams[ROAR_DIR_BIDIR   ].flags = ROAR_FLAG_ANTIECHO;
365
366 g_config->location = "***default***";
367
368 g_config->memlock_level = -1;
369
370 return 0;
371}
372
373#ifdef ROAR_SUPPORT_LISTEN
374int init_listening (void) {
375 int i;
376
377 memset(g_listen, 0, sizeof(g_listen));
378
379 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
380  g_listen[i].proto  = ROAR_PROTO_ROARAUDIO;
381  server[i]          = NULL;
382 }
383
384 return 0;
385}
386
387int get_listen(struct roard_listen ** sock, char *** sockname) {
388 int i;
389
390 if ( sock == NULL )
391  return -1;
392
393 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
394  if ( ! g_listen[i].used ) {
395   server[i] = NULL;
396   *sock = &(g_listen[i]);
397
398   if ( sockname != NULL )
399    *sockname = &(server[i]);
400
401   return 0;
402  }
403 }
404
405 return -1;
406}
407
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:
420#ifdef ROAR_HAVE_UNIX
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"},
427#endif
428#ifdef ROAR_HAVE_IPV4
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"},
435#endif
436#ifdef ROAR_HAVE_LIBDNET
437 {"roar-dnet",      ROAR_SOCKET_TYPE_DECNET, 0,                 ROAR_DEFAULT_LISTEN_OBJECT,
438                    ROAR_PROTO_ROARAUDIO, -1, NULL,
439                    "RoarAudio default DECnet"},
440#endif
441#ifdef ROAR_HAVE_UNIX
442 {"roar-abstract",  ROAR_SOCKET_TYPE_UNIX,   0,                 "+abstract",        ROAR_PROTO_ROARAUDIO, -1, NULL,
443                    "RoarAudio abstract namespace profile"},
444#endif
445
446 // EsounD:
447#if !defined(ROAR_WITHOUT_DCOMP_EMUL_ESD) && defined(ROAR_HAVE_H_ESD)
448#ifdef ROAR_HAVE_UNIX
449 {"esd-unix",       ROAR_SOCKET_TYPE_UNIX,   0,                 ROAR_DEFAULT_ESD_GSOCK,
450                    ROAR_PROTO_ESOUND,    -1, NULL,
451                    "EsounD default local profile"},
452#endif
453#ifdef ROAR_HAVE_IPV4
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"},
460#endif
461#endif
462
463 // RSound:
464#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
465#ifdef ROAR_HAVE_UNIX
466 {"rsound-unix",    ROAR_SOCKET_TYPE_UNIX,   0,                 ROAR_DEFAULT_RSOUND_GSOCK,
467                    ROAR_PROTO_RSOUND,    -1, NULL,
468                    "RSound default local profile"},
469#endif
470#ifdef ROAR_HAVE_IPV4
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"},
477#endif
478#ifdef ROAR_HAVE_LIBDNET
479 {"rsound-dnet",    ROAR_SOCKET_TYPE_DECNET, 0,                 ROAR_DEFAULT_RSOUND_OBJECT,
480                    ROAR_PROTO_RSOUND,    -1, NULL,
481                    "RSound DECnet profile"},
482#endif
483#endif
484
485 // PulseAudio Simple:
486#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
487#ifdef ROAR_HAVE_IPV4
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"},
494#endif
495#endif
496
497 // RPlay:
498#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
499#ifdef ROAR_HAVE_IPV4
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"},
506#endif
507#endif
508
509 // End of List:
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;
517 char port[8];
518
519 printf("Name           Type    Address          Port    Protocol  Dir        Audio Profile - Description\n");
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  }
532
533  if ( p->port ) {
534   snprintf(port, sizeof(port)-1, "%i", p->port);
535   port[sizeof(port)-1] = 0;
536  } else {
537   strcpy(port, "(none)");
538  }
539
540  printf("%-14s %-7s %-16s %-7s %-9s %-10s %-13s - %s\n",
541           p->name,
542           type,
543           p->sockaddr, port,
544           roar_proto2str(p->proto),
545           p->dir == -1 ? "(none)" : roar_dir2str(p->dir), p->aiprofile == NULL ? "(none)" : p->aiprofile,
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
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   }
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
586int add_listen (char * addr, int port, int sock_type, char * user, char * group, int proto, int dir, struct roar_audio_info * info) {
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;
597#ifdef ROAR_HAVE_UNIX
598 int    sock;
599#endif
600 int    i;
601
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
607 if ( *addr != 0 ) {
608  for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
609   if ( ! g_listen[i].used ) {
610    sockid = i;
611    break;
612   }
613  }
614
615  if ( sockid == -1 )
616   return -1;
617
618  g_listen[sockid].proto = proto;
619
620  ROAR_DBG("add_listen(*): proto=0x%.4x", proto);
621
622  if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) {
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);
631     ROAR_ERR("Can not open listen socket: Socket allready in use");
632     return 1;
633    } else {
634     unlink(addr);
635     if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) {
636      ROAR_ERR("Can not open listen socket: %s", strerror(errno));
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 {
648    ROAR_ERR("Can not open listen socket: %s", strerror(errno));
649    return 1;
650   }
651  }
652
653#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
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   }
658  }
659#endif
660#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
661  if ( user ) {
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 == '/' ) {
670   if ( grp || pwd ) {
671     if ( chown(addr, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 )
672      return 1;
673   }
674#ifdef ROAR_HAVE_GETUID
675   if ( grp ) {
676    if ( getuid() == 0 )
677     if ( chmod(addr, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
678      return 1;
679   }
680#endif
681  }
682#endif
683 }
684
685 // in case we opened the listening socket correctly.
686 if ( dir == -1 )
687  dir = ROAR_DIR_PLAY;
688
689 g_listen[sockid].inst.stpl.dir = dir;
690 memcpy(&(g_listen[sockid].inst.stpl.info), info, sizeof(struct roar_audio_info));
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
712 g_listen[sockid].used = 1;
713 server[sockid]        = addr;
714
715 return 0;
716}
717#endif
718
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) {
743   case '+': k++; op = ROAR_SET_FLAG;   break;
744   case '-': k++; op = ROAR_RESET_FLAG; break;
745   default:
746     op = ROAR_SET_FLAG;
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;
763  } else if ( !strcmp(k, "passmixer") ) {
764   flag = ROAR_FLAG_PASSMIXER;
765  } else {
766   return -1;
767  }
768
769  g_config->streams[dir].flags |= flag;
770
771  if ( op == ROAR_RESET_FLAG )
772   g_config->streams[dir].flags -= flag;
773 }
774
775 return 0;
776}
777
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) {
782 char * drvs[] = {
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
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:
797  NULL
798 };
799 int i;
800 int ret;
801 int _alive;
802
803 if ( drv != NULL )
804  return add_output(drv, dev, opts, prim, count);
805
806 if ( dev != NULL ) {
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);
808 }
809
810 for (i = 0; drvs[i] != NULL; i++) {
811  ROAR_INFO("add_default_output(*): trying driver %s", ROAR_DBG_INFO_INFO, drvs[i]);
812  _alive = alive; // save global alive setting
813
814  ret = add_output(drvs[i], dev, opts, prim, count);
815  if ( ret != -1 )
816   return ret;
817
818  alive = _alive; // restore global alive setting
819  ROAR_INFO("add_default_output(*): Driver %s faild to load", ROAR_DBG_INFO_VERBOSE, drvs[i]);
820 }
821
822 return -1;
823}
824#endif
825
826int add_output (char * drv, char * dev, char * opts, int prim, int count) {
827 int stream;
828 struct roar_stream * s;
829 struct roar_stream_server * ss;
830 char * k, * v;
831#ifdef ROAR_DRIVER_CODEC
832 char * to_free = NULL;
833#endif
834 int sync = 0, f_mmap = 0;
835 int32_t blocks = -1, blocksize = -1;
836 int dir = ROAR_DIR_OUTPUT;
837 int error = 0;
838 // DMX:
839 int32_t channel  = -1;
840 int32_t universe = -1;
841 uint16_t tu16;
842 float q = -32e6;
843
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);
845
846 if ( drv == NULL && count == 0 ) {
847#ifdef ROAR_DRIVER_DEFAULT
848  drv  = ROAR_DRIVER_DEFAULT;
849  prim = 1;
850  sync = 1;
851
852#ifdef ROAR_DRIVER_CODEC
853  if ( opts == NULL ) {
854   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
855  }
856#endif
857#else
858  ROAR_ERR("add_output(*): Can not find default driver");
859  return -1;
860#endif
861 }
862
863 if ( opts == NULL && count == 0 ) {
864  sync = 1;
865  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
866 }
867
868 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
869
870 if ( (stream = streams_new()) == -1 ) {
871  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
872  if ( prim ) alive = 0;
873  return -1;
874 }
875
876 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
877
878 streams_get(stream, &ss);
879 s = ROAR_STREAM(ss);
880
881 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
882
883 memset(&(s->info), 0xFF, sizeof(struct roar_audio_info)); // set everything to -1
884
885 s->pos_rel_id = -1;
886// s->info.codec = codec;
887
888 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
889
890 if ( opts == NULL ) {
891  k = NULL;
892 } else {
893  k = strtok(opts, ",");
894 }
895
896 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s'): initial k='%s'(%p)", drv, dev, opts, k, k);
897
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 ) {
913   if ( (s->info.codec = roar_str2codec(v)) == -1 ) {
914    ROAR_ERR("add_output(*): unknown codec '%s'", v);
915    error++;
916   }
917  } else if ( strcmp(k, "q") == 0 ) {
918   q = atof(v);
919  } else if ( strcmp(k, "blocks") == 0 ) {
920   blocks = atoi(v);
921  } else if ( strcmp(k, "blocksize") == 0 ) {
922   blocksize = atoi(v);
923  } else if ( strcmp(k, "mmap") == 0 ) {
924   f_mmap = 1;
925  } else if ( strcmp(k, "subsystem") == 0 ) {
926   if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) {
927    dir = ROAR_DIR_OUTPUT;
928#ifndef ROAR_WITHOUT_DCOMP_MIDI
929   } else if ( !strcasecmp(v, "midi") ) {
930    dir = ROAR_DIR_MIDI_OUT;
931#endif
932#ifndef ROAR_WITHOUT_DCOMP_LIGHT
933   } else if ( !strcasecmp(v, "light") ) {
934    dir = ROAR_DIR_LIGHT_OUT;
935#endif
936#ifndef ROAR_WITHOUT_DCOMP_RAW
937   } else if ( !strcasecmp(v, "raw") ) {
938    dir = ROAR_DIR_RAW_OUT;
939#endif
940   } else if ( !strcasecmp(v, "complex") ) {
941    dir = ROAR_DIR_COMPLEX_OUT;
942   } else {
943    ROAR_ERR("add_output(*): unknown/unsupported subsystem '%s'", k);
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++;
953   }
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
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   }
967
968  } else if ( strcmp(k, "meta") == 0 ) {
969   streams_set_flag(stream, ROAR_FLAG_META);
970  } else if ( strcmp(k, "sync") == 0 ) {
971   sync = 1;
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 ) {
978   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
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);
983  } else {
984   ROAR_ERR("add_output(*): unknown option '%s'", k);
985   error++;
986  }
987
988  if ( error ) {
989   streams_delete(stream);
990   if ( prim ) alive = 0;
991#ifdef ROAR_DRIVER_CODEC
992   if ( to_free != NULL )
993    free(to_free);
994#endif
995   return -1;
996  }
997
998  k = strtok(NULL, ",");
999 }
1000
1001 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
1002
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
1054 if ( streams_set_dir(stream, dir, 1) == -1 ) {
1055  streams_delete(stream);
1056  return -1;
1057 }
1058
1059#ifdef ROAR_DRIVER_CODEC
1060 if ( to_free != NULL )
1061  free(to_free);
1062#endif
1063
1064 if ( s->info.codec == ROAR_CODEC_ALAW || s->info.codec == ROAR_CODEC_MULAW )
1065  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
1066
1067 ROAR_STREAM_SERVER(s)->codec_orgi = s->info.codec;
1068
1069 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) {
1070  ss->driver_id = -1; // don't close a driver not opened...
1071  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
1072  streams_delete(stream);
1073  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
1074  if ( prim ) alive = 0;
1075  return -1;
1076 }
1077
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
1081 if ( blocks != -1 )
1082  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
1083
1084 if ( blocksize != -1 )
1085  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
1086
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
1097 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
1098
1099 streams_set_fh(stream, -1); // update some internal structures
1100
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
1106 client_stream_add(g_self_client, stream);
1107
1108 if ( prim ) {
1109  streams_mark_primary(stream);
1110  s->pos_rel_id = stream;
1111 }
1112
1113 if ( sync ) {
1114  streams_set_flag(stream, ROAR_FLAG_SYNC);
1115 } else {
1116  streams_reset_flag(stream, ROAR_FLAG_SYNC);
1117 }
1118
1119 if ( f_mmap )
1120  streams_set_flag(stream, ROAR_FLAG_MMAP);
1121
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);
1123 return 0;
1124}
1125
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
1132 if ( (x11con = roar_x11_connect(x11display)) == NULL ) {
1133  ROAR_ERR("Can not connect to X11 server for %sregistering", unreg ? "un" : "");
1134  return -1;
1135 }
1136
1137 if ( unreg ) {
1138  if ( roar_x11_delete_prop(x11con, "ROAR_SERVER") == -1 ) {
1139   ret = -1;
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  }
1144 } else {
1145  if ( roar_x11_set_prop(x11con, "ROAR_SERVER", sockname) == -1 ) {
1146   ret = -1;
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  }
1151 }
1152
1153 roar_x11_disconnect(x11con);
1154
1155 return ret;
1156}
1157#endif
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] = "";
1174 char * location;
1175
1176 if ( sockname != NULL )
1177  sn = sockname;
1178
1179 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE_ROAR "://%s", sn);
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) {
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
1196  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
1197#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1198                               "(light-channels=%i),"
1199#endif
1200                               "(location=%s)",
1201           g_sa->rate, g_sa->channels, g_sa->bits,
1202#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1203           g_light_state.channels,
1204#endif
1205           location
1206          );
1207
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);
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 }
1225
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;
1239 }
1240
1241 SLPClose(hslp);
1242 return 0;
1243#else
1244 return -1;
1245#endif
1246}
1247
1248
1249// MAIN:
1250
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                       }
1255
1256#ifdef ROAR_HAVE_MAIN_ARGS
1257int main (int argc, char * argv[]) {
1258#else
1259int main (void) {
1260#endif
1261#ifdef ROAR_HAVE_MAIN_ARGS
1262 int i;
1263 char * k;
1264#endif
1265#if defined(ROAR_SUPPORT_LISTEN) && defined(ROAR_HAVE_GETUID)
1266 char user_sock[80]  = {0};
1267#endif
1268 struct roar_audio_info sa, max_sa;
1269 struct roard_config config;
1270#ifdef ROAR_HAVE_FORK
1271 int    daemon       = 0;
1272#endif
1273 int    realtime     = 0;
1274 int    sysclocksync = 0;
1275 char * driver    = NULL;
1276 char * device    = NULL;
1277#ifdef ROAR_HAVE_MAIN_ARGS
1278 char * opts      = NULL;
1279#endif
1280// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
1281#ifdef ROAR_SUPPORT_LISTEN
1282 int    port       = ROAR_DEFAULT_PORT;
1283 char * sock_addr  = NULL;
1284 int    sock_proto = ROAR_PROTO_ROARAUDIO;
1285 int    sock_dir   = -1;
1286 struct roar_audio_info sock_info = {0, 0, 0, 0};
1287#endif
1288 int               drvid;
1289#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1290 char * s_drv     = "cf";
1291 char * s_dev     = NULL;
1292 char * s_con     = NULL;
1293 char * s_opt     = NULL;
1294 int    s_prim    = 0;
1295#endif
1296 char * o_drv     = getenv("ROAR_DRIVER");
1297 char * o_dev     = getenv("ROAR_DEVICE");
1298 char * o_opts    = NULL;
1299 int    o_prim    = 0;
1300 int    o_count   = 0;
1301#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1302 int    light_channels = LIGHT_CHANNELS_DEFAULT;
1303#endif
1304#ifdef ROAR_DEFAULT_SOCKGRP
1305 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
1306#else
1307 char * sock_grp  = NULL;
1308#endif
1309 char * sock_user = NULL;
1310#ifdef ROAR_SUPPORT_LISTEN
1311 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
1312#endif
1313#ifdef ROAR_HAVE_LIBSLP
1314 int    reg_slp   = 0;
1315#endif
1316#ifdef ROAR_HAVE_LIBX11
1317 int    reg_x11   = 0;
1318#endif
1319#ifdef ROAR_HAVE_CHROOT
1320 char * chrootdir = NULL;
1321#endif
1322#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
1323 struct group   * grp  = NULL;
1324#endif
1325#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
1326 struct passwd  * pwd  = NULL;
1327#endif
1328#ifdef ROAR_HAVE_GETSERVBYNAME
1329 struct servent * serv = NULL;
1330#endif
1331 DRIVER_USERDATA_T drvinst;
1332 struct roar_client * self = NULL;
1333#ifdef ROAR_HAVE_LIBDNET
1334 char decnethost[80];
1335#endif
1336#ifdef SUPPORT_PIDFILE
1337 struct roar_vio_calls pidfile_vio;
1338#endif
1339
1340 ROAR_DBG("main(*): starting roard...");
1341
1342 g_standby       =  0;
1343 g_autostandby   =  0;
1344 alive           =  1;
1345#ifdef ROAR_SUPPORT_LISTEN
1346 g_no_listen     =  0;
1347#else
1348 g_terminate     =  1;
1349#endif
1350
1351 g_verbose       = ROAR_DBG_INFO_NONE;
1352
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
1358 g_sa        = &sa;
1359 g_max_sa    = &max_sa;
1360
1361 memcpy(g_max_sa, g_sa, sizeof(max_sa));
1362
1363 counters_init();
1364
1365 g_config = &config;
1366
1367 if ( init_config() == -1 ) {
1368  ROAR_ERR("Can not init default config!");
1369  return 1;
1370 }
1371
1372 // load config
1373 roar_libroar_get_config();
1374
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
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
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
1398#ifndef ROAR_WITHOUT_DCOMP_MIDI
1399 if ( midi_init_config() == -1 ) {
1400  ROAR_ERR("Can not init MIDI config!");
1401  return 1;
1402 }
1403#endif
1404
1405#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1406 if ( ssynth_init_config() == -1 ) {
1407  ROAR_ERR("Can not init ssynth config!");
1408  return 1;
1409 }
1410#endif
1411
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
1419 if ( plugins_preinit() == -1 ) {
1420  ROAR_ERR("Can not pre-init plugins!");
1421  return 1;
1422 }
1423
1424#ifdef ROAR_SUPPORT_LISTEN
1425#ifndef ROAR_TARGET_WIN32
1426 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
1427#else
1428 sock_addr = ROAR_DEFAULT_HOST;
1429#endif
1430
1431#ifdef ROAR_HAVE_GETUID
1432 if ( getuid() != 0 && getenv("HOME") != NULL ) {
1433/*
1434  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
1435*/
1436  roar_env_render_path_r(user_sock, sizeof(user_sock), "~/" ROAR_DEFAULT_SOCK_USER);
1437  sock_addr = user_sock;
1438  ROAR_DBG("main(*): setting sock_addr='%s'", sock_addr);
1439 }
1440#endif
1441
1442 if ( getenv("ROAR_SERVER") != NULL )
1443  sock_addr = getenv("ROAR_SERVER");
1444#endif
1445
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
1456 if ( (g_self_client = clients_new()) == -1 ) {
1457  ROAR_ERR("Can not create self client!");
1458  return 1;
1459 }
1460
1461#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1462 if ( sources_init() == -1 ) {
1463  ROAR_ERR("Can not init sources!");
1464  return 1;
1465 }
1466
1467 if ( (sources_set_client(g_self_client)) == -1 ) {
1468  ROAR_ERR("Can not init set source client!");
1469  return 1;
1470 }
1471#endif
1472
1473#ifdef ROAR_HAVE_MAIN_ARGS
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
1481  } else if ( strcmp(k, "--start") == 0 ) {
1482   // this is a no op
1483  } else if ( strcmp(k, "--restart") == 0 ) {
1484#ifdef ROAR_SUPPORT_LISTEN
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);
1487   }
1488#else
1489   ROAR_ERR("--restart not supported");
1490#endif
1491  } else if ( strcmp(k, "--shutdown") == 0 ) {
1492#ifdef ROAR_SUPPORT_LISTEN
1493   if ( restart_server(sock_addr, 1) == -1 ) {
1494    ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
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
1504   if ( restart_server(sock_addr, 0) == -1 ) {
1505    ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
1506    return 1;
1507   }
1508   return 0;
1509#else
1510   ROAR_ERR("--stop not supported");
1511   return 1;
1512#endif
1513
1514
1515  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
1516#ifdef ROAR_HAVE_FORK
1517   daemon = 1;
1518#else
1519   ROAR_ERR("--daemon not supported");
1520#endif
1521  } else if ( strcmp(k, "--verbose") == 0 ) {
1522   g_verbose++;
1523  } else if ( strcmp(k, "--terminate") == 0 ) {
1524   g_terminate = 1;
1525  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1526   sysclocksync = 1000;
1527  } else if ( strcmp(k, "--realtime") == 0 ) {
1528   realtime++;
1529  } else if ( strcmp(k, "--memlock") == 0 ) {
1530   _CKHAVEARGS(1);
1531   g_config->memlock_level = memlock_str2level(argv[++i]);
1532  } else if ( strcmp(k, "--chroot") == 0 ) {
1533   _CKHAVEARGS(1);
1534#ifdef ROAR_HAVE_CHROOT
1535   chrootdir = argv[++i];
1536#else
1537   ROAR_ERR("--chroot not supported");
1538   i++;
1539#endif
1540  } else if ( strcmp(k, "--setgid") == 0 ) {
1541#ifdef ROAR_HAVE_SETGID
1542   setids |= R_SETGID;
1543#else
1544   ROAR_ERR("--setgid not supported");
1545#endif
1546  } else if ( strcmp(k, "--setuid") == 0 ) {
1547#ifdef ROAR_HAVE_SETUID
1548   setids |= R_SETUID;
1549#else
1550   ROAR_ERR("--setuid not supported");
1551#endif
1552  } else if ( strcmp(k, "--location") == 0 ) {
1553   _CKHAVEARGS(1);
1554   g_config->location = argv[++i];
1555  } else if ( strcmp(k, "--pidfile") == 0 ) {
1556   _CKHAVEARGS(1);
1557#ifdef SUPPORT_PIDFILE
1558   pidfile = argv[++i];
1559#else
1560   ROAR_ERR("--pidfile not supported");
1561   i++;
1562#endif
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
1570
1571  } else if ( strcmp(k, "--plugin-load") == 0 ) {
1572   _CKHAVEARGS(1);
1573   if ( plugins_load(argv[++i]) == -1 ) {
1574    ROAR_ERR("Can not load plugin");
1575   }
1576
1577  } else if ( strcmp(k, "--list-cf") == 0 ) {
1578   print_codecfilterlist();
1579   return 0;
1580
1581  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
1582   _CKHAVEARGS(1);
1583   sa.rate = atoi(argv[++i]);
1584  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
1585   _CKHAVEARGS(1);
1586   sa.bits = atoi(argv[++i]);
1587  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
1588   _CKHAVEARGS(1);
1589   sa.channels = atoi(argv[++i]);
1590
1591  } else if ( strcmp(k, "--aiprofile") == 0 ) {
1592   _CKHAVEARGS(1);
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
1599  } else if ( strcmp(k, "--stream-flags") == 0 ) {
1600   _CKHAVEARGS(1);
1601   if ( update_stream_flags(argv[++i]) == -1 ) {
1602    ROAR_ERR("Can not set stream flags");
1603    return 1;
1604   }
1605
1606  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
1607   _CKHAVEARGS(1);
1608   driver = argv[++i];
1609   if ( strcmp(driver, "list") == 0 ) {
1610    ROAR_WARN("The option is obsolete, use --list-driver!");
1611    print_driverlist();
1612    return 0;
1613   }
1614  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
1615   _CKHAVEARGS(1);
1616   device = argv[++i];
1617  } else if ( strcmp(k, "-dO") == 0 ) {
1618   _CKHAVEARGS(1);
1619   opts = argv[++i];
1620  } else if ( strcmp(k, "--list-driver") == 0 ) {
1621   print_driverlist();
1622   return 0;
1623
1624  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
1625   _CKHAVEARGS(1);
1626   o_drv  = argv[++i];
1627  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
1628   _CKHAVEARGS(1);
1629   o_dev  = argv[++i];
1630  } else if ( strcmp(k, "-oO") == 0 ) {
1631   _CKHAVEARGS(1);
1632   o_opts = argv[++i];
1633  } else if ( strcmp(k, "-oP") == 0 ) {
1634   o_prim = 1;
1635  } else if ( strcmp(k, "-oN") == 0 ) {
1636   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1637    o_count++;
1638
1639   o_drv  = o_dev = o_opts = NULL;
1640   o_prim = 0;
1641
1642  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
1643   _CKHAVEARGS(1);
1644#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1645   s_drv = argv[++i];
1646#else
1647   ROAR_ERR("main(*): No support for sources compiled in");
1648   i++;
1649#endif
1650  } else if ( strcmp(k, "-S") == 0 ) {
1651   _CKHAVEARGS(1);
1652#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1653   s_dev = argv[++i];
1654#else
1655   ROAR_ERR("main(*): No support for sources compiled in");
1656   i++;
1657#endif
1658  } else if ( strcmp(k, "-sO") == 0 ) {
1659   _CKHAVEARGS(1);
1660#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1661   s_opt = argv[++i];
1662#else
1663   ROAR_ERR("main(*): No support for sources compiled in");
1664   i++;
1665#endif
1666  } else if ( strcmp(k, "-sC") == 0 ) {
1667   _CKHAVEARGS(1);
1668#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1669   s_con = argv[++i];
1670#else
1671   ROAR_ERR("main(*): No support for sources compiled in");
1672   i++;
1673#endif
1674  } else if ( strcmp(k, "-sP") == 0 ) {
1675#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1676   s_prim = 1;
1677#else
1678   ROAR_ERR("main(*): No support for sources compiled in");
1679#endif
1680  } else if ( strcmp(k, "-sN") == 0 ) {
1681#ifndef ROAR_WITHOUT_DCOMP_SOURCES
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;
1688#else
1689   ROAR_ERR("main(*): No support for sources compiled in");
1690#endif
1691  } else if ( strcmp(k, "--list-sources") == 0 ) {
1692#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1693   print_sourcelist();
1694   return 0;
1695#else
1696   ROAR_ERR("main(*): No support for sources compiled in");
1697   return 1;
1698#endif
1699
1700  } else if ( strcmp(k, "--light-channels") == 0 ) {
1701   _CKHAVEARGS(1);
1702#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1703   light_channels = atoi(argv[++i]);
1704#else
1705   ROAR_WARN("main(*): no light subsystem compiled in");
1706   i++;
1707#endif
1708
1709  } else if ( strcmp(k, "--rds-pi") == 0 ) {
1710   _CKHAVEARGS(1);
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");
1715   i++;
1716#endif
1717  } else if ( strcmp(k, "--rds-ps") == 0 ) {
1718   _CKHAVEARGS(1);
1719#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1720   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
1721    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]);
1722    return 1;
1723   }
1724#else
1725   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1726   i++;
1727#endif
1728  } else if ( strcmp(k, "--rds-pty") == 0 ) {
1729   _CKHAVEARGS(1);
1730#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1731   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
1732    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
1733    return 1;
1734   }
1735#else
1736   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1737   i++;
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
1758
1759  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
1760#ifndef ROAR_WITHOUT_DCOMP_CB
1761   midi_config.init_cb = 0;
1762#else
1763   // no warning here as this is the disable option
1764#endif
1765  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
1766#ifndef ROAR_WITHOUT_DCOMP_CB
1767   midi_config.init_cb = 1;
1768#else
1769   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1770#endif
1771  } else if ( strcmp(k, "--midi-console") == 0 ) {
1772   _CKHAVEARGS(1);
1773#ifndef ROAR_WITHOUT_DCOMP_CB
1774   midi_config.console_dev = argv[++i];
1775   midi_config.init_cb = 1;
1776#else
1777   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1778   i++;
1779#endif
1780
1781  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
1782#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1783   ssynth_conf.enable = 1;
1784#else
1785   ROAR_ERR("main(*): No support for ssynth compiled in");
1786#endif
1787  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
1788#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1789   ssynth_conf.enable = 0;
1790#else
1791   // we can safely ignore the disable
1792#endif
1793
1794  } else if ( strcmp(k, "--x11-display") == 0 || strcmp(k, "--display") == 0 ) {
1795   _CKHAVEARGS(1);
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
1804  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
1805   _CKHAVEARGS(1);
1806   // This is only useful in INET not UNIX mode.
1807#ifdef ROAR_SUPPORT_LISTEN
1808   if ( *sock_addr == '/' )
1809    sock_addr = ROAR_DEFAULT_HOST;
1810
1811   errno = 0;
1812   if ( (port = atoi(argv[++i])) < 1 ) {
1813#ifdef ROAR_HAVE_GETSERVBYNAME
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);
1822#else
1823    ROAR_ERR("invalite port number: %s", argv[i]);
1824    return 1;
1825#endif
1826   }
1827#endif
1828  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
1829   _CKHAVEARGS(1);
1830#ifdef ROAR_SUPPORT_LISTEN
1831   sock_addr = argv[++i];
1832#else
1833   i++;
1834#endif
1835
1836  } else if ( strcmp(k, "--proto") == 0 ) {
1837#ifdef ROAR_SUPPORT_LISTEN
1838   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
1839    ROAR_ERR("Unknown protocol: %s", argv[i]);
1840    return 1;
1841   }
1842#endif
1843  } else if ( strcmp(k, "--proto-dir") == 0 ) {
1844   _CKHAVEARGS(1);
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   }
1850#else
1851   i++;
1852#endif
1853  } else if ( strcmp(k, "--proto-rate") == 0 ) {
1854   _CKHAVEARGS(1);
1855#ifdef ROAR_SUPPORT_LISTEN
1856   sock_info.rate = atoi(argv[++i]);
1857#else
1858   i++;
1859#endif
1860  } else if ( strcmp(k, "--proto-bits") == 0 ) {
1861   _CKHAVEARGS(1);
1862#ifdef ROAR_SUPPORT_LISTEN
1863   sock_info.bits = atoi(argv[++i]);
1864#else
1865   i++;
1866#endif
1867  } else if ( strcmp(k, "--proto-chans") == 0 ) {
1868   _CKHAVEARGS(1);
1869#ifdef ROAR_SUPPORT_LISTEN
1870   sock_info.channels = atoi(argv[++i]);
1871#else
1872   i++;
1873#endif
1874  } else if ( strcmp(k, "--proto-codec") == 0 ) {
1875   _CKHAVEARGS(1);
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   }
1881#else
1882   i++;
1883#endif
1884  } else if ( strcmp(k, "--proto-aiprofile") == 0 ) {
1885   _CKHAVEARGS(1);
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   }
1891#else
1892   i++;
1893#endif
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 ) {
1900   _CKHAVEARGS(1);
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   }
1906#else
1907   i++;
1908#endif
1909
1910
1911  } else if ( strcmp(k, "--list-proto") == 0 ) {
1912   list_proto();
1913   return 0;
1914
1915  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
1916#ifdef ROAR_SUPPORT_LISTEN
1917   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
1918    sock_type = ROAR_SOCKET_TYPE_TCP;
1919
1920   if ( *sock_addr == '/' )
1921    sock_addr = ROAR_DEFAULT_HOST;
1922#endif
1923
1924  } else if ( strcmp(k, "-4") == 0 ) {
1925#ifdef ROAR_SUPPORT_LISTEN
1926   sock_type = ROAR_SOCKET_TYPE_TCP;
1927   if ( *sock_addr == '/' )
1928    sock_addr = ROAR_DEFAULT_HOST;
1929#endif
1930  } else if ( strcmp(k, "-6") == 0 ) {
1931#ifdef ROAR_SUPPORT_LISTEN
1932#ifdef AF_INET6
1933   sock_type = ROAR_SOCKET_TYPE_TCP6;
1934   if ( *sock_addr == '/' )
1935    sock_addr = ROAR_DEFAULT_HOST;
1936#else
1937    ROAR_ERR("No IPv6 support compiled in!");
1938    return 1;
1939#endif
1940#endif
1941
1942  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
1943#ifdef ROAR_SUPPORT_LISTEN
1944   // ignore this case as it is the default behavor.
1945   sock_type = ROAR_SOCKET_TYPE_UNIX;
1946#endif
1947
1948  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
1949#ifdef ROAR_SUPPORT_LISTEN
1950#ifdef ROAR_HAVE_LIBDNET
1951    port   = ROAR_DEFAULT_NUM;
1952    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
1953    sock_addr = decnethost;
1954    sock_type = ROAR_SOCKET_TYPE_DECNET;
1955#else
1956    ROAR_ERR("No DECnet support compiled in!");
1957    return 1;
1958#endif
1959#endif
1960  } else if ( strcmp(k, "--new-sock") == 0 ) {
1961#ifdef ROAR_SUPPORT_LISTEN
1962   if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
1963    ROAR_ERR("Can not open listen socket!");
1964    return 1;
1965   }
1966#endif
1967
1968  } else if ( strcmp(k, "--slp") == 0 ) {
1969#ifdef ROAR_HAVE_LIBSLP
1970   reg_slp = 1;
1971#else
1972   ROAR_ERR("No OpenSLP support compiled in!");
1973   return 1;
1974#endif
1975
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
1985  } else if ( strcmp(k, "--jumbo-mtu") == 0 ) {
1986   _CKHAVEARGS(1);
1987   g_config->jumbo_mtu = atoi(argv[++i]);
1988
1989  } else if ( strcmp(k, "-G") == 0 ) {
1990   _CKHAVEARGS(1);
1991   sock_grp  = argv[++i];
1992  } else if ( strcmp(k, "-U") == 0 ) {
1993   _CKHAVEARGS(1);
1994   sock_user = argv[++i];
1995
1996  } else if ( strcmp(k, "--no-listen") == 0 ) {
1997#ifdef ROAR_SUPPORT_LISTEN
1998   sock_addr   = "";
1999   g_terminate = 1;
2000   g_no_listen = 1;
2001#endif
2002  } else if ( strcmp(k, "--client-fh") == 0 ) {
2003   _CKHAVEARGS(1);
2004   if ( clients_new_from_fh(atoi(argv[++i]), ROAR_PROTO_ROARAUDIO, ROAR_BYTEORDER_NETWORK, 1) == -1 ) {
2005    ROAR_ERR("main(*): Can not set client's fh");
2006    return 1;
2007   }
2008  } else if ( strcmp(k, "--close-fh") == 0 ) {
2009   _CKHAVEARGS(1);
2010#ifdef ROAR_HAVE_IO_POSIX
2011   close(atoi(argv[++i]));
2012#else
2013   i++;
2014   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
2015#endif
2016
2017  } else if ( strcmp(k, "--standby") == 0 ) {
2018   g_standby = 1;
2019  } else if ( strcmp(k, "--auto-standby") == 0 ) {
2020   g_autostandby = 1;
2021  } else {
2022   usage();
2023   return 1;
2024  }
2025
2026 }
2027#endif
2028
2029#ifndef ROAR_WITHOUT_DCOMP_SOURCES
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 }
2035#endif
2036
2037 add_default_output(o_drv, o_dev, o_opts, o_prim, o_count);
2038
2039 ROAR_INFO("Server config: rate=%i, bits=%i, chans=%i", ROAR_DBG_INFO_NOTICE, sa.rate, sa.bits, sa.channels);
2040
2041 if ( waveform_init() == -1 ) {
2042  ROAR_ERR("Can not initialize Waveform subsystem");
2043  return 1;
2044 }
2045
2046#ifndef ROAR_WITHOUT_DCOMP_MIDI
2047 if ( midi_init() == -1 ) {
2048  ROAR_ERR("Can not initialize MIDI subsystem");
2049 }
2050#endif
2051
2052#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2053 if ( ssynth_init() == -1 ) {
2054  ROAR_ERR("Can not initialize ssynth subsystem");
2055 }
2056#endif
2057
2058#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2059 if ( light_init(light_channels) == -1 ) {
2060  ROAR_ERR("Can not initialize light control subsystem");
2061 }
2062#endif
2063
2064#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2065 if ( rdtcs_init() == -1 ) {
2066  ROAR_ERR("Can not initialize RDTCS subsystem");
2067 }
2068#endif
2069
2070 if ( plugins_init() == -1 ) {
2071  ROAR_ERR("Can not initialize plugins");
2072 }
2073
2074#ifdef ROAR_SUPPORT_LISTEN
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  }
2080 }
2081#endif
2082
2083 if ( output_buffer_init(&sa) == -1 ) {
2084  ROAR_ERR("Can not init output buffer!");
2085  return 1;
2086 }
2087
2088 if ( driver == NULL ) {
2089  driver = "null";
2090 } else {
2091  ROAR_ERR("Usage of old driver interface. use -o not -d!");
2092  ROAR_WARN("-d will be removed within the next releases");
2093 }
2094
2095 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
2096  ROAR_ERR("Can not open output driver!");
2097  return 1;
2098 }
2099
2100 if ( samples_init() == -1 ) {
2101  ROAR_ERR("Can not init samples!");
2102  return 1;
2103 }
2104
2105
2106 // we should handle this on microcontrollers, too.
2107#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
2108 signal(SIGINT,  on_sig_int);
2109 signal(SIGTERM, on_sig_term);
2110 signal(SIGCHLD, on_sig_chld);
2111 signal(SIGUSR1, on_sig_usr1);
2112 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
2113#endif
2114
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
2123 if ( realtime ) {
2124#ifdef DEBUG
2125  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compile without -DDEBUG");
2126#endif
2127
2128#ifdef ROAR_HAVE_NICE
2129  errno = 0;
2130  nice(-5*realtime); // -5 for each --realtime
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
2137/*
2138#ifdef __linux__
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));
2141#endif
2142*/
2143 }
2144
2145#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
2146 if ( setids & R_SETGID ) {
2147  if ( sock_grp == NULL ) {
2148   ROAR_ERR("Can not set GID if no groupname is supplied");
2149   return 1;
2150  }
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  }
2155  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
2156   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
2157  }
2158  if ( !grp || setgid(grp->gr_gid) == -1 ) {
2159   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
2160  }
2161 }
2162#endif
2163
2164
2165 clients_set_pid(g_self_client, getpid());
2166#ifdef ROAR_HAVE_GETUID
2167 clients_set_uid(g_self_client, getuid());
2168#endif
2169#ifdef ROAR_HAVE_GETGID
2170 clients_set_gid(g_self_client, getgid());
2171#endif
2172 clients_get(g_self_client, &self);
2173
2174 if ( self == NULL ) {
2175  ROAR_ERR("Can not get self client!");
2176  return 1;
2177 }
2178
2179 strcpy(self->name, "RoarAudio daemon internal");
2180
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
2190#ifdef ROAR_HAVE_FORK
2191 if ( daemon ) {
2192#ifdef ROAR_HAVE_SYSLOG
2193  roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
2194#else
2195  roar_debug_set_stderr_fh(-1);
2196#endif
2197
2198  close(ROAR_STDIN );
2199  close(ROAR_STDOUT);
2200  close(ROAR_STDERR);
2201
2202  if ( fork() )
2203   ROAR_U_EXIT(0);
2204
2205#ifdef ROAR_HAVE_SETSID
2206  setsid();
2207#endif
2208  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
2209 }
2210#endif
2211
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
2222#ifdef SUPPORT_PIDFILE
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  }
2230#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
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  }
2239#endif
2240 }
2241#endif
2242
2243#ifdef ROAR_HAVE_CHROOT
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 }
2254#endif
2255
2256#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2257 if ( setids & R_SETUID ) {
2258  if ( sock_user == NULL ) {
2259   ROAR_ERR("Can not set UID if no username is supplied");
2260   return 1;
2261  }
2262  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
2263   ROAR_ERR("Can not set UserID: %s", strerror(errno));
2264   return 3;
2265  }
2266#ifdef ROAR_HAVE_GETUID
2267  clients_set_uid(g_self_client, getuid());
2268#endif
2269 }
2270#endif
2271
2272 // Register with OpenSLP:
2273#ifdef ROAR_HAVE_LIBSLP
2274 if ( reg_slp ) {
2275  register_slp(0, sock_addr);
2276 }
2277#endif
2278
2279#ifdef ROAR_HAVE_LIBX11
2280 if ( reg_x11 ) {
2281  register_x11(0, sock_addr);
2282 }
2283#endif
2284
2285 ROAR_INFO("Startup complet", ROAR_DBG_INFO_INFO);
2286
2287 // start main loop...
2288 ROAR_INFO("Entering main loop", ROAR_DBG_INFO_INFO);
2289 main_loop(drvid, drvinst, &sa, sysclocksync);
2290 ROAR_INFO("Left main loop", ROAR_DBG_INFO_INFO);
2291
2292 // clean up.
2293 clean_quit_prep();
2294 driver_close(drvinst, drvid);
2295 output_buffer_free();
2296
2297 roar_notify_core_free(NULL);
2298
2299 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
2300 return 0;
2301}
2302
2303void cleanup_listen_socket (int terminate) {
2304 int i;
2305
2306 ROAR_DBG("cleanup_listen_socket(terminate=%i) = (void)?", terminate);
2307
2308 ROAR_INFO("Cleaning up listen sockets", ROAR_DBG_INFO_INFO);
2309
2310 // Deregister from SLP:
2311#ifdef ROAR_HAVE_LIBSLP
2312 register_slp(1, NULL);
2313#endif
2314
2315#ifdef ROAR_HAVE_LIBX11
2316 register_x11(1, NULL);
2317#endif
2318
2319#ifdef ROAR_SUPPORT_LISTEN
2320 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
2321  if ( g_listen[i].used  ) {
2322   roar_vio_close(&(g_listen[i].sock));
2323
2324   g_listen[i].used = 0;
2325
2326#ifdef ROAR_HAVE_UNIX
2327   if ( server[i] != NULL )
2328    if ( *(server[i]) == '/' )
2329     unlink(server[i]);
2330#endif
2331  }
2332 }
2333
2334#endif
2335
2336 if ( terminate )
2337  g_terminate = 1;
2338}
2339
2340void clean_quit_prep (void) {
2341 cleanup_listen_socket(0);
2342
2343 plugins_free();
2344
2345#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2346 sources_free();
2347#endif
2348 streams_free();
2349 clients_free();
2350#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2351 ssynth_free();
2352#endif
2353#ifndef ROAR_WITHOUT_DCOMP_CB
2354 midi_cb_stop(); // stop console beep
2355#endif
2356#ifndef ROAR_WITHOUT_DCOMP_MIDI
2357 midi_free();
2358#endif
2359#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2360 light_free();
2361#endif
2362#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2363 rdtcs_free();
2364#endif
2365
2366 waveform_free();
2367
2368#ifdef SUPPORT_PIDFILE
2369 if ( pidfile != NULL )
2370  unlink(pidfile);
2371#endif
2372}
2373
2374void clean_quit (void) {
2375 ROAR_INFO("Shuting down", ROAR_DBG_INFO_INFO);
2376
2377 counters_print(ROAR_DEBUG_TYPE_INFO, 0);
2378
2379 clean_quit_prep();
2380// driver_close(drvinst, drvid);
2381// output_buffer_free();
2382
2383 roar_notify_core_free(NULL);
2384
2385 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
2386 exit(0);
2387}
2388
2389//ll
Note: See TracBrowser for help on using the repository browser.