source: roaraudio/roard/roard.c @ 4101:42b7ee5d2f76

Last change on this file since 4101:42b7ee5d2f76 was 4101:42b7ee5d2f76, checked in by phi, 14 years ago

some work to support counters

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