2 * Copyright (C) 2005-2008 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"
18 #include "grab_client.cmdline.h"
19 #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)
37 size_t len = strlen(buf);
38 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
41 __malloc static char *audiod_status_string(void)
43 const char *status = (audiod_status == AUDIOD_ON)?
44 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
45 return make_message("%s: %s\n", status_item_list[SI_AUDIOD_STATUS], status);
48 static struct timeval *wstime(void)
51 struct timeval *max = NULL;
53 struct slot_info *s = &slot[i];
56 if (max && tv_diff(&s->wstime, max, NULL) <= 0)
62 __malloc static char *decoder_flags(void)
65 char flags[MAX_STREAM_SLOTS + 1];
68 struct slot_info *s = &slot[i];
76 flags[MAX_STREAM_SLOTS] = '\0';
77 return make_message("%s: %s\n", status_item_list[SI_DECODER_FLAGS],
81 static int dump_commands(int fd)
83 char *buf = para_strdup(""), *tmp = NULL;
88 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
89 audiod_cmds[i].description);
93 ret = client_write(fd, buf);
99 * command handlers don't close their fd on errors (ret < 0) so that
100 * its caller can send an error message. Otherwise (ret >= 0) it's up
101 * to each individual command to close the fd if necessary.
104 int com_help(int fd, int argc, char **argv)
108 const char *dflt = "No such command. Available commands:\n";
111 ret = dump_commands(fd);
114 FOR_EACH_COMMAND(i) {
115 if (strcmp(audiod_cmds[i].name, argv[1]))
119 "SYNOPSIS\n\tpara_audioc %s\n"
122 audiod_cmds[i].description,
123 audiod_cmds[i].usage,
126 ret = client_write(fd, buf);
130 ret = client_write(fd, dflt);
132 ret = dump_commands(fd);
139 int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
141 char *tl = get_task_list();
144 ret = client_write(fd, tl);
151 int com_kill(int fd, int argc, char **argv)
155 return -E_AUDIOD_SYNTAX;
156 for (i = 1; i < argc; i++) {
157 ret = kill_task(argv[i]);
166 int com_stat(int fd, __a_unused int argc, __a_unused char **argv)
170 long unsigned mask = ~0LU;
174 for (i = 1; i < argc; i++) {
175 ret = stat_item_valid(argv[i]);
181 PARA_INFO_LOG("mask: 0x%lx\n", mask);
182 if (mask & (1 << SI_PLAY_TIME)) {
183 struct timeval *t = wstime();
184 char *ts = get_time_string(t);
186 ret = client_write(fd, ts);
192 if (mask & (1 << SI_AUDIOD_UPTIME)) {
193 char *tmp, *us = uptime_str();
194 tmp = make_message("%s: %s\n",
195 status_item_list[SI_AUDIOD_UPTIME], us);
197 ret = client_write(fd, tmp);
202 if (mask & (1 << SI_AUDIOD_STATUS)) {
203 char *s = audiod_status_string();
204 ret = client_write(fd, s);
209 if (mask & (1 << SI_DECODER_FLAGS)) {
210 char *df = decoder_flags();
211 ret = client_write(fd, df);
216 FOR_EACH_STATUS_ITEM(i) {
218 if (!((1 << i) & mask))
220 v = stat_item_values[i];
221 tmp = make_message("%s%s%s", buf? buf: "",
222 v? v : "", v? "\n" : "");
226 ret = client_write(fd, buf);
229 ret = stat_client_add(fd, mask);
234 static struct filter_node *find_filter_node(int slot_num, int format, int filternum)
239 struct slot_info *s = &slot[i];
240 if (s->format < 0 || !s->fc)
242 if (slot_num >= 0 && slot_num != i)
244 if (format >= 0 && s->format != format)
246 if (num_filters(i) <= filternum)
249 return s->fc->filter_nodes + filternum;
254 int com_grab(int fd, char *cmdline)
256 struct grab_client *gc;
257 struct filter_node *fn;
261 gc = grab_client_new(fd, cmdline, &err);
264 fn = find_filter_node(gc->conf->slot_arg, gc->audio_format_num, gc->conf->filter_num_arg);
266 activate_grab_client(gc, fn);
269 if (err != -E_GC_HELP_GIVEN && err != -E_GC_VERSION_GIVEN)
271 if (err == -E_GC_HELP_GIVEN) {
272 msg = make_message("%s\n\n", grab_client_args_info_usage);
273 for (i = 0; grab_client_args_info_help[i]; i++) {
274 char *tmp = make_message("%s%s\n", msg,
275 grab_client_args_info_help[i]);
280 msg = make_message("%s %s\n",
281 GRAB_CLIENT_CMDLINE_PARSER_PACKAGE,
282 GRAB_CLIENT_CMDLINE_PARSER_VERSION);
283 err = client_write(fd, msg);
291 __noreturn int com_term(int fd, __a_unused int argc, __a_unused char **argv)
294 clean_exit(EXIT_SUCCESS, "terminating on user request");
297 int com_on(int fd, __a_unused int argc, __a_unused char **argv)
299 audiod_status = AUDIOD_ON;
304 int com_off(int fd, __a_unused int argc, __a_unused char **argv)
306 audiod_status = AUDIOD_OFF;
311 int com_sb(int fd, __a_unused int argc, __a_unused char **argv)
313 audiod_status = AUDIOD_STANDBY;
318 int com_cycle(int fd, int argc, char **argv)
320 switch (audiod_status) {
322 return com_sb(fd, argc, argv);
325 return com_on(fd, argc, argv);
328 return com_off(fd, argc, argv);
335 static int check_perms(uid_t uid)
339 if (!conf.user_allow_given)
341 for (i = 0; i < conf.user_allow_given; i++)
342 if (uid == conf.user_allow_arg[i])
344 return -E_UCRED_PERM;
348 * handle arriving connections on the local socket
350 * \param accept_fd the fd to call accept() on
352 * This is called whenever para_audiod's main task detects an incoming
353 * connection by the readability of \a accept_fd. This function reads the
354 * command sent by the peer, checks the connecting user's permissions by using
355 * unix socket credentials (if supported by the OS) and calls the corresponding
356 * command handler if permissions are OK.
358 * \return positive on success, negative on errors
360 * \sa para_accept(), recv_cred_buffer()
362 int handle_connect(int accept_fd)
364 int i, argc, ret, clifd = -1;
365 char *cmd = NULL, *p, *buf = para_calloc(MAXLINE), **argv = NULL;
366 struct sockaddr_un unix_addr;
369 ret = para_accept(accept_fd, &unix_addr, sizeof(struct sockaddr_un));
373 ret = recv_cred_buffer(clifd, buf, MAXLINE - 1);
377 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret, buf);
378 ret = check_perms(uid);
381 ret = -E_INVALID_AUDIOD_CMD;
382 cmd = para_strdup(buf);
383 p = strchr(cmd, '\n');
390 for (i = 0; audiod_cmds[i].name; i++) {
392 if (strcmp(audiod_cmds[i].name, cmd))
394 if (audiod_cmds[i].handler) {
395 argc = split_args(buf, &argv, "\n");
396 PARA_INFO_LOG("argv[0]: %s, argc= %d\n", argv[0], argc);
397 ret = audiod_cmds[i].handler(clifd, argc, argv);
400 for (j = 0; p[j]; j++)
403 PARA_INFO_LOG("cmd: %s, options: %s\n", cmd, p);
404 ret = audiod_cmds[i].line_handler(clifd, p);
407 ret = -E_INVALID_AUDIOD_CMD;
412 if (clifd > 0 && ret < 0 && ret != -E_CLIENT_WRITE) {
413 char *tmp = make_message("%s\n", para_strerror(-ret));
414 client_write(clifd, tmp);
421 * send the current audiod status to all connected stat clients
423 void audiod_status_dump(void)
425 struct timeval *t = wstime();
426 char *old, *new, *tmp;
428 old = stat_item_values[SI_PLAY_TIME];
429 new = get_time_string(t);
431 if (!old || strcmp(old, new)) {
433 stat_client_write(new, SI_PLAY_TIME);
434 stat_item_values[SI_PLAY_TIME] = new;
440 old = stat_item_values[SI_AUDIOD_UPTIME];
441 if (!old || strcmp(old, new)) {
443 tmp = make_message("%s: %s\n",
444 status_item_list[SI_AUDIOD_UPTIME], new);
445 stat_client_write(tmp, SI_AUDIOD_UPTIME);
447 stat_item_values[SI_AUDIOD_UPTIME] = new;
451 old = stat_item_values[SI_AUDIOD_STATUS];
452 new = audiod_status_string();
453 if (!old || strcmp(old, new)) {
455 stat_client_write(new, SI_AUDIOD_STATUS);
456 stat_item_values[SI_AUDIOD_STATUS] = new;
460 old = stat_item_values[SI_DECODER_FLAGS];
461 new = decoder_flags();
462 if (!old || strcmp(old, new)) {
464 stat_client_write(new, SI_DECODER_FLAGS);
465 stat_item_values[SI_DECODER_FLAGS] = new;
471 * send empty status list
473 * Send to each connected client the full status item list
476 void dump_empty_status(void)
480 FOR_EACH_STATUS_ITEM(i) {
481 char *tmp = make_message("%s:\n", status_item_list[i]);
482 stat_client_write(tmp, i);
484 free(stat_item_values[i]);
485 stat_item_values[i] = NULL;