source: roaraudio/roard/roard.c @ 2104:4140ab350dda

Last change on this file since 2104:4140ab350dda was 2104:4140ab350dda, checked in by phi, 15 years ago

added pidfile support

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