]> git.tuebingen.mpg.de Git - paraslash.git/blob - audiod_command.c
server: Reduce scope of struct server_command.
[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 /* Defines one command of para_audiod. */
39 struct audiod_command {
40         const char *name;
41         /* Pointer to the function that handles the command. */
42         int (*handler)(int, int, char **);
43         /* One-line description. */
44         const char *description;
45         /* Summary of the command line options. */
46         const char *usage;
47         /* The long help text. */
48         const char *help;
49 };
50
51 static struct audiod_command audiod_cmds[] = {DEFINE_AUDIOD_CMD_ARRAY};
52
53 /** Iterate over the array of all audiod commands. */
54 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
55
56 /** The maximal number of simultaneous connections. */
57 #define MAX_STAT_CLIENTS 50
58
59 /** Flags used for the stat command of para_audiod. */
60 enum stat_client_flags {
61         /** Enable parser-friendly output. */
62         SCF_PARSER_FRIENDLY = 1,
63 };
64
65 /**
66  * Describes a status client of para_audiod.
67  *
68  * There's one such structure per audiod client that sent the 'stat' command.
69  *
70  * A status client is identified by its file descriptor.  para_audiod
71  * keeps a list of connected status clients.
72  */
73 struct stat_client {
74         /** The stat client's file descriptor. */
75         int fd;
76         /** Bitmask of those status items the client is interested in. */
77         uint64_t item_mask;
78         /** See \ref stat_client flags. s*/
79         unsigned flags;
80         /** Its entry in the list of stat clients. */
81         struct list_head node;
82 };
83
84 static INITIALIZED_LIST_HEAD(client_list);
85 static int num_clients;
86
87 /** The list of all status items used by para_{server,audiod,gui}. */
88 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
89
90 static void dump_stat_client_list(void)
91 {
92         struct stat_client *sc;
93
94         list_for_each_entry(sc, &client_list, node)
95                 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
96 }
97 /**
98  * Add a status client to the list.
99  *
100  * \param fd The file descriptor of the client.
101  * \param mask Bitfield of status items for this client.
102  * \param parser_friendly Enable parser-friendly output mode.
103  *
104  * Only those status items having the bit set in \a mask will be
105  * sent to the client.
106  *
107  * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
108  * the number of connected clients exceeds #MAX_STAT_CLIENTS.
109  */
110 static int stat_client_add(int fd, uint64_t mask, int parser_friendly)
111 {
112         struct stat_client *new_client;
113
114         if (num_clients >= MAX_STAT_CLIENTS) {
115                 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
116                         MAX_STAT_CLIENTS);
117                 return -E_TOO_MANY_CLIENTS;
118         }
119         PARA_INFO_LOG("adding client on fd %d\n", fd);
120         new_client = para_calloc(sizeof(struct stat_client));
121         new_client->fd = fd;
122         new_client->item_mask = mask;
123         if (parser_friendly)
124                 new_client->flags = SCF_PARSER_FRIENDLY;
125         para_list_add(&new_client->node, &client_list);
126         dump_stat_client_list();
127         num_clients++;
128         return 1;
129 }
130
131 static void close_stat_client(struct stat_client *sc)
132 {
133         PARA_INFO_LOG("closing client fd %d\n", sc->fd);
134         close(sc->fd);
135         list_del(&sc->node);
136         free(sc);
137         num_clients--;
138 }
139
140 /**
141  * Empty the status clients list.
142  *
143  * This iterates over the list of connected status clients, closes each client
144  * file descriptor and frees the resources.
145  */
146 void close_stat_clients(void)
147 {
148         struct stat_client *sc, *tmp;
149
150         list_for_each_entry_safe(sc, tmp, &client_list, node)
151                 close_stat_client(sc);
152         assert(num_clients == 0);
153 }
154
155 /**
156  * Write a message to all connected status clients.
157  *
158  * \param item_num The number of the status item of \a msg.
159  *
160  * On write errors, remove the status client from the client list and close its
161  * file descriptor.
162  */
163 void stat_client_write_item(int item_num)
164 {
165         struct stat_client *sc, *tmp;
166         struct para_buffer pb = {.flags = 0};
167         struct para_buffer pfpb = {.flags = PBF_SIZE_PREFIX};
168         const uint64_t one = 1;
169         char *msg = stat_item_values[item_num];
170         struct para_buffer *b;
171
172         list_for_each_entry_safe(sc, tmp, &client_list, node) {
173                 int ret;
174
175                 if (!((one << item_num) & sc->item_mask))
176                         continue;
177                 b = (sc->flags & SCF_PARSER_FRIENDLY)? &pfpb : &pb;
178                 if (!b->buf)
179                         (void)WRITE_STATUS_ITEM(b, item_num, "%s\n",
180                                 msg? msg : "");
181                 ret = write(sc->fd, b->buf, b->offset);
182                 if (ret == b->offset)
183                         continue;
184                 /* write error or short write */
185                 close_stat_client(sc);
186                 dump_stat_client_list();
187         }
188         free(pb.buf);
189         free(pfpb.buf);
190 }
191
192 /**
193  * Check if string is a known status item.
194  *
195  * \param item Buffer containing the text to check.
196  *
197  * \return If \a item is a valid status item, the number of that status item is
198  * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
199  */
200 static int stat_item_valid(const char *item)
201 {
202         int i;
203         if (!item || !*item) {
204                 PARA_ERROR_LOG("%s\n", "no item");
205                 return -E_UNKNOWN_STAT_ITEM;
206         }
207         FOR_EACH_STATUS_ITEM(i)
208                 if (!strcmp(status_item_list[i], item))
209                         return i;
210         PARA_ERROR_LOG("invalid stat item: %s\n", item);
211         return -E_UNKNOWN_STAT_ITEM;
212 }
213
214 static int client_write(int fd, const char *buf)
215 {
216         size_t len;
217
218         if (!buf)
219                 return 0;
220         len = strlen(buf);
221         return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
222 }
223
224 __malloc static char *audiod_status_string(void)
225 {
226         const char *status = (audiod_status == AUDIOD_ON)?
227                 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
228         return para_strdup(status);
229 }
230
231 static int dump_commands(int fd)
232 {
233         char *buf = para_strdup(""), *tmp = NULL;
234         int i;
235         ssize_t ret;
236
237         FOR_EACH_COMMAND(i) {
238                 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
239                         audiod_cmds[i].description);
240                 free(buf);
241                 buf = tmp;
242         }
243         ret = client_write(fd, buf);
244         free(buf);
245         return ret;
246 }
247
248 /*
249  * command handlers don't close their fd on errors (ret < 0) so that
250  * its caller can send an error message. Otherwise (ret >= 0) it's up
251  * to each individual command to close the fd if necessary.
252  */
253
254 static int com_help(int fd, int argc, char **argv)
255 {
256         int i, ret;
257         char *buf;
258         const char *dflt = "No such command. Available commands:\n";
259
260         if (argc < 2) {
261                 ret = dump_commands(fd);
262                 goto out;
263         }
264         FOR_EACH_COMMAND(i) {
265                 if (strcmp(audiod_cmds[i].name, argv[1]))
266                         continue;
267                 buf = make_message(
268                         "NAME\n\t%s -- %s\n"
269                         "SYNOPSIS\n\tpara_audioc %s\n"
270                         "DESCRIPTION\n%s\n",
271                         argv[1],
272                         audiod_cmds[i].description,
273                         audiod_cmds[i].usage,
274                         audiod_cmds[i].help
275                 );
276                 ret = client_write(fd, buf);
277                 free(buf);
278                 goto out;
279         }
280         ret = client_write(fd, dflt);
281         if (ret > 0)
282                 ret = dump_commands(fd);
283 out:
284         if (ret >= 0)
285                 close(fd);
286         return ret;
287 }
288
289 static int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
290 {
291         char *tl = get_task_list(&sched);
292         int ret = 1;
293
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 static int com_term(int fd, __a_unused int argc, __a_unused char **argv)
353 {
354         close(fd);
355         return -E_AUDIOD_TERM;
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 com_version(int fd, int argc, char **argv)
397 {
398         int ret;
399         char *msg;
400
401         if (argc > 1 && strcmp(argv[1], "-v") == 0)
402                 msg = make_message("%s", version_text("audiod"));
403         else
404                 msg = make_message("%s\n", version_single_line("audiod"));
405         ret = client_write(fd, msg);
406         free(msg);
407         if (ret >= 0)
408                 close(fd);
409         return ret;
410 }
411
412 static int check_perms(uid_t uid, uid_t *whitelist)
413 {
414         int i;
415
416         if (!conf.user_allow_given)
417                 return 1;
418         for (i = 0; i < conf.user_allow_given; i++)
419                 if (uid == whitelist[i])
420                         return 1;
421         return -E_UCRED_PERM;
422 }
423
424 /**
425  * Handle arriving connections on the local socket.
426  *
427  * \param accept_fd The fd to accept connections on.
428  * \param rfds If \a accept_fd is not set in \a rfds, do nothing.
429  * \param uid_whitelist Array of UIDs which are allowed to connect.
430  *
431  * This is called in each iteration of the select loop. If there is an incoming
432  * connection on \a accept_fd, this function reads the command sent by the peer,
433  * checks the connecting user's permissions by using unix socket credentials
434  * (if supported by the OS) and calls the corresponding command handler if
435  * permissions are OK.
436  *
437  * \return Positive on success, negative on errors, zero if there was no
438  * connection to accept.
439  *
440  * \sa para_accept(), recv_cred_buffer()
441  * */
442 int handle_connect(int accept_fd, fd_set *rfds, uid_t *uid_whitelist)
443 {
444         int i, argc, ret, clifd;
445         char buf[MAXLINE], **argv = NULL;
446         struct sockaddr_un unix_addr;
447         uid_t uid;
448
449         ret = para_accept(accept_fd, rfds, &unix_addr, sizeof(struct sockaddr_un), &clifd);
450         if (ret <= 0)
451                 return ret;
452         ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
453         if (ret < 0)
454                 goto out;
455         uid = ret;
456         PARA_INFO_LOG("connection from user %i, buf: %s\n",  ret, buf);
457         ret = check_perms(uid, uid_whitelist);
458         if (ret < 0)
459                 goto out;
460         ret = create_argv(buf, "\n", &argv);
461         if (ret <= 0)
462                 goto out;
463         argc = ret;
464         //PARA_INFO_LOG("argv[0]: %s, argc = %d\n", argv[0], argc);
465         FOR_EACH_COMMAND(i) {
466                 if (strcmp(audiod_cmds[i].name, argv[0]))
467                         continue;
468                 ret = audiod_cmds[i].handler(clifd, argc, argv);
469                 goto out;
470         }
471         ret = -E_INVALID_AUDIOD_CMD;
472 out:
473         free_argv(argv);
474         if (clifd > 0 && ret < 0 && ret != -E_CLIENT_WRITE) {
475                 char *tmp = make_message("%s\n", para_strerror(-ret));
476                 client_write(clifd, tmp);
477                 free(tmp);
478                 close(clifd);
479         }
480         return ret;
481 }
482
483 /**
484  * Send the current audiod status to all connected stat clients.
485  *
486  * \param force Whether to write unchanged items.
487  */
488 void audiod_status_dump(bool force)
489 {
490         char *old, *new;
491
492         old = stat_item_values[SI_PLAY_TIME];
493         new = get_time_string();
494         if (new) {
495                 if (force || !old || strcmp(old, new)) {
496                         free(old);
497                         stat_item_values[SI_PLAY_TIME] = new;
498                         stat_client_write_item(SI_PLAY_TIME);
499                 } else
500                         free(new);
501         }
502
503         new = daemon_get_uptime_str(now);
504         old = stat_item_values[SI_AUDIOD_UPTIME];
505         if (force || !old || strcmp(old, new)) {
506                 free(old);
507                 stat_item_values[SI_AUDIOD_UPTIME] = new;
508                 stat_client_write_item(SI_AUDIOD_UPTIME);
509         } else
510                 free(new);
511
512         old = stat_item_values[SI_AUDIOD_STATUS];
513         new = audiod_status_string();
514         if (force || !old || strcmp(old, new)) {
515                 free(old);
516                 stat_item_values[SI_AUDIOD_STATUS] = new;
517                 stat_client_write_item(SI_AUDIOD_STATUS);
518         } else
519                 free(new);
520
521         old = stat_item_values[SI_DECODER_FLAGS];
522         new = audiod_get_decoder_flags();
523         if (force || !old || strcmp(old, new)) {
524                 free(old);
525                 stat_item_values[SI_DECODER_FLAGS] = new;
526                 stat_client_write_item(SI_DECODER_FLAGS);
527         } else
528                 free(new);
529 }
530
531 /**
532  * Flush and send all status items.
533  *
534  * Send to  each connected client the full status item list
535  * with empty values.
536  */
537 void clear_and_dump_items(void)
538 {
539         int i;
540
541         FOR_EACH_STATUS_ITEM(i) {
542                 free(stat_item_values[i]);
543                 stat_item_values[i] = NULL;
544                 stat_client_write_item(i);
545         }
546 }