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