source: roaraudio/roard/roard.c @ 4052:c46fdb0f198a

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

make argument parsing more safe, error on wong number of arguments

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