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.cmdline.h"
19 #include "audiod.command_list.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 typedef int audiod_command_handler_t(int, int, char **);
39 static audiod_command_handler_t AUDIOD_COMMAND_HANDLERS;
41 /* Defines one command of para_audiod. */
42 struct audiod_command {
44 /* Pointer to the function that handles the command. */
46 * Command handlers must never never close their file descriptor. A
47 * positive return value tells audiod that the status items have
48 * changed. In this case audiod will send an updated version of all
49 * status items to to each connected stat client.
51 audiod_command_handler_t *handler;
52 /* One-line description. */
53 const char *description;
54 /* Summary of the command line options. */
56 /* The long help text. */
60 static struct audiod_command audiod_cmds[] = {DEFINE_AUDIOD_CMD_ARRAY};
62 /** Iterate over the array of all audiod commands. */
63 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
65 /** The maximal number of simultaneous connections. */
66 #define MAX_STAT_CLIENTS 50
68 /** Flags used for the stat command of para_audiod. */
69 enum stat_client_flags {
70 /** Enable parser-friendly output. */
71 SCF_PARSER_FRIENDLY = 1,
75 * Describes a status client of para_audiod.
77 * There's one such structure per audiod client that sent the 'stat' command.
79 * A status client is identified by its file descriptor. para_audiod
80 * keeps a list of connected status clients.
83 /** The stat client's file descriptor. */
85 /** Bitmask of those status items the client is interested in. */
87 /** See \ref stat_client flags. */
89 /** Its entry in the list of stat clients. */
90 struct list_head node;
93 static INITIALIZED_LIST_HEAD(client_list);
94 static int num_clients;
96 /** The list of all status items used by para_{server,audiod,gui}. */
97 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
99 static void dump_stat_client_list(void)
101 struct stat_client *sc;
103 list_for_each_entry(sc, &client_list, node)
104 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
107 * Add a status client to the list.
109 * \param fd The file descriptor of the client.
110 * \param mask Bitfield of status items for this client.
111 * \param parser_friendly Enable parser-friendly output mode.
113 * Only those status items having the bit set in \a mask will be
114 * sent to the client.
116 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
117 * the number of connected clients exceeds #MAX_STAT_CLIENTS.
119 static int stat_client_add(int fd, uint64_t mask, int parser_friendly)
121 struct stat_client *new_client;
124 if (num_clients >= MAX_STAT_CLIENTS) {
125 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
127 return -E_TOO_MANY_CLIENTS;
131 return -ERRNO_TO_PARA_ERROR(errno);
132 new_client = para_calloc(sizeof(*new_client));
133 new_client->fd = ret;
134 PARA_INFO_LOG("adding client on fd %d\n", new_client->fd);
135 new_client->item_mask = mask;
137 new_client->flags = SCF_PARSER_FRIENDLY;
138 para_list_add(&new_client->node, &client_list);
139 dump_stat_client_list();
144 static void close_stat_client(struct stat_client *sc)
146 PARA_INFO_LOG("closing client fd %d\n", sc->fd);
154 * Empty the status clients list.
156 * This iterates over the list of connected status clients, closes each client
157 * file descriptor and frees the resources.
159 void close_stat_clients(void)
161 struct stat_client *sc, *tmp;
163 list_for_each_entry_safe(sc, tmp, &client_list, node)
164 close_stat_client(sc);
165 assert(num_clients == 0);
169 * Write a message to all connected status clients.
171 * \param item_num The number of the status item of \a msg.
173 * On write errors, remove the status client from the client list and close its
176 void stat_client_write_item(int item_num)
178 struct stat_client *sc, *tmp;
179 struct para_buffer pb = {.flags = 0};
180 struct para_buffer pfpb = {.flags = PBF_SIZE_PREFIX};
181 const uint64_t one = 1;
182 char *msg = stat_item_values[item_num];
183 struct para_buffer *b;
185 list_for_each_entry_safe(sc, tmp, &client_list, node) {
188 if (!((one << item_num) & sc->item_mask))
190 b = (sc->flags & SCF_PARSER_FRIENDLY)? &pfpb : &pb;
192 WRITE_STATUS_ITEM(b, item_num, "%s\n", msg? msg : "");
193 ret = write(sc->fd, b->buf, b->offset);
194 if (ret == b->offset)
196 /* write error or short write */
197 close_stat_client(sc);
198 dump_stat_client_list();
205 * Check if string is a known status item.
207 * \param item Buffer containing the text to check.
209 * \return If \a item is a valid status item, the number of that status item is
210 * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
212 static int stat_item_valid(const char *item)
215 if (!item || !*item) {
216 PARA_ERROR_LOG("%s\n", "no item");
217 return -E_UNKNOWN_STAT_ITEM;
219 FOR_EACH_STATUS_ITEM(i)
220 if (!strcmp(status_item_list[i], item))
222 PARA_ERROR_LOG("invalid stat item: %s\n", item);
223 return -E_UNKNOWN_STAT_ITEM;
226 static int client_write(int fd, const char *buf)
233 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
236 __malloc static char *audiod_status_string(void)
238 const char *status = (audiod_status == AUDIOD_ON)?
239 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
240 return para_strdup(status);
243 static int dump_commands(int fd)
245 char *buf = para_strdup(""), *tmp = NULL;
249 FOR_EACH_COMMAND(i) {
250 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
251 audiod_cmds[i].description);
255 ret = client_write(fd, buf);
260 static int com_help(int fd, int argc, char **argv)
266 ret = dump_commands(fd);
269 FOR_EACH_COMMAND(i) {
270 if (strcmp(audiod_cmds[i].name, argv[1]))
274 "SYNOPSIS\n\tpara_audioc %s\n"
277 audiod_cmds[i].description,
278 audiod_cmds[i].usage,
281 ret = client_write(fd, buf);
285 ret = client_write(fd, "No such command. Available commands:\n");
287 ret = dump_commands(fd);
289 return ret < 0? ret : 0;
292 static int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
294 char *tl = get_task_list(&sched);
298 ret = client_write(fd, tl);
300 return ret < 0? ret : 0;
303 static int com_stat(int fd, int argc, char **argv)
305 int i, ret, parser_friendly = 0;
307 const uint64_t one = 1;
308 struct para_buffer b = {.flags = 0};
310 ret = mark_fd_nonblocking(fd);
313 for (i = 1; i < argc; i++) {
314 const char *arg = argv[i];
317 if (!strcmp(arg, "--")) {
321 if (!strncmp(arg, "-p", 2)) {
323 b.flags = PBF_SIZE_PREFIX;
327 mask--; /* set all bits */
328 for (; i < argc; i++) {
329 ret = stat_item_valid(argv[i]);
332 mask |= (one << ret);
334 PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask);
335 FOR_EACH_STATUS_ITEM(i) {
336 char *item = stat_item_values[i];
337 if (!((one << i) & mask))
339 WRITE_STATUS_ITEM(&b, i, "%s\n", item? item : "");
341 ret = client_write(fd, b.buf);
343 ret = stat_client_add(fd, mask, parser_friendly);
345 return ret < 0? ret : 0;
348 static int com_grab(int fd, int argc, char **argv)
350 int ret = grab_client_new(fd, argc, argv, &sched);
351 return ret < 0? ret : 0;
354 static int com_term(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
356 return -E_AUDIOD_TERM;
359 static int com_on(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
361 audiod_status = AUDIOD_ON;
365 static int com_off(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
367 audiod_status = AUDIOD_OFF;
371 static int com_sb(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
373 audiod_status = AUDIOD_STANDBY;
377 static int com_cycle(__a_unused int fd, int argc, char **argv)
379 switch (audiod_status) {
381 return com_sb(fd, argc, argv);
384 return com_on(fd, argc, argv);
387 return com_off(fd, argc, argv);
393 static int com_version(int fd, int argc, char **argv)
398 if (argc > 1 && strcmp(argv[1], "-v") == 0)
399 msg = make_message("%s", version_text("audiod"));
401 msg = make_message("%s\n", version_single_line("audiod"));
402 ret = client_write(fd, msg);
404 return ret < 0? ret : 0;
408 * Handle arriving connections on the local socket.
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.
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.
419 * \return Positive on success, negative on errors, zero if there was no
420 * connection to accept.
422 * \sa para_accept(), recv_cred_buffer()
424 int handle_connect(int accept_fd, fd_set *rfds)
426 int i, argc, ret, clifd;
427 char buf[MAXLINE], **argv = NULL;
428 struct sockaddr_un unix_addr;
431 ret = para_accept(accept_fd, rfds, &unix_addr, sizeof(struct sockaddr_un), &clifd);
434 ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
438 PARA_INFO_LOG("connection from UID %d, buf: %s\n", ret, buf);
440 if (!uid_is_whitelisted(uid))
442 ret = create_argv(buf, "\n", &argv);
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]))
450 ret = audiod_cmds[i].handler(clifd, argc, argv);
453 ret = -E_INVALID_AUDIOD_CMD;
456 if (ret < 0 && ret != -E_CLIENT_WRITE) {
457 char *tmp = make_message("%s\n", para_strerror(-ret));
458 client_write(clifd, tmp);
466 * Send the current audiod status to all connected stat clients.
468 * \param force Whether to write unchanged items.
470 void audiod_status_dump(bool force)
474 old = stat_item_values[SI_PLAY_TIME];
475 new = get_time_string();
477 if (force || !old || strcmp(old, new)) {
479 stat_item_values[SI_PLAY_TIME] = new;
480 stat_client_write_item(SI_PLAY_TIME);
485 new = daemon_get_uptime_str(now);
486 old = stat_item_values[SI_AUDIOD_UPTIME];
487 if (force || !old || strcmp(old, new)) {
489 stat_item_values[SI_AUDIOD_UPTIME] = new;
490 stat_client_write_item(SI_AUDIOD_UPTIME);
494 old = stat_item_values[SI_AUDIOD_STATUS];
495 new = audiod_status_string();
496 if (force || !old || strcmp(old, new)) {
498 stat_item_values[SI_AUDIOD_STATUS] = new;
499 stat_client_write_item(SI_AUDIOD_STATUS);
503 old = stat_item_values[SI_DECODER_FLAGS];
504 new = audiod_get_decoder_flags();
505 if (force || !old || strcmp(old, new)) {
507 stat_item_values[SI_DECODER_FLAGS] = new;
508 stat_client_write_item(SI_DECODER_FLAGS);
514 * Flush and send all status items.
516 * Send to each connected client the full status item list
519 void clear_and_dump_items(void)
523 FOR_EACH_STATUS_ITEM(i) {
524 free(stat_item_values[i]);
525 stat_item_values[i] = NULL;
526 stat_client_write_item(i);