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. */
45 audiod_command_handler_t *handler;
46 /* One-line description. */
47 const char *description;
48 /* Summary of the command line options. */
50 /* The long help text. */
54 static struct audiod_command audiod_cmds[] = {DEFINE_AUDIOD_CMD_ARRAY};
56 /** Iterate over the array of all audiod commands. */
57 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
59 /** The maximal number of simultaneous connections. */
60 #define MAX_STAT_CLIENTS 50
62 /** Flags used for the stat command of para_audiod. */
63 enum stat_client_flags {
64 /** Enable parser-friendly output. */
65 SCF_PARSER_FRIENDLY = 1,
69 * Describes a status client of para_audiod.
71 * There's one such structure per audiod client that sent the 'stat' command.
73 * A status client is identified by its file descriptor. para_audiod
74 * keeps a list of connected status clients.
77 /** The stat client's file descriptor. */
79 /** Bitmask of those status items the client is interested in. */
81 /** See \ref stat_client flags. */
83 /** Its entry in the list of stat clients. */
84 struct list_head node;
87 static INITIALIZED_LIST_HEAD(client_list);
88 static int num_clients;
90 /** The list of all status items used by para_{server,audiod,gui}. */
91 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
93 static void dump_stat_client_list(void)
95 struct stat_client *sc;
97 list_for_each_entry(sc, &client_list, node)
98 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
101 * Add a status client to the list.
103 * \param fd The file descriptor of the client.
104 * \param mask Bitfield of status items for this client.
105 * \param parser_friendly Enable parser-friendly output mode.
107 * Only those status items having the bit set in \a mask will be
108 * sent to the client.
110 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
111 * the number of connected clients exceeds #MAX_STAT_CLIENTS.
113 static int stat_client_add(int fd, uint64_t mask, int parser_friendly)
115 struct stat_client *new_client;
118 if (num_clients >= MAX_STAT_CLIENTS) {
119 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
121 return -E_TOO_MANY_CLIENTS;
125 return -ERRNO_TO_PARA_ERROR(errno);
126 new_client = para_calloc(sizeof(*new_client));
127 new_client->fd = ret;
128 PARA_INFO_LOG("adding client on fd %d\n", new_client->fd);
129 new_client->item_mask = mask;
131 new_client->flags = SCF_PARSER_FRIENDLY;
132 para_list_add(&new_client->node, &client_list);
133 dump_stat_client_list();
138 static void close_stat_client(struct stat_client *sc)
140 PARA_INFO_LOG("closing client fd %d\n", sc->fd);
148 * Empty the status clients list.
150 * This iterates over the list of connected status clients, closes each client
151 * file descriptor and frees the resources.
153 void close_stat_clients(void)
155 struct stat_client *sc, *tmp;
157 list_for_each_entry_safe(sc, tmp, &client_list, node)
158 close_stat_client(sc);
159 assert(num_clients == 0);
163 * Write a message to all connected status clients.
165 * \param item_num The number of the status item of \a msg.
167 * On write errors, remove the status client from the client list and close its
170 void stat_client_write_item(int item_num)
172 struct stat_client *sc, *tmp;
173 struct para_buffer pb = {.flags = 0};
174 struct para_buffer pfpb = {.flags = PBF_SIZE_PREFIX};
175 const uint64_t one = 1;
176 char *msg = stat_item_values[item_num];
177 struct para_buffer *b;
179 list_for_each_entry_safe(sc, tmp, &client_list, node) {
182 if (!((one << item_num) & sc->item_mask))
184 b = (sc->flags & SCF_PARSER_FRIENDLY)? &pfpb : &pb;
186 (void)WRITE_STATUS_ITEM(b, item_num, "%s\n",
188 ret = write(sc->fd, b->buf, b->offset);
189 if (ret == b->offset)
191 /* write error or short write */
192 close_stat_client(sc);
193 dump_stat_client_list();
200 * Check if string is a known status item.
202 * \param item Buffer containing the text to check.
204 * \return If \a item is a valid status item, the number of that status item is
205 * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
207 static int stat_item_valid(const char *item)
210 if (!item || !*item) {
211 PARA_ERROR_LOG("%s\n", "no item");
212 return -E_UNKNOWN_STAT_ITEM;
214 FOR_EACH_STATUS_ITEM(i)
215 if (!strcmp(status_item_list[i], item))
217 PARA_ERROR_LOG("invalid stat item: %s\n", item);
218 return -E_UNKNOWN_STAT_ITEM;
221 static int client_write(int fd, const char *buf)
228 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
231 __malloc static char *audiod_status_string(void)
233 const char *status = (audiod_status == AUDIOD_ON)?
234 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
235 return para_strdup(status);
238 static int dump_commands(int fd)
240 char *buf = para_strdup(""), *tmp = NULL;
244 FOR_EACH_COMMAND(i) {
245 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
246 audiod_cmds[i].description);
250 ret = client_write(fd, buf);
255 static int com_help(int fd, int argc, char **argv)
259 const char *dflt = "No such command. Available commands:\n";
262 return dump_commands(fd);
263 FOR_EACH_COMMAND(i) {
264 if (strcmp(audiod_cmds[i].name, argv[1]))
268 "SYNOPSIS\n\tpara_audioc %s\n"
271 audiod_cmds[i].description,
272 audiod_cmds[i].usage,
275 ret = client_write(fd, buf);
279 ret = client_write(fd, dflt);
281 ret = dump_commands(fd);
285 static int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
287 char *tl = get_task_list(&sched);
291 ret = client_write(fd, tl);
296 static int com_stat(int fd, int argc, char **argv)
298 int i, ret, parser_friendly = 0;
300 const uint64_t one = 1;
301 struct para_buffer b = {.flags = 0};
303 ret = mark_fd_nonblocking(fd);
306 for (i = 1; i < argc; i++) {
307 const char *arg = argv[i];
310 if (!strcmp(arg, "--")) {
314 if (!strncmp(arg, "-p", 2)) {
316 b.flags = PBF_SIZE_PREFIX;
320 mask--; /* set all bits */
321 for (; i < argc; i++) {
322 ret = stat_item_valid(argv[i]);
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 (void)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);
341 static int com_grab(int fd, int argc, char **argv)
343 return grab_client_new(fd, argc, argv, &sched);
346 static int com_term(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
348 return -E_AUDIOD_TERM;
351 static int com_on(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
353 audiod_status = AUDIOD_ON;
357 static int com_off(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
359 audiod_status = AUDIOD_OFF;
363 static int com_sb(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
365 audiod_status = AUDIOD_STANDBY;
369 static int com_cycle(__a_unused int fd, int argc, char **argv)
371 switch (audiod_status) {
373 return com_sb(fd, argc, argv);
376 return com_on(fd, argc, argv);
379 return com_off(fd, argc, argv);
385 static int com_version(int fd, int argc, char **argv)
390 if (argc > 1 && strcmp(argv[1], "-v") == 0)
391 msg = make_message("%s", version_text("audiod"));
393 msg = make_message("%s\n", version_single_line("audiod"));
394 ret = client_write(fd, msg);
399 static int check_perms(uid_t uid, uid_t *whitelist)
403 if (!conf.user_allow_given)
405 for (i = 0; i < conf.user_allow_given; i++)
406 if (uid == whitelist[i])
408 return -E_UCRED_PERM;
412 * Handle arriving connections on the local socket.
414 * \param accept_fd The fd to accept connections on.
415 * \param rfds If \a accept_fd is not set in \a rfds, do nothing.
416 * \param uid_whitelist Array of UIDs which are allowed to connect.
418 * This is called in each iteration of the select loop. If there is an incoming
419 * connection on \a accept_fd, this function reads the command sent by the peer,
420 * checks the connecting user's permissions by using unix socket credentials
421 * (if supported by the OS) and calls the corresponding command handler if
422 * permissions are OK.
424 * \return Positive on success, negative on errors, zero if there was no
425 * connection to accept.
427 * \sa para_accept(), recv_cred_buffer()
429 int handle_connect(int accept_fd, fd_set *rfds, uid_t *uid_whitelist)
431 int i, argc, ret, clifd;
432 char buf[MAXLINE], **argv = NULL;
433 struct sockaddr_un unix_addr;
436 ret = para_accept(accept_fd, rfds, &unix_addr, sizeof(struct sockaddr_un), &clifd);
439 ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
443 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret, buf);
444 ret = check_perms(uid, uid_whitelist);
447 ret = create_argv(buf, "\n", &argv);
451 //PARA_INFO_LOG("argv[0]: %s, argc = %d\n", argv[0], argc);
452 FOR_EACH_COMMAND(i) {
453 if (strcmp(audiod_cmds[i].name, argv[0]))
455 ret = audiod_cmds[i].handler(clifd, argc, argv);
458 ret = -E_INVALID_AUDIOD_CMD;
461 if (ret < 0 && ret != -E_CLIENT_WRITE) {
462 char *tmp = make_message("%s\n", para_strerror(-ret));
463 client_write(clifd, tmp);
471 * Send the current audiod status to all connected stat clients.
473 * \param force Whether to write unchanged items.
475 void audiod_status_dump(bool force)
479 old = stat_item_values[SI_PLAY_TIME];
480 new = get_time_string();
482 if (force || !old || strcmp(old, new)) {
484 stat_item_values[SI_PLAY_TIME] = new;
485 stat_client_write_item(SI_PLAY_TIME);
490 new = daemon_get_uptime_str(now);
491 old = stat_item_values[SI_AUDIOD_UPTIME];
492 if (force || !old || strcmp(old, new)) {
494 stat_item_values[SI_AUDIOD_UPTIME] = new;
495 stat_client_write_item(SI_AUDIOD_UPTIME);
499 old = stat_item_values[SI_AUDIOD_STATUS];
500 new = audiod_status_string();
501 if (force || !old || strcmp(old, new)) {
503 stat_item_values[SI_AUDIOD_STATUS] = new;
504 stat_client_write_item(SI_AUDIOD_STATUS);
508 old = stat_item_values[SI_DECODER_FLAGS];
509 new = audiod_get_decoder_flags();
510 if (force || !old || strcmp(old, new)) {
512 stat_item_values[SI_DECODER_FLAGS] = new;
513 stat_client_write_item(SI_DECODER_FLAGS);
519 * Flush and send all status items.
521 * Send to each connected client the full status item list
524 void clear_and_dump_items(void)
528 FOR_EACH_STATUS_ITEM(i) {
529 free(stat_item_values[i]);
530 stat_item_values[i] = NULL;
531 stat_client_write_item(i);