source: roaraudio/roard/roard.c @ 4055:00addfba7e7f

Last change on this file since 4055:00addfba7e7f was 4055:00addfba7e7f, checked in by phi, 14 years ago

adding some more ROAR_INFO()s

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