Debian Codegraph @

rplayd/rplayd.c

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