source: roaraudio/roard/roard.c @ 4049:802580eeb8a8

Last change on this file since 4049:802580eeb8a8 was 4049:802580eeb8a8, checked in by phi, 14 years ago

fixed security warnings

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