source: roaraudio/roard/roard.c @ 2291:d3f351907764

Last change on this file since 2291:d3f351907764 was 2291:d3f351907764, checked in by phi, 15 years ago

playback streams have set the meta flag by default now

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