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

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

typo

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