2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file audiod_command.c Commands for para_audiod. */
9 #include <netinet/in.h>
10 #include <sys/socket.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
18 #include "audiod.lsg.h"
20 #include "audiod_cmd.lsg.h"
23 #include "buffer_tree.h"
25 #include "grab_client.h"
35 extern struct sched sched;
36 extern char *stat_item_values[NUM_STAT_ITEMS];
38 /** The maximal number of simultaneous connections. */
39 #define MAX_STAT_CLIENTS 50
41 /** Pointer to a command handler function. */
42 typedef int (*audiod_cmd_handler_t)(int, struct lls_parse_result *);
44 /** The lopsub user_data pointer. Only the command handler at the moment. */
45 struct audiod_command_info {
46 audiod_cmd_handler_t handler; /**< Implementation of the command. */
49 /** Define the user_data pointer as expected by lopsub. */
50 #define EXPORT_AUDIOD_CMD_HANDLER(_cmd) \
51 /** Implementation of _cmd. */ \
52 const struct audiod_command_info lsg_audiod_cmd_com_ ## _cmd ## _user_data = { \
53 .handler = com_ ## _cmd \
56 /** Flags used for the stat command of para_audiod. */
57 enum stat_client_flags {
58 /** Enable parser-friendly output. */
59 SCF_PARSER_FRIENDLY = 1,
63 * Describes a status client of para_audiod.
65 * There's one such structure per audiod client that sent the 'stat' command.
67 * A status client is identified by its file descriptor. para_audiod
68 * keeps a list of connected status clients.
71 /** The stat client's file descriptor. */
73 /** Bitmask of those status items the client is interested in. */
75 /** See \ref stat_client flags. */
77 /** Its entry in the list of stat clients. */
78 struct list_head node;
81 static INITIALIZED_LIST_HEAD(client_list);
82 static int num_clients;
84 /** The list of all status items used by para_{server,audiod,gui}. */
85 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
87 static void dump_stat_client_list(void)
89 struct stat_client *sc;
91 list_for_each_entry(sc, &client_list, node)
92 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
95 * Add a status client to the list.
97 * \param fd The file descriptor of the client.
98 * \param mask Bitfield of status items for this client.
99 * \param parser_friendly Enable parser-friendly output mode.
101 * Only those status items having the bit set in \a mask will be
102 * sent to the client.
104 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
105 * the number of connected clients exceeds #MAX_STAT_CLIENTS.
107 static int stat_client_add(int fd, uint64_t mask, int parser_friendly)
109 struct stat_client *new_client;
112 if (num_clients >= MAX_STAT_CLIENTS) {
113 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
115 return -E_TOO_MANY_CLIENTS;
119 return -ERRNO_TO_PARA_ERROR(errno);
120 new_client = para_calloc(sizeof(*new_client));
121 new_client->fd = ret;
122 PARA_INFO_LOG("adding client on fd %d\n", new_client->fd);
123 new_client->item_mask = mask;
125 new_client->flags = SCF_PARSER_FRIENDLY;
126 para_list_add(&new_client->node, &client_list);
127 dump_stat_client_list();
132 static void close_stat_client(struct stat_client *sc)
134 PARA_INFO_LOG("closing client fd %d\n", sc->fd);
142 * Empty the status clients list.
144 * This iterates over the list of connected status clients, closes each client
145 * file descriptor and frees the resources.
147 void close_stat_clients(void)
149 struct stat_client *sc, *tmp;
151 list_for_each_entry_safe(sc, tmp, &client_list, node)
152 close_stat_client(sc);
153 assert(num_clients == 0);
157 * Write a message to all connected status clients.
159 * \param item_num The number of the status item of \a msg.
161 * On write errors, remove the status client from the client list and close its
164 void stat_client_write_item(int item_num)
166 struct stat_client *sc, *tmp;
167 struct para_buffer pb = {.flags = 0};
168 struct para_buffer pfpb = {.flags = PBF_SIZE_PREFIX};
169 const uint64_t one = 1;
170 char *msg = stat_item_values[item_num];
171 struct para_buffer *b;
173 list_for_each_entry_safe(sc, tmp, &client_list, node) {
176 if (!((one << item_num) & sc->item_mask))
178 b = (sc->flags & SCF_PARSER_FRIENDLY)? &pfpb : &pb;
180 WRITE_STATUS_ITEM(b, item_num, "%s\n", msg? msg : "");
181 ret = write(sc->fd, b->buf, b->offset);
182 if (ret == b->offset)
184 /* write error or short write */
185 close_stat_client(sc);
186 dump_stat_client_list();
193 * Check if string is a known status item.
195 * \param item Buffer containing the text to check.
197 * \return If \a item is a valid status item, the number of that status item is
198 * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
200 static int stat_item_valid(const char *item)
203 if (!item || !*item) {
204 PARA_ERROR_LOG("%s\n", "no item");
205 return -E_UNKNOWN_STAT_ITEM;
207 FOR_EACH_STATUS_ITEM(i)
208 if (!strcmp(status_item_list[i], item))
210 PARA_ERROR_LOG("invalid stat item: %s\n", item);
211 return -E_UNKNOWN_STAT_ITEM;
214 static int client_write(int fd, const char *buf)
221 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
224 __malloc static char *audiod_status_string(void)
226 const char *status = (audiod_status == AUDIOD_ON)?
227 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
228 return para_strdup(status);
231 static int dump_commands(int fd)
233 char *buf = para_strdup(""), *tmp = NULL;
236 const struct lls_command *cmd;
238 for (i = 1; (cmd = lls_cmd(i, audiod_cmd_suite)); i++) {
239 tmp = make_message("%s%s\t%s\n", buf, lls_command_name(cmd),
244 ret = client_write(fd, buf);
249 static int com_help(int fd, struct lls_parse_result *lpr)
253 const struct lls_command *cmd;
255 ret = lls(lls_check_arg_count(lpr, 0, 1, &errctx));
258 buf = make_message("%s\n", errctx);
259 client_write(fd, buf);
265 if (lls_num_inputs(lpr) == 0)
266 return dump_commands(fd);
267 ret = lls(lls_lookup_subcmd(lls_input(0, lpr), audiod_cmd_suite,
270 buf = make_message("%s: %s\nAvailable commands:\n", errctx?
271 errctx : lls_input(0, lpr), para_strerror(-ret));
272 if (client_write(fd, buf) >= 0)
278 cmd = lls_cmd(ret, audiod_cmd_suite);
279 buf = lls_long_help(cmd);
281 ret = client_write(fd, buf);
284 return ret < 0? ret : 0;
286 EXPORT_AUDIOD_CMD_HANDLER(help)
288 static int com_tasks(int fd, __a_unused struct lls_parse_result *lpr)
291 char *tl = get_task_list(&sched);
293 if (!tl) /* no tasks registered yet */
295 ret = client_write(fd, tl);
299 EXPORT_AUDIOD_CMD_HANDLER(tasks)
301 static int com_stat(int fd, struct lls_parse_result *lpr)
303 int i, ret, parser_friendly = 0;
305 const uint64_t one = 1;
306 struct para_buffer b = {.flags = 0};
307 const struct lls_opt_result *r;
310 ret = mark_fd_nonblocking(fd);
313 r = lls_opt_result(LSG_AUDIOD_CMD_STAT_OPT_PARSER_FRIENDLY, lpr);
314 if (lls_opt_given(r) > 0) {
316 b.flags = PBF_SIZE_PREFIX;
318 num_inputs = lls_num_inputs(lpr);
320 mask--; /* set all bits */
321 for (i = 0; i < num_inputs; i++) {
322 ret = stat_item_valid(lls_input(i, lpr));
325 mask |= (one << ret);
327 PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask);
328 FOR_EACH_STATUS_ITEM(i) {
329 char *item = stat_item_values[i];
330 if (!((one << i) & mask))
332 WRITE_STATUS_ITEM(&b, i, "%s\n", item? item : "");
334 ret = client_write(fd, b.buf);
336 ret = stat_client_add(fd, mask, parser_friendly);
338 return ret < 0? ret : 0;
340 EXPORT_AUDIOD_CMD_HANDLER(stat)
342 static int com_grab(int fd, struct lls_parse_result *lpr)
344 int ret = grab_client_new(fd, lpr, &sched);
345 return ret < 0? ret : 0;
347 EXPORT_AUDIOD_CMD_HANDLER(grab)
349 static int com_term(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
351 return -E_AUDIOD_TERM;
353 EXPORT_AUDIOD_CMD_HANDLER(term)
355 static int com_on(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
357 audiod_status = AUDIOD_ON;
360 EXPORT_AUDIOD_CMD_HANDLER(on)
362 static int com_off(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
364 audiod_status = AUDIOD_OFF;
367 EXPORT_AUDIOD_CMD_HANDLER(off)
369 static int com_sb(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
371 audiod_status = AUDIOD_STANDBY;
374 EXPORT_AUDIOD_CMD_HANDLER(sb)
376 static int com_cycle(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
378 switch (audiod_status) {
379 case AUDIOD_ON: audiod_status = AUDIOD_STANDBY; break;
380 case AUDIOD_OFF: audiod_status = AUDIOD_ON; break;
381 case AUDIOD_STANDBY: audiod_status = AUDIOD_OFF; break;
385 EXPORT_AUDIOD_CMD_HANDLER(cycle)
387 static int com_version(int fd, struct lls_parse_result *lpr)
391 const struct lls_opt_result *r_v;
393 r_v = lls_opt_result(LSG_AUDIOD_CMD_VERSION_OPT_VERBOSE, lpr);
394 if (lls_opt_given(r_v))
395 msg = make_message("%s", version_text("audiod"));
397 msg = make_message("%s\n", version_single_line("audiod"));
398 ret = client_write(fd, msg);
400 return ret < 0? ret : 0;
402 EXPORT_AUDIOD_CMD_HANDLER(version)
405 * Handle arriving connections on the local socket.
407 * \param accept_fd The fd to accept connections on.
408 * \param rfds If \a accept_fd is not set in \a rfds, do nothing.
410 * This is called in each iteration of the select loop. If there is an incoming
411 * connection on \a accept_fd, this function reads the command sent by the peer,
412 * checks the connecting user's permissions by using unix socket credentials
413 * (if supported by the OS) and calls the corresponding command handler if
414 * permissions are OK.
416 * \return Positive on success, negative on errors, zero if there was no
417 * connection to accept.
419 * \sa para_accept(), recv_cred_buffer()
421 int handle_connect(int accept_fd, fd_set *rfds)
423 int argc, ret, clifd;
424 char buf[MAXLINE], **argv = NULL;
425 struct sockaddr_un unix_addr;
427 const struct lls_command *cmd;
428 struct lls_parse_result *lpr;
430 const struct audiod_command_info *aci;
432 ret = para_accept(accept_fd, rfds, &unix_addr, sizeof(struct sockaddr_un), &clifd);
435 ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
439 PARA_INFO_LOG("connection from UID %d, buf: %s\n", ret, buf);
441 if (!uid_is_whitelisted(uid))
443 ret = create_argv(buf, "\n", &argv);
447 ret = lls(lls_lookup_subcmd(argv[0], audiod_cmd_suite, &errctx));
450 cmd = lls_cmd(ret, audiod_cmd_suite);
451 ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
454 aci = lls_user_data(cmd);
455 ret = aci->handler(clifd, lpr);
456 lls_free_parse_result(lpr, cmd);
459 if (ret < 0 && ret != -E_CLIENT_WRITE) {
462 tmp = make_message("%s\n", errctx);
464 client_write(clifd, tmp);
467 tmp = make_message("%s\n", para_strerror(-ret));
468 client_write(clifd, tmp);
476 * Send the current audiod status to all connected stat clients.
478 * \param force Whether to write unchanged items.
480 void audiod_status_dump(bool force)
484 old = stat_item_values[SI_PLAY_TIME];
485 new = get_time_string();
487 if (force || !old || strcmp(old, new)) {
489 stat_item_values[SI_PLAY_TIME] = new;
490 stat_client_write_item(SI_PLAY_TIME);
495 new = daemon_get_uptime_str(now);
496 old = stat_item_values[SI_AUDIOD_UPTIME];
497 if (force || !old || strcmp(old, new)) {
499 stat_item_values[SI_AUDIOD_UPTIME] = new;
500 stat_client_write_item(SI_AUDIOD_UPTIME);
504 old = stat_item_values[SI_AUDIOD_STATUS];
505 new = audiod_status_string();
506 if (force || !old || strcmp(old, new)) {
508 stat_item_values[SI_AUDIOD_STATUS] = new;
509 stat_client_write_item(SI_AUDIOD_STATUS);
513 old = stat_item_values[SI_DECODER_FLAGS];
514 new = audiod_get_decoder_flags();
515 if (force || !old || strcmp(old, new)) {
517 stat_item_values[SI_DECODER_FLAGS] = new;
518 stat_client_write_item(SI_DECODER_FLAGS);
524 * Flush and send all status items.
526 * Send to each connected client the full status item list
529 void clear_and_dump_items(void)
533 FOR_EACH_STATUS_ITEM(i) {
534 free(stat_item_values[i]);
535 stat_item_values[i] = NULL;
536 stat_client_write_item(i);