Merge branch 't/sound_device_lock'
[paraslash.git] / audiod_command.c
1 /*
2 * Copyright (C) 2005-2014 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file audiod_command.c Commands for para_audiod. */
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
17 #include "para.h"
18 #include "audiod.cmdline.h"
19 #include "list.h"
20 #include "sched.h"
21 #include "ggo.h"
22 #include "buffer_tree.h"
23 #include "filter.h"
24 #include "grab_client.h"
25 #include "error.h"
26 #include "audiod.h"
27 #include "net.h"
28 #include "daemon.h"
29 #include "string.h"
30 #include "write.h"
31 #include "fd.h"
32 #include "version.h"
33 #include "audiod_command_list.h"
34
35 extern struct sched sched;
36 extern char *stat_item_values[NUM_STAT_ITEMS];
37
38
39 static struct audiod_command audiod_cmds[] = {DEFINE_AUDIOD_CMD_ARRAY};
40
41 /** Iterate over the array of all audiod commands. */
42 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
43
44 /** The maximal number of simultaneous connections. */
45 #define MAX_STAT_CLIENTS 50
46
47 /** Flags used for the stat command of para_audiod. */
48 enum stat_client_flags {
49 /** Enable parser-friendly output. */
50 SCF_PARSER_FRIENDLY = 1,
51 };
52
53 /**
54 * Describes a status client of para_audiod.
55 *
56 * There's one such structure per audiod client that sent the 'stat' command.
57 *
58 * A status client is identified by its file descriptor. para_audiod
59 * keeps a list of connected status clients.
60 */
61 struct stat_client {
62 /** The stat client's file descriptor. */
63 int fd;
64 /** Bitmask of those status items the client is interested in. */
65 uint64_t item_mask;
66 /** See \ref stat_client flags. s*/
67 unsigned flags;
68 /** Its entry in the list of stat clients. */
69 struct list_head node;
70 };
71
72 static INITIALIZED_LIST_HEAD(client_list);
73 static int num_clients;
74
75 /** The list of all status items used by para_{server,audiod,gui}. */
76 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
77
78 static void dump_stat_client_list(void)
79 {
80 struct stat_client *sc;
81
82 list_for_each_entry(sc, &client_list, node)
83 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
84 }
85 /**
86 * Add a status client to the list.
87 *
88 * \param fd The file descriptor of the client.
89 * \param mask Bitfield of status items for this client.
90 * \param parser_friendly Enable parser-friendly output mode.
91 *
92 * Only those status items having the bit set in \a mask will be
93 * sent to the client.
94 *
95 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
96 * the number of connected clients exceeds #MAX_STAT_CLIENTS.
97 */
98 static int stat_client_add(int fd, uint64_t mask, int parser_friendly)
99 {
100 struct stat_client *new_client;
101
102 if (num_clients >= MAX_STAT_CLIENTS) {
103 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
104 MAX_STAT_CLIENTS);
105 return -E_TOO_MANY_CLIENTS;
106 }
107 PARA_INFO_LOG("adding client on fd %d\n", fd);
108 new_client = para_calloc(sizeof(struct stat_client));
109 new_client->fd = fd;
110 new_client->item_mask = mask;
111 if (parser_friendly)
112 new_client->flags = SCF_PARSER_FRIENDLY;
113 para_list_add(&new_client->node, &client_list);
114 dump_stat_client_list();
115 num_clients++;
116 return 1;
117 }
118
119 static void close_stat_client(struct stat_client *sc)
120 {
121 PARA_INFO_LOG("closing client fd %d\n", sc->fd);
122 close(sc->fd);
123 list_del(&sc->node);
124 free(sc);
125 num_clients--;
126 }
127
128 /**
129 * Empty the status clients list.
130 *
131 * This iterates over the list of connected status clients, closes each client
132 * file descriptor and frees the resources.
133 */
134 void close_stat_clients(void)
135 {
136 struct stat_client *sc, *tmp;
137
138 list_for_each_entry_safe(sc, tmp, &client_list, node)
139 close_stat_client(sc);
140 assert(num_clients == 0);
141 }
142
143 /**
144 * Write a message to all connected status clients.
145 *
146 * \param item_num The number of the status item of \a msg.
147 *
148 * On write errors, remove the status client from the client list and close its
149 * file descriptor.
150 */
151 void stat_client_write_item(int item_num)
152 {
153 struct stat_client *sc, *tmp;
154 struct para_buffer pb = {.flags = 0};
155 struct para_buffer pfpb = {.flags = PBF_SIZE_PREFIX};
156 const uint64_t one = 1;
157 char *msg = stat_item_values[item_num];
158 struct para_buffer *b;
159
160 list_for_each_entry_safe(sc, tmp, &client_list, node) {
161 int ret;
162
163 if (!((one << item_num) & sc->item_mask))
164 continue;
165 b = (sc->flags & SCF_PARSER_FRIENDLY)? &pfpb : &pb;
166 if (!b->buf)
167 (void)WRITE_STATUS_ITEM(b, item_num, "%s\n",
168 msg? msg : "");
169 ret = write(sc->fd, b->buf, b->offset);
170 if (ret == b->offset)
171 continue;
172 /* write error or short write */
173 close_stat_client(sc);
174 dump_stat_client_list();
175 }
176 free(pb.buf);
177 free(pfpb.buf);
178 }
179
180 /**
181 * Check if string is a known status item.
182 *
183 * \param item Buffer containing the text to check.
184 *
185 * \return If \a item is a valid status item, the number of that status item is
186 * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
187 */
188 static int stat_item_valid(const char *item)
189 {
190 int i;
191 if (!item || !*item) {
192 PARA_ERROR_LOG("%s\n", "no item");
193 return -E_UNKNOWN_STAT_ITEM;
194 }
195 FOR_EACH_STATUS_ITEM(i)
196 if (!strcmp(status_item_list[i], item))
197 return i;
198 PARA_ERROR_LOG("invalid stat item: %s\n", item);
199 return -E_UNKNOWN_STAT_ITEM;
200 }
201
202 static int client_write(int fd, const char *buf)
203 {
204 size_t len;
205
206 if (!buf)
207 return 0;
208 len = strlen(buf);
209 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
210 }
211
212 __malloc static char *audiod_status_string(void)
213 {
214 const char *status = (audiod_status == AUDIOD_ON)?
215 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
216 return para_strdup(status);
217 }
218
219 static int get_play_time_slot_num(void)
220 {
221 int i, oldest_slot = -1;
222 struct timeval oldest_wstime = {0, 0};
223
224 FOR_EACH_SLOT(i) {
225 struct slot_info *s = &slot[i];
226 struct timeval wstime;
227 if (!s->wns || !s->wns[0].btrn)
228 continue;
229 btr_get_node_start(s->wns[0].btrn, &wstime);
230 if (oldest_slot >= 0 && tv_diff(&wstime, &oldest_wstime, NULL) > 0)
231 continue;
232 oldest_wstime = wstime;
233 oldest_slot = i;
234 }
235 //PARA_CRIT_LOG("oldest slot: %d\n", oldest_slot);
236 return oldest_slot;
237 }
238
239 __malloc static char *decoder_flags(void)
240 {
241 int i;
242 char flags[MAX_STREAM_SLOTS + 1];
243
244 FOR_EACH_SLOT(i) {
245 struct slot_info *s = &slot[i];
246 char flag = '0';
247 if (s->receiver_node)
248 flag += 1;
249 if (s->fns)
250 flag += 2;
251 if (s->wns)
252 flag += 4;
253 flags[i] = flag;
254 }
255 flags[MAX_STREAM_SLOTS] = '\0';
256 return para_strdup(flags);
257 }
258
259 static int dump_commands(int fd)
260 {
261 char *buf = para_strdup(""), *tmp = NULL;
262 int i;
263 ssize_t ret;
264
265 FOR_EACH_COMMAND(i) {
266 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
267 audiod_cmds[i].description);
268 free(buf);
269 buf = tmp;
270 }
271 ret = client_write(fd, buf);
272 free(buf);
273 return ret;
274 }
275
276 /*
277 * command handlers don't close their fd on errors (ret < 0) so that
278 * its caller can send an error message. Otherwise (ret >= 0) it's up
279 * to each individual command to close the fd if necessary.
280 */
281
282 static int com_help(int fd, int argc, char **argv)
283 {
284 int i, ret;
285 char *buf;
286 const char *dflt = "No such command. Available commands:\n";
287
288 if (argc < 2) {
289 ret = dump_commands(fd);
290 goto out;
291 }
292 FOR_EACH_COMMAND(i) {
293 if (strcmp(audiod_cmds[i].name, argv[1]))
294 continue;
295 buf = make_message(
296 "NAME\n\t%s -- %s\n"
297 "SYNOPSIS\n\tpara_audioc %s\n"
298 "DESCRIPTION\n%s\n",
299 argv[1],
300 audiod_cmds[i].description,
301 audiod_cmds[i].usage,
302 audiod_cmds[i].help
303 );
304 ret = client_write(fd, buf);
305 free(buf);
306 goto out;
307 }
308 ret = client_write(fd, dflt);
309 if (ret > 0)
310 ret = dump_commands(fd);
311 out:
312 if (ret >= 0)
313 close(fd);
314 return ret;
315 }
316
317 static int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
318 {
319 char *tl = get_task_list(&sched);
320 int ret = 1;
321 if (tl)
322 ret = client_write(fd, tl);
323 free(tl);
324 if (ret > 0)
325 close(fd);
326 return ret;
327 }
328
329 static int com_stat(int fd, int argc, char **argv)
330 {
331 int i, ret, parser_friendly = 0;
332 uint64_t mask = 0;
333 const uint64_t one = 1;
334 struct para_buffer b = {.flags = 0};
335
336 ret = mark_fd_nonblocking(fd);
337 if (ret < 0)
338 return ret;
339 for (i = 1; i < argc; i++) {
340 const char *arg = argv[i];
341 if (arg[0] != '-')
342 break;
343 if (!strcmp(arg, "--")) {
344 i++;
345 break;
346 }
347 if (!strncmp(arg, "-p", 2)) {
348 parser_friendly = 1;
349 b.flags = PBF_SIZE_PREFIX;
350 }
351 }
352 if (i >= argc)
353 mask--; /* set all bits */
354 for (; i < argc; i++) {
355 ret = stat_item_valid(argv[i]);
356 if (ret < 0)
357 return ret;
358 mask |= (one << ret);
359 }
360 PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask);
361 FOR_EACH_STATUS_ITEM(i) {
362 char *item = stat_item_values[i];
363 if (!((one << i) & mask))
364 continue;
365 (void)WRITE_STATUS_ITEM(&b, i, "%s\n", item? item : "");
366 }
367 ret = client_write(fd, b.buf);
368 if (ret >= 0)
369 ret = stat_client_add(fd, mask, parser_friendly);
370 free(b.buf);
371 return ret;
372 }
373
374 static int com_grab(int fd, int argc, char **argv)
375 {
376 return grab_client_new(fd, argc, argv, &sched);
377 }
378
379 static int com_term(int fd, __a_unused int argc, __a_unused char **argv)
380 {
381 close(fd);
382 return -E_AUDIOD_TERM;
383 }
384
385 static int com_on(int fd, __a_unused int argc, __a_unused char **argv)
386 {
387 audiod_status = AUDIOD_ON;
388 close(fd);
389 return 1;
390 }
391
392 static int com_off(int fd, __a_unused int argc, __a_unused char **argv)
393 {
394 audiod_status = AUDIOD_OFF;
395 close(fd);
396 return 1;
397 }
398
399 static int com_sb(int fd, __a_unused int argc, __a_unused char **argv)
400 {
401 audiod_status = AUDIOD_STANDBY;
402 close(fd);
403 return 1;
404 }
405
406 static int com_cycle(int fd, int argc, char **argv)
407 {
408 switch (audiod_status) {
409 case AUDIOD_ON:
410 return com_sb(fd, argc, argv);
411 break;
412 case AUDIOD_OFF:
413 return com_on(fd, argc, argv);
414 break;
415 case AUDIOD_STANDBY:
416 return com_off(fd, argc, argv);
417 break;
418 }
419 close(fd);
420 return 1;
421 }
422
423 static int com_version(int fd, int argc, char **argv)
424 {
425 int ret;
426 char *msg;
427
428 if (argc > 1 && strcmp(argv[1], "-v") == 0)
429 msg = make_message("%s", version_text("audiod"));
430 else
431 msg = make_message("%s\n", version_single_line("audiod"));
432 ret = client_write(fd, msg);
433 free(msg);
434 if (ret >= 0)
435 close(fd);
436 return ret;
437 }
438
439 static int check_perms(uid_t uid)
440 {
441 int i;
442
443 if (!conf.user_allow_given)
444 return 1;
445 for (i = 0; i < conf.user_allow_given; i++)
446 if (uid == conf.user_allow_arg[i])
447 return 1;
448 return -E_UCRED_PERM;
449 }
450
451 /**
452 * Handle arriving connections on the local socket.
453 *
454 * \param accept_fd The fd to accept connections on.
455 * \param rfds If \a accept_fd is not set in \a rfds, do nothing.
456 *
457 * This is called in each iteration of the select loop. If there is an incoming
458 * connection on \a accept_fd, this function reads the command sent by the peer,
459 * checks the connecting user's permissions by using unix socket credentials
460 * (if supported by the OS) and calls the corresponding command handler if
461 * permissions are OK.
462 *
463 * \return Positive on success, negative on errors, zero if there was no
464 * connection to accept.
465 *
466 * \sa para_accept(), recv_cred_buffer()
467 * */
468 int handle_connect(int accept_fd, fd_set *rfds)
469 {
470 int i, argc, ret, clifd;
471 char buf[MAXLINE], **argv = NULL;
472 struct sockaddr_un unix_addr;
473 uid_t uid;
474
475 ret = para_accept(accept_fd, rfds, &unix_addr, sizeof(struct sockaddr_un), &clifd);
476 if (ret <= 0)
477 return ret;
478 ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
479 if (ret < 0)
480 goto out;
481 uid = ret;
482 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret, buf);
483 ret = check_perms(uid);
484 if (ret < 0)
485 goto out;
486 ret = create_argv(buf, "\n", &argv);
487 if (ret <= 0)
488 goto out;
489 argc = ret;
490 //PARA_INFO_LOG("argv[0]: %s, argc = %d\n", argv[0], argc);
491 FOR_EACH_COMMAND(i) {
492 if (strcmp(audiod_cmds[i].name, argv[0]))
493 continue;
494 ret = audiod_cmds[i].handler(clifd, argc, argv);
495 goto out;
496 }
497 ret = -E_INVALID_AUDIOD_CMD;
498 out:
499 free_argv(argv);
500 if (clifd > 0 && ret < 0 && ret != -E_CLIENT_WRITE) {
501 char *tmp = make_message("%s\n", para_strerror(-ret));
502 client_write(clifd, tmp);
503 free(tmp);
504 close(clifd);
505 }
506 return ret;
507 }
508
509 /**
510 * Send the current audiod status to all connected stat clients.
511 *
512 * \param force Whether to write unchanged items.
513 */
514 void audiod_status_dump(bool force)
515 {
516 int slot_num = get_play_time_slot_num();
517 char *old, *new;
518
519 old = stat_item_values[SI_PLAY_TIME];
520 new = get_time_string(slot_num);
521 if (new) {
522 if (force || !old || strcmp(old, new)) {
523 free(old);
524 stat_item_values[SI_PLAY_TIME] = new;
525 stat_client_write_item(SI_PLAY_TIME);
526 } else
527 free(new);
528 }
529
530 new = daemon_get_uptime_str(now);
531 old = stat_item_values[SI_AUDIOD_UPTIME];
532 if (force || !old || strcmp(old, new)) {
533 free(old);
534 stat_item_values[SI_AUDIOD_UPTIME] = new;
535 stat_client_write_item(SI_AUDIOD_UPTIME);
536 } else
537 free(new);
538
539 old = stat_item_values[SI_AUDIOD_STATUS];
540 new = audiod_status_string();
541 if (force || !old || strcmp(old, new)) {
542 free(old);
543 stat_item_values[SI_AUDIOD_STATUS] = new;
544 stat_client_write_item(SI_AUDIOD_STATUS);
545 } else
546 free(new);
547
548 old = stat_item_values[SI_DECODER_FLAGS];
549 new = decoder_flags();
550 if (force || !old || strcmp(old, new)) {
551 free(old);
552 stat_item_values[SI_DECODER_FLAGS] = new;
553 stat_client_write_item(SI_DECODER_FLAGS);
554 } else
555 free(new);
556 }
557
558 /**
559 * Flush and send all status items.
560 *
561 * Send to each connected client the full status item list
562 * with empty values.
563 */
564 void clear_and_dump_items(void)
565 {
566 int i;
567
568 FOR_EACH_STATUS_ITEM(i) {
569 free(stat_item_values[i]);
570 stat_item_values[i] = NULL;
571 stat_client_write_item(i);
572 }
573 }