d424cf2e76f32ca90058685b68186b5a00be6a2b
[paraslash.git] / audiod_command.c
1 /*
2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file audiod_command.c commands for para_audiod */
8
9 #include <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "audiod.cmdline.h"
14 #include "list.h"
15 #include "close_on_fork.h"
16 #include "sched.h"
17 #include "filter.h"
18 #include "grab_client.cmdline.h"
19 #include "grab_client.h"
20
21 #include "error.h"
22 #include "audiod.h"
23 #include "net.h"
24 #include "daemon.h"
25 #include "string.h"
26 #include "fd.h"
27 #include "audiod_command_list.h"
28
29 /** iterate over the array of all audiod commands */
30 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
31
32 static int client_write(int fd, const char *buf)
33 {
34 size_t len = strlen(buf);
35 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
36 }
37
38 static char *get_time_string(struct timeval *newest_stime)
39 {
40 struct timeval diff, adj_stream_start, tmp;
41 int total = 0, use_server_time = 1,
42 length_seconds = stat_task->length_seconds;
43
44 if (!stat_task->playing) {
45 if (length_seconds)
46 return NULL;
47 return make_message("%s:\n", status_item_list[SI_PLAY_TIME]);
48 }
49 if (audiod_status == AUDIOD_OFF)
50 goto out;
51 if (stat_task->sa_time_diff_sign > 0)
52 tv_diff(&stat_task->server_stream_start, &stat_task->sa_time_diff,
53 &adj_stream_start);
54 else
55 tv_add(&stat_task->server_stream_start, &stat_task->sa_time_diff,
56 &adj_stream_start);
57 tmp = adj_stream_start;
58 if (newest_stime && audiod_status == AUDIOD_ON) {
59 tv_diff(newest_stime, &adj_stream_start, &diff);
60 if (tv2ms(&diff) < 5000) {
61 tmp = *newest_stime;
62 use_server_time = 0;
63 }
64 }
65 tv_diff(now, &tmp, &diff);
66 total = diff.tv_sec + stat_task->offset_seconds;
67 if (total > length_seconds)
68 total = length_seconds;
69 if (total < 0)
70 total = 0;
71 out:
72 return make_message(
73 "%s:%s%d:%02d [%d:%02d] (%d%%/%d:%02d)\n",
74 status_item_list[SI_PLAY_TIME],
75 use_server_time? "~" : "",
76 total / 60,
77 total % 60,
78 (length_seconds - total) / 60,
79 (length_seconds - total) % 60,
80 length_seconds? (total * 100 + length_seconds / 2) /
81 length_seconds : 0,
82 length_seconds / 60,
83 length_seconds % 60
84 );
85 }
86
87 __malloc static char *audiod_status_string(void)
88 {
89 const char *status = (audiod_status == AUDIOD_ON)?
90 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
91 return make_message("%s:%s\n", status_item_list[SI_AUDIOD_STATUS], status);
92 }
93
94 static struct timeval *wstime(void)
95 {
96 int i;
97 struct timeval *max = NULL;
98 FOR_EACH_SLOT(i) {
99 struct slot_info *s = &slot[i];
100 if (!s->wng)
101 continue;
102 if (max && tv_diff(&s->wstime, max, NULL) <= 0)
103 continue;
104 max = &s->wstime;
105 }
106 return max;
107 }
108 __malloc static char *decoder_flags(void)
109 {
110 int i;
111 char flags[MAX_STREAM_SLOTS + 1];
112
113 FOR_EACH_SLOT(i) {
114 struct slot_info *s = &slot[i];
115 char flag = '0';
116 if (s->receiver_node)
117 flag += 1;
118 if (s->wng)
119 flag += 2;
120 flags[i] = flag;
121 }
122 flags[MAX_STREAM_SLOTS] = '\0';
123 return make_message("%s:%s\n", status_item_list[SI_DECODER_FLAGS],
124 flags);
125 }
126
127 static int dump_commands(int fd)
128 {
129 char *buf = para_strdup(""), *tmp = NULL;
130 int i;
131 ssize_t ret;
132
133 FOR_EACH_COMMAND(i) {
134 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
135 audiod_cmds[i].description);
136 free(buf);
137 buf = tmp;
138 }
139 ret = client_write(fd, buf);
140 free(buf);
141 return ret;
142 }
143
144 /*
145 * command handlers don't close their fd on errors (ret < 0) so that
146 * its caller can send an error message. Otherwise (ret >= 0) it's up
147 * to each individual command to close the fd if necessary.
148 */
149
150 int com_help(int fd, int argc, char **argv)
151 {
152 int i, ret;
153 char *buf;
154 const char *dflt = "No such command. Available commands:\n";
155
156 if (argc < 2) {
157 ret = dump_commands(fd);
158 goto out;
159 }
160 FOR_EACH_COMMAND(i) {
161 if (strcmp(audiod_cmds[i].name, argv[1]))
162 continue;
163 buf = make_message(
164 "NAME\n\t%s -- %s\n"
165 "SYNOPSIS\n\tpara_audioc %s\n"
166 "DESCRIPTION\n%s\n",
167 argv[1],
168 audiod_cmds[i].description,
169 audiod_cmds[i].usage,
170 audiod_cmds[i].help
171 );
172 ret = client_write(fd, buf);
173 free(buf);
174 goto out;
175 }
176 ret = client_write(fd, dflt);
177 if (ret > 0)
178 ret = dump_commands(fd);
179 out:
180 if (ret >= 0)
181 close(fd);
182 return ret;
183 }
184
185 int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
186 {
187 char *tl = get_task_list();
188 int ret = 1;
189 if (tl)
190 ret = client_write(fd, tl);
191 free(tl);
192 if (ret > 0)
193 close(fd);
194 return ret;
195 }
196
197 int com_kill(int fd, int argc, char **argv)
198 {
199 int i, ret = 1;
200 if (argc < 2)
201 return -E_AUDIOD_SYNTAX;
202 for (i = 1; i < argc; i++) {
203 ret = kill_task(argv[i]);
204 if (ret < 0)
205 break;
206 }
207 if (ret > 0)
208 close(fd);
209 return ret;
210 }
211
212 int com_stat(int fd, __a_unused int argc, __a_unused char **argv)
213 {
214 int i, ret;
215 char *buf = NULL;
216 long unsigned mask = ~0LU;
217
218 if (argc > 1) {
219 mask = 0;
220 for (i = 1; i < argc; i++) {
221 ret = stat_item_valid(argv[i]);
222 if (ret < 0)
223 return ret;
224 mask |= (1 << ret);
225 }
226 }
227 PARA_INFO_LOG("mask: 0x%lx\n", mask);
228 if (mask & (1 << SI_PLAY_TIME)) {
229 struct timeval *t = wstime();
230 char *ts = get_time_string(t);
231 if (ts) {
232 ret = client_write(fd, ts);
233 if (ret < 0)
234 goto out;
235 free(ts);
236 }
237 }
238 if (mask & (1 << SI_AUDIOD_UPTIME)) {
239 char *tmp, *us = uptime_str();
240 tmp = make_message("%s:%s\n",
241 status_item_list[SI_AUDIOD_UPTIME], us);
242 free(us);
243 ret = client_write(fd, tmp);
244 if (ret < 0)
245 goto out;
246 free(tmp);
247 }
248 if (mask & (1 << SI_AUDIOD_STATUS)) {
249 char *s = audiod_status_string();
250 ret = client_write(fd, s);
251 if (ret < 0)
252 goto out;
253 free(s);
254 }
255 if (mask & (1 << SI_DECODER_FLAGS)) {
256 char *df =decoder_flags();
257 ret = client_write(fd, df);
258 if (ret < 0)
259 goto out;
260 free(df);
261 }
262
263 for (i = 0; i < NUM_STAT_ITEMS; i++) {
264 char *tmp, *v;
265 if (!((1 << i) & mask))
266 continue;
267 v = stat_task->stat_item_values[i];
268 tmp = make_message("%s%s%s", buf? buf: "",
269 v? v : "", v? "\n" : "");
270 free(buf);
271 buf = tmp;
272 }
273 ret = client_write(fd, buf);
274 out:
275 if (ret > 0)
276 ret = stat_client_add(fd, mask);
277 free(buf);
278 return ret;
279 }
280
281 static struct filter_node *find_filter_node(int slot_num, int format, int filternum)
282 {
283 struct filter_node *fn;
284 int i, j;
285
286 FOR_EACH_SLOT(i) {
287 struct slot_info *s = &slot[i];
288 if (s->format < 0 || !s->fc)
289 continue;
290 if (slot_num >= 0 && slot_num != i)
291 continue;
292 if (format >= 0 && s->format != format)
293 continue;
294 if (num_filters(i) < filternum)
295 continue;
296 /* success */
297 j = 1;
298 list_for_each_entry(fn, &s->fc->filters, node)
299 if (filternum <= 0 || j++ == filternum)
300 break;
301 return fn;
302 }
303 return NULL;
304 }
305
306 int com_grab(int fd, char *cmdline)
307 {
308 struct grab_client *gc;
309 struct filter_node *fn;
310 int i, err;
311 char *msg;
312
313 gc = grab_client_new(fd, cmdline, &err);
314 if (!gc)
315 goto err_out;
316 fn = find_filter_node(gc->conf->slot_arg, gc->audio_format_num, gc->conf->filter_num_arg);
317 if (fn)
318 activate_grab_client(gc, fn);
319 return 1;
320 err_out:
321 if (err != -E_GC_HELP_GIVEN && err != -E_GC_VERSION_GIVEN)
322 return err;
323 if (err == -E_GC_HELP_GIVEN) {
324 msg = make_message("%s\n\n", grab_client_args_info_usage);
325 for (i = 0; grab_client_args_info_help[i]; i++) {
326 char *tmp = make_message("%s%s\n", msg,
327 grab_client_args_info_help[i]);
328 free(msg);
329 msg = tmp;
330 }
331 } else
332 msg = make_message("%s %s\n",
333 GRAB_CLIENT_CMDLINE_PARSER_PACKAGE,
334 GRAB_CLIENT_CMDLINE_PARSER_VERSION);
335 err = client_write(fd, msg);
336 free(msg);
337 if (err < 0)
338 return err;
339 close(fd);
340 return 1;
341 }
342
343 __noreturn int com_term(int fd, __a_unused int argc, __a_unused char **argv)
344 {
345 close(fd);
346 clean_exit(EXIT_SUCCESS, "terminating on user request");
347 }
348
349 int com_on(int fd, __a_unused int argc, __a_unused char **argv)
350 {
351 audiod_status = AUDIOD_ON;
352 close(fd);
353 return 1;
354 }
355
356 int com_off(int fd, __a_unused int argc, __a_unused char **argv)
357 {
358 audiod_status = AUDIOD_OFF;
359 close(fd);
360 return 1;
361 }
362
363 int com_sb(int fd, __a_unused int argc, __a_unused char **argv)
364 {
365 audiod_status = AUDIOD_STANDBY;
366 close(fd);
367 return 1;
368 }
369
370 int com_cycle(int fd, int argc, char **argv)
371 {
372 switch (audiod_status) {
373 case AUDIOD_ON:
374 return com_sb(fd, argc, argv);
375 break;
376 case AUDIOD_OFF:
377 return com_on(fd, argc, argv);
378 break;
379 case AUDIOD_STANDBY:
380 return com_off(fd, argc, argv);
381 break;
382 }
383 close(fd);
384 return 1;
385 }
386
387 static int check_perms(uid_t uid)
388 {
389 int i;
390
391 if (!conf.user_allow_given)
392 return 1;
393 for (i = 0; i < conf.user_allow_given; i++)
394 if (uid == conf.user_allow_arg[i])
395 return 1;
396 return -E_UCRED_PERM;
397 }
398
399 /**
400 * handle arriving connections on the local socket
401 *
402 * \param accept_fd the fd to call accept() on
403 *
404 * This is called whenever para_audiod's main task detects an incoming
405 * connection by the readability of \a accept_fd. This function reads the
406 * command sent by the peer, checks the connecting user's permissions by using
407 * unix socket credentials (if supported by the OS) and calls the corresponding
408 * command handler if permissions are OK.
409 *
410 * \return positive on success, negative on errors
411 *
412 * \sa para_accept(), recv_cred_buffer()
413 * */
414 int handle_connect(int accept_fd)
415 {
416 int i, argc, ret, clifd = -1;
417 char *cmd = NULL, *p, *buf = para_calloc(MAXLINE), **argv = NULL;
418 struct sockaddr_un unix_addr;
419 uid_t uid;
420
421 ret = para_accept(accept_fd, &unix_addr, sizeof(struct sockaddr_un));
422 if (ret < 0)
423 goto out;
424 clifd = ret;
425 ret = recv_cred_buffer(clifd, buf, MAXLINE - 1);
426 if (ret < 0)
427 goto out;
428 uid = ret;
429 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret, buf);
430 ret = check_perms(uid);
431 if (ret < 0)
432 goto out;
433 ret = -E_INVALID_AUDIOD_CMD;
434 cmd = para_strdup(buf);
435 p = strchr(cmd, '\n');
436 if (!p)
437 p = "";
438 else {
439 *p = '\0';
440 p++;
441 }
442 for (i = 0; audiod_cmds[i].name; i++) {
443 int j;
444 if (strcmp(audiod_cmds[i].name, cmd))
445 continue;
446 if (audiod_cmds[i].handler) {
447 argc = split_args(buf, &argv, "\n");
448 PARA_INFO_LOG("argv[0]: %s, argc= %d\n", argv[0], argc);
449 ret = audiod_cmds[i].handler(clifd, argc, argv);
450 goto out;
451 }
452 for (j = 0; p[j]; j++)
453 if (p[j] == '\n')
454 p[j] = ' ';
455 PARA_INFO_LOG("cmd: %s, options: %s\n", cmd, p);
456 ret = audiod_cmds[i].line_handler(clifd, p);
457 goto out;
458 }
459 ret = -E_INVALID_AUDIOD_CMD;
460 out:
461 free(cmd);
462 free(buf);
463 free(argv);
464 if (clifd > 0 && ret < 0 && ret != -E_CLIENT_WRITE) {
465 char *tmp = make_message("%s\n", PARA_STRERROR(-ret));
466 client_write(clifd, tmp);
467 free(tmp);
468 close(clifd);
469 }
470 return ret;
471 }
472 /**
473 * send the current audiod status to all connected stat clients
474 */
475 void audiod_status_dump(void)
476 {
477 struct timeval *t = wstime();
478 char *old, *new, *tmp;
479
480 old = stat_task->stat_item_values[SI_PLAY_TIME];
481 new = get_time_string(t);
482 if (new) {
483 if (!old || strcmp(old, new)) {
484 free(old);
485 stat_client_write(new, SI_PLAY_TIME);
486 stat_task->stat_item_values[SI_PLAY_TIME] = new;
487 } else
488 free(new);
489 }
490
491 new = uptime_str();
492 old = stat_task->stat_item_values[SI_AUDIOD_UPTIME];
493 if (!old || strcmp(old, new)) {
494 free(old);
495 tmp = make_message("%s:%s\n",
496 status_item_list[SI_AUDIOD_UPTIME], new);
497 stat_client_write(tmp, SI_AUDIOD_UPTIME);
498 free(tmp);
499 stat_task->stat_item_values[SI_AUDIOD_UPTIME] = new;
500 } else
501 free(new);
502
503 old = stat_task->stat_item_values[SI_AUDIOD_STATUS];
504 new = audiod_status_string();
505 if (!old || strcmp(old, new)) {
506 free(old);
507 stat_client_write(new, SI_AUDIOD_STATUS);
508 stat_task->stat_item_values[SI_AUDIOD_STATUS] = new;
509 } else
510 free(new);
511
512 old = stat_task->stat_item_values[SI_DECODER_FLAGS];
513 new = decoder_flags();
514 if (!old || strcmp(old, new)) {
515 free(old);
516 stat_client_write(new, SI_DECODER_FLAGS);
517 stat_task->stat_item_values[SI_DECODER_FLAGS] = new;
518 } else
519 free(new);
520 }
521
522 /**
523 * send empty status list
524 *
525 * Send to each connected client the full status item list
526 * with empty values.
527 */
528 void dump_empty_status(void)
529 {
530 int i;
531
532 FOR_EACH_STAT_ITEM(i) {
533 char *tmp = make_message("%s:\n", status_item_list[i]);
534 stat_client_write(tmp, i);
535 free(tmp);
536 free(stat_task->stat_item_values[i]);
537 stat_task->stat_item_values[i] = NULL;
538 }
539 }