source: roaraudio/roard/roard.c @ 2160:39b8ea65bfe6

Last change on this file since 2160:39b8ea65bfe6 was 2160:39b8ea65bfe6, checked in by phi, 15 years ago

set antiecho flag per default on BIDIR streams

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