source: roaraudio/roard/roard.c @ 4140:b367d67b0343

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

begin of better auto detection of driver

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