gui: Remove pointless special casing in align_str().
[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. */
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 WRITE_STATUS_ITEM(b, item_num, "%s\n", msg? msg : "");
193 ret = write(sc->fd, b->buf, b->offset);
194 if (ret == b->offset)
195 continue;
196 /* write error or short write */
197 close_stat_client(sc);
198 dump_stat_client_list();
199 }
200 free(pb.buf);
201 free(pfpb.buf);
202 }
203
204 /**
205 * Check if string is a known status item.
206 *
207 * \param item Buffer containing the text to check.
208 *
209 * \return If \a item is a valid status item, the number of that status item is
210 * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
211 */
212 static int stat_item_valid(const char *item)
213 {
214 int i;
215 if (!item || !*item) {
216 PARA_ERROR_LOG("%s\n", "no item");
217 return -E_UNKNOWN_STAT_ITEM;
218 }
219 FOR_EACH_STATUS_ITEM(i)
220 if (!strcmp(status_item_list[i], item))
221 return i;
222 PARA_ERROR_LOG("invalid stat item: %s\n", item);
223 return -E_UNKNOWN_STAT_ITEM;
224 }
225
226 static int client_write(int fd, const char *buf)
227 {
228 size_t len;
229
230 if (!buf)
231 return 0;
232 len = strlen(buf);
233 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
234 }
235
236 __malloc static char *audiod_status_string(void)
237 {
238 const char *status = (audiod_status == AUDIOD_ON)?
239 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
240 return para_strdup(status);
241 }
242
243 static int dump_commands(int fd)
244 {
245 char *buf = para_strdup(""), *tmp = NULL;
246 int i;
247 ssize_t ret;
248
249 FOR_EACH_COMMAND(i) {
250 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
251 audiod_cmds[i].description);
252 free(buf);
253 buf = tmp;
254 }
255 ret = client_write(fd, buf);
256 free(buf);
257 return ret;
258 }
259
260 static int com_help(int fd, int argc, char **argv)
261 {
262 int i, ret;
263 char *buf;
264
265 if (argc < 2) {
266 ret = dump_commands(fd);
267 goto out;
268 }
269 FOR_EACH_COMMAND(i) {
270 if (strcmp(audiod_cmds[i].name, argv[1]))
271 continue;
272 buf = make_message(
273 "NAME\n\t%s -- %s\n"
274 "SYNOPSIS\n\tpara_audioc %s\n"
275 "DESCRIPTION\n%s\n",
276 argv[1],
277 audiod_cmds[i].description,
278 audiod_cmds[i].usage,
279 audiod_cmds[i].help
280 );
281 ret = client_write(fd, buf);
282 free(buf);
283 goto out;
284 }
285 ret = client_write(fd, "No such command. Available commands:\n");
286 if (ret > 0)
287 ret = dump_commands(fd);
288 out:
289 return ret < 0? ret : 0;
290 }
291
292 static int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
293 {
294 char *tl = get_task_list(&sched);
295 int ret = 1;
296
297 if (tl)
298 ret = client_write(fd, tl);
299 free(tl);
300 return ret < 0? ret : 0;
301 }
302
303 static int com_stat(int fd, int argc, char **argv)
304 {
305 int i, ret, parser_friendly = 0;
306 uint64_t mask = 0;
307 const uint64_t one = 1;
308 struct para_buffer b = {.flags = 0};
309
310 ret = mark_fd_nonblocking(fd);
311 if (ret < 0)
312 return ret;
313 for (i = 1; i < argc; i++) {
314 const char *arg = argv[i];
315 if (arg[0] != '-')
316 break;
317 if (!strcmp(arg, "--")) {
318 i++;
319 break;
320 }
321 if (!strncmp(arg, "-p", 2)) {
322 parser_friendly = 1;
323 b.flags = PBF_SIZE_PREFIX;
324 }
325 }
326 if (i >= argc)
327 mask--; /* set all bits */
328 for (; i < argc; i++) {
329 ret = stat_item_valid(argv[i]);
330 if (ret < 0)
331 return ret;
332 mask |= (one << ret);
333 }
334 PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask);
335 FOR_EACH_STATUS_ITEM(i) {
336 char *item = stat_item_values[i];
337 if (!((one << i) & mask))
338 continue;
339 WRITE_STATUS_ITEM(&b, i, "%s\n", item? item : "");
340 }
341 ret = client_write(fd, b.buf);
342 if (ret >= 0)
343 ret = stat_client_add(fd, mask, parser_friendly);
344 free(b.buf);
345 return ret < 0? ret : 0;
346 }
347
348 static int com_grab(int fd, int argc, char **argv)
349 {
350 int ret = grab_client_new(fd, argc, argv, &sched);
351 return ret < 0? ret : 0;
352 }
353
354 static int com_term(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
355 {
356 return -E_AUDIOD_TERM;
357 }
358
359 static int com_on(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
360 {
361 audiod_status = AUDIOD_ON;
362 return 1;
363 }
364
365 static int com_off(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
366 {
367 audiod_status = AUDIOD_OFF;
368 return 1;
369 }
370
371 static int com_sb(__a_unused int fd, __a_unused int argc, __a_unused char **argv)
372 {
373 audiod_status = AUDIOD_STANDBY;
374 return 1;
375 }
376
377 static int com_cycle(__a_unused int fd, int argc, char **argv)
378 {
379 switch (audiod_status) {
380 case AUDIOD_ON:
381 return com_sb(fd, argc, argv);
382 break;
383 case AUDIOD_OFF:
384 return com_on(fd, argc, argv);
385 break;
386 case AUDIOD_STANDBY:
387 return com_off(fd, argc, argv);
388 break;
389 }
390 return 1;
391 }
392
393 static int com_version(int fd, int argc, char **argv)
394 {
395 int ret;
396 char *msg;
397
398 if (argc > 1 && strcmp(argv[1], "-v") == 0)
399 msg = make_message("%s", version_text("audiod"));
400 else
401 msg = make_message("%s\n", version_single_line("audiod"));
402 ret = client_write(fd, msg);
403 free(msg);
404 return ret < 0? ret : 0;
405 }
406
407 static int check_perms(uid_t uid, uid_t *whitelist)
408 {
409 int i;
410
411 if (!conf.user_allow_given)
412 return 1;
413 for (i = 0; i < conf.user_allow_given; i++)
414 if (uid == whitelist[i])
415 return 1;
416 return -E_UCRED_PERM;
417 }
418
419 /**
420 * Handle arriving connections on the local socket.
421 *
422 * \param accept_fd The fd to accept connections on.
423 * \param rfds If \a accept_fd is not set in \a rfds, do nothing.
424 * \param uid_whitelist Array of UIDs which are allowed to connect.
425 *
426 * This is called in each iteration of the select loop. If there is an incoming
427 * connection on \a accept_fd, this function reads the command sent by the peer,
428 * checks the connecting user's permissions by using unix socket credentials
429 * (if supported by the OS) and calls the corresponding command handler if
430 * permissions are OK.
431 *
432 * \return Positive on success, negative on errors, zero if there was no
433 * connection to accept.
434 *
435 * \sa para_accept(), recv_cred_buffer()
436 * */
437 int handle_connect(int accept_fd, fd_set *rfds, uid_t *uid_whitelist)
438 {
439 int i, argc, ret, clifd;
440 char buf[MAXLINE], **argv = NULL;
441 struct sockaddr_un unix_addr;
442 uid_t uid;
443
444 ret = para_accept(accept_fd, rfds, &unix_addr, sizeof(struct sockaddr_un), &clifd);
445 if (ret <= 0)
446 return ret;
447 ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
448 if (ret < 0)
449 goto out;
450 uid = ret;
451 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret, buf);
452 ret = check_perms(uid, uid_whitelist);
453 if (ret < 0)
454 goto out;
455 ret = create_argv(buf, "\n", &argv);
456 if (ret <= 0)
457 goto out;
458 argc = ret;
459 //PARA_INFO_LOG("argv[0]: %s, argc = %d\n", argv[0], argc);
460 FOR_EACH_COMMAND(i) {
461 if (strcmp(audiod_cmds[i].name, argv[0]))
462 continue;
463 ret = audiod_cmds[i].handler(clifd, argc, argv);
464 goto out;
465 }
466 ret = -E_INVALID_AUDIOD_CMD;
467 out:
468 free_argv(argv);
469 if (ret < 0 && ret != -E_CLIENT_WRITE) {
470 char *tmp = make_message("%s\n", para_strerror(-ret));
471 client_write(clifd, tmp);
472 free(tmp);
473 }
474 close(clifd);
475 return ret;
476 }
477
478 /**
479 * Send the current audiod status to all connected stat clients.
480 *
481 * \param force Whether to write unchanged items.
482 */
483 void audiod_status_dump(bool force)
484 {
485 char *old, *new;
486
487 old = stat_item_values[SI_PLAY_TIME];
488 new = get_time_string();
489 if (new) {
490 if (force || !old || strcmp(old, new)) {
491 free(old);
492 stat_item_values[SI_PLAY_TIME] = new;
493 stat_client_write_item(SI_PLAY_TIME);
494 } else
495 free(new);
496 }
497
498 new = daemon_get_uptime_str(now);
499 old = stat_item_values[SI_AUDIOD_UPTIME];
500 if (force || !old || strcmp(old, new)) {
501 free(old);
502 stat_item_values[SI_AUDIOD_UPTIME] = new;
503 stat_client_write_item(SI_AUDIOD_UPTIME);
504 } else
505 free(new);
506
507 old = stat_item_values[SI_AUDIOD_STATUS];
508 new = audiod_status_string();
509 if (force || !old || strcmp(old, new)) {
510 free(old);
511 stat_item_values[SI_AUDIOD_STATUS] = new;
512 stat_client_write_item(SI_AUDIOD_STATUS);
513 } else
514 free(new);
515
516 old = stat_item_values[SI_DECODER_FLAGS];
517 new = audiod_get_decoder_flags();
518 if (force || !old || strcmp(old, new)) {
519 free(old);
520 stat_item_values[SI_DECODER_FLAGS] = new;
521 stat_client_write_item(SI_DECODER_FLAGS);
522 } else
523 free(new);
524 }
525
526 /**
527 * Flush and send all status items.
528 *
529 * Send to each connected client the full status item list
530 * with empty values.
531 */
532 void clear_and_dump_items(void)
533 {
534 int i;
535
536 FOR_EACH_STATUS_ITEM(i) {
537 free(stat_item_values[i]);
538 stat_item_values[i] = NULL;
539 stat_client_write_item(i);
540 }
541 }