source: roaraudio/roard/roard.c @ 1925:71a2f7a932f5

Last change on this file since 1925:71a2f7a932f5 was 1924:4b8f87ffd0b0, checked in by phi, 15 years ago

make midi subsystem a bit configurable

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