source: roaraudio/roard/roard.c @ 1787:ed6218aaa6ba

Last change on this file since 1787:ed6218aaa6ba was 1787:ed6218aaa6ba, checked in by phi, 15 years ago

set socket to "" in case of not listen instad of *server=0

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