source: roaraudio/roard/roard.c @ 3966:7076f996366a

Last change on this file since 3966:7076f996366a was 3966:7076f996366a, checked in by phi, 14 years ago

added rsound-unix profile

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