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. s*/
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 (void)WRITE_STATUS_ITEM(b, item_num, "%s\n",
194 ret = write(sc->fd, b->buf, b->offset);
195 if (ret == b->offset)
197 /* write error or short write */
198 close_stat_client(sc);
199 dump_stat_client_list();
206 * Check if string is a known status item.
208 * \param item Buffer containing the text to check.
210 * \return If \a item is a valid status item, the number of that status item is
211 * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
213 static int stat_item_valid(const char *item)
216 if (!item || !*item) {
217 PARA_ERROR_LOG("%s\n", "no item");
218 return -E_UNKNOWN_STAT_ITEM;
220 FOR_EACH_STATUS_ITEM(i)
221 if (!strcmp(status_item_list[i], item))
223 PARA_ERROR_LOG("invalid stat item: %s\n", item);
224 return -E_UNKNOWN_STAT_ITEM;
227 static int client_write(int fd, const char *buf)
234 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
237 __malloc static char *audiod_status_string(void)
239 const char *status = (audiod_status == AUDIOD_ON)?
240 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
241 return para_strdup(status);
244 static int dump_commands(int fd)
246 char *buf = para_strdup(""), *tmp = NULL;
250 FOR_EACH_COMMAND(i) {
251 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
252 audiod_cmds[i].description);
256 ret = client_write(fd, buf);
261 static int com_help(int fd, int argc, char **argv)
267 ret = dump_commands(fd);
270 FOR_EACH_COMMAND(i) {
271 if (strcmp(audiod_cmds[i].name, argv[1]))
275 "SYNOPSIS\n\tpara_audioc %s\n"
278 audiod_cmds[i].description,
279 audiod_cmds[i].usage,
282 ret = client_write(fd, buf);
286 ret = client_write(fd, "No such command. Available commands:\n");
288 ret = dump_commands(fd);
290 return ret < 0? ret : 0;
293 static int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
295 char *tl = get_task_list(&sched);
299 ret = client_write(fd, tl);
301 return ret < 0? ret : 0;
304 static int com_stat(int fd, int argc, char **argv)
306 int i, ret, parser_friendly = 0;
308 const uint64_t one = 1;
309 struct para_buffer b = {.flags = 0};
311 ret = mark_fd_nonblocking(fd);
314 for (i = 1; i < argc; i++) {
315 const char *arg = argv[i];
318 if (!strcmp(arg, "--")) {
322 if (!strncmp(arg, "-p", 2)) {
324 b.flags = PBF_SIZE_PREFIX;
328 mask--; /* set all bits */
329 for (; i < argc; i++) {
330 ret = stat_item_valid(argv[i]);
333 mask |= (one << ret);
335 PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask);
336 FOR_EACH_STATUS_ITEM(i) {
337 char *item = stat_item_values[i];
338 if (!((one << i) & mask))
340 (void)WRITE_STATUS_ITEM(&b, i, "%s\n", item? item : "");
342 ret = client_write(fd, b.buf);
344 ret = stat_client_add(fd, mask, parser_friendly);
346 return ret < 0? ret : 0;
349 static int com_grab(int fd, int argc, char **argv)
351 int ret = grab_client_new(fd, argc, argv, &sched);
352 return ret < 0? ret : 0;
355 static int com_term(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
357 return -E_AUDIOD_TERM;
360 static int com_on(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
362 audiod_status = AUDIOD_ON;
366 static int com_off(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
368 audiod_status = AUDIOD_OFF;
372 static int com_sb(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
374 audiod_status = AUDIOD_STANDBY;
378 static int com_cycle(__a_unused int fd, int argc, char **argv)
380 switch (audiod_status) {
382 return com_sb(fd, argc, argv);
385 return com_on(fd, argc, argv);
388 return com_off(fd, argc, argv);
394 static int com_version(int fd, int argc, char **argv)
399 if (argc > 1 && strcmp(argv[1], "-v") == 0)
400 msg = make_message("%s", version_text("audiod"));
402 msg = make_message("%s\n", version_single_line("audiod"));
403 ret = client_write(fd, msg);
405 return ret < 0? ret : 0;
408 static int check_perms(uid_t uid, uid_t *whitelist)
412 if (!conf.user_allow_given)
414 for (i = 0; i < conf.user_allow_given; i++)
415 if (uid == whitelist[i])
417 return -E_UCRED_PERM;
421 * Handle arriving connections on the local socket.
423 * \param accept_fd The fd to accept connections on.
424 * \param rfds If \a accept_fd is not set in \a rfds, do nothing.
425 * \param uid_whitelist Array of UIDs which are allowed to connect.
427 * This is called in each iteration of the select loop. If there is an incoming
428 * connection on \a accept_fd, this function reads the command sent by the peer,
429 * checks the connecting user's permissions by using unix socket credentials
430 * (if supported by the OS) and calls the corresponding command handler if
431 * permissions are OK.
433 * \return Positive on success, negative on errors, zero if there was no
434 * connection to accept.
436 * \sa para_accept(), recv_cred_buffer()
438 int handle_connect(int accept_fd, fd_set *rfds, uid_t *uid_whitelist)
440 int i, argc, ret, clifd;
441 char buf[MAXLINE], **argv = NULL;
442 struct sockaddr_un unix_addr;
445 ret = para_accept(accept_fd, rfds, &unix_addr, sizeof(struct sockaddr_un), &clifd);
448 ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
452 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret, buf);
453 ret = check_perms(uid, uid_whitelist);
456 ret = create_argv(buf, "\n", &argv);
460 //PARA_INFO_LOG("argv[0]: %s, argc = %d\n", argv[0], argc);
461 FOR_EACH_COMMAND(i) {
462 if (strcmp(audiod_cmds[i].name, argv[0]))
464 ret = audiod_cmds[i].handler(clifd, argc, argv);
467 ret = -E_INVALID_AUDIOD_CMD;
470 if (ret < 0 && ret != -E_CLIENT_WRITE) {
471 char *tmp = make_message("%s\n", para_strerror(-ret));
472 client_write(clifd, tmp);
480 * Send the current audiod status to all connected stat clients.
482 * \param force Whether to write unchanged items.
484 void audiod_status_dump(bool force)
488 old = stat_item_values[SI_PLAY_TIME];
489 new = get_time_string();
491 if (force || !old || strcmp(old, new)) {
493 stat_item_values[SI_PLAY_TIME] = new;
494 stat_client_write_item(SI_PLAY_TIME);
499 new = daemon_get_uptime_str(now);
500 old = stat_item_values[SI_AUDIOD_UPTIME];
501 if (force || !old || strcmp(old, new)) {
503 stat_item_values[SI_AUDIOD_UPTIME] = new;
504 stat_client_write_item(SI_AUDIOD_UPTIME);
508 old = stat_item_values[SI_AUDIOD_STATUS];
509 new = audiod_status_string();
510 if (force || !old || strcmp(old, new)) {
512 stat_item_values[SI_AUDIOD_STATUS] = new;
513 stat_client_write_item(SI_AUDIOD_STATUS);
517 old = stat_item_values[SI_DECODER_FLAGS];
518 new = audiod_get_decoder_flags();
519 if (force || !old || strcmp(old, new)) {
521 stat_item_values[SI_DECODER_FLAGS] = new;
522 stat_client_write_item(SI_DECODER_FLAGS);
528 * Flush and send all status items.
530 * Send to each connected client the full status item list
533 void clear_and_dump_items(void)
537 FOR_EACH_STATUS_ITEM(i) {
538 free(stat_item_values[i]);
539 stat_item_values[i] = NULL;
540 stat_client_write_item(i);