source: roaraudio/roard/roard.c @ 1923:cd3072dcde5f

Last change on this file since 1923:cd3072dcde5f was 1923:cd3072dcde5f, checked in by phi, 15 years ago

added option --light-channels

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