source: roaraudio/roard/roard.c @ 1516:8537a24adfa3

Last change on this file since 1516:8537a24adfa3 was 1516:8537a24adfa3, checked in by phi, 15 years ago

only try to chown UNIX socket if we have UNIX support

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