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