source: roaraudio/roard/roard.c @ 1493:405133c68d28

Last change on this file since 1493:405133c68d28 was 1486:b283d24f34ef, checked in by phi, 15 years ago

done a lot checks for target capabilities, check for: ROAR_HAVE_UNIX, ROAR_HAVE_IO_POSIX, ROAR_HAVE_SETUID, ROAR_HAVE_SETGID, ROAR_HAVE_CHROOT, ROAR_HAVE_FORK, ROAR_HAVE_NICE, ROAR_HAVE_GETSERVBYNAME; also updated signal interface becasue current one only works on POSIX systems

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