Merge branch 'master' into next
[paraslash.git] / audiod_command.c
1 /*
2  * Copyright (C) 2005-2009 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 <regex.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12
13 #include "para.h"
14 #include "audiod.cmdline.h"
15 #include "list.h"
16 #include "close_on_fork.h"
17 #include "sched.h"
18 #include "ggo.h"
19 #include "filter.h"
20 #include "grab_client.h"
21 #include "error.h"
22 #include "audiod.h"
23 #include "net.h"
24 #include "daemon.h"
25 #include "string.h"
26 #include "fd.h"
27 #include "audiod_command_list.h"
28
29 extern char *stat_item_values[NUM_STAT_ITEMS];
30
31
32 /** iterate over the array of all audiod commands */
33 #define FOR_EACH_COMMAND(c) for (c = 0; audiod_cmds[c].name; c++)
34
35 static int client_write(int fd, const char *buf)
36 {
37         size_t len;
38
39         if (!buf)
40                 return 0;
41         len = strlen(buf);
42         return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
43 }
44
45 __malloc static char *audiod_status_string(void)
46 {
47         const char *status = (audiod_status == AUDIOD_ON)?
48                 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
49         return para_strdup(status);
50 }
51
52 static int get_play_time_slot_num(void)
53 {
54         int i, oldest = -1;
55
56         FOR_EACH_SLOT(i) {
57                 struct slot_info *s = &slot[i];
58                 if (!s->wng)
59                         continue;
60                 if (oldest >= 0 && tv_diff(&s->wstime, &slot[oldest].wstime,
61                                 NULL) > 0)
62                         continue;
63                 oldest = i;
64         }
65         return oldest;
66 }
67
68 __malloc static char *decoder_flags(void)
69 {
70         int i;
71         char flags[MAX_STREAM_SLOTS + 1];
72
73         FOR_EACH_SLOT(i) {
74                 struct slot_info *s = &slot[i];
75                 char flag = '0';
76                 if (s->receiver_node)
77                         flag += 1;
78                 if (s->fc)
79                         flag += 2;
80                 if (s->wng)
81                         flag += 4;
82                 flags[i] = flag;
83         }
84         flags[MAX_STREAM_SLOTS] = '\0';
85         return para_strdup(flags);
86 }
87
88 static int dump_commands(int fd)
89 {
90         char *buf = para_strdup(""), *tmp = NULL;
91         int i;
92         ssize_t ret;
93
94         FOR_EACH_COMMAND(i) {
95                 tmp = make_message("%s%s\t%s\n", buf, audiod_cmds[i].name,
96                         audiod_cmds[i].description);
97                 free(buf);
98                 buf = tmp;
99         }
100         ret = client_write(fd, buf);
101         free(buf);
102         return ret;
103 }
104
105 /*
106  * command handlers don't close their fd on errors (ret < 0) so that
107  * its caller can send an error message. Otherwise (ret >= 0) it's up
108  * to each individual command to close the fd if necessary.
109  */
110
111 int com_help(int fd, int argc, char **argv)
112 {
113         int i, ret;
114         char *buf;
115         const char *dflt = "No such command. Available commands:\n";
116
117         if (argc < 2) {
118                 ret = dump_commands(fd);
119                 goto out;
120         }
121         FOR_EACH_COMMAND(i) {
122                 if (strcmp(audiod_cmds[i].name, argv[1]))
123                         continue;
124                 buf = make_message(
125                         "NAME\n\t%s -- %s\n"
126                         "SYNOPSIS\n\tpara_audioc %s\n"
127                         "DESCRIPTION\n%s\n",
128                         argv[1],
129                         audiod_cmds[i].description,
130                         audiod_cmds[i].usage,
131                         audiod_cmds[i].help
132                 );
133                 ret = client_write(fd, buf);
134                 free(buf);
135                 goto out;
136         }
137         ret = client_write(fd, dflt);
138         if (ret > 0)
139                 ret = dump_commands(fd);
140 out:
141         if (ret >= 0)
142                 close(fd);
143         return ret;
144 }
145
146 int com_tasks(int fd, __a_unused int argc, __a_unused char **argv)
147 {
148         char *tl = get_task_list();
149         int ret = 1;
150         if (tl)
151                 ret = client_write(fd, tl);
152         free(tl);
153         if (ret > 0)
154                 close(fd);
155         return ret;
156 }
157
158 int com_kill(int fd, int argc, char **argv)
159 {
160         int i, ret = 1;
161         if (argc < 2)
162                 return -E_AUDIOD_SYNTAX;
163         for (i = 1; i < argc; i++) {
164                 ret = kill_task(argv[i]);
165                 if (ret < 0)
166                         break;
167         }
168         if (ret > 0)
169                 close(fd);
170         return ret;
171 }
172
173 int com_stat(int fd, int argc, char **argv)
174 {
175         int i, ret, parser_friendly = 0;
176         uint64_t mask = 0;
177         const uint64_t one = 1;
178         struct para_buffer b = {.flags = 0};
179
180         for (i = 1; i < argc; i++) {
181                 const char *arg = argv[i];
182                 if (arg[0] != '-')
183                         break;
184                 if (!strcmp(arg, "--")) {
185                         i++;
186                         break;
187                 }
188                 if (!strncmp(arg, "-p", 2)) {
189                         parser_friendly = 1;
190                         b.flags = PBF_SIZE_PREFIX;
191                         continue;
192                 }
193         }
194         if (i >= argc)
195                 mask--; /* set all bits */
196         for (; i < argc; i++) {
197                 ret = stat_item_valid(argv[i]);
198                 if (ret < 0)
199                         return ret;
200                 mask |= (one << ret);
201         }
202         PARA_INFO_LOG("mask: 0x%llx\n", (long long unsigned)mask);
203         FOR_EACH_STATUS_ITEM(i) {
204                 char *item = stat_item_values[i];
205                 if (!((one << i) & mask))
206                         continue;
207                 WRITE_STATUS_ITEM(&b, i, "%s\n", item? item : "");
208         }
209         ret = client_write(fd, b.buf);
210         if (ret >= 0)
211                 ret = stat_client_add(fd, mask, parser_friendly);
212         free(b.buf);
213         return ret;
214 }
215
216 int com_grab(int fd, int argc, char **argv)
217 {
218         return grab_client_new(fd, argc, argv);
219 }
220
221 __noreturn int com_term(int fd, __a_unused int argc, __a_unused char **argv)
222 {
223         close(fd);
224         clean_exit(EXIT_SUCCESS, "terminating on user request");
225 }
226
227 int com_on(int fd, __a_unused int argc, __a_unused char **argv)
228 {
229         audiod_status = AUDIOD_ON;
230         close(fd);
231         return 1;
232 }
233
234 int com_off(int fd, __a_unused int argc, __a_unused char **argv)
235 {
236         audiod_status = AUDIOD_OFF;
237         close(fd);
238         return 1;
239 }
240
241 int com_sb(int fd, __a_unused int argc, __a_unused char **argv)
242 {
243         audiod_status = AUDIOD_STANDBY;
244         close(fd);
245         return 1;
246 }
247
248 int com_cycle(int fd, int argc, char **argv)
249 {
250         switch (audiod_status) {
251                 case  AUDIOD_ON:
252                         return com_sb(fd, argc, argv);
253                         break;
254                 case  AUDIOD_OFF:
255                         return com_on(fd, argc, argv);
256                         break;
257                 case  AUDIOD_STANDBY:
258                         return com_off(fd, argc, argv);
259                         break;
260         }
261         close(fd);
262         return 1;
263 }
264
265 static int check_perms(uid_t uid)
266 {
267         int i;
268
269         if (!conf.user_allow_given)
270                 return 1;
271         for (i = 0; i < conf.user_allow_given; i++)
272                 if (uid == conf.user_allow_arg[i])
273                         return 1;
274         return -E_UCRED_PERM;
275 }
276
277 /**
278  * handle arriving connections on the local socket
279  *
280  * \param accept_fd the fd to call accept() on
281  *
282  * This is called whenever para_audiod's main task detects an incoming
283  * connection by the readability of \a accept_fd. This function reads the
284  * command sent by the peer, checks the connecting user's permissions by using
285  * unix socket credentials (if supported by the OS) and calls the corresponding
286  * command handler if permissions are OK.
287  *
288  * \return positive on success, negative on errors
289  *
290  * \sa para_accept(), recv_cred_buffer()
291  * */
292 int handle_connect(int accept_fd)
293 {
294         int i, argc, ret, clifd = -1;
295         char buf[MAXLINE], **argv = NULL;
296         struct sockaddr_un unix_addr;
297         uid_t uid;
298
299         ret = para_accept(accept_fd, &unix_addr, sizeof(struct sockaddr_un));
300         if (ret < 0)
301                 goto out;
302         clifd = ret;
303         ret = recv_cred_buffer(clifd, buf, sizeof(buf) - 1);
304         if (ret < 0)
305                 goto out;
306         uid = ret;
307         PARA_INFO_LOG("connection from user %i, buf: %s\n",  ret, buf);
308         ret = check_perms(uid);
309         if (ret < 0)
310                 goto out;
311         ret = create_argv(buf, "\n", &argv);
312         if (ret < 0)
313                 goto out;
314         argc = ret;
315         //PARA_INFO_LOG("argv[0]: %s, argc = %d\n", argv[0], argc);
316         FOR_EACH_COMMAND(i) {
317                 if (strcmp(audiod_cmds[i].name, argv[0]))
318                         continue;
319                 ret = audiod_cmds[i].handler(clifd, argc, argv);
320                 goto out;
321         }
322         ret = -E_INVALID_AUDIOD_CMD;
323 out:
324         free_argv(argv);
325         if (clifd > 0 && ret < 0 && ret != -E_CLIENT_WRITE) {
326                 char *tmp = make_message("%s\n", para_strerror(-ret));
327                 client_write(clifd, tmp);
328                 free(tmp);
329                 close(clifd);
330         }
331         return ret;
332 }
333
334 /**
335  * Send the current audiod status to all connected stat clients.
336  */
337 void audiod_status_dump(void)
338 {
339         int slot_num = get_play_time_slot_num();
340         char *old, *new;
341
342         old = stat_item_values[SI_PLAY_TIME];
343         new = get_time_string(slot_num);
344         if (new) {
345                 if (!old || strcmp(old, new)) {
346                         free(old);
347                         stat_item_values[SI_PLAY_TIME] = new;
348                         stat_client_write_item(SI_PLAY_TIME);
349                 } else
350                         free(new);
351         }
352
353         new = uptime_str();
354         old = stat_item_values[SI_AUDIOD_UPTIME];
355         if (!old || strcmp(old, new)) {
356                 free(old);
357                 stat_item_values[SI_AUDIOD_UPTIME] = new;
358                 stat_client_write_item(SI_AUDIOD_UPTIME);
359         } else
360                 free(new);
361
362         old = stat_item_values[SI_AUDIOD_STATUS];
363         new = audiod_status_string();
364         if (!old || strcmp(old, new)) {
365                 free(old);
366                 stat_item_values[SI_AUDIOD_STATUS] = new;
367                 stat_client_write_item(SI_AUDIOD_STATUS);
368         } else
369                 free(new);
370
371         old = stat_item_values[SI_DECODER_FLAGS];
372         new = decoder_flags();
373         if (!old || strcmp(old, new)) {
374                 free(old);
375                 stat_item_values[SI_DECODER_FLAGS] = new;
376                 stat_client_write_item(SI_DECODER_FLAGS);
377         } else
378                 free(new);
379 }
380
381 /**
382  * Flush and send all status items.
383  *
384  * Send to  each connected client the full status item list
385  * with empty values.
386  */
387 void clear_and_dump_items(void)
388 {
389         int i;
390
391         FOR_EACH_STATUS_ITEM(i) {
392                 free(stat_item_values[i]);
393                 stat_item_values[i] = NULL;
394                 stat_client_write_item(i);
395         }
396 }