source: roaraudio/roard/roard.c @ 1511:27353ecb0db8

Last change on this file since 1511:27353ecb0db8 was 1503:c0005aee16ff, checked in by phi, 15 years ago

added config var if we have args in main()

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