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 /* Defines one command of para_audiod. */
39 struct audiod_command {
41 /* Pointer to the function that handles the command. */
42 int (*handler)(int, int, char **);
43 /* One-line description. */
44 const char *description;
45 /* Summary of the command line options. */
47 /* The long help text. */
51 static struct audiod_command audiod_cmds[] = {DEFINE_AUDIOD_CMD_ARRAY};
53 /** Iterate over the array of all audiod commands. */
54 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
56 /** The maximal number of simultaneous connections. */
57 #define MAX_STAT_CLIENTS 50
59 /** Flags used for the stat command of para_audiod. */
60 enum stat_client_flags {
61 /** Enable parser-friendly output. */
62 SCF_PARSER_FRIENDLY = 1,
66 * Describes a status client of para_audiod.
68 * There's one such structure per audiod client that sent the 'stat' command.
70 * A status client is identified by its file descriptor. para_audiod
71 * keeps a list of connected status clients.
74 /** The stat client's file descriptor. */
76 /** Bitmask of those status items the client is interested in. */
78 /** See \ref stat_client flags. s*/
80 /** Its entry in the list of stat clients. */
81 struct list_head node;
84 static INITIALIZED_LIST_HEAD(client_list);
85 static int num_clients;
87 /** The list of all status items used by para_{server,audiod,gui}. */
88 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
90 static void dump_stat_client_list(void)
92 struct stat_client *sc;
94 list_for_each_entry(sc, &client_list, node)
95 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
98 * Add a status client to the list.
100 * \param fd The file descriptor of the client.
101 * \param mask Bitfield of status items for this client.
102 * \param parser_friendly Enable parser-friendly output mode.
104 * Only those status items having the bit set in \a mask will be
105 * sent to the client.
107 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
108 * the number of connected clients exceeds #MAX_STAT_CLIENTS.
110 static int stat_client_add(int fd, uint64_t mask, int parser_friendly)
112 struct stat_client *new_client;
114 if (num_clients >= MAX_STAT_CLIENTS) {
115 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
117 return -E_TOO_MANY_CLIENTS;
119 PARA_INFO_LOG("adding client on fd %d\n", fd);
120 new_client = para_calloc(sizeof(struct stat_client));
122 new_client->item_mask = mask;
124 new_client->flags = SCF_PARSER_FRIENDLY;
125 para_list_add(&new_client->node, &client_list);
126 dump_stat_client_list();
131 static void close_stat_client(struct stat_client *sc)
133 PARA_INFO_LOG("closing client fd %d\n", sc->fd);
141 * Empty the status clients list.
143 * This iterates over the list of connected status clients, closes each client
144 * file descriptor and frees the resources.
146 void close_stat_clients(void)
148 struct stat_client *sc, *tmp;
150 list_for_each_entry_safe(sc, tmp, &client_list, node)
151 close_stat_client(sc);
152 assert(num_clients == 0);
156 * Write a message to all connected status clients.
158 * \param item_num The number of the status item of \a msg.
160 * On write errors, remove the status client from the client list and close its
163 void stat_client_write_item(int item_num)
165 struct stat_client *sc, *tmp;
166 struct para_buffer pb = {.flags = 0};
167 struct para_buffer pfpb = {.flags = PBF_SIZE_PREFIX};
168 const uint64_t one = 1;
169 char *msg = stat_item_values[item_num];
170 struct para_buffer *b;
172 list_for_each_entry_safe(sc, tmp, &client_list, node) {
175 if (!((one << item_num) & sc->item_mask))
177 b = (sc->flags & SCF_PARSER_FRIENDLY)? &pfpb : &pb;
179 (void)WRITE_STATUS_ITEM(b, item_num, "%s\n",
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;
237 FOR_EACH_COMMAND(i) {
238 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
239 audiod_cmds[i].description);
243 ret = client_write(fd, buf);
249 * command handlers don't close their fd on errors (ret < 0) so that
250 * its caller can send an error message. Otherwise (ret >= 0) it's up
251 * to each individual command to close the fd if necessary.
254 static int com_help(int fd, int argc, char **argv)
258 const char *dflt = "No such command. Available commands:\n";
261 ret = dump_commands(fd);
264 FOR_EACH_COMMAND(i) {
265 if (strcmp(audiod_cmds[i].name, argv[1]))
269 "SYNOPSIS\n\tpara_audioc %s\n"
272 audiod_cmds[i].description,
273 audiod_cmds[i].usage,
276 ret = client_write(fd, buf);
280 ret = client_write(fd, dflt);
282 ret = dump_commands(fd);
289 static int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
291 char *tl = get_task_list(&sched);
295 ret = client_write(fd, tl);
302 static int com_stat(int fd, int argc, char **argv)
304 int i, ret, parser_friendly = 0;
306 const uint64_t one = 1;
307 struct para_buffer b = {.flags = 0};
309 ret = mark_fd_nonblocking(fd);
312 for (i = 1; i < argc; i++) {
313 const char *arg = argv[i];
316 if (!strcmp(arg, "--")) {
320 if (!strncmp(arg, "-p", 2)) {
322 b.flags = PBF_SIZE_PREFIX;
326 mask--; /* set all bits */
327 for (; i < argc; i++) {
328 ret = stat_item_valid(argv[i]);
331 mask |= (one << ret);
333 PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask);
334 FOR_EACH_STATUS_ITEM(i) {
335 char *item = stat_item_values[i];
336 if (!((one << i) & mask))
338 (void)WRITE_STATUS_ITEM(&b, i, "%s\n", item? item : "");
340 ret = client_write(fd, b.buf);
342 ret = stat_client_add(fd, mask, parser_friendly);
347 static int com_grab(int fd, int argc, char **argv)
349 return grab_client_new(fd, argc, argv, &sched);
352 static int com_term(int fd, __a_unused int argc, __a_unused char **argv)
355 return -E_AUDIOD_TERM;
358 static int com_on(int fd, __a_unused int argc, __a_unused char **argv)
360 audiod_status = AUDIOD_ON;
365 static int com_off(int fd, __a_unused int argc, __a_unused char **argv)
367 audiod_status = AUDIOD_OFF;
372 static int com_sb(int fd, __a_unused int argc, __a_unused char **argv)
374 audiod_status = AUDIOD_STANDBY;
379 static int com_cycle(int fd, int argc, char **argv)
381 switch (audiod_status) {
383 return com_sb(fd, argc, argv);
386 return com_on(fd, argc, argv);
389 return com_off(fd, argc, argv);
396 static int com_version(int fd, int argc, char **argv)
401 if (argc > 1 && strcmp(argv[1], "-v") == 0)
402 msg = make_message("%s", version_text("audiod"));
404 msg = make_message("%s\n", version_single_line("audiod"));
405 ret = client_write(fd, msg);
412 static int check_perms(uid_t uid, uid_t *whitelist)
416 if (!conf.user_allow_given)
418 for (i = 0; i < conf.user_allow_given; i++)
419 if (uid == whitelist[i])
421 return -E_UCRED_PERM;
425 * Handle arriving connections on the local socket.
427 * \param accept_fd The fd to accept connections on.
428 * \param rfds If \a accept_fd is not set in \a rfds, do nothing.
429 * \param uid_whitelist Array of UIDs which are allowed to connect.
431 * This is called in each iteration of the select loop. If there is an incoming
432 * connection on \a accept_fd, this function reads the command sent by the peer,
433 * checks the connecting user's permissions by using unix socket credentials
434 * (if supported by the OS) and calls the corresponding command handler if
435 * permissions are OK.
437 * \return Positive on success, negative on errors, zero if there was no
438 * connection to accept.
440 * \sa para_accept(), recv_cred_buffer()
442 int handle_connect(int accept_fd, fd_set *rfds, uid_t *uid_whitelist)
444 int i, argc, ret, clifd;
445 char buf[MAXLINE], **argv = NULL;
446 struct sockaddr_un unix_addr;
449 ret = para_accept(accept_fd, rfds, &unix_addr, sizeof(struct sockaddr_un), &clifd);
452 ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
456 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret, buf);
457 ret = check_perms(uid, uid_whitelist);
460 ret = create_argv(buf, "\n", &argv);
464 //PARA_INFO_LOG("argv[0]: %s, argc = %d\n", argv[0], argc);
465 FOR_EACH_COMMAND(i) {
466 if (strcmp(audiod_cmds[i].name, argv[0]))
468 ret = audiod_cmds[i].handler(clifd, argc, argv);
471 ret = -E_INVALID_AUDIOD_CMD;
474 if (clifd > 0 && ret < 0 && ret != -E_CLIENT_WRITE) {
475 char *tmp = make_message("%s\n", para_strerror(-ret));
476 client_write(clifd, tmp);
484 * Send the current audiod status to all connected stat clients.
486 * \param force Whether to write unchanged items.
488 void audiod_status_dump(bool force)
492 old = stat_item_values[SI_PLAY_TIME];
493 new = get_time_string();
495 if (force || !old || strcmp(old, new)) {
497 stat_item_values[SI_PLAY_TIME] = new;
498 stat_client_write_item(SI_PLAY_TIME);
503 new = daemon_get_uptime_str(now);
504 old = stat_item_values[SI_AUDIOD_UPTIME];
505 if (force || !old || strcmp(old, new)) {
507 stat_item_values[SI_AUDIOD_UPTIME] = new;
508 stat_client_write_item(SI_AUDIOD_UPTIME);
512 old = stat_item_values[SI_AUDIOD_STATUS];
513 new = audiod_status_string();
514 if (force || !old || strcmp(old, new)) {
516 stat_item_values[SI_AUDIOD_STATUS] = new;
517 stat_client_write_item(SI_AUDIOD_STATUS);
521 old = stat_item_values[SI_DECODER_FLAGS];
522 new = audiod_get_decoder_flags();
523 if (force || !old || strcmp(old, new)) {
525 stat_item_values[SI_DECODER_FLAGS] = new;
526 stat_client_write_item(SI_DECODER_FLAGS);
532 * Flush and send all status items.
534 * Send to each connected client the full status item list
537 void clear_and_dump_items(void)
541 FOR_EACH_STATUS_ITEM(i) {
542 free(stat_item_values[i]);
543 stat_item_values[i] = NULL;
544 stat_client_write_item(i);