2 * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file audiod_command.c commands for para_audiod */
10 #include <sys/types.h>
14 #include "audiod.cmdline.h"
18 #include "buffer_tree.h"
20 #include "grab_client.h"
28 #include "audiod_command_list.h"
30 extern char *stat_item_values[NUM_STAT_ITEMS];
32 /** Iterate over the array of all audiod commands. */
33 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
35 /** The maximal number of simultaneous connections. */
36 #define MAX_STAT_CLIENTS 50
38 /** Flags used for the stat command of para_audiod. */
39 enum stat_client_flags {
40 /** Enable parser-friendly output. */
41 SCF_PARSER_FRIENDLY = 1,
45 * Describes a status client of para_audiod.
47 * There's one such structure per audiod client that sent the 'stat' command.
49 * A status client is identified by its file descriptor. para_audiod
50 * keeps a list of connected status clients.
53 /** The stat client's file descriptor. */
55 /** Bitmask of those status items the client is interested in. */
57 /** See \ref stat_client flags. s*/
59 /** Its entry in the list of stat clients. */
60 struct list_head node;
63 static INITIALIZED_LIST_HEAD(client_list);
64 static int num_clients;
66 /** The list of all status items used by para_{server,audiod,gui}. */
67 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
69 static void dump_stat_client_list(void)
71 struct stat_client *sc;
73 list_for_each_entry(sc, &client_list, node)
74 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
77 * Add a status client to the list.
79 * \param fd The file descriptor of the client.
80 * \param mask Bitfield of status items for this client.
81 * \param parser_friendly Enable parser-friendly output mode.
83 * Only those status items having the bit set in \a mask will be
86 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
87 * the number of connected clients exceeds #MAX_STAT_CLIENTS.
89 static int stat_client_add(int fd, uint64_t mask, int parser_friendly)
91 struct stat_client *new_client;
93 if (num_clients >= MAX_STAT_CLIENTS) {
94 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
96 return -E_TOO_MANY_CLIENTS;
98 PARA_INFO_LOG("adding client on fd %d\n", fd);
99 new_client = para_calloc(sizeof(struct stat_client));
101 new_client->item_mask = mask;
103 new_client->flags = SCF_PARSER_FRIENDLY;
104 para_list_add(&new_client->node, &client_list);
105 dump_stat_client_list();
110 * Write a message to all connected status clients.
112 * \param item_num The number of the status item of \a msg.
114 * On write errors, remove the status client from the client list and close its
117 void stat_client_write_item(int item_num)
119 struct stat_client *sc, *tmp;
120 struct para_buffer pb = {.flags = 0};
121 struct para_buffer pfpb = {.flags = PBF_SIZE_PREFIX};
122 const uint64_t one = 1;
124 list_for_each_entry_safe(sc, tmp, &client_list, node) {
125 int fd = sc->fd, ret;
127 if (!((one << item_num) & sc->item_mask))
129 if (write_ok(fd) > 0) {
130 struct para_buffer *b =
131 (sc->flags & SCF_PARSER_FRIENDLY)? &pfpb : &pb;
132 char *msg = stat_item_values[item_num];
134 WRITE_STATUS_ITEM(b, item_num, "%s\n",
136 ret = write(fd, b->buf, b->offset);
137 if (ret == b->offset)
140 /* write error or fd not ready for writing */
143 PARA_INFO_LOG("deleting client on fd %d\n", fd);
146 dump_stat_client_list();
151 // PARA_DEBUG_LOG("%d client(s)\n", num_clients);
155 * Check if string is a known status item.
157 * \param item Buffer containing the text to check.
159 * \return If \a item is a valid status item, the number of that status item is
160 * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
162 static int stat_item_valid(const char *item)
165 if (!item || !*item) {
166 PARA_ERROR_LOG("%s\n", "no item");
167 return -E_UNKNOWN_STAT_ITEM;
169 FOR_EACH_STATUS_ITEM(i)
170 if (!strcmp(status_item_list[i], item))
172 PARA_ERROR_LOG("invalid stat item: %s\n", item);
173 return -E_UNKNOWN_STAT_ITEM;
176 static int client_write(int fd, const char *buf)
183 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
186 __malloc static char *audiod_status_string(void)
188 const char *status = (audiod_status == AUDIOD_ON)?
189 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
190 return para_strdup(status);
193 static int get_play_time_slot_num(void)
195 int i, oldest_slot = -1;
196 struct timeval oldest_wstime = {0, 0};
199 struct slot_info *s = &slot[i];
200 struct timeval wstime;
203 btr_get_node_start(s->wns[0].btrn, &wstime);
204 if (oldest_slot >= 0 && tv_diff(&wstime, &oldest_wstime, NULL) > 0)
206 oldest_wstime = wstime;
209 //PARA_CRIT_LOG("oldest slot: %d\n", oldest_slot);
213 __malloc static char *decoder_flags(void)
216 char flags[MAX_STREAM_SLOTS + 1];
219 struct slot_info *s = &slot[i];
221 if (s->receiver_node)
229 flags[MAX_STREAM_SLOTS] = '\0';
230 return para_strdup(flags);
233 static int dump_commands(int fd)
235 char *buf = para_strdup(""), *tmp = NULL;
239 FOR_EACH_COMMAND(i) {
240 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
241 audiod_cmds[i].description);
245 ret = client_write(fd, buf);
251 * command handlers don't close their fd on errors (ret < 0) so that
252 * its caller can send an error message. Otherwise (ret >= 0) it's up
253 * to each individual command to close the fd if necessary.
256 int com_help(int fd, int argc, char **argv)
260 const char *dflt = "No such command. Available commands:\n";
263 ret = dump_commands(fd);
266 FOR_EACH_COMMAND(i) {
267 if (strcmp(audiod_cmds[i].name, argv[1]))
271 "SYNOPSIS\n\tpara_audioc %s\n"
274 audiod_cmds[i].description,
275 audiod_cmds[i].usage,
278 ret = client_write(fd, buf);
282 ret = client_write(fd, dflt);
284 ret = dump_commands(fd);
291 int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
293 char *tl = get_task_list();
296 ret = client_write(fd, tl);
303 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 for (i = 1; i < argc; i++) {
311 const char *arg = argv[i];
314 if (!strcmp(arg, "--")) {
318 if (!strncmp(arg, "-p", 2)) {
320 b.flags = PBF_SIZE_PREFIX;
325 mask--; /* set all bits */
326 for (; i < argc; i++) {
327 ret = stat_item_valid(argv[i]);
330 mask |= (one << ret);
332 PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask);
333 FOR_EACH_STATUS_ITEM(i) {
334 char *item = stat_item_values[i];
335 if (!((one << i) & mask))
337 WRITE_STATUS_ITEM(&b, i, "%s\n", item? item : "");
339 ret = client_write(fd, b.buf);
341 ret = stat_client_add(fd, mask, parser_friendly);
346 int com_grab(int fd, int argc, char **argv)
348 return grab_client_new(fd, argc, argv);
351 __noreturn int com_term(int fd, __a_unused int argc, __a_unused char **argv)
354 clean_exit(EXIT_SUCCESS, "terminating on user request");
357 int com_on(int fd, __a_unused int argc, __a_unused char **argv)
359 audiod_status = AUDIOD_ON;
364 int com_off(int fd, __a_unused int argc, __a_unused char **argv)
366 audiod_status = AUDIOD_OFF;
371 int com_sb(int fd, __a_unused int argc, __a_unused char **argv)
373 audiod_status = AUDIOD_STANDBY;
378 int com_cycle(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);
395 static int check_perms(uid_t uid)
399 if (!conf.user_allow_given)
401 for (i = 0; i < conf.user_allow_given; i++)
402 if (uid == conf.user_allow_arg[i])
404 return -E_UCRED_PERM;
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 user %i, buf: %s\n", ret, buf);
439 ret = check_perms(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 (clifd > 0 && 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 void audiod_status_dump(void)
470 int slot_num = get_play_time_slot_num();
473 old = stat_item_values[SI_PLAY_TIME];
474 new = get_time_string(slot_num);
476 if (!old || strcmp(old, new)) {
478 stat_item_values[SI_PLAY_TIME] = new;
479 stat_client_write_item(SI_PLAY_TIME);
485 old = stat_item_values[SI_AUDIOD_UPTIME];
486 if (!old || strcmp(old, new)) {
488 stat_item_values[SI_AUDIOD_UPTIME] = new;
489 stat_client_write_item(SI_AUDIOD_UPTIME);
493 old = stat_item_values[SI_AUDIOD_STATUS];
494 new = audiod_status_string();
495 if (!old || strcmp(old, new)) {
497 stat_item_values[SI_AUDIOD_STATUS] = new;
498 stat_client_write_item(SI_AUDIOD_STATUS);
502 old = stat_item_values[SI_DECODER_FLAGS];
503 new = decoder_flags();
504 if (!old || strcmp(old, new)) {
506 stat_item_values[SI_DECODER_FLAGS] = new;
507 stat_client_write_item(SI_DECODER_FLAGS);
513 * Flush and send all status items.
515 * Send to each connected client the full status item list
518 void clear_and_dump_items(void)
522 FOR_EACH_STATUS_ITEM(i) {
523 free(stat_item_values[i]);
524 stat_item_values[i] = NULL;
525 stat_client_write_item(i);