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