source: roaraudio/roard/roard.c @ 1494:687defe4fca9

Last change on this file since 1494:687defe4fca9 was 1494:687defe4fca9, checked in by phi, 15 years ago

test for ROAR_SUPPORT_LISTEN, disable listening socket code if not set

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