source: roaraudio/roard/roard.c @ 1923:cd3072dcde5f

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

added option --light-channels

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