source: roaraudio/roard/roard.c @ 4323:4313296dd84c

Last change on this file since 4323:4313296dd84c was 4323:4313296dd84c, checked in by phi, 14 years ago

init and free notify core

File size: 63.6 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 // init notify core:
1338 // TODO: reconsider number of lists.
1339 if ( roar_notify_core_new_global(-1) == -1 ) {
1340  ROAR_ERR("Can not init notify core!");
1341  return 1;
1342 }
1343
1344#ifdef ROAR_SUPPORT_LISTEN
1345 if ( init_listening() == -1 ) {
1346  ROAR_ERR("Can not init listening sockets!");
1347  return 1;
1348 }
1349#endif
1350
1351#ifndef ROAR_WITHOUT_DCOMP_MIDI
1352 if ( midi_init_config() == -1 ) {
1353  ROAR_ERR("Can not init MIDI config!");
1354  return 1;
1355 }
1356#endif
1357
1358#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1359 if ( ssynth_init_config() == -1 ) {
1360  ROAR_ERR("Can not init ssynth config!");
1361  return 1;
1362 }
1363#endif
1364
1365#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1366 if ( rdtcs_init_config() == -1 ) {
1367  ROAR_ERR("Can not init RDTCS config!");
1368  return 1;
1369 }
1370#endif
1371
1372 if ( plugins_preinit() == -1 ) {
1373  ROAR_ERR("Can not pre-init plugins!");
1374  return 1;
1375 }
1376
1377#ifdef ROAR_SUPPORT_LISTEN
1378#ifndef ROAR_TARGET_WIN32
1379 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
1380#else
1381 sock_addr = ROAR_DEFAULT_HOST;
1382#endif
1383
1384#ifdef ROAR_HAVE_GETUID
1385 if ( getuid() != 0 && getenv("HOME") != NULL ) {
1386/*
1387  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
1388*/
1389  roar_env_render_path_r(user_sock, sizeof(user_sock), "~/" ROAR_DEFAULT_SOCK_USER);
1390  sock_addr = user_sock;
1391  ROAR_DBG("main(*): setting sock_addr='%s'", sock_addr);
1392 }
1393#endif
1394
1395 if ( getenv("ROAR_SERVER") != NULL )
1396  sock_addr = getenv("ROAR_SERVER");
1397#endif
1398
1399 if ( clients_init() == -1 ) {
1400  ROAR_ERR("Can not init clients!");
1401  return 1;
1402 }
1403
1404 if ( streams_init() == -1 ) {
1405  ROAR_ERR("Can not init streams!");
1406  return 1;
1407 }
1408
1409 if ( (g_self_client = clients_new()) == -1 ) {
1410  ROAR_ERR("Can not create self client!");
1411  return 1;
1412 }
1413
1414#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1415 if ( sources_init() == -1 ) {
1416  ROAR_ERR("Can not init sources!");
1417  return 1;
1418 }
1419
1420 if ( (sources_set_client(g_self_client)) == -1 ) {
1421  ROAR_ERR("Can not init set source client!");
1422  return 1;
1423 }
1424#endif
1425
1426#ifdef ROAR_HAVE_MAIN_ARGS
1427 for (i = 1; i < argc; i++) {
1428  k = argv[i];
1429
1430  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
1431   usage();
1432   return 0;
1433
1434  } else if ( strcmp(k, "--start") == 0 ) {
1435   // this is a no op
1436  } else if ( strcmp(k, "--restart") == 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?), tring to continue anyway", sock_addr);
1440   }
1441#else
1442   ROAR_ERR("--restart not supported");
1443#endif
1444  } else if ( strcmp(k, "--shutdown") == 0 ) {
1445#ifdef ROAR_SUPPORT_LISTEN
1446   if ( restart_server(sock_addr, 1) == -1 ) {
1447    ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
1448    return 1;
1449   }
1450   return 0;
1451#else
1452   ROAR_ERR("--shutdown not supported");
1453   return 1;
1454#endif
1455  } else if ( strcmp(k, "--stop") == 0 ) {
1456#ifdef ROAR_SUPPORT_LISTEN
1457   if ( restart_server(sock_addr, 0) == -1 ) {
1458    ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
1459    return 1;
1460   }
1461   return 0;
1462#else
1463   ROAR_ERR("--stop not supported");
1464   return 1;
1465#endif
1466
1467
1468  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
1469#ifdef ROAR_HAVE_FORK
1470   daemon = 1;
1471#else
1472   ROAR_ERR("--daemon not supported");
1473#endif
1474  } else if ( strcmp(k, "--verbose") == 0 ) {
1475   g_verbose++;
1476  } else if ( strcmp(k, "--terminate") == 0 ) {
1477   g_terminate = 1;
1478  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1479   sysclocksync = 1000;
1480  } else if ( strcmp(k, "--realtime") == 0 ) {
1481   realtime++;
1482  } else if ( strcmp(k, "--memlock") == 0 ) {
1483   _CKHAVEARGS(1);
1484   g_config->memlock_level = memlock_str2level(argv[++i]);
1485  } else if ( strcmp(k, "--chroot") == 0 ) {
1486   _CKHAVEARGS(1);
1487#ifdef ROAR_HAVE_CHROOT
1488   chrootdir = argv[++i];
1489#else
1490   ROAR_ERR("--chroot not supported");
1491   i++;
1492#endif
1493  } else if ( strcmp(k, "--setgid") == 0 ) {
1494#ifdef ROAR_HAVE_SETGID
1495   setids |= R_SETGID;
1496#else
1497   ROAR_ERR("--setgid not supported");
1498#endif
1499  } else if ( strcmp(k, "--setuid") == 0 ) {
1500#ifdef ROAR_HAVE_SETUID
1501   setids |= R_SETUID;
1502#else
1503   ROAR_ERR("--setuid not supported");
1504#endif
1505  } else if ( strcmp(k, "--location") == 0 ) {
1506   _CKHAVEARGS(1);
1507   g_config->location = argv[++i];
1508  } else if ( strcmp(k, "--pidfile") == 0 ) {
1509   _CKHAVEARGS(1);
1510#ifdef SUPPORT_PIDFILE
1511   pidfile = argv[++i];
1512#else
1513   ROAR_ERR("--pidfile not supported");
1514   i++;
1515#endif
1516  } else if ( strcmp(k, "--log-syslog") == 0 ) {
1517#ifdef ROAR_HAVE_SYSLOG
1518   roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
1519#else
1520   ROAR_ERR("--log-syslog not supported");
1521#endif
1522
1523
1524  } else if ( strcmp(k, "--plugin-load") == 0 ) {
1525   _CKHAVEARGS(1);
1526   if ( plugins_load(argv[++i]) == -1 ) {
1527    ROAR_ERR("Can not load plugin");
1528   }
1529
1530  } else if ( strcmp(k, "--list-cf") == 0 ) {
1531   print_codecfilterlist();
1532   return 0;
1533
1534  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
1535   _CKHAVEARGS(1);
1536   sa.rate = atoi(argv[++i]);
1537  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
1538   _CKHAVEARGS(1);
1539   sa.bits = atoi(argv[++i]);
1540  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
1541   _CKHAVEARGS(1);
1542   sa.channels = atoi(argv[++i]);
1543
1544  } else if ( strcmp(k, "--aiprofile") == 0 ) {
1545   _CKHAVEARGS(1);
1546   if ( roar_profile2info(&sa, argv[++i]) == -1 ) {
1547    ROAR_ERR("Unknown audio profile: %s", argv[i]);
1548    return 1;
1549   }
1550   sa.codec    = ROAR_CODEC_DEFAULT;
1551
1552  } else if ( strcmp(k, "--stream-flags") == 0 ) {
1553   _CKHAVEARGS(1);
1554   if ( update_stream_flags(argv[++i]) == -1 ) {
1555    ROAR_ERR("Can not set stream flags");
1556    return 1;
1557   }
1558
1559  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
1560   _CKHAVEARGS(1);
1561   driver = argv[++i];
1562   if ( strcmp(driver, "list") == 0 ) {
1563    ROAR_WARN("The option is obsolete, use --list-driver!");
1564    print_driverlist();
1565    return 0;
1566   }
1567  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
1568   _CKHAVEARGS(1);
1569   device = argv[++i];
1570  } else if ( strcmp(k, "-dO") == 0 ) {
1571   _CKHAVEARGS(1);
1572   opts = argv[++i];
1573  } else if ( strcmp(k, "--list-driver") == 0 ) {
1574   print_driverlist();
1575   return 0;
1576
1577  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
1578   _CKHAVEARGS(1);
1579   o_drv  = argv[++i];
1580  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
1581   _CKHAVEARGS(1);
1582   o_dev  = argv[++i];
1583  } else if ( strcmp(k, "-oO") == 0 ) {
1584   _CKHAVEARGS(1);
1585   o_opts = argv[++i];
1586  } else if ( strcmp(k, "-oP") == 0 ) {
1587   o_prim = 1;
1588  } else if ( strcmp(k, "-oN") == 0 ) {
1589   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1590    o_count++;
1591
1592   o_drv  = o_dev = o_opts = NULL;
1593   o_prim = 0;
1594
1595  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
1596   _CKHAVEARGS(1);
1597#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1598   s_drv = argv[++i];
1599#else
1600   ROAR_ERR("main(*): No support for sources compiled in");
1601   i++;
1602#endif
1603  } else if ( strcmp(k, "-S") == 0 ) {
1604   _CKHAVEARGS(1);
1605#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1606   s_dev = argv[++i];
1607#else
1608   ROAR_ERR("main(*): No support for sources compiled in");
1609   i++;
1610#endif
1611  } else if ( strcmp(k, "-sO") == 0 ) {
1612   _CKHAVEARGS(1);
1613#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1614   s_opt = argv[++i];
1615#else
1616   ROAR_ERR("main(*): No support for sources compiled in");
1617   i++;
1618#endif
1619  } else if ( strcmp(k, "-sC") == 0 ) {
1620   _CKHAVEARGS(1);
1621#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1622   s_con = argv[++i];
1623#else
1624   ROAR_ERR("main(*): No support for sources compiled in");
1625   i++;
1626#endif
1627  } else if ( strcmp(k, "-sP") == 0 ) {
1628#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1629   s_prim = 1;
1630#else
1631   ROAR_ERR("main(*): No support for sources compiled in");
1632#endif
1633  } else if ( strcmp(k, "-sN") == 0 ) {
1634#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1635   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1636    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1637   }
1638   s_opt = s_dev = s_con = NULL;
1639   s_drv = "cf";
1640   s_prim = 0;
1641#else
1642   ROAR_ERR("main(*): No support for sources compiled in");
1643#endif
1644  } else if ( strcmp(k, "--list-sources") == 0 ) {
1645#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1646   print_sourcelist();
1647   return 0;
1648#else
1649   ROAR_ERR("main(*): No support for sources compiled in");
1650   return 1;
1651#endif
1652
1653  } else if ( strcmp(k, "--light-channels") == 0 ) {
1654   _CKHAVEARGS(1);
1655#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1656   light_channels = atoi(argv[++i]);
1657#else
1658   ROAR_WARN("main(*): no light subsystem compiled in");
1659   i++;
1660#endif
1661
1662  } else if ( strcmp(k, "--rds-pi") == 0 ) {
1663   _CKHAVEARGS(1);
1664#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1665   g_rdtcs.rds.pi = atoi(argv[++i]);
1666#else
1667   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1668   i++;
1669#endif
1670  } else if ( strcmp(k, "--rds-ps") == 0 ) {
1671   _CKHAVEARGS(1);
1672#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1673   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
1674    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", 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-pty") == 0 ) {
1682   _CKHAVEARGS(1);
1683#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1684   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
1685    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
1686    return 1;
1687   }
1688#else
1689   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1690   i++;
1691#endif
1692  } else if ( strcmp(k, "--rds-tp") == 0 ) {
1693#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1694   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) {
1695    ROAR_ERR("Can not set RDS TP flag");
1696    return 1;
1697   }
1698#else
1699   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1700#endif
1701  } else if ( strcmp(k, "--rds-ct") == 0 ) {
1702#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1703   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) {
1704    ROAR_ERR("Can not set RDS CT flag");
1705    return 1;
1706   }
1707#else
1708   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1709#endif
1710
1711
1712  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
1713#ifndef ROAR_WITHOUT_DCOMP_CB
1714   midi_config.init_cb = 0;
1715#else
1716   // no warning here as this is the disable option
1717#endif
1718  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
1719#ifndef ROAR_WITHOUT_DCOMP_CB
1720   midi_config.init_cb = 1;
1721#else
1722   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1723#endif
1724  } else if ( strcmp(k, "--midi-console") == 0 ) {
1725   _CKHAVEARGS(1);
1726#ifndef ROAR_WITHOUT_DCOMP_CB
1727   midi_config.console_dev = argv[++i];
1728   midi_config.init_cb = 1;
1729#else
1730   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1731   i++;
1732#endif
1733
1734  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
1735#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1736   ssynth_conf.enable = 1;
1737#else
1738   ROAR_ERR("main(*): No support for ssynth compiled in");
1739#endif
1740  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
1741#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
1742   ssynth_conf.enable = 0;
1743#else
1744   // we can safely ignore the disable
1745#endif
1746
1747  } else if ( strcmp(k, "--x11-display") == 0 || strcmp(k, "--display") == 0 ) {
1748   _CKHAVEARGS(1);
1749#ifdef ROAR_HAVE_LIBX11
1750   x11display = argv[++i];
1751#else
1752   ROAR_ERR("No X11 support compiled in!");
1753   return 1;
1754#endif
1755
1756
1757  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
1758   _CKHAVEARGS(1);
1759   // This is only useful in INET not UNIX mode.
1760#ifdef ROAR_SUPPORT_LISTEN
1761   if ( *sock_addr == '/' )
1762    sock_addr = ROAR_DEFAULT_HOST;
1763
1764   errno = 0;
1765   if ( (port = atoi(argv[++i])) < 1 ) {
1766#ifdef ROAR_HAVE_GETSERVBYNAME
1767    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
1768     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
1769     return 1;
1770    }
1771    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
1772    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
1773            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
1774    port = ROAR_NET2HOST16(serv->s_port);
1775#else
1776    ROAR_ERR("invalite port number: %s", argv[i]);
1777    return 1;
1778#endif
1779   }
1780#endif
1781  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
1782   _CKHAVEARGS(1);
1783#ifdef ROAR_SUPPORT_LISTEN
1784   sock_addr = argv[++i];
1785#else
1786   i++;
1787#endif
1788
1789  } else if ( strcmp(k, "--proto") == 0 ) {
1790#ifdef ROAR_SUPPORT_LISTEN
1791   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
1792    ROAR_ERR("Unknown protocol: %s", argv[i]);
1793    return 1;
1794   }
1795#endif
1796  } else if ( strcmp(k, "--proto-dir") == 0 ) {
1797   _CKHAVEARGS(1);
1798#ifdef ROAR_SUPPORT_LISTEN
1799   if ( (sock_dir = roar_str2dir(argv[++i])) == -1 ) {
1800    ROAR_ERR("Unknown stream direction: %s", argv[i]);
1801    return 1;
1802   }
1803#else
1804   i++;
1805#endif
1806  } else if ( strcmp(k, "--proto-rate") == 0 ) {
1807   _CKHAVEARGS(1);
1808#ifdef ROAR_SUPPORT_LISTEN
1809   sock_info.rate = atoi(argv[++i]);
1810#else
1811   i++;
1812#endif
1813  } else if ( strcmp(k, "--proto-bits") == 0 ) {
1814   _CKHAVEARGS(1);
1815#ifdef ROAR_SUPPORT_LISTEN
1816   sock_info.bits = atoi(argv[++i]);
1817#else
1818   i++;
1819#endif
1820  } else if ( strcmp(k, "--proto-chans") == 0 ) {
1821   _CKHAVEARGS(1);
1822#ifdef ROAR_SUPPORT_LISTEN
1823   sock_info.channels = atoi(argv[++i]);
1824#else
1825   i++;
1826#endif
1827  } else if ( strcmp(k, "--proto-codec") == 0 ) {
1828   _CKHAVEARGS(1);
1829#ifdef ROAR_SUPPORT_LISTEN
1830   if ( (sock_info.codec = roar_str2codec(argv[++i])) == -1 ) {
1831    ROAR_ERR("Unknown codec: %s", argv[i]);
1832    return 1;
1833   }
1834#else
1835   i++;
1836#endif
1837  } else if ( strcmp(k, "--proto-aiprofile") == 0 ) {
1838   _CKHAVEARGS(1);
1839#ifdef ROAR_SUPPORT_LISTEN
1840   if ( roar_profile2info(&sock_info, argv[++i]) == -1 ) {
1841    ROAR_ERR("Unknown audio profile: %s", argv[i]);
1842    return 1;
1843   }
1844#else
1845   i++;
1846#endif
1847  } else if ( strcmp(k, "--list-profiles") == 0 ) {
1848#ifdef ROAR_SUPPORT_LISTEN
1849   listen_listen_profiles();
1850   return 0;
1851#endif
1852  } else if ( strcmp(k, "--proto-profile") == 0 ) {
1853   _CKHAVEARGS(1);
1854#ifdef ROAR_SUPPORT_LISTEN
1855   if ( get_listen_profile(argv[++i], &port, &sock_addr, &sock_type, &sock_proto, &sock_dir, &sock_info) == -1 ) {
1856    ROAR_ERR("Unknown listen profile: %s", argv[i]);
1857    return 1;
1858   }
1859#else
1860   i++;
1861#endif
1862
1863
1864  } else if ( strcmp(k, "--list-proto") == 0 ) {
1865   list_proto();
1866   return 0;
1867
1868  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
1869#ifdef ROAR_SUPPORT_LISTEN
1870   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
1871    sock_type = ROAR_SOCKET_TYPE_TCP;
1872
1873   if ( *sock_addr == '/' )
1874    sock_addr = ROAR_DEFAULT_HOST;
1875#endif
1876
1877  } else if ( strcmp(k, "-4") == 0 ) {
1878#ifdef ROAR_SUPPORT_LISTEN
1879   sock_type = ROAR_SOCKET_TYPE_TCP;
1880   if ( *sock_addr == '/' )
1881    sock_addr = ROAR_DEFAULT_HOST;
1882#endif
1883  } else if ( strcmp(k, "-6") == 0 ) {
1884#ifdef ROAR_SUPPORT_LISTEN
1885#ifdef AF_INET6
1886   sock_type = ROAR_SOCKET_TYPE_TCP6;
1887   if ( *sock_addr == '/' )
1888    sock_addr = ROAR_DEFAULT_HOST;
1889#else
1890    ROAR_ERR("No IPv6 support compiled in!");
1891    return 1;
1892#endif
1893#endif
1894
1895  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
1896#ifdef ROAR_SUPPORT_LISTEN
1897   // ignore this case as it is the default behavor.
1898   sock_type = ROAR_SOCKET_TYPE_UNIX;
1899#endif
1900
1901  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
1902#ifdef ROAR_SUPPORT_LISTEN
1903#ifdef ROAR_HAVE_LIBDNET
1904    port   = ROAR_DEFAULT_NUM;
1905    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
1906    sock_addr = decnethost;
1907    sock_type = ROAR_SOCKET_TYPE_DECNET;
1908#else
1909    ROAR_ERR("No DECnet support compiled in!");
1910    return 1;
1911#endif
1912#endif
1913  } else if ( strcmp(k, "--new-sock") == 0 ) {
1914#ifdef ROAR_SUPPORT_LISTEN
1915   if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
1916    ROAR_ERR("Can not open listen socket!");
1917    return 1;
1918   }
1919#endif
1920
1921  } else if ( strcmp(k, "--slp") == 0 ) {
1922#ifdef ROAR_HAVE_LIBSLP
1923   reg_slp = 1;
1924#else
1925   ROAR_ERR("No OpenSLP support compiled in!");
1926   return 1;
1927#endif
1928
1929  } else if ( strcmp(k, "--x11") == 0 ) {
1930#ifdef ROAR_HAVE_LIBX11
1931   reg_x11 = 1;
1932#else
1933   ROAR_ERR("No X11 support compiled in!");
1934   return 1;
1935#endif
1936
1937
1938  } else if ( strcmp(k, "--jumbo-mtu") == 0 ) {
1939   _CKHAVEARGS(1);
1940   g_config->jumbo_mtu = atoi(argv[++i]);
1941
1942  } else if ( strcmp(k, "-G") == 0 ) {
1943   _CKHAVEARGS(1);
1944   sock_grp  = argv[++i];
1945  } else if ( strcmp(k, "-U") == 0 ) {
1946   _CKHAVEARGS(1);
1947   sock_user = argv[++i];
1948
1949  } else if ( strcmp(k, "--no-listen") == 0 ) {
1950#ifdef ROAR_SUPPORT_LISTEN
1951   sock_addr   = "";
1952   g_terminate = 1;
1953   g_no_listen = 1;
1954#endif
1955  } else if ( strcmp(k, "--client-fh") == 0 ) {
1956   _CKHAVEARGS(1);
1957   if ( clients_new_from_fh(atoi(argv[++i]), ROAR_PROTO_ROARAUDIO, ROAR_BYTEORDER_NETWORK, 1) == -1 ) {
1958    ROAR_ERR("main(*): Can not set client's fh");
1959    return 1;
1960   }
1961  } else if ( strcmp(k, "--close-fh") == 0 ) {
1962   _CKHAVEARGS(1);
1963#ifdef ROAR_HAVE_IO_POSIX
1964   close(atoi(argv[++i]));
1965#else
1966   i++;
1967   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
1968#endif
1969
1970  } else if ( strcmp(k, "--standby") == 0 ) {
1971   g_standby = 1;
1972  } else if ( strcmp(k, "--auto-standby") == 0 ) {
1973   g_autostandby = 1;
1974  } else {
1975   usage();
1976   return 1;
1977  }
1978
1979 }
1980#endif
1981
1982#ifndef ROAR_WITHOUT_DCOMP_SOURCES
1983 if ( s_dev != NULL ) {
1984  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1985   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1986  }
1987 }
1988#endif
1989
1990 add_default_output(o_drv, o_dev, o_opts, o_prim, o_count);
1991
1992 ROAR_INFO("Server config: rate=%i, bits=%i, chans=%i", ROAR_DBG_INFO_NOTICE, sa.rate, sa.bits, sa.channels);
1993
1994 if ( waveform_init() == -1 ) {
1995  ROAR_ERR("Can not initialize Waveform subsystem");
1996  return 1;
1997 }
1998
1999#ifndef ROAR_WITHOUT_DCOMP_MIDI
2000 if ( midi_init() == -1 ) {
2001  ROAR_ERR("Can not initialize MIDI subsystem");
2002 }
2003#endif
2004
2005#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2006 if ( ssynth_init() == -1 ) {
2007  ROAR_ERR("Can not initialize ssynth subsystem");
2008 }
2009#endif
2010
2011#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2012 if ( light_init(light_channels) == -1 ) {
2013  ROAR_ERR("Can not initialize light control subsystem");
2014 }
2015#endif
2016
2017#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2018 if ( rdtcs_init() == -1 ) {
2019  ROAR_ERR("Can not initialize RDTCS subsystem");
2020 }
2021#endif
2022
2023 if ( plugins_init() == -1 ) {
2024  ROAR_ERR("Can not initialize plugins");
2025 }
2026
2027#ifdef ROAR_SUPPORT_LISTEN
2028 if ( !g_no_listen ) {
2029  if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) {
2030   ROAR_ERR("Can not open listen socket!");
2031   return 1;
2032  }
2033 }
2034#endif
2035
2036 if ( output_buffer_init(&sa) == -1 ) {
2037  ROAR_ERR("Can not init output buffer!");
2038  return 1;
2039 }
2040
2041 if ( driver == NULL ) {
2042  driver = "null";
2043 } else {
2044  ROAR_ERR("Usage of old driver interface. use -o not -d!");
2045  ROAR_WARN("-d will be removed within the next releases");
2046 }
2047
2048 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
2049  ROAR_ERR("Can not open output driver!");
2050  return 1;
2051 }
2052
2053 if ( samples_init() == -1 ) {
2054  ROAR_ERR("Can not init samples!");
2055  return 1;
2056 }
2057
2058
2059 // we should handle this on microcontrollers, too.
2060#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
2061 signal(SIGINT,  on_sig_int);
2062 signal(SIGTERM, on_sig_term);
2063 signal(SIGCHLD, on_sig_chld);
2064 signal(SIGUSR1, on_sig_usr1);
2065 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
2066#endif
2067
2068 if ( g_config->memlock_level == -1 ) {
2069  g_config->memlock_level = MEMLOCK_DEFAULT;
2070 }
2071
2072 if ( memlock_set_level(g_config->memlock_level) == -1 ) {
2073  ROAR_WARN("Can not set memory locking level to target level.");
2074 }
2075
2076 if ( realtime ) {
2077#ifdef DEBUG
2078  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compile without -DDEBUG");
2079#endif
2080
2081#ifdef ROAR_HAVE_NICE
2082  errno = 0;
2083  nice(-5*realtime); // -5 for each --realtime
2084  if ( errno ) {
2085   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2086  }
2087#else
2088  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
2089#endif
2090/*
2091#ifdef __linux__
2092  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
2093   ROAR_WARN("Can not set io priority: %s", strerror(errno));
2094#endif
2095*/
2096 }
2097
2098#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
2099 if ( setids & R_SETGID ) {
2100  if ( sock_grp == NULL ) {
2101   ROAR_ERR("Can not set GID if no groupname is supplied");
2102   return 1;
2103  }
2104  if ( (grp = getgrnam(sock_grp)) == NULL ) {
2105   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
2106   return 1;
2107  }
2108  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
2109   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
2110  }
2111  if ( !grp || setgid(grp->gr_gid) == -1 ) {
2112   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
2113  }
2114 }
2115#endif
2116
2117
2118 clients_set_pid(g_self_client, getpid());
2119#ifdef ROAR_HAVE_GETUID
2120 clients_set_uid(g_self_client, getuid());
2121#endif
2122#ifdef ROAR_HAVE_GETGID
2123 clients_set_gid(g_self_client, getgid());
2124#endif
2125 clients_get(g_self_client, &self);
2126
2127 if ( self == NULL ) {
2128  ROAR_ERR("Can not get self client!");
2129  return 1;
2130 }
2131
2132 strcpy(self->name, "RoarAudio daemon internal");
2133
2134 if ( roar_nnode_free(&(self->nnode)) == -1 )
2135  return 1;
2136
2137 // not fully correct but ok as workaorund
2138 // so tools assume that roard runs on the same machine as
2139 // they in case they use AF_UNIX:
2140 if ( roar_nnode_new(&(self->nnode), ROAR_SOCKET_TYPE_UNIX) == -1 )
2141  return 1;
2142
2143#ifdef ROAR_HAVE_FORK
2144 if ( daemon ) {
2145#ifdef ROAR_HAVE_SYSLOG
2146  roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG);
2147#else
2148  roar_debug_set_stderr_fh(-1);
2149#endif
2150
2151  close(ROAR_STDIN );
2152  close(ROAR_STDOUT);
2153  close(ROAR_STDERR);
2154
2155  if ( fork() )
2156   ROAR_U_EXIT(0);
2157
2158#ifdef ROAR_HAVE_SETSID
2159  setsid();
2160#endif
2161  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
2162 }
2163#endif
2164
2165#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2166 // early test for UID as we need this for the pidfile and the setuid()
2167 if ( sock_user != NULL ) {
2168  if ( (pwd = getpwnam(sock_user)) == NULL ) {
2169   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
2170   return 1;
2171  }
2172 }
2173#endif
2174
2175#ifdef SUPPORT_PIDFILE
2176 if ( pidfile != NULL ) {
2177  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
2178   ROAR_ERR("Can not write pidfile: %s", pidfile);
2179  } else {
2180   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
2181   roar_vio_close(&pidfile_vio);
2182  }
2183#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2184  if ( pwd || grp ) {
2185   if ( chown(pidfile, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 ) {
2186    ROAR_WARN("Can not change ownership of pidfile: %s: %s", pidfile, strerror(errno));
2187   }
2188  }
2189  if ( chmod(pidfile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1 ) {
2190   ROAR_WARN("Can not change permissions of pidfile: %s: %s", pidfile, strerror(errno));
2191  }
2192#endif
2193 }
2194#endif
2195
2196#ifdef ROAR_HAVE_CHROOT
2197 if (chrootdir) {
2198  if ( chroot(chrootdir) == -1 ) {
2199   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
2200   return 2;
2201  }
2202  if ( chdir("/") == -1 ) {
2203   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
2204   return 2;
2205  }
2206 }
2207#endif
2208
2209#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
2210 if ( setids & R_SETUID ) {
2211  if ( sock_user == NULL ) {
2212   ROAR_ERR("Can not set UID if no username is supplied");
2213   return 1;
2214  }
2215  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
2216   ROAR_ERR("Can not set UserID: %s", strerror(errno));
2217   return 3;
2218  }
2219#ifdef ROAR_HAVE_GETUID
2220  clients_set_uid(g_self_client, getuid());
2221#endif
2222 }
2223#endif
2224
2225 // Register with OpenSLP:
2226#ifdef ROAR_HAVE_LIBSLP
2227 if ( reg_slp ) {
2228  register_slp(0, sock_addr);
2229 }
2230#endif
2231
2232#ifdef ROAR_HAVE_LIBX11
2233 if ( reg_x11 ) {
2234  register_x11(0, sock_addr);
2235 }
2236#endif
2237
2238 ROAR_INFO("Startup complet", ROAR_DBG_INFO_INFO);
2239
2240 // start main loop...
2241 ROAR_INFO("Entering main loop", ROAR_DBG_INFO_INFO);
2242 main_loop(drvid, drvinst, &sa, sysclocksync);
2243 ROAR_INFO("Left main loop", ROAR_DBG_INFO_INFO);
2244
2245 // clean up.
2246 clean_quit_prep();
2247 driver_close(drvinst, drvid);
2248 output_buffer_free();
2249
2250 roar_notify_core_free(NULL);
2251
2252 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
2253 return 0;
2254}
2255
2256void cleanup_listen_socket (int terminate) {
2257 int i;
2258
2259 ROAR_DBG("cleanup_listen_socket(terminate=%i) = (void)?", terminate);
2260
2261 ROAR_INFO("Cleaning up listen sockets", ROAR_DBG_INFO_INFO);
2262
2263 // Deregister from SLP:
2264#ifdef ROAR_HAVE_LIBSLP
2265 register_slp(1, NULL);
2266#endif
2267
2268#ifdef ROAR_HAVE_LIBX11
2269 register_x11(1, NULL);
2270#endif
2271
2272#ifdef ROAR_SUPPORT_LISTEN
2273 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
2274  if ( g_listen[i].used  ) {
2275   roar_vio_close(&(g_listen[i].sock));
2276
2277   g_listen[i].used = 0;
2278
2279#ifdef ROAR_HAVE_UNIX
2280   if ( server[i] != NULL )
2281    if ( *(server[i]) == '/' )
2282     unlink(server[i]);
2283#endif
2284  }
2285 }
2286
2287#endif
2288
2289 if ( terminate )
2290  g_terminate = 1;
2291}
2292
2293void clean_quit_prep (void) {
2294 cleanup_listen_socket(0);
2295
2296 plugins_free();
2297
2298#ifndef ROAR_WITHOUT_DCOMP_SOURCES
2299 sources_free();
2300#endif
2301 streams_free();
2302 clients_free();
2303#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
2304 ssynth_free();
2305#endif
2306#ifndef ROAR_WITHOUT_DCOMP_CB
2307 midi_cb_stop(); // stop console beep
2308#endif
2309#ifndef ROAR_WITHOUT_DCOMP_MIDI
2310 midi_free();
2311#endif
2312#ifndef ROAR_WITHOUT_DCOMP_LIGHT
2313 light_free();
2314#endif
2315#ifndef ROAR_WITHOUT_DCOMP_RDTCS
2316 rdtcs_free();
2317#endif
2318
2319 waveform_free();
2320
2321#ifdef SUPPORT_PIDFILE
2322 if ( pidfile != NULL )
2323  unlink(pidfile);
2324#endif
2325}
2326
2327void clean_quit (void) {
2328 ROAR_INFO("Shuting down", ROAR_DBG_INFO_INFO);
2329
2330 counters_print(ROAR_DEBUG_TYPE_INFO, 0);
2331
2332 clean_quit_prep();
2333// driver_close(drvinst, drvid);
2334// output_buffer_free();
2335
2336 roar_notify_core_free(NULL);
2337
2338 ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO);
2339 exit(0);
2340}
2341
2342//ll
Note: See TracBrowser for help on using the repository browser.