source: roaraudio/roard/roard.c @ 4141:b36fa38ea35e

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

enable new default behavor

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