8b9422bc322b8c989a9f04456dd1d50253f68e5d
[paraslash.git] / audiod.c
1 /*
2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file audiod.c The paraslash's audio daemon. */
8
9 #include <netinet/in.h>
10 #include <sys/socket.h>
11 #include <regex.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
14 #include <sys/un.h>
15 #include <netdb.h>
16 #include <signal.h>
17 #include <pwd.h>
18
19 #include "para.h"
20 #include "error.h"
21 #include "crypt.h"
22 #include "audiod.cmdline.h"
23 #include "list.h"
24 #include "sched.h"
25 #include "ggo.h"
26 #include "buffer_tree.h"
27 #include "recv.h"
28 #include "filter.h"
29 #include "grab_client.h"
30 #include "client.cmdline.h"
31 #include "client.h"
32 #include "audiod.h"
33 #include "net.h"
34 #include "daemon.h"
35 #include "string.h"
36 #include "fd.h"
37 #include "write.h"
38 #include "write_common.h"
39 #include "signal.h"
40 #include "version.h"
41
42 __printf_2_3 void (*para_log)(int, const char*, ...) = daemon_log;
43 /** define the array of error lists needed by para_audiod */
44 INIT_AUDIOD_ERRLISTS;
45 /** define the array containing all supported audio formats */
46 const char *audio_formats[] = {AUDIOD_AUDIO_FORMAT_ARRAY NULL};
47
48 DEFINE_RECEIVER_ARRAY;
49
50 /** Defines how audiod handles one supported audio format. */
51 struct audio_format_info {
52 /** pointer to the receiver for this audio format */
53 struct receiver *receiver;
54 /** the receiver configuration */
55 void *receiver_conf;
56 /** the number of filters that should be activated for this audio format */
57 unsigned int num_filters;
58 /** Array of filter numbers to be activated. */
59 unsigned *filter_nums;
60 /** Pointer to the array of filter configurations. */
61 void **filter_conf;
62 /** the number of filters that should be activated for this audio format */
63 unsigned int num_writers;
64 /** Array of writer numbers to be activated. */
65 int *writer_nums;
66 /** pointer to the array of writer configurations */
67 void **writer_conf;
68 /** do not start receiver/filters/writer before this time */
69 struct timeval restart_barrier;
70 };
71
72 /* Describes one instance of a receiver-filter-writer chain. */
73 struct slot_info {
74 /* Number of the audio format in this slot. */
75 int format;
76 /* The stream_start status item announced by para_server. */
77 struct timeval server_stream_start;
78 /* The offset status item announced by para_server. */
79 unsigned offset_seconds;
80 /* The seconds_total status item announced by para_server. */
81 unsigned seconds_total;
82 /* The receiver info associated with this slot. */
83 struct receiver_node *receiver_node;
84 /* The array of filter nodes. */
85 struct filter_node *fns;
86 /* The array of writers attached to the last filter. */
87 struct writer_node *wns;
88 };
89
90 /** Maximal number of simultaneous instances. */
91 #define MAX_STREAM_SLOTS 5
92
93 /** Iterate over all slots. */
94 #define FOR_EACH_SLOT(_slot) for (_slot = 0; _slot < MAX_STREAM_SLOTS; _slot++)
95
96 /**
97 * para_audiod uses \p MAX_STREAM_SLOTS different slots, each of which may
98 * be associated with a receiver/filter/writer triple. This array holds all
99 * information on the status of these slots.
100 *
101 * \sa struct slot_info
102 * */
103 struct slot_info slot[MAX_STREAM_SLOTS];
104
105 /** The vss status flags audiod is interested in. */
106 enum vss_status_flags {
107 /** Whether the 'N' flag is set. */
108 VSS_STATUS_FLAG_NEXT = 1,
109 /** The 'P' flag is set. */
110 VSS_STATUS_FLAG_PLAYING = 2,
111 };
112
113 /**
114 * The scheduler instance of para_audiod.
115 *
116 * This is needed also in audiod_command.c (for the tasks command), so it can
117 * not be made static.
118 */
119 struct sched sched = {.max_fileno = 0};
120
121 /* The task for obtaining para_server's status (para_client stat). */
122 struct status_task {
123 /** The associated task structure of audiod. */
124 struct task *task;
125 /** Client data associated with the stat task. */
126 struct client_task *ct;
127 /** Do not restart client command until this time. */
128 struct timeval restart_barrier;
129 /** Last time we received status data from para_server. */
130 struct timeval last_status_read;
131 size_t min_iqs;
132 /** The offset value announced by para_server. */
133 int offset_seconds;
134 /** The length of the current audio file as announced by para_server. */
135 int length_seconds;
136 /** The start of the current stream from the view of para_server. */
137 struct timeval server_stream_start;
138 /** The average time deviation between para_server and para_audiod. */
139 struct timeval sa_time_diff;
140 /** Whether client time is ahead of server time. */
141 int sa_time_diff_sign;
142 /** The 'P' and the 'N' flags as announced by para_server. */
143 enum vss_status_flags vss_status;
144 /** Number of times the clock difference is to be checked. */
145 unsigned clock_diff_count;
146 /** When to start the next check for clock difference. */
147 struct timeval clock_diff_barrier;
148 /** Number of the audio format as announced by para_server. */
149 int current_audio_format_num;
150 /* The status task btrn is the child of the client task. */
151 struct btr_node *btrn;
152 };
153
154 /** The array of status items sent by para_server. */
155 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
156
157 /**
158 * the current mode of operation of which can be changed by the on/off/cycle
159 * commands. It is either, AUDIOD_OFF, AUDIOD_ON or AUDIOD_STANDBY.
160 */
161 int audiod_status = AUDIOD_ON;
162
163 /**
164 * the gengetopt args_info struct that holds information on all command line
165 * arguments
166 */
167 struct audiod_args_info conf;
168
169 static char *socket_name;
170 static struct audio_format_info afi[NUM_AUDIO_FORMATS];
171
172 static struct signal_task *signal_task;
173
174 static struct status_task status_task_struct;
175
176 static uid_t *uid_whitelist;
177
178 /**
179 * the task that calls the status command of para_server
180 *
181 * \sa struct status_task
182 */
183 static struct status_task *stat_task = &status_task_struct;
184
185 /*
186 * The task for handling audiod commands.
187 *
188 * We need two listening sockets for backward compability: on Linux systems
189 * fd[0] is an abstract socket (more precisely, a socket bound to an address in
190 * the abstract namespace), and fd[1] is the usual pathname socket. On other
191 * systems, fd[0] is negative, and only the pathname socket is used.
192 *
193 * For 0.5.x we accept connections on both sockets to make sure that old
194 * para_audioc versions can still connect. New versions use only the abstract
195 * socket. Hence after v0.6.0 we can go back to a single socket, either an
196 * abstract one (Linux) or a pathname socket (all other systems).
197 */
198 struct command_task {
199 /** The local listening sockets. */
200 int fd[2];
201 /** the associated task structure */
202 struct task *task;
203 };
204
205 /** iterate over all supported audio formats */
206 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
207
208 /**
209 * Get the audio format number.
210 *
211 * \param name The name of the audio format.
212 *
213 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
214 * \a name is not a supported audio format.
215 */
216 static int get_audio_format_num(const char *name)
217 {
218 int i;
219
220 while (para_isspace(*name))
221 name++;
222 FOR_EACH_AUDIO_FORMAT(i)
223 if (!strcmp(name, audio_formats[i]))
224 return i;
225 return -E_UNSUPPORTED_AUDIO_FORMAT;
226 }
227
228 /**
229 * Return the flags for the \a decoder_flags status item.
230 *
231 * Allocates a string which contains one octal digit per slot. Bit zero (value
232 * 1) is set if a receiver is active. Bit one (value 2) and bit three (value 4)
233 * have the analogous meaning for filter and writer, respectively.
234 *
235 * \return String that must be freed by the caller.
236 */
237 __malloc char *audiod_get_decoder_flags(void)
238 {
239 int i;
240 char flags[MAX_STREAM_SLOTS + 1];
241
242 FOR_EACH_SLOT(i) {
243 struct slot_info *s = &slot[i];
244 char flag = '0';
245 if (s->receiver_node)
246 flag += 1;
247 if (s->fns)
248 flag += 2;
249 if (s->wns)
250 flag += 4;
251 flags[i] = flag;
252 }
253 flags[MAX_STREAM_SLOTS] = '\0';
254 return para_strdup(flags);
255 }
256
257 static int get_matching_audio_format_nums(const char *re)
258 {
259 int i, ret;
260 regex_t preg;
261
262 ret = para_regcomp(&preg, re, REG_EXTENDED | REG_NOSUB);
263 if (ret < 0)
264 return ret;
265 ret = 0;
266 FOR_EACH_AUDIO_FORMAT(i)
267 if (regexec(&preg, audio_formats[i], 0, NULL, 0) != REG_NOMATCH)
268 ret |= (1 << i);
269 regfree(&preg);
270 return ret;
271 }
272
273 static int get_play_time_slot_num(void)
274 {
275 int i, oldest_slot = -1;
276 struct timeval oldest_wstime = {0, 0};
277
278 FOR_EACH_SLOT(i) {
279 struct slot_info *s = &slot[i];
280 struct timeval wstime;
281 if (!s->wns || !s->wns[0].btrn)
282 continue;
283 btr_get_node_start(s->wns[0].btrn, &wstime);
284 if (oldest_slot >= 0 && tv_diff(&wstime, &oldest_wstime, NULL) > 0)
285 continue;
286 oldest_wstime = wstime;
287 oldest_slot = i;
288 }
289 return oldest_slot;
290 }
291
292 /**
293 * Compute the play time based on information of the current slot.
294 *
295 * This computes a string of the form "0:07 [3:33] (3%/3:40)" using information
296 * from the status items received from para_server and the start time of the
297 * (first) writer of the current slot.
298 *
299 * It has to take into account that the stream was probably not started at
300 * the beginning of the file, that the clock between the server and the client
301 * host may differ and that playback of the stream was delayed, e.g. because
302 * the prebuffer filter is used in the filter configuration.
303 *
304 * If no writer is active, for example because para_audiod runs in standby
305 * mode, an approximation based only on the status items is computed and the
306 * returned string is prefixed with "~".
307 *
308 * \return A string that must be freed by the caller.
309 */
310 char *get_time_string(void)
311 {
312 int ret, seconds = 0, length;
313 struct timeval *tmp, sum, sss, /* server stream start */
314 rstime, /* receiver start time */
315 wstime, /* writer start time */
316 wtime, /* now - writer start */
317 rskip; /* receiver start - sss */
318 int slot_num = get_play_time_slot_num();
319 struct slot_info *s = slot_num < 0? NULL : &slot[slot_num];
320 char *msg;
321
322 if (audiod_status == AUDIOD_OFF)
323 goto empty;
324 if (!(stat_task->vss_status & VSS_STATUS_FLAG_PLAYING)) {
325 if (stat_task->length_seconds) /* paused */
326 return NULL;
327 goto empty; /* stopped */
328 }
329 /*
330 * Valid status items and playing, set length and tmp to the stream
331 * start. We use the slot info and fall back to the info from current
332 * status items if no slot info is available.
333 */
334 length = stat_task->length_seconds;
335 tmp = &stat_task->server_stream_start;
336 if (s && s->wns && s->wns[0].btrn) { /* writer active in this slot */
337 btr_get_node_start(s->wns[0].btrn, &wstime);
338 if (wstime.tv_sec != 0) { /* writer wrote something */
339 if (s->server_stream_start.tv_sec == 0) {
340 /* copy status info to slot */
341 s->server_stream_start = stat_task->server_stream_start;
342 s->offset_seconds = stat_task->offset_seconds;
343 s->seconds_total = stat_task->length_seconds;
344 }
345 length = s->seconds_total;
346 tmp = &s->server_stream_start;
347 }
348 }
349 if (stat_task->sa_time_diff_sign > 0)
350 tv_diff(tmp, &stat_task->sa_time_diff, &sss);
351 else
352 tv_add(tmp, &stat_task->sa_time_diff, &sss);
353 if (!s || !s->wns || !s->wns[0].btrn) {
354 struct timeval diff;
355 tv_diff(now, &sss, &diff);
356 seconds = diff.tv_sec + stat_task->offset_seconds;
357 goto out;
358 }
359 tv_diff(now, &wstime, &wtime);
360 //PARA_CRIT_LOG("offset %d\n", s->offset_seconds);
361 seconds = s->offset_seconds;
362 if (s->receiver_node->btrn) {
363 btr_get_node_start(s->receiver_node->btrn, &rstime);
364 ret = tv_diff(&rstime, &sss, &rskip);
365 if (ret > 0) { /* audiod was started in the middle of the stream */
366 tv_add(&wtime, &rskip, &sum);
367 seconds += sum.tv_sec;
368 } else
369 seconds += wtime.tv_sec;
370 } else
371 seconds += wtime.tv_sec;
372 out:
373 seconds = PARA_MIN(seconds, length);
374 seconds = PARA_MAX(seconds, 0);
375 msg = make_message(
376 "%s%d:%02d [%d:%02d] (%d%%/%d:%02d)",
377 s? "" : "~",
378 seconds / 60,
379 seconds % 60,
380 (length - seconds) / 60,
381 (length - seconds) % 60,
382 length? (seconds * 100 + length / 2) / length : 0,
383 length / 60,
384 length % 60
385 );
386 //PARA_DEBUG_LOG("slot %d: %s\n", slot_num, msg);
387 return msg;
388 empty:
389 return para_strdup(NULL);
390 }
391
392 static void parse_config_or_die(void)
393 {
394 int ret, i;
395 char *config_file;
396 struct audiod_cmdline_parser_params params = {
397 .override = 0,
398 .initialize = 0,
399 .check_required = 1,
400 .check_ambiguity = 0,
401 .print_errors = 1
402 };
403
404 if (conf.config_file_given)
405 config_file = para_strdup(conf.config_file_arg);
406 else {
407 char *home = para_homedir();
408 config_file = make_message("%s/.paraslash/audiod.conf", home);
409 free(home);
410 }
411 ret = file_exists(config_file);
412 if (conf.config_file_given && !ret) {
413 PARA_EMERG_LOG("can not read config file %s\n", config_file);
414 free(config_file);
415 goto err;
416 }
417 if (ret) {
418 audiod_cmdline_parser_config_file(config_file, &conf, &params);
419 daemon_set_loglevel(conf.loglevel_arg);
420 }
421 free(config_file);
422 if (conf.user_allow_given > 0) {
423 uid_whitelist = para_malloc(conf.user_allow_given
424 * sizeof(uid_t));
425 for (i = 0; i < conf.user_allow_given; i++) {
426 int32_t val;
427 struct passwd *pw;
428 ret = para_atoi32(conf.user_allow_arg[i], &val);
429 if (ret >= 0) {
430 uid_whitelist[i] = val;
431 continue;
432 }
433 errno = 0; /* see getpwnam(3) */
434 pw = getpwnam(conf.user_allow_arg[i]);
435 if (!pw) {
436 PARA_EMERG_LOG("invalid username: %s\n",
437 conf.user_allow_arg[i]);
438 goto err;
439 }
440 uid_whitelist[i] = pw->pw_uid;
441 }
442 }
443 return;
444 err:
445 exit(EXIT_FAILURE);
446 }
447
448 static void setup_signal_handling(void)
449 {
450 signal_task = signal_init_or_die();
451 para_install_sighandler(SIGINT);
452 para_install_sighandler(SIGTERM);
453 para_install_sighandler(SIGHUP);
454 para_sigaction(SIGPIPE, SIG_IGN);
455 }
456
457 static void clear_slot(int slot_num)
458 {
459 struct slot_info *s = &slot[slot_num];
460
461 PARA_INFO_LOG("clearing slot %d\n", slot_num);
462 memset(s, 0, sizeof(struct slot_info));
463 s->format = -1;
464 }
465
466 static void close_receiver(int slot_num)
467 {
468 struct slot_info *s = &slot[slot_num];
469 struct audio_format_info *a;
470
471 if (s->format < 0 || !s->receiver_node)
472 return;
473 a = &afi[s->format];
474 PARA_NOTICE_LOG("closing %s receiver in slot %d\n",
475 audio_formats[s->format], slot_num);
476 a->receiver->close(s->receiver_node);
477 btr_remove_node(&s->receiver_node->btrn);
478 task_reap(&s->receiver_node->task);
479 free(s->receiver_node);
480 s->receiver_node = NULL;
481 stat_task->current_audio_format_num = -1;
482 tv_add(now, &(struct timeval)EMBRACE(0, 200 * 1000),
483 &a->restart_barrier);
484 }
485
486 static void writer_cleanup(struct writer_node *wn)
487 {
488 struct writer *w;
489
490 if (!wn)
491 return;
492 w = writers + wn->writer_num;
493 PARA_INFO_LOG("closing %s\n", writer_names[wn->writer_num]);
494 w->close(wn);
495 btr_remove_node(&wn->btrn);
496 task_reap(&wn->task);
497 }
498
499 static void close_writers(struct slot_info *s)
500 {
501 struct audio_format_info *a;
502 int i;
503
504 if (s->format < 0)
505 return;
506 assert(s->wns);
507 a = afi + s->format;
508 if (a->num_writers == 0)
509 writer_cleanup(s->wns);
510 else {
511 for (i = 0; i < a->num_writers; i++)
512 writer_cleanup(s->wns + i);
513 }
514 free(s->wns);
515 s->wns = NULL;
516 }
517
518 static void close_filters(struct slot_info *s)
519 {
520 int i;
521 struct audio_format_info *a = afi + s->format;
522 if (a->num_filters == 0)
523 return;
524 for (i = a->num_filters - 1; i >= 0; i--) {
525 struct filter_node *fn = s->fns + i;
526 const struct filter *f;
527
528 if (!fn)
529 continue;
530 f = filter_get(fn->filter_num);
531 if (f->close)
532 f->close(fn);
533 btr_remove_node(&fn->btrn);
534 task_reap(&fn->task);
535 }
536 free(s->fns);
537 s->fns = NULL;
538 }
539
540 static void notify_receivers(int error)
541 {
542 int i;
543
544 FOR_EACH_SLOT(i) {
545 struct slot_info *s = slot + i;
546 if (s->format < 0)
547 continue;
548 if (!s->receiver_node)
549 continue;
550 task_notify(s->receiver_node->task, error);
551 }
552 }
553
554 static int get_empty_slot(void)
555 {
556 int i;
557 struct slot_info *s;
558
559 FOR_EACH_SLOT(i) {
560 s = &slot[i];
561 if (s->format < 0) {
562 clear_slot(i);
563 return i;
564 }
565 if (s->wns || s->receiver_node || s->fns)
566 continue;
567 clear_slot(i);
568 return i;
569 }
570 return -E_NO_MORE_SLOTS;
571 }
572
573 static void open_filters(struct slot_info *s)
574 {
575 struct audio_format_info *a = afi + s->format;
576 struct filter_node *fn;
577 int nf = a->num_filters;
578 struct btr_node *parent;
579 int i;
580
581 if (nf == 0)
582 return;
583 PARA_INFO_LOG("opening %s filters\n", audio_formats[s->format]);
584 assert(s->fns == NULL);
585 s->fns = para_calloc(nf * sizeof(struct filter_node));
586 parent = s->receiver_node->btrn;
587 for (i = 0; i < nf; i++) {
588 char buf[20];
589 const struct filter *f = filter_get(a->filter_nums[i]);
590 fn = s->fns + i;
591 fn->filter_num = a->filter_nums[i];
592 fn->conf = a->filter_conf[i];
593 fn->btrn = btr_new_node(&(struct btr_node_description)
594 EMBRACE(.name = f->name, .parent = parent,
595 .handler = f->execute, .context = fn));
596
597 f->open(fn);
598 sprintf(buf, "%s (slot %d)", f->name, (int)(s - slot));
599 fn->task = task_register(&(struct task_info) {
600 .name = buf,
601 .pre_select = f->pre_select,
602 .post_select = f->post_select,
603 .context = fn,
604 }, &sched);
605 parent = fn->btrn;
606 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
607 audio_formats[s->format], i, nf, f->name, (int)(s - slot));
608 }
609 }
610
611 static void open_writers(struct slot_info *s)
612 {
613 int i;
614 struct audio_format_info *a = afi + s->format;
615 struct writer_node *wn;
616 struct btr_node *parent = s->fns[a->num_filters - 1].btrn;
617
618 assert(s->wns == NULL);
619 s->wns = para_calloc(PARA_MAX(1U, a->num_writers)
620 * sizeof(struct writer_node));
621 for (i = 0; i < a->num_writers; i++) {
622 wn = s->wns + i;
623 wn->conf = a->writer_conf[i];
624 wn->writer_num = a->writer_nums[i];
625 register_writer_node(wn, parent, &sched);
626 PARA_NOTICE_LOG("%s writer started in slot %d\n",
627 writer_names[a->writer_nums[i]], (int)(s - slot));
628 }
629 }
630
631 /* returns slot num on success */
632 static int open_receiver(int format)
633 {
634 struct audio_format_info *a = &afi[format];
635 struct slot_info *s;
636 int ret, slot_num;
637 struct receiver *r = a->receiver;
638 struct receiver_node *rn;
639
640 tv_add(now, &(struct timeval)EMBRACE(2, 0), &a->restart_barrier);
641 ret = get_empty_slot();
642 if (ret < 0)
643 return ret;
644 slot_num = ret;
645 rn = para_calloc(sizeof(*rn));
646 rn->receiver = r;
647 rn->conf = a->receiver_conf;
648 rn->btrn = btr_new_node(&(struct btr_node_description)
649 EMBRACE(.name = r->name, .context = rn));
650 ret = r->open(rn);
651 if (ret < 0) {
652 btr_remove_node(&rn->btrn);
653 free(rn);
654 return ret;
655 }
656 s = &slot[slot_num];
657 s->format = format;
658 s->receiver_node = rn;
659 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
660 audio_formats[format], r->name, slot_num);
661 rn->task = task_register(&(struct task_info) {
662 .name = r->name,
663 .pre_select = r->pre_select,
664 .post_select = r->post_select,
665 .context = rn,
666 }, &sched);
667 return slot_num;
668 }
669
670 static bool receiver_running(void)
671 {
672 int i;
673 long unsigned ss1 = stat_task->server_stream_start.tv_sec;
674
675 FOR_EACH_SLOT(i) {
676 struct slot_info *s = &slot[i];
677 long unsigned ss2 = s->server_stream_start.tv_sec;
678
679 if (!s->receiver_node)
680 continue;
681 if (task_status(s->receiver_node->task) >= 0)
682 return true;
683 if (ss1 == ss2)
684 return true;
685 }
686 return false;
687 }
688
689 /**
690 * Return the root node of the current buffer tree.
691 *
692 * This is only used for stream grabbing.
693 *
694 * \return \p NULL if no slot is currently active. If more than one buffer tree
695 * exists, the node corresponding to the most recently started receiver is
696 * returned.
697 */
698 struct btr_node *audiod_get_btr_root(void)
699 {
700 int i, newest_slot = -1;
701 struct timeval newest_rstime = {0, 0};
702
703 FOR_EACH_SLOT(i) {
704 struct slot_info *s = &slot[i];
705 struct timeval rstime;
706 if (!s->receiver_node)
707 continue;
708 if (task_status(s->receiver_node->task) < 0)
709 continue;
710 btr_get_node_start(s->receiver_node->btrn, &rstime);
711 if (newest_slot >= 0 && tv_diff(&rstime, &newest_rstime, NULL) < 0)
712 continue;
713 newest_rstime = rstime;
714 newest_slot = i;
715 }
716 if (newest_slot == -1)
717 return NULL;
718 return slot[newest_slot].receiver_node->btrn;
719 }
720
721 /* whether a new instance of a decoder should be started. */
722 static bool must_start_decoder(void)
723 {
724 int cafn = stat_task->current_audio_format_num;
725 unsigned vs = stat_task->vss_status;
726
727 if (audiod_status != AUDIOD_ON)
728 return false;
729 if (cafn < 0)
730 return false;
731 if (!stat_task->ct)
732 return false;
733 if (vs & VSS_STATUS_FLAG_NEXT)
734 return false;
735 if (!(vs & VSS_STATUS_FLAG_PLAYING))
736 return false;
737 if (receiver_running())
738 return false;
739 if (tv_diff(now, &afi[cafn].restart_barrier, NULL) < 0)
740 return false;
741 return true;
742 }
743
744 static void compute_time_diff(const struct timeval *status_time)
745 {
746 struct timeval tmp, diff;
747 static unsigned count;
748 int sign, sa_time_diff_sign = stat_task->sa_time_diff_sign;
749 const struct timeval max_deviation = {0, 500 * 1000};
750 const int time_smooth = 5;
751
752 sign = tv_diff(status_time, now, &diff);
753 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
754 // sign, sa_time_diff_sign);
755 if (!count) {
756 sa_time_diff_sign = sign;
757 stat_task->sa_time_diff = diff;
758 count++;
759 goto out;
760 }
761 if (count > 5) {
762 int s = tv_diff(&diff, &stat_task->sa_time_diff, &tmp);
763 if (tv_diff(&max_deviation, &tmp, NULL) < 0)
764 PARA_WARNING_LOG("time diff jump: %lims\n",
765 s * tv2ms(&tmp));
766 }
767 count++;
768 sa_time_diff_sign = tv_convex_combination(
769 sa_time_diff_sign * time_smooth, &stat_task->sa_time_diff,
770 count > 10? sign : sign * time_smooth, &diff,
771 &tmp);
772 stat_task->sa_time_diff = tmp;
773 PARA_INFO_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
774 sign < 0? "-" : "+",
775 tv2ms(&diff),
776 sa_time_diff_sign < 0? "-" : "+",
777 tv2ms(&stat_task->sa_time_diff)
778 );
779 out:
780 stat_task->sa_time_diff_sign = sa_time_diff_sign;
781 }
782
783 static int update_item(int itemnum, char *buf)
784 {
785 long unsigned sec, usec;
786
787 if (stat_task->clock_diff_count && itemnum != SI_CURRENT_TIME)
788 return 1;
789 free(stat_item_values[itemnum]);
790 stat_item_values[itemnum] = para_strdup(buf);
791 stat_client_write_item(itemnum);
792 switch (itemnum) {
793 case SI_STATUS_FLAGS:
794 stat_task->vss_status = 0;
795 if (strchr(buf, 'N'))
796 stat_task->vss_status |= VSS_STATUS_FLAG_NEXT;
797 if (strchr(buf, 'P'))
798 stat_task->vss_status |= VSS_STATUS_FLAG_PLAYING;
799 break;
800 case SI_OFFSET:
801 stat_task->offset_seconds = atoi(buf);
802 break;
803 case SI_SECONDS_TOTAL:
804 stat_task->length_seconds = atoi(buf);
805 break;
806 case SI_STREAM_START:
807 if (sscanf(buf, "%lu.%lu", &sec, &usec) == 2) {
808 stat_task->server_stream_start.tv_sec = sec;
809 stat_task->server_stream_start.tv_usec = usec;
810 }
811 break;
812 case SI_CURRENT_TIME:
813 if (sscanf(buf, "%lu.%lu", &sec, &usec) == 2) {
814 struct timeval tv = {sec, usec};
815 compute_time_diff(&tv);
816 }
817 break;
818 case SI_FORMAT:
819 stat_task->current_audio_format_num
820 = get_audio_format_num(buf);
821 }
822 return 1;
823 }
824
825 static int parse_stream_command(const char *txt, char **cmd)
826 {
827 int ret, len;
828 char *re, *p = strchr(txt, ':');
829
830 if (!p)
831 return -E_MISSING_COLON;
832 *cmd = p + 1;
833 len = p - txt;
834 re = malloc(len + 1);
835 strncpy(re, txt, len);
836 re[len] = '\0';
837 ret = get_matching_audio_format_nums(re);
838 free(re);
839 return ret;
840 }
841
842 static int add_filter(int format, char *cmdline)
843 {
844 struct audio_format_info *a = &afi[format];
845 int filter_num, nf = a->num_filters;
846 void *cfg;
847
848 filter_num = check_filter_arg(cmdline, &cfg);
849 if (filter_num < 0)
850 return filter_num;
851 a->filter_conf = para_realloc(a->filter_conf,
852 (nf + 1) * sizeof(void *));
853 a->filter_nums = para_realloc(a->filter_nums,
854 (nf + 1) * sizeof(unsigned));
855 a->filter_nums[nf] = filter_num;
856 a->filter_conf[nf] = cfg;
857 a->num_filters++;
858 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats[format], nf,
859 filter_get(filter_num)->name);
860 return filter_num;
861 }
862
863 static int parse_writer_args(void)
864 {
865 int i, ret;
866 char *cmd;
867 struct audio_format_info *a;
868
869 for (i = 0; i < conf.writer_given; i++) {
870 void *wconf;
871 int j, nw, writer_num, af_mask;
872
873 ret = parse_stream_command(conf.writer_arg[i], &cmd);
874 if (ret < 0)
875 return ret;
876 af_mask = ret;
877 FOR_EACH_AUDIO_FORMAT(j) {
878 a = afi + j;
879 if ((af_mask & (1 << j)) == 0) /* no match */
880 continue;
881 wconf = check_writer_arg_or_die(cmd, &writer_num);
882 nw = a->num_writers;
883 a->writer_nums = para_realloc(a->writer_nums, (nw + 1) * sizeof(int));
884 a->writer_conf = para_realloc(a->writer_conf, (nw + 1) * sizeof(void *));
885 a->writer_nums[nw] = writer_num;
886 a->writer_conf[nw] = wconf;
887 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats[j],
888 nw, writer_names[writer_num]);
889 a->num_writers++;
890 }
891 }
892 /* Use default writer for audio formats which are not yet set up. */
893 FOR_EACH_AUDIO_FORMAT(i) {
894 void *writer_conf;
895 int writer_num;
896 a = afi + i;
897 if (a->num_writers > 0)
898 continue; /* already set up */
899 writer_conf = check_writer_arg_or_die(NULL, &writer_num);
900 a->writer_nums = para_malloc(sizeof(int));
901 a->writer_nums[0] = writer_num;
902 a->writer_conf = para_malloc(sizeof(void *));
903 a->writer_conf[0] = writer_conf;
904 a->num_writers = 1;
905 PARA_INFO_LOG("%s writer: %s (default)\n", audio_formats[i],
906 writer_names[writer_num]);
907 }
908 return 1;
909 }
910
911 static int parse_receiver_args(void)
912 {
913 int i, ret, receiver_num;
914 char *cmd = NULL;
915 struct audio_format_info *a;
916
917 for (i = conf.receiver_given - 1; i >= 0; i--) {
918 char *arg;
919 int j, af_mask;
920
921 ret = parse_stream_command(conf.receiver_arg[i], &arg);
922 if (ret < 0)
923 goto out;
924 af_mask = ret;
925 FOR_EACH_AUDIO_FORMAT(j) {
926 a = afi + j;
927 if ((af_mask & (1 << j)) == 0) /* no match */
928 continue;
929 /*
930 * If multiple receivers are given for this audio format, the
931 * last one wins and we have to free the previous receiver
932 * config here. Since we are iterating backwards, the winning
933 * receiver arg is in fact the first one given.
934 */
935 if (a->receiver_conf)
936 a->receiver->free_config(a->receiver_conf);
937 a->receiver_conf = check_receiver_arg(arg, &receiver_num);
938 ret = -E_RECV_SYNTAX;
939 if (!a->receiver_conf)
940 goto out;
941 a->receiver = receivers + receiver_num;
942 }
943 }
944 /*
945 * Use the first available receiver with no arguments for those audio
946 * formats for which no receiver was specified.
947 */
948 cmd = para_strdup(receivers[0].name);
949 FOR_EACH_AUDIO_FORMAT(i) {
950 a = &afi[i];
951 if (a->receiver_conf)
952 continue;
953 a->receiver_conf = check_receiver_arg(cmd, &receiver_num);
954 if (!a->receiver_conf)
955 return -E_RECV_SYNTAX;
956 a->receiver = &receivers[receiver_num];
957 }
958 FOR_EACH_AUDIO_FORMAT(i) {
959 a = afi + i;
960 PARA_INFO_LOG("receiving %s streams via %s receiver\n",
961 audio_formats[i], a->receiver->name);
962 }
963 ret = 1;
964 out:
965 free(cmd);
966 return ret;
967 }
968
969 static int init_default_filters(void)
970 {
971 int i, ret = 1;
972
973 FOR_EACH_AUDIO_FORMAT(i) {
974 struct audio_format_info *a = &afi[i];
975 char *tmp;
976 int j;
977
978 if (a->num_filters)
979 continue; /* no default -- nothing to to */
980 /*
981 * udp and dccp streams are fec-encoded, so add fecdec as the
982 * first filter.
983 */
984 if (strcmp(afi[i].receiver->name, "udp") == 0 ||
985 strcmp(afi[i].receiver->name, "dccp") == 0) {
986 tmp = para_strdup("fecdec");
987 add_filter(i, tmp);
988 free(tmp);
989 if (ret < 0)
990 goto out;
991 }
992 /* add "dec" to audio format name */
993 tmp = make_message("%sdec", audio_formats[i]);
994 for (j = 0; filter_get(j); j++)
995 if (!strcmp(tmp, filter_get(j)->name))
996 break;
997 free(tmp);
998 ret = -E_UNSUPPORTED_FILTER;
999 if (!filter_get(j))
1000 goto out;
1001 tmp = para_strdup(filter_get(j)->name);
1002 ret = add_filter(i, tmp);
1003 free(tmp);
1004 if (ret < 0)
1005 goto out;
1006 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats[i],
1007 filter_get(j)->name);
1008 }
1009 out:
1010 return ret;
1011 }
1012
1013 static int parse_filter_args(void)
1014 {
1015 int i, j, ret, af_mask, num_matches;
1016
1017 for (i = 0; i < conf.filter_given; i++) {
1018 char *arg;
1019 ret = parse_stream_command(conf.filter_arg[i], &arg);
1020 if (ret < 0)
1021 goto out;
1022 af_mask = ret;
1023 num_matches = 0;
1024 FOR_EACH_AUDIO_FORMAT(j) {
1025 if ((af_mask & (1 << j)) == 0) /* no match */
1026 continue;
1027 ret = add_filter(j, arg);
1028 if (ret < 0)
1029 goto out;
1030 num_matches++;
1031 }
1032 if (num_matches == 0)
1033 PARA_WARNING_LOG("ignoring filter spec: %s\n",
1034 conf.filter_arg[i]);
1035 }
1036 ret = init_default_filters(); /* use default values for the rest */
1037 out:
1038 return ret;
1039 }
1040
1041 static int parse_stream_args(void)
1042 {
1043 int ret;
1044
1045 ret = parse_receiver_args();
1046 if (ret < 0)
1047 return ret;
1048 ret = parse_filter_args();
1049 if (ret < 0)
1050 return ret;
1051 ret = parse_writer_args();
1052 if (ret < 0)
1053 return ret;
1054 return 1;
1055 }
1056
1057 /* does not unlink socket on errors */
1058 static void init_local_sockets(struct command_task *ct)
1059 {
1060 if (conf.socket_given)
1061 socket_name = para_strdup(conf.socket_arg);
1062 else {
1063 char *hn = para_hostname();
1064 socket_name = make_message("/var/paraslash/audiod_socket.%s",
1065 hn);
1066 free(hn);
1067 }
1068 PARA_NOTICE_LOG("local socket: %s\n", socket_name);
1069 if (conf.force_given)
1070 unlink(socket_name);
1071 ct->fd[0] = create_local_socket(socket_name, 0);
1072 ct->fd[1] = create_local_socket(socket_name,
1073 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
1074 if (ct->fd[0] >= 0 || ct->fd[1] >= 0)
1075 return;
1076 PARA_EMERG_LOG("%s\n", para_strerror(-ct->fd[1]));
1077 exit(EXIT_FAILURE);
1078 }
1079
1080 static int signal_post_select(struct sched *s, void *context)
1081 {
1082 struct signal_task *st = context;
1083 int ret, signum;
1084
1085 ret = task_get_notification(st->task);
1086 if (ret < 0)
1087 return ret;
1088 signum = para_next_signal(&s->rfds);
1089 switch (signum) {
1090 case SIGINT:
1091 case SIGTERM:
1092 case SIGHUP:
1093 PARA_NOTICE_LOG("received signal %d\n", signum);
1094 task_notify_all(s, E_AUDIOD_SIGNAL);
1095 return -E_AUDIOD_SIGNAL;
1096 }
1097 return 0;
1098 }
1099
1100 static void command_pre_select(struct sched *s, void *context)
1101 {
1102 struct command_task *ct = context;
1103 int i;
1104
1105 for (i = 0; i < 2; i++)
1106 if (ct->fd[i] >= 0)
1107 para_fd_set(ct->fd[i], &s->rfds, &s->max_fileno);
1108 }
1109
1110 static int command_post_select(struct sched *s, void *context)
1111 {
1112 int ret, i;
1113 struct command_task *ct = context;
1114 static struct timeval last_status_dump;
1115 struct timeval tmp, delay;
1116 bool force = false;
1117
1118 ret = task_get_notification(ct->task);
1119 if (ret < 0)
1120 return ret;
1121 for (i = 0; i < 2; i++) {
1122 if (ct->fd[i] < 0)
1123 continue;
1124 ret = handle_connect(ct->fd[i], &s->rfds, uid_whitelist);
1125 if (ret < 0) {
1126 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1127 if (ret == -E_AUDIOD_TERM) {
1128 task_notify_all(s, -ret);
1129 return ret;
1130 }
1131 } else if (ret > 0)
1132 force = true;
1133 }
1134 if (force == true)
1135 goto dump;
1136
1137 /* if last status dump was less than 500ms ago, do nothing */
1138 delay.tv_sec = 0;
1139 delay.tv_usec = 500 * 1000;
1140 tv_add(&last_status_dump, &delay, &tmp);
1141 if (tv_diff(now, &tmp, NULL) < 0)
1142 return 0;
1143
1144 /*
1145 * If last status dump was more than 5s ago, force update. Otherwise,
1146 * update only those items that have changed.
1147 */
1148 delay.tv_sec = 5;
1149 delay.tv_usec = 0;
1150 tv_add(&last_status_dump, &delay, &tmp);
1151 if (tv_diff(now, &tmp, NULL) > 0)
1152 force = true;
1153 dump:
1154 audiod_status_dump(force);
1155 last_status_dump = *now;
1156 return 1;
1157 }
1158
1159 static void init_command_task(struct command_task *ct)
1160 {
1161 init_local_sockets(ct); /* doesn't return on errors */
1162
1163 ct->task = task_register(&(struct task_info) {
1164 .name = "command",
1165 .pre_select = command_pre_select,
1166 .post_select = command_post_select,
1167 .context = ct,
1168 }, &sched);
1169 }
1170
1171 static void close_stat_pipe(void)
1172 {
1173 if (!stat_task->ct)
1174 return;
1175 task_reap(&stat_task->ct->task);
1176 client_close(stat_task->ct);
1177 stat_task->ct = NULL;
1178 clear_and_dump_items();
1179 stat_task->length_seconds = 0;
1180 stat_task->offset_seconds = 0;
1181 stat_task->vss_status = 0;
1182 stat_task->current_audio_format_num = -1;
1183 audiod_status_dump(true);
1184 }
1185
1186 /* avoid busy loop if server is down */
1187 static void set_stat_task_restart_barrier(unsigned seconds)
1188 {
1189 struct timeval delay = {seconds, 0};
1190 tv_add(now, &delay, &stat_task->restart_barrier);
1191 }
1192
1193 static bool must_close_slot(int slot_num)
1194 {
1195 struct slot_info *s = &slot[slot_num];
1196 struct audio_format_info *a = afi + s->format;
1197 int i;
1198
1199 if (s->format < 0)
1200 return false;
1201 if (s->receiver_node && task_status(s->receiver_node->task) >= 0)
1202 return false;
1203 for (i = 0; i < a->num_filters; i++)
1204 if (s->fns && task_status(s->fns[i].task) >= 0)
1205 return false;
1206 if (a->num_writers > 0) {
1207 for (i = 0; i < a->num_writers; i++)
1208 if (s->wns && task_status(s->wns[i].task) >= 0)
1209 return false;
1210 } else {
1211 if (s->wns && task_status(s->wns[0].task) >= 0)
1212 return false;
1213 }
1214 return true;
1215 }
1216
1217 static void close_slot(int slot_num)
1218 {
1219 struct slot_info *s = slot + slot_num;
1220
1221 PARA_INFO_LOG("closing slot %d\n", slot_num);
1222 close_writers(s);
1223 close_filters(s);
1224 close_receiver(slot_num);
1225 clear_slot(slot_num);
1226 }
1227
1228 static void close_unused_slots(void)
1229 {
1230 int i;
1231
1232 FOR_EACH_SLOT(i)
1233 if (must_close_slot(i))
1234 close_slot(i);
1235 }
1236
1237 /*
1238 * Cleanup all resources.
1239 *
1240 * This performs various cleanups, removes the audiod socket and closes the
1241 * connection to para_server.
1242 */
1243 static void audiod_cleanup(void)
1244 {
1245 if (socket_name)
1246 unlink(socket_name);
1247 close_stat_pipe();
1248 close_unused_slots();
1249 audiod_cmdline_parser_free(&conf);
1250 close_stat_clients();
1251 free(uid_whitelist);
1252 }
1253
1254 /*
1255 * Check if any receivers/filters/writers need to be started and do so if
1256 * necessary.
1257 */
1258 static void start_stop_decoders(void)
1259 {
1260 int ret;
1261 struct slot_info *sl;
1262
1263 close_unused_slots();
1264 if (audiod_status != AUDIOD_ON ||
1265 !(stat_task->vss_status & VSS_STATUS_FLAG_PLAYING))
1266 return notify_receivers(E_NOT_PLAYING);
1267 if (!must_start_decoder())
1268 return;
1269 ret = open_receiver(stat_task->current_audio_format_num);
1270 if (ret < 0) {
1271 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1272 return;
1273 }
1274 sl = slot + ret;
1275 open_filters(sl);
1276 open_writers(sl);
1277 activate_grab_clients(&sched);
1278 btr_log_tree(sl->receiver_node->btrn, LL_NOTICE);
1279 }
1280
1281 static void status_pre_select(struct sched *s, void *context)
1282 {
1283 struct status_task *st = context;
1284 int i, ret, cafn = stat_task->current_audio_format_num;
1285
1286 if (must_start_decoder())
1287 goto min_delay;
1288 FOR_EACH_SLOT(i)
1289 if (must_close_slot(i))
1290 goto min_delay;
1291 ret = btr_node_status(st->btrn, st->min_iqs, BTR_NT_LEAF);
1292 if (ret > 0)
1293 goto min_delay;
1294 if (st->ct && audiod_status == AUDIOD_OFF)
1295 goto min_delay;
1296 if (!st->ct && audiod_status != AUDIOD_OFF)
1297 sched_request_barrier_or_min_delay(&st->restart_barrier, s);
1298 if (cafn >= 0)
1299 sched_request_barrier(&afi[cafn].restart_barrier, s);
1300 /*
1301 * If para_server is playing we'd like to have a smooth time display
1302 * even if we are running in standby mode. So we request a timeout that
1303 * expires at the next full second.
1304 */
1305 if (stat_task->vss_status & VSS_STATUS_FLAG_PLAYING)
1306 sched_request_timeout_ms(1000 - now->tv_usec / 1000, s);
1307 return;
1308 min_delay:
1309 sched_min_delay(s);
1310 }
1311
1312 /* restart the client task if necessary */
1313 static int status_post_select(struct sched *s, void *context)
1314 {
1315 struct status_task *st = context;
1316 int ret;
1317
1318 ret = task_get_notification(st->task);
1319 if (ret < 0)
1320 return ret;
1321 if (audiod_status == AUDIOD_OFF) {
1322 if (!st->ct)
1323 goto out;
1324 if (task_status(st->ct->task) >= 0) {
1325 task_notify(st->ct->task, E_AUDIOD_OFF);
1326 goto out;
1327 }
1328 close_stat_pipe();
1329 st->clock_diff_count = conf.clock_diff_count_arg;
1330 goto out;
1331 }
1332 if (st->ct) {
1333 char *buf;
1334 size_t sz;
1335
1336 ret = btr_node_status(st->btrn, st->min_iqs, BTR_NT_LEAF);
1337 if (ret < 0) {
1338 close_stat_pipe();
1339 goto out;
1340 }
1341 if (st->ct->status != CL_EXECUTING)
1342 goto out;
1343 if (ret == 0) {
1344 struct timeval diff;
1345 tv_diff(now, &st->last_status_read, &diff);
1346 if (diff.tv_sec > 61)
1347 task_notify(st->ct->task, E_STATUS_TIMEOUT);
1348 goto out;
1349 }
1350 btr_merge(st->btrn, st->min_iqs);
1351 sz = btr_next_buffer(st->btrn, &buf);
1352 ret = for_each_stat_item(buf, sz, update_item);
1353 if (ret < 0) {
1354 task_notify(st->ct->task, -ret);
1355 goto out;
1356 }
1357 if (sz != ret) {
1358 btr_consume(st->btrn, sz - ret);
1359 st->last_status_read = *now;
1360 st->min_iqs = 0;
1361 } else /* current status item crosses buffers */
1362 st->min_iqs = sz + 1;
1363 goto out;
1364 }
1365 btr_drain(st->btrn);
1366 st->current_audio_format_num = -1;
1367 if (tv_diff(now, &st->restart_barrier, NULL) < 0)
1368 goto out;
1369 if (st->clock_diff_count) { /* get status only one time */
1370 char *argv[] = {"audiod", "--", "stat", "-p", "-n=1", NULL};
1371 int argc = 5;
1372 PARA_INFO_LOG("clock diff count: %d\n", st->clock_diff_count);
1373 st->clock_diff_count--;
1374 client_open(argc, argv, &st->ct, NULL, NULL, st->btrn, s);
1375 set_stat_task_restart_barrier(2);
1376
1377 } else {
1378 char *argv[] = {"audiod", "--", "stat", "-p", NULL};
1379 int argc = 4;
1380 client_open(argc, argv, &st->ct, NULL, NULL, st->btrn, s);
1381 set_stat_task_restart_barrier(5);
1382 }
1383 free(stat_item_values[SI_BASENAME]);
1384 stat_item_values[SI_BASENAME] = para_strdup(
1385 "no connection to para_server");
1386 stat_client_write_item(SI_BASENAME);
1387 st->last_status_read = *now;
1388 out:
1389 start_stop_decoders();
1390 return 0;
1391 }
1392
1393 static void init_status_task(struct status_task *st)
1394 {
1395 memset(st, 0, sizeof(struct status_task));
1396 st->sa_time_diff_sign = 1;
1397 st->clock_diff_count = conf.clock_diff_count_arg;
1398 st->current_audio_format_num = -1;
1399 st->btrn = btr_new_node(&(struct btr_node_description)
1400 EMBRACE(.name = "stat"));
1401
1402 stat_task->task = task_register(&(struct task_info) {
1403 .name = "stat",
1404 .pre_select = status_pre_select,
1405 .post_select = status_post_select,
1406 .context = stat_task,
1407 }, &sched);
1408 }
1409
1410 static void set_initial_status(void)
1411 {
1412 audiod_status = AUDIOD_ON;
1413 if (!conf.mode_given)
1414 return;
1415 if (!strcmp(conf.mode_arg, "sb")) {
1416 audiod_status = AUDIOD_STANDBY;
1417 return;
1418 }
1419 if (!strcmp(conf.mode_arg, "off")) {
1420 audiod_status = AUDIOD_OFF;
1421 return;
1422 }
1423 if (strcmp(conf.mode_arg, "on"))
1424 PARA_WARNING_LOG("invalid mode\n");
1425 }
1426
1427 __noreturn static void print_help_and_die(void)
1428 {
1429 struct ggo_help h = DEFINE_GGO_HELP(audiod);
1430 bool d = conf.detailed_help_given;
1431 unsigned flags;
1432
1433 flags = d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS;
1434 ggo_print_help(&h, flags);
1435
1436 flags = d? GPH_MODULE_FLAGS_DETAILED : GPH_MODULE_FLAGS;
1437 print_receiver_helps(flags);
1438 print_filter_helps(flags);
1439 print_writer_helps(flags);
1440 exit(0);
1441 }
1442
1443 /**
1444 * the main function of para_audiod
1445 *
1446 * \param argc usual argument count
1447 * \param argv usual argument vector
1448 *
1449 * \return EXIT_SUCCESS or EXIT_FAILURE
1450 *
1451 * \sa para_audiod(1)
1452 * */
1453 int main(int argc, char *argv[])
1454 {
1455 int ret, i;
1456 struct command_task command_task_struct, *cmd_task = &command_task_struct;
1457 struct audiod_cmdline_parser_params params = {
1458 .override = 0,
1459 .initialize = 1,
1460 .check_required = 0,
1461 .check_ambiguity = 0,
1462 .print_errors = 1
1463 };
1464
1465 valid_fd_012();
1466 audiod_cmdline_parser_ext(argc, argv, &conf, &params);
1467 daemon_set_loglevel(conf.loglevel_arg);
1468 version_handle_flag("audiod", conf.version_given);
1469 /* init receivers/filters/writers early to make help work */
1470 recv_init();
1471 filter_init();
1472 writer_init();
1473 if (conf.help_given || conf.detailed_help_given)
1474 print_help_and_die();
1475 daemon_set_priority(conf.priority_arg);
1476 daemon_drop_privileges_or_die(conf.user_arg, conf.group_arg);
1477 parse_config_or_die();
1478 daemon_init_colors_or_die(conf.color_arg, color_arg_auto, color_arg_no,
1479 conf.logfile_given, conf.log_color_arg, conf.log_color_given);
1480 init_random_seed_or_die();
1481 daemon_set_flag(DF_LOG_TIME);
1482 daemon_set_flag(DF_LOG_HOSTNAME);
1483 daemon_set_flag(DF_LOG_LL);
1484 if (conf.log_timing_given)
1485 daemon_set_flag(DF_LOG_TIMING);
1486 if (conf.logfile_given) {
1487 daemon_set_logfile(conf.logfile_arg);
1488 daemon_open_log_or_die();
1489 }
1490 ret = parse_stream_args();
1491 if (ret < 0) {
1492 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
1493 exit(EXIT_FAILURE);
1494 }
1495 daemon_log_welcome("audiod");
1496 daemon_set_start_time();
1497 set_initial_status();
1498 FOR_EACH_SLOT(i)
1499 clear_slot(i);
1500 setup_signal_handling();
1501
1502 init_status_task(stat_task);
1503 init_command_task(cmd_task);
1504
1505 if (conf.daemon_given)
1506 daemonize(false /* parent exits immediately */);
1507
1508 signal_task->task = task_register(&(struct task_info) {
1509 .name = "signal",
1510 .pre_select = signal_pre_select,
1511 .post_select = signal_post_select,
1512 .context = signal_task,
1513 }, &sched);
1514
1515 sched.default_timeout.tv_sec = 2;
1516 sched.default_timeout.tv_usec = 999 * 1000;
1517 ret = schedule(&sched);
1518 audiod_cleanup();
1519 sched_shutdown(&sched);
1520 signal_shutdown(signal_task);
1521
1522 if (ret < 0)
1523 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
1524 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
1525 }