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