source: roaraudio/roard/roard.c @ 2087:30d3653999b9

Last change on this file since 2087:30d3653999b9 was 2087:30d3653999b9, checked in by phi, 15 years ago

added some debug outputs and got setting of quality for vorbis working :)

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