enqueue_chunk(): pass a struct chunk_queue * instead of struct http_client *.
[paraslash.git] / audiod_command.c
1 /*
2  * Copyright (C) 2005-2007 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 "para.h"
10
11 #include "audiod.cmdline.h"
12 #include "list.h"
13 #include "close_on_fork.h"
14 #include "sched.h"
15 #include "filter.h"
16 #include "grab_client.cmdline.h"
17 #include "grab_client.h"
18
19 #include "error.h"
20 #include "audiod.h"
21 #include "net.h"
22 #include "daemon.h"
23 #include "string.h"
24 #include "fd.h"
25 #include "audiod_command_list.h"
26
27 /** iterate over the array of all audiod commands */
28 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
29
30 static int client_write(int fd, const char *buf)
31 {
32         size_t len = strlen(buf);
33         return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
34 }
35
36 static char *get_time_string(struct timeval *newest_stime)
37 {
38         struct timeval diff, adj_stream_start, tmp;
39         int total = 0, use_server_time = 1,
40                 length_seconds = stat_task->length_seconds;
41
42         if (!stat_task->playing) {
43                 if (length_seconds)
44                         return NULL;
45                 return make_message("%s:\n", status_item_list[SI_PLAY_TIME]);
46         }
47         if (audiod_status == AUDIOD_OFF)
48                 goto out;
49         if (stat_task->sa_time_diff_sign > 0)
50                 tv_diff(&stat_task->server_stream_start, &stat_task->sa_time_diff,
51                         &adj_stream_start);
52         else
53                 tv_add(&stat_task->server_stream_start, &stat_task->sa_time_diff,
54                         &adj_stream_start);
55         tmp = adj_stream_start;
56         if (newest_stime && audiod_status == AUDIOD_ON) {
57                 tv_diff(newest_stime, &adj_stream_start, &diff);
58                 if (tv2ms(&diff) < 5000) {
59                         tmp = *newest_stime;
60                         use_server_time = 0;
61                 }
62         }
63         tv_diff(now, &tmp, &diff);
64         total = diff.tv_sec + stat_task->offset_seconds;
65         if (total > length_seconds)
66                 total = length_seconds;
67         if (total < 0)
68                 total = 0;
69 out:
70         return make_message(
71                 "%s:%s%d:%02d [%d:%02d] (%d%%/%d:%02d)\n",
72                 status_item_list[SI_PLAY_TIME],
73                 use_server_time? "~" : "",
74                 total / 60,
75                 total % 60,
76                 (length_seconds - total) / 60,
77                 (length_seconds - total) % 60,
78                 length_seconds? (total * 100 + length_seconds / 2) /
79                         length_seconds : 0,
80                 length_seconds / 60,
81                 length_seconds % 60
82         );
83 }
84
85 __malloc static char *audiod_status_string(void)
86 {
87         const char *status = (audiod_status == AUDIOD_ON)?
88                 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
89         return make_message("%s:%s\n", status_item_list[SI_AUDIOD_STATUS], status);
90 }
91
92 static struct timeval *wstime(void)
93 {
94         int i;
95         struct timeval *max = NULL;
96         FOR_EACH_SLOT(i) {
97                 struct slot_info *s = &slot[i];
98                 if (!s->wng)
99                         continue;
100                 if (max && tv_diff(&s->wstime, max, NULL) <= 0)
101                         continue;
102                 max = &s->wstime;
103         }
104         return max;
105 }
106 __malloc static char *decoder_flags(void)
107 {
108         int i;
109         char flags[MAX_STREAM_SLOTS + 1];
110
111         FOR_EACH_SLOT(i) {
112                 struct slot_info *s = &slot[i];
113                 char flag = '0';
114                 if (s->receiver_node)
115                         flag += 1;
116                 if (s->wng)
117                         flag += 2;
118                 flags[i] = flag;
119         }
120         flags[MAX_STREAM_SLOTS] = '\0';
121         return make_message("%s:%s\n", status_item_list[SI_DECODER_FLAGS],
122                 flags);
123 }
124
125 static int dump_commands(int fd)
126 {
127         char *buf = para_strdup(""), *tmp = NULL;
128         int i;
129         ssize_t ret;
130
131         FOR_EACH_COMMAND(i) {
132                 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
133                         audiod_cmds[i].description);
134                 free(buf);
135                 buf = tmp;
136         }
137         ret = client_write(fd, buf);
138         free(buf);
139         return ret;
140 }
141
142 /*
143  * command handlers don't close their fd on errors (ret < 0) so that
144  * its caller can send an error message. Otherwise (ret >= 0) it's up
145  * to each individual command to close the fd if necessary.
146  */
147
148 int com_help(int fd, int argc, char **argv)
149 {
150         int i, ret;
151         char *buf;
152         const char *dflt = "No such command. Available commands:\n";
153
154         if (argc < 2) {
155                 ret = dump_commands(fd);
156                 goto out;
157         }
158         FOR_EACH_COMMAND(i) {
159                 if (strcmp(audiod_cmds[i].name, argv[1]))
160                         continue;
161                 buf = make_message(
162                         "NAME\n\t%s -- %s\n"
163                         "SYNOPSIS\n\tpara_audioc %s\n"
164                         "DESCRIPTION\n%s\n",
165                         argv[1],
166                         audiod_cmds[i].description,
167                         audiod_cmds[i].usage,
168                         audiod_cmds[i].help
169                 );
170                 ret = client_write(fd, buf);
171                 free(buf);
172                 goto out;
173         }
174         ret = client_write(fd, dflt);
175         if (ret > 0)
176                 ret = dump_commands(fd);
177 out:
178         if (ret >= 0)
179                 close(fd);
180         return ret;
181 }
182
183 int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
184 {
185         char *tl = get_task_list();
186         int ret = 1;
187         if (tl)
188                 ret = client_write(fd, tl);
189         free(tl);
190         if (ret > 0)
191                 close(fd);
192         return ret;
193 }
194
195 int com_kill(int fd, int argc, char **argv)
196 {
197         int i, ret = 1;
198         if (argc < 2)
199                 return -E_AUDIOD_SYNTAX;
200         for (i = 1; i < argc; i++) {
201                 ret = kill_task(argv[i]);
202                 if (ret < 0)
203                         break;
204         }
205         if (ret > 0)
206                 close(fd);
207         return ret;
208 }
209
210 int com_stat(int fd, __a_unused int argc, __a_unused char **argv)
211 {
212         int i, ret;
213         char *buf = NULL;
214         long unsigned mask = ~0LU;
215
216         if (argc > 1) {
217                 mask = 0;
218                 for (i = 1; i < argc; i++) {
219                         ret = stat_item_valid(argv[i]);
220                         if (ret < 0)
221                                 return ret;
222                         mask |= (1 << ret);
223                 }
224         }
225         PARA_INFO_LOG("mask: 0x%lx\n", mask);
226         if (mask & (1 << SI_PLAY_TIME)) {
227                 struct timeval *t = wstime();
228                 char *ts = get_time_string(t);
229                 if (ts) {
230                         ret = client_write(fd, ts);
231                         if (ret < 0)
232                                 goto out;
233                         free(ts);
234                 }
235         }
236         if (mask & (1 << SI_AUDIOD_UPTIME)) {
237                 char *tmp, *us = uptime_str();
238                 tmp = make_message("%s:%s\n",
239                         status_item_list[SI_AUDIOD_UPTIME], us);
240                 free(us);
241                 ret = client_write(fd, tmp);
242                 if (ret < 0)
243                         goto out;
244                 free(tmp);
245         }
246         if (mask & (1 << SI_AUDIOD_STATUS)) {
247                 char *s = audiod_status_string();
248                 ret = client_write(fd, s);
249                 if (ret < 0)
250                         goto out;
251                 free(s);
252         }
253         if (mask & (1 << SI_DECODER_FLAGS)) {
254                 char *df =decoder_flags();
255                 ret = client_write(fd, df);
256                 if (ret < 0)
257                         goto out;
258                 free(df);
259         }
260
261         for (i = 0; i < NUM_STAT_ITEMS; i++) {
262                 char *tmp, *v;
263                 if (!((1 << i) & mask))
264                         continue;
265                 v = stat_task->stat_item_values[i];
266                 tmp = make_message("%s%s%s", buf? buf: "",
267                         v? v : "", v? "\n" : "");
268                 free(buf);
269                 buf = tmp;
270         }
271         ret = client_write(fd, buf);
272 out:
273         if (ret > 0)
274                 ret = stat_client_add(fd, mask);
275         free(buf);
276         return ret;
277 }
278
279 static struct filter_node *find_filter_node(int slot_num, int format, int filternum)
280 {
281         struct filter_node *fn;
282         int i, j;
283
284         FOR_EACH_SLOT(i) {
285                 struct slot_info *s = &slot[i];
286                 if (s->format < 0 || !s->fc)
287                         continue;
288                 if (slot_num >= 0 && slot_num != i)
289                         continue;
290                 if (format >= 0 && s->format != format)
291                         continue;
292                 if (num_filters(i) < filternum)
293                         continue;
294                 /* success */
295                 j = 1;
296                 list_for_each_entry(fn, &s->fc->filters, node)
297                         if (filternum <= 0 || j++ == filternum)
298                                 break;
299                 return fn;
300         }
301         return NULL;
302 }
303
304 int com_grab(int fd, char *cmdline)
305 {
306         struct grab_client *gc;
307         struct filter_node *fn;
308         int i, err;
309         char *msg;
310
311         gc = grab_client_new(fd, cmdline, &err);
312         if (!gc)
313                 goto err_out;
314         fn = find_filter_node(gc->conf->slot_arg, gc->audio_format_num, gc->conf->filter_num_arg);
315         if (fn)
316                 activate_grab_client(gc, fn);
317         return 1;
318 err_out:
319         if (err != -E_GC_HELP_GIVEN && err != -E_GC_VERSION_GIVEN)
320                 return err;
321         if (err == -E_GC_HELP_GIVEN) {
322                 msg = make_message("%s\n\n", grab_client_args_info_usage);
323                 for (i = 0; grab_client_args_info_help[i]; i++) {
324                         char *tmp = make_message("%s%s\n", msg,
325                                 grab_client_args_info_help[i]);
326                         free(msg);
327                         msg = tmp;
328                 }
329         } else
330                 msg = make_message("%s %s\n",
331                         GRAB_CLIENT_CMDLINE_PARSER_PACKAGE,
332                         GRAB_CLIENT_CMDLINE_PARSER_VERSION);
333         err = client_write(fd, msg);
334         free(msg);
335         if (err < 0)
336                 return err;
337         close(fd);
338         return 1;
339 }
340
341 __noreturn int com_term(int fd, __a_unused int argc, __a_unused char **argv)
342 {
343         close(fd);
344         clean_exit(EXIT_SUCCESS, "terminating on user request");
345 }
346
347 int com_on(int fd, __a_unused int argc, __a_unused char **argv)
348 {
349         audiod_status = AUDIOD_ON;
350         close(fd);
351         return 1;
352 }
353
354 int com_off(int fd, __a_unused int argc, __a_unused char **argv)
355 {
356         audiod_status = AUDIOD_OFF;
357         close(fd);
358         return 1;
359 }
360
361 int com_sb(int fd, __a_unused int argc, __a_unused char **argv)
362 {
363         audiod_status = AUDIOD_STANDBY;
364         close(fd);
365         return 1;
366 }
367
368 int com_cycle(int fd, int argc, char **argv)
369 {
370         switch (audiod_status) {
371                 case  AUDIOD_ON:
372                         return com_sb(fd, argc, argv);
373                         break;
374                 case  AUDIOD_OFF:
375                         return com_on(fd, argc, argv);
376                         break;
377                 case  AUDIOD_STANDBY:
378                         return com_off(fd, argc, argv);
379                         break;
380         }
381         close(fd);
382         return 1;
383 }
384
385 static int check_perms(uid_t uid)
386 {
387         int i;
388
389         if (!conf.user_allow_given)
390                 return 1;
391         for (i = 0; i < conf.user_allow_given; i++)
392                 if (uid == conf.user_allow_arg[i])
393                         return 1;
394         return -E_UCRED_PERM;
395 }
396
397 /**
398  * handle arriving connections on the local socket
399  *
400  * \param accept_fd the fd to call accept() on
401  *
402  * This is called whenever para_audiod's main task detects an incoming
403  * connection by the readability of \a accept_fd. This function reads the
404  * command sent by the peer, checks the connecting user's permissions by using
405  * unix socket credentials (if supported by the OS) and calls the corresponding
406  * command handler if permissions are OK.
407  *
408  * \return positive on success, negative on errors
409  *
410  * \sa para_accept(), recv_cred_buffer()
411  * */
412 int handle_connect(int accept_fd)
413 {
414         int i, argc, ret, clifd = -1;
415         char *cmd = NULL, *p, *buf = para_calloc(MAXLINE), **argv = NULL;
416         struct sockaddr_un unix_addr;
417         uid_t uid;
418
419         ret = para_accept(accept_fd, &unix_addr, sizeof(struct sockaddr_un));
420         if (ret < 0)
421                 goto out;
422         clifd = ret;
423         ret = recv_cred_buffer(clifd, buf, MAXLINE - 1);
424         if (ret < 0)
425                 goto out;
426         uid = ret;
427         PARA_INFO_LOG("connection from user %i, buf: %s\n",  ret, buf);
428         ret = check_perms(uid);
429         if (ret < 0)
430                 goto out;
431         ret = -E_INVALID_AUDIOD_CMD;
432         cmd = para_strdup(buf);
433         p = strchr(cmd, '\n');
434         if (!p)
435                 p = "";
436         else {
437                 *p = '\0';
438                 p++;
439         }
440         for (i = 0; audiod_cmds[i].name; i++) {
441                 int j;
442                 if (strcmp(audiod_cmds[i].name, cmd))
443                         continue;
444                 if (audiod_cmds[i].handler) {
445                         argc = split_args(buf, &argv, "\n");
446                         PARA_INFO_LOG("argv[0]: %s, argc= %d\n", argv[0], argc);
447                         ret = audiod_cmds[i].handler(clifd, argc, argv);
448                         goto out;
449                 }
450                 for (j = 0; p[j]; j++)
451                         if (p[j] == '\n')
452                                 p[j] = ' ';
453                 PARA_INFO_LOG("cmd: %s, options: %s\n", cmd, p);
454                 ret = audiod_cmds[i].line_handler(clifd, p);
455                 goto out;
456         }
457         ret = -E_INVALID_AUDIOD_CMD;
458 out:
459         free(cmd);
460         free(buf);
461         free(argv);
462         if (clifd > 0 && ret < 0 && ret != -E_CLIENT_WRITE) {
463                 char *tmp = make_message("%s\n", PARA_STRERROR(-ret));
464                 client_write(clifd, tmp);
465                 free(tmp);
466                 close(clifd);
467         }
468         return ret;
469 }
470 /**
471  * send the current audiod status to all connected stat clients
472  */
473 void audiod_status_dump(void)
474 {
475         struct timeval *t = wstime();
476         char *old, *new, *tmp;
477
478         old = stat_task->stat_item_values[SI_PLAY_TIME];
479         new = get_time_string(t);
480         if (new) {
481                 if (!old || strcmp(old, new)) {
482                         free(old);
483                         stat_client_write(new, SI_PLAY_TIME);
484                         stat_task->stat_item_values[SI_PLAY_TIME] = new;
485                 } else
486                         free(new);
487         }
488
489         new = uptime_str();
490         old = stat_task->stat_item_values[SI_AUDIOD_UPTIME];
491         if (!old || strcmp(old, new)) {
492                 free(old);
493                 tmp = make_message("%s:%s\n",
494                         status_item_list[SI_AUDIOD_UPTIME], new);
495                 stat_client_write(tmp, SI_AUDIOD_UPTIME);
496                 free(tmp);
497                 stat_task->stat_item_values[SI_AUDIOD_UPTIME] = new;
498         } else
499                 free(new);
500
501         old = stat_task->stat_item_values[SI_AUDIOD_STATUS];
502         new = audiod_status_string();
503         if (!old || strcmp(old, new)) {
504                 free(old);
505                 stat_client_write(new, SI_AUDIOD_STATUS);
506                 stat_task->stat_item_values[SI_AUDIOD_STATUS] = new;
507         } else
508                 free(new);
509
510         old = stat_task->stat_item_values[SI_DECODER_FLAGS];
511         new = decoder_flags();
512         if (!old || strcmp(old, new)) {
513                 free(old);
514                 stat_client_write(new, SI_DECODER_FLAGS);
515                 stat_task->stat_item_values[SI_DECODER_FLAGS] = new;
516         } else
517                 free(new);
518 }
519
520 /**
521  * send empty status list
522  *
523  * Send to  each connected client the full status item list
524  * with empty values.
525  */
526 void dump_empty_status(void)
527 {
528         int i;
529
530         FOR_EACH_STAT_ITEM(i) {
531                 char *tmp = make_message("%s:\n", status_item_list[i]);
532                 stat_client_write(tmp, i);
533                 free(tmp);
534                 free(stat_task->stat_item_values[i]);
535                 stat_task->stat_item_values[i] = NULL;
536         }
537 }