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"
22 #include "buffer_tree.h"
24 #include "grab_client.h"
33 #include "audiod.command_list.h"
35 extern struct sched sched;
36 extern char *stat_item_values[NUM_STAT_ITEMS];
39 static struct audiod_command audiod_cmds[] = {DEFINE_AUDIOD_CMD_ARRAY};
41 /** Iterate over the array of all audiod commands. */
42 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
44 /** The maximal number of simultaneous connections. */
45 #define MAX_STAT_CLIENTS 50
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,
54 * Describes a status client of para_audiod.
56 * There's one such structure per audiod client that sent the 'stat' command.
58 * A status client is identified by its file descriptor. para_audiod
59 * keeps a list of connected status clients.
62 /** The stat client's file descriptor. */
64 /** Bitmask of those status items the client is interested in. */
66 /** See \ref stat_client flags. s*/
68 /** Its entry in the list of stat clients. */
69 struct list_head node;
72 static INITIALIZED_LIST_HEAD(client_list);
73 static int num_clients;
75 /** The list of all status items used by para_{server,audiod,gui}. */
76 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
78 static void dump_stat_client_list(void)
80 struct stat_client *sc;
82 list_for_each_entry(sc, &client_list, node)
83 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
86 * Add a status client to the list.
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.
92 * Only those status items having the bit set in \a mask will be
95 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
96 * the number of connected clients exceeds #MAX_STAT_CLIENTS.
98 static int stat_client_add(int fd, uint64_t mask, int parser_friendly)
100 struct stat_client *new_client;
102 if (num_clients >= MAX_STAT_CLIENTS) {
103 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
105 return -E_TOO_MANY_CLIENTS;
107 PARA_INFO_LOG("adding client on fd %d\n", fd);
108 new_client = para_calloc(sizeof(struct stat_client));
110 new_client->item_mask = mask;
112 new_client->flags = SCF_PARSER_FRIENDLY;
113 para_list_add(&new_client->node, &client_list);
114 dump_stat_client_list();
119 static void close_stat_client(struct stat_client *sc)
121 PARA_INFO_LOG("closing client fd %d\n", sc->fd);
129 * Empty the status clients list.
131 * This iterates over the list of connected status clients, closes each client
132 * file descriptor and frees the resources.
134 void close_stat_clients(void)
136 struct stat_client *sc, *tmp;
138 list_for_each_entry_safe(sc, tmp, &client_list, node)
139 close_stat_client(sc);
140 assert(num_clients == 0);
144 * Write a message to all connected status clients.
146 * \param item_num The number of the status item of \a msg.
148 * On write errors, remove the status client from the client list and close its
151 void stat_client_write_item(int item_num)
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;
160 list_for_each_entry_safe(sc, tmp, &client_list, node) {
163 if (!((one << item_num) & sc->item_mask))
165 b = (sc->flags & SCF_PARSER_FRIENDLY)? &pfpb : &pb;
167 (void)WRITE_STATUS_ITEM(b, item_num, "%s\n",
169 ret = write(sc->fd, b->buf, b->offset);
170 if (ret == b->offset)
172 /* write error or short write */
173 close_stat_client(sc);
174 dump_stat_client_list();
181 * Check if string is a known status item.
183 * \param item Buffer containing the text to check.
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.
188 static int stat_item_valid(const char *item)
191 if (!item || !*item) {
192 PARA_ERROR_LOG("%s\n", "no item");
193 return -E_UNKNOWN_STAT_ITEM;
195 FOR_EACH_STATUS_ITEM(i)
196 if (!strcmp(status_item_list[i], item))
198 PARA_ERROR_LOG("invalid stat item: %s\n", item);
199 return -E_UNKNOWN_STAT_ITEM;
202 static int client_write(int fd, const char *buf)
209 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
212 __malloc static char *audiod_status_string(void)
214 const char *status = (audiod_status == AUDIOD_ON)?
215 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
216 return para_strdup(status);
219 __malloc static char *decoder_flags(void)
222 char flags[MAX_STREAM_SLOTS + 1];
225 struct slot_info *s = &slot[i];
227 if (s->receiver_node)
235 flags[MAX_STREAM_SLOTS] = '\0';
236 return para_strdup(flags);
239 static int dump_commands(int fd)
241 char *buf = para_strdup(""), *tmp = NULL;
245 FOR_EACH_COMMAND(i) {
246 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
247 audiod_cmds[i].description);
251 ret = client_write(fd, buf);
257 * command handlers don't close their fd on errors (ret < 0) so that
258 * its caller can send an error message. Otherwise (ret >= 0) it's up
259 * to each individual command to close the fd if necessary.
262 static int com_help(int fd, int argc, char **argv)
266 const char *dflt = "No such command. Available commands:\n";
269 ret = dump_commands(fd);
272 FOR_EACH_COMMAND(i) {
273 if (strcmp(audiod_cmds[i].name, argv[1]))
277 "SYNOPSIS\n\tpara_audioc %s\n"
280 audiod_cmds[i].description,
281 audiod_cmds[i].usage,
284 ret = client_write(fd, buf);
288 ret = client_write(fd, dflt);
290 ret = dump_commands(fd);
297 static int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
299 char *tl = get_task_list(&sched);
302 ret = client_write(fd, tl);
309 static int com_stat(int fd, int argc, char **argv)
311 int i, ret, parser_friendly = 0;
313 const uint64_t one = 1;
314 struct para_buffer b = {.flags = 0};
316 ret = mark_fd_nonblocking(fd);
319 for (i = 1; i < argc; i++) {
320 const char *arg = argv[i];
323 if (!strcmp(arg, "--")) {
327 if (!strncmp(arg, "-p", 2)) {
329 b.flags = PBF_SIZE_PREFIX;
333 mask--; /* set all bits */
334 for (; i < argc; i++) {
335 ret = stat_item_valid(argv[i]);
338 mask |= (one << ret);
340 PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask);
341 FOR_EACH_STATUS_ITEM(i) {
342 char *item = stat_item_values[i];
343 if (!((one << i) & mask))
345 (void)WRITE_STATUS_ITEM(&b, i, "%s\n", item? item : "");
347 ret = client_write(fd, b.buf);
349 ret = stat_client_add(fd, mask, parser_friendly);
354 static int com_grab(int fd, int argc, char **argv)
356 return grab_client_new(fd, argc, argv, &sched);
359 static int com_term(int fd, __a_unused int argc, __a_unused char **argv)
362 return -E_AUDIOD_TERM;
365 static int com_on(int fd, __a_unused int argc, __a_unused char **argv)
367 audiod_status = AUDIOD_ON;
372 static int com_off(int fd, __a_unused int argc, __a_unused char **argv)
374 audiod_status = AUDIOD_OFF;
379 static int com_sb(int fd, __a_unused int argc, __a_unused char **argv)
381 audiod_status = AUDIOD_STANDBY;
386 static int com_cycle(int fd, int argc, char **argv)
388 switch (audiod_status) {
390 return com_sb(fd, argc, argv);
393 return com_on(fd, argc, argv);
396 return com_off(fd, argc, argv);
403 static int com_version(int fd, int argc, char **argv)
408 if (argc > 1 && strcmp(argv[1], "-v") == 0)
409 msg = make_message("%s", version_text("audiod"));
411 msg = make_message("%s\n", version_single_line("audiod"));
412 ret = client_write(fd, msg);
419 static int check_perms(uid_t uid)
423 if (!conf.user_allow_given)
425 for (i = 0; i < conf.user_allow_given; i++)
426 if (uid == conf.user_allow_arg[i])
428 return -E_UCRED_PERM;
432 * Handle arriving connections on the local socket.
434 * \param accept_fd The fd to accept connections on.
435 * \param rfds If \a accept_fd is not set in \a rfds, do nothing.
437 * This is called in each iteration of the select loop. If there is an incoming
438 * connection on \a accept_fd, this function reads the command sent by the peer,
439 * checks the connecting user's permissions by using unix socket credentials
440 * (if supported by the OS) and calls the corresponding command handler if
441 * permissions are OK.
443 * \return Positive on success, negative on errors, zero if there was no
444 * connection to accept.
446 * \sa para_accept(), recv_cred_buffer()
448 int handle_connect(int accept_fd, fd_set *rfds)
450 int i, argc, ret, clifd;
451 char buf[MAXLINE], **argv = NULL;
452 struct sockaddr_un unix_addr;
455 ret = para_accept(accept_fd, rfds, &unix_addr, sizeof(struct sockaddr_un), &clifd);
458 ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
462 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret, buf);
463 ret = check_perms(uid);
466 ret = create_argv(buf, "\n", &argv);
470 //PARA_INFO_LOG("argv[0]: %s, argc = %d\n", argv[0], argc);
471 FOR_EACH_COMMAND(i) {
472 if (strcmp(audiod_cmds[i].name, argv[0]))
474 ret = audiod_cmds[i].handler(clifd, argc, argv);
477 ret = -E_INVALID_AUDIOD_CMD;
480 if (clifd > 0 && ret < 0 && ret != -E_CLIENT_WRITE) {
481 char *tmp = make_message("%s\n", para_strerror(-ret));
482 client_write(clifd, tmp);
490 * Send the current audiod status to all connected stat clients.
492 * \param force Whether to write unchanged items.
494 void audiod_status_dump(bool force)
498 old = stat_item_values[SI_PLAY_TIME];
499 new = get_time_string();
501 if (force || !old || strcmp(old, new)) {
503 stat_item_values[SI_PLAY_TIME] = new;
504 stat_client_write_item(SI_PLAY_TIME);
509 new = daemon_get_uptime_str(now);
510 old = stat_item_values[SI_AUDIOD_UPTIME];
511 if (force || !old || strcmp(old, new)) {
513 stat_item_values[SI_AUDIOD_UPTIME] = new;
514 stat_client_write_item(SI_AUDIOD_UPTIME);
518 old = stat_item_values[SI_AUDIOD_STATUS];
519 new = audiod_status_string();
520 if (force || !old || strcmp(old, new)) {
522 stat_item_values[SI_AUDIOD_STATUS] = new;
523 stat_client_write_item(SI_AUDIOD_STATUS);
527 old = stat_item_values[SI_DECODER_FLAGS];
528 new = decoder_flags();
529 if (force || !old || strcmp(old, new)) {
531 stat_item_values[SI_DECODER_FLAGS] = new;
532 stat_client_write_item(SI_DECODER_FLAGS);
538 * Flush and send all status items.
540 * Send to each connected client the full status item list
543 void clear_and_dump_items(void)
547 FOR_EACH_STATUS_ITEM(i) {
548 free(stat_item_values[i]);
549 stat_item_values[i] = NULL;
550 stat_client_write_item(i);