source: roaraudio/roard/roard.c @ 1993:735317524c03

Last change on this file since 1993:735317524c03 was 1993:735317524c03, checked in by phi, 15 years ago

added support to *REGISTER* to SLP, need to write code to deregister ;)

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