source: roaraudio/roard/roard.c @ 1787:ed6218aaa6ba

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

set socket to "" in case of not listen instad of *server=0

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