]> git.tuebingen.mpg.de Git - paraslash.git/blob - audiod_command.c
openssl: Switch to evp API for sha1 and sha256.
[paraslash.git] / audiod_command.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file audiod_command.c Commands for para_audiod. */
4
5 #include <netinet/in.h>
6 #include <sys/socket.h>
7 #include <regex.h>
8 #include <sys/types.h>
9 #include <arpa/inet.h>
10 #include <sys/un.h>
11 #include <netdb.h>
12 #include <lopsub.h>
13
14 #include "audiod.lsg.h"
15 #include "para.h"
16 #include "lsu.h"
17 #include "audiod_cmd.lsg.h"
18 #include "list.h"
19 #include "sched.h"
20 #include "buffer_tree.h"
21 #include "filter.h"
22 #include "grab_client.h"
23 #include "error.h"
24 #include "audiod.h"
25 #include "net.h"
26 #include "daemon.h"
27 #include "string.h"
28 #include "write.h"
29 #include "fd.h"
30 #include "version.h"
31
32 extern struct sched sched;
33 extern char *stat_item_values[NUM_STAT_ITEMS];
34
35 /** The maximal number of simultaneous connections. */
36 #define MAX_STAT_CLIENTS 50
37
38 /** Pointer to a command handler function. */
39 typedef int (*audiod_cmd_handler_t)(int, struct lls_parse_result *);
40
41 /** The lopsub user_data pointer. Only the command handler at the moment. */
42 struct audiod_command_info {
43         audiod_cmd_handler_t handler; /**< Implementation of the command. */
44 };
45
46 /** Define the user_data pointer as expected by lopsub. */
47 #define EXPORT_AUDIOD_CMD_HANDLER(_cmd) \
48         /** Implementation of _cmd. */ \
49         const struct audiod_command_info lsg_audiod_cmd_com_ ## _cmd ## _user_data = { \
50                 .handler = com_ ## _cmd \
51         };
52
53 /** Flags used for the stat command of para_audiod. */
54 enum stat_client_flags {
55         /** Enable parser-friendly output. */
56         SCF_PARSER_FRIENDLY = 1,
57 };
58
59 /**
60  * Describes a status client of para_audiod.
61  *
62  * There's one such structure per audiod client that sent the 'stat' command.
63  *
64  * A status client is identified by its file descriptor.  para_audiod
65  * keeps a list of connected status clients.
66  */
67 struct stat_client {
68         /** The stat client's file descriptor. */
69         int fd;
70         /** Bitmask of those status items the client is interested in. */
71         uint64_t item_mask;
72         /** See \ref stat_client flags. */
73         unsigned flags;
74         /** Its entry in the list of stat clients. */
75         struct list_head node;
76 };
77
78 static INITIALIZED_LIST_HEAD(client_list);
79 static int num_clients;
80
81 /** The list of all status items used by para_{server,audiod,gui}. */
82 const char *status_item_list[] = {STATUS_ITEMS};
83
84 static void dump_stat_client_list(void)
85 {
86         struct stat_client *sc;
87
88         list_for_each_entry(sc, &client_list, node)
89                 PARA_INFO_LOG("stat client on fd %d\n", sc->fd);
90 }
91 /**
92  * Add a status client to the list.
93  *
94  * \param fd The file descriptor of the client.
95  * \param mask Bitfield of status items for this client.
96  * \param parser_friendly Enable parser-friendly output mode.
97  *
98  * Only those status items having the bit set in \a mask will be
99  * sent to the client.
100  *
101  * \return Positive value on success, or -E_TOO_MANY_CLIENTS if
102  * the number of connected clients exceeds #MAX_STAT_CLIENTS.
103  */
104 static int stat_client_add(int fd, uint64_t mask, int parser_friendly)
105 {
106         struct stat_client *new_client;
107         int ret;
108
109         if (num_clients >= MAX_STAT_CLIENTS) {
110                 PARA_ERROR_LOG("maximal number of stat clients (%d) exceeded\n",
111                         MAX_STAT_CLIENTS);
112                 return -E_TOO_MANY_CLIENTS;
113         }
114         ret = dup(fd);
115         if (ret < 0)
116                 return -ERRNO_TO_PARA_ERROR(errno);
117         new_client = zalloc(sizeof(*new_client));
118         new_client->fd = ret;
119         PARA_INFO_LOG("adding client on fd %d\n", new_client->fd);
120         new_client->item_mask = mask;
121         if (parser_friendly)
122                 new_client->flags = SCF_PARSER_FRIENDLY;
123         para_list_add(&new_client->node, &client_list);
124         dump_stat_client_list();
125         num_clients++;
126         return 1;
127 }
128
129 static void close_stat_client(struct stat_client *sc)
130 {
131         PARA_INFO_LOG("closing client fd %d\n", sc->fd);
132         close(sc->fd);
133         list_del(&sc->node);
134         free(sc);
135         num_clients--;
136 }
137
138 /**
139  * Empty the status clients list.
140  *
141  * This iterates over the list of connected status clients, closes each client
142  * file descriptor and frees the resources.
143  */
144 void close_stat_clients(void)
145 {
146         struct stat_client *sc, *tmp;
147
148         list_for_each_entry_safe(sc, tmp, &client_list, node)
149                 close_stat_client(sc);
150         assert(num_clients == 0);
151 }
152
153 /**
154  * Write a message to all connected status clients.
155  *
156  * \param item_num The number of the status item of \a msg.
157  *
158  * On write errors, remove the status client from the client list and close its
159  * file descriptor.
160  */
161 void stat_client_write_item(int item_num)
162 {
163         struct stat_client *sc, *tmp;
164         struct para_buffer pb = {.flags = 0};
165         struct para_buffer pfpb = {.flags = PBF_SIZE_PREFIX};
166         const uint64_t one = 1;
167         char *msg = stat_item_values[item_num];
168         struct para_buffer *b;
169
170         list_for_each_entry_safe(sc, tmp, &client_list, node) {
171                 int ret;
172
173                 if (!((one << item_num) & sc->item_mask))
174                         continue;
175                 b = (sc->flags & SCF_PARSER_FRIENDLY)? &pfpb : &pb;
176                 if (!b->buf)
177                         WRITE_STATUS_ITEM(b, item_num, "%s\n", msg? msg : "");
178                 ret = write(sc->fd, b->buf, b->offset);
179                 if (ret == b->offset)
180                         continue;
181                 /* write error or short write */
182                 close_stat_client(sc);
183                 dump_stat_client_list();
184         }
185         free(pb.buf);
186         free(pfpb.buf);
187 }
188
189 /**
190  * Check if string is a known status item.
191  *
192  * \param item Buffer containing the text to check.
193  *
194  * \return If \a item is a valid status item, the number of that status item is
195  * returned. Otherwise, this function returns \p -E_UNKNOWN_STAT_ITEM.
196  */
197 static int stat_item_valid(const char *item)
198 {
199         int i;
200         if (!item || !*item) {
201                 PARA_ERROR_LOG("%s\n", "no item");
202                 return -E_UNKNOWN_STAT_ITEM;
203         }
204         FOR_EACH_STATUS_ITEM(i)
205                 if (!strcmp(status_item_list[i], item))
206                         return i;
207         PARA_ERROR_LOG("invalid stat item: %s\n", item);
208         return -E_UNKNOWN_STAT_ITEM;
209 }
210
211 static int client_write(int fd, const char *buf)
212 {
213         size_t len;
214
215         if (!buf)
216                 return 0;
217         len = strlen(buf);
218         return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
219 }
220
221 __malloc static char *audiod_status_string(void)
222 {
223         const char *status = (audiod_status == AUDIOD_ON)?
224                 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
225         return para_strdup(status);
226 }
227
228 static int com_help(int fd, struct lls_parse_result *lpr)
229 {
230         char *buf;
231         int ret;
232         const struct lls_opt_result *r =
233                 lls_opt_result(LSG_AUDIOD_CMD_HELP_OPT_LONG, lpr);
234         bool long_help = lls_opt_given(r);
235
236         lsu_com_help(long_help, lpr, audiod_cmd_suite, NULL, &buf, NULL);
237         ret = client_write(fd, buf);
238         free(buf);
239         return ret < 0? ret : 0;
240 }
241 EXPORT_AUDIOD_CMD_HANDLER(help)
242
243 static int com_ll(int fd, struct lls_parse_result *lpr)
244 {
245         unsigned ll;
246         char *errctx;
247         const char *sev[] = {SEVERITIES};
248         const char *arg;
249         int ret = lls(lls_check_arg_count(lpr, 0, 1, &errctx));
250
251         if (ret < 0) {
252                 char *tmp = make_message("%s\n", errctx);
253                 free(errctx);
254                 client_write(fd, tmp);
255                 free(tmp);
256                 return ret;
257         }
258         if (lls_num_inputs(lpr) == 0) {
259                 char *msg;
260                 ll = daemon_get_loglevel();
261                 msg = make_message("%s\n", sev[ll]);
262                 ret = client_write(fd, msg);
263                 free(msg);
264                 return ret;
265         }
266         arg = lls_input(0, lpr);
267         for (ll = 0; ll < NUM_LOGLEVELS; ll++) {
268                 if (!strcmp(arg, sev[ll]))
269                         break;
270         }
271         if (ll >= NUM_LOGLEVELS)
272                 return -ERRNO_TO_PARA_ERROR(EINVAL);
273         PARA_INFO_LOG("new log level: %s\n", sev[ll]);
274         daemon_set_loglevel(ll);
275         return 1;
276 }
277 EXPORT_AUDIOD_CMD_HANDLER(ll)
278
279 static int com_tasks(int fd, __a_unused struct lls_parse_result *lpr)
280 {
281         int ret;
282         char *tl = get_task_list(&sched);
283
284         if (!tl) /* no tasks registered yet */
285                 return 0;
286         ret = client_write(fd, tl);
287         free(tl);
288         return ret;
289 }
290 EXPORT_AUDIOD_CMD_HANDLER(tasks)
291
292 static int com_stat(int fd, struct lls_parse_result *lpr)
293 {
294         int i, ret, parser_friendly = 0;
295         uint64_t mask = 0;
296         const uint64_t one = 1;
297         struct para_buffer b = {.flags = 0};
298         const struct lls_opt_result *r;
299         unsigned num_inputs;
300
301         ret = mark_fd_nonblocking(fd);
302         if (ret < 0)
303                 return ret;
304         r = lls_opt_result(LSG_AUDIOD_CMD_STAT_OPT_PARSER_FRIENDLY, lpr);
305         if (lls_opt_given(r) > 0) {
306                 parser_friendly = 1;
307                 b.flags = PBF_SIZE_PREFIX;
308         }
309         num_inputs = lls_num_inputs(lpr);
310         if (num_inputs == 0)
311                 mask--; /* set all bits */
312         for (i = 0; i < num_inputs; i++) {
313                 ret = stat_item_valid(lls_input(i, lpr));
314                 if (ret < 0)
315                         return ret;
316                 mask |= (one << ret);
317         }
318         PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask);
319         FOR_EACH_STATUS_ITEM(i) {
320                 char *item = stat_item_values[i];
321                 if (!((one << i) & mask))
322                         continue;
323                 WRITE_STATUS_ITEM(&b, i, "%s\n", item? item : "");
324         }
325         ret = client_write(fd, b.buf);
326         if (ret >= 0)
327                 ret = stat_client_add(fd, mask, parser_friendly);
328         free(b.buf);
329         return ret < 0? ret : 0;
330 }
331 EXPORT_AUDIOD_CMD_HANDLER(stat)
332
333 static int com_grab(int fd, struct lls_parse_result *lpr)
334 {
335         int ret = grab_client_new(fd, lpr, &sched);
336         return ret < 0? ret : 0;
337 }
338 EXPORT_AUDIOD_CMD_HANDLER(grab)
339
340 static int com_term(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
341 {
342         return -E_AUDIOD_TERM;
343 }
344 EXPORT_AUDIOD_CMD_HANDLER(term)
345
346 static int com_on(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
347 {
348         audiod_status = AUDIOD_ON;
349         return 1;
350 }
351 EXPORT_AUDIOD_CMD_HANDLER(on)
352
353 static int com_off(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
354 {
355         audiod_status = AUDIOD_OFF;
356         return 1;
357 }
358 EXPORT_AUDIOD_CMD_HANDLER(off)
359
360 static int com_sb(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
361 {
362         audiod_status = AUDIOD_STANDBY;
363         return 1;
364 }
365 EXPORT_AUDIOD_CMD_HANDLER(sb)
366
367 static int com_cycle(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
368 {
369         switch (audiod_status) {
370                 case  AUDIOD_ON: audiod_status = AUDIOD_STANDBY; break;
371                 case  AUDIOD_OFF: audiod_status = AUDIOD_ON; break;
372                 case  AUDIOD_STANDBY: audiod_status = AUDIOD_OFF; break;
373         }
374         return 1;
375 }
376 EXPORT_AUDIOD_CMD_HANDLER(cycle)
377
378 static int com_version(int fd, struct lls_parse_result *lpr)
379 {
380         int ret;
381         char *msg;
382         const struct lls_opt_result *r_v;
383
384         r_v = lls_opt_result(LSG_AUDIOD_CMD_VERSION_OPT_VERBOSE, lpr);
385         if (lls_opt_given(r_v))
386                 msg = make_message("%s", version_text("audiod"));
387         else
388                 msg = make_message("%s\n", version_single_line("audiod"));
389         ret = client_write(fd, msg);
390         free(msg);
391         return ret < 0? ret : 0;
392 }
393 EXPORT_AUDIOD_CMD_HANDLER(version)
394
395 /**
396  * Handle arriving connections on the local socket.
397  *
398  * \param accept_fd The fd to accept connections on.
399  *
400  * This is called in each iteration of the main loop of the scheduler. If there
401  * is an incoming connection, the function reads the command sent by the peer,
402  * checks the connecting user's permissions by using unix socket credentials
403  * (if supported by the OS) and calls the corresponding command handler if
404  * permissions are OK.
405  *
406  * \return Positive on success, negative on errors, zero if there was no
407  * connection to accept.
408  *
409  * \sa \ref para_accept(), \ref recv_cred_buffer().
410  */
411 int dispatch_local_connection(int accept_fd)
412 {
413         int argc, ret, clifd;
414         char buf[MAXLINE], **argv = NULL;
415         struct sockaddr_un unix_addr;
416         uid_t uid;
417         const struct lls_command *cmd;
418         struct lls_parse_result *lpr;
419         char *errctx = NULL;
420         const struct audiod_command_info *aci;
421
422         ret = para_accept(accept_fd, &unix_addr, sizeof(struct sockaddr_un), &clifd);
423         if (ret <= 0)
424                 return ret;
425         ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
426         if (ret < 0)
427                 goto out;
428         uid = ret;
429         PARA_INFO_LOG("connection from UID %d, buf: %s\n", ret, buf);
430         ret = -E_UCRED_PERM;
431         if (!uid_is_whitelisted(uid))
432                 goto out;
433         ret = create_argv(buf, "\n", &argv);
434         if (ret <= 0)
435                 goto out;
436         argc = ret;
437         ret = lls(lls_lookup_subcmd(argv[0], audiod_cmd_suite, &errctx));
438         if (ret < 0)
439                 goto out;
440         cmd = lls_cmd(ret, audiod_cmd_suite);
441         ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
442         if (ret < 0)
443                 goto out;
444         aci = lls_user_data(cmd);
445         ret = aci->handler(clifd, lpr);
446         lls_free_parse_result(lpr, cmd);
447 out:
448         free_argv(argv);
449         if (ret < 0 && ret != -E_CLIENT_WRITE) {
450                 char *tmp;
451                 if (errctx) {
452                         tmp = make_message("%s\n", errctx);
453                         free(errctx);
454                         client_write(clifd, tmp);
455                         free(tmp);
456                 }
457                 tmp = make_message("%s\n", para_strerror(-ret));
458                 client_write(clifd, tmp);
459                 free(tmp);
460         }
461         close(clifd);
462         return ret;
463 }
464
465 /**
466  * Send the current audiod status to all connected stat clients.
467  *
468  * \param force Whether to write unchanged items.
469  */
470 void audiod_status_dump(bool force)
471 {
472         char *old, *new;
473
474         old = stat_item_values[SI_play_time];
475         new = get_time_string();
476         if (new) {
477                 if (force || !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 = daemon_get_uptime_str(now);
486         old = stat_item_values[SI_audiod_uptime];
487         if (force || !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 (force || !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 = audiod_get_decoder_flags();
505         if (force || !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 }