source: roaraudio/roard/roard.c @ 1581:485598ab37df

Last change on this file since 1581:485598ab37df was 1581:485598ab37df, checked in by phi, 15 years ago

don't close driver not opened

File size: 23.0 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  } else if ( strcmp(k, "autoconf") == 0 ) {
219   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
220  } else {
221   ROAR_ERR("add_output(*): unknown option '%s'", k);
222   streams_delete(stream);
223   if ( prim ) alive = 0;
224#ifdef ROAR_DRIVER_CODEC
225   if ( to_free != NULL )
226    free(to_free);
227#endif
228   return -1;
229  }
230
231  k = strtok(NULL, ",");
232 }
233
234#ifdef ROAR_DRIVER_CODEC
235 if ( to_free != NULL )
236  free(to_free);
237#endif
238
239 if ( codec == ROAR_CODEC_ALAW || codec == ROAR_CODEC_MULAW )
240  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
241
242 s->info.codec = codec;
243 ROAR_STREAM_SERVER(s)->codec_orgi = codec;
244
245 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1) == -1 ) {
246  ss->driver_id = -1; // don't close a driver not opened...
247  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
248  streams_delete(stream);
249  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
250  if ( prim ) alive = 0;
251  return -1;
252 }
253
254 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here
255 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM,   s); // ignore errors here
256
257 if ( blocks != -1 )
258  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
259
260 if ( blocksize != -1 )
261  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
262
263 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
264
265 streams_set_fh(stream, -1); // update some internal structures
266
267 client_stream_add(g_source_client, stream);
268
269 if ( prim ) {
270  streams_mark_primary(stream);
271  s->pos_rel_id = stream;
272 }
273
274 if ( sync ) {
275  streams_set_flag(stream, ROAR_FLAG_SYNC);
276 } else {
277  streams_reset_flag(stream, ROAR_FLAG_SYNC);
278 }
279
280 return 0;
281}
282
283#ifdef ROAR_HAVE_MAIN_ARGS
284int main (int argc, char * argv[]) {
285#else
286int main (void) {
287#endif
288#ifdef ROAR_HAVE_MAIN_ARGS
289 int i;
290 char * k;
291#endif
292#ifdef ROAR_SUPPORT_LISTEN
293 char user_sock[80]  = {0};
294#endif
295 struct roar_audio_info sa;
296#ifdef ROAR_HAVE_FORK
297 int    daemon       = 0;
298#endif
299 int    realtime     = 0;
300 int    sysclocksync = 0;
301 char * driver    = NULL;
302 char * device    = NULL;
303#ifdef ROAR_HAVE_MAIN_ARGS
304 char * opts      = NULL;
305#endif
306// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
307#ifdef ROAR_SUPPORT_LISTEN
308 int      port    = ROAR_DEFAULT_PORT;
309#endif
310 int               drvid;
311 char * s_drv     = "cf";
312 char * s_dev     = NULL;
313 char * s_con     = NULL;
314 char * s_opt     = NULL;
315 int    s_prim    = 0;
316 char * o_drv     = getenv("ROAR_DRIVER");
317 char * o_dev     = getenv("ROAR_DEVICE");
318 char * o_opts    = NULL;
319 int    o_prim    = 0;
320 int    o_count   = 0;
321 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
322 char * sock_user = NULL;
323#ifdef ROAR_SUPPORT_LISTEN
324 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
325#endif
326#ifdef ROAR_HAVE_CHROOT
327 char * chrootdir = NULL;
328#endif
329#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
330 int    setids    = 0;
331#endif
332#ifdef ROAR_HAVE_UNIX
333 char * env_roar_proxy_backup;
334#endif
335#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
336 struct group   * grp  = NULL;
337#endif
338#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
339 struct passwd  * pwd  = NULL;
340#endif
341#ifdef ROAR_HAVE_GETSERVBYNAME
342 struct servent * serv = NULL;
343#endif
344 DRIVER_USERDATA_T drvinst;
345 struct roar_client * self = NULL;
346#ifdef ROAR_HAVE_LIBDNET
347 char decnethost[80];
348#endif
349
350 g_standby       =  0;
351 g_autostandby   =  0;
352 alive           =  1;
353#ifdef ROAR_SUPPORT_LISTEN
354 g_no_listen     =  0;
355 g_listen_socket = -1;
356#else
357 g_terminate     =  1;
358#endif
359
360 sa.bits     = ROAR_BITS_DEFAULT;
361 sa.channels = ROAR_CHANNELS_DEFAULT;
362 sa.rate     = ROAR_RATE_DEFAULT;
363 sa.codec    = ROAR_CODEC_DEFAULT;
364
365 g_sa = &sa;
366
367
368#ifdef ROAR_SUPPORT_LISTEN
369 if ( getuid() != 0 && getenv("HOME") != NULL ) {
370  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
371  server = user_sock;
372 }
373
374 if ( getenv("ROAR_SERVER") != NULL )
375  server = getenv("ROAR_SERVER");
376#endif
377
378 if ( clients_init() == -1 ) {
379  ROAR_ERR("Can not init clients!");
380  return 1;
381 }
382
383 if ( streams_init() == -1 ) {
384  ROAR_ERR("Can not init streams!");
385  return 1;
386 }
387
388 if ( (g_self_client = clients_new()) == -1 ) {
389  ROAR_ERR("Can not create self client!");
390  return 1;
391 }
392
393 if ( sources_init() == -1 ) {
394  ROAR_ERR("Can not init sources!");
395  return 1;
396 }
397
398 if ( (sources_set_client(g_self_client)) == -1 ) {
399  ROAR_ERR("Can not init set source client!");
400  return 1;
401 }
402
403#ifdef ROAR_HAVE_MAIN_ARGS
404 for (i = 1; i < argc; i++) {
405  k = argv[i];
406
407  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
408   usage();
409   return 0;
410
411  } else if ( strcmp(k, "--restart") == 0 ) {
412#ifdef ROAR_SUPPORT_LISTEN
413   if ( restart_server(server) == -1 ) {
414    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server);
415   }
416#else
417   ROAR_ERR("--restart not supported");
418#endif
419
420  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
421#ifdef ROAR_HAVE_FORK
422   daemon = 1;
423#else
424   ROAR_ERR("--daemon not supported");
425#endif
426  } else if ( strcmp(k, "--terminate") == 0 ) {
427   g_terminate = 1;
428  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
429   sysclocksync = 1000;
430  } else if ( strcmp(k, "--realtime") == 0 ) {
431   realtime++;
432  } else if ( strcmp(k, "--chroot") == 0 ) {
433#ifdef ROAR_HAVE_CHROOT
434   chrootdir = argv[++i];
435#else
436   ROAR_ERR("--chroot not supported");
437   i++;
438#endif
439  } else if ( strcmp(k, "--setgid") == 0 ) {
440#ifdef ROAR_HAVE_SETGID
441   setids |= R_SETGID;
442#else
443   ROAR_ERR("--setgid not supported");
444#endif
445  } else if ( strcmp(k, "--setuid") == 0 ) {
446#ifdef ROAR_HAVE_SETUID
447   setids |= R_SETUID;
448#else
449   ROAR_ERR("--setuid not supported");
450#endif
451
452  } else if ( strcmp(k, "--list-cf") == 0 ) {
453   print_codecfilterlist();
454   return 0;
455
456  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
457   sa.rate = atoi(argv[++i]);
458  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
459   sa.bits = atoi(argv[++i]);
460  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
461   sa.channels = atoi(argv[++i]);
462
463  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
464   driver = argv[++i];
465   if ( strcmp(driver, "list") == 0 ) {
466    ROAR_WARN("The option is obsolete, use --list-driver!");
467    print_driverlist();
468    return 0;
469   }
470  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
471   device = argv[++i];
472  } else if ( strcmp(k, "-dO") == 0 ) {
473   opts = argv[++i];
474  } else if ( strcmp(k, "--list-driver") == 0 ) {
475   print_driverlist();
476   return 0;
477
478  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
479   o_drv  = argv[++i];
480  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
481   o_dev  = argv[++i];
482  } else if ( strcmp(k, "-oO") == 0 ) {
483   o_opts = argv[++i];
484  } else if ( strcmp(k, "-oP") == 0 ) {
485   o_prim = 1;
486  } else if ( strcmp(k, "-oN") == 0 ) {
487   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
488    o_count++;
489
490   o_drv  = o_dev = o_opts = NULL;
491   o_prim = 0;
492
493  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
494   s_drv = argv[++i];
495  } else if ( strcmp(k, "-S") == 0 ) {
496   s_dev = argv[++i];
497  } else if ( strcmp(k, "-sO") == 0 ) {
498   s_opt = argv[++i];
499  } else if ( strcmp(k, "-sC") == 0 ) {
500   s_con = argv[++i];
501  } else if ( strcmp(k, "-sP") == 0 ) {
502   s_prim = 1;
503  } else if ( strcmp(k, "-sN") == 0 ) {
504   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
505    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
506   }
507   s_opt = s_dev = s_con = NULL;
508   s_drv = "cf";
509   s_prim = 0;
510
511  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
512   // This is only usefull in INET not UNIX mode.
513#ifdef ROAR_SUPPORT_LISTEN
514   if ( *server == '/' )
515    server = ROAR_DEFAULT_HOST;
516
517   errno = 0;
518   if ( (port = atoi(argv[++i])) < 1 ) {
519#ifdef ROAR_HAVE_GETSERVBYNAME
520    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
521     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
522     return 1;
523    }
524    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
525    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
526            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
527    port = ROAR_NET2HOST16(serv->s_port);
528#else
529    ROAR_ERR("invalite port number: %s", argv[i]);
530    return 1;
531#endif
532   }
533#endif
534  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
535#ifdef ROAR_SUPPORT_LISTEN
536   server = argv[++i];
537#endif
538
539  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
540#ifdef ROAR_SUPPORT_LISTEN
541   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
542    sock_type = ROAR_SOCKET_TYPE_TCP;
543
544   if ( *server == '/' )
545    server = ROAR_DEFAULT_HOST;
546#endif
547
548  } else if ( strcmp(k, "-4") == 0 ) {
549#ifdef ROAR_SUPPORT_LISTEN
550   sock_type = ROAR_SOCKET_TYPE_TCP;
551   if ( *server == '/' )
552    server = ROAR_DEFAULT_HOST;
553#endif
554  } else if ( strcmp(k, "-6") == 0 ) {
555#ifdef ROAR_SUPPORT_LISTEN
556#ifdef PF_INET6
557   sock_type = ROAR_SOCKET_TYPE_TCP6;
558   if ( *server == '/' )
559    server = ROAR_DEFAULT_HOST;
560#else
561    ROAR_ERR("No IPv6 support compiled in!");
562    return 1;
563#endif
564#endif
565
566  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
567#ifdef ROAR_SUPPORT_LISTEN
568   // ignore this case as it is the default behavor.
569   sock_type = ROAR_SOCKET_TYPE_UNIX;
570#endif
571
572  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
573#ifdef ROAR_SUPPORT_LISTEN
574#ifdef ROAR_HAVE_LIBDNET
575    port   = ROAR_DEFAULT_NUM;
576    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
577    server = decnethost;
578    sock_type = ROAR_SOCKET_TYPE_DECNET;
579#else
580    ROAR_ERR("No DECnet support compiled in!");
581    return 1;
582#endif
583#endif
584
585  } else if ( strcmp(k, "-G") == 0 ) {
586   sock_grp  = argv[++i];
587  } else if ( strcmp(k, "-U") == 0 ) {
588   sock_user = argv[++i];
589
590  } else if ( strcmp(k, "--no-listen") == 0 ) {
591#ifdef ROAR_SUPPORT_LISTEN
592   *server     = 0;
593   g_terminate = 1;
594   g_no_listen = 1;
595#endif
596  } else if ( strcmp(k, "--client-fh") == 0 ) {
597   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
598    ROAR_ERR("main(*): Can not set client's fh");
599    return 1;
600   }
601  } else if ( strcmp(k, "--close-fh") == 0 ) {
602#ifdef ROAR_HAVE_IO_POSIX
603   close(atoi(argv[++i]));
604#else
605   i++;
606   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
607#endif
608
609  } else if ( strcmp(k, "--standby") == 0 ) {
610   g_standby = 1;
611  } else if ( strcmp(k, "--auto-standby") == 0 ) {
612   g_autostandby = 1;
613  } else {
614   usage();
615   return 1;
616  }
617
618 }
619#endif
620
621 if ( s_dev != NULL ) {
622  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
623   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
624  }
625 }
626
627 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
628
629 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
630
631 if ( midi_init() == -1 )
632  ROAR_ERR("Can not initialize MIDI subsystem");
633
634#ifdef ROAR_SUPPORT_LISTEN
635 if ( *server != 0 ) {
636  if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
637#ifdef ROAR_HAVE_UNIX
638   if ( *server == '/' ) {
639    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
640     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
641     unsetenv("ROAR_PROXY");
642    }
643    if ( (i = roar_socket_connect(server, port)) != -1 ) {
644     close(i);
645     ROAR_ERR("Can not open listen socket!");
646     return 1;
647    } else {
648     unlink(server);
649     if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
650      ROAR_ERR("Can not open listen socket!");
651      return 1;
652     }
653    }
654    if ( env_roar_proxy_backup != NULL ) {
655     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
656     free(env_roar_proxy_backup);
657    }
658#else
659   if (0) { // noop
660#endif
661   } else {
662    ROAR_ERR("Can not open listen socket!");
663    return 1;
664   }
665  }
666
667#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
668  if ( (grp = getgrnam(sock_grp)) == NULL ) {
669   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
670  }
671#endif
672#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
673  if ( sock_user || (setids & R_SETUID) ) {
674   if ( (pwd = getpwnam(sock_user)) == NULL ) {
675    ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
676   }
677  }
678#endif
679
680#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
681  if ( *server == '/' ) {
682   if ( grp ) {
683    if ( pwd ) {
684     chown(server, pwd->pw_uid, grp->gr_gid);
685    } else {
686     chown(server, -1, grp->gr_gid);
687    }
688    if ( getuid() == 0 )
689     chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
690   }
691  }
692#endif
693 }
694#endif
695
696 if ( output_buffer_init(&sa) == -1 ) {
697  ROAR_ERR("Can not init output buffer!");
698  return 1;
699 }
700
701 if ( driver == NULL ) {
702  driver = "null";
703 } else {
704  ROAR_WARN("Usage of old driver interface. use -o not -d!");
705 }
706
707 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
708  ROAR_ERR("Can not open output driver!");
709  return 1;
710 }
711
712 if ( samples_init() == -1 ) {
713  ROAR_ERR("Can not init samples!");
714  return 1;
715 }
716
717
718 // we should handle this on microcontrollers, too.
719#if !defined(ROAR_TARGET_MICROCONTROLLER)
720 signal(SIGINT,  on_sig_int);
721 signal(SIGCHLD, on_sig_chld);
722 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
723#endif
724
725 if ( realtime ) {
726#ifdef DEBUG
727  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
728#endif
729
730#ifdef ROAR_HAVE_NICE
731  errno = 0;
732  nice(-5*realtime); // -5 for each --realtime
733  if ( errno ) {
734   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
735  }
736#else
737  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
738#endif
739/*
740#ifdef __linux__
741  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
742   ROAR_WARN("Can not set io priority: %s", strerror(errno));
743#endif
744*/
745 }
746
747#ifdef ROAR_HAVE_SETGID
748 if ( setids & R_SETGID ) {
749  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
750   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
751  }
752  if ( !grp || setgid(grp->gr_gid) == -1 ) {
753   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
754  }
755 }
756#endif
757
758
759 clients_set_pid(g_self_client, getpid());
760 clients_set_uid(g_self_client, getuid());
761 clients_set_gid(g_self_client, getgid());
762 clients_get(g_self_client, &self);
763
764 if ( self == NULL ) {
765  ROAR_ERR("Can not get self client!");
766  return 1;
767 }
768
769 strcpy(self->name, "RoarAudio daemon internal");
770
771#ifdef ROAR_HAVE_FORK
772 if ( daemon ) {
773  close(ROAR_STDIN );
774  close(ROAR_STDOUT);
775  close(ROAR_STDERR);
776  setsid();
777  if ( fork() )
778   ROAR_U_EXIT(0);
779  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
780 }
781#endif
782
783#ifdef ROAR_HAVE_CHROOT
784 if (chrootdir) {
785  if ( chroot(chrootdir) == -1 ) {
786   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
787   return 2;
788  }
789  if ( chdir("/") == -1 ) {
790   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
791   return 2;
792  }
793 }
794#endif
795
796#ifdef ROAR_HAVE_SETUID
797 if ( setids & R_SETUID ) {
798  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
799   ROAR_ERR("Can not set UserID: %s", strerror(errno));
800   return 3;
801  }
802  clients_set_uid(g_self_client, getuid());
803 }
804#endif
805
806 // start main loop...
807 main_loop(drvid, drvinst, &sa, sysclocksync);
808
809 // clean up.
810 clean_quit_prep();
811 driver_close(drvinst, drvid);
812 output_buffer_free();
813
814 return 0;
815}
816
817void cleanup_listen_socket (int terminate) {
818
819#ifdef ROAR_SUPPORT_LISTEN
820 if ( g_listen_socket != -1 ) {
821#ifdef ROAR_HAVE_IO_POSIX
822  close(g_listen_socket);
823#endif // #else is useless because we are in void context.
824
825  g_listen_socket = -1;
826
827#ifdef ROAR_HAVE_UNIX
828  if ( *server == '/' )
829   unlink(server);
830#endif
831 }
832
833#endif
834
835 if ( terminate )
836  g_terminate = 1;
837}
838
839void clean_quit_prep (void) {
840 cleanup_listen_socket(0);
841
842 sources_free();
843 streams_free();
844 clients_free();
845 midi_cb_stop(); // stop console beep
846 midi_free();
847}
848
849void clean_quit (void) {
850 clean_quit_prep();
851// driver_close(drvinst, drvid);
852// output_buffer_free();
853 exit(0);
854}
855
856//ll
Note: See TracBrowser for help on using the repository browser.