audiod: Document and fix command handler return values.
[paraslash.git] / audiod_command.c
1 /*
2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
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 <netinet/in.h>
10 #include <sys/socket.h>
11 #include <regex.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
14 #include <sys/un.h>
15 #include <netdb.h>
16
17 #include "para.h"
18 #include "audiod.cmdline.h"
19 #include "audiod.command_list.h"
20 #include "list.h"
21 #include "sched.h"
22 #include "ggo.h"
23 #include "buffer_tree.h"
24 #include "filter.h"
25 #include "grab_client.h"
26 #include "error.h"
27 #include "audiod.h"
28 #include "net.h"
29 #include "daemon.h"
30 #include "string.h"
31 #include "write.h"
32 #include "fd.h"
33 #include "version.h"
34
35 extern struct sched sched;
36 extern char *stat_item_values[NUM_STAT_ITEMS];
37
38 typedef int audiod_command_handler_t(int, int, char **);
39 static audiod_command_handler_t AUDIOD_COMMAND_HANDLERS;
40
41 /* Defines one command of para_audiod. */
42 struct audiod_command {
43 const char *name;
44 /* Pointer to the function that handles the command. */
45 /*
46 * Command handlers must never never close their file descriptor. A
47 * positive return value tells audiod that the status items have
48 * changed. In this case audiod will send an updated version of all
49 * status items to to each connected stat client.
50 */
51 audiod_command_handler_t *handler;
52 /* One-line description. */
53 const char *description;
54 /* Summary of the command line options. */
55 const char *usage;
56 /* The long help text. */
57 const char *help;
58 };
59
60 static struct audiod_command audiod_cmds[] = {DEFINE_AUDIOD_CMD_ARRAY};
61
62 /** Iterate over the array of all audiod commands. */
63 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
64
65 /** The maximal number of simultaneous connections. */
66 #define MAX_STAT_CLIENTS 50
67
68 /** Flags used for the stat command of para_audiod. */
69 enum stat_client_flags {
70 /** Enable parser-friendly output. */
71 SCF_PARSER_FRIENDLY = 1,
72 };
73
74 /**
75 * Describes a status client of para_audiod.
76 *
77 * There's one such structure per audiod client that sent the 'stat' command.
78 *
79 * A status client is identified by its file descriptor. para_audiod
80 * keeps a list of connected status clients.
81 */
82 struct stat_client {
83 /** The stat client's file descriptor. */
84 int fd;
85 /** Bitmask of those status items the client is interested in. */
86 uint64_t item_mask;
87 /** See \ref stat_client flags. s*/
88 unsigned flags;
89 /** Its entry in the list of stat clients. */
90 struct list_head node;
91 };
92
93 static INITIALIZED_LIST_HEAD(client_list);
94 static int num_clients;
95
96 /** The list of all status items used by para_{server,audiod,gui}. */
97 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
98
99 static void dump_stat_client_list(void)
100 {
101 struct stat_client *sc;
102
103 list_for_each_entry(sc, &client_list, node)
104 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
105 }
106 /**
107 * Add a status client to the list.
108 *
109 * \param fd The file descriptor of the client.
110 * \param mask Bitfield of status items for this client.
111 * \param parser_friendly Enable parser-friendly output mode.
112 *
113 * Only those status items having the bit set in \a mask will be
114 * sent to the client.
115 *
116 * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
117 * the number of connected clients exceeds #MAX_STAT_CLIENTS.
118 */
119 static int stat_client_add(int fd, uint64_t mask, int parser_friendly)
120 {
121 struct stat_client *new_client;
122 int ret;
123
124 if (num_clients >= MAX_STAT_CLIENTS) {
125 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
126 MAX_STAT_CLIENTS);
127 return -E_TOO_MANY_CLIENTS;
128 }
129 ret = dup(fd);
130 if (ret < 0)
131 return -ERRNO_TO_PARA_ERROR(errno);
132 new_client = para_calloc(sizeof(*new_client));
133 new_client->fd = ret;
134 PARA_INFO_LOG("adding client on fd %d\n", new_client->fd);
135 new_client->item_mask = mask;
136 if (parser_friendly)
137 new_client->flags = SCF_PARSER_FRIENDLY;
138 para_list_add(&new_client->node, &client_list);
139 dump_stat_client_list();
140 num_clients++;
141 return 1;
142 }
143
144 static void close_stat_client(struct stat_client *sc)
145 {
146 PARA_INFO_LOG("closing client fd %d\n", sc->fd);
147 close(sc->fd);
148 list_del(&sc->node);
149 free(sc);
150 num_clients--;
151 }
152
153 /**
154 * Empty the status clients list.
155 *
156 * This iterates over the list of connected status clients, closes each client
157 * file descriptor and frees the resources.
158 */
159 void close_stat_clients(void)
160 {
161 struct stat_client *sc, *tmp;
162
163 list_for_each_entry_safe(sc, tmp, &client_list, node)
164 close_stat_client(sc);
165 assert(num_clients == 0);
166 }
167
168 /**
169 * Write a message to all connected status clients.
170 *
171 * \param item_num The number of the status item of \a msg.
172 *
173 * On write errors, remove the status client from the client list and close its
174 * file descriptor.
175 */
176 void stat_client_write_item(int item_num)
177 {
178 struct stat_client *sc, *tmp;
179 struct para_buffer pb = {.flags = 0};
180 struct para_buffer pfpb = {.flags = PBF_SIZE_PREFIX};
181 const uint64_t one = 1;
182 char *msg = stat_item_values[item_num];
183 struct para_buffer *b;
184
185 list_for_each_entry_safe(sc, tmp, &client_list, node) {
186 int ret;
187
188 if (!((one << item_num) & sc->item_mask))
189 continue;
190 b = (sc->flags & SCF_PARSER_FRIENDLY)? &pfpb : &pb;
191 if (!b->buf)
192 (void)WRITE_STATUS_ITEM(b, item_num, "%s\n",
193 msg? msg : "");
194 ret = write(sc->fd, b->buf, b->offset);
195 if (ret == b->offset)
196 continue;
197 /* write error or short write */
198 close_stat_client(sc);
199 dump_stat_client_list();
200 }
201 free(pb.buf);
202 free(pfpb.buf);
203 }
204
205 /**
206 * Check if string is a known status item.
207 *
208 * \param item Buffer containing the text to check.
209 *
210 * \return If \a item is a valid status item, the number of that status item is
211 * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
212 */
213 static int stat_item_valid(const char *item)
214 {
215 int i;
216 if (!item || !*item) {
217 PARA_ERROR_LOG("%s\n", "no item");
218 return -E_UNKNOWN_STAT_ITEM;
219 }
220 FOR_EACH_STATUS_ITEM(i)
221 if (!strcmp(status_item_list[i], item))
222 return i;
223 PARA_ERROR_LOG("invalid stat item: %s\n", item);
224 return -E_UNKNOWN_STAT_ITEM;
225 }
226
227 static int client_write(int fd, const char *buf)
228 {
229 size_t len;
230
231 if (!buf)
232 return 0;
233 len = strlen(buf);
234 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
235 }
236
237 __malloc static char *audiod_status_string(void)
238 {
239 const char *status = (audiod_status == AUDIOD_ON)?
240 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
241 return para_strdup(status);
242 }
243
244 static int dump_commands(int fd)
245 {
246 char *buf = para_strdup(""), *tmp = NULL;
247 int i;
248 ssize_t ret;
249
250 FOR_EACH_COMMAND(i) {
251 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
252 audiod_cmds[i].description);
253 free(buf);
254 buf = tmp;
255 }
256 ret = client_write(fd, buf);
257 free(buf);
258 return ret;
259 }
260
261 static int com_help(int fd, int argc, char **argv)
262 {
263 int i, ret;
264 char *buf;
265
266 if (argc < 2) {
267 ret = dump_commands(fd);
268 goto out;
269 }
270 FOR_EACH_COMMAND(i) {
271 if (strcmp(audiod_cmds[i].name, argv[1]))
272 continue;
273 buf = make_message(
274 "NAME\n\t%s -- %s\n"
275 "SYNOPSIS\n\tpara_audioc %s\n"
276 "DESCRIPTION\n%s\n",
277 argv[1],
278 audiod_cmds[i].description,
279 audiod_cmds[i].usage,
280 audiod_cmds[i].help
281 );
282 ret = client_write(fd, buf);
283 free(buf);
284 goto out;
285 }
286 ret = client_write(fd, "No such command. Available commands:\n");
287 if (ret > 0)
288 ret = dump_commands(fd);
289 out:
290 return ret < 0? ret : 0;
291 }
292
293 static int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
294 {
295 char *tl = get_task_list(&sched);
296 int ret = 1;
297
298 if (tl)
299 ret = client_write(fd, tl);
300 free(tl);
301 return ret < 0? ret : 0;
302 }
303
304 static int com_stat(int fd, int argc, char **argv)
305 {
306 int i, ret, parser_friendly = 0;
307 uint64_t mask = 0;
308 const uint64_t one = 1;
309 struct para_buffer b = {.flags = 0};
310
311 ret = mark_fd_nonblocking(fd);
312 if (ret < 0)
313 return ret;
314 for (i = 1; i < argc; i++) {
315 const char *arg = argv[i];
316 if (arg[0] != '-')
317 break;
318 if (!strcmp(arg, "--")) {
319 i++;
320 break;
321 }
322 if (!strncmp(arg, "-p", 2)) {
323 parser_friendly = 1;
324 b.flags = PBF_SIZE_PREFIX;
325 }
326 }
327 if (i >= argc)
328 mask--; /* set all bits */
329 for (; i < argc; i++) {
330 ret = stat_item_valid(argv[i]);
331 if (ret < 0)
332 return ret;
333 mask |= (one << ret);
334 }
335 PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask);
336 FOR_EACH_STATUS_ITEM(i) {
337 char *item = stat_item_values[i];
338 if (!((one << i) & mask))
339 continue;
340 (void)WRITE_STATUS_ITEM(&b, i, "%s\n", item? item : "");
341 }
342 ret = client_write(fd, b.buf);
343 if (ret >= 0)
344 ret = stat_client_add(fd, mask, parser_friendly);
345 free(b.buf);
346 return ret < 0? ret : 0;
347 }
348
349 static int com_grab(int fd, int argc, char **argv)
350 {
351 int ret = grab_client_new(fd, argc, argv, &sched);
352 return ret < 0? ret : 0;
353 }
354
355 static int com_term(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
356 {
357 return -E_AUDIOD_TERM;
358 }
359
360 static int com_on(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
361 {
362 audiod_status = AUDIOD_ON;
363 return 1;
364 }
365
366 static int com_off(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
367 {
368 audiod_status = AUDIOD_OFF;
369 return 1;
370 }
371
372 static int com_sb(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
373 {
374 audiod_status = AUDIOD_STANDBY;
375 return 1;
376 }
377
378 static int com_cycle(__a_unused int fd, int argc, char **argv)
379 {
380 switch (audiod_status) {
381 case AUDIOD_ON:
382 return com_sb(fd, argc, argv);
383 break;
384 case AUDIOD_OFF:
385 return com_on(fd, argc, argv);
386 break;
387 case AUDIOD_STANDBY:
388 return com_off(fd, argc, argv);
389 break;
390 }
391 return 1;
392 }
393
394 static int com_version(int fd, int argc, char **argv)
395 {
396 int ret;
397 char *msg;
398
399 if (argc > 1 && strcmp(argv[1], "-v") == 0)
400 msg = make_message("%s", version_text("audiod"));
401 else
402 msg = make_message("%s\n", version_single_line("audiod"));
403 ret = client_write(fd, msg);
404 free(msg);
405 return ret < 0? ret : 0;
406 }
407
408 static int check_perms(uid_t uid, uid_t *whitelist)
409 {
410 int i;
411
412 if (!conf.user_allow_given)
413 return 1;
414 for (i = 0; i < conf.user_allow_given; i++)
415 if (uid == whitelist[i])
416 return 1;
417 return -E_UCRED_PERM;
418 }
419
420 /**
421 * Handle arriving connections on the local socket.
422 *
423 * \param accept_fd The fd to accept connections on.
424 * \param rfds If \a accept_fd is not set in \a rfds, do nothing.
425 * \param uid_whitelist Array of UIDs which are allowed to connect.
426 *
427 * This is called in each iteration of the select loop. If there is an incoming
428 * connection on \a accept_fd, this function reads the command sent by the peer,
429 * checks the connecting user's permissions by using unix socket credentials
430 * (if supported by the OS) and calls the corresponding command handler if
431 * permissions are OK.
432 *
433 * \return Positive on success, negative on errors, zero if there was no
434 * connection to accept.
435 *
436 * \sa para_accept(), recv_cred_buffer()
437 * */
438 int handle_connect(int accept_fd, fd_set *rfds, uid_t *uid_whitelist)
439 {
440 int i, argc, ret, clifd;
441 char buf[MAXLINE], **argv = NULL;
442 struct sockaddr_un unix_addr;
443 uid_t uid;
444
445 ret = para_accept(accept_fd, rfds, &unix_addr, sizeof(struct sockaddr_un), &clifd);
446 if (ret <= 0)
447 return ret;
448 ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
449 if (ret < 0)
450 goto out;
451 uid = ret;
452 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret, buf);
453 ret = check_perms(uid, uid_whitelist);
454 if (ret < 0)
455 goto out;
456 ret = create_argv(buf, "\n", &argv);
457 if (ret <= 0)
458 goto out;
459 argc = ret;
460 //PARA_INFO_LOG("argv[0]: %s, argc = %d\n", argv[0], argc);
461 FOR_EACH_COMMAND(i) {
462 if (strcmp(audiod_cmds[i].name, argv[0]))
463 continue;
464 ret = audiod_cmds[i].handler(clifd, argc, argv);
465 goto out;
466 }
467 ret = -E_INVALID_AUDIOD_CMD;
468 out:
469 free_argv(argv);
470 if (ret < 0 && ret != -E_CLIENT_WRITE) {
471 char *tmp = make_message("%s\n", para_strerror(-ret));
472 client_write(clifd, tmp);
473 free(tmp);
474 }
475 close(clifd);
476 return ret;
477 }
478
479 /**
480 * Send the current audiod status to all connected stat clients.
481 *
482 * \param force Whether to write unchanged items.
483 */
484 void audiod_status_dump(bool force)
485 {
486 char *old, *new;
487
488 old = stat_item_values[SI_PLAY_TIME];
489 new = get_time_string();
490 if (new) {
491 if (force || !old || strcmp(old, new)) {
492 free(old);
493 stat_item_values[SI_PLAY_TIME] = new;
494 stat_client_write_item(SI_PLAY_TIME);
495 } else
496 free(new);
497 }
498
499 new = daemon_get_uptime_str(now);
500 old = stat_item_values[SI_AUDIOD_UPTIME];
501 if (force || !old || strcmp(old, new)) {
502 free(old);
503 stat_item_values[SI_AUDIOD_UPTIME] = new;
504 stat_client_write_item(SI_AUDIOD_UPTIME);
505 } else
506 free(new);
507
508 old = stat_item_values[SI_AUDIOD_STATUS];
509 new = audiod_status_string();
510 if (force || !old || strcmp(old, new)) {
511 free(old);
512 stat_item_values[SI_AUDIOD_STATUS] = new;
513 stat_client_write_item(SI_AUDIOD_STATUS);
514 } else
515 free(new);
516
517 old = stat_item_values[SI_DECODER_FLAGS];
518 new = audiod_get_decoder_flags();
519 if (force || !old || strcmp(old, new)) {
520 free(old);
521 stat_item_values[SI_DECODER_FLAGS] = new;
522 stat_client_write_item(SI_DECODER_FLAGS);
523 } else
524 free(new);
525 }
526
527 /**
528 * Flush and send all status items.
529 *
530 * Send to each connected client the full status item list
531 * with empty values.
532 */
533 void clear_and_dump_items(void)
534 {
535 int i;
536
537 FOR_EACH_STATUS_ITEM(i) {
538 free(stat_item_values[i]);
539 stat_item_values[i] = NULL;
540 stat_client_write_item(i);
541 }
542 }