source: roaraudio/roard/roard.c @ 3959:56b27f189c07

Last change on this file since 3959:56b27f189c07 was 3959:56b27f189c07, checked in by phi, 14 years ago

support for home-relative paths

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