source: roaraudio/roard/roard.c @ 1522:2ab4c1e3d956

Last change on this file since 1522:2ab4c1e3d956 was 1522:2ab4c1e3d956, checked in by phi, 15 years ago

added ROAR_VIO_CTL_SET_DBLKSIZE, added blocksize= stream option

File size: 22.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#ifdef ROAR_HAVE_MAIN_ARGS
32void usage (void) {
33 printf("Usage: roard [OPTIONS]...\n\n");
34
35 printf("Misc Options:\n\n");
36 printf(
37        " --daemon              - Bring the server into background after init\n"
38        " --terminate           - Terminate after last client quited\n"
39        " --restart             - Trys to stop an old instance and start a new with new settings\n"
40        " --realtime            - Trys to get realtime priority,\n"
41        "                         give multible times for being more realtime\n"
42        " --chroot DIR          - chroots to the given dir\n"
43        " --setgid              - GroupID to the audio group as specified via -G\n"
44        " --setuid              - UserID to the audio user as specified via -U\n"
45        " --sysclocksync        - calculate exact sample rate using the system clock\n"
46       );
47
48 printf("\nAudio Options:\n\n");
49 printf(
50        " -R  --rate   RATE     - Set server rate\n"
51        " -B  --bits   BITS     - Set server bits\n"
52        " -C  --chans  CHANNELS - Set server channels\n"
53       );
54
55 printf("\nDriver Options:\n\n");
56 printf(" -d  --driver DRV      - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT);
57 printf(" -D  --device DEV      - Set the device\n");
58 printf(" -dO OPTS              - Set output options\n");
59 printf(" --list-driver         - List all drivers\n");
60
61 printf("\nOutput Options:\n\n");
62 printf(" -o  --odriver DRV     - Set the driver, use '--list-driver' to get a list\n");
63 printf(" -O  --odevice DEV     - Set the device\n");
64 printf(" -oO OPTS              - Set output options\n");
65 printf(" -oN                   - Adds another output\n");
66 printf(" -oP                   - Mark output as primary\n");
67
68 printf("\nSource Options:\n\n");
69 printf(" -s  --source DRV      - Use DRV as input driver\n"
70        " -S           DEV      - Use DEV as input device\n"
71        " -sO          OPTS     - Use OPTS as input options\n"
72        " -sN                   - Adds another source\n"
73        " -sP                   - Make souce as primary\n"
74       );
75
76 printf("\nCodec Filter Options:\n\n");
77 printf(" --list-cf             - List all codec filter\n"
78       );
79
80 printf("\nServer Options:\n\n");
81 printf(" -t  --tcp             - Use TCP listen socket\n"
82        " -u  --unix            - Use UNIX Domain listen socket (default)\n"
83#ifdef ROAR_HAVE_LIBDNET
84        " -n  --decnet          - use DECnet listen socket\n"
85#endif
86        " -4                    - Use IPv4 connections (implies -t)\n"
87#ifdef PF_INET6
88        " -6                    - Use IPv6 connections (implies -t)\n"
89#endif
90#ifdef IPV6_ADDRFORM
91        " -64                   - Try to downgrade sockets from IPv6 into IPv4,\n"
92        "                         this is normaly not usefull.\n"
93#endif
94        " -p  --port            - TCP Port to bind to\n"
95        " -b  --bind            - IP/Hostname to bind to\n"
96        "     --sock            - Filename for UNIX Domain Socket\n"
97        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: %s)\n"
98        "                         You need the permittions to change the GID\n"
99        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
100        "                         You need the permittions to change the UID (normaly only root has)\n"
101        " --no-listen           - Do not listen for new clients\n"
102        "                         (only usefull for relaing, impleys --terminate)\n"
103        " --client-fh           - Comunicate with a client over this handle\n"
104        "                         (only usefull for relaing)\n"
105        " --close-fh            - Closes the given fh\n"
106        " --standby             - Start in standby state\n"
107        " --auto-standby        - Automatical goes into standby if there are no streams\n",
108        ROAR_DEFAULT_SOCKGRP
109       );
110// printf("\n Options:\n\n");
111 printf("\n");
112}
113#endif
114
115int restart_server (char * server) {
116 struct roar_connection con;
117 if ( roar_connect(&con, server) == -1 ) {
118  return -1;
119 }
120
121 if ( roar_terminate(&con, 1) == -1 ) {
122  return -1;
123 }
124
125 return roar_disconnect(&con);
126}
127
128#define R_SETUID 1
129#define R_SETGID 2
130
131int add_output (char * drv, char * dev, char * opts, int prim, int count) {
132 int stream;
133 struct roar_stream * s;
134 struct roar_stream_server * ss;
135 char * k, * v;
136#ifdef ROAR_DRIVER_CODEC
137 char * to_free = NULL;
138#endif
139 int codec;
140 int sync = 0;
141 int32_t blocks = -1, blocksize = -1;
142
143 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
144
145 if ( drv == NULL && count == 0 ) {
146  drv  = ROAR_DRIVER_DEFAULT;
147  prim = 1;
148  sync = 1;
149
150#ifdef ROAR_DRIVER_CODEC
151  if ( opts == NULL ) {
152   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
153  }
154#endif
155 }
156
157 if ( opts == NULL && count == 0 ) {
158  sync = 1;
159  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
160 }
161
162 if ( (stream = streams_new()) == -1 ) {
163  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
164  if ( prim ) alive = 0;
165  return -1;
166 }
167
168 streams_get(stream, &ss);
169 s = ROAR_STREAM(ss);
170
171 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
172
173 s->dir        = ROAR_DIR_OUTPUT;
174 s->pos_rel_id = -1;
175// s->info.codec = codec;
176
177 codec = s->info.codec;
178
179 k = strtok(opts, ",");
180 while (k != NULL) {
181//  ROAR_WARN("add_output(*): opts: %s", k);
182
183  if ( (v = strstr(k, "=")) != NULL ) {
184   *v++ = 0;
185  }
186
187  ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v);
188  if ( strcmp(k, "rate") == 0 ) {
189   s->info.rate = atoi(v);
190  } else if ( strcmp(k, "channels") == 0 ) {
191   s->info.channels = atoi(v);
192  } else if ( strcmp(k, "bits") == 0 ) {
193   s->info.bits = atoi(v);
194  } else if ( strcmp(k, "codec") == 0 ) {
195   if ( (codec = roar_str2codec(v)) == -1 ) {
196    ROAR_ERR("add_output(*): unknown codec '%s'", v);
197    streams_delete(stream);
198    if ( prim ) alive = 0;
199#ifdef ROAR_DRIVER_CODEC
200    if ( to_free != NULL )
201     free(to_free);
202#endif
203    return -1;
204   }
205  } else if ( strcmp(k, "blocks") == 0 ) {
206   blocks = atoi(v);
207  } else if ( strcmp(k, "blocksize") == 0 ) {
208   blocksize = atoi(v);
209  } else if ( strcmp(k, "meta") == 0 ) {
210   streams_set_flag(stream, ROAR_FLAG_META);
211  } else if ( strcmp(k, "sync") == 0 ) {
212   sync = 1;
213  } else if ( strcmp(k, "primary") == 0 ) {
214   prim = 1;
215
216  } else if ( strcmp(k, "cleanmeta") == 0 ) {
217   streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
218
219  /* ignore this for the moment ... */
220  } else if ( strcmp(k, "autoconf") == 0 ) {
221  } else {
222   ROAR_ERR("add_output(*): unknown option '%s'", k);
223   streams_delete(stream);
224   if ( prim ) alive = 0;
225#ifdef ROAR_DRIVER_CODEC
226   if ( to_free != NULL )
227    free(to_free);
228#endif
229   return -1;
230  }
231
232  k = strtok(NULL, ",");
233 }
234
235#ifdef ROAR_DRIVER_CODEC
236 if ( to_free != NULL )
237  free(to_free);
238#endif
239
240 if ( codec == ROAR_CODEC_ALAW || codec == ROAR_CODEC_MULAW )
241  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
242
243 s->info.codec = codec;
244 ROAR_STREAM_SERVER(s)->codec_orgi = codec;
245
246 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1) == -1 ) {
247  streams_delete(stream);
248  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
249  if ( prim ) alive = 0;
250  return -1;
251 }
252
253 if ( blocks != -1 )
254  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
255
256 if ( blocksize != -1 )
257  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
258
259 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
260
261 streams_set_fh(stream, -1); // update some internal structures
262
263 client_stream_add(g_source_client, stream);
264
265 if ( prim ) {
266  streams_mark_primary(stream);
267  s->pos_rel_id = stream;
268 }
269
270 if ( sync ) {
271  streams_set_flag(stream, ROAR_FLAG_SYNC);
272 } else {
273  streams_reset_flag(stream, ROAR_FLAG_SYNC);
274 }
275
276 return 0;
277}
278
279#ifdef ROAR_HAVE_MAIN_ARGS
280int main (int argc, char * argv[]) {
281#else
282int main (void) {
283#endif
284#ifdef ROAR_HAVE_MAIN_ARGS
285 int i;
286 char * k;
287#endif
288#ifdef ROAR_SUPPORT_LISTEN
289 char user_sock[80]  = {0};
290#endif
291 struct roar_audio_info sa;
292#ifdef ROAR_HAVE_FORK
293 int    daemon       = 0;
294#endif
295 int    realtime     = 0;
296 int    sysclocksync = 0;
297 char * driver    = NULL;
298 char * device    = NULL;
299#ifdef ROAR_HAVE_MAIN_ARGS
300 char * opts      = NULL;
301#endif
302// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
303#ifdef ROAR_SUPPORT_LISTEN
304 int      port    = ROAR_DEFAULT_PORT;
305#endif
306 int               drvid;
307 char * s_drv     = "cf";
308 char * s_dev     = NULL;
309 char * s_con     = NULL;
310 char * s_opt     = NULL;
311 int    s_prim    = 0;
312 char * o_drv     = getenv("ROAR_DRIVER");
313 char * o_dev     = getenv("ROAR_DEVICE");
314 char * o_opts    = NULL;
315 int    o_prim    = 0;
316 int    o_count   = 0;
317 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
318 char * sock_user = NULL;
319#ifdef ROAR_SUPPORT_LISTEN
320 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
321#endif
322#ifdef ROAR_HAVE_CHROOT
323 char * chrootdir = NULL;
324#endif
325#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
326 int    setids    = 0;
327#endif
328#ifdef ROAR_HAVE_UNIX
329 char * env_roar_proxy_backup;
330#endif
331#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
332 struct group   * grp  = NULL;
333#endif
334#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
335 struct passwd  * pwd  = NULL;
336#endif
337#ifdef ROAR_HAVE_GETSERVBYNAME
338 struct servent * serv = NULL;
339#endif
340 DRIVER_USERDATA_T drvinst;
341 struct roar_client * self = NULL;
342#ifdef ROAR_HAVE_LIBDNET
343 char decnethost[80];
344#endif
345
346 g_standby       =  0;
347 g_autostandby   =  0;
348 alive           =  1;
349#ifdef ROAR_SUPPORT_LISTEN
350 g_no_listen     =  0;
351 g_listen_socket = -1;
352#else
353 g_terminate     =  1;
354#endif
355
356 sa.bits     = ROAR_BITS_DEFAULT;
357 sa.channels = ROAR_CHANNELS_DEFAULT;
358 sa.rate     = ROAR_RATE_DEFAULT;
359 sa.codec    = ROAR_CODEC_DEFAULT;
360
361 g_sa = &sa;
362
363
364#ifdef ROAR_SUPPORT_LISTEN
365 if ( getuid() != 0 && getenv("HOME") != NULL ) {
366  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
367  server = user_sock;
368 }
369
370 if ( getenv("ROAR_SERVER") != NULL )
371  server = getenv("ROAR_SERVER");
372#endif
373
374 if ( clients_init() == -1 ) {
375  ROAR_ERR("Can not init clients!");
376  return 1;
377 }
378
379 if ( streams_init() == -1 ) {
380  ROAR_ERR("Can not init streams!");
381  return 1;
382 }
383
384 if ( (g_self_client = clients_new()) == -1 ) {
385  ROAR_ERR("Can not create self client!");
386  return 1;
387 }
388
389 if ( sources_init() == -1 ) {
390  ROAR_ERR("Can not init sources!");
391  return 1;
392 }
393
394 if ( (sources_set_client(g_self_client)) == -1 ) {
395  ROAR_ERR("Can not init set source client!");
396  return 1;
397 }
398
399#ifdef ROAR_HAVE_MAIN_ARGS
400 for (i = 1; i < argc; i++) {
401  k = argv[i];
402
403  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
404   usage();
405   return 0;
406
407  } else if ( strcmp(k, "--restart") == 0 ) {
408#ifdef ROAR_SUPPORT_LISTEN
409   if ( restart_server(server) == -1 ) {
410    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server);
411   }
412#else
413   ROAR_ERR("--restart not supported");
414#endif
415
416  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
417#ifdef ROAR_HAVE_FORK
418   daemon = 1;
419#else
420   ROAR_ERR("--daemon not supported");
421#endif
422  } else if ( strcmp(k, "--terminate") == 0 ) {
423   g_terminate = 1;
424  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
425   sysclocksync = 1000;
426  } else if ( strcmp(k, "--realtime") == 0 ) {
427   realtime++;
428  } else if ( strcmp(k, "--chroot") == 0 ) {
429#ifdef ROAR_HAVE_CHROOT
430   chrootdir = argv[++i];
431#else
432   ROAR_ERR("--chroot not supported");
433   i++;
434#endif
435  } else if ( strcmp(k, "--setgid") == 0 ) {
436#ifdef ROAR_HAVE_SETGID
437   setids |= R_SETGID;
438#else
439   ROAR_ERR("--setgid not supported");
440#endif
441  } else if ( strcmp(k, "--setuid") == 0 ) {
442#ifdef ROAR_HAVE_SETUID
443   setids |= R_SETUID;
444#else
445   ROAR_ERR("--setuid not supported");
446#endif
447
448  } else if ( strcmp(k, "--list-cf") == 0 ) {
449   print_codecfilterlist();
450   return 0;
451
452  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
453   sa.rate = atoi(argv[++i]);
454  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
455   sa.bits = atoi(argv[++i]);
456  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
457   sa.channels = atoi(argv[++i]);
458
459  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
460   driver = argv[++i];
461   if ( strcmp(driver, "list") == 0 ) {
462    ROAR_WARN("The option is obsolete, use --list-driver!");
463    print_driverlist();
464    return 0;
465   }
466  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
467   device = argv[++i];
468  } else if ( strcmp(k, "-dO") == 0 ) {
469   opts = argv[++i];
470  } else if ( strcmp(k, "--list-driver") == 0 ) {
471   print_driverlist();
472   return 0;
473
474  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
475   o_drv  = argv[++i];
476  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
477   o_dev  = argv[++i];
478  } else if ( strcmp(k, "-oO") == 0 ) {
479   o_opts = argv[++i];
480  } else if ( strcmp(k, "-oP") == 0 ) {
481   o_prim = 1;
482  } else if ( strcmp(k, "-oN") == 0 ) {
483   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
484    o_count++;
485
486   o_drv  = o_dev = o_opts = NULL;
487   o_prim = 0;
488
489  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
490   s_drv = argv[++i];
491  } else if ( strcmp(k, "-S") == 0 ) {
492   s_dev = argv[++i];
493  } else if ( strcmp(k, "-sO") == 0 ) {
494   s_opt = argv[++i];
495  } else if ( strcmp(k, "-sC") == 0 ) {
496   s_con = argv[++i];
497  } else if ( strcmp(k, "-sP") == 0 ) {
498   s_prim = 1;
499  } else if ( strcmp(k, "-sN") == 0 ) {
500   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
501    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
502   }
503   s_opt = s_dev = s_con = NULL;
504   s_drv = "cf";
505   s_prim = 0;
506
507  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
508   // This is only usefull in INET not UNIX mode.
509#ifdef ROAR_SUPPORT_LISTEN
510   if ( *server == '/' )
511    server = ROAR_DEFAULT_HOST;
512
513   errno = 0;
514   if ( (port = atoi(argv[++i])) < 1 ) {
515#ifdef ROAR_HAVE_GETSERVBYNAME
516    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
517     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
518     return 1;
519    }
520    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
521    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
522            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
523    port = ROAR_NET2HOST16(serv->s_port);
524#else
525    ROAR_ERR("invalite port number: %s", argv[i]);
526    return 1;
527#endif
528   }
529#endif
530  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
531#ifdef ROAR_SUPPORT_LISTEN
532   server = argv[++i];
533#endif
534
535  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
536#ifdef ROAR_SUPPORT_LISTEN
537   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
538    sock_type = ROAR_SOCKET_TYPE_TCP;
539
540   if ( *server == '/' )
541    server = ROAR_DEFAULT_HOST;
542#endif
543
544  } else if ( strcmp(k, "-4") == 0 ) {
545#ifdef ROAR_SUPPORT_LISTEN
546   sock_type = ROAR_SOCKET_TYPE_TCP;
547   if ( *server == '/' )
548    server = ROAR_DEFAULT_HOST;
549#endif
550  } else if ( strcmp(k, "-6") == 0 ) {
551#ifdef ROAR_SUPPORT_LISTEN
552#ifdef PF_INET6
553   sock_type = ROAR_SOCKET_TYPE_TCP6;
554   if ( *server == '/' )
555    server = ROAR_DEFAULT_HOST;
556#else
557    ROAR_ERR("No IPv6 support compiled in!");
558    return 1;
559#endif
560#endif
561
562  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
563#ifdef ROAR_SUPPORT_LISTEN
564   // ignore this case as it is the default behavor.
565   sock_type = ROAR_SOCKET_TYPE_UNIX;
566#endif
567
568  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
569#ifdef ROAR_SUPPORT_LISTEN
570#ifdef ROAR_HAVE_LIBDNET
571    port   = ROAR_DEFAULT_NUM;
572    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
573    server = decnethost;
574    sock_type = ROAR_SOCKET_TYPE_DECNET;
575#else
576    ROAR_ERR("No DECnet support compiled in!");
577    return 1;
578#endif
579#endif
580
581  } else if ( strcmp(k, "-G") == 0 ) {
582   sock_grp  = argv[++i];
583  } else if ( strcmp(k, "-U") == 0 ) {
584   sock_user = argv[++i];
585
586  } else if ( strcmp(k, "--no-listen") == 0 ) {
587#ifdef ROAR_SUPPORT_LISTEN
588   *server     = 0;
589   g_terminate = 1;
590   g_no_listen = 1;
591#endif
592  } else if ( strcmp(k, "--client-fh") == 0 ) {
593   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
594    ROAR_ERR("main(*): Can not set client's fh");
595    return 1;
596   }
597  } else if ( strcmp(k, "--close-fh") == 0 ) {
598#ifdef ROAR_HAVE_IO_POSIX
599   close(atoi(argv[++i]));
600#else
601   i++;
602   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
603#endif
604
605  } else if ( strcmp(k, "--standby") == 0 ) {
606   g_standby = 1;
607  } else if ( strcmp(k, "--auto-standby") == 0 ) {
608   g_autostandby = 1;
609  } else {
610   usage();
611   return 1;
612  }
613
614 }
615#endif
616
617 if ( s_dev != NULL ) {
618  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
619   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
620  }
621 }
622
623 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
624
625 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
626
627 if ( midi_init() == -1 )
628  ROAR_ERR("Can not initialize MIDI subsystem");
629
630#ifdef ROAR_SUPPORT_LISTEN
631 if ( *server != 0 ) {
632  if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
633#ifdef ROAR_HAVE_UNIX
634   if ( *server == '/' ) {
635    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
636     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
637     unsetenv("ROAR_PROXY");
638    }
639    if ( (i = roar_socket_connect(server, port)) != -1 ) {
640     close(i);
641     ROAR_ERR("Can not open listen socket!");
642     return 1;
643    } else {
644     unlink(server);
645     if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
646      ROAR_ERR("Can not open listen socket!");
647      return 1;
648     }
649    }
650    if ( env_roar_proxy_backup != NULL ) {
651     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
652     free(env_roar_proxy_backup);
653    }
654#else
655   if (0) { // noop
656#endif
657   } else {
658    ROAR_ERR("Can not open listen socket!");
659    return 1;
660   }
661  }
662
663#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
664  if ( (grp = getgrnam(sock_grp)) == NULL ) {
665   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
666  }
667#endif
668#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
669  if ( sock_user || (setids & R_SETUID) ) {
670   if ( (pwd = getpwnam(sock_user)) == NULL ) {
671    ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
672   }
673  }
674#endif
675
676#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
677  if ( *server == '/' ) {
678   if ( grp ) {
679    if ( pwd ) {
680     chown(server, pwd->pw_uid, grp->gr_gid);
681    } else {
682     chown(server, -1, grp->gr_gid);
683    }
684    if ( getuid() == 0 )
685     chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
686   }
687  }
688#endif
689 }
690#endif
691
692 if ( output_buffer_init(&sa) == -1 ) {
693  ROAR_ERR("Can not init output buffer!");
694  return 1;
695 }
696
697 if ( driver == NULL ) {
698  driver = "null";
699 } else {
700  ROAR_WARN("Usage of old driver interface. use -o not -d!");
701 }
702
703 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
704  ROAR_ERR("Can not open output driver!");
705  return 1;
706 }
707
708 if ( samples_init() == -1 ) {
709  ROAR_ERR("Can not init samples!");
710  return 1;
711 }
712
713
714 // we should handle this on microcontrollers, too.
715#if !defined(ROAR_TARGET_MICROCONTROLLER)
716 signal(SIGINT,  on_sig_int);
717 signal(SIGCHLD, on_sig_chld);
718 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
719#endif
720
721 if ( realtime ) {
722#ifdef DEBUG
723  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
724#endif
725
726#ifdef ROAR_HAVE_NICE
727  errno = 0;
728  nice(-5*realtime); // -5 for each --realtime
729  if ( errno ) {
730   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
731  }
732#else
733  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
734#endif
735/*
736#ifdef __linux__
737  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
738   ROAR_WARN("Can not set io priority: %s", strerror(errno));
739#endif
740*/
741 }
742
743#ifdef ROAR_HAVE_SETGID
744 if ( setids & R_SETGID ) {
745  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
746   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
747  }
748  if ( !grp || setgid(grp->gr_gid) == -1 ) {
749   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
750  }
751 }
752#endif
753
754
755 clients_set_pid(g_self_client, getpid());
756 clients_set_uid(g_self_client, getuid());
757 clients_set_gid(g_self_client, getgid());
758 clients_get(g_self_client, &self);
759
760 if ( self == NULL ) {
761  ROAR_ERR("Can not get self client!");
762  return 1;
763 }
764
765 strcpy(self->name, "RoarAudio daemon internal");
766
767#ifdef ROAR_HAVE_FORK
768 if ( daemon ) {
769  close(ROAR_STDIN );
770  close(ROAR_STDOUT);
771  close(ROAR_STDERR);
772  setsid();
773  if ( fork() )
774   ROAR_U_EXIT(0);
775  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
776 }
777#endif
778
779#ifdef ROAR_HAVE_CHROOT
780 if (chrootdir) {
781  if ( chroot(chrootdir) == -1 ) {
782   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
783   return 2;
784  }
785  if ( chdir("/") == -1 ) {
786   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
787   return 2;
788  }
789 }
790#endif
791
792#ifdef ROAR_HAVE_SETUID
793 if ( setids & R_SETUID ) {
794  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
795   ROAR_ERR("Can not set UserID: %s", strerror(errno));
796   return 3;
797  }
798  clients_set_uid(g_self_client, getuid());
799 }
800#endif
801
802 // start main loop...
803 main_loop(drvid, drvinst, &sa, sysclocksync);
804
805 // clean up.
806 clean_quit_prep();
807 driver_close(drvinst, drvid);
808 output_buffer_free();
809
810 return 0;
811}
812
813void cleanup_listen_socket (int terminate) {
814
815#ifdef ROAR_SUPPORT_LISTEN
816 if ( g_listen_socket != -1 ) {
817#ifdef ROAR_HAVE_IO_POSIX
818  close(g_listen_socket);
819#endif // #else is useless because we are in void context.
820
821  g_listen_socket = -1;
822
823#ifdef ROAR_HAVE_UNIX
824  if ( *server == '/' )
825   unlink(server);
826#endif
827 }
828
829#endif
830
831 if ( terminate )
832  g_terminate = 1;
833}
834
835void clean_quit_prep (void) {
836 cleanup_listen_socket(0);
837
838 sources_free();
839 streams_free();
840 clients_free();
841 midi_cb_stop(); // stop console beep
842 midi_free();
843}
844
845void clean_quit (void) {
846 clean_quit_prep();
847// driver_close(drvinst, drvid);
848// output_buffer_free();
849 exit(0);
850}
851
852//ll
Note: See TracBrowser for help on using the repository browser.