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