source: roaraudio/roard/roard.c @ 1991:92666322b25e

Last change on this file since 1991:92666322b25e was 1991:92666322b25e, checked in by phi, 15 years ago

added stream options channel and universe to roard

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