audiod: remove bogus check_timeouts()
[paraslash.git] / audiod.c
1 /*
2 * Copyright (C) 2005-2006 Andre Noll <noll@mathematik.tu-darmstadt.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file audiod.c the paraslash's audio daemon */
20
21 #include "para.h"
22
23 #include "audiod.cmdline.h"
24 #include "list.h"
25 #include "close_on_fork.h"
26 #include "sched.h"
27 #include "recv.h"
28 #include "filter.h"
29 #include "grab_client.cmdline.h"
30 #include "grab_client.h"
31
32 #include "error.h"
33 #include "audiod.h"
34 #include "net.h"
35 #include "daemon.h"
36 #include "string.h"
37 #include "fd.h"
38 #include "write.h"
39 #include "write_common.h"
40
41 /** define the array of error lists needed by para_audiod */
42 INIT_AUDIOD_ERRLISTS;
43 /** define the array containing all supported audio formats */
44 DEFINE_AUDIO_FORMAT_ARRAY;
45
46 /**
47 * the possible modes of operation
48 *
49 * - off: disconnect from para_server
50 * - on: receive status information from para_server and play the audio stream
51 * - sb: only receive status information but not the audio stream
52 */
53 enum {AUDIOD_OFF, AUDIOD_ON, AUDIOD_STANDBY};
54
55 /** defines how to handle one supported audio format */
56 struct audio_format_info {
57 /** pointer to the receiver for this audio format */
58 struct receiver *receiver;
59 /** the receiver configuration */
60 void *receiver_conf;
61 /** the number of filters that should be activated for this audio format */
62 unsigned int num_filters;
63 /** pointer to the array of filters to be activated */
64 struct filter **filters;
65 /** pointer to the array of filter configurations */
66 void **filter_conf;
67 /** the number of filters that should be activated for this audio format */
68 unsigned int num_writers;
69 /** pointer to the array of writers to be activated */
70 struct writer **writers;
71 /** pointer to the array of writer configurations */
72 void **writer_conf;
73 /** do not start receiver/filters/writer before this time */
74 struct timeval restart_barrier;
75 };
76
77 /**
78 * describes one instance of a receiver-filter-writer chain
79 *
80 * \sa receier_node, receiver, filter, filter_node, filter_chain, writer,
81 * writer_node, writer_node_group.
82 */
83 struct slot_info {
84 /** number of the audio format in this slot */
85 int format;
86 /** writer start time */
87 struct timeval wstime;
88 /** the receiver info associated with this slot */
89 struct receiver_node *receiver_node;
90 /** the active filter chain */
91 struct filter_chain *fc;
92 /** the active writer node group */
93 struct writer_node_group *wng;
94 };
95 static struct slot_info slot[MAX_STREAM_SLOTS];
96
97 extern const char *status_item_list[NUM_STAT_ITEMS];
98
99 static struct gengetopt_args_info conf;
100 static struct timeval server_stream_start, sa_time_diff;
101 static int playing, current_decoder = -1,
102 audiod_status = AUDIOD_ON, offset_seconds, length_seconds,
103 sa_time_diff_sign = 1;
104 static char *af_status, /* the audio format announced in server status */
105 *socket_name, *hostname;
106 static char *stat_item_values[NUM_STAT_ITEMS];
107 static FILE *logfile;
108 static const struct timeval restart_delay = {0, 300 * 1000};
109 static struct audio_format_info afi[NUM_AUDIO_FORMATS];
110 static struct timeval *now;
111
112 static struct signal_task signal_task_struct, *sig_task = &signal_task_struct;
113
114 struct command_task {
115 int fd;
116 struct task task;
117 };
118 static struct command_task command_task_struct, *cmd_task = &command_task_struct;
119
120 struct status_task {
121 int fd;
122 struct task task;
123 char buf[STRINGSIZE];
124 unsigned loaded;
125 };
126 static struct status_task status_task_struct, *stat_task = &status_task_struct;
127
128 struct audiod_task {
129 struct task task;
130 };
131 static struct audiod_task audiod_task_struct, *at = &audiod_task_struct;
132
133 struct signal_task {
134 int fd;
135 int signum;
136 struct task task;
137 };
138
139
140 /** defines one command of para_audiod */
141 struct audiod_command {
142 /** the name of the command */
143 const char *name;
144 /** pointer to the function that handles the command */
145 int (*handler)(int, int, char**);
146 int (*line_handler)(int, char*);
147 /** one-line description of the command */
148 const char *description;
149 /** summary of the command line options */
150 const char *synopsis;
151 /** the long help text */
152 const char *help;
153 };
154 static int com_grab(int, char *);
155 static int com_cycle(int, int, char **);
156 static int com_help(int, int, char **);
157 static int com_off(int, int, char **);
158 static int com_on(int, int, char **);
159 static int com_sb(int, int, char **);
160 static int com_stat(int, int, char **);
161 static int com_term(int, int, char **);
162 static struct audiod_command cmds[] = {
163 {
164 .name = "cycle",
165 .handler = com_cycle,
166 .description = "switch to next mode",
167 .synopsis = "cycle",
168 .help =
169
170 "on -> standby -> off -> on\n"
171
172 },
173 {
174 .name = "grab",
175 .line_handler = com_grab,
176 .description = "grab the audio stream",
177 .synopsis = "-- grab [grab_options]",
178 .help =
179
180 "grab ('splice') the audio stream at any position in the filter \n"
181 "chain and send that data back to the client. Try\n"
182 "\t para_audioc -- grab -h\n"
183 "for the list of available options.\n"
184 },
185
186 {
187 .name = "help",
188 .handler = com_help,
189 .description = "display command list or help for given command",
190 .synopsis = "help [command]",
191 .help =
192
193 "When I was younger, so much younger than today, I never needed\n"
194 "anybody's help in any way. But now these days are gone, I'm not so\n"
195 "self assured. Now I find I've changed my mind and opened up the doors.\n"
196 "\n"
197 " -- Beatles: Help\n"
198
199 },
200 {
201 .name = "off",
202 .handler = com_off,
203 .description = "deactivate para_audiod",
204 .synopsis = "off",
205 .help =
206
207 "Close connection to para_server and stop all decoders.\n"
208
209 },
210 {
211 .name = "on",
212 .handler = com_on,
213 .description = "activate para_audiod",
214 .synopsis = "on",
215 .help =
216
217 "Establish connection to para_server, retrieve para_server's current\n"
218 "status. If playing, start corresponding decoder. Otherwise stop\n"
219 "all decoders.\n"
220
221 },
222 {
223 .name = "sb",
224 .handler = com_sb,
225 .description = "enter standby mode",
226 .synopsis = "sb",
227 .help =
228
229 "Stop all decoders but leave connection to para_server open.\n"
230
231 },
232 {
233 .name = "stat",
234 .handler = com_stat,
235 .description = "print status information",
236 .synopsis = "stat [item1 ...]",
237 .help =
238
239 "Dump given status items (all if none given) to stdout.\n"
240
241 },
242 {
243 .name = "term",
244 .handler = com_term,
245 .description = "terminate audiod",
246 .synopsis = "term",
247 .help =
248
249 "Stop all decoders, shut down connection to para_server and exit.\n"
250
251 },
252 {
253 .name = NULL,
254 }
255 };
256
257 /** iterate over all slots */
258 #define FOR_EACH_SLOT(_slot) for (_slot = 0; _slot < MAX_STREAM_SLOTS; _slot++)
259 /** iterate over all supported audio formats */
260 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
261 /** iterate over the array of all audiod commands */
262 #define FOR_EACH_COMMAND(c) for (c = 0; cmds[c].name; c++)
263
264 /**
265 * get the audio format number
266 * \param name the name of the audio format
267 *
268 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
269 * \a name is not a supported audio format.
270 */
271 int get_audio_format_num(char *name)
272 {
273 int i;
274 FOR_EACH_AUDIO_FORMAT(i)
275 if (!strcmp(name, audio_formats[i]))
276 return i;
277 return -E_UNSUPPORTED_AUDIO_FORMAT;
278 }
279
280 /*
281 * log function. first argument is loglevel.
282 */
283 void para_log(int ll, const char* fmt,...)
284 {
285 va_list argp;
286 FILE *outfd;
287 struct tm *tm;
288 time_t t1;
289 char str[MAXLINE] = "";
290
291 if (ll < conf.loglevel_arg)
292 return;
293 outfd = logfile? logfile : stderr;
294 time(&t1);
295 tm = localtime(&t1);
296 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
297 fprintf(outfd, "%s %s ", str, hostname);
298 if (conf.loglevel_arg <= INFO)
299 fprintf(outfd, "%i ", ll);
300 va_start(argp, fmt);
301 vfprintf(outfd, fmt, argp);
302 va_end(argp);
303 }
304
305 static int client_write(int fd, const char *buf)
306 {
307 size_t len = strlen(buf);
308 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
309 }
310
311 static char *get_time_string(struct timeval *newest_stime)
312 {
313 struct timeval diff, adj_stream_start, tmp;
314 int total = 0, use_server_time = 1;
315
316 if (!playing) {
317 if (length_seconds)
318 return NULL;
319 return make_message("%s:\n", status_item_list[SI_PLAY_TIME]);
320 }
321 if (audiod_status == AUDIOD_OFF)
322 goto out;
323 if (sa_time_diff_sign > 0)
324 tv_diff(&server_stream_start, &sa_time_diff,
325 &adj_stream_start);
326 else
327 tv_add(&server_stream_start, &sa_time_diff,
328 &adj_stream_start);
329 tmp = adj_stream_start;
330 if (newest_stime && audiod_status == AUDIOD_ON) {
331 tv_diff(newest_stime, &adj_stream_start, &diff);
332 if (tv2ms(&diff) < 5000) {
333 tmp = *newest_stime;
334 use_server_time = 0;
335 }
336 }
337 tv_diff(now, &tmp, &diff);
338 total = diff.tv_sec + offset_seconds;
339 if (total > length_seconds)
340 total = length_seconds;
341 if (total < 0)
342 total = 0;
343 out:
344 return make_message(
345 "%s:%s%d:%02d [%d:%02d] (%d%%/%d:%02d)\n",
346 status_item_list[SI_PLAY_TIME],
347 use_server_time? "~" : "",
348 total / 60,
349 total % 60,
350 (length_seconds - total) / 60,
351 (length_seconds - total) % 60,
352 length_seconds? (total * 100 + length_seconds / 2) /
353 length_seconds : 0,
354 length_seconds / 60,
355 length_seconds % 60
356 );
357 }
358
359 __malloc static char *audiod_status_string(void)
360 {
361 const char *status = (audiod_status == AUDIOD_ON)?
362 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
363 return make_message("%s:%s\n", status_item_list[SI_AUDIOD_STATUS], status);
364 }
365
366 static struct timeval *wstime(void)
367 {
368 int i;
369 struct timeval *max = NULL;
370 FOR_EACH_SLOT(i) {
371 struct slot_info *s = &slot[i];
372 if (!s->wng)
373 continue;
374 if (max && tv_diff(&s->wstime, max, NULL) <= 0)
375 continue;
376 max = &s->wstime;
377 }
378 return max;
379 }
380 __malloc static char *decoder_flags(void)
381 {
382 int i;
383 char decoder_flags[MAX_STREAM_SLOTS + 1];
384
385 FOR_EACH_SLOT(i) {
386 struct slot_info *s = &slot[i];
387 char flag = '0';
388 if (s->receiver_node)
389 flag += 1;
390 if (s->wng)
391 flag += 2;
392 decoder_flags[i] = flag;
393 }
394 decoder_flags[MAX_STREAM_SLOTS] = '\0';
395 return make_message("%s:%s\n", status_item_list[SI_DECODER_FLAGS],
396 decoder_flags);
397 }
398
399 static char *configfile_exists(void)
400 {
401 static char *config_file;
402
403 if (!config_file) {
404 char *home = para_homedir();
405 config_file = make_message("%s/.paraslash/audiod.conf", home);
406 free(home);
407 }
408 return file_exists(config_file)? config_file : NULL;
409 }
410
411 static void setup_signal_handling(void)
412 {
413 sig_task->fd = para_signal_init();
414 PARA_INFO_LOG("signal pipe: fd %d\n", sig_task->fd);
415 para_install_sighandler(SIGINT);
416 para_install_sighandler(SIGTERM);
417 para_install_sighandler(SIGCHLD);
418 para_install_sighandler(SIGHUP);
419 signal(SIGPIPE, SIG_IGN);
420 }
421
422 static void audiod_status_dump(void)
423 {
424 static char *p_ts, *p_us, *p_as, *p_df;
425 struct timeval *t = wstime();
426 char *us, *tmp = get_time_string(t);
427
428 if (tmp && (!p_ts || strcmp(tmp, p_ts)))
429 stat_client_write(tmp, SI_PLAY_TIME);
430 free(p_ts);
431 p_ts = tmp;
432
433 us = uptime_str();
434 tmp = make_message("%s:%s\n", status_item_list[SI_AUDIOD_UPTIME], us);
435 free(us);
436 if (!p_us || strcmp(p_us, tmp))
437 stat_client_write(tmp, SI_AUDIOD_UPTIME);
438 free(p_us);
439 p_us = tmp;
440
441 tmp = audiod_status_string();
442 if (!p_as || strcmp(p_as, tmp))
443 stat_client_write(tmp, SI_AUDIOD_STATUS);
444 free(p_as);
445 p_as = tmp;
446
447 tmp = decoder_flags();
448 if (!p_df || strcmp(p_df, tmp))
449 stat_client_write(tmp, SI_DECODER_FLAGS);
450 free(p_df);
451 p_df = tmp;
452 }
453
454 static void clear_slot(int slot_num)
455 {
456 struct slot_info *s = &slot[slot_num];
457
458 PARA_INFO_LOG("clearing slot %d\n", slot_num);
459 memset(s, 0, sizeof(struct slot_info));
460 s->format = -1;
461 }
462
463 static void close_receiver(int slot_num)
464 {
465 struct slot_info *s = &slot[slot_num];
466 struct audio_format_info *a;
467
468 if (s->format < 0 || !s->receiver_node)
469 return;
470 a = &afi[s->format];
471 PARA_NOTICE_LOG("closing %s receiver in slot %d (eof = %d)\n",
472 audio_formats[s->format] , slot_num, s->receiver_node->eof);
473 if (!s->receiver_node->eof)
474 unregister_task(&s->receiver_node->task);
475 a->receiver->close(s->receiver_node);
476 free(s->receiver_node);
477 s->receiver_node = NULL;
478 /* set restart barrier */
479 tv_add(now, &restart_delay, &afi[s->format].restart_barrier);
480 }
481
482 static void kill_all_decoders(void)
483 {
484 int i;
485
486 FOR_EACH_SLOT(i) {
487 struct slot_info *s = &slot[i];
488 if (s->receiver_node)
489 s->receiver_node->eof = 1;
490 }
491 }
492
493 static int get_empty_slot(void)
494 {
495 int i;
496 struct slot_info *s;
497
498 FOR_EACH_SLOT(i) {
499 s = &slot[i];
500 if (s->format < 0) {
501 clear_slot(i);
502 return i;
503 }
504 if (s->wng || s->receiver_node || s->fc)
505 continue;
506 clear_slot(i);
507 return i;
508 }
509 return -E_NO_MORE_SLOTS;
510 }
511
512 static int decoder_running(int format)
513 {
514 int i, ret = 0;
515 struct slot_info *s;
516
517 FOR_EACH_SLOT(i) {
518 s = &slot[i];
519 if (s->format == format && s->receiver_node)
520 ret |= 1;
521 if (s->format == format && s->wng)
522 ret |= 2;
523 }
524 return ret;
525 }
526
527 static void close_stat_pipe(void)
528 {
529 int i;
530
531 if (stat_task->fd < 0)
532 return;
533 PARA_NOTICE_LOG("%s", "closing status pipe\n");
534 close(stat_task->fd);
535 del_close_on_fork_list(stat_task->fd);
536 stat_task->fd = -1;
537 kill_all_decoders();
538 for (i = 0; i < NUM_STAT_ITEMS; i++) {
539 free(stat_item_values[i]);
540 stat_item_values[i] = NULL;
541 }
542 dump_empty_status();
543 length_seconds = 0;
544 offset_seconds = 0;
545 audiod_status_dump();
546 playing = 0;
547 stat_item_values[SI_STATUS_BAR] = make_message(
548 "%s:no connection to para_server\n",
549 status_item_list[SI_STATUS_BAR]);
550 stat_client_write(stat_item_values[SI_STATUS_BAR], SI_STATUS_BAR);
551 }
552
553 static void __noreturn clean_exit(int status, const char *msg)
554 {
555 PARA_EMERG_LOG("%s\n", msg);
556 kill_all_decoders();
557 if (socket_name)
558 unlink(socket_name);
559 if (stat_task->fd >= 0)
560 close_stat_pipe();
561 exit(status);
562 }
563
564 /**
565 * get the number of filters
566 *
567 * \param audio_format_num the number identifying the audio format
568 *
569 * \return the number of filters for the given audio format
570 *
571 * \sa struct filter;
572 */
573 int num_filters(int audio_format_num)
574 {
575 return afi[audio_format_num].num_filters;
576 }
577
578 static void filter_event_handler(struct task *t)
579 {
580 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
581 unregister_task(t);
582 }
583
584 static void open_filters(int slot_num)
585 {
586 struct slot_info *s = &slot[slot_num];
587 struct audio_format_info *a = &afi[s->format];
588 int nf = a->num_filters;
589 int i;
590
591 s->fc = NULL;
592 if (!nf)
593 return;
594 PARA_INFO_LOG("opening %s filters\n", audio_formats[s->format]);
595 s->fc = para_calloc(sizeof(struct filter_chain));
596 INIT_LIST_HEAD(&s->fc->filters);
597 s->fc->inbuf = s->receiver_node->buf;
598 s->fc->in_loaded = &s->receiver_node->loaded;
599 s->fc->input_eof = &s->receiver_node->eof;
600
601 s->fc->task.pre_select = filter_pre_select;
602 s->fc->task.event_handler = filter_event_handler;
603 s->fc->task.private_data = s->fc;
604 s->fc->task.flags = 0;
605 s->fc->eof = 0;
606 sprintf(s->fc->task.status, "filter chain");
607 for (i = 0; i < nf; i++) {
608 struct filter_node *fn = para_calloc(sizeof(struct filter_node));
609 fn->conf = a->filter_conf[i];
610 fn->fc = s->fc;
611 fn->filter = a->filters[i];
612 INIT_LIST_HEAD(&fn->callbacks);
613 list_add_tail(&fn->node, &s->fc->filters);
614 fn->filter->open(fn);
615 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
616 audio_formats[s->format], i + 1, nf,
617 fn->filter->name, slot_num);
618 s->fc->outbuf = fn->buf;
619 s->fc->out_loaded = &fn->loaded;
620 }
621 register_task(&s->fc->task);
622 // PARA_DEBUG_LOG("output loaded for filter chain %p: %p\n", s->fc,
623 // s->fc->out_loaded);
624 }
625
626 static struct filter_node *find_filter_node(int slot_num, int format, int filternum)
627 {
628 struct filter_node *fn;
629 int i, j;
630
631 FOR_EACH_SLOT(i) {
632 struct slot_info *s = &slot[i];
633 if (s->format < 0 || !s->fc)
634 continue;
635 if (slot_num >= 0 && slot_num != i)
636 continue;
637 if (format >= 0 && s->format != format)
638 continue;
639 if (num_filters(i) < filternum)
640 continue;
641 /* success */
642 j = 1;
643 list_for_each_entry(fn, &s->fc->filters, node)
644 if (filternum <= 0 || j++ == filternum)
645 break;
646 return fn;
647 }
648 return NULL;
649 }
650
651 static void wng_event_handler(struct task *t)
652 {
653 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
654 unregister_task(t);
655 }
656
657 static void open_writers(int slot_num)
658 {
659 int ret, i;
660 struct slot_info *s = &slot[slot_num];
661 struct audio_format_info *a = &afi[s->format];
662
663 PARA_INFO_LOG("opening %s writers\n", audio_formats[s->format]);
664 if (!a->num_writers)
665 s->wng = setup_default_wng();
666 else
667 s->wng = wng_new(a->num_writers);
668 if (s->fc) {
669 s->wng->buf = s->fc->outbuf;
670 s->wng->loaded = s->fc->out_loaded;
671 s->wng->input_eof = &s->fc->eof;
672 s->fc->output_eof = &s->wng->eof;
673 } else {
674 s->wng->buf = s->receiver_node->buf;
675 s->wng->loaded = &s->receiver_node->loaded;
676 s->wng->input_eof = &s->receiver_node->eof;
677 }
678 s->wng->task.event_handler = wng_event_handler;
679 for (i = 0; i < a->num_writers; i++) {
680 s->wng->writer_nodes[i].conf = a->writer_conf[i];
681 s->wng->writer_nodes[i].writer = a->writers[i];
682 sprintf(s->wng->writer_nodes[i].task.status, "writer_node");
683 }
684 ret = wng_open(s->wng);
685 s->wstime = *now;
686 current_decoder = slot_num;
687 activate_inactive_grab_clients(slot_num, s->format, &s->fc->filters);
688 }
689
690 static void rn_event_handler(struct task *t)
691 {
692 // struct receiver_node *rn = t->private_data;
693 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
694 unregister_task(t);
695 }
696
697 static void open_receiver(int format)
698 {
699 struct audio_format_info *a = &afi[format];
700 struct slot_info *s;
701 int ret, slot_num;
702 struct receiver_node *rn;
703
704 slot_num = get_empty_slot();
705 if (slot_num < 0)
706 clean_exit(EXIT_FAILURE, PARA_STRERROR(-slot_num));
707 s = &slot[slot_num];
708 s->format = format;
709 s->receiver_node = para_calloc(sizeof(struct receiver_node));
710 rn = s->receiver_node;
711 rn->receiver = a->receiver;
712 rn->conf = a->receiver_conf;
713 ret = a->receiver->open(s->receiver_node);
714 if (ret < 0) {
715 PARA_ERROR_LOG("failed to open receiver (%s)\n",
716 PARA_STRERROR(-ret));
717 free(s->receiver_node);
718 s->receiver_node = NULL;
719 return;
720 }
721 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
722 audio_formats[s->format], a->receiver->name, slot_num);
723 rn->task.private_data = s->receiver_node;
724 rn->task.pre_select = a->receiver->pre_select;
725 rn->task.post_select = a->receiver->post_select;
726 rn->task.event_handler = rn_event_handler;
727 rn->task.flags = 0;
728 sprintf(rn->task.status, "receiver node");
729 register_task(&rn->task);
730 }
731
732 static int is_frozen(int format)
733 {
734 struct audio_format_info *a = &afi[format];
735
736 return (tv_diff(now, &a->restart_barrier, NULL) > 0)? 0 : 1;
737 }
738
739 static void open_current_receiver(void)
740 {
741 int i;
742
743 if (!af_status)
744 return;
745 i = get_audio_format_num(af_status);
746 if (i < 0)
747 return;
748 if (decoder_running(i) || is_frozen(i))
749 return;
750 open_receiver(i);
751 }
752
753 static void compute_time_diff(const struct timeval *status_time)
754 {
755 struct timeval tmp, diff;
756 static int count;
757 int sign;
758 const struct timeval max_deviation = {0, 500 * 1000};
759 const int time_smooth = 5;
760
761 sign = tv_diff(status_time, now, &diff);
762 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
763 // sign, sa_time_diff_sign);
764 if (!count) {
765 sa_time_diff_sign = sign;
766 sa_time_diff = diff;
767 count++;
768 return;
769 }
770 if (count > 5) {
771 int s = tv_diff(&diff, &sa_time_diff, &tmp);
772 if (tv_diff(&max_deviation, &tmp, NULL) < 0)
773 PARA_WARNING_LOG("time diff jump: %lims\n",
774 s * tv2ms(&tmp));
775 }
776 count++;
777 sa_time_diff_sign = tv_convex_combination(
778 sa_time_diff_sign * time_smooth, &sa_time_diff,
779 count > 10? sign : sign * time_smooth, &diff,
780 &tmp);
781 sa_time_diff = tmp;
782 PARA_INFO_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
783 sign > 0? "+" : "-",
784 tv2ms(&diff),
785 sa_time_diff_sign ? "+" : "-",
786 tv2ms(&sa_time_diff)
787 );
788 }
789
790 static void check_stat_line(char *line)
791 {
792 int itemnum;
793 size_t ilen = 0;
794 long unsigned sec, usec;
795 char *tmp;
796
797 // PARA_INFO_LOG("line: %s\n", line);
798 if (!line)
799 return;
800 itemnum = stat_line_valid(line);
801 if (itemnum < 0) {
802 PARA_WARNING_LOG("invalid status line: %s\n", line);
803 return;
804 }
805 tmp = make_message("%s\n", line);
806 stat_client_write(tmp, itemnum);
807 free(tmp);
808 free(stat_item_values[itemnum]);
809 stat_item_values[itemnum] = para_strdup(line);
810 ilen = strlen(status_item_list[itemnum]);
811 switch (itemnum) {
812 case SI_STATUS:
813 playing = strstr(line, "playing")? 1 : 0;
814 break;
815 case SI_FORMAT:
816 free(af_status);
817 af_status = para_strdup(line + ilen + 1);
818 break;
819 case SI_OFFSET:
820 offset_seconds = atoi(line + ilen + 1);
821 break;
822 case SI_LENGTH:
823 length_seconds = atoi(line + ilen + 1);
824 break;
825 case SI_STREAM_START:
826 if (sscanf(line + ilen + 1, "%lu.%lu", &sec, &usec) == 2) {
827 server_stream_start.tv_sec = sec;
828 server_stream_start.tv_usec = usec;
829 }
830 break;
831 case SI_CURRENT_TIME:
832 if (sscanf(line + ilen + 1, "%lu.%lu", &sec, &usec) == 2) {
833 struct timeval tv = {sec, usec};
834 compute_time_diff(&tv);
835 }
836 break;
837 }
838 }
839
840 static void handle_signal(int sig)
841 {
842 switch (sig) {
843 case SIGCHLD:
844 for (;;) {
845 pid_t pid = para_reap_child();
846 if (pid <= 0)
847 return;
848 PARA_CRIT_LOG("para_client died (pid %d)\n", pid);
849 }
850 case SIGINT:
851 case SIGTERM:
852 case SIGHUP:
853 PARA_EMERG_LOG("terminating on signal %d\n", sig);
854 clean_exit(EXIT_FAILURE, "caught deadly signal");
855 return;
856 }
857 }
858
859 static void try_to_close_slot(int slot_num)
860 {
861 struct slot_info *s = &slot[slot_num];
862
863 if (s->format < 0)
864 return;
865 if (s->receiver_node && !s->receiver_node->eof)
866 return;
867 if (s->fc && !s->fc->eof)
868 return;
869 if (s->wng && !s->wng->eof)
870 return;
871 PARA_INFO_LOG("closing slot %d \n", slot_num);
872 wng_close(s->wng);
873 wng_destroy(s->wng);
874 close_filters(s->fc);
875 free(s->fc);
876 close_receiver(slot_num);
877 clear_slot(slot_num);
878 }
879
880 static void audiod_pre_select(struct sched *s, __a_unused struct task *t)
881 {
882 int i;
883
884 now = &s->now;
885 if (audiod_status != AUDIOD_ON)
886 kill_all_decoders();
887 else if (playing)
888 open_current_receiver();
889 FOR_EACH_SLOT(i) {
890 struct receiver_node *rn;
891
892 try_to_close_slot(i);
893 if (slot[i].format < 0)
894 continue;
895 rn = slot[i].receiver_node;
896 if (rn && rn->loaded && !slot[i].wng) {
897 open_filters(i);
898 open_writers(i);
899 }
900 }
901 }
902
903 static void audiod_post_select(struct sched *s, __a_unused struct task *t)
904 {
905 /* only save away the current time for other users */
906 now = &s->now;
907 }
908
909 static void init_audiod_task(struct audiod_task *at)
910 {
911 at->task.pre_select = audiod_pre_select;
912 at->task.post_select = audiod_post_select;
913 at->task.private_data = at;
914 at->task.flags = 0;
915 sprintf(at->task.status, "audiod task");
916 }
917
918 static int parse_stream_command(const char *txt, char **cmd)
919 {
920 char *p = strchr(txt, ':');
921 int i;
922
923 if (!p)
924 return -E_MISSING_COLON;
925 p++;
926 FOR_EACH_AUDIO_FORMAT(i) {
927 if (strncmp(txt, audio_formats[i], strlen(audio_formats[i])))
928 continue;
929 *cmd = p;
930 return i;
931 }
932 return -E_UNSUPPORTED_AUDIO_FORMAT;
933 }
934
935 static int add_filter(int format, char *cmdline)
936 {
937 struct audio_format_info *a = &afi[format];
938 int filter_num, nf = a->num_filters;
939
940 filter_num = check_filter_arg(cmdline, &a->filter_conf[nf]);
941 if (filter_num < 0)
942 return filter_num;
943 a->filters[nf] = &filters[filter_num];
944 a->num_filters++;
945 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats[format], nf + 1,
946 a->filters[nf]->name);
947 return filter_num;
948 }
949
950 static int init_writers(void)
951 {
952 int i, ret, nw;
953 char *cmd;
954 struct audio_format_info *a;
955
956 init_supported_writers();
957 nw = PARA_MAX(1, conf.writer_given);
958 PARA_INFO_LOG("maximal number of writers: %d\n", nw);
959 FOR_EACH_AUDIO_FORMAT(i) {
960 a = &afi[i];
961 a->writer_conf = para_malloc(nw * sizeof(void *));
962 a->writers = para_malloc(nw * sizeof(struct writer *));
963 a->num_writers = 0;
964 }
965 for (i = 0; i < conf.writer_given; i++) {
966 void *wconf;
967 int writer_num;
968 ret = parse_stream_command(conf.writer_arg[i], &cmd);
969 if (ret < 0)
970 goto out;
971 a = &afi[ret];
972 nw = a->num_writers;
973 wconf = check_writer_arg(cmd, &writer_num);
974 if (!wconf) {
975 ret = writer_num;
976 goto out;
977 }
978 a->writers[nw] = &writers[writer_num];
979 a->writer_conf[nw] = wconf;
980 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats[ret],
981 nw, writer_names[writer_num]);
982 a->num_writers++;
983 }
984 ret = 1;
985 out:
986 return ret;
987 }
988
989 static int init_receivers(void)
990 {
991 int i, ret, receiver_num;
992 char *cmd = NULL;
993 struct audio_format_info *a;
994
995 for (i = 0; receivers[i].name; i++) {
996 PARA_INFO_LOG("initializing %s receiver\n", receivers[i].name);
997 receivers[i].init(&receivers[i]);
998 }
999 for (i = 0; i < conf.receiver_given; i++) {
1000 char *arg = conf.receiver_arg[i];
1001 char *recv = strchr(arg, ':');
1002 ret = -E_MISSING_COLON;
1003 if (!recv)
1004 goto out;
1005 *recv = '\0';
1006 recv++;
1007 ret = get_audio_format_num(arg);
1008 if (ret < 0)
1009 goto out;
1010 afi[ret].receiver_conf = check_receiver_arg(recv, &receiver_num);
1011 if (!afi[ret].receiver_conf) {
1012 ret = -E_RECV_SYNTAX;
1013 goto out;
1014 }
1015 afi[ret].receiver = &receivers[receiver_num];
1016 }
1017 /* use the first available receiver with no arguments
1018 * for those audio formats for which no receiver
1019 * was specified
1020 */
1021 cmd = para_strdup(receivers[0].name);
1022 FOR_EACH_AUDIO_FORMAT(i) {
1023 a = &afi[i];
1024 if (a->receiver_conf)
1025 continue;
1026 a->receiver_conf = check_receiver_arg(cmd, &receiver_num);
1027 if (!a->receiver_conf)
1028 return -E_RECV_SYNTAX;
1029 a->receiver = &receivers[receiver_num];
1030 }
1031 ret = 1;
1032 out:
1033 free(cmd);
1034 return ret;
1035 }
1036
1037 static int init_default_filters(void)
1038 {
1039 int i, ret = 1;
1040
1041 FOR_EACH_AUDIO_FORMAT(i) {
1042 struct audio_format_info *a = &afi[i];
1043 char *tmp;
1044 int j;
1045
1046 if (a->num_filters)
1047 continue; /* no default -- nothing to to */
1048 /* add "dec" to audio format name */
1049 tmp = make_message("%sdec", audio_formats[i]);
1050 for (j = 0; filters[j].name; j++)
1051 if (!strcmp(tmp, filters[j].name))
1052 break;
1053 free(tmp);
1054 ret = -E_UNSUPPORTED_FILTER;
1055 if (!filters[j].name)
1056 goto out;
1057 tmp = para_strdup(filters[j].name);
1058 ret = add_filter(i, tmp);
1059 free(tmp);
1060 if (ret < 0)
1061 goto out;
1062 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats[i],
1063 filters[j].name);
1064 }
1065 out:
1066 return ret;
1067 }
1068
1069 static int init_filters(void)
1070 {
1071 int i, ret, nf;
1072
1073 filter_init(filters);
1074 nf = PARA_MAX(1, conf.filter_given);
1075 PARA_INFO_LOG("maximal number of filters: %d\n", nf);
1076 FOR_EACH_AUDIO_FORMAT(i) {
1077 afi[i].filter_conf = para_malloc(nf * sizeof(void *));
1078 afi[i].filters = para_malloc(nf * sizeof(struct filter *));
1079 }
1080 if (!conf.no_default_filters_given)
1081 return init_default_filters();
1082 for (i = 0; i < conf.filter_given; i++) {
1083 char *arg = conf.filter_arg[i];
1084 char *filter_name = strchr(arg, ':');
1085 ret = -E_MISSING_COLON;
1086 if (!filter_name)
1087 goto out;
1088 *filter_name = '\0';
1089 filter_name++;
1090 ret = get_audio_format_num(arg);
1091 if (ret < 0)
1092 goto out;
1093 ret = add_filter(ret, filter_name);
1094 if (ret < 0)
1095 goto out;
1096 }
1097 ret = init_default_filters(); /* use default values for the rest */
1098 out:
1099 return ret;
1100 }
1101
1102 static int init_stream_io(void)
1103 {
1104 int ret;
1105
1106 ret = init_writers();
1107 if (ret < 0)
1108 return ret;
1109 ret = init_receivers();
1110 if (ret < 0)
1111 return ret;
1112 ret = init_filters();
1113 if (ret < 0)
1114 return ret;
1115 return 1;
1116 }
1117
1118 static int dump_commands(int fd)
1119 {
1120 char *buf = para_strdup(""), *tmp = NULL;
1121 int i;
1122 ssize_t ret;
1123
1124 FOR_EACH_COMMAND(i) {
1125 tmp = make_message("%s%s\t%s\n", buf, cmds[i].name,
1126 cmds[i].description);
1127 free(buf);
1128 buf = tmp;
1129 }
1130 ret = client_write(fd, buf);
1131 free(buf);
1132 return ret;
1133 }
1134
1135 /*
1136 * command handlers don't close their fd on errors (ret < 0) so that
1137 * its caller can send an error message. Otherwise (ret >= 0) it's up
1138 * to each individual command to close the fd if necessary.
1139 */
1140
1141 static int com_help(int fd, int argc, char **argv)
1142 {
1143 int i, ret;
1144 char *buf;
1145 const char *dflt = "No such command. Available commands:\n";
1146
1147 if (argc < 2) {
1148 ret = dump_commands(fd);
1149 goto out;
1150 }
1151 FOR_EACH_COMMAND(i) {
1152 if (strcmp(cmds[i].name, argv[1]))
1153 continue;
1154 buf = make_message(
1155 "NAME\n\t%s -- %s\n"
1156 "SYNOPSIS\n\tpara_audioc %s\n"
1157 "DESCRIPTION\n%s\n",
1158 argv[1],
1159 cmds[i].description,
1160 cmds[i].synopsis,
1161 cmds[i].help
1162 );
1163 ret = client_write(fd, buf);
1164 free(buf);
1165 goto out;
1166 }
1167 ret = client_write(fd, dflt);
1168 if (ret > 0)
1169 ret = dump_commands(fd);
1170 out:
1171 if (ret >= 0)
1172 close(fd);
1173 return ret;
1174 }
1175
1176 static int com_stat(int fd, __a_unused int argc, __a_unused char **argv)
1177 {
1178 int i, ret;
1179 char *buf = NULL;
1180 long unsigned mask = ~0LU;
1181
1182 if (argc > 1) {
1183 mask = 0;
1184 for (i = 1; i < argc; i++) {
1185 ret = stat_item_valid(argv[i]);
1186 if (ret < 0)
1187 return ret;
1188 mask |= (1 << ret);
1189 }
1190 }
1191 PARA_INFO_LOG("mask: 0x%lx\n", mask);
1192 if (mask & (1 << SI_PLAY_TIME)) {
1193 struct timeval *t = wstime();
1194 char *ts = get_time_string(t);
1195 if (ts) {
1196 ret = client_write(fd, ts);
1197 if (ret < 0)
1198 goto out;
1199 free(ts);
1200 }
1201 }
1202 if (mask & (1 << SI_AUDIOD_UPTIME)) {
1203 char *tmp, *us = uptime_str();
1204 tmp = make_message("%s:%s\n",
1205 status_item_list[SI_AUDIOD_UPTIME], us);
1206 free(us);
1207 ret = client_write(fd, tmp);
1208 if (ret < 0)
1209 goto out;
1210 free(tmp);
1211 }
1212 if (mask & (1 << SI_AUDIOD_STATUS)) {
1213 char *s = audiod_status_string();
1214 ret = client_write(fd, s);
1215 if (ret < 0)
1216 goto out;
1217 free(s);
1218 }
1219 if (mask & (1 << SI_DECODER_FLAGS)) {
1220 char *df =decoder_flags();
1221 ret = client_write(fd, df);
1222 if (ret < 0)
1223 goto out;
1224 free(df);
1225 }
1226
1227 for (i = 0; i < NUM_STAT_ITEMS; i++) {
1228 char *tmp, *v;
1229 if (!((1 << i) & mask))
1230 continue;
1231 v = stat_item_values[i];
1232 tmp = make_message("%s%s%s", buf? buf: "",
1233 v? v : "", v? "\n" : "");
1234 free(buf);
1235 buf = tmp;
1236 }
1237 ret = client_write(fd, buf);
1238 out:
1239 if (ret > 0)
1240 ret = stat_client_add(fd, mask);
1241 free(buf);
1242 return ret;
1243 }
1244
1245 static int com_grab(int fd, char *cmdline)
1246 {
1247 struct grab_client *gc;
1248 struct filter_node *fn;
1249 int i, err;
1250 char *msg;
1251
1252 gc = grab_client_new(fd, cmdline, &err);
1253 if (!gc)
1254 goto err_out;
1255 fn = find_filter_node(gc->conf->slot_arg, gc->audio_format_num, gc->conf->filter_num_arg);
1256 if (fn)
1257 activate_grab_client(gc, fn);
1258 return 1;
1259 err_out:
1260 if (err != -E_GC_HELP_GIVEN && err != -E_GC_VERSION_GIVEN)
1261 return err;
1262 if (err == -E_GC_HELP_GIVEN) {
1263 msg = make_message("%s\n\n", grab_client_args_info_usage);
1264 for (i = 0; grab_client_args_info_help[i]; i++) {
1265 char *tmp = make_message("%s%s\n", msg,
1266 grab_client_args_info_help[i]);
1267 free(msg);
1268 msg = tmp;
1269 }
1270 } else
1271 msg = make_message("%s %s\n",
1272 GRAB_CLIENT_CMDLINE_PARSER_PACKAGE,
1273 GRAB_CLIENT_CMDLINE_PARSER_VERSION);
1274 err = client_write(fd, msg);
1275 free(msg);
1276 if (err < 0)
1277 return err;
1278 close(fd);
1279 return 1;
1280 }
1281
1282 static int __noreturn com_term(int fd, __a_unused int argc, __a_unused char **argv)
1283 {
1284 close(fd);
1285 clean_exit(EXIT_SUCCESS, "terminating on user request");
1286 }
1287
1288 static int com_on(int fd, __a_unused int argc, __a_unused char **argv)
1289 {
1290 audiod_status = AUDIOD_ON;
1291 close(fd);
1292 return 1;
1293 }
1294
1295 static int com_off(int fd, __a_unused int argc, __a_unused char **argv)
1296 {
1297 audiod_status = AUDIOD_OFF;
1298 close(fd);
1299 return 1;
1300 }
1301
1302 static int com_sb(int fd, __a_unused int argc, __a_unused char **argv)
1303 {
1304 audiod_status = AUDIOD_STANDBY;
1305 close(fd);
1306 return 1;
1307 }
1308
1309 static int com_cycle(int fd, int argc, char **argv)
1310 {
1311 switch (audiod_status) {
1312 case AUDIOD_ON:
1313 return com_sb(fd, argc, argv);
1314 break;
1315 case AUDIOD_OFF:
1316 return com_on(fd, argc, argv);
1317 break;
1318 case AUDIOD_STANDBY:
1319 return com_off(fd, argc, argv);
1320 break;
1321 }
1322 close(fd);
1323 return 1;
1324 }
1325
1326 static int check_perms(uid_t uid)
1327 {
1328 int i;
1329
1330 if (!conf.user_allow_given)
1331 return 1;
1332 for (i = 0; i < conf.user_allow_given; i++)
1333 if (uid == conf.user_allow_arg[i])
1334 return 1;
1335 return -E_UCRED_PERM;
1336 }
1337
1338 static int handle_connect(void)
1339 {
1340 int i, argc, ret, clifd = -1;
1341 char *cmd = NULL, *p, *buf = para_calloc(MAXLINE), **argv = NULL;
1342 struct sockaddr_un unix_addr;
1343
1344 ret = para_accept(cmd_task->fd, &unix_addr, sizeof(struct sockaddr_un));
1345 if (ret < 0)
1346 goto out;
1347 clifd = ret;
1348 ret = recv_cred_buffer(clifd, buf, MAXLINE - 1);
1349 if (ret < 0)
1350 goto out;
1351 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret, buf);
1352 ret = check_perms(ret);
1353 if (ret < 0)
1354 goto out;
1355 ret = -E_INVALID_AUDIOD_CMD;
1356 cmd = para_strdup(buf);
1357 p = strchr(cmd, '\n');
1358 if (!p)
1359 p = "";
1360 else {
1361 *p = '\0';
1362 p++;
1363 }
1364 for (i = 0; cmds[i].name; i++) {
1365 int j;
1366 if (strcmp(cmds[i].name, cmd))
1367 continue;
1368 if (cmds[i].handler) {
1369 argc = split_args(buf, &argv, "\n");
1370 PARA_INFO_LOG("argv[0]: %s, argc= %d\n", argv[0], argc);
1371 ret = cmds[i].handler(clifd, argc, argv);
1372 goto out;
1373 }
1374 for (j = 0; p[j]; j++)
1375 if (p[j] == '\n')
1376 p[j] = ' ';
1377 PARA_INFO_LOG("cmd: %s, options: %s\n", cmd, p);
1378 ret = cmds[i].line_handler(clifd, p);
1379 goto out;
1380 }
1381 ret = -E_INVALID_AUDIOD_CMD; /* cmd not found */
1382 out:
1383 free(cmd);
1384 free(buf);
1385 free(argv);
1386 if (clifd > 0 && ret < 0 && ret != -E_CLIENT_WRITE) {
1387 char *tmp = make_message("%s\n", PARA_STRERROR(-ret));
1388 client_write(clifd, tmp);
1389 free(tmp);
1390 close(clifd);
1391 }
1392 return ret;
1393 }
1394
1395 static void audiod_get_socket(void)
1396 {
1397 struct sockaddr_un unix_addr;
1398
1399 if (conf.socket_given)
1400 socket_name = para_strdup(conf.socket_arg);
1401 else {
1402 char *hn = para_hostname();
1403 socket_name = make_message("/var/paraslash/audiod_socket.%s",
1404 hn);
1405 free(hn);
1406 }
1407 PARA_NOTICE_LOG("connecting to local socket %s\n", socket_name);
1408 if (conf.force_given)
1409 unlink(socket_name);
1410 cmd_task->fd = create_pf_socket(socket_name, &unix_addr,
1411 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
1412 if (cmd_task->fd < 0) {
1413 PARA_EMERG_LOG("%s", "can not connect to socket\n");
1414 exit(EXIT_FAILURE); /* do not unlink socket */
1415 }
1416 if (listen(cmd_task->fd , 5) < 0) {
1417 PARA_EMERG_LOG("%s", "can not listen on socket\n");
1418 exit(EXIT_FAILURE); /* do not unlink socket */
1419 }
1420 add_close_on_fork_list(cmd_task->fd);
1421 }
1422
1423 static int open_stat_pipe(void)
1424 {
1425 int ret, fd[3] = {-1, 1, 0};
1426 pid_t pid;
1427 ret = para_exec_cmdline_pid(&pid, BINDIR "/para_client stat", fd);
1428 if (ret >= 0) {
1429 ret = fd[1];
1430 PARA_NOTICE_LOG("stat pipe opened, fd %d\n", ret);
1431 add_close_on_fork_list(ret);
1432 } else
1433 clean_exit(EXIT_FAILURE, "failed to open status pipe");
1434 return ret;
1435 }
1436
1437 void signal_event_handler(struct task *t)
1438 {
1439 struct signal_task *st = t->private_data;
1440 handle_signal(st->signum);
1441 }
1442
1443 void signal_pre_select(struct sched *s, struct task *t)
1444 {
1445 struct signal_task *st = t->private_data;
1446 t->ret = 1;
1447 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
1448 }
1449
1450 void signal_post_select(struct sched *s, struct task *t)
1451 {
1452 struct signal_task *st = t->private_data;
1453 t->ret = 1;
1454 if (!FD_ISSET(st->fd, &s->rfds))
1455 return;
1456 t->ret = -E_SIGNAL_CAUGHT;
1457 st->signum = para_next_signal();
1458 }
1459
1460 void signal_setup_default(struct signal_task *st)
1461 {
1462 st->task.pre_select = signal_pre_select;
1463 st->task.post_select = signal_post_select;
1464 st->task.private_data = st;
1465 st->task.flags = 0;
1466 sprintf(st->task.status, "signal task");
1467 }
1468
1469 static void command_pre_select(struct sched *s, struct task *t)
1470 {
1471 struct command_task *ct = t->private_data;
1472 para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
1473
1474 }
1475
1476 static void command_post_select(struct sched *s, struct task *t)
1477 {
1478 int ret;
1479 struct command_task *ct = t->private_data;
1480
1481 if (audiod_status != AUDIOD_OFF)
1482 audiod_status_dump();
1483 t->ret = 1; /* always successful */
1484 if (!FD_ISSET(ct->fd, &s->rfds))
1485 return;
1486 ret = handle_connect();
1487 if (ret < 0)
1488 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
1489 }
1490
1491 static void init_command_task(struct command_task *ct)
1492 {
1493 ct->task.pre_select = command_pre_select;
1494 ct->task.post_select = command_post_select;
1495 ct->task.private_data = ct;
1496 ct->task.flags = 0;
1497 sprintf(ct->task.status, "command task");
1498 }
1499
1500 static void status_pre_select(struct sched *s, struct task *t)
1501 {
1502 struct status_task *st = t->private_data;
1503 t->ret = 1;
1504 if (st->fd >= 0 && audiod_status == AUDIOD_OFF)
1505 close_stat_pipe();
1506 if (st->fd < 0 && audiod_status != AUDIOD_OFF) {
1507 st->fd = open_stat_pipe();
1508 st->loaded = 0;
1509 st->buf[0] = '\0';
1510 }
1511 if (st->fd >= 0 && audiod_status != AUDIOD_OFF)
1512 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
1513 }
1514
1515 static void status_post_select(struct sched *s, struct task *t)
1516 {
1517 struct status_task *st = t->private_data;
1518 int ret;
1519
1520 t->ret = 1;
1521 if (st->fd < 0 || !FD_ISSET(st->fd, &s->rfds))
1522 return;
1523 ret = read(st->fd, st->buf + st->loaded,
1524 STRINGSIZE - 1 - st->loaded);
1525 if (ret <= 0) {
1526 close_stat_pipe();
1527 /* avoid busy loop if server is down */
1528 while (sleep(1) > 0) /* FIXME */
1529 ; /* try again*/
1530 } else {
1531 st->buf[ret + st->loaded] = '\0';
1532 st->loaded = for_each_line(st->buf, ret + st->loaded,
1533 &check_stat_line);
1534 }
1535 }
1536
1537 static void init_status_task(struct status_task *st)
1538 {
1539 st->task.pre_select = status_pre_select;
1540 st->task.post_select = status_post_select;
1541 st->task.private_data = st;
1542 st->task.flags = 0;
1543 st->loaded = 0;
1544 st->fd = -1;
1545 st->buf[0] = '\0';
1546 sprintf(st->task.status, "status task");
1547 }
1548
1549 static void set_initial_status(void)
1550 {
1551 audiod_status = AUDIOD_ON;
1552 if (!conf.mode_given)
1553 return;
1554 if (!strcmp(conf.mode_arg, "sb")) {
1555 audiod_status = AUDIOD_STANDBY;
1556 return;
1557 }
1558 if (!strcmp(conf.mode_arg, "off")) {
1559 audiod_status = AUDIOD_OFF;
1560 return;
1561 }
1562 if (strcmp(conf.mode_arg, "on"))
1563 PARA_WARNING_LOG("%s", "invalid mode\n");
1564 }
1565
1566 int main(int argc, char *argv[])
1567 {
1568 char *cf;
1569 int ret, i;
1570 struct sched s;
1571
1572 init_sched();
1573
1574 valid_fd_012();
1575 hostname = para_hostname();
1576 cmdline_parser(argc, argv, &conf);
1577 para_drop_privileges(conf.user_arg, conf.group_arg);
1578 cf = configfile_exists();
1579 if (cf) {
1580 if (cmdline_parser_configfile(cf, &conf, 0, 0, 0)) {
1581 PARA_EMERG_LOG("%s", "parse error in config file\n");
1582 exit(EXIT_FAILURE);
1583 }
1584 }
1585 if (conf.logfile_given)
1586 logfile = open_log(conf.logfile_arg);
1587 log_welcome("para_audiod", conf.loglevel_arg);
1588 i = init_stream_io();
1589 if (i < 0) {
1590 PARA_EMERG_LOG("init stream io error: %s\n", PARA_STRERROR(-i));
1591 exit(EXIT_FAILURE);
1592 }
1593 server_uptime(UPTIME_SET);
1594 set_initial_status();
1595 FOR_EACH_SLOT(i)
1596 clear_slot(i);
1597 init_grabbing();
1598 setup_signal_handling();
1599 if (conf.daemon_given)
1600 daemon_init();
1601 audiod_get_socket(); /* doesn't return on errors */
1602
1603 signal_setup_default(sig_task);
1604 sig_task->task.event_handler = signal_event_handler;
1605
1606 init_status_task(stat_task);
1607 init_command_task(cmd_task);
1608 init_audiod_task(at);
1609
1610 register_task(&sig_task->task);
1611 register_task(&cmd_task->task);
1612 register_task(&stat_task->task);
1613 register_task(&at->task);
1614 s.default_timeout.tv_sec = 0;
1615 s.default_timeout.tv_usec = 99 * 1000;
1616 ret = sched(&s);
1617
1618 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
1619 return EXIT_FAILURE;
1620 }