source: roaraudio/roard/roard.c @ 4225:e1cabba4bbc3

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

removed -d/-D/-dO from help

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