]> git.tuebingen.mpg.de Git - paraslash.git/blob - audiod_command.c
paraslash 0.7.3
[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 /*
85  * Add a status client to the global client list and increment num_clients.
86  *
87  * The mask parameter specifies which status items are sent to the client.
88  */
89 static int stat_client_add(int fd, uint64_t mask, bool parser_friendly)
90 {
91         struct stat_client *new_client;
92         int ret;
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         ret = dup(fd);
100         if (ret < 0)
101                 return -ERRNO_TO_PARA_ERROR(errno);
102         new_client = zalloc(sizeof(*new_client));
103         new_client->fd = ret;
104         PARA_INFO_LOG("adding client on fd %d\n", new_client->fd);
105         new_client->item_mask = mask;
106         if (parser_friendly)
107                 new_client->flags = SCF_PARSER_FRIENDLY;
108         para_list_add(&new_client->node, &client_list);
109         num_clients++;
110         return 1;
111 }
112
113 static void close_stat_client(struct stat_client *sc)
114 {
115         PARA_INFO_LOG("closing client fd %d\n", sc->fd);
116         close(sc->fd);
117         list_del(&sc->node);
118         free(sc);
119         num_clients--;
120 }
121
122 /**
123  * Empty the status clients list.
124  *
125  * This iterates over the list of connected status clients, closes each client
126  * file descriptor and frees the resources.
127  */
128 void close_stat_clients(void)
129 {
130         struct stat_client *sc, *tmp;
131
132         list_for_each_entry_safe(sc, tmp, &client_list, node)
133                 close_stat_client(sc);
134         assert(num_clients == 0);
135 }
136
137 /**
138  * Write a message to all connected status clients.
139  *
140  * \param item_num The number of the status item of \a msg.
141  *
142  * On write errors, remove the status client from the client list and close its
143  * file descriptor.
144  */
145 void stat_client_write_item(int item_num)
146 {
147         struct stat_client *sc, *tmp;
148         struct para_buffer pb = {.flags = 0};
149         struct para_buffer pfpb = {.flags = PBF_SIZE_PREFIX};
150         const uint64_t one = 1;
151         char *msg = stat_item_values[item_num];
152         struct para_buffer *b;
153
154         list_for_each_entry_safe(sc, tmp, &client_list, node) {
155                 int ret;
156
157                 if (!((one << item_num) & sc->item_mask))
158                         continue;
159                 b = (sc->flags & SCF_PARSER_FRIENDLY)? &pfpb : &pb;
160                 if (!b->buf)
161                         WRITE_STATUS_ITEM(b, item_num, "%s\n", msg? msg : "");
162                 ret = write(sc->fd, b->buf, b->offset);
163                 if (ret == b->offset)
164                         continue;
165                 /* write error or short write */
166                 close_stat_client(sc);
167         }
168         free(pb.buf);
169         free(pfpb.buf);
170 }
171
172 /* Check if the given string is a known status item and return its index. */
173 static int stat_item_valid(const char *item)
174 {
175         int i;
176         if (!item || !*item) {
177                 PARA_ERROR_LOG("%s\n", "no item");
178                 return -E_UNKNOWN_STAT_ITEM;
179         }
180         FOR_EACH_STATUS_ITEM(i)
181                 if (!strcmp(status_item_list[i], item))
182                         return i;
183         PARA_ERROR_LOG("invalid stat item: %s\n", item);
184         return -E_UNKNOWN_STAT_ITEM;
185 }
186
187 static int client_write(int fd, const char *buf)
188 {
189         size_t len;
190
191         if (!buf)
192                 return 0;
193         len = strlen(buf);
194         return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
195 }
196
197 __malloc static char *audiod_status_string(void)
198 {
199         const char *status = (audiod_status == AUDIOD_ON)?
200                 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
201         return para_strdup(status);
202 }
203
204 static int com_help(int fd, struct lls_parse_result *lpr)
205 {
206         char *buf;
207         int ret;
208         const struct lls_opt_result *r =
209                 lls_opt_result(LSG_AUDIOD_CMD_HELP_OPT_LONG, lpr);
210         bool long_help = lls_opt_given(r);
211
212         lsu_com_help(long_help, lpr, audiod_cmd_suite, NULL, &buf, NULL);
213         ret = client_write(fd, buf);
214         free(buf);
215         return ret < 0? ret : 0;
216 }
217 EXPORT_AUDIOD_CMD_HANDLER(help)
218
219 static int com_ll(int fd, struct lls_parse_result *lpr)
220 {
221         unsigned ll;
222         char *errctx;
223         const char *sev[] = {SEVERITIES};
224         const char *arg;
225         int ret = lls(lls_check_arg_count(lpr, 0, 1, &errctx));
226
227         if (ret < 0) {
228                 char *tmp = make_message("%s\n", errctx);
229                 free(errctx);
230                 client_write(fd, tmp);
231                 free(tmp);
232                 return ret;
233         }
234         if (lls_num_inputs(lpr) == 0) {
235                 char *msg;
236                 ll = daemon_get_loglevel();
237                 msg = make_message("%s\n", sev[ll]);
238                 ret = client_write(fd, msg);
239                 free(msg);
240                 return ret;
241         }
242         arg = lls_input(0, lpr);
243         for (ll = 0; ll < NUM_LOGLEVELS; ll++) {
244                 if (!strcmp(arg, sev[ll]))
245                         break;
246         }
247         if (ll >= NUM_LOGLEVELS)
248                 return -ERRNO_TO_PARA_ERROR(EINVAL);
249         PARA_INFO_LOG("new log level: %s\n", sev[ll]);
250         daemon_set_loglevel(ll);
251         return 1;
252 }
253 EXPORT_AUDIOD_CMD_HANDLER(ll)
254
255 static int com_tasks(int fd, __a_unused struct lls_parse_result *lpr)
256 {
257         int ret;
258         char *tl = get_task_list(&sched);
259
260         if (!tl) /* no tasks registered yet */
261                 return 0;
262         ret = client_write(fd, tl);
263         free(tl);
264         return ret;
265 }
266 EXPORT_AUDIOD_CMD_HANDLER(tasks)
267
268 static int com_stat(int fd, struct lls_parse_result *lpr)
269 {
270         int i, ret;
271         bool parser_friendly = false;
272         uint64_t mask = 0;
273         const uint64_t one = 1;
274         struct para_buffer b = {.flags = 0};
275         const struct lls_opt_result *r;
276         unsigned num_inputs;
277
278         ret = mark_fd_nonblocking(fd);
279         if (ret < 0)
280                 return ret;
281         r = lls_opt_result(LSG_AUDIOD_CMD_STAT_OPT_PARSER_FRIENDLY, lpr);
282         if (lls_opt_given(r) > 0) {
283                 parser_friendly = true;
284                 b.flags = PBF_SIZE_PREFIX;
285         }
286         num_inputs = lls_num_inputs(lpr);
287         if (num_inputs == 0)
288                 mask--; /* set all bits */
289         for (i = 0; i < num_inputs; i++) {
290                 ret = stat_item_valid(lls_input(i, lpr));
291                 if (ret < 0)
292                         return ret;
293                 mask |= (one << ret);
294         }
295         PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask);
296         FOR_EACH_STATUS_ITEM(i) {
297                 char *item = stat_item_values[i];
298                 if (!((one << i) & mask))
299                         continue;
300                 WRITE_STATUS_ITEM(&b, i, "%s\n", item? item : "");
301         }
302         ret = client_write(fd, b.buf);
303         if (ret >= 0)
304                 ret = stat_client_add(fd, mask, parser_friendly);
305         free(b.buf);
306         return ret < 0? ret : 0;
307 }
308 EXPORT_AUDIOD_CMD_HANDLER(stat)
309
310 static int com_grab(int fd, struct lls_parse_result *lpr)
311 {
312         int ret = grab_client_new(fd, lpr, &sched);
313         return ret < 0? ret : 0;
314 }
315 EXPORT_AUDIOD_CMD_HANDLER(grab)
316
317 static int com_term(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
318 {
319         return -E_AUDIOD_TERM;
320 }
321 EXPORT_AUDIOD_CMD_HANDLER(term)
322
323 static int com_on(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
324 {
325         audiod_status = AUDIOD_ON;
326         return 1;
327 }
328 EXPORT_AUDIOD_CMD_HANDLER(on)
329
330 static int com_off(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
331 {
332         audiod_status = AUDIOD_OFF;
333         return 1;
334 }
335 EXPORT_AUDIOD_CMD_HANDLER(off)
336
337 static int com_sb(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
338 {
339         audiod_status = AUDIOD_STANDBY;
340         return 1;
341 }
342 EXPORT_AUDIOD_CMD_HANDLER(sb)
343
344 static int com_cycle(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
345 {
346         switch (audiod_status) {
347                 case  AUDIOD_ON: audiod_status = AUDIOD_STANDBY; break;
348                 case  AUDIOD_OFF: audiod_status = AUDIOD_ON; break;
349                 case  AUDIOD_STANDBY: audiod_status = AUDIOD_OFF; break;
350         }
351         return 1;
352 }
353 EXPORT_AUDIOD_CMD_HANDLER(cycle)
354
355 static int com_version(int fd, struct lls_parse_result *lpr)
356 {
357         int ret;
358         char *msg;
359         const struct lls_opt_result *r_v;
360
361         r_v = lls_opt_result(LSG_AUDIOD_CMD_VERSION_OPT_VERBOSE, lpr);
362         if (lls_opt_given(r_v))
363                 msg = make_message("%s", version_text("audiod"));
364         else
365                 msg = make_message("%s\n", version_single_line("audiod"));
366         ret = client_write(fd, msg);
367         free(msg);
368         return ret < 0? ret : 0;
369 }
370 EXPORT_AUDIOD_CMD_HANDLER(version)
371
372 /**
373  * Handle arriving connections on the local socket.
374  *
375  * \param accept_fd The fd to accept connections on.
376  *
377  * This is called in each iteration of the main loop of the scheduler. If there
378  * is an incoming connection, the function reads the command sent by the peer,
379  * checks the connecting user's permissions by using unix socket credentials
380  * (if supported by the OS) and calls the corresponding command handler if
381  * permissions are OK.
382  *
383  * \return Positive on success, negative on errors, zero if there was no
384  * connection to accept.
385  *
386  * \sa \ref para_accept(), \ref recv_cred_buffer().
387  */
388 int dispatch_local_connection(int accept_fd)
389 {
390         int argc, ret, clifd;
391         char buf[MAXLINE], **argv = NULL;
392         struct sockaddr_un unix_addr;
393         uid_t uid;
394         const struct lls_command *cmd;
395         struct lls_parse_result *lpr;
396         char *errctx = NULL;
397         const struct audiod_command_info *aci;
398
399         ret = para_accept(accept_fd, &unix_addr, sizeof(struct sockaddr_un), &clifd);
400         if (ret <= 0)
401                 return ret;
402         ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
403         if (ret < 0)
404                 goto out;
405         uid = ret;
406         PARA_INFO_LOG("connection from UID %d, buf: %s\n", ret, buf);
407         ret = -E_UCRED_PERM;
408         if (!uid_is_whitelisted(uid))
409                 goto out;
410         ret = create_argv(buf, "\n", &argv);
411         if (ret <= 0)
412                 goto out;
413         argc = ret;
414         ret = lls(lls_lookup_subcmd(argv[0], audiod_cmd_suite, &errctx));
415         if (ret < 0)
416                 goto out;
417         cmd = lls_cmd(ret, audiod_cmd_suite);
418         ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
419         if (ret < 0)
420                 goto out;
421         aci = lls_user_data(cmd);
422         ret = aci->handler(clifd, lpr);
423         lls_free_parse_result(lpr, cmd);
424 out:
425         free_argv(argv);
426         if (ret < 0 && ret != -E_CLIENT_WRITE) {
427                 char *tmp;
428                 if (errctx) {
429                         tmp = make_message("%s\n", errctx);
430                         free(errctx);
431                         client_write(clifd, tmp);
432                         free(tmp);
433                 }
434                 tmp = make_message("%s\n", para_strerror(-ret));
435                 client_write(clifd, tmp);
436                 free(tmp);
437         }
438         close(clifd);
439         return ret;
440 }
441
442 /**
443  * Send the current audiod status to all connected stat clients.
444  *
445  * \param force Whether to write unchanged items.
446  */
447 void audiod_status_dump(bool force)
448 {
449         char *old, *new;
450
451         old = stat_item_values[SI_play_time];
452         new = get_time_string();
453         if (new) {
454                 if (force || !old || strcmp(old, new)) {
455                         free(old);
456                         stat_item_values[SI_play_time] = new;
457                         stat_client_write_item(SI_play_time);
458                 } else
459                         free(new);
460         }
461
462         new = daemon_get_uptime_str(now);
463         old = stat_item_values[SI_audiod_uptime];
464         if (force || !old || strcmp(old, new)) {
465                 free(old);
466                 stat_item_values[SI_audiod_uptime] = new;
467                 stat_client_write_item(SI_audiod_uptime);
468         } else
469                 free(new);
470
471         old = stat_item_values[SI_audiod_status];
472         new = audiod_status_string();
473         if (force || !old || strcmp(old, new)) {
474                 free(old);
475                 stat_item_values[SI_audiod_status] = new;
476                 stat_client_write_item(SI_audiod_status);
477         } else
478                 free(new);
479
480         old = stat_item_values[SI_decoder_flags];
481         new = audiod_get_decoder_flags();
482         if (force || !old || strcmp(old, new)) {
483                 free(old);
484                 stat_item_values[SI_decoder_flags] = new;
485                 stat_client_write_item(SI_decoder_flags);
486         } else
487                 free(new);
488 }
489
490 /**
491  * Flush and send all status items.
492  *
493  * Send to  each connected client the full status item list
494  * with empty values.
495  */
496 void clear_and_dump_items(void)
497 {
498         int i;
499
500         FOR_EACH_STATUS_ITEM(i) {
501                 free(stat_item_values[i]);
502                 stat_item_values[i] = NULL;
503                 stat_client_write_item(i);
504         }
505 }