source: roaraudio/roard/roard.c @ 4058:fc24cc9422bc

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

use const, added descriptions

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