Debian Codegraph @

rplayd/rplayd.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 4 — not the installed (fully patched) source.
4 Debian patches touch this file
1 /* $Id: rplayd.c,v 1.8 1999/03/10 07:58:04 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * rplay is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 #include "version.h"
28 #include "rplay.h"
29 #include <sys/param.h>
30 #include <sys/time.h>
31 #include <sys/signal.h>
32 #include <sys/file.h>
33 #include <sys/stat.h>
34 #ifdef HAVE_SYS_WAIT_H
35 #include <sys/wait.h>
36 #endif /* HAVE_SYS_WAIT_H */
37 #ifdef ultrix
38 #include <fcntl.h>
39 #else
40 #include <sys/fcntl.h>
41 #endif
42 #include <sys/uio.h>
43 #include <netdb.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <unistd.h>
47 #ifdef HAVE_STRING_H
48 #include <string.h>
49 #endif
50 #include <sys/errno.h>
51 #ifdef __STDC__
52 #include <stdarg.h>
53 #else
54 #include <varargs.h>
55 #endif
56 #include <pwd.h>
57 #include <grp.h>
58 #include "rplayd.h"
59 #include "connection.h"
60 #include "spool.h"
61 #include "sound.h"
62 #ifdef AUTH
63 #include "host.h"
64 #endif /* AUTH */
65 #include "server.h"
66 #include "misc.h"
67 #include "cache.h"
68 #include "timer.h"
69 #include "strdup.h"
70 #include "getopt.h"
71 #include "tilde.h"
72 #ifdef HAVE_CDROM
73 #include "cdrom.h"
74 #endif /* HAVE_CDROM */
75 #ifdef HAVE_HELPERS
76 #include "helper.h"
77 #endif /* HAVE_HELPERS */
78
79 /* Make sure MAXHOSTNAMELEN is defined. */
80 #ifndef MAXHOSTNAMELEN
81 #define MAXHOSTNAMELEN 256
82 #endif
83
84 /* Global variables */
85 fd_set read_mask; /* Mask of RPTP clients that are reading. */
86 fd_set write_mask; /* Mask of RPTP clients that are writing. */
87 int debug = 0; /* Is debugging enable? (--debug) */
88 int rptp_timeout = RPTP_CONNECTION_TIMEOUT; /* --connection-timeout */
89 char hostname[MAXHOSTNAMELEN]; /* Name of the local system. */
90 char *hostaddr; /* IP address of the local system. */
91 int rplay_fd = -1; /* Socket used for RPLAY packets. */
92 int rptp_fd = -1; /* Socket used for RPTP connections. */
93 #ifdef OTHER_RPLAY_PORTS
94 int other_rplay_fd = -1; /* Another RPLAY socket. */
95 int other_rptp_fd = -1; /* Another RPTP socket. */
96 #endif
97 int curr_rate; /* Current audio rate. */
98 #ifndef HAVE_OSS
99 int curr_bufsize; /* Current audio bufsize. */
100 #endif /* !HAVE_OSS */
101 int rplayd_pid; /* Process ID of rplayd. */
102 time_t starttime; /* The time in seconds that rplayd started. */
103 #ifdef AUTH
104 int auth_enabled = 1; /* Host authentication. (--auth, --no-auth) */
105 #endif /* AUTH */
106 int rplay_priority_threshold = RPLAY_DEFAULT_PRIORITY; /* Default priority level allowed. */
107
108 /* Global audio-specific variables */
109 char *rplay_audio_device = RPLAY_AUDIO_DEVICE; /* Which audio device to use. */
110 #ifndef HAVE_OSS
111 int rplay_audio_bufsize = 0; /* --audio-bufsize */
112 #endif /* !HAVE_OSS */
113 int rplay_audio_rate = 0; /* --audio-rate */
114 int rplay_audio_sample_rate = 0; /* --audio-sample-rate */
115 int rplay_audio_channels = 0; /* --audio-channels */
116 int rplay_audio_format = 0; /* --audio-format */
117 int rplay_audio_precision = 0; /* --audio-precison */
118 int rplay_audio_port = 0; /* --audio_port */
119 int audio_enabled = 1; /* --no-audio */
120 int rplay_audio_volume = RPLAY_DEFAULT_VOLUME; /* Current audio volume. */
121 int rplay_audio_match = 0;
122 RPLAY_AUDIO_TABLE *rplay_audio_table = NULL;
123
124 /* Optional audio parameters specified on the command line. */
125 int optional_sample_rate = 0;
126 int optional_precision = 0;
127 int optional_channels = 0;
128 int optional_format = 0;
129 int optional_port = 0;
130
131 /* Default audio parameters. */
132 int default_sample_rate = 0;
133 int default_precision = 0;
134 int default_channels = 0;
135 int default_format = 0;
136 int default_port = 0;
137
138 #ifdef HAVE_OSS
139 int rplay_audio_fragsize = 0;
140 int optional_fragsize = 0;
141 int default_fragsize = 0;
142 #endif /* HAVE_OSS */
143
144 /* The audio buffer and its current and maximum size. */
145 int rplay_audio_size = 0;
146 char *rplay_audio_buf = NULL;
147 int max_rplay_audio_bufsize = 0;
148
149 /* Audio levels. */
150 int rplay_audio_left_level = 0;
151 int rplay_audio_right_level = 0;
152
153 static int monitor_total_bytes = 176400 * 5; /* 5 seconds */
154 static int monitor_update = 0;
155 int monitor_count = 0;
156 BUFFER *monitor_buffers = NULL;
157
158 int rplay_audio_timeout = RPLAY_AUDIO_TIMEOUT;
159 static int rplay_audio_flush_timeout = RPLAY_AUDIO_FLUSH_TIMEOUT;
160 static int rplayd_timeout = RPLAYD_TIMEOUT; /* seconds */
161 static int sound_cleanup_timeout = 10; /* seconds */
162 static int buffer_cleanup_timeout = 10; /* seconds */
163 static int spool_cleanup_timeout = 0; /* seconds -- disabled for now */
164 static int need_to_reset = 0;
165 static char *progname;
166 static char *sounds_file = NULL;
167 #ifdef AUTH
168 static char *hosts_file = NULL;
169 #endif /* AUTH */
170 static char *servers_file = NULL;
171 static char *cache_dir = NULL;
172 static char *log_file = NULL;
173 #ifdef HAVE_HELPERS
174 static char *helpers_file = NULL;
175 #endif /* HAVE_HELPERS */
176 static int logging = 0;
177 static int report_level;
178 static int log_fd = -1;
179 static int forward_fd = -1;
180 static char *forward = NULL;
181 static int rplay_port = 0;
182 static int rptp_port = 0;
183 #ifdef OTHER_RPLAY_PORTS
184 static int other_rplay_port = 0;
185 static int other_rptp_port = 0;
186 #endif /* OTHER_RPLAY_PORTS */
187 static int audio_test_mode = 0; /* enable audio test mode */
188 static int do_fork = 1;
189 static int inetd = 0; /* Was rplayd started by inetd? */
190
191 #ifndef RPLAYD_USER
192 #define RPLAYD_USER ""
193 #endif
194 #ifndef RPLAYD_GROUP
195 #define RPLAYD_GROUP ""
196 #endif
197 static char *run_as_user = RPLAYD_USER;
198 static char *run_as_group = RPLAYD_GROUP;
199
200 extern char *optarg; /* getopt_long */
201 extern int optind; /* getopt_long */
202
203 /*
204 * Long options for getopt_long.
205 */
206 static struct option longopts[] =
207 {
208 {"audio-device", required_argument, NULL, 'A'},
209 #ifndef HAVE_OSS
210 {"audio-bufsize", required_argument, NULL, 'b'},
211 #endif /* !HAVE_OSS */
212 {"audio-bits", required_argument, NULL, 'B'},
213 {"audio-channels", required_argument, NULL, 1},
214 {"audio-close", required_argument, NULL, 'c'},
215 {"audio-flush", required_argument, NULL, 'F'},
216 {"audio-format", required_argument, NULL, 3},
217 #ifdef HAVE_OSS
218 {"audio-fragsize", required_argument, NULL, 18},
219 #endif /* HAVE_OSS */
220 {"audio-info", required_argument, NULL, 'i'},
221 {"audio-info-ulaw", no_argument, NULL, 13},
222 {"audio-match", no_argument, &rplay_audio_match, 1},
223 {"audio-port", required_argument, NULL, 10},
224 {"audio-rate", required_argument, NULL, 'r'},
225 {"audio-sample-rate", required_argument, NULL, 'R'},
226 {"audio-test", no_argument, &audio_test_mode, 1},
227 #ifdef AUTH
228 {"auth", no_argument, &auth_enabled, 1},
229 #endif
230 {"cache-directory", required_argument, NULL, 'D'},
231 {"cache-remove", no_argument, NULL, 12},
232 {"cache-size", required_argument, NULL, 's'},
233 #ifdef HAVE_CDROM
234 {"cdrom0", required_argument, NULL, 14},
235 {"cdrom1", required_argument, NULL, 15},
236 {"cdrom2", required_argument, NULL, 16},
237 {"cdrom3", required_argument, NULL, 17},
238 #endif /* HAVE_CDROM */
239 {"conf", required_argument, NULL, 'C'},
240 {"connection-timeout", required_argument, NULL, 'T'},
241 {"debug", no_argument, NULL, 'd'},
242 {"fork", no_argument, &do_fork, 1},
243 {"forward", required_argument, NULL, 'f'},
244 {"group", required_argument, NULL, 21},
245 {"help", no_argument, NULL, 2},
246 #ifdef HAVE_HELPERS
247 {"helpers", required_argument, NULL, 20},
248 #endif /* HAVE_HELPERS */
249 #ifdef AUTH
250 {"hosts", required_argument, NULL, 'H'},
251 #endif
252 {"inetd", no_argument, &inetd, 1},
253 {"info", required_argument, NULL, 'i'},
254 {"info-ulaw", no_argument, NULL, 13},
255 {"log-file", required_argument, NULL, 'L'},
256 {"log-level", required_argument, NULL, 'l'},
257 {"memory-cache-size", required_argument, NULL, 4},
258 {"memory-cache-sound-size", required_argument, NULL, 5},
259 {"no-audio", no_argument, NULL, 'N'},
260 #ifdef AUTH
261 {"no-auth", no_argument, &auth_enabled, 0},
262 #endif
263 {"no-fork", no_argument, &do_fork, 0},
264 {"no-inetd", no_argument, NULL, 'n'},
265 {"option-file", required_argument, NULL, 11},
266 {"options-file", required_argument, NULL, 11},
267 #ifdef OTHER_RPLAY_PORTS
268 {"other-rplay-port", required_argument, NULL, 9},
269 {"other-rptp-port", required_argument, NULL, 8},
270 #endif
271 {"port", required_argument, NULL, 6},
272 {"rplay-port", required_argument, NULL, 6},
273 {"rptp-port", required_argument, NULL, 7},
274 {"servers", required_argument, NULL, 'S'},
275 {"timeout", required_argument, NULL, 't'},
276 {"user", required_argument, NULL, 22},
277 {"version", no_argument, NULL, 'v'},
278 {NULL, 0, NULL, 0}
279 };
280
281 #ifdef sun
282 #ifdef __STDC__
283 extern int gethostname(char *name, int namelen);
284 #else
285 extern int gethostname( /* char *name, int namelen */ );
286 #endif
287 #endif
288
289 extern char *ctime();
290 #ifdef __STDC__
291 static void do_option_file(char *option_file);
292 static void do_option(int option_value);
293 #else
294 static void do_option_file( /* char *option_file */ );
295 static void do_option( /* int option_value */ );
296 #endif
297 static void doit();
298 static void handle_signals();
299 static void handle_sighup();
300 static void handle_sigint();
301 static void handle_sigchld();
302 static void reset();
303 static void audio_test();
304
305 #if defined(HAVE_CDROM) || defined(HAVE_HELPERS)
306 #ifdef __STDC__
307 void rplayd_pipe_read( /* fd_set *rfds */ );
308 #else
309 void rplayd_pipe_read( /* fd_set *rfds */ );
310 #endif
311 #endif
312
313 #ifdef AUTH
314 #define OPTIONS "A:C:D:F:S:b:B:c:df:l:L:Nnr:R:s:t:T:vi:H:"
315 #else
316 #define OPTIONS "A:C:D:F:S:b:B:c:df:l:L:Nnr:R:s:t:T:vi:"
317 #endif
318
319 #ifdef __STDC__
320 main(int argc, char **argv)
321 #else
322 main(argc, argv)
323 int argc;
324 char **argv;
325 #endif
326 {
327 int c;
328 struct hostent *hp;
329 struct in_addr addr;
330 char *rplaydrc;
331
332 starttime = time(0);
333
334 progname = argv[0];
335
336 /* no need to keep stdin around */
337 close(0);
338
339 servers_file = tilde_expand(RPLAY_SERVERS);
340 #ifdef HAVE_HELPERS
341 helpers_file = tilde_expand(RPLAY_HELPERS);
342 #endif /* HAVE_HELPERS */
343 sounds_file = tilde_expand(RPLAY_CONF);
344 #ifdef AUTH
345 hosts_file = tilde_expand(RPLAY_HOSTS);
346 #endif /* AUTH */
347 cache_dir = tilde_expand(RPLAY_CACHE);
348 log_file = tilde_expand(RPLAY_LOG);
349
350 #ifdef RPLAYD_ALWAYS_LOG
351 report_level = RPLAYD_LOG_LEVEL;
352 if (report_level != REPORT_NONE)
353 {
354 logging++;
355 }
356 #else
357 report_level = REPORT_ERROR;
358 #endif
359
360 /* Parse RPLAYDRC. */
361 rplaydrc = tilde_expand(RPLAYDRC);
362 if (rplaydrc)
363 {
364 struct stat st;
365
366 if (stat(rplaydrc, &st) == 0 && S_ISREG(st.st_mode))
367 {
368 do_option_file(rplaydrc);
369 }
370
371 free(rplaydrc);
372 }
373
374 /* Parse command line options. */
375 while ((c = getopt_long(argc, argv, OPTIONS, longopts, 0)) != -1)
376 {
377 do_option(c);
378 }
379
380 if (audio_test_mode)
381 {
382 report_level = 0; /* don't want debug output */
383 audio_test();
384 done(0);
385 }
386
387 if (run_as_group && *run_as_group)
388 {
389 struct group *gr = getgrnam(run_as_group);
390 if (!gr)
391 {
392 fprintf(stderr, "Unknown group `%s'.\n", run_as_group);
393 done(1);
394 }
395 if (setgid(gr->gr_gid) < 0)
396 {
397 report(REPORT_ERROR, "setgid: %s\n", sys_err_str(errno));
398 done(1);
399 }
400 report(REPORT_DEBUG, "running as group %s (%d)\n", run_as_group, gr->gr_gid);
401 }
402 if (run_as_user && *run_as_user)
403 {
404 struct passwd *pw = getpwnam(run_as_user);
405 if (!pw)
406 {
407 fprintf(stderr, "Unknown user `%s'.\n", run_as_user);
408 done(1);
409 }
410 if (setuid(pw->pw_uid) < 0)
411 {
412 report(REPORT_ERROR, "setuid: %s\n", sys_err_str(errno));
413 done(1);
414 }
415 report(REPORT_DEBUG, "running as user %s (%d)\n", run_as_user, pw->pw_uid);
416 }
417
418 if (debug)
419 {
420 logging = 0;
421 do_fork = 0;
422 }
423
424 /* Fork */
425 if (do_fork)
426 {
427 int pid = fork();
428 if (pid != 0)
429 {
430 exit(0);
431 }
432 }
433
434 if (gethostname(hostname, sizeof(hostname)) < 0)
435 {
436 report(REPORT_ERROR, "gethostname: %s\n", sys_err_str(errno));
437 done(1);
438 }
439
440 hp = gethostbyname(hostname);
441 if (hp == NULL)
442 {
443 report(REPORT_ERROR, "gethostbyname: cannot resolve hostname: %s\n", hostname);
444 done(1);
445 }
446 memcpy((char *) &addr, (char *) hp->h_addr, hp->h_length);
447 hostaddr = strdup(inet_ntoa(addr));
448
449 rplayd_pid = getpid();
450
451 handle_signals();
452
453 /* Call all the initialization routines. */
454 rplayd_init();
455 #ifdef HAVE_HELPERS
456 helper_read(helpers_file);
457 #endif /* HAVE_HELPERS */
458 sound_read(sounds_file);
459 cache_init(cache_dir);
460 cache_read();
461 #ifdef AUTH
462 host_read(hosts_file);
463 #endif /* AUTH */
464 server_read(servers_file);
465 spool_init();
466 timer_init();
467 if (rplayd_audio_init() < 0)
468 {
469 /* Disable audio. */
470 audio_enabled = 0;
471 report(REPORT_DEBUG, "audio disabled\n");
472 }
473 else
474 {
475 /* Save the initial audio configuration. */
476 default_sample_rate = rplay_audio_sample_rate;
477 default_precision = rplay_audio_precision;
478 default_channels = rplay_audio_channels;
479 default_format = rplay_audio_format;
480 default_port = rplay_audio_port;
481
482 /* Close audio device to avoid hogging it. */
483 rplay_audio_close();
484 }
485
486 #ifdef AUTH
487 report(REPORT_DEBUG, "authentication %s\n", auth_enabled ? "enabled" : "disabled");
488 #else
489 report(REPORT_DEBUG, "no authentication\n");
490 #endif
491
492 if (forward)
493 {
494 report(REPORT_DEBUG, "forwarding sounds to %s\n", forward);
495 }
496
497 report(REPORT_DEBUG, "%s rplayd %s ready.\n", hostname, RPLAY_VERSION);
498
499 doit();
500 }
501
502 #ifdef __STDC__
503 static void
504 do_option_file(char *option_file)
505 #else
506 static void
507 do_option_file(option_file)
508 char *option_file;
509 #endif
510 {
511 int first, c;
512 char buf[BUFSIZ], *p;
513 FILE *fp;
514 int saved_optind;
515 char *argv[1024]; /* that should be enough */
516 int argc = 0;
517
518 fp = fopen(option_file, "r");
519 if (fp == NULL)
520 {
521 report(REPORT_ERROR, "cannot open `%s'.\n", option_file);
522 return;
523 }
524
525 argv[argc++] = progname;
526
527 tilde_additional_prefixes = (char **) xmalloc(2 * sizeof(char *));
528 tilde_additional_prefixes[0] = "=~";
529 tilde_additional_prefixes[1] = (char *) NULL;
530
531 while (fgets(buf, sizeof(buf), fp))
532 {
533 switch (buf[0])
534 {
535 case '#':
536 case ' ':
537 case '\n':
538 case '\t':
539 continue;
540 }
541
542 first = 1;
543 while (p = (char *) strtok(first ? buf : NULL, " \t\n"))
544 {
545 first = 0;
546 argv[argc++] = tilde_expand(p);
547 }
548 }
549
550 fclose(fp);
551 free((char *) tilde_additional_prefixes);
552 tilde_additional_prefixes = NULL;
553 argv[argc] = NULL;
554
555 saved_optind = optind; /* save optind */
556 optind = 1; /* reset optind */
557 while ((c = getopt_long(argc, argv, OPTIONS, longopts, 0)) != -1)
558 {
559 do_option(c);
560 }
561 optind = saved_optind; /* restore optind */
562 }
563
564
565 #ifdef __STDC__
566 static void
567 do_option(int option_value)
568 #else
569 static void
570 do_option(option_value)
571 int option_value;
572 #endif
573 {
574 static int within_file;
575 int i;
576 char *p;
577
578 switch (option_value)
579 {
580 case 0:
581 /* getopt has processed a long-named option -- do nothing */
582 break;
583
584 case 1:
585 /* --audio-channels */
586
587 optional_channels = atoi(optarg);
588 if (optional_channels != 1 && optional_channels != 2)
589 {
590 usage();
591 done(1);
592 }
593 break;
594
595 case 2:
596 /* --help */
597 usage();
598 done(1);
599
600 case 3:
601 /* --audio-format */
602 optional_format = string_to_audio_format(optarg);
603 if (optional_format < 0)
604 {
605 usage();
606 done(1);
607 }
608 break;
609
610 case 4:
611 /* --memory-cache-size */
612 sound_cache_max_size = atoi(optarg);
613 break;
614
615 case 5:
616 /* --memory-cache-sound-size */
617 sound_cache_max_sound_size = atoi(optarg);
618 break;
619
620 case 6:
621 /* --port or --rplay-port */
622 rplay_port = atoi(optarg);
623 break;
624
625 case 7:
626 /* --rptp-port */
627 rptp_port = atoi(optarg);
628 break;
629
630 #ifdef OTHER_RPLAY_PORTS
631 case 8:
632 /* --other-rplay-port */
633 other_rplay_port = atoi(optarg);
634 break;
635
636 case 9:
637 /* --other-rptp-port */
638 other_rptp_port = atoi(optarg);
639 break;
640 #endif /* OTHER_RPTP_PORTS */
641
642 case 10:
643 /* --audio-port */
644 if (strstr(optarg, "none"))
645 {
646 SET_BIT(optional_port, RPLAY_AUDIO_PORT_NONE);
647 }
648 if (strstr(optarg, "speaker"))
649 {
650 SET_BIT(optional_port, RPLAY_AUDIO_PORT_SPEAKER);
651 }
652 if (strstr(optarg, "headphone"))
653 {
654 SET_BIT(optional_port, RPLAY_AUDIO_PORT_HEADPHONE);
655 }
656 if (strstr(optarg, "lineout"))
657 {
658 SET_BIT(optional_port, RPLAY_AUDIO_PORT_LINEOUT);
659 }
660 break;
661
662 case 11:
663 do_option_file(optarg);
664 break;
665
666 case 12: /* --cache-remove */
667 cache_remove++;
668 break;
669
670 case 13: /* --audio-info-ulaw, --info-ulaw */
671 optional_format = RPLAY_FORMAT_ULAW;
672 optional_sample_rate = 8000;
673 optional_precision = 8;
674 optional_channels = 1;
675 break;
676
677 #ifdef HAVE_CDROM
678 case 14: /* cdrom0 */
679 for (i = 0; i < MAX_CDROMS; i++)
680 {
681 if (strncmp(cdrom_table[i].name, "cdrom0", 6) == 0)
682 {
683 cdrom_table[i].device = optarg;
684 break;
685 }
686 }
687 break;
688
689 case 15: /* cdrom1 */
690 for (i = 0; i < MAX_CDROMS; i++)
691 {
692 if (strncmp(cdrom_table[i].name, "cdrom1", 6) == 0)
693 {
694 cdrom_table[i].device = optarg;
695 break;
696 }
697 }
698 break;
699
700 case 16: /* cdrom2 */
701 for (i = 0; i < MAX_CDROMS; i++)
702 {
703 if (strncmp(cdrom_table[i].name, "cdrom2", 6) == 0)
704 {
705 cdrom_table[i].device = optarg;
706 break;
707 }
708 }
709 break;
710
711 case 17: /* cdrom3 */
712 for (i = 0; i < MAX_CDROMS; i++)
713 {
714 if (strncmp(cdrom_table[i].name, "cdrom3", 6) == 0)
715 {
716 cdrom_table[i].device = optarg;
717 break;
718 }
719 }
720 break;
721 #endif /* HAVE_CDROM */
722
723 #ifdef HAVE_OSS
724 case 18: /* audio-fragsize */
725 optional_fragsize = atoi(optarg);
726 break;
727 #endif /* HAVE_OSS */
728
729 case 19: /* fork */
730 do_fork++;
731 break;
732
733 case 20: /* helpers */
734 helpers_file = optarg;
735 break;
736
737 case 21: /* group */
738 run_as_group = optarg;
739 break;
740
741 case 22: /* user */
742 run_as_user = optarg;
743 break;
744
745 case 'A':
746 rplay_audio_device = optarg;
747 break;
748
749 #ifndef HAVE_OSS
750 case 'b':
751 rplay_audio_bufsize = atoi(optarg);
752 break;
753 #endif /* !HAVE_OSS */
754
755 case 'B':
756 optional_precision = atoi(optarg);
757 if (optional_precision != 8 && optional_precision != 16)
758 {
759 usage();
760 done(1);
761 }
762 break;
763
764 case 'c':
765 rplay_audio_timeout = atoi(optarg);
766 break;
767
768 case 'C':
769 sounds_file = optarg;
770 break;
771
772 case 'd':
773 debug++;
774 report_level = REPORT_DEBUG;
775 break;
776
777 case 'D':
778 cache_dir = optarg;
779 break;
780
781 case 'f':
782 forward = optarg;
783 break;
784
785 case 'F':
786 rplay_audio_flush_timeout = atoi(optarg);
787 break;
788
789 #ifdef AUTH
790 case 'H':
791 hosts_file = optarg;
792 break;
793 #endif /* AUTH */
794
795 case 'i': /* --audio-info, --info */
796 /* Example: ulaw,8000,8,1 */
797 p = strtok(optarg, ", ");
798 if (p)
799 optional_format = string_to_audio_format(p);
800 p = strtok(NULL, ",");
801 if (p)
802 optional_sample_rate = atoi(p);
803 p = strtok(NULL, ",");
804 if (p)
805 optional_precision = atoi(p);
806 p = strtok(NULL, ",");
807 if (p)
808 optional_channels = atoi(p);
809 break;
810
811 case 'l':
812 logging++;
813 report_level = atoi(optarg);
814 break;
815
816 case 'L':
817 log_file = optarg;
818 break;
819
820 case 'n':
821 inetd = 0;
822 break;
823
824 case 'N':
825 audio_enabled = 0;
826 break;
827
828 case 'r':
829 rplay_audio_rate = atoi(optarg);
830 break;
831
832 case 'R':
833 optional_sample_rate = atoi(optarg);
834 break;
835
836 case 's':
837 cache_max_size = atoi(optarg);
838 break;
839
840 case 'S':
841 servers_file = optarg;
842 break;
843
844 case 't':
845 rplayd_timeout = atoi(optarg);
846 break;
847
848 case 'T':
849 rptp_timeout = atoi(optarg);
850 break;
851
852 case 'v':
853 printf("rplay %s\n", RPLAY_VERSION);
854 done(0);
855 break;
856
857 default:
858 fprintf(stderr, "Try `%s --help' for more information.\n", progname);
859 done(1);
860 }
861 }
862
863 /*
864 * The main loop of rplayd.
865 */
866 static void
867 doit()
868 {
869 int nfds, icount = 0;
870 int idle = 1;
871 struct timeval select_timeout;
872 fd_set rfds, wfds;
873 SPOOL *sp;
874 /* Initialize the fd_sets before starting. */
875 FD_ZERO(&rfds);
876 FD_ZERO(&wfds);
877 FD_ZERO(&read_mask);
878 FD_ZERO(&write_mask);
879 FD_SET(rplay_fd, &read_mask);
880 FD_SET(rptp_fd, &read_mask);
881 #ifdef OTHER_RPLAY_PORTS
882 FD_SET(other_rplay_fd, &read_mask);
883 FD_SET(other_rptp_fd, &read_mask);
884 #endif
885
886 for (;;)
887 {
888 if (need_to_reset)
889 {
890 reset();
891 need_to_reset = 0;
892 }
893
894 if (audio_enabled)
895 {
896 if (timer_enabled)
897 {
898 if (!spool_nplaying)
899 {
900 timer_stop();
901
902 rplay_audio_size = 0;
903 if (rplay_audio_flush_timeout == -1)
904 {
905 report(REPORT_DEBUG, "flushing %s\n", rplay_audio_device);
906 rplay_audio_flush();
907 }
908
909 /* Notify the final zero levels. */
910 rplay_audio_left_level = 0;
911 rplay_audio_right_level = 0;
912 if (connection_want_level_notify)
913 {
914 connection_notify(0, NOTIFY_LEVEL, 1); /* force notification */
915 }
916 }
917 else
918 {
919 /* keep trying to open */
920 if (!rplay_audio_isopen())
921 {
922 rplay_audio_open();
923 }
924
925 if (connection_level_notify)
926 {
927 connection_notify(0, NOTIFY_LEVEL, 0);
928 connection_level_notify = 0;
929 }
930
931 for (sp = spool; sp; sp = sp->next)
932 {
933 if (sp->notify_position)
934 {
935 connection_notify(0, NOTIFY_POSITION, sp);
936 sp->notify_position = 0;
937 }
938 }
939
940 if (monitor_count && monitor_update)
941 {
942 connection_monitor_continue();
943 monitor_update = 0;
944 }
945 }
946 }
947 else if (spool_nplaying)
948 {
949 if (!rplay_audio_isopen())
950 {
951 report(REPORT_DEBUG, "opening %s\n", rplay_audio_device);
952 if (rplay_audio_open() < 0)
953 {
954 report(REPORT_ERROR,
955 "rplay_audio_open: %s: %s (will keep trying)\n",
956 rplay_audio_device, sys_err_str(errno));
957 }
958 }
959 /* start the timer even if the audio device isn't open */
960 timer_start(curr_rate);
961 }
962
963 if (spool_needs_update)
964 {
965 spool_update();
966 }
967 }
968
969 if (connections)
970 {
971 connection_check_timeout();
972 }
973
974 rfds = read_mask;
975 wfds = write_mask;
976
977 if (idle)
978 {
979 report(REPORT_DEBUG, "entering idle mode\n");
980 select_timeout.tv_sec = rplayd_timeout;
981 select_timeout.tv_usec = 0;
982 #ifdef __hpux
983 nfds = select(FD_SETSIZE, (int *) &rfds, (int *) &wfds, 0,
984 rplayd_timeout ? &select_timeout : 0);
985 #else
986 nfds = select(FD_SETSIZE, &rfds, &wfds, 0,
987 rplayd_timeout ? &select_timeout : 0);
988 #endif
989 if (nfds == 0)
990 {
991 done(0);
992 }
993
994 #ifdef AUTO_REREAD
995 #ifdef HAVE_HELPERS
996 helper_stat(helpers_file);
997 #endif /* HAVE_HELPERS */
998 sound_stat(sounds_file);
999 #ifdef AUTH
1000 host_stat(hosts_file);
1001 #endif /* AUTH */
1002 server_stat(servers_file);
1003 #endif /* AUTO_REREAD */
1004
1005 idle = 0;
1006 icount = 0;
1007 }
1008 else
1009 {
1010 select_timeout.tv_sec = 1;
1011 select_timeout.tv_usec = 0;
1012 #ifdef __hpux
1013 nfds = select(FD_SETSIZE, (int *) &rfds, (int *) &wfds, 0,
1014 &select_timeout);
1015 #else
1016 nfds = select(FD_SETSIZE, &rfds, &wfds, 0,
1017 &select_timeout);
1018 #endif
1019 }
1020
1021 switch (nfds)
1022 {
1023 case -1:
1024 if (errno != EINTR && errno != EAGAIN)
1025 {
1026 report(REPORT_ERROR, "select: %s\n", sys_err_str(errno));
1027 done(1);
1028 }
1029 break;
1030
1031 case 0:
1032 icount++;
1033
1034 /*
1035 * Timeouts that occur when no sounds are playing.
1036 */
1037 if (!spool_nplaying && !rplay_audio_size)
1038 {
1039 if (audio_enabled && rplay_audio_timeout && icount == rplay_audio_timeout
1040 && rplay_audio_isopen())
1041 {
1042 report(REPORT_DEBUG, "closing %s\n", rplay_audio_device);
1043 rplay_audio_close();
1044 }
1045 if (audio_enabled && rplay_audio_flush_timeout > 0
1046 && icount == rplay_audio_flush_timeout && rplay_audio_isopen())
1047 {
1048 report(REPORT_DEBUG, "flushing %s\n", rplay_audio_device);
1049 rplay_audio_flush();
1050 }
1051 if (sound_cleanup_timeout && icount == sound_cleanup_timeout)
1052 {
1053 sound_cleanup();
1054 }
1055 if (buffer_cleanup_timeout && icount == buffer_cleanup_timeout)
1056 {
1057 buffer_cleanup();
1058 }
1059 if (spool_cleanup_timeout && icount == spool_cleanup_timeout)
1060 {
1061 spool_cleanup();
1062 }
1063
1064 /*
1065 * See if rplayd should enter idle mode.
1066 * Idle mode should only be entered when *nothing*
1067 * is happening.
1068 */
1069 if (icount >= rplay_audio_timeout
1070 && connection_idle()
1071 && icount >= rplay_audio_flush_timeout
1072 && icount >= sound_cleanup_timeout
1073 && icount >= buffer_cleanup_timeout
1074 && icount >= spool_cleanup_timeout)
1075 {
1076 idle = 1;
1077 if (monitor_buffers)
1078 {
1079 if (!monitor_count)
1080 {
1081 report(REPORT_DEBUG, "cleaning monitor buffers - %d bytes\n",
1082 monitor_total_bytes);
1083 monitor_cleanup();
1084 }
1085 else
1086 {
1087 report(REPORT_DEBUG, "keeping monitor buffers - %d bytes\n",
1088 monitor_total_bytes);
1089 }
1090 }
1091 }
1092 }
1093 break;
1094
1095 default:
1096 if (FD_ISSET(rplay_fd, &rfds))
1097 {
1098 rplayd_read(rplay_fd);
1099 }
1100 #ifdef OTHER_RPLAY_PORTS
1101 if (FD_ISSET(other_rplay_fd, &rfds))
1102 {
1103 rplayd_read(other_rplay_fd);
1104 }
1105 #endif
1106 connection_update(&rfds, &wfds);
1107 #if defined(HAVE_CDROM) || defined(HAVE_HELPERS)
1108 rplayd_pipe_read(&rfds);
1109 #endif
1110 icount = 0;
1111 break;
1112 }
1113 }
1114 }
1115
1116
1117 /*
1118 * Set up the sockets to receive RPLAY packets and RPTP connections.
1119 */
1120 void
1121 rplayd_init()
1122 {
1123 struct sockaddr_in s;
1124 struct servent *sp;
1125
1126 if (inetd)
1127 {
1128 rplay_fd = 0;
1129 #ifdef OTHER_RPLAY_PORTS
1130 /* Try to figure out which port is being used by inetd. */
1131 sp = getservbyname("rplay", "udp");
1132 if (sp == NULL)
1133 {
1134 report(REPORT_ERROR, "can't find rplay/udp service\n");
1135 done(1);
1136 }
1137
1138 if (ntohs(sp->s_port) == RPLAY_PORT)
1139 {
1140 other_rplay_fd = udp_socket(other_rplay_port ? other_rplay_port : OLD_RPLAY_PORT);
1141 }
1142 else
1143 {
1144 other_rplay_fd = udp_socket(other_rplay_port ? other_rplay_port : RPLAY_PORT);
1145 }
1146 #endif
1147 }
1148 else
1149 {
1150 rplay_fd = udp_socket(rplay_port ? rplay_port : RPLAY_PORT);
1151 #ifdef OTHER_RPLAY_PORTS
1152 other_rplay_fd = udp_socket(other_rplay_port ? other_rplay_port : OLD_RPLAY_PORT);
1153 #endif
1154 }
1155
1156 /*
1157 * Make the RPLAY sockets non-blocking.
1158 */
1159 fd_nonblock(rplay_fd);
1160 #ifdef OTHER_RPLAY_PORTS
1161 fd_nonblock(other_rplay_fd);
1162 #endif
1163
1164 if (forward)
1165 {
1166 unsigned long addr = inet_addr(forward);
1167
1168 memset((char *) &s, 0, sizeof(s));
1169 s.sin_family = AF_INET;
1170 s.sin_port = htons(RPLAY_PORT);
1171
1172 if (addr == 0xffffffff)
1173 {
1174 struct hostent *hp = gethostbyname(forward);
1175 if (hp == NULL)
1176 {
1177 report(REPORT_ERROR, "gethostbyname: %s unknown host\n", forward);
1178 done(1);
1179 }
1180 memcpy((char *) &s.sin_addr.s_addr, (char *) hp->h_addr, hp->h_length);
1181 }
1182 else
1183 {
1184 memcpy((char *) &s.sin_addr.s_addr, (char *) &addr, sizeof(addr));
1185 }
1186
1187 forward_fd = socket(AF_INET, SOCK_DGRAM, 0);
1188 if (forward_fd < 0)
1189 {
1190 report(REPORT_ERROR, "socket: %s\n", sys_err_str(errno));
1191 done(1);
1192 }
1193
1194 if (connect(forward_fd, (struct sockaddr *) &s, sizeof(s)) < 0)
1195 {
1196 report(REPORT_ERROR, "connect: %s\n", sys_err_str(errno));
1197 done(1);
1198 }
1199 }
1200
1201 rptp_fd = tcp_socket(rptp_port ? rptp_port : RPTP_PORT);
1202 #ifdef OTHER_RPLAY_PORTS
1203 other_rptp_fd = tcp_socket(other_rptp_port ? other_rptp_port : OLD_RPTP_PORT);
1204 #endif
1205
1206 /*
1207 * Make the RPTP sockets non-blocking.
1208 */
1209 fd_nonblock(rptp_fd);
1210 #ifdef OTHER_RPLAY_PORTS
1211 fd_nonblock(other_rptp_fd);
1212 #endif
1213 }
1214
1215 int
1216 rplayd_audio_init()
1217 {
1218 int sample_size;
1219
1220 if (!audio_enabled)
1221 {
1222 return -1;
1223 }
1224
1225 if (optional_sample_rate && optional_sample_rate > MAX_SAMPLE_RATE)
1226 {
1227 optional_sample_rate = MAX_SAMPLE_RATE;
1228 }
1229
1230 /* Call rplay_audio_init() which sets all the rplay_audio variables. */
1231 rplay_audio_table = NULL; /* will be set by rplay_audio_init() */
1232 if (rplay_audio_init() < 0)
1233 {
1234 return -1;
1235 }
1236
1237 /* XXX */
1238 optional_sample_rate = rplay_audio_sample_rate;
1239 optional_precision = rplay_audio_precision;
1240 optional_channels = rplay_audio_channels;
1241 optional_format = rplay_audio_format;
1242 optional_port = rplay_audio_port;
1243
1244 sample_size = (rplay_audio_precision >> 3) * rplay_audio_channels;
1245
1246 if (rplay_audio_match || rplay_audio_rate == 0)
1247 {
1248 rplay_audio_rate = RPLAY_AUDIO_RATE;
1249 }
1250 curr_rate = rplay_audio_rate;
1251
1252 #ifndef HAVE_OSS
1253 /* Click prevention: Make sure curr_rate won't split up samples.
1254 If it will, it's easiest right now to modify the rate and not
1255 the sample_rate. */
1256
1257 if (rplay_audio_sample_rate % curr_rate)
1258 {
1259 while (rplay_audio_sample_rate % curr_rate)
1260 {
1261 curr_rate--; /* Slow it down. */
1262 }
1263
1264 report(REPORT_DEBUG, "changed curr_rate from %d to %d\n",
1265 rplay_audio_rate, curr_rate);
1266 }
1267
1268 if (rplay_audio_match || rplay_audio_bufsize == 0)
1269 {
1270 rplay_audio_bufsize = rplay_audio_sample_rate / curr_rate * sample_size;
1271 }
1272 curr_bufsize = rplay_audio_bufsize;
1273
1274 /* More click prevention: Make sure curr_bufsize won't split up samples.
1275 If it will, curr_bufsize should be increased, not decreased. */
1276
1277 if (curr_bufsize % sample_size)
1278 {
1279 while (curr_bufsize % sample_size)
1280 {
1281 curr_bufsize++; /* Add a little. */
1282 }
1283
1284 report(REPORT_DEBUG, "changed curr_bufsize from %d to %d\n",
1285 rplay_audio_bufsize, curr_bufsize);
1286 }
1287 #endif /* !HAVE_OSS */
1288
1289 /*
1290 * Create the audio buffer which holds at most 1 second of 16-bit
1291 * stereo audio data sampled at MAX_SAMPLE_RATE Hz.
1292 */
1293 max_rplay_audio_bufsize = MAX_SAMPLE_RATE * 2 /* 16-bit */ * 2 /* stereo */ ;
1294 if (rplay_audio_buf)
1295 {
1296 free((char *) rplay_audio_buf);
1297 }
1298 rplay_audio_buf = (char *) malloc(max_rplay_audio_bufsize);
1299 if (rplay_audio_buf == NULL)
1300 {
1301 report(REPORT_ERROR, "cannot allocate rplay_audio_buf, size=%d\n",
1302 max_rplay_audio_bufsize);
1303 done(1);
1304 }
1305
1306 /*
1307 * Update the volume of the audio device.
1308 */
1309 rplay_audio_volume = rplay_audio_get_volume();
1310 if (rplay_audio_volume < 0)
1311 {
1312 rplay_audio_volume = 0;
1313 }
1314
1315 report(REPORT_DEBUG, "\
1316 audio: bits=%d sample-rate=%d channels=%d format=%s volume=%d port-mask=%0x\n",
1317 rplay_audio_precision, rplay_audio_sample_rate, rplay_audio_channels,
1318 audio_format_to_string(rplay_audio_format), rplay_audio_volume, rplay_audio_port);
1319
1320 #ifdef HAVE_OSS
1321 report(REPORT_DEBUG, "timer: rate=%d, min bytes/sec=%d\n", curr_rate,
1322 (rplay_audio_sample_rate / curr_rate * sample_size));
1323 #else
1324 report(REPORT_DEBUG, "timer: rate=%d, bufsize=%d\n", curr_rate, curr_bufsize);
1325 #endif
1326
1327 return 0;
1328 }
1329
1330 /* Attempt to match the audio device configuration with the sound
1331 in `sp'. Currenly only the sample-rate is matched. */
1332 #ifdef __STDC__
1333 void
1334 rplayd_audio_match(SPOOL *match_sp)
1335 #else
1336 void
1337 rplayd_audio_match(match_sp)
1338 SPOOL *match_sp;
1339 #endif
1340 {
1341 if (rplay_audio_match
1342 && spool_nplaying == 0 /* Only is nothing else is playing. */
1343 && match_sp->sample_rate != rplay_audio_sample_rate)
1344 {
1345 RPLAY_AUDIO_TABLE *t;
1346 SPOOL *sp;
1347 int prev_sample_rate;
1348
1349 /* Find the closest matching table entry. */
1350 optional_sample_rate = match_sp->sample_rate;
1351 prev_sample_rate = rplay_audio_table ? rplay_audio_table->sample_rate : 0;
1352 for (t = rplay_audio_table; t; t++)
1353 {
1354 if (t->sample_rate == 0
1355 || optional_sample_rate < prev_sample_rate)
1356 {
1357 optional_sample_rate = prev_sample_rate;
1358 break;
1359 }
1360 else if (optional_sample_rate >= prev_sample_rate
1361 && optional_sample_rate <= t->sample_rate)
1362 {
1363 if ((t->sample_rate - optional_sample_rate)
1364 > (optional_sample_rate - prev_sample_rate))
1365 {
1366 optional_sample_rate = prev_sample_rate;
1367 }
1368 else
1369 {
1370 optional_sample_rate = t->sample_rate;
1371 }
1372 break;
1373 }
1374 prev_sample_rate = t->sample_rate;
1375 }
1376
1377 report(REPORT_DEBUG, "matched %d with %d\n", match_sp->sample_rate,
1378 optional_sample_rate);
1379
1380 if (rplayd_audio_init() < 0)
1381 {
1382 /* That sample rate didn't work -- Use the default instead. */
1383 optional_sample_rate = default_sample_rate;
1384 rplayd_audio_init();
1385 }
1386 if (timer_enabled)
1387 {
1388 timer_restart(curr_rate);
1389 }
1390
1391 /* Update the spool entries with the new sample-rate. */
1392 for (sp = spool; sp; sp = sp->next)
1393 {
1394 sp->sample_factor = (double) sp->sample_rate / (double) rplay_audio_sample_rate;
1395 }
1396 }
1397 }
1398
1399 monitor_alloc()
1400 {
1401 if (!monitor_buffers)
1402 {
1403 BUFFER *b, *end;
1404 monitor_buffers = buffer_alloc(monitor_total_bytes, BUFFER_KEEP);
1405 for (b = monitor_buffers; b; b = b->next)
1406 {
1407 /* these will always be full buffers */
1408 b->nbytes = BUFFER_SIZE;
1409 if (!b->next)
1410 {
1411 end = b;
1412 }
1413 }
1414 /* make the list of buffer cyclic */
1415 end->next = monitor_buffers;
1416 }
1417
1418 monitor_update = 0;
1419 }
1420
1421 monitor_cleanup()
1422 {
1423 BUFFER *b, *b_next;
1424
1425 for (b = monitor_buffers; b->next != monitor_buffers; b = b->next);
1426 b->next = NULL;
1427 buffer_dealloc(monitor_buffers, 1);
1428 monitor_buffers = NULL;
1429 }
1430
1431
1432 /*
1433 * Write nbytes of audio data from the audio buffer to the audio device.
1434 */
1435 #ifdef __STDC__
1436 void
1437 rplayd_write(int nbytes)
1438 #else
1439 void
1440 rplayd_write(nbytes)
1441 int nbytes;
1442 #endif
1443 {
1444 int nwritten = 0;
1445
1446 /* Insert `nbytes' of audio data from the spool into `rplay_audio_buf'. */
1447 spool_process(rplay_audio_buf, nbytes);
1448
1449 /*
1450 * Write the audio data if the audio device is open.
1451 * Otherwise forget about the data.
1452 */
1453 if (rplay_audio_size && rplay_audio_isopen())
1454 {
1455 nwritten = rplay_audio_write(rplay_audio_buf, rplay_audio_size);
1456 #if 0
1457 report(REPORT_DEBUG, "audio_write: %d -> %d\n", rplay_audio_size, nwritten);
1458 #endif
1459 /* Flush after every write? */
1460 if (rplay_audio_flush_timeout == -2)
1461 {
1462 report(REPORT_DEBUG, "flushing %s\n", rplay_audio_device);
1463 rplay_audio_flush();
1464 }
1465 }
1466
1467 if (monitor_count)
1468 {
1469 char *buf = rplay_audio_buf;
1470 int size = rplay_audio_size;
1471 int n;
1472
1473 while (size)
1474 {
1475 n = MIN(BUFFER_SIZE - monitor_buffers->offset, size);
1476 memcpy(monitor_buffers->buf + monitor_buffers->offset, buf, n);
1477 monitor_buffers->offset += n;
1478 buf += n;
1479 size -= n;
1480
1481 if (monitor_buffers->offset == BUFFER_SIZE)
1482 {
1483 monitor_buffers = monitor_buffers->next;
1484 monitor_buffers->offset = 0;
1485 monitor_update++;
1486 }
1487 }
1488 }
1489
1490 rplay_audio_size = 0;
1491
1492 /* Flush when the audio stream has ended. */
1493 if (spool_nplaying == 0 && rplay_audio_size == 0)
1494 {
1495 if (rplay_audio_flush_timeout == -1)
1496 {
1497 report(REPORT_DEBUG, "flushing %s\n", rplay_audio_device);
1498 rplay_audio_flush();
1499 }
1500 }
1501 }
1502
1503 /*
1504 * Read RPLAY packets from the UDP socket.
1505 */
1506 #ifdef __STDC__
1507 void
1508 rplayd_read(int fd)
1509 #else
1510 void
1511 rplayd_read(fd)
1512 int fd;
1513 #endif
1514 {
1515 static char recv_buf[MAX_PACKET];
1516 char *packet;
1517 struct sockaddr_in f;
1518 int j, n, flen;
1519 RPLAY *rp;
1520
1521 for (j = 0; j < SPOOL_SIZE; j++)
1522 {
1523 flen = sizeof(f);
1524
1525 n = recvfrom(fd, recv_buf, sizeof(recv_buf), 0, (struct sockaddr *) &f, &flen);
1526 if (n <= 0)
1527 {
1528 continue;
1529 }
1530
1531 if (forward)
1532 {
1533 write(forward_fd, recv_buf, n);
1534 continue;
1535 }
1536
1537 #ifdef AUTH
1538 if (!host_access(f, HOST_EXECUTE))
1539 {
1540 if (!host_access(f, HOST_READ) && !host_access(f, HOST_WRITE))
1541 {
1542 report(REPORT_NOTICE, "%s permission denied\n", inet_ntoa(f.sin_addr));
1543 }
1544 continue;
1545 }
1546 #endif /* AUTH */
1547
1548 packet = recv_buf;
1549
1550 switch (*packet)
1551 {
1552 #ifdef OLD_RPLAY
1553 case OLD_RPLAY_PLAY:
1554 case OLD_RPLAY_STOP:
1555 case OLD_RPLAY_PAUSE:
1556 case OLD_RPLAY_CONTINUE:
1557 packet = rplay_convert(recv_buf);
1558 #endif /* OLD_RPLAY */
1559
1560 case RPLAY_PACKET_ID:
1561 rp = rplay_unpack(packet);
1562 if (rp == NULL)
1563 {
1564 report(REPORT_ERROR, "rplay_unpack: %s\n", rplay_errlist[rplay_errno]);
1565 break;
1566 }
1567
1568 switch (rp->command)
1569 {
1570 case RPLAY_PING:
1571 #ifdef DEBUG
1572 report(REPORT_DEBUG, "received a ping packet\n");
1573 #endif
1574 rplay_destroy(rp);
1575 break;
1576
1577 case RPLAY_PLAY:
1578 report(REPORT_INFO, "%s play %s\n",
1579 inet_ntoa(f.sin_addr),
1580 (char *) rplay_get(rp, RPLAY_SOUND, 0));
1581 rplayd_play(rp, f);
1582 break;
1583
1584 case RPLAY_STOP:
1585 report(REPORT_INFO, "%s stop %s\n",
1586 inet_ntoa(f.sin_addr),
1587 (char *) rplay_get(rp, RPLAY_SOUND, 0));
1588 rplayd_stop(rp, f);
1589 break;
1590
1591 case RPLAY_PAUSE:
1592 report(REPORT_INFO, "%s pause %s\n",
1593 inet_ntoa(f.sin_addr),
1594 (char *) rplay_get(rp, RPLAY_SOUND, 0));
1595 rplayd_pause(rp, f);
1596 break;
1597
1598 case RPLAY_CONTINUE:
1599 report(REPORT_INFO, "%s continue %s\n",
1600 inet_ntoa(f.sin_addr),
1601 (char *) rplay_get(rp, RPLAY_SOUND, 0));
1602 rplayd_continue(rp, f);
1603 break;
1604
1605 case RPLAY_RESET:
1606 report(REPORT_INFO, "received a reset packet\n");
1607 need_reset();
1608 rplay_destroy(rp);
1609 break;
1610
1611 case RPLAY_DONE:
1612 report(REPORT_INFO, "%s done %s\n",
1613 inet_ntoa(f.sin_addr),
1614 (char *) rplay_get(rp, RPLAY_SOUND, 0));
1615 rplayd_done(rp, f);
1616 break;
1617
1618 case RPLAY_PUT:
1619 report(REPORT_INFO, "%s put id=%d sequence=%d size=%d\n",
1620 inet_ntoa(f.sin_addr),
1621 rp->id,
1622 rp->sequence,
1623 rp->data_size);
1624 rplayd_put(rp, f);
1625 break;
1626 }
1627 break;
1628
1629 default:
1630 report(REPORT_ERROR, "unknown RPLAY packet received from %s\n",
1631 inet_ntoa(f.sin_addr));
1632 break;
1633 }
1634 }
1635 }
1636
1637 #if defined(HAVE_CDROM) || defined(HAVE_HELPERS)
1638 /* Read data from piped devices and insert it into the spool entry.
1639 This is done using rplayd main select loop for efficiency. */
1640 #ifdef __STDC__
1641 void
1642 rplayd_pipe_read(fd_set * rfds)
1643 #else
1644 void
1645 rplayd_pipe_read(rfds)
1646 fd_set *rfds;
1647 #endif
1648 {
1649 SPOOL *sp, *sp_next;
1650 extern BUFFER *sound_pipe_read(SINDEX *si);
1651
1652 for (sp = spool; sp; sp = sp_next)
1653 {
1654 sp_next = sp->next;
1655 if (!sp->si)
1656 {
1657 continue;
1658 }
1659
1660 #ifdef HAVE_CDROM
1661 if (sp->si->sound->type == SOUND_CDROM && FD_ISSET(sp->si->fd, rfds))
1662 {
1663 BUFFER *b = sound_pipe_read(sp->si);
1664 if (b)
1665 {
1666 spool_flow_insert(sp, b);
1667 }
1668 }
1669 #endif
1670
1671 #ifdef HAVE_HELPERS
1672 if (sp->si->sound->needs_helper && FD_ISSET(sp->si->fd, rfds))
1673 {
1674 BUFFER *b = sound_pipe_read(sp->si);
1675 if (b)
1676 {
1677 spool_flow_insert(sp, b);
1678 }
1679 }
1680 #endif
1681
1682 }
1683 }
1684 #endif
1685
1686 #ifdef __STDC__
1687 int
1688 rplayd_play(RPLAY *rp, struct sockaddr_in sin)
1689 #else
1690 int
1691 rplayd_play(rp, sin)
1692 RPLAY *rp;
1693 struct sockaddr_in sin;
1694 #endif
1695 {
1696 SPOOL *sp = NULL;
1697 int i, n;
1698 int start_sound, end_sound, merged_lists;
1699 RPLAY_ATTRS *attrs;
1700
1701 if (!audio_enabled)
1702 {
1703 return -1;
1704 }
1705
1706 /* If a list_name is specified, try to find a matching list. */
1707 if (*rp->list_name)
1708 {
1709 for (sp = spool; sp; sp = sp->next)
1710 {
1711 if (*sp->rp->list_name
1712 && strcmp(rp->list_name, sp->rp->list_name) == 0)
1713 {
1714 break;
1715 }
1716 }
1717 if (sp)
1718 {
1719 *(sp->rp->attrsp) = rp->attrs; /* Append the new attrs. */
1720 sp->rp->attrsp = rp->attrsp;
1721 start_sound = sp->rp->nsounds;
1722 sp->rp->nsounds += rp->nsounds;
1723 end_sound = sp->rp->nsounds;
1724 merged_lists = 1;
1725 }
1726 }
1727
1728 /* Find a spool entry. */
1729 if (!sp)
1730 {
1731 /* Check the priority level. rplayd will ignore all sounds
1732 with priorities less than rplay_priority_threshold. */
1733 if (rp->priority < rplay_priority_threshold)
1734 {
1735 report(REPORT_DEBUG, "sound at priority %d ignored\n",
1736 rp->priority);
1737 rplay_destroy(rp);
1738 return -1;
1739 }
1740
1741 sp = spool_next(rp->priority);
1742 if (!sp)
1743 {
1744 report(REPORT_DEBUG, "spool full\n");
1745 rplay_destroy(rp);
1746 return -1;
1747 }
1748 start_sound = 0;
1749 end_sound = rp->nsounds;
1750 merged_lists = 0;
1751 }
1752
1753 /* Gotta have attributes! */
1754 if (!rp->attrs)
1755 {
1756 report(REPORT_DEBUG, "`rp' missing attributes\n");
1757 rplay_destroy(rp);
1758 spool_destroy(sp);
1759 return -1;
1760 }
1761
1762 /* Add the sounds to the spool entry. */
1763 n = 0;
1764 for (i = start_sound, attrs = rp->attrs;
1765 i < end_sound && attrs;
1766 i++, attrs = attrs->next)
1767 {
1768 if (attrs->rptp_server)
1769 {
1770 SERVER *s;
1771
1772 s = (SERVER *) malloc(sizeof(SERVER));
1773 s->next = NULL;
1774 s->sin.sin_family = AF_INET;
1775 s->sin.sin_port = htons(attrs->rptp_server_port);
1776 s->sin.sin_addr.s_addr = inet_addr(attrs->rptp_server);
1777 sp->sound[i] = sound_lookup(attrs->sound,
1778 attrs->rptp_search ? SOUND_FIND : SOUND_DONT_FIND, s);
1779 }
1780 else
1781 {
1782 sp->sound[i] = sound_lookup(attrs->sound,
1783 attrs->rptp_search ? SOUND_FIND : SOUND_DONT_FIND, NULL);
1784 }
1785
1786 if (sp->sound[i] == NULL)
1787 {
1788 rplay_destroy(rp);
1789 spool_destroy(sp);
1790 return -1;
1791 }
1792
1793 if (sp->sound[i]->status != SOUND_READY)
1794 {
1795 n++;
1796 }
1797 }
1798
1799 /* Set-up the spool entry only if it's a new sound list. */
1800 if (!merged_lists)
1801 {
1802 sp->rp = rp;
1803 sp->curr_attrs = rp->attrs;
1804 sp->curr_sound = 0;
1805 sp->curr_count = sp->curr_attrs->count;
1806 sp->list_count = rp->count;
1807 sp->sin = sin;
1808
1809 if (n)
1810 {
1811 sp->state = SPOOL_WAIT;
1812 }
1813 else
1814 {
1815 spool_play(sp);
1816 }
1817 }
1818
1819 return sp->id;
1820 }
1821
1822 #ifdef __STDC__
1823 int
1824 rplayd_stop(RPLAY *rp, struct sockaddr_in sin)
1825 #else
1826 int
1827 rplayd_stop(rp, sin)
1828 RPLAY *rp;
1829 struct sockaddr_in sin;
1830 #endif
1831 {
1832 int n;
1833
1834 if (!audio_enabled)
1835 {
1836 rplay_destroy(rp);
1837 return -1;
1838 }
1839
1840 n = spool_match(rp, spool_stop, sin);
1841 rplay_destroy(rp);
1842
1843 return n > 0 ? 0 : -1;
1844 }
1845
1846 #ifdef __STDC__
1847 int
1848 rplayd_pause(RPLAY *rp, struct sockaddr_in sin)
1849 #else
1850 int
1851 rplayd_pause(rp, sin)
1852 RPLAY *rp;
1853 struct sockaddr_in sin;
1854 #endif
1855 {
1856 int n;
1857
1858 if (!audio_enabled)
1859 {
1860 rplay_destroy(rp);
1861 return -1;
1862 }
1863
1864 n = spool_match(rp, spool_pause, sin);
1865 rplay_destroy(rp);
1866
1867 return n > 0 ? 0 : -1;
1868 }
1869
1870 #ifdef __STDC__
1871 int
1872 rplayd_continue(RPLAY *rp, struct sockaddr_in sin)
1873 #else
1874 int
1875 rplayd_continue(rp, sin)
1876 RPLAY *rp;
1877 struct sockaddr_in sin;
1878 #endif
1879 {
1880 int n;
1881
1882 if (!audio_enabled)
1883 {
1884 rplay_destroy(rp);
1885 return -1;
1886 }
1887
1888 n = spool_match(rp, spool_continue, sin);
1889 rplay_destroy(rp);
1890
1891 return n > 0 ? 0 : -1;
1892 }
1893
1894 #ifdef __STDC__
1895 int
1896 rplayd_done(RPLAY *rp, struct sockaddr_in sin)
1897 #else
1898 int
1899 rplayd_done(rp, sin)
1900 RPLAY *rp;
1901 struct sockaddr_in sin;
1902 #endif
1903 {
1904 int n;
1905
1906 if (!audio_enabled)
1907 {
1908 rplay_destroy(rp);
1909 return -1;
1910 }
1911
1912 n = spool_match(rp, spool_done, sin);
1913 rplay_destroy(rp);
1914
1915 return n > 0 ? 0 : -1;
1916 }
1917
1918 #ifdef __STDC__
1919 int
1920 rplayd_put(RPLAY *rp, struct sockaddr_in sin)
1921 #else
1922 int
1923 rplayd_put(rp, sin)
1924 RPLAY *rp;
1925 struct sockaddr_in sin;
1926 #endif
1927 {
1928 BUFFER *b;
1929 SPOOL *sp;
1930
1931 if (!audio_enabled)
1932 {
1933 rplay_destroy(rp);
1934 return -1;
1935 }
1936
1937 /* Find the spool entry. */
1938 sp = spool_find(rp->id);
1939 if (!sp)
1940 {
1941 report(REPORT_DEBUG, "put: `%d' no such spool id\n", rp->id);
1942 rplay_destroy(rp);
1943 return -1;
1944 }
1945
1946 /* Create a buffer and insert the flow data. */
1947 b = buffer_create();
1948 b->nbytes = rp->data_size;
1949 memcpy(b->buf, rp->data, b->nbytes);
1950 spool_flow_insert(sp, b);
1951
1952 rplay_destroy(rp);
1953
1954 return 0;
1955 }
1956
1957 BUFFER *
1958 rplayd_status()
1959 {
1960 BUFFER *b;
1961
1962 b = buffer_create();
1963
1964 b->buf[0] = RPTP_OK;
1965 b->buf[1] = '\0';
1966
1967 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), "host=%s", hostname);
1968 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " version=%s", RPLAY_VERSION);
1969 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " uptime=%s", time2string(time(0) - starttime));
1970
1971 if (audio_enabled)
1972 {
1973 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " audio-bits=%d", rplay_audio_precision);
1974 #ifndef HAVE_OSS
1975 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " audio-bufsize=%d", rplay_audio_bufsize);
1976 #endif /* !HAVE_OSS */
1977 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " audio-byte-order=%s",
1978 byte_order_to_string(RPLAY_AUDIO_BYTE_ORDER));
1979 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " audio-channels=%d", rplay_audio_channels);
1980 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " audio-device=%s", rplay_audio_device);
1981 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " audio-format=%s",
1982 audio_format_to_string(rplay_audio_format));
1983 #ifdef HAVE_OSS
1984 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " audio-fragsize=%d", rplay_audio_fragsize);
1985 #endif /* HAVE_OSS */
1986 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " audio-port=%s",
1987 audio_port_to_string(rplay_audio_port));
1988 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " audio-rate=%d", rplay_audio_rate);
1989 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " audio-sample-rate=%d", rplay_audio_sample_rate);
1990 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " volume=%d", rplay_audio_volume);
1991 #ifndef HAVE_OSS
1992 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " curr-bufsize=%d", curr_bufsize);
1993 #endif /* !HAVE_OSS */
1994 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " curr-rate=%d", curr_rate);
1995 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " priority-threshold=%d",
1996 rplay_priority_threshold);
1997 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " audio-close=%d",
1998 rplay_audio_timeout);
1999 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), " audio-device-status=%s",
2000 rplay_audio_isopen() ? "open" : "closed");
2001 }
2002
2003 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), "\r\n");
2004
2005 b->nbytes = strlen(b->buf);
2006
2007 return b;
2008 }
2009
2010 void
2011 need_reset()
2012 {
2013 need_to_reset++;
2014 }
2015
2016 /* Reset rplayd's state. */
2017 static void
2018 reset()
2019 {
2020 int was_enabled = timer_enabled;
2021
2022 if (was_enabled)
2023 {
2024 timer_stop();
2025 }
2026 spool_init();
2027 connection_cleanup();
2028 #ifdef HAVE_HELPERS
2029 helper_reread(helpers_file);
2030 #endif /* HAVE_HELPERS */
2031 sound_reread(sounds_file);
2032 cache_init(cache_dir);
2033 cache_read();
2034 #ifdef AUTH
2035 host_reread(hosts_file);
2036 #endif /* AUTH */
2037 server_reread(servers_file);
2038 if (was_enabled)
2039 {
2040 timer_start(curr_rate);
2041 }
2042 }
2043
2044 static void
2045 handle_signals()
2046 {
2047 /* Ingore SIGPIPE since read will handle closed TCP connections.
2048 Handle SIGHUP, SIGINT, and SIGCHLD. */
2049 #ifdef HAVE_SIGSET
2050 sigset(SIGPIPE, SIG_IGN);
2051 sigset(SIGHUP, handle_sighup);
2052 sigset(SIGINT, handle_sigint);
2053 sigset(SIGCHLD, handle_sigchld);
2054 #else
2055 signal(SIGPIPE, SIG_IGN);
2056 signal(SIGHUP, handle_sighup);
2057 signal(SIGINT, handle_sigint);
2058 signal(SIGCHLD, handle_sigchld);
2059 #endif
2060 }
2061
2062
2063 static void
2064 handle_sighup()
2065 {
2066 report(REPORT_DEBUG, "received SIGHUP signal\n");
2067 need_reset();
2068 }
2069
2070 static void
2071 handle_sigint()
2072 {
2073 report(REPORT_DEBUG, "received SIGINT signal\n");
2074 done(0);
2075 }
2076
2077 static void
2078 handle_sigchld()
2079 {
2080 pid_t pid;
2081 SPOOL *sp;
2082
2083 #ifdef linux
2084 handle_signals();
2085 #endif
2086
2087 report(REPORT_DEBUG, "received SIGCHLD signal\n");
2088
2089 #ifdef HAVE_WAITPID
2090 pid = waitpid((pid_t) - 1, NULL, WNOHANG);
2091 #else /* not HAVE_WAITPID */
2092 pid = wait(NULL);
2093 #endif /* not HAVE_WAITPID */
2094 if (pid <= 0)
2095 {
2096 return;
2097 }
2098 }
2099
2100 static void
2101 audio_test()
2102 {
2103 static char *spin = "|\\-/";
2104 static int spin_pos;
2105
2106 printf("Testing supported audio configurations (this may take a long time)...\n");
2107 for (optional_sample_rate = 4000; optional_sample_rate <= 48000; optional_sample_rate += 5)
2108 for (optional_precision = 8; optional_precision <= 16; optional_precision += 8)
2109 for (optional_channels = 1; optional_channels <= 2; optional_channels++)
2110 for (optional_format = 1; optional_format <= 5; optional_format++)
2111 {
2112 if (!spin[spin_pos])
2113 {
2114 spin_pos = 0;
2115 }
2116 fprintf(stderr, "%c\r", spin[spin_pos++]);
2117
2118 if (rplay_audio_init() >= 0)
2119 {
2120 printf(" { %5d, %-24s, %2d, %d },\n",
2121 rplay_audio_sample_rate,
2122 rplay_audio_format == RPLAY_FORMAT_ULAW ? "RPLAY_FORMAT_ULAW" :
2123 rplay_audio_format == RPLAY_FORMAT_LINEAR_8 ? "RPLAY_FORMAT_LINEAR_8" :
2124 rplay_audio_format == RPLAY_FORMAT_ULINEAR_8 ? "RPLAY_FORMAT_ULINEAR_8" :
2125 rplay_audio_format == RPLAY_FORMAT_LINEAR_16 ? "RPLAY_FORMAT_LINEAR_16" :
2126 rplay_audio_format == RPLAY_FORMAT_ULINEAR_16 ? "RPLAY_FORMAT_ULINEAR_16" : "?",
2127 rplay_audio_precision,
2128 rplay_audio_channels);
2129 }
2130 }
2131 }
2132
2133 #ifdef __STDC__
2134 void
2135 done(int exit_value)
2136 #else
2137 void
2138 done(exit_value)
2139 int exit_value;
2140 #endif
2141 {
2142 SPOOL *sp;
2143
2144 /* #ifdef HAVE_CDROM */
2145 /* for (sp = spool; sp; sp = sp->next) */
2146 /* { */
2147 /* if (sp->si && sp->si->pid > 0) */
2148 /* { */
2149 /* report (REPORT_DEBUG, "killing process %d\n", sp->si->pid); */
2150 /* if (kill (sp->si->pid, SIGKILL) < 0) */
2151 /* { */
2152 /* report (REPORT_DEBUG, "kill %d: %s\n", sp->si->pid, sys_err_str (errno)); */
2153 /* } */
2154 /* } */
2155 /* } */
2156 /* #endif */
2157 /* connection_cleanup (); */
2158
2159 cache_cleanup();
2160 rplay_audio_close();
2161 close(rplay_fd);
2162 close(rptp_fd);
2163 #ifdef OTHER_RPLAY_PORTS
2164 close(other_rplay_fd);
2165 close(other_rptp_fd);
2166 #endif
2167 report(REPORT_DEBUG, "exit(%d)\n", exit_value);
2168 if (logging && log_fd >= 0)
2169 {
2170 close(log_fd);
2171 }
2172 exit(exit_value);
2173 }
2174
2175 #ifdef __STDC__
2176 void
2177 report(int level, char *fmt,...)
2178 #else
2179 void
2180 report(va_alist)
2181 va_dcl
2182 #endif
2183 {
2184 va_list args;
2185 char *p;
2186 time_t now;
2187 static struct iovec iov[2];
2188 static char header[256];
2189 static char message[2048]; /* XXX: largest message */
2190
2191 #ifdef __STDC__
2192 va_start(args, fmt);
2193 #else
2194 char *fmt;
2195 int level;
2196 va_start(args);
2197 level = va_arg(args, int);
2198 fmt = va_arg(args, char *);
2199 #endif
2200
2201 if (report_level >= level)
2202 {
2203 VSNPRINTF(SIZE(message, sizeof(message)), fmt, args);
2204 iov[1].iov_base = message;
2205 iov[1].iov_len = strlen(message);
2206
2207 if (logging)
2208 {
2209 now = time(0);
2210 p = ctime(&now);
2211 p += 4;
2212 p[15] = '\0';
2213
2214 if (log_fd < 0)
2215 {
2216 #ifndef O_SYNC
2217 #define O_SYNC 0
2218 #endif
2219 log_fd = open(log_file, O_WRONLY | O_CREAT | O_APPEND | O_SYNC, 0644);
2220 if (log_fd < 0)
2221 {
2222 fprintf(stderr, "%s: cannot open logfile `%s': %s\n",
2223 progname, log_file, sys_err_str(errno));
2224 /*
2225 * Turn off logging to avoid further problems.
2226 */
2227 logging = 0;
2228 log_fd = -1;
2229 return;
2230 }
2231 }
2232
2233 SNPRINTF(SIZE(header, sizeof(header)), "%s %s rplayd[%d]: ", p, hostname, rplayd_pid);
2234 iov[0].iov_base = header;
2235 iov[0].iov_len = strlen(header);
2236
2237 if (writev(log_fd, iov, 2) < 0)
2238 {
2239 fprintf(stderr, "%s: cannot write to log file `%s': %s\n",
2240 progname, log_file, sys_err_str(errno));
2241 logging = 0;
2242 log_fd = -1;
2243 fprintf(stderr, "%s: logging disabled\n", progname);
2244 }
2245 }
2246
2247 if (debug || level == REPORT_ERROR)
2248 {
2249 SNPRINTF(SIZE(header, sizeof(header)), "%s: ", progname);
2250 iov[0].iov_base = header;
2251 iov[0].iov_len = strlen(header);
2252 #ifndef STDERR_FILENO
2253 #define STDERR_FILENO 2
2254 #endif
2255 writev(STDERR_FILENO, iov, 2);
2256 }
2257
2258 }
2259
2260 va_end(args);
2261 }
2262
2263 void
2264 usage()
2265 {
2266 printf("\n");
2267 printf("rplay %s\n", RPLAY_VERSION);
2268 printf("\n");
2269 printf("usage: %s [options]\n", progname);
2270 printf("\n");
2271
2272 printf("-A DEVICE, --audio-device=DEVICE\n");
2273 printf("\tUse DEVICE for the audio device (%s).\n", rplay_audio_device);
2274 printf("\n");
2275
2276 #ifndef HAVE_OSS
2277 printf("-b N, --audio-bufsize=N\n");
2278 printf("\tAudio buffer size (%d).\n", rplay_audio_bufsize);
2279 printf("\n");
2280 #endif /* !HAVE_OSS */
2281
2282 printf("-B N, --audio-bits=N\n");
2283 printf("\tAudio device bits per sample, 8 or 16.\n");
2284 printf("\n");
2285
2286 printf("--audio-channels=N\n");
2287 printf("\tNumber of audio channels to use, 1 == mono, 2 == stereo.\n");
2288 printf("\n");
2289
2290 printf("-c N, --audio-close=N\n");
2291 printf("\tClose %s after N idle seconds, disabled with 0 (%d).\n",
2292 RPLAY_AUDIO_DEVICE, rplay_audio_timeout);
2293 printf("\n");
2294
2295 printf("-F N, --audio_flush=N\n");
2296 printf("\tFlush %s after N idle seconds, disabled with 0 (%d).\n",
2297 RPLAY_AUDIO_DEVICE, rplay_audio_flush_timeout);
2298 printf("\tN = -1 : flush when spool is empty.\n");
2299 printf("\tN = -2 : flush after each audio write. (not recommended)\n");
2300 printf("\tN should be <= to the audio close timeout.\n");
2301 printf("\n");
2302
2303 printf("--audio-format=FORMAT\n");
2304 printf("\tTell rplayd to write audio data using FORMAT, where FORMAT\n");
2305 printf("\tcan be ulaw, linear-8, ulinear-8, linear-16, or ulinear-16.\n");
2306 printf("\t(linear = signed, ulinear = unsigned)\n");
2307 printf("\n");
2308
2309 #ifdef HAVE_OSS
2310 printf("--audio-fragsize=N\n");
2311 printf("\tAudio fragment size (%d). The default size is zero which lets\n",
2312 rplay_audio_fragsize);
2313 printf("\tthe audio driver pick the \"best\" size. The size specified must\n");
2314 printf("\tbe a power of 2 greater than 16. Example: 256, 1024, 4096.\n");
2315 printf("\n");
2316 #endif /* HAVE_OSS */
2317
2318 printf("--audio-info=INFO, --info=INFO, -i INFO\n");
2319 printf("\tSpecify complete audio device information with one option.\n");
2320 printf("\tINFO is of the form: format,sample-rate,bits,channels\n");
2321 printf("\tExamples: `ulaw,8000,8,1' and `linear-16,44100,16,2'\n");
2322 printf("\tAlso provided are:\n");
2323 printf("\t --audio-info-ulaw, --info-ulaw -> ulaw,8000,8,1\n");
2324 printf("\n");
2325
2326 printf("--audio-match\n");
2327 printf("\tAttempt to match the sample rate of the audio device with\n");
2328 printf("\tthe sample rate of the current sound when no other sounds\n");
2329 printf("\tare playing. If the match fails, --audio-sample-rate is used.\n");
2330 printf("\tThis option overrides --audio-bufsize.\n");
2331 printf("\n");
2332
2333 printf("--audio-port=PORT[,PORT...]\n");
2334 printf("\tOutput audio to the specified audio port(s).\n");
2335 printf("\tValid ports are `speaker', `headphone', and `lineout'.\n");
2336 printf("\tMultiple ports can be specified using `speaker,headphone,lineout'\n");
2337 printf("\n");
2338
2339 printf("-r N, --audio-rate=N\n");
2340 printf("\tWrite the audio buffer N times per second (%d).\n", rplay_audio_rate);
2341 printf("\n");
2342
2343 printf("-R N, --audio-sample-rate=N\n");
2344 printf("\tSample rate of the audio device.\n");
2345 printf("\n");
2346
2347 #ifdef AUTH
2348 printf("--auth\n");
2349 printf("\tEnable host access authentication.\n");
2350 printf("\n");
2351 #endif
2352
2353 printf("-D DIR, --cache-directory=DIR\n");
2354 printf("\tUse DIR for rplay.cache (%s).\n", RPLAY_CACHE);
2355 printf("\n");
2356
2357 printf("--cache-remove\n");
2358 printf("\tRemove the cache directory and all its contents when rplayd exists.\n");
2359 printf("\n");
2360
2361 printf("-s N, --cache-size=N\n");
2362 printf("\tMaximum size in bytes of the rplay cache, disabled with 0 (%d).\n",
2363 RPLAY_CACHE_SIZE);
2364 printf("\n");
2365
2366 #ifdef HAVE_CDROM
2367 printf("--cdrom0=DEVICE, --cdrom1=DEVICE, --cdrom2=DEVICE, --cdrom3=DEVICE\n");
2368 printf("\tSpecify the cdrom[0-3] to DEVICE mapping. For Solaris 2.x the default\n");
2369 printf("\tmapping is cdrom[0-3] -> /vol/dev/aliases/cdrom[0-3].\n");
2370 printf("\tLinux uses cdrom[0-3] -> /dev/cdrom[0-3].\n");
2371 printf("\n");
2372 #endif /* HAVE_CDROM */
2373
2374 printf("-C FILE, --conf=FILE\n");
2375 printf("\tUse FILE for rplay.conf (%s).\n", RPLAY_CONF);
2376 printf("\n");
2377
2378 printf("-T N, --connection-timeout=N\n");
2379 printf("\tClose idle RPTP connections after N seconds, disabled with 0 (%d).\n",
2380 rptp_timeout);
2381 printf("\n");
2382
2383 printf("-d, --debug\n");
2384 printf("\tEnable debug mode.\n");
2385 printf("\n");
2386
2387 printf("-f HOST, --forward=HOST\n");
2388 printf("\tForward all RPLAY packets to HOST.\n");
2389 printf("\n");
2390
2391 printf("--fork\n");
2392 printf("\tEnable backgrounding rplayd at startup. (%s)\n",
2393 do_fork ? "enabled" : "disabled");
2394 printf("\n");
2395
2396 printf("--group=GROUP\n");
2397 printf("\tRun with GROUP privs. (%s)\n", run_as_group);
2398 printf("\n");
2399
2400 printf("--help\n");
2401 printf("\tDisplay helpful information.\n");
2402 printf("\n");
2403
2404 #ifdef HAVE_HELPERS
2405 printf("--helpers=FILE\n");
2406 printf("\tUse FILE for rplay.helpers (%s).\n", RPLAY_HELPERS);
2407 printf("\n");
2408 #endif /* HAVE_HELPERS */
2409
2410 #ifdef AUTH
2411 printf("-H FILE, --hosts=FILE\n");
2412 printf("\tUse FILE for rplay.hosts (%s).\n", RPLAY_HOSTS);
2413 printf("\n");
2414 #endif /* AUTH */
2415
2416 printf("--inetd\n");
2417 printf("\tEnable inetd mode. (%s)\n", inetd ? "enabled" : "disabled");
2418 printf("\n");
2419
2420 printf("-L FILE, --log-file=FILE\n");
2421 printf("\tUse file for rplay.log (%s).\n", RPLAY_LOG);
2422 printf("\n");
2423
2424 printf("-l N, --log-level=N\n");
2425 printf("\tUse logging level N where %d <= n <= %d.\n", REPORT_MIN, REPORT_MAX);
2426 printf("\n");
2427
2428 printf("--memory-cache-size=N\n");
2429 printf("\tMaximum size in bytes of the memory cache, disable caching with 0 (%d).\n",
2430 MEMORY_CACHE_SIZE);
2431 printf("\n");
2432
2433 printf("--memory-cache-sound-size=N\n");
2434 printf("\tMaximum size in bytes of a sound that can be cached in memory.\n");
2435 printf("\tA value of 0 means to try and cache all sounds. (%d)\n",
2436 MEMORY_CACHE_SOUND_SIZE);
2437 printf("\n");
2438
2439 printf("-N, --no-audio\n");
2440 printf("\tDisable audio, RPTP file server mode.\n");
2441 printf("\n");
2442
2443 #ifdef AUTH
2444 printf("--no-auth\n");
2445 printf("\tDisable host access authentication.\n");
2446 printf("\n");
2447 #endif
2448
2449 printf("-n, --no-inetd\n");
2450 printf("\tDisable inetd mode. (%s)\n", inetd ? "enabled" : "disabled");
2451 printf("\n");
2452
2453 printf("--no-fork\n");
2454 printf("\tDisable backgrounding rplayd at startup. (%s)\n",
2455 do_fork ? "enabled" : "disabled");
2456 printf("\n");
2457
2458 printf("--options-file=FILE\n");
2459 printf("\tRead rplayd options from FILE.\n");
2460 printf("\n");
2461
2462 printf("--port=PORT, --rplay-port=PORT\n");
2463 printf("\tUse PORT as the RPLAY/UDP port. (%d)\n", RPLAY_PORT);
2464 printf("\t(--other-rplay-port may also be available)\n");
2465 printf("\n");
2466
2467 printf("--rptp-port=PORT\n");
2468 printf("\tUse PORT as the RPTP/TCP port. (%d)\n", RPTP_PORT);
2469 printf("\t(--other-rptp-port may also be available)\n");
2470 printf("\n");
2471
2472 printf("-S FILE, --servers=FILE\n");
2473 printf("\tUse FILE for rplay.servers (%s).\n", RPLAY_SERVERS);
2474 printf("\n");
2475
2476 printf("-t N, --timeout=N\n");
2477 printf("\tExit after N idle seconds, disabled with 0 (%d).\n", rplayd_timeout);
2478 printf("\n");
2479
2480 printf("--user=USER\n");
2481 printf("\tRun with USER privs. (%s)\n", run_as_user);
2482 printf("\n");
2483
2484 printf("-v, --version\n");
2485 printf("\tPrint the rplay version and exit.\n");
2486 }