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