source: roaraudio/roard/roard.c @ 2028:e5bfbf9b184e

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

only use OpenSLP if it is present....

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