source: roaraudio/roard/roard.c @ 2104:4140ab350dda

Last change on this file since 2104:4140ab350dda was 2104:4140ab350dda, checked in by phi, 15 years ago

added pidfile support

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