source: roaraudio/roard/roard.c @ 3956:3f62a7d00c0c

Last change on this file since 3956:3f62a7d00c0c was 3956:3f62a7d00c0c, checked in by phi, 14 years ago

added --proto-profile and --list-profiles

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