Debian Codegraph @

rplayd/rplayd.c

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