source: roaraudio/roard/roard.c @ 2234:5c84620cf44d

Last change on this file since 2234:5c84620cf44d was 2234:5c84620cf44d, checked in by phi, 15 years ago

make it possible to set subsystem=raw

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