source: roaraudio/roard/roard.c @ 2159:2ab138cbc626

Last change on this file since 2159:2ab138cbc626 was 2115:16ddc6566cb2, checked in by phi, 15 years ago

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