1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file audiod_command.c Commands for para_audiod. */
5 #include <netinet/in.h>
6 #include <sys/socket.h>
14 #include "audiod.lsg.h"
17 #include "audiod_cmd.lsg.h"
20 #include "buffer_tree.h"
22 #include "grab_client.h"
32 extern struct sched sched;
33 extern char *stat_item_values[NUM_STAT_ITEMS];
35 /** The maximal number of simultaneous connections. */
36 #define MAX_STAT_CLIENTS 50
38 /** Pointer to a command handler function. */
39 typedef int (*audiod_cmd_handler_t)(int, struct lls_parse_result *);
41 /** The lopsub user_data pointer. Only the command handler at the moment. */
42 struct audiod_command_info {
43 audiod_cmd_handler_t handler; /**< Implementation of the command. */
46 /** Define the user_data pointer as expected by lopsub. */
47 #define EXPORT_AUDIOD_CMD_HANDLER(_cmd) \
48 /** Implementation of _cmd. */ \
49 const struct audiod_command_info lsg_audiod_cmd_com_ ## _cmd ## _user_data = { \
50 .handler = com_ ## _cmd \
53 /** Flags used for the stat command of para_audiod. */
54 enum stat_client_flags {
55 /** Enable parser-friendly output. */
56 SCF_PARSER_FRIENDLY = 1,
60 * Describes a status client of para_audiod.
62 * There's one such structure per audiod client that sent the 'stat' command.
64 * A status client is identified by its file descriptor. para_audiod
65 * keeps a list of connected status clients.
68 /** The stat client's file descriptor. */
70 /** Bitmask of those status items the client is interested in. */
72 /** See \ref stat_client flags. */
74 /** Its entry in the list of stat clients. */
75 struct list_head node;
78 static INITIALIZED_LIST_HEAD(client_list);
79 static int num_clients;
81 /** The list of all status items used by para_{server,audiod,gui}. */
82 const char *status_item_list[] = {STATUS_ITEMS};
85 * Add a status client to the global client list and increment num_clients.
87 * The mask parameter specifies which status items are sent to the client.
89 static int stat_client_add(int fd, uint64_t mask, bool parser_friendly)
91 struct stat_client *new_client;
94 if (num_clients >= MAX_STAT_CLIENTS) {
95 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
97 return -E_TOO_MANY_CLIENTS;
101 return -ERRNO_TO_PARA_ERROR(errno);
102 new_client = zalloc(sizeof(*new_client));
103 new_client->fd = ret;
104 PARA_INFO_LOG("adding client on fd %d\n", new_client->fd);
105 new_client->item_mask = mask;
107 new_client->flags = SCF_PARSER_FRIENDLY;
108 para_list_add(&new_client->node, &client_list);
113 static void close_stat_client(struct stat_client *sc)
115 PARA_INFO_LOG("closing client fd %d\n", sc->fd);
123 * Empty the status clients list.
125 * This iterates over the list of connected status clients, closes each client
126 * file descriptor and frees the resources.
128 void close_stat_clients(void)
130 struct stat_client *sc, *tmp;
132 list_for_each_entry_safe(sc, tmp, &client_list, node)
133 close_stat_client(sc);
134 assert(num_clients == 0);
138 * Write a message to all connected status clients.
140 * \param item_num The number of the status item of \a msg.
142 * On write errors, remove the status client from the client list and close its
145 void stat_client_write_item(int item_num)
147 struct stat_client *sc, *tmp;
148 struct para_buffer pb = {.flags = 0};
149 struct para_buffer pfpb = {.flags = PBF_SIZE_PREFIX};
150 const uint64_t one = 1;
151 char *msg = stat_item_values[item_num];
152 struct para_buffer *b;
154 list_for_each_entry_safe(sc, tmp, &client_list, node) {
157 if (!((one << item_num) & sc->item_mask))
159 b = (sc->flags & SCF_PARSER_FRIENDLY)? &pfpb : &pb;
161 WRITE_STATUS_ITEM(b, item_num, "%s\n", msg? msg : "");
162 ret = write(sc->fd, b->buf, b->offset);
163 if (ret == b->offset)
165 /* write error or short write */
166 close_stat_client(sc);
172 /* Check if the given string is a known status item and return its index. */
173 static int stat_item_valid(const char *item)
176 if (!item || !*item) {
177 PARA_ERROR_LOG("%s\n", "no item");
178 return -E_UNKNOWN_STAT_ITEM;
180 FOR_EACH_STATUS_ITEM(i)
181 if (!strcmp(status_item_list[i], item))
183 PARA_ERROR_LOG("invalid stat item: %s\n", item);
184 return -E_UNKNOWN_STAT_ITEM;
187 static int client_write(int fd, const char *buf)
194 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
197 __malloc static char *audiod_status_string(void)
199 const char *status = (audiod_status == AUDIOD_ON)?
200 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
201 return para_strdup(status);
204 static int com_help(int fd, struct lls_parse_result *lpr)
208 const struct lls_opt_result *r =
209 lls_opt_result(LSG_AUDIOD_CMD_HELP_OPT_LONG, lpr);
210 bool long_help = lls_opt_given(r);
212 lsu_com_help(long_help, lpr, audiod_cmd_suite, NULL, &buf, NULL);
213 ret = client_write(fd, buf);
215 return ret < 0? ret : 0;
217 EXPORT_AUDIOD_CMD_HANDLER(help)
219 static int com_ll(int fd, struct lls_parse_result *lpr)
223 const char *sev[] = {SEVERITIES};
225 int ret = lls(lls_check_arg_count(lpr, 0, 1, &errctx));
228 char *tmp = make_message("%s\n", errctx);
230 client_write(fd, tmp);
234 if (lls_num_inputs(lpr) == 0) {
236 ll = daemon_get_loglevel();
237 msg = make_message("%s\n", sev[ll]);
238 ret = client_write(fd, msg);
242 arg = lls_input(0, lpr);
243 for (ll = 0; ll < NUM_LOGLEVELS; ll++) {
244 if (!strcmp(arg, sev[ll]))
247 if (ll >= NUM_LOGLEVELS)
248 return -ERRNO_TO_PARA_ERROR(EINVAL);
249 PARA_INFO_LOG("new log level: %s\n", sev[ll]);
250 daemon_set_loglevel(ll);
253 EXPORT_AUDIOD_CMD_HANDLER(ll)
255 static int com_tasks(int fd, __a_unused struct lls_parse_result *lpr)
258 char *tl = get_task_list(&sched);
260 if (!tl) /* no tasks registered yet */
262 ret = client_write(fd, tl);
266 EXPORT_AUDIOD_CMD_HANDLER(tasks)
268 static int com_stat(int fd, struct lls_parse_result *lpr)
271 bool parser_friendly = false;
273 const uint64_t one = 1;
274 struct para_buffer b = {.flags = 0};
275 const struct lls_opt_result *r;
278 ret = mark_fd_nonblocking(fd);
281 r = lls_opt_result(LSG_AUDIOD_CMD_STAT_OPT_PARSER_FRIENDLY, lpr);
282 if (lls_opt_given(r) > 0) {
283 parser_friendly = true;
284 b.flags = PBF_SIZE_PREFIX;
286 num_inputs = lls_num_inputs(lpr);
288 mask--; /* set all bits */
289 for (i = 0; i < num_inputs; i++) {
290 ret = stat_item_valid(lls_input(i, lpr));
293 mask |= (one << ret);
295 PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask);
296 FOR_EACH_STATUS_ITEM(i) {
297 char *item = stat_item_values[i];
298 if (!((one << i) & mask))
300 WRITE_STATUS_ITEM(&b, i, "%s\n", item? item : "");
302 ret = client_write(fd, b.buf);
304 ret = stat_client_add(fd, mask, parser_friendly);
306 return ret < 0? ret : 0;
308 EXPORT_AUDIOD_CMD_HANDLER(stat)
310 static int com_grab(int fd, struct lls_parse_result *lpr)
312 int ret = grab_client_new(fd, lpr, &sched);
313 return ret < 0? ret : 0;
315 EXPORT_AUDIOD_CMD_HANDLER(grab)
317 static int com_term(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
319 return -E_AUDIOD_TERM;
321 EXPORT_AUDIOD_CMD_HANDLER(term)
323 static int com_on(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
325 audiod_status = AUDIOD_ON;
328 EXPORT_AUDIOD_CMD_HANDLER(on)
330 static int com_off(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
332 audiod_status = AUDIOD_OFF;
335 EXPORT_AUDIOD_CMD_HANDLER(off)
337 static int com_sb(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
339 audiod_status = AUDIOD_STANDBY;
342 EXPORT_AUDIOD_CMD_HANDLER(sb)
344 static int com_cycle(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
346 switch (audiod_status) {
347 case AUDIOD_ON: audiod_status = AUDIOD_STANDBY; break;
348 case AUDIOD_OFF: audiod_status = AUDIOD_ON; break;
349 case AUDIOD_STANDBY: audiod_status = AUDIOD_OFF; break;
353 EXPORT_AUDIOD_CMD_HANDLER(cycle)
355 static int com_version(int fd, struct lls_parse_result *lpr)
359 const struct lls_opt_result *r_v;
361 r_v = lls_opt_result(LSG_AUDIOD_CMD_VERSION_OPT_VERBOSE, lpr);
362 if (lls_opt_given(r_v))
363 msg = make_message("%s", version_text("audiod"));
365 msg = make_message("%s\n", version_single_line("audiod"));
366 ret = client_write(fd, msg);
368 return ret < 0? ret : 0;
370 EXPORT_AUDIOD_CMD_HANDLER(version)
373 * Handle arriving connections on the local socket.
375 * \param accept_fd The fd to accept connections on.
377 * This is called in each iteration of the main loop of the scheduler. If there
378 * is an incoming connection, the function reads the command sent by the peer,
379 * checks the connecting user's permissions by using unix socket credentials
380 * (if supported by the OS) and calls the corresponding command handler if
381 * permissions are OK.
383 * \return Positive on success, negative on errors, zero if there was no
384 * connection to accept.
386 * \sa \ref para_accept(), \ref recv_cred_buffer().
388 int dispatch_local_connection(int accept_fd)
390 int argc, ret, clifd;
391 char buf[MAXLINE], **argv = NULL;
392 struct sockaddr_un unix_addr;
394 const struct lls_command *cmd;
395 struct lls_parse_result *lpr;
397 const struct audiod_command_info *aci;
399 ret = para_accept(accept_fd, &unix_addr, sizeof(struct sockaddr_un), &clifd);
402 ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
406 PARA_INFO_LOG("connection from UID %d, buf: %s\n", ret, buf);
408 if (!uid_is_whitelisted(uid))
410 ret = create_argv(buf, "\n", &argv);
414 ret = lls(lls_lookup_subcmd(argv[0], audiod_cmd_suite, &errctx));
417 cmd = lls_cmd(ret, audiod_cmd_suite);
418 ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
421 aci = lls_user_data(cmd);
422 ret = aci->handler(clifd, lpr);
423 lls_free_parse_result(lpr, cmd);
426 if (ret < 0 && ret != -E_CLIENT_WRITE) {
429 tmp = make_message("%s\n", errctx);
431 client_write(clifd, tmp);
434 tmp = make_message("%s\n", para_strerror(-ret));
435 client_write(clifd, tmp);
443 * Send the current audiod status to all connected stat clients.
445 * \param force Whether to write unchanged items.
447 void audiod_status_dump(bool force)
451 old = stat_item_values[SI_play_time];
452 new = get_time_string();
454 if (force || !old || strcmp(old, new)) {
456 stat_item_values[SI_play_time] = new;
457 stat_client_write_item(SI_play_time);
462 new = daemon_get_uptime_str(now);
463 old = stat_item_values[SI_audiod_uptime];
464 if (force || !old || strcmp(old, new)) {
466 stat_item_values[SI_audiod_uptime] = new;
467 stat_client_write_item(SI_audiod_uptime);
471 old = stat_item_values[SI_audiod_status];
472 new = audiod_status_string();
473 if (force || !old || strcmp(old, new)) {
475 stat_item_values[SI_audiod_status] = new;
476 stat_client_write_item(SI_audiod_status);
480 old = stat_item_values[SI_decoder_flags];
481 new = audiod_get_decoder_flags();
482 if (force || !old || strcmp(old, new)) {
484 stat_item_values[SI_decoder_flags] = new;
485 stat_client_write_item(SI_decoder_flags);
491 * Flush and send all status items.
493 * Send to each connected client the full status item list
496 void clear_and_dump_items(void)
500 FOR_EACH_STATUS_ITEM(i) {
501 free(stat_item_values[i]);
502 stat_item_values[i] = NULL;
503 stat_client_write_item(i);