source: roaraudio/roard/roard.c @ 1819:2604692f2a5b

Last change on this file since 1819:2604692f2a5b was 1819:2604692f2a5b, checked in by phi, 15 years ago

init and free light control subsystem

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