source: roaraudio/roard/roard.c @ 1522:2ab4c1e3d956

Last change on this file since 1522:2ab4c1e3d956 was 1522:2ab4c1e3d956, checked in by phi, 15 years ago

added ROAR_VIO_CTL_SET_DBLKSIZE, added blocksize= stream option

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