source: roaraudio/roard/roard.c @ 2364:4d2d1aa717b1

Last change on this file since 2364:4d2d1aa717b1 was 2364:4d2d1aa717b1, checked in by phi, 15 years ago

added argument ss of vio open driver...

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