source: roaraudio/roard/roard.c @ 3990:63fe21d2d3e2

Last change on this file since 3990:63fe21d2d3e2 was 3990:63fe21d2d3e2, checked in by phi, 14 years ago

typo

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