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