2 * Copyright (C) 2005-2006 Andre Noll <noll@mathematik.tu-darmstadt.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file audiod_command.c commands for para_audiod */
23 #include "audiod.cmdline.h"
25 #include "close_on_fork.h"
28 #include "grab_client.cmdline.h"
29 #include "grab_client.h"
37 #include "audiod_command_list.h"
39 /** iterate over the array of all audiod commands */
40 #define FOR_EACH_COMMAND(c) for (c = 0; cmds[c].name; c++)
42 static int client_write(int fd, const char *buf)
44 size_t len = strlen(buf);
45 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
48 static char *get_time_string(struct timeval *newest_stime)
50 struct timeval diff, adj_stream_start, tmp;
51 int total = 0, use_server_time = 1,
52 length_seconds = stat_task->length_seconds;
54 if (!stat_task->playing) {
57 return make_message("%s:\n", status_item_list[SI_PLAY_TIME]);
59 if (audiod_status == AUDIOD_OFF)
61 if (stat_task->sa_time_diff_sign > 0)
62 tv_diff(&stat_task->server_stream_start, &stat_task->sa_time_diff,
65 tv_add(&stat_task->server_stream_start, &stat_task->sa_time_diff,
67 tmp = adj_stream_start;
68 if (newest_stime && audiod_status == AUDIOD_ON) {
69 tv_diff(newest_stime, &adj_stream_start, &diff);
70 if (tv2ms(&diff) < 5000) {
75 tv_diff(now, &tmp, &diff);
76 total = diff.tv_sec + stat_task->offset_seconds;
77 if (total > length_seconds)
78 total = length_seconds;
83 "%s:%s%d:%02d [%d:%02d] (%d%%/%d:%02d)\n",
84 status_item_list[SI_PLAY_TIME],
85 use_server_time? "~" : "",
88 (length_seconds - total) / 60,
89 (length_seconds - total) % 60,
90 length_seconds? (total * 100 + length_seconds / 2) /
97 __malloc static char *audiod_status_string(void)
99 const char *status = (audiod_status == AUDIOD_ON)?
100 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
101 return make_message("%s:%s\n", status_item_list[SI_AUDIOD_STATUS], status);
104 static struct timeval *wstime(void)
107 struct timeval *max = NULL;
109 struct slot_info *s = &slot[i];
112 if (max && tv_diff(&s->wstime, max, NULL) <= 0)
118 __malloc static char *decoder_flags(void)
121 char flags[MAX_STREAM_SLOTS + 1];
124 struct slot_info *s = &slot[i];
126 if (s->receiver_node)
132 flags[MAX_STREAM_SLOTS] = '\0';
133 return make_message("%s:%s\n", status_item_list[SI_DECODER_FLAGS],
137 static int dump_commands(int fd)
139 char *buf = para_strdup(""), *tmp = NULL;
143 FOR_EACH_COMMAND(i) {
144 tmp = make_message("%s%s\t%s\n", buf, cmds[i].name,
145 cmds[i].description);
149 ret = client_write(fd, buf);
155 * command handlers don't close their fd on errors (ret < 0) so that
156 * its caller can send an error message. Otherwise (ret >= 0) it's up
157 * to each individual command to close the fd if necessary.
160 int com_help(int fd, int argc, char **argv)
164 const char *dflt = "No such command. Available commands:\n";
167 ret = dump_commands(fd);
170 FOR_EACH_COMMAND(i) {
171 if (strcmp(cmds[i].name, argv[1]))
175 "SYNOPSIS\n\tpara_audioc %s\n"
182 ret = client_write(fd, buf);
186 ret = client_write(fd, dflt);
188 ret = dump_commands(fd);
195 int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
197 char *tl = get_task_list();
200 ret = client_write(fd, tl);
207 int com_kill(int fd, int argc, char **argv)
211 return -E_AUDIOD_SYNTAX;
212 for (i = 1; i < argc; i++) {
213 ret = kill_task(argv[i]);
222 int com_stat(int fd, __a_unused int argc, __a_unused char **argv)
226 long unsigned mask = ~0LU;
230 for (i = 1; i < argc; i++) {
231 ret = stat_item_valid(argv[i]);
237 PARA_INFO_LOG("mask: 0x%lx\n", mask);
238 if (mask & (1 << SI_PLAY_TIME)) {
239 struct timeval *t = wstime();
240 char *ts = get_time_string(t);
242 ret = client_write(fd, ts);
248 if (mask & (1 << SI_AUDIOD_UPTIME)) {
249 char *tmp, *us = uptime_str();
250 tmp = make_message("%s:%s\n",
251 status_item_list[SI_AUDIOD_UPTIME], us);
253 ret = client_write(fd, tmp);
258 if (mask & (1 << SI_AUDIOD_STATUS)) {
259 char *s = audiod_status_string();
260 ret = client_write(fd, s);
265 if (mask & (1 << SI_DECODER_FLAGS)) {
266 char *df =decoder_flags();
267 ret = client_write(fd, df);
273 for (i = 0; i < NUM_STAT_ITEMS; i++) {
275 if (!((1 << i) & mask))
277 v = stat_task->stat_item_values[i];
278 tmp = make_message("%s%s%s", buf? buf: "",
279 v? v : "", v? "\n" : "");
283 ret = client_write(fd, buf);
286 ret = stat_client_add(fd, mask);
291 static struct filter_node *find_filter_node(int slot_num, int format, int filternum)
293 struct filter_node *fn;
297 struct slot_info *s = &slot[i];
298 if (s->format < 0 || !s->fc)
300 if (slot_num >= 0 && slot_num != i)
302 if (format >= 0 && s->format != format)
304 if (num_filters(i) < filternum)
308 list_for_each_entry(fn, &s->fc->filters, node)
309 if (filternum <= 0 || j++ == filternum)
316 int com_grab(int fd, char *cmdline)
318 struct grab_client *gc;
319 struct filter_node *fn;
323 gc = grab_client_new(fd, cmdline, &err);
326 fn = find_filter_node(gc->conf->slot_arg, gc->audio_format_num, gc->conf->filter_num_arg);
328 activate_grab_client(gc, fn);
331 if (err != -E_GC_HELP_GIVEN && err != -E_GC_VERSION_GIVEN)
333 if (err == -E_GC_HELP_GIVEN) {
334 msg = make_message("%s\n\n", grab_client_args_info_usage);
335 for (i = 0; grab_client_args_info_help[i]; i++) {
336 char *tmp = make_message("%s%s\n", msg,
337 grab_client_args_info_help[i]);
342 msg = make_message("%s %s\n",
343 GRAB_CLIENT_CMDLINE_PARSER_PACKAGE,
344 GRAB_CLIENT_CMDLINE_PARSER_VERSION);
345 err = client_write(fd, msg);
353 int __noreturn com_term(int fd, __a_unused int argc, __a_unused char **argv)
356 clean_exit(EXIT_SUCCESS, "terminating on user request");
359 int com_on(int fd, __a_unused int argc, __a_unused char **argv)
361 audiod_status = AUDIOD_ON;
366 int com_off(int fd, __a_unused int argc, __a_unused char **argv)
368 audiod_status = AUDIOD_OFF;
373 int com_sb(int fd, __a_unused int argc, __a_unused char **argv)
375 audiod_status = AUDIOD_STANDBY;
380 int com_cycle(int fd, int argc, char **argv)
382 switch (audiod_status) {
384 return com_sb(fd, argc, argv);
387 return com_on(fd, argc, argv);
390 return com_off(fd, argc, argv);
397 static int check_perms(uid_t uid)
401 if (!conf.user_allow_given)
403 for (i = 0; i < conf.user_allow_given; i++)
404 if (uid == conf.user_allow_arg[i])
406 return -E_UCRED_PERM;
409 int handle_connect(int accept_fd)
411 int i, argc, ret, clifd = -1;
412 char *cmd = NULL, *p, *buf = para_calloc(MAXLINE), **argv = NULL;
413 struct sockaddr_un unix_addr;
415 ret = para_accept(accept_fd, &unix_addr, sizeof(struct sockaddr_un));
419 ret = recv_cred_buffer(clifd, buf, MAXLINE - 1);
422 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret, buf);
423 ret = check_perms(ret);
426 ret = -E_INVALID_AUDIOD_CMD;
427 cmd = para_strdup(buf);
428 p = strchr(cmd, '\n');
435 for (i = 0; cmds[i].name; i++) {
437 if (strcmp(cmds[i].name, cmd))
439 if (cmds[i].handler) {
440 argc = split_args(buf, &argv, "\n");
441 PARA_INFO_LOG("argv[0]: %s, argc= %d\n", argv[0], argc);
442 ret = cmds[i].handler(clifd, argc, argv);
445 for (j = 0; p[j]; j++)
448 PARA_INFO_LOG("cmd: %s, options: %s\n", cmd, p);
449 ret = cmds[i].line_handler(clifd, p);
452 ret = -E_INVALID_AUDIOD_CMD;
457 if (clifd > 0 && ret < 0 && ret != -E_CLIENT_WRITE) {
458 char *tmp = make_message("%s\n", PARA_STRERROR(-ret));
459 client_write(clifd, tmp);
466 void audiod_status_dump(void)
468 struct timeval *t = wstime();
469 char *old, *new, *tmp;
471 old = stat_task->stat_item_values[SI_PLAY_TIME];
472 new = get_time_string(t);
474 if (!old || strcmp(old, new)) {
476 stat_client_write(new, SI_PLAY_TIME);
477 stat_task->stat_item_values[SI_PLAY_TIME] = new;
483 old = stat_task->stat_item_values[SI_AUDIOD_UPTIME];
484 if (!old || strcmp(old, new)) {
486 tmp = make_message("%s:%s\n",
487 status_item_list[SI_AUDIOD_UPTIME], new);
488 stat_client_write(tmp, SI_AUDIOD_UPTIME);
490 stat_task->stat_item_values[SI_AUDIOD_UPTIME] = new;
494 old = stat_task->stat_item_values[SI_AUDIOD_STATUS];
495 new = audiod_status_string();
496 if (!old || strcmp(old, new)) {
498 stat_client_write(new, SI_AUDIOD_STATUS);
499 stat_task->stat_item_values[SI_AUDIOD_STATUS] = new;
503 old = stat_task->stat_item_values[SI_DECODER_FLAGS];
504 new = decoder_flags();
505 if (!old || strcmp(old, new)) {
506 stat_client_write(new, SI_DECODER_FLAGS);
507 stat_task->stat_item_values[SI_DECODER_FLAGS] = new;
513 * send empty status list
515 * Send to each connected client the full status item list
518 void dump_empty_status(void)
522 FOR_EACH_STAT_ITEM(i) {
523 char *tmp = make_message("%s:\n", status_item_list[i]);
524 stat_client_write(tmp, i);
526 free(stat_task->stat_item_values[i]);
527 stat_task->stat_item_values[i] = NULL;