source: roaraudio/roard/roard.c @ 2031:61c561130a8a

Last change on this file since 2031:61c561130a8a was 2028:e5bfbf9b184e, checked in by phi, 15 years ago

only use OpenSLP if it is present....

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