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