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