source: roaraudio/roard/roard.c @ 2489:266b1a6c9683

Last change on this file since 2489:266b1a6c9683 was 2489:266b1a6c9683, checked in by phi, 15 years ago

support to disable ssynth

File size: 36.4 KB
RevLine 
[0]1//roard.c:
2
[668]3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
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
[60]28char * server = ROAR_DEFAULT_SOCK_GLOBAL; // global server address
[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
[1503]36#ifdef ROAR_HAVE_MAIN_ARGS
[0]37void usage (void) {
38 printf("Usage: roard [OPTIONS]...\n\n");
39
[68]40 printf("Misc Options:\n\n");
41 printf(
[775]42        " --daemon              - Bring the server into background after init\n"
[71]43        " --terminate           - Terminate after last client quited\n"
[2109]44        " --start               - No op parameter (starting roard is default operation)\n"
[274]45        " --restart             - Trys to stop an old instance and start a new with new settings\n"
[2109]46        " --stop                - Stops a running roard (provide --pidfile!)\n"
47        " --shutdown            - Terminates a running roard (provide --pidfile!)\n"
[276]48        " --realtime            - Trys to get realtime priority,\n"
49        "                         give multible times for being more realtime\n"
[444]50        " --chroot DIR          - chroots to the given dir\n"
51        " --setgid              - GroupID to the audio group as specified via -G\n"
52        " --setuid              - UserID to the audio user as specified via -U\n"
[905]53        " --sysclocksync        - calculate exact sample rate using the system clock\n"
[2017]54        " --location  LOC       - Set lion readable location of server\n"
[2104]55#ifdef SUPPORT_PIDFILE
56        " --pidfile PIDFILE     - Write a pidfile at PIDFILE\n"
57#endif
[68]58       );
59
60 printf("\nAudio Options:\n\n");
[0]61 printf(
62        " -R  --rate   RATE     - Set server rate\n"
63        " -B  --bits   BITS     - Set server bits\n"
64        " -C  --chans  CHANNELS - Set server channels\n"
65       );
66
[2339]67 printf("\nStream Options:\n\n");
68 printf(
69        " --stream-flags D=F    - Set default flags for stream directions\n"
70        "                         D is the stream direction and F is a comma seperated\n"
71        "                         list of flags in form +flag or -flag to set or unset\n"
72        "                         a flag as default or remove it from the default\n"
73       );
74
[1923]75 printf("\nDriver Options: (obsolete, do not use, Use Ouput Options)\n\n");
[974]76 printf(" -d  --driver DRV      - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT);
[0]77 printf(" -D  --device DEV      - Set the device\n");
78 printf(" -dO OPTS              - Set output options\n");
[973]79 printf(" --list-driver         - List all drivers\n");
[0]80
[932]81 printf("\nOutput Options:\n\n");
[974]82 printf(" -o  --odriver DRV     - Set the driver, use '--list-driver' to get a list\n");
[932]83 printf(" -O  --odevice DEV     - Set the device\n");
84 printf(" -oO OPTS              - Set output options\n");
85 printf(" -oN                   - Adds another output\n");
[961]86 printf(" -oP                   - Mark output as primary\n");
[932]87
[2485]88#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]89 printf("\nSource Options:\n\n");
90 printf(" -s  --source DRV      - Use DRV as input driver\n"
91        " -S           DEV      - Use DEV as input device\n"
92        " -sO          OPTS     - Use OPTS as input options\n"
[1512]93        " -sN                   - Adds another source\n"
[0]94        " -sP                   - Make souce as primary\n"
95       );
[2270]96 printf(" --list-sources        - List all sources\n");
[2485]97#endif
[0]98
[280]99 printf("\nCodec Filter Options:\n\n");
100 printf(" --list-cf             - List all codec filter\n"
101       );
[0]102
[1923]103 printf("\nMIDI Options:\n\n");
[1924]104 printf(" --midi-no-console     - Disable console based MIDI synth\n"
[2444]105        " --midi-console-enable - Enables the console based MIDI synth\n"
[1924]106        " --midi-console DEV    - Set device for MIDI console\n"
[2489]107#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]108        " --ssynth-enable       - Enable simple software synth\n"
109        " --ssynth-disable      - Disable simple software synth\n"
[2489]110#endif
[1924]111       );
[1923]112
113 printf("\nLight Control Options:\n\n");
114 printf(" --light-channels NUM  - Sets the number of channels for Light control (default: %i)\n",
115                                  LIGHT_CHANNELS_DEFAULT
116       );
117
[0]118 printf("\nServer Options:\n\n");
119 printf(" -t  --tcp             - Use TCP listen socket\n"
120        " -u  --unix            - Use UNIX Domain listen socket (default)\n"
[508]121#ifdef ROAR_HAVE_LIBDNET
122        " -n  --decnet          - use DECnet listen socket\n"
123#endif
[518]124        " -4                    - Use IPv4 connections (implies -t)\n"
125#ifdef PF_INET6
126        " -6                    - Use IPv6 connections (implies -t)\n"
127#endif
128#ifdef IPV6_ADDRFORM
129        " -64                   - Try to downgrade sockets from IPv6 into IPv4,\n"
130        "                         this is normaly not usefull.\n"
131#endif
[0]132        " -p  --port            - TCP Port to bind to\n"
133        " -b  --bind            - IP/Hostname to bind to\n"
[1115]134        "     --sock            - Filename for UNIX Domain Socket\n"
[1993]135#ifdef ROAR_HAVE_LIBSLP
136        "     --slp             - Enable OpenSLP support\n"
137#endif
[450]138        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: %s)\n"
[60]139        "                         You need the permittions to change the GID\n"
[444]140        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
141        "                         You need the permittions to change the UID (normaly only root has)\n"
[548]142        " --no-listen           - Do not listen for new clients\n"
143        "                         (only usefull for relaing, impleys --terminate)\n"
[274]144        " --client-fh           - Comunicate with a client over this handle\n"
[501]145        "                         (only usefull for relaing)\n"
[920]146        " --close-fh            - Closes the given fh\n"
147        " --standby             - Start in standby state\n"
148        " --auto-standby        - Automatical goes into standby if there are no streams\n",
[450]149        ROAR_DEFAULT_SOCKGRP
[0]150       );
151// printf("\n Options:\n\n");
152 printf("\n");
153}
[1503]154#endif
[0]155
[2109]156int restart_server (char * server, int terminate) {
[579]157 struct roar_connection con;
[2111]158#ifdef ROAR_HAVE_KILL
159 char buf[80];
160 ssize_t l;
161 struct roar_vio_calls fh;
162 pid_t pid;
163 int ok;
164
165 if ( pidfile != NULL ) {
166  if ( roar_vio_open_file(&fh, pidfile, O_RDONLY, 0644) == -1 ) {
167   ROAR_WARN("restart_server(*): Can not read pidfile: %s", pidfile);
168  } else {
169   l = roar_vio_read(&fh, buf, 80);
170   roar_vio_close(&fh);
171   if ( l > 0 ) {
172    buf[l-1] = 0;
173    buf[79]  = 0;
174    pid = atoi(buf);
175    if ( terminate ) {
[2113]176     ok = kill(pid, SIGUSR1);
[2111]177    } else {
178     ok = kill(pid, SIGINT);
179    }
180    if ( ok == 0 ) {
181     return 0;
182    } else {
183     ROAR_WARN("restart_server(*): Can not kill roard by pidfile");
184    }
185   } else {
186    ROAR_WARN("restart_server(*): Can not find a PID in the pidfile");
187   }
188  }
189 }
190#endif
[2109]191
[579]192 if ( roar_connect(&con, server) == -1 ) {
193  return -1;
194 }
195
[2109]196 if ( roar_terminate(&con, terminate) == -1 ) {
[579]197  return -1;
198 }
199
200 return roar_disconnect(&con);
201}
202
[444]203#define R_SETUID 1
204#define R_SETGID 2
205
[1609]206int init_config (void) {
207 int i;
208
209 memset(g_config, 0, sizeof(struct roard_config));
210
211 for (i = 0; i < ROAR_DIR_DIRIDS; i++) {
212  g_config->streams[i].mixer_channels = 1;
213  g_config->streams[i].mixer.rpg_mul  = 1;
214  g_config->streams[i].mixer.rpg_div  = 1;
215  g_config->streams[i].mixer.scale    = 65535;
216  g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale;
217 }
218
[2291]219 g_config->streams[ROAR_DIR_PLAY    ].flags = ROAR_FLAG_META;
[2413]220 g_config->streams[ROAR_DIR_OUTPUT  ].flags = ROAR_FLAG_PASSMIXER;
[2264]221 g_config->streams[ROAR_DIR_FILTER  ].flags = ROAR_FLAG_SYNC;
[1899]222 g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC;
[2160]223 g_config->streams[ROAR_DIR_BIDIR   ].flags = ROAR_FLAG_ANTIECHO;
[1899]224
[2017]225 g_config->location = "***default***";
226
[1609]227 return 0;
228}
229
[2339]230int update_stream_flags (char * str) {
231 int    dir;
232 char * flags;
233 char * k;
234 int    op;
235 int    flag;
236
237 if ( (flags = strstr(str, "=")) == NULL )
238  return -1;
239
240 *flags = 0;
241  flags++;
242
243 if ( (dir = roar_str2dir(str)) == -1 )
244  return -1;
245
246 while (flags != NULL) {
247  k = flags;
248  flags = strstr(flags, ",");
249
250  if ( flags != NULL )
251   *(flags++) = 0;
252
253  switch (*k) {
[2340]254   case '+': k++; op = ROAR_SET_FLAG;   break;
255   case '-': k++; op = ROAR_RESET_FLAG; break;
[2339]256   default:
[2340]257     op = ROAR_SET_FLAG;
[2339]258  }
259
260  flag = 0;
261
262  if ( !strcmp(k, "sync") ) {
263   flag = ROAR_FLAG_SYNC;
264  } else if ( !strcmp(k, "meta") ) {
265   flag = ROAR_FLAG_META;
266  } else if ( !strcmp(k, "cleanmeta") ) {
267   flag = ROAR_FLAG_CLEANMETA;
268  } else if ( !strcmp(k, "pause") ) {
269   flag = ROAR_FLAG_PAUSE;
270  } else if ( !strcmp(k, "mute") ) {
271   flag = ROAR_FLAG_MUTE;
272  } else if ( !strcmp(k, "antiecho") ) {
273   flag = ROAR_FLAG_ANTIECHO;
[2413]274  } else if ( !strcmp(k, "passmixer") ) {
275   flag = ROAR_FLAG_PASSMIXER;
[2339]276  } else {
277   return -1;
278  }
279
280  g_config->streams[dir].flags |= flag;
281
[2340]282  if ( op == ROAR_RESET_FLAG )
[2339]283   g_config->streams[dir].flags -= flag;
284 }
285
286 return 0;
287}
288
[1145]289int add_output (char * drv, char * dev, char * opts, int prim, int count) {
[933]290 int stream;
291 struct roar_stream * s;
292 struct roar_stream_server * ss;
[938]293 char * k, * v;
[1208]294#ifdef ROAR_DRIVER_CODEC
295 char * to_free = NULL;
296#endif
[938]297 int codec;
[1926]298 int sync = 0, f_mmap = 0;
[1522]299 int32_t blocks = -1, blocksize = -1;
[1920]300 int dir = ROAR_DIR_OUTPUT;
[1991]301 int error = 0;
302 // DMX:
303 int32_t channel  = -1;
304 int32_t universe = -1;
305 uint16_t tu16;
[2085]306 float q = -32e6;
[933]307
[938]308 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
[933]309
[1145]310 if ( drv == NULL && count == 0 ) {
311  drv  = ROAR_DRIVER_DEFAULT;
312  prim = 1;
313  sync = 1;
[1208]314
315#ifdef ROAR_DRIVER_CODEC
316  if ( opts == NULL ) {
317   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
318  }
319#endif
[1145]320 }
321
[1227]322 if ( opts == NULL && count == 0 ) {
323  sync = 1;
324  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
325 }
326
[933]327 if ( (stream = streams_new()) == -1 ) {
[938]328  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]329  if ( prim ) alive = 0;
[933]330  return -1;
331 }
332
333 streams_get(stream, &ss);
334 s = ROAR_STREAM(ss);
335
336 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
337
338 s->pos_rel_id = -1;
339// s->info.codec = codec;
340
[938]341 codec = s->info.codec;
342
343 k = strtok(opts, ",");
344 while (k != NULL) {
345//  ROAR_WARN("add_output(*): opts: %s", k);
346
347  if ( (v = strstr(k, "=")) != NULL ) {
348   *v++ = 0;
349  }
350
351  ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v);
352  if ( strcmp(k, "rate") == 0 ) {
353   s->info.rate = atoi(v);
354  } else if ( strcmp(k, "channels") == 0 ) {
355   s->info.channels = atoi(v);
356  } else if ( strcmp(k, "bits") == 0 ) {
357   s->info.bits = atoi(v);
358  } else if ( strcmp(k, "codec") == 0 ) {
359   if ( (codec = roar_str2codec(v)) == -1 ) {
360    ROAR_ERR("add_output(*): unknown codec '%s'", v);
[1991]361    error++;
[938]362   }
[2085]363  } else if ( strcmp(k, "q") == 0 ) {
364   q = atof(v);
[1221]365  } else if ( strcmp(k, "blocks") == 0 ) {
366   blocks = atoi(v);
[1522]367  } else if ( strcmp(k, "blocksize") == 0 ) {
368   blocksize = atoi(v);
[1926]369  } else if ( strcmp(k, "mmap") == 0 ) {
370   f_mmap = 1;
[1919]371  } else if ( strcmp(k, "subsystem") == 0 ) {
372   if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) {
[1920]373    dir = ROAR_DIR_OUTPUT;
[1919]374   } else if ( !strcasecmp(v, "midi") ) {
375    dir = ROAR_DIR_MIDI_OUT;
376   } else if ( !strcasecmp(v, "light") ) {
377    dir = ROAR_DIR_LIGHT_OUT;
[2234]378   } else if ( !strcasecmp(v, "raw") ) {
379    dir = ROAR_DIR_RAW_OUT;
[1919]380   } else {
381    ROAR_ERR("add_output(*): unknown subsystem '%s'", k);
[1991]382    error++;
383   }
384  // DMX:
385  } else if ( strcmp(k, "channel") == 0 ) {
386   channel  = atoi(v);
387   if ( channel < 0 || channel > 65535 ) {
388    ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel);
389    channel = -1;
390    error++;
[1919]391   }
[1991]392  } else if ( strcmp(k, "universe") == 0 ) {
393   universe = atoi(v);
394   if ( universe < 0 || universe > 255 ) {
395    ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe);
396    universe = -1;
397    error++;
398   }
399
[2115]400  } else if ( strcmp(k, "name") == 0 ) {
401   if ( streams_set_name(stream, v) == -1 ) {
402    ROAR_ERR("add_output(*): Can not set Stream name");
403    error++;
404   }
[1991]405
[1032]406  } else if ( strcmp(k, "meta") == 0 ) {
407   streams_set_flag(stream, ROAR_FLAG_META);
[1117]408  } else if ( strcmp(k, "sync") == 0 ) {
409   sync = 1;
[1221]410  } else if ( strcmp(k, "primary") == 0 ) {
411   prim = 1;
412
413  } else if ( strcmp(k, "cleanmeta") == 0 ) {
414   streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
415  } else if ( strcmp(k, "autoconf") == 0 ) {
[1531]416   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
[2413]417  } else if ( strcmp(k, "recsource") == 0 ) {
418   streams_set_flag(stream, ROAR_FLAG_RECSOURCE);
419  } else if ( strcmp(k, "passmixer") == 0 ) {
420   streams_set_flag(stream, ROAR_FLAG_PASSMIXER);
[938]421  } else {
422   ROAR_ERR("add_output(*): unknown option '%s'", k);
[1991]423   error++;
424  }
425
426  if ( error ) {
[938]427   streams_delete(stream);
[982]428   if ( prim ) alive = 0;
[1208]429#ifdef ROAR_DRIVER_CODEC
430   if ( to_free != NULL )
431    free(to_free);
432#endif
[938]433   return -1;
434  }
435
436  k = strtok(NULL, ",");
437 }
438
[1919]439 if ( streams_set_dir(stream, dir, 1) == -1 ) {
440  streams_delete(stream);
441  return -1;
442 }
443
[1208]444#ifdef ROAR_DRIVER_CODEC
445 if ( to_free != NULL )
446  free(to_free);
447#endif
448
[941]449 if ( codec == ROAR_CODEC_ALAW || codec == ROAR_CODEC_MULAW )
450  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
451
[938]452 s->info.codec = codec;
453 ROAR_STREAM_SERVER(s)->codec_orgi = codec;
[933]454
[2364]455 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) {
[1581]456  ss->driver_id = -1; // don't close a driver not opened...
457  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
[933]458  streams_delete(stream);
[938]459  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]460  if ( prim ) alive = 0;
[933]461  return -1;
462 }
463
[1528]464 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here
465 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM,   s); // ignore errors here
466
[1221]467 if ( blocks != -1 )
468  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
469
[1522]470 if ( blocksize != -1 )
471  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
472
[1991]473 // TODO: we shoudld *really* check for errors here...
474 if ( channel != -1 ) {
475  tu16 = channel;
476  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16);
477 }
478 if ( universe != -1 ) {
479  tu16 = universe;
480  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16);
481 }
482
[1156]483 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
484
[938]485 streams_set_fh(stream, -1); // update some internal structures
486
[2087]487 if ( q > -1e6 ) {
488  ROAR_DBG("add_output(*): setting q=%f", q);
489  streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q);
490 }
491
[933]492 client_stream_add(g_source_client, stream);
493
[1200]494 if ( prim ) {
[961]495  streams_mark_primary(stream);
[1200]496  s->pos_rel_id = stream;
497 }
[961]498
[1120]499 if ( sync ) {
[1117]500  streams_set_flag(stream, ROAR_FLAG_SYNC);
[1120]501 } else {
502  streams_reset_flag(stream, ROAR_FLAG_SYNC);
503 }
[1117]504
[1926]505 if ( f_mmap )
506  streams_set_flag(stream, ROAR_FLAG_MMAP);
507
[933]508 return 0;
[932]509}
510
[1993]511
512// SLP:
513void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
514 /* return the error code in the cookie */
515 *(SLPError*)cookie = errcode;
516}
517
518int register_slp (int unreg, char * sockname) {
519#ifdef ROAR_HAVE_LIBSLP
520 static int regged = 0;
521 static char * sn = NULL;
522 SLPError err;
523 SLPError callbackerr;
524 SLPHandle hslp;
525 char addr[1024];
526 char attr[1024] = "";
[2017]527 char * location;
[1993]528
529 if ( sockname != NULL )
530  sn = sockname;
531
[2016]532 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE "://%s", sn);
[1993]533
534 err = SLPOpen("en", SLP_FALSE, &hslp);
535
536 if (err != SLP_OK) {
537  ROAR_ERR("Error opening slp handle: Error #%i", err);
538  return -1;
539 }
540
541 if (!unreg) {
[2017]542
543  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
544   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
545   SLPClose(hslp);
546   return -1;
547  }
548
[1995]549  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
[2017]550                               "(light-channels=%i),(location=%s)",
[1995]551           g_sa->rate, g_sa->channels, g_sa->bits,
[2017]552           g_light_state.channels,
553           location
[1995]554          );
555
[1993]556  /* Register a service with SLP */
557  err = SLPReg(hslp,
558               addr,
559               SLP_LIFETIME_MAXIMUM,
560               0,
561               attr,
562               SLP_TRUE,
563               register_slp_callback,
564               &callbackerr);
[1994]565  regged = 1;
566 } else if ( unreg && regged ) {
567  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
568  regged = 0;
569 } else {
570  SLPClose(hslp);
571  return -1;
572 }
[1993]573
[1994]574 /* err may contain an error code that occurred as the slp library    */
575 /* _prepared_ to make the call.                                     */
576 if ( err != SLP_OK ) {
577  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
578  return -1;
579 }
580
581 /* callbackerr may contain an error code (that was assigned through */
582 /* the callback cookie) that occurred as slp packets were sent on    */
583 /* the wire */
584 if (callbackerr != SLP_OK) {
585  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
586  return -1;
[1993]587 }
588
589 SLPClose(hslp);
590 return 0;
591#else
592 return -1;
593#endif
594}
595
596
597// MAIN:
598
[1503]599#ifdef ROAR_HAVE_MAIN_ARGS
[0]600int main (int argc, char * argv[]) {
[1503]601#else
602int main (void) {
603#endif
604#ifdef ROAR_HAVE_MAIN_ARGS
[0]605 int i;
606 char * k;
[1503]607#endif
[1494]608#ifdef ROAR_SUPPORT_LISTEN
[905]609 char user_sock[80]  = {0};
[1494]610#endif
[1609]611 struct roar_audio_info sa, max_sa;
612 struct roard_config config;
[1486]613#ifdef ROAR_HAVE_FORK
[905]614 int    daemon       = 0;
[1486]615#endif
[905]616 int    realtime     = 0;
617 int    sysclocksync = 0;
[1207]618 char * driver    = NULL;
619 char * device    = NULL;
[1503]620#ifdef ROAR_HAVE_MAIN_ARGS
[1207]621 char * opts      = NULL;
[1503]622#endif
[60]623// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
[1494]624#ifdef ROAR_SUPPORT_LISTEN
[1207]625 int      port    = ROAR_DEFAULT_PORT;
[1494]626#endif
[0]627 int               drvid;
[2486]628#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]629 char * s_drv     = "cf";
[1110]630 char * s_dev     = NULL;
[444]631 char * s_con     = NULL;
632 char * s_opt     = NULL;
633 int    s_prim    = 0;
[2486]634#endif
[1207]635 char * o_drv     = getenv("ROAR_DRIVER");
636 char * o_dev     = getenv("ROAR_DEVICE");
[932]637 char * o_opts    = NULL;
[961]638 int    o_prim    = 0;
[1145]639 int    o_count   = 0;
[1923]640 int    light_channels = LIGHT_CHANNELS_DEFAULT;
[450]641 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
[444]642 char * sock_user = NULL;
[1494]643#ifdef ROAR_SUPPORT_LISTEN
[517]644 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
[1494]645#endif
[1993]646#ifdef ROAR_HAVE_LIBSLP
647 int    reg_slp   = 0;
648#endif
[1486]649#ifdef ROAR_HAVE_CHROOT
[444]650 char * chrootdir = NULL;
[1486]651#endif
652#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
[444]653 int    setids    = 0;
[1486]654#endif
655#ifdef ROAR_HAVE_UNIX
[1011]656 char * env_roar_proxy_backup;
[1486]657#endif
658#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[446]659 struct group   * grp  = NULL;
[1486]660#endif
661#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[446]662 struct passwd  * pwd  = NULL;
[1486]663#endif
664#ifdef ROAR_HAVE_GETSERVBYNAME
[446]665 struct servent * serv = NULL;
[1486]666#endif
[0]667 DRIVER_USERDATA_T drvinst;
[39]668 struct roar_client * self = NULL;
[508]669#ifdef ROAR_HAVE_LIBDNET
670 char decnethost[80];
671#endif
[2104]672#ifdef SUPPORT_PIDFILE
673 struct roar_vio_calls pidfile_vio;
674#endif
[0]675
676 g_standby       =  0;
[920]677 g_autostandby   =  0;
[982]678 alive           =  1;
[1494]679#ifdef ROAR_SUPPORT_LISTEN
[1155]680 g_no_listen     =  0;
[1494]681 g_listen_socket = -1;
682#else
683 g_terminate     =  1;
684#endif
[0]685
686 sa.bits     = ROAR_BITS_DEFAULT;
687 sa.channels = ROAR_CHANNELS_DEFAULT;
688 sa.rate     = ROAR_RATE_DEFAULT;
689 sa.codec    = ROAR_CODEC_DEFAULT;
690
[1609]691 g_sa        = &sa;
692 g_max_sa    = &max_sa;
693
694 memcpy(g_max_sa, g_sa, sizeof(max_sa));
695
696 g_config = &config;
697
698 if ( init_config() == -1 ) {
699  ROAR_ERR("Can not init default config!");
700  return 1;
701 }
[0]702
[1924]703 if ( midi_init_config() == -1 ) {
704  ROAR_ERR("Can not init MIDI config!");
705  return 1;
706 }
[60]707
[2489]708#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]709 if ( ssynth_init_config() == -1 ) {
710  ROAR_ERR("Can not init ssynth config!");
711  return 1;
712 }
[2489]713#endif
[2451]714
[1494]715#ifdef ROAR_SUPPORT_LISTEN
[1753]716#ifdef ROAR_HAVE_GETUID
[1486]717 if ( getuid() != 0 && getenv("HOME") != NULL ) {
718  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
[60]719  server = user_sock;
720 }
[1753]721#endif
[60]722
[279]723 if ( getenv("ROAR_SERVER") != NULL )
724  server = getenv("ROAR_SERVER");
[1494]725#endif
[279]726
[63]727 if ( clients_init() == -1 ) {
728  ROAR_ERR("Can not init clients!");
729  return 1;
730 }
731
732 if ( streams_init() == -1 ) {
733  ROAR_ERR("Can not init streams!");
734  return 1;
735 }
736
[64]737 if ( (g_self_client = clients_new()) == -1 ) {
738  ROAR_ERR("Can not create self client!");
739  return 1;
740 }
741
[2485]742#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]743 if ( sources_init() == -1 ) {
744  ROAR_ERR("Can not init sources!");
745  return 1;
746 }
747
[64]748 if ( (sources_set_client(g_self_client)) == -1 ) {
749  ROAR_ERR("Can not init set source client!");
750  return 1;
751 }
[2485]752#endif
[64]753
[1503]754#ifdef ROAR_HAVE_MAIN_ARGS
[0]755 for (i = 1; i < argc; i++) {
756  k = argv[i];
757
758  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
759   usage();
760   return 0;
761
[2109]762  } else if ( strcmp(k, "--start") == 0 ) {
763   // this is a no op
[579]764  } else if ( strcmp(k, "--restart") == 0 ) {
[1494]765#ifdef ROAR_SUPPORT_LISTEN
[2109]766   if ( restart_server(server, 1) == -1 ) {
[579]767    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server);
768   }
[1494]769#else
770   ROAR_ERR("--restart not supported");
771#endif
[2109]772  } else if ( strcmp(k, "--shutdown") == 0 ) {
773#ifdef ROAR_SUPPORT_LISTEN
774   if ( restart_server(server, 1) == -1 ) {
775    ROAR_WARN("Can not terminate old server (not running at %s?)", server);
776    return 1;
777   }
778   return 0;
779#else
780   ROAR_ERR("--shutdown not supported");
781   return 1;
782#endif
783  } else if ( strcmp(k, "--stop") == 0 ) {
784#ifdef ROAR_SUPPORT_LISTEN
[2111]785   if ( restart_server(server, 0) == -1 ) {
[2109]786    ROAR_WARN("Can not stop old server (not running at %s?)", server);
787    return 1;
788   }
789   return 0;
790#else
791   ROAR_ERR("--stop not supported");
792   return 1;
793#endif
794
[579]795
[775]796  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
[1486]797#ifdef ROAR_HAVE_FORK
[775]798   daemon = 1;
[1486]799#else
800   ROAR_ERR("--daemon not supported");
801#endif
[71]802  } else if ( strcmp(k, "--terminate") == 0 ) {
803   g_terminate = 1;
[905]804  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
805   sysclocksync = 1000;
[275]806  } else if ( strcmp(k, "--realtime") == 0 ) {
[276]807   realtime++;
[444]808  } else if ( strcmp(k, "--chroot") == 0 ) {
[1486]809#ifdef ROAR_HAVE_CHROOT
[444]810   chrootdir = argv[++i];
[1486]811#else
812   ROAR_ERR("--chroot not supported");
813   i++;
814#endif
[444]815  } else if ( strcmp(k, "--setgid") == 0 ) {
[1486]816#ifdef ROAR_HAVE_SETGID
[444]817   setids |= R_SETGID;
[1486]818#else
819   ROAR_ERR("--setgid not supported");
820#endif
[444]821  } else if ( strcmp(k, "--setuid") == 0 ) {
[1486]822#ifdef ROAR_HAVE_SETUID
[444]823   setids |= R_SETUID;
[1486]824#else
825   ROAR_ERR("--setuid not supported");
826#endif
[2017]827  } else if ( strcmp(k, "--location") == 0 ) {
828   g_config->location = argv[++i];
[2104]829  } else if ( strcmp(k, "--pidfile") == 0 ) {
830#ifdef SUPPORT_PIDFILE
831   pidfile = argv[++i];
832#else
833   ROAR_ERR("--pidfile not supported");
834   i++;
835#endif
[68]836
[280]837  } else if ( strcmp(k, "--list-cf") == 0 ) {
838   print_codecfilterlist();
839   return 0;
840
[0]841  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
842   sa.rate = atoi(argv[++i]);
843  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
844   sa.bits = atoi(argv[++i]);
845  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
846   sa.channels = atoi(argv[++i]);
847
[2339]848  } else if ( strcmp(k, "--stream-flags") == 0 ) {
849   if ( update_stream_flags(argv[++i]) == -1 ) {
850    ROAR_ERR("Can not set stream flags");
851    return 1;
852   }
853
[0]854  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
855   driver = argv[++i];
856   if ( strcmp(driver, "list") == 0 ) {
[974]857    ROAR_WARN("The option is obsolete, use --list-driver!");
[0]858    print_driverlist();
[973]859    return 0;
[0]860   }
861  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
862   device = argv[++i];
863  } else if ( strcmp(k, "-dO") == 0 ) {
864   opts = argv[++i];
[973]865  } else if ( strcmp(k, "--list-driver") == 0 ) {
866   print_driverlist();
867   return 0;
[0]868
[932]869  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
870   o_drv  = argv[++i];
871  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
872   o_dev  = argv[++i];
873  } else if ( strcmp(k, "-oO") == 0 ) {
874   o_opts = argv[++i];
[961]875  } else if ( strcmp(k, "-oP") == 0 ) {
876   o_prim = 1;
[932]877  } else if ( strcmp(k, "-oN") == 0 ) {
[1145]878   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
879    o_count++;
880
[961]881   o_drv  = o_dev = o_opts = NULL;
882   o_prim = 0;
[932]883
[0]884  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
[2485]885#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]886   s_drv = argv[++i];
[2485]887#else
888   ROAR_ERR("main(*): No support for sources compiled in");
889#endif
[550]890  } else if ( strcmp(k, "-S") == 0 ) {
[2485]891#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]892   s_dev = argv[++i];
[2485]893#else
894   ROAR_ERR("main(*): No support for sources compiled in");
895#endif
[0]896  } else if ( strcmp(k, "-sO") == 0 ) {
[2485]897#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]898   s_opt = argv[++i];
[2485]899#else
900   ROAR_ERR("main(*): No support for sources compiled in");
901#endif
[0]902  } else if ( strcmp(k, "-sC") == 0 ) {
[2485]903#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]904   s_con = argv[++i];
[2485]905#else
906   ROAR_ERR("main(*): No support for sources compiled in");
907#endif
[0]908  } else if ( strcmp(k, "-sP") == 0 ) {
[2485]909#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]910   s_prim = 1;
[2485]911#else
912   ROAR_ERR("main(*): No support for sources compiled in");
913#endif
[1110]914  } else if ( strcmp(k, "-sN") == 0 ) {
[2485]915#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]916   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
917    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
918   }
919   s_opt = s_dev = s_con = NULL;
920   s_drv = "cf";
921   s_prim = 0;
[2485]922#else
923   ROAR_ERR("main(*): No support for sources compiled in");
924#endif
[2270]925  } else if ( strcmp(k, "--list-sources") == 0 ) {
[2485]926#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[2270]927   print_sourcelist();
928   return 0;
[2485]929#else
930   ROAR_ERR("main(*): No support for sources compiled in");
931#endif
[0]932
[1923]933  } else if ( strcmp(k, "--light-channels") == 0 ) {
934   light_channels = atoi(argv[++i]);
935
[1924]936  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
[2487]937#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]938   midi_config.init_cb = 0;
[2487]939#else
940   // no warning here as this is the disable option
941#endif
[2444]942  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
[2487]943#ifndef ROAR_WITHOUT_DCOMP_CB
[2444]944   midi_config.init_cb = 1;
[2487]945#else
946   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
947#endif
[1924]948  } else if ( strcmp(k, "--midi-console") == 0 ) {
[2487]949#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]950   midi_config.console_dev = argv[++i];
[2444]951   midi_config.init_cb = 1;
[2487]952#else
953   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
954#endif
[1924]955
[2451]956  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
[2489]957#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]958   ssynth_conf.enable = 1;
[2489]959#else
960   ROAR_ERR("main(*): No support for ssynth compiled in");
961#endif
[2451]962  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
[2489]963#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]964   ssynth_conf.enable = 0;
[2489]965#else
966   // we can safely ignore the disable
967#endif
[2451]968
[0]969  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
[447]970   // This is only usefull in INET not UNIX mode.
[1494]971#ifdef ROAR_SUPPORT_LISTEN
[447]972   if ( *server == '/' )
973    server = ROAR_DEFAULT_HOST;
974
[446]975   errno = 0;
976   if ( (port = atoi(argv[++i])) < 1 ) {
[1486]977#ifdef ROAR_HAVE_GETSERVBYNAME
[446]978    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
979     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
980     return 1;
981    }
982    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
983    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
984            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
985    port = ROAR_NET2HOST16(serv->s_port);
[1486]986#else
987    ROAR_ERR("invalite port number: %s", argv[i]);
988    return 1;
989#endif
[446]990   }
[1494]991#endif
[1115]992  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
[1494]993#ifdef ROAR_SUPPORT_LISTEN
[0]994   server = argv[++i];
[1494]995#endif
[518]996
[573]997  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
[1494]998#ifdef ROAR_SUPPORT_LISTEN
[518]999   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
1000    sock_type = ROAR_SOCKET_TYPE_TCP;
1001
1002   if ( *server == '/' )
1003    server = ROAR_DEFAULT_HOST;
[1494]1004#endif
[518]1005
1006  } else if ( strcmp(k, "-4") == 0 ) {
[1494]1007#ifdef ROAR_SUPPORT_LISTEN
[517]1008   sock_type = ROAR_SOCKET_TYPE_TCP;
[447]1009   if ( *server == '/' )
1010    server = ROAR_DEFAULT_HOST;
[1494]1011#endif
[518]1012  } else if ( strcmp(k, "-6") == 0 ) {
[1494]1013#ifdef ROAR_SUPPORT_LISTEN
[519]1014#ifdef PF_INET6
[518]1015   sock_type = ROAR_SOCKET_TYPE_TCP6;
1016   if ( *server == '/' )
1017    server = ROAR_DEFAULT_HOST;
[519]1018#else
1019    ROAR_ERR("No IPv6 support compiled in!");
1020    return 1;
1021#endif
[1494]1022#endif
[518]1023
[573]1024  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
[1494]1025#ifdef ROAR_SUPPORT_LISTEN
[62]1026   // ignore this case as it is the default behavor.
[517]1027   sock_type = ROAR_SOCKET_TYPE_UNIX;
[1494]1028#endif
[518]1029
[573]1030  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
[1494]1031#ifdef ROAR_SUPPORT_LISTEN
[508]1032#ifdef ROAR_HAVE_LIBDNET
1033    port   = ROAR_DEFAULT_NUM;
1034    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
1035    server = decnethost;
[517]1036    sock_type = ROAR_SOCKET_TYPE_DECNET;
[508]1037#else
1038    ROAR_ERR("No DECnet support compiled in!");
1039    return 1;
1040#endif
[1494]1041#endif
[518]1042
[1993]1043  } else if ( strcmp(k, "--slp") == 0 ) {
1044#ifdef ROAR_HAVE_LIBSLP
1045   reg_slp = 1;
1046#else
1047    ROAR_ERR("No OpenSLP support compiled in!");
1048    return 1;
1049#endif
1050
[60]1051  } else if ( strcmp(k, "-G") == 0 ) {
[444]1052   sock_grp  = argv[++i];
1053  } else if ( strcmp(k, "-U") == 0 ) {
1054   sock_user = argv[++i];
[0]1055
[68]1056  } else if ( strcmp(k, "--no-listen") == 0 ) {
[1494]1057#ifdef ROAR_SUPPORT_LISTEN
[1787]1058   server      = "";
[548]1059   g_terminate = 1;
[1155]1060   g_no_listen = 1;
[1494]1061#endif
[68]1062  } else if ( strcmp(k, "--client-fh") == 0 ) {
1063   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
1064    ROAR_ERR("main(*): Can not set client's fh");
1065    return 1;
1066   }
[501]1067  } else if ( strcmp(k, "--close-fh") == 0 ) {
[1486]1068#ifdef ROAR_HAVE_IO_POSIX
[501]1069   close(atoi(argv[++i]));
[1486]1070#else
1071   i++;
1072   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
1073#endif
[68]1074
[920]1075  } else if ( strcmp(k, "--standby") == 0 ) {
1076   g_standby = 1;
1077  } else if ( strcmp(k, "--auto-standby") == 0 ) {
1078   g_autostandby = 1;
[0]1079  } else {
1080   usage();
1081   return 1;
1082  }
1083
1084 }
[1503]1085#endif
[0]1086
[2485]1087#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1088 if ( s_dev != NULL ) {
1089  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1090   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1091  }
1092 }
[2485]1093#endif
[1110]1094
[1145]1095 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
[932]1096
[0]1097 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
1098
[1819]1099 if ( midi_init() == -1 ) {
[281]1100  ROAR_ERR("Can not initialize MIDI subsystem");
[1819]1101 }
1102
[2489]1103#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1104 if ( ssynth_init() == -1 ) {
1105  ROAR_ERR("Can not initialize ssynth subsystem");
1106 }
[2489]1107#endif
[2451]1108
[1819]1109 if ( light_init(light_channels) == -1 ) {
1110  ROAR_ERR("Can not initialize light control subsystem");
1111 }
[281]1112
[1494]1113#ifdef ROAR_SUPPORT_LISTEN
[68]1114 if ( *server != 0 ) {
[517]1115  if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
[1486]1116#ifdef ROAR_HAVE_UNIX
[286]1117   if ( *server == '/' ) {
[1011]1118    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
1119     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
1120     unsetenv("ROAR_PROXY");
1121    }
[286]1122    if ( (i = roar_socket_connect(server, port)) != -1 ) {
1123     close(i);
1124     ROAR_ERR("Can not open listen socket!");
1125     return 1;
1126    } else {
1127     unlink(server);
[517]1128     if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
[286]1129      ROAR_ERR("Can not open listen socket!");
1130      return 1;
1131     }
1132    }
[1011]1133    if ( env_roar_proxy_backup != NULL ) {
1134     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
1135     free(env_roar_proxy_backup);
1136    }
[1486]1137#else
1138   if (0) { // noop
1139#endif
[286]1140   } else {
1141    ROAR_ERR("Can not open listen socket!");
1142    return 1;
1143   }
[68]1144  }
[0]1145
[1486]1146#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[523]1147  if ( (grp = getgrnam(sock_grp)) == NULL ) {
1148   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
1149  }
[1486]1150#endif
1151#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[523]1152  if ( sock_user || (setids & R_SETUID) ) {
1153   if ( (pwd = getpwnam(sock_user)) == NULL ) {
1154    ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
1155   }
1156  }
[1486]1157#endif
[523]1158
[1516]1159#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
[68]1160  if ( *server == '/' ) {
[523]1161   if ( grp ) {
[444]1162    if ( pwd ) {
[1691]1163     if ( chown(server, pwd->pw_uid, grp->gr_gid) == -1 )
1164      return 1;
[444]1165    } else {
[1691]1166     if ( chown(server, -1, grp->gr_gid) == -1 )
1167      return 1;
[444]1168    }
[1753]1169#ifdef ROAR_HAVE_GETUID
[68]1170    if ( getuid() == 0 )
[1691]1171     if ( chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
1172      return 1;
[1753]1173#endif
[68]1174   }
[60]1175  }
[1486]1176#endif
[60]1177 }
[1494]1178#endif
[60]1179
[0]1180 if ( output_buffer_init(&sa) == -1 ) {
1181  ROAR_ERR("Can not init output buffer!");
1182  return 1;
1183 }
1184
[1145]1185 if ( driver == NULL ) {
1186  driver = "null";
1187 } else {
1188  ROAR_WARN("Usage of old driver interface. use -o not -d!");
1189 }
1190
[0]1191 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
1192  ROAR_ERR("Can not open output driver!");
1193  return 1;
1194 }
1195
[44]1196 if ( samples_init() == -1 ) {
1197  ROAR_ERR("Can not init samples!");
1198  return 1;
1199 }
1200
1201
[1486]1202 // we should handle this on microcontrollers, too.
[1753]1203#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
[0]1204 signal(SIGINT,  on_sig_int);
[285]1205 signal(SIGCHLD, on_sig_chld);
[2112]1206 signal(SIGUSR1, on_sig_usr1);
[0]1207 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
[1486]1208#endif
[0]1209
[275]1210 if ( realtime ) {
[278]1211#ifdef DEBUG
1212  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
1213#endif
1214
[1486]1215#ifdef ROAR_HAVE_NICE
[275]1216  errno = 0;
[276]1217  nice(-5*realtime); // -5 for each --realtime
[1486]1218  if ( errno ) {
1219   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1220  }
1221#else
1222  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1223#endif
[277]1224/*
[276]1225#ifdef __linux__
[277]1226  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
1227   ROAR_WARN("Can not set io priority: %s", strerror(errno));
[276]1228#endif
[277]1229*/
[275]1230 }
1231
[1486]1232#ifdef ROAR_HAVE_SETGID
[444]1233 if ( setids & R_SETGID ) {
1234  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
1235   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
1236  }
[523]1237  if ( !grp || setgid(grp->gr_gid) == -1 ) {
[444]1238   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
1239  }
1240 }
[1486]1241#endif
[444]1242
[0]1243
[39]1244 clients_set_pid(g_self_client, getpid());
[1753]1245#ifdef ROAR_HAVE_GETUID
[440]1246 clients_set_uid(g_self_client, getuid());
[1753]1247#endif
1248#ifdef ROAR_HAVE_GETGID
[440]1249 clients_set_gid(g_self_client, getgid());
[1753]1250#endif
[39]1251 clients_get(g_self_client, &self);
[37]1252
[39]1253 if ( self == NULL ) {
1254  ROAR_ERR("Can not get self client!");
1255  return 1;
1256 }
1257
[775]1258 strcpy(self->name, "RoarAudio daemon internal");
[68]1259
[1486]1260#ifdef ROAR_HAVE_FORK
[775]1261 if ( daemon ) {
[68]1262  close(ROAR_STDIN );
1263  close(ROAR_STDOUT);
1264  close(ROAR_STDERR);
[1753]1265
[68]1266  if ( fork() )
[1486]1267   ROAR_U_EXIT(0);
[1753]1268
1269#ifdef ROAR_HAVE_SETSID
1270  setsid();
1271#endif
[1046]1272  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
[68]1273 }
[1486]1274#endif
[68]1275
[2104]1276#ifdef SUPPORT_PIDFILE
[2106]1277 if ( pidfile != NULL ) {
1278  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
1279   ROAR_ERR("Can not write pidfile: %s", pidfile);
1280  } else {
1281   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
1282   roar_vio_close(&pidfile_vio);
1283  }
[2104]1284 }
1285#endif
1286
[1486]1287#ifdef ROAR_HAVE_CHROOT
[444]1288 if (chrootdir) {
1289  if ( chroot(chrootdir) == -1 ) {
1290   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
1291   return 2;
1292  }
1293  if ( chdir("/") == -1 ) {
1294   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
1295   return 2;
1296  }
1297 }
[1486]1298#endif
[444]1299
[1486]1300#ifdef ROAR_HAVE_SETUID
[444]1301 if ( setids & R_SETUID ) {
1302  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
1303   ROAR_ERR("Can not set UserID: %s", strerror(errno));
1304   return 3;
1305  }
[1753]1306#ifdef ROAR_HAVE_GETUID
[444]1307  clients_set_uid(g_self_client, getuid());
[1753]1308#endif
[444]1309 }
[1486]1310#endif
[444]1311
[1993]1312 // Register with OpenSLP:
[2028]1313#ifdef ROAR_HAVE_LIBSLP
[1993]1314 if ( reg_slp ) {
1315  register_slp(0, server);
1316 }
[2028]1317#endif
[1993]1318
[0]1319 // start main loop...
[905]1320 main_loop(drvid, drvinst, &sa, sysclocksync);
[0]1321
1322 // clean up.
1323 clean_quit_prep();
1324 driver_close(drvinst, drvid);
1325 output_buffer_free();
1326
1327 return 0;
1328}
1329
[574]1330void cleanup_listen_socket (int terminate) {
[1993]1331 // Deregister from SLP:
[2028]1332#ifdef ROAR_HAVE_LIBSLP
[1993]1333 register_slp(1, NULL);
[2028]1334#endif
[580]1335
[1494]1336#ifdef ROAR_SUPPORT_LISTEN
[580]1337 if ( g_listen_socket != -1 ) {
[1486]1338#ifdef ROAR_HAVE_IO_POSIX
[580]1339  close(g_listen_socket);
[1486]1340#endif // #else is useless because we are in void context.
[60]1341
[580]1342  g_listen_socket = -1;
[576]1343
[1486]1344#ifdef ROAR_HAVE_UNIX
[580]1345  if ( *server == '/' )
1346   unlink(server);
[1486]1347#endif
[580]1348 }
[60]1349
[1494]1350#endif
1351
[574]1352 if ( terminate )
1353  g_terminate = 1;
1354}
1355
1356void clean_quit_prep (void) {
1357 cleanup_listen_socket(0);
[60]1358
[2485]1359#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1360 sources_free();
[2485]1361#endif
[0]1362 streams_free();
1363 clients_free();
[2489]1364#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1365 ssynth_free();
[2489]1366#endif
[2487]1367#ifndef ROAR_WITHOUT_DCOMP_CB
[282]1368 midi_cb_stop(); // stop console beep
[2487]1369#endif
[281]1370 midi_free();
[1819]1371 light_free();
[2104]1372
1373#ifdef SUPPORT_PIDFILE
1374 if ( pidfile != NULL )
1375  unlink(pidfile);
1376#endif
[0]1377}
1378
1379void clean_quit (void) {
1380 clean_quit_prep();
1381// driver_close(drvinst, drvid);
1382// output_buffer_free();
1383 exit(0);
1384}
1385
1386//ll
Note: See TracBrowser for help on using the repository browser.