source: roaraudio/roard/roard.c @ 1926:4522f8bfab00

Last change on this file since 1926:4522f8bfab00 was 1926:4522f8bfab00, checked in by phi, 15 years ago

added sill dummy stream flag MMAP :)

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