2 * Copyright (C) 2005-2009 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"
16 #include "close_on_fork.h"
20 #include "grab_client.h"
27 #include "audiod_command_list.h"
29 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 static int client_write(int fd, const char *buf)
42 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
45 __malloc static char *audiod_status_string(void)
47 const char *status = (audiod_status == AUDIOD_ON)?
48 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
49 return para_strdup(status);
52 static int get_play_time_slot_num(void)
57 struct slot_info *s = &slot[i];
60 if (oldest >= 0 && tv_diff(&s->wstime, &slot[oldest].wstime,
68 __malloc static char *decoder_flags(void)
71 char flags[MAX_STREAM_SLOTS + 1];
74 struct slot_info *s = &slot[i];
84 flags[MAX_STREAM_SLOTS] = '\0';
85 return para_strdup(flags);
88 static int dump_commands(int fd)
90 char *buf = para_strdup(""), *tmp = NULL;
95 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
96 audiod_cmds[i].description);
100 ret = client_write(fd, buf);
106 * command handlers don't close their fd on errors (ret < 0) so that
107 * its caller can send an error message. Otherwise (ret >= 0) it's up
108 * to each individual command to close the fd if necessary.
111 int com_help(int fd, int argc, char **argv)
115 const char *dflt = "No such command. Available commands:\n";
118 ret = dump_commands(fd);
121 FOR_EACH_COMMAND(i) {
122 if (strcmp(audiod_cmds[i].name, argv[1]))
126 "SYNOPSIS\n\tpara_audioc %s\n"
129 audiod_cmds[i].description,
130 audiod_cmds[i].usage,
133 ret = client_write(fd, buf);
137 ret = client_write(fd, dflt);
139 ret = dump_commands(fd);
146 int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
148 char *tl = get_task_list();
151 ret = client_write(fd, tl);
158 int com_kill(int fd, int argc, char **argv)
162 return -E_AUDIOD_SYNTAX;
163 for (i = 1; i < argc; i++) {
164 ret = kill_task(argv[i]);
173 int com_stat(int fd, int argc, char **argv)
175 int i, ret, parser_friendly = 0;
177 const uint64_t one = 1;
178 struct para_buffer b = {.flags = 0};
180 for (i = 1; i < argc; i++) {
181 const char *arg = argv[i];
184 if (!strcmp(arg, "--")) {
188 if (!strncmp(arg, "-p", 2)) {
190 b.flags = PBF_SIZE_PREFIX;
195 mask--; /* set all bits */
196 for (; i < argc; i++) {
197 ret = stat_item_valid(argv[i]);
200 mask |= (one << ret);
202 PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask);
203 FOR_EACH_STATUS_ITEM(i) {
204 char *item = stat_item_values[i];
205 if (!((one << i) & mask))
207 WRITE_STATUS_ITEM(&b, i, "%s\n", item? item : "");
209 ret = client_write(fd, b.buf);
211 ret = stat_client_add(fd, mask, parser_friendly);
216 int com_grab(int fd, int argc, char **argv)
218 return grab_client_new(fd, argc, argv);
221 __noreturn int com_term(int fd, __a_unused int argc, __a_unused char **argv)
224 clean_exit(EXIT_SUCCESS, "terminating on user request");
227 int com_on(int fd, __a_unused int argc, __a_unused char **argv)
229 audiod_status = AUDIOD_ON;
234 int com_off(int fd, __a_unused int argc, __a_unused char **argv)
236 audiod_status = AUDIOD_OFF;
241 int com_sb(int fd, __a_unused int argc, __a_unused char **argv)
243 audiod_status = AUDIOD_STANDBY;
248 int com_cycle(int fd, int argc, char **argv)
250 switch (audiod_status) {
252 return com_sb(fd, argc, argv);
255 return com_on(fd, argc, argv);
258 return com_off(fd, argc, argv);
265 static int check_perms(uid_t uid)
269 if (!conf.user_allow_given)
271 for (i = 0; i < conf.user_allow_given; i++)
272 if (uid == conf.user_allow_arg[i])
274 return -E_UCRED_PERM;
278 * handle arriving connections on the local socket
280 * \param accept_fd the fd to call accept() on
282 * This is called whenever para_audiod's main task detects an incoming
283 * connection by the readability of \a accept_fd. This function reads the
284 * command sent by the peer, checks the connecting user's permissions by using
285 * unix socket credentials (if supported by the OS) and calls the corresponding
286 * command handler if permissions are OK.
288 * \return positive on success, negative on errors
290 * \sa para_accept(), recv_cred_buffer()
292 int handle_connect(int accept_fd)
294 int i, argc, ret, clifd = -1;
295 char buf[MAXLINE], **argv = NULL;
296 struct sockaddr_un unix_addr;
299 ret = para_accept(accept_fd, &unix_addr, sizeof(struct sockaddr_un));
303 ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
307 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret, buf);
308 ret = check_perms(uid);
311 ret = create_argv(buf, "\n", &argv);
315 //PARA_INFO_LOG("argv[0]: %s, argc = %d\n", argv[0], argc);
316 FOR_EACH_COMMAND(i) {
317 if (strcmp(audiod_cmds[i].name, argv[0]))
319 ret = audiod_cmds[i].handler(clifd, argc, argv);
322 ret = -E_INVALID_AUDIOD_CMD;
325 if (clifd > 0 && ret < 0 && ret != -E_CLIENT_WRITE) {
326 char *tmp = make_message("%s\n", para_strerror(-ret));
327 client_write(clifd, tmp);
335 * Send the current audiod status to all connected stat clients.
337 void audiod_status_dump(void)
339 int slot_num = get_play_time_slot_num();
342 old = stat_item_values[SI_PLAY_TIME];
343 new = get_time_string(slot_num);
345 if (!old || strcmp(old, new)) {
347 stat_item_values[SI_PLAY_TIME] = new;
348 stat_client_write_item(SI_PLAY_TIME);
354 old = stat_item_values[SI_AUDIOD_UPTIME];
355 if (!old || strcmp(old, new)) {
357 stat_item_values[SI_AUDIOD_UPTIME] = new;
358 stat_client_write_item(SI_AUDIOD_UPTIME);
362 old = stat_item_values[SI_AUDIOD_STATUS];
363 new = audiod_status_string();
364 if (!old || strcmp(old, new)) {
366 stat_item_values[SI_AUDIOD_STATUS] = new;
367 stat_client_write_item(SI_AUDIOD_STATUS);
371 old = stat_item_values[SI_DECODER_FLAGS];
372 new = decoder_flags();
373 if (!old || strcmp(old, new)) {
375 stat_item_values[SI_DECODER_FLAGS] = new;
376 stat_client_write_item(SI_DECODER_FLAGS);
382 * Flush and send all status items.
384 * Send to each connected client the full status item list
387 void clear_and_dump_items(void)
391 FOR_EACH_STATUS_ITEM(i) {
392 free(stat_item_values[i]);
393 stat_item_values[i] = NULL;
394 stat_client_write_item(i);