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