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
Line 
1//roard.c:
2
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
25#include "roard.h"
26
27#ifdef ROAR_SUPPORT_LISTEN
28char * server = ROAR_DEFAULT_SOCK_GLOBAL; // global server address
29#endif
30
31#ifdef ROAR_HAVE_MAIN_ARGS
32void usage (void) {
33 printf("Usage: roard [OPTIONS]...\n\n");
34
35 printf("Misc Options:\n\n");
36 printf(
37        " --daemon              - Bring the server into background after init\n"
38        " --terminate           - Terminate after last client quited\n"
39        " --restart             - Trys to stop an old instance and start a new with new settings\n"
40        " --realtime            - Trys to get realtime priority,\n"
41        "                         give multible times for being more realtime\n"
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"
45        " --sysclocksync        - calculate exact sample rate using the system clock\n"
46       );
47
48 printf("\nAudio Options:\n\n");
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");
56 printf(" -d  --driver DRV      - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT);
57 printf(" -D  --device DEV      - Set the device\n");
58 printf(" -dO OPTS              - Set output options\n");
59 printf(" --list-driver         - List all drivers\n");
60
61 printf("\nOutput Options:\n\n");
62 printf(" -o  --odriver DRV     - Set the driver, use '--list-driver' to get a list\n");
63 printf(" -O  --odevice DEV     - Set the device\n");
64 printf(" -oO OPTS              - Set output options\n");
65 printf(" -oN                   - Adds another output\n");
66 printf(" -oP                   - Mark output as primary\n");
67
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        " -sN                   - Adds another source\n"
73        " -sP                   - Make souce as primary\n"
74       );
75
76 printf("\nCodec Filter Options:\n\n");
77 printf(" --list-cf             - List all codec filter\n"
78       );
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"
83#ifdef ROAR_HAVE_LIBDNET
84        " -n  --decnet          - use DECnet listen socket\n"
85#endif
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
94        " -p  --port            - TCP Port to bind to\n"
95        " -b  --bind            - IP/Hostname to bind to\n"
96        "     --sock            - Filename for UNIX Domain Socket\n"
97        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: %s)\n"
98        "                         You need the permittions to change the GID\n"
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"
101        " --no-listen           - Do not listen for new clients\n"
102        "                         (only usefull for relaing, impleys --terminate)\n"
103        " --client-fh           - Comunicate with a client over this handle\n"
104        "                         (only usefull for relaing)\n"
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",
108        ROAR_DEFAULT_SOCKGRP
109       );
110// printf("\n Options:\n\n");
111 printf("\n");
112}
113#endif
114
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
128#define R_SETUID 1
129#define R_SETGID 2
130
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 g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC;
145
146 return 0;
147}
148
149int add_output (char * drv, char * dev, char * opts, int prim, int count) {
150 int stream;
151 struct roar_stream * s;
152 struct roar_stream_server * ss;
153 char * k, * v;
154#ifdef ROAR_DRIVER_CODEC
155 char * to_free = NULL;
156#endif
157 int codec;
158 int sync = 0;
159 int32_t blocks = -1, blocksize = -1;
160 int dir = OAR_DIR_OUTPUT;
161
162 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
163
164 if ( drv == NULL && count == 0 ) {
165  drv  = ROAR_DRIVER_DEFAULT;
166  prim = 1;
167  sync = 1;
168
169#ifdef ROAR_DRIVER_CODEC
170  if ( opts == NULL ) {
171   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
172  }
173#endif
174 }
175
176 if ( opts == NULL && count == 0 ) {
177  sync = 1;
178  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
179 }
180
181 if ( (stream = streams_new()) == -1 ) {
182  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
183  if ( prim ) alive = 0;
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
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);
216    if ( prim ) alive = 0;
217#ifdef ROAR_DRIVER_CODEC
218    if ( to_free != NULL )
219     free(to_free);
220#endif
221    return -1;
222   }
223  } else if ( strcmp(k, "blocks") == 0 ) {
224   blocks = atoi(v);
225  } else if ( strcmp(k, "blocksize") == 0 ) {
226   blocksize = atoi(v);
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   }
244  } else if ( strcmp(k, "meta") == 0 ) {
245   streams_set_flag(stream, ROAR_FLAG_META);
246  } else if ( strcmp(k, "sync") == 0 ) {
247   sync = 1;
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 ) {
254   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
255  } else {
256   ROAR_ERR("add_output(*): unknown option '%s'", k);
257   streams_delete(stream);
258   if ( prim ) alive = 0;
259#ifdef ROAR_DRIVER_CODEC
260   if ( to_free != NULL )
261    free(to_free);
262#endif
263   return -1;
264  }
265
266  k = strtok(NULL, ",");
267 }
268
269 if ( streams_set_dir(stream, dir, 1) == -1 ) {
270  streams_delete(stream);
271  return -1;
272 }
273
274#ifdef ROAR_DRIVER_CODEC
275 if ( to_free != NULL )
276  free(to_free);
277#endif
278
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
282 s->info.codec = codec;
283 ROAR_STREAM_SERVER(s)->codec_orgi = codec;
284
285 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1) == -1 ) {
286  ss->driver_id = -1; // don't close a driver not opened...
287  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
288  streams_delete(stream);
289  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
290  if ( prim ) alive = 0;
291  return -1;
292 }
293
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
297 if ( blocks != -1 )
298  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
299
300 if ( blocksize != -1 )
301  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
302
303 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
304
305 streams_set_fh(stream, -1); // update some internal structures
306
307 client_stream_add(g_source_client, stream);
308
309 if ( prim ) {
310  streams_mark_primary(stream);
311  s->pos_rel_id = stream;
312 }
313
314 if ( sync ) {
315  streams_set_flag(stream, ROAR_FLAG_SYNC);
316 } else {
317  streams_reset_flag(stream, ROAR_FLAG_SYNC);
318 }
319
320 return 0;
321}
322
323#ifdef ROAR_HAVE_MAIN_ARGS
324int main (int argc, char * argv[]) {
325#else
326int main (void) {
327#endif
328#ifdef ROAR_HAVE_MAIN_ARGS
329 int i;
330 char * k;
331#endif
332#ifdef ROAR_SUPPORT_LISTEN
333 char user_sock[80]  = {0};
334#endif
335 struct roar_audio_info sa, max_sa;
336 struct roard_config config;
337#ifdef ROAR_HAVE_FORK
338 int    daemon       = 0;
339#endif
340 int    realtime     = 0;
341 int    sysclocksync = 0;
342 char * driver    = NULL;
343 char * device    = NULL;
344#ifdef ROAR_HAVE_MAIN_ARGS
345 char * opts      = NULL;
346#endif
347// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
348#ifdef ROAR_SUPPORT_LISTEN
349 int      port    = ROAR_DEFAULT_PORT;
350#endif
351 int               drvid;
352 char * s_drv     = "cf";
353 char * s_dev     = NULL;
354 char * s_con     = NULL;
355 char * s_opt     = NULL;
356 int    s_prim    = 0;
357 char * o_drv     = getenv("ROAR_DRIVER");
358 char * o_dev     = getenv("ROAR_DEVICE");
359 char * o_opts    = NULL;
360 int    o_prim    = 0;
361 int    o_count   = 0;
362 int    light_channels = 512;
363 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
364 char * sock_user = NULL;
365#ifdef ROAR_SUPPORT_LISTEN
366 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
367#endif
368#ifdef ROAR_HAVE_CHROOT
369 char * chrootdir = NULL;
370#endif
371#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
372 int    setids    = 0;
373#endif
374#ifdef ROAR_HAVE_UNIX
375 char * env_roar_proxy_backup;
376#endif
377#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
378 struct group   * grp  = NULL;
379#endif
380#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
381 struct passwd  * pwd  = NULL;
382#endif
383#ifdef ROAR_HAVE_GETSERVBYNAME
384 struct servent * serv = NULL;
385#endif
386 DRIVER_USERDATA_T drvinst;
387 struct roar_client * self = NULL;
388#ifdef ROAR_HAVE_LIBDNET
389 char decnethost[80];
390#endif
391
392 g_standby       =  0;
393 g_autostandby   =  0;
394 alive           =  1;
395#ifdef ROAR_SUPPORT_LISTEN
396 g_no_listen     =  0;
397 g_listen_socket = -1;
398#else
399 g_terminate     =  1;
400#endif
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
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 }
418
419
420#ifdef ROAR_SUPPORT_LISTEN
421#ifdef ROAR_HAVE_GETUID
422 if ( getuid() != 0 && getenv("HOME") != NULL ) {
423  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
424  server = user_sock;
425 }
426#endif
427
428 if ( getenv("ROAR_SERVER") != NULL )
429  server = getenv("ROAR_SERVER");
430#endif
431
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
442 if ( (g_self_client = clients_new()) == -1 ) {
443  ROAR_ERR("Can not create self client!");
444  return 1;
445 }
446
447 if ( sources_init() == -1 ) {
448  ROAR_ERR("Can not init sources!");
449  return 1;
450 }
451
452 if ( (sources_set_client(g_self_client)) == -1 ) {
453  ROAR_ERR("Can not init set source client!");
454  return 1;
455 }
456
457#ifdef ROAR_HAVE_MAIN_ARGS
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
465  } else if ( strcmp(k, "--restart") == 0 ) {
466#ifdef ROAR_SUPPORT_LISTEN
467   if ( restart_server(server) == -1 ) {
468    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server);
469   }
470#else
471   ROAR_ERR("--restart not supported");
472#endif
473
474  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
475#ifdef ROAR_HAVE_FORK
476   daemon = 1;
477#else
478   ROAR_ERR("--daemon not supported");
479#endif
480  } else if ( strcmp(k, "--terminate") == 0 ) {
481   g_terminate = 1;
482  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
483   sysclocksync = 1000;
484  } else if ( strcmp(k, "--realtime") == 0 ) {
485   realtime++;
486  } else if ( strcmp(k, "--chroot") == 0 ) {
487#ifdef ROAR_HAVE_CHROOT
488   chrootdir = argv[++i];
489#else
490   ROAR_ERR("--chroot not supported");
491   i++;
492#endif
493  } else if ( strcmp(k, "--setgid") == 0 ) {
494#ifdef ROAR_HAVE_SETGID
495   setids |= R_SETGID;
496#else
497   ROAR_ERR("--setgid not supported");
498#endif
499  } else if ( strcmp(k, "--setuid") == 0 ) {
500#ifdef ROAR_HAVE_SETUID
501   setids |= R_SETUID;
502#else
503   ROAR_ERR("--setuid not supported");
504#endif
505
506  } else if ( strcmp(k, "--list-cf") == 0 ) {
507   print_codecfilterlist();
508   return 0;
509
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 ) {
520    ROAR_WARN("The option is obsolete, use --list-driver!");
521    print_driverlist();
522    return 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];
528  } else if ( strcmp(k, "--list-driver") == 0 ) {
529   print_driverlist();
530   return 0;
531
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];
538  } else if ( strcmp(k, "-oP") == 0 ) {
539   o_prim = 1;
540  } else if ( strcmp(k, "-oN") == 0 ) {
541   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
542    o_count++;
543
544   o_drv  = o_dev = o_opts = NULL;
545   o_prim = 0;
546
547  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
548   s_drv = argv[++i];
549  } else if ( strcmp(k, "-S") == 0 ) {
550   s_dev = argv[++i];
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;
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;
564
565  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
566   // This is only usefull in INET not UNIX mode.
567#ifdef ROAR_SUPPORT_LISTEN
568   if ( *server == '/' )
569    server = ROAR_DEFAULT_HOST;
570
571   errno = 0;
572   if ( (port = atoi(argv[++i])) < 1 ) {
573#ifdef ROAR_HAVE_GETSERVBYNAME
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);
582#else
583    ROAR_ERR("invalite port number: %s", argv[i]);
584    return 1;
585#endif
586   }
587#endif
588  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
589#ifdef ROAR_SUPPORT_LISTEN
590   server = argv[++i];
591#endif
592
593  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
594#ifdef ROAR_SUPPORT_LISTEN
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;
600#endif
601
602  } else if ( strcmp(k, "-4") == 0 ) {
603#ifdef ROAR_SUPPORT_LISTEN
604   sock_type = ROAR_SOCKET_TYPE_TCP;
605   if ( *server == '/' )
606    server = ROAR_DEFAULT_HOST;
607#endif
608  } else if ( strcmp(k, "-6") == 0 ) {
609#ifdef ROAR_SUPPORT_LISTEN
610#ifdef PF_INET6
611   sock_type = ROAR_SOCKET_TYPE_TCP6;
612   if ( *server == '/' )
613    server = ROAR_DEFAULT_HOST;
614#else
615    ROAR_ERR("No IPv6 support compiled in!");
616    return 1;
617#endif
618#endif
619
620  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
621#ifdef ROAR_SUPPORT_LISTEN
622   // ignore this case as it is the default behavor.
623   sock_type = ROAR_SOCKET_TYPE_UNIX;
624#endif
625
626  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
627#ifdef ROAR_SUPPORT_LISTEN
628#ifdef ROAR_HAVE_LIBDNET
629    port   = ROAR_DEFAULT_NUM;
630    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
631    server = decnethost;
632    sock_type = ROAR_SOCKET_TYPE_DECNET;
633#else
634    ROAR_ERR("No DECnet support compiled in!");
635    return 1;
636#endif
637#endif
638
639  } else if ( strcmp(k, "-G") == 0 ) {
640   sock_grp  = argv[++i];
641  } else if ( strcmp(k, "-U") == 0 ) {
642   sock_user = argv[++i];
643
644  } else if ( strcmp(k, "--no-listen") == 0 ) {
645#ifdef ROAR_SUPPORT_LISTEN
646   server      = "";
647   g_terminate = 1;
648   g_no_listen = 1;
649#endif
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   }
655  } else if ( strcmp(k, "--close-fh") == 0 ) {
656#ifdef ROAR_HAVE_IO_POSIX
657   close(atoi(argv[++i]));
658#else
659   i++;
660   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
661#endif
662
663  } else if ( strcmp(k, "--standby") == 0 ) {
664   g_standby = 1;
665  } else if ( strcmp(k, "--auto-standby") == 0 ) {
666   g_autostandby = 1;
667  } else {
668   usage();
669   return 1;
670  }
671
672 }
673#endif
674
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
681 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
682
683 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
684
685 if ( midi_init() == -1 ) {
686  ROAR_ERR("Can not initialize MIDI subsystem");
687 }
688
689 if ( light_init(light_channels) == -1 ) {
690  ROAR_ERR("Can not initialize light control subsystem");
691 }
692
693#ifdef ROAR_SUPPORT_LISTEN
694 if ( *server != 0 ) {
695  if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
696#ifdef ROAR_HAVE_UNIX
697   if ( *server == '/' ) {
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    }
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);
708     if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
709      ROAR_ERR("Can not open listen socket!");
710      return 1;
711     }
712    }
713    if ( env_roar_proxy_backup != NULL ) {
714     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
715     free(env_roar_proxy_backup);
716    }
717#else
718   if (0) { // noop
719#endif
720   } else {
721    ROAR_ERR("Can not open listen socket!");
722    return 1;
723   }
724  }
725
726#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
727  if ( (grp = getgrnam(sock_grp)) == NULL ) {
728   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
729  }
730#endif
731#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
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  }
737#endif
738
739#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
740  if ( *server == '/' ) {
741   if ( grp ) {
742    if ( pwd ) {
743     if ( chown(server, pwd->pw_uid, grp->gr_gid) == -1 )
744      return 1;
745    } else {
746     if ( chown(server, -1, grp->gr_gid) == -1 )
747      return 1;
748    }
749#ifdef ROAR_HAVE_GETUID
750    if ( getuid() == 0 )
751     if ( chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
752      return 1;
753#endif
754   }
755  }
756#endif
757 }
758#endif
759
760 if ( output_buffer_init(&sa) == -1 ) {
761  ROAR_ERR("Can not init output buffer!");
762  return 1;
763 }
764
765 if ( driver == NULL ) {
766  driver = "null";
767 } else {
768  ROAR_WARN("Usage of old driver interface. use -o not -d!");
769 }
770
771 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
772  ROAR_ERR("Can not open output driver!");
773  return 1;
774 }
775
776 if ( samples_init() == -1 ) {
777  ROAR_ERR("Can not init samples!");
778  return 1;
779 }
780
781
782 // we should handle this on microcontrollers, too.
783#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
784 signal(SIGINT,  on_sig_int);
785 signal(SIGCHLD, on_sig_chld);
786 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
787#endif
788
789 if ( realtime ) {
790#ifdef DEBUG
791  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
792#endif
793
794#ifdef ROAR_HAVE_NICE
795  errno = 0;
796  nice(-5*realtime); // -5 for each --realtime
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
803/*
804#ifdef __linux__
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));
807#endif
808*/
809 }
810
811#ifdef ROAR_HAVE_SETGID
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  }
816  if ( !grp || setgid(grp->gr_gid) == -1 ) {
817   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
818  }
819 }
820#endif
821
822
823 clients_set_pid(g_self_client, getpid());
824#ifdef ROAR_HAVE_GETUID
825 clients_set_uid(g_self_client, getuid());
826#endif
827#ifdef ROAR_HAVE_GETGID
828 clients_set_gid(g_self_client, getgid());
829#endif
830 clients_get(g_self_client, &self);
831
832 if ( self == NULL ) {
833  ROAR_ERR("Can not get self client!");
834  return 1;
835 }
836
837 strcpy(self->name, "RoarAudio daemon internal");
838
839#ifdef ROAR_HAVE_FORK
840 if ( daemon ) {
841  close(ROAR_STDIN );
842  close(ROAR_STDOUT);
843  close(ROAR_STDERR);
844
845  if ( fork() )
846   ROAR_U_EXIT(0);
847
848#ifdef ROAR_HAVE_SETSID
849  setsid();
850#endif
851  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
852 }
853#endif
854
855#ifdef ROAR_HAVE_CHROOT
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 }
866#endif
867
868#ifdef ROAR_HAVE_SETUID
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  }
874#ifdef ROAR_HAVE_GETUID
875  clients_set_uid(g_self_client, getuid());
876#endif
877 }
878#endif
879
880 // start main loop...
881 main_loop(drvid, drvinst, &sa, sysclocksync);
882
883 // clean up.
884 clean_quit_prep();
885 driver_close(drvinst, drvid);
886 output_buffer_free();
887
888 return 0;
889}
890
891void cleanup_listen_socket (int terminate) {
892
893#ifdef ROAR_SUPPORT_LISTEN
894 if ( g_listen_socket != -1 ) {
895#ifdef ROAR_HAVE_IO_POSIX
896  close(g_listen_socket);
897#endif // #else is useless because we are in void context.
898
899  g_listen_socket = -1;
900
901#ifdef ROAR_HAVE_UNIX
902  if ( *server == '/' )
903   unlink(server);
904#endif
905 }
906
907#endif
908
909 if ( terminate )
910  g_terminate = 1;
911}
912
913void clean_quit_prep (void) {
914 cleanup_listen_socket(0);
915
916 sources_free();
917 streams_free();
918 clients_free();
919 midi_cb_stop(); // stop console beep
920 midi_free();
921 light_free();
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.