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