source: roaraudio/roard/roard.c @ 1504:d00454bcfd80

Last change on this file since 1504:d00454bcfd80 was 1503:c0005aee16ff, checked in by phi, 15 years ago

added config var if we have args in main()

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