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