source: roaraudio/roard/roard.c @ 1919:ab6c76b94351

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

make it possible to set subsystem (dir)

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