source: roaraudio/roard/roard.c @ 3358:7f9d211148e0

Last change on this file since 3358:7f9d211148e0 was 3358:7f9d211148e0, checked in by phi, 14 years ago

updated (C) statements

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