source: roaraudio/roard/roard.c @ 3992:d09e8cf44d30

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

only include profiles we support

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