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 */
13 #include "audiod.cmdline.h"
15 #include "close_on_fork.h"
19 #include "grab_client.cmdline.h"
20 #include "grab_client.h"
28 #include "audiod_command_list.h"
30 extern char *stat_item_values[NUM_STAT_ITEMS];
33 /** iterate over the array of all audiod commands */
34 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
36 static int client_write(int fd, const char *buf)
43 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
46 __malloc static char *audiod_status_string(void)
48 const char *status = (audiod_status == AUDIOD_ON)?
49 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
50 return para_strdup(status);
53 static int get_play_time_slot_num(void)
58 struct slot_info *s = &slot[i];
61 if (oldest >= 0 && tv_diff(&s->wstime, &slot[oldest].wstime,
69 __malloc static char *decoder_flags(void)
72 char flags[MAX_STREAM_SLOTS + 1];
75 struct slot_info *s = &slot[i];
85 flags[MAX_STREAM_SLOTS] = '\0';
86 return para_strdup(flags);
89 static int dump_commands(int fd)
91 char *buf = para_strdup(""), *tmp = NULL;
96 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
97 audiod_cmds[i].description);
101 ret = client_write(fd, buf);
107 * command handlers don't close their fd on errors (ret < 0) so that
108 * its caller can send an error message. Otherwise (ret >= 0) it's up
109 * to each individual command to close the fd if necessary.
112 int com_help(int fd, int argc, char **argv)
116 const char *dflt = "No such command. Available commands:\n";
119 ret = dump_commands(fd);
122 FOR_EACH_COMMAND(i) {
123 if (strcmp(audiod_cmds[i].name, argv[1]))
127 "SYNOPSIS\n\tpara_audioc %s\n"
130 audiod_cmds[i].description,
131 audiod_cmds[i].usage,
134 ret = client_write(fd, buf);
138 ret = client_write(fd, dflt);
140 ret = dump_commands(fd);
147 int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
149 char *tl = get_task_list();
152 ret = client_write(fd, tl);
159 int com_kill(int fd, int argc, char **argv)
163 return -E_AUDIOD_SYNTAX;
164 for (i = 1; i < argc; i++) {
165 ret = kill_task(argv[i]);
174 int com_stat(int fd, int argc, char **argv)
176 int i, ret, parser_friendly = 0;
178 const uint64_t one = 1;
179 struct para_buffer b = {.flags = 0};
181 for (i = 1; i < argc; i++) {
182 const char *arg = argv[i];
185 if (!strcmp(arg, "--")) {
189 if (!strncmp(arg, "-p", 2)) {
191 b.flags = PBF_SIZE_PREFIX;
196 mask--; /* set all bits */
197 for (; i < argc; i++) {
198 ret = stat_item_valid(argv[i]);
201 mask |= (one << ret);
203 PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask);
204 FOR_EACH_STATUS_ITEM(i) {
205 char *item = stat_item_values[i];
206 if (!((one << i) & mask))
208 WRITE_STATUS_ITEM(&b, i, "%s\n", item? item : "");
210 ret = client_write(fd, b.buf);
212 ret = stat_client_add(fd, mask, parser_friendly);
218 static struct filter_node *find_filter_node(int slot_num, int format, int filternum)
223 struct slot_info *s = &slot[i];
224 if (s->format < 0 || !s->fc)
226 if (slot_num >= 0 && slot_num != i)
228 if (format >= 0 && s->format != format)
230 if (num_filters(i) <= filternum)
233 return s->fc->filter_nodes + filternum;
239 int com_grab(int fd, __a_unused int argc, __a_unused char **argv)
241 client_write(fd, "grab is currently b0rken\n");
245 struct grab_client *gc;
246 struct filter_node *fn;
250 gc = grab_client_new(fd, cmdline, &err);
253 fn = find_filter_node(gc->conf->slot_arg, gc->audio_format_num, gc->conf->filter_num_arg);
255 activate_grab_client(gc, fn);
258 if (err != -E_GC_HELP_GIVEN && err != -E_GC_VERSION_GIVEN)
260 if (err == -E_GC_HELP_GIVEN) {
261 msg = make_message("%s\n\n", grab_client_args_info_usage);
262 for (i = 0; grab_client_args_info_help[i]; i++) {
263 char *tmp = make_message("%s%s\n", msg,
264 grab_client_args_info_help[i]);
269 msg = make_message("%s %s\n",
270 GRAB_CLIENT_CMDLINE_PARSER_PACKAGE,
271 GRAB_CLIENT_CMDLINE_PARSER_VERSION);
272 err = client_write(fd, msg);
281 __noreturn int com_term(int fd, __a_unused int argc, __a_unused char **argv)
284 clean_exit(EXIT_SUCCESS, "terminating on user request");
287 int com_on(int fd, __a_unused int argc, __a_unused char **argv)
289 audiod_status = AUDIOD_ON;
294 int com_off(int fd, __a_unused int argc, __a_unused char **argv)
296 audiod_status = AUDIOD_OFF;
301 int com_sb(int fd, __a_unused int argc, __a_unused char **argv)
303 audiod_status = AUDIOD_STANDBY;
308 int com_cycle(int fd, int argc, char **argv)
310 switch (audiod_status) {
312 return com_sb(fd, argc, argv);
315 return com_on(fd, argc, argv);
318 return com_off(fd, argc, argv);
325 static int check_perms(uid_t uid)
329 if (!conf.user_allow_given)
331 for (i = 0; i < conf.user_allow_given; i++)
332 if (uid == conf.user_allow_arg[i])
334 return -E_UCRED_PERM;
338 * handle arriving connections on the local socket
340 * \param accept_fd the fd to call accept() on
342 * This is called whenever para_audiod's main task detects an incoming
343 * connection by the readability of \a accept_fd. This function reads the
344 * command sent by the peer, checks the connecting user's permissions by using
345 * unix socket credentials (if supported by the OS) and calls the corresponding
346 * command handler if permissions are OK.
348 * \return positive on success, negative on errors
350 * \sa para_accept(), recv_cred_buffer()
352 int handle_connect(int accept_fd)
354 int i, argc, ret, clifd = -1;
355 char buf[MAXLINE], **argv = NULL;
356 struct sockaddr_un unix_addr;
359 ret = para_accept(accept_fd, &unix_addr, sizeof(struct sockaddr_un));
363 ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
367 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret, buf);
368 ret = check_perms(uid);
371 ret = create_argv(buf, "\n", &argv);
375 //PARA_INFO_LOG("argv[0]: %s, argc = %d\n", argv[0], argc);
376 FOR_EACH_COMMAND(i) {
377 if (strcmp(audiod_cmds[i].name, argv[0]))
379 ret = audiod_cmds[i].handler(clifd, argc, argv);
382 ret = -E_INVALID_AUDIOD_CMD;
385 if (clifd > 0 && ret < 0 && ret != -E_CLIENT_WRITE) {
386 char *tmp = make_message("%s\n", para_strerror(-ret));
387 client_write(clifd, tmp);
394 * Send the current audiod status to all connected stat clients.
396 void audiod_status_dump(void)
398 int slot_num = get_play_time_slot_num();
401 old = stat_item_values[SI_PLAY_TIME];
402 new = get_time_string(slot_num);
404 if (!old || strcmp(old, new)) {
406 stat_item_values[SI_PLAY_TIME] = new;
407 stat_client_write_item(SI_PLAY_TIME);
413 old = stat_item_values[SI_AUDIOD_UPTIME];
414 if (!old || strcmp(old, new)) {
416 stat_item_values[SI_AUDIOD_UPTIME] = new;
417 stat_client_write_item(SI_AUDIOD_UPTIME);
421 old = stat_item_values[SI_AUDIOD_STATUS];
422 new = audiod_status_string();
423 if (!old || strcmp(old, new)) {
425 stat_item_values[SI_AUDIOD_STATUS] = new;
426 stat_client_write_item(SI_AUDIOD_STATUS);
430 old = stat_item_values[SI_DECODER_FLAGS];
431 new = decoder_flags();
432 if (!old || strcmp(old, new)) {
434 stat_item_values[SI_DECODER_FLAGS] = new;
435 stat_client_write_item(SI_DECODER_FLAGS);
441 * Flush and send all status items.
443 * Send to each connected client the full status item list
446 void clear_and_dump_items(void)
450 FOR_EACH_STATUS_ITEM(i) {
451 free(stat_item_values[i]);
452 stat_item_values[i] = NULL;
453 stat_client_write_item(i);