afs: Deplete user list at startup.
[paraslash.git] / audioc.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file audioc.c The client program used to connect to 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 <signal.h>
13 #include <lopsub.h>
14
15 #include "audiod_cmd.lsg.h"
16 #include "audioc.lsg.h"
17
18 #include "para.h"
19 #include "error.h"
20 #include "net.h"
21 #include "string.h"
22 #include "fd.h"
23 #include "version.h"
24
25 /** Array of error strings. */
26 DEFINE_PARA_ERRLIST;
27
28 static char *socket_name;
29 static struct lls_parse_result *lpr;
30
31 #define CMD_PTR (lls_cmd(0, audioc_suite))
32 #define OPT_RESULT(_name) \
33         (lls_opt_result(LSG_AUDIOC_PARA_AUDIOC_OPT_ ## _name, lpr))
34 #define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name)))
35 #define OPT_STRING_VAL(_name) (lls_string_val(0, OPT_RESULT(_name)))
36 #define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name)))
37
38 static int loglevel;
39 INIT_STDERR_LOGGING(loglevel);
40
41 static char *concat_args(unsigned argc, char * const *argv)
42 {
43         int i;
44         char *buf = NULL;
45
46         for (i = 0; i < argc; i++) {
47                 const char *arg = argv? argv[i] : lls_input(i, lpr);
48                 buf = para_strcat(buf, arg);
49                 if (i != argc - 1)
50                         buf = para_strcat(buf, "\n");
51         }
52         return buf;
53 }
54
55 static int connect_audiod(const char *sname, char *args)
56 {
57         int fd = -1, ret;
58
59         ret = connect_local_socket(sname);
60         if (ret < 0)
61                 goto fail;
62         fd = ret;
63         ret = send_cred_buffer(fd, args);
64         if (ret < 0)
65                 goto fail;
66         return fd;
67 fail:
68         PARA_ERROR_LOG("could not connect %s\n", sname);
69         if (fd >= 0)
70                 close(fd);
71         return ret;
72 }
73
74 #ifdef HAVE_READLINE
75 #include "list.h"
76 #include "sched.h"
77 #include "buffer_tree.h"
78 #include "interactive.h"
79
80 static struct sched sched;
81
82 struct audioc_task {
83         int fd;
84         struct btr_node *btrn;
85         struct task *task;
86 };
87
88 static struct i9e_completer audiod_completers[];
89
90 I9E_DUMMY_COMPLETER(cycle);
91 I9E_DUMMY_COMPLETER(off);
92 I9E_DUMMY_COMPLETER(on);
93 I9E_DUMMY_COMPLETER(sb);
94 I9E_DUMMY_COMPLETER(tasks);
95 I9E_DUMMY_COMPLETER(term);
96
97 static void help_completer(struct i9e_completion_info *ci,
98                 struct i9e_completion_result *result)
99 {
100         result->matches = i9e_complete_commands(ci->word, audiod_completers);
101 }
102
103 static void version_completer(struct i9e_completion_info *ci,
104                 struct i9e_completion_result *cr)
105 {
106         char *opts[] = {LSG_AUDIOD_CMD_VERSION_OPTS, NULL};
107
108         if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
109                 i9e_complete_option(opts, ci, cr);
110 }
111
112 static void stat_completer(struct i9e_completion_info *ci,
113                 struct i9e_completion_result *cr)
114 {
115         char *sia[] = {STATUS_ITEMS NULL};
116         char *opts[] = {LSG_AUDIOD_CMD_STAT_OPTS, NULL};
117
118         if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
119                 i9e_complete_option(opts, ci, cr);
120         else
121                 i9e_extract_completions(ci->word, sia, &cr->matches);
122 }
123
124 static void grab_completer(struct i9e_completion_info *ci,
125                 struct i9e_completion_result *cr)
126 {
127         char *opts[] = {LSG_AUDIOD_CMD_GRAB_OPTS, NULL};
128         i9e_complete_option(opts, ci, cr);
129 }
130
131 static struct i9e_completer audiod_completers[] = {
132 #define LSG_AUDIOD_CMD_CMD(_name) {.name = #_name, \
133         .completer = _name ## _completer}
134         LSG_AUDIOD_CMD_SUBCOMMANDS
135 #undef LSG_AUDIOD_CMD_CMD
136         {.name = NULL}
137 };
138
139 static void audioc_pre_select(struct sched *s, void *context)
140 {
141         struct audioc_task *at = context;
142         int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
143
144         if (ret < 0)
145                 sched_min_delay(s);
146         para_fd_set(at->fd, &s->rfds, &s->max_fileno);
147 }
148
149 static int audioc_post_select(struct sched *s, void *context)
150 {
151         char *buf = NULL;
152         struct audioc_task *at = context;
153         int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
154         size_t bufsize;
155
156         if (ret < 0)
157                 goto out;
158         if (!FD_ISSET(at->fd, &s->rfds))
159                 return 0;
160         bufsize = PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE));
161         buf = para_malloc(bufsize);
162         ret = recv_bin_buffer(at->fd, buf, bufsize);
163         PARA_DEBUG_LOG("recv: %d\n", ret);
164         if (ret == 0)
165                 ret = -E_AUDIOC_EOF;
166         if (ret < 0)
167                 goto out;
168         btr_add_output(buf, ret, at->btrn);
169         return 0;
170 out:
171         if (ret < 0) {
172                 free(buf);
173                 btr_remove_node(&at->btrn);
174                 close(at->fd);
175         }
176         return ret;
177 }
178
179 static struct audioc_task audioc_task, *at = &audioc_task;
180
181 static int audioc_i9e_line_handler(char *line)
182 {
183         int argc, ret;
184         char *args, **argv;
185
186         PARA_DEBUG_LOG("line: %s\n", line);
187         ret = create_argv(line, " ", &argv);
188         if (ret < 0)
189                 return ret;
190         argc = ret;
191         args = concat_args(argc, argv);
192         free_argv(argv);
193         if (!args)
194                 return 0;
195         ret = connect_audiod(socket_name, args);
196         free(args);
197         if (ret < 0)
198                 return ret;
199         at->fd = ret;
200         ret = mark_fd_nonblocking(at->fd);
201         if (ret < 0)
202                 goto close;
203         at->btrn = btr_new_node(&(struct btr_node_description)
204                 EMBRACE(.name = "audioc line handler"));
205         at->task = task_register(&(struct task_info) {
206                 .name = "audioc",
207                 .pre_select = audioc_pre_select,
208                 .post_select = audioc_post_select,
209                 .context = at,
210         }, &sched);
211         i9e_attach_to_stdout(at->btrn);
212         return 1;
213 close:
214         close(at->fd);
215         return ret;
216 }
217
218 __noreturn static void interactive_session(void)
219 {
220         int ret;
221         char *history_file;
222         struct sigaction act;
223         struct i9e_client_info ici = {
224                 .fds = {0, 1, 2},
225                 .prompt = "para_audioc> ",
226                 .line_handler = audioc_i9e_line_handler,
227                 .loglevel = loglevel,
228                 .completers = audiod_completers,
229         };
230
231         PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
232         if (OPT_GIVEN(HISTORY_FILE))
233                 history_file = para_strdup(OPT_STRING_VAL(HISTORY_FILE));
234         else {
235                 char *home = para_homedir();
236                 history_file = make_message("%s/.paraslash/audioc.history",
237                         home);
238                 free(home);
239         }
240         ici.history_file = history_file;
241
242         act.sa_handler = i9e_signal_dispatch;
243         sigemptyset(&act.sa_mask);
244         act.sa_flags = 0;
245         sigaction(SIGINT, &act, NULL);
246         sched.select_function = i9e_select;
247
248         sched.default_timeout.tv_sec = 1;
249         ret = i9e_open(&ici, &sched);
250         if (ret < 0)
251                 goto out;
252         para_log = i9e_log;
253         ret = schedule(&sched);
254         sched_shutdown(&sched);
255         i9e_close();
256         para_log = stderr_log;
257 out:
258         free(history_file);
259         free(socket_name);
260         if (ret < 0)
261                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
262         exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
263 }
264
265 __noreturn static void print_completions(void)
266 {
267         int ret = i9e_print_completions(audiod_completers);
268         exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
269 }
270
271 #else /* HAVE_READLINE */
272
273 __noreturn static void interactive_session(void)
274 {
275         PARA_EMERG_LOG("interactive sessions not available\n");
276         exit(EXIT_FAILURE);
277 }
278
279 __noreturn static void print_completions(void)
280 {
281         PARA_EMERG_LOG("command completion not available\n");
282         exit(EXIT_FAILURE);
283 }
284
285 #endif /* HAVE_READLINE */
286
287 static char *configfile_exists(void)
288 {
289         char *config_file;
290         char *home = para_homedir();
291
292         config_file = make_message("%s/.paraslash/audioc.conf", home);
293         free(home);
294         if (file_exists(config_file))
295                 return config_file;
296         free(config_file);
297         return NULL;
298 }
299
300 static void handle_help_flag(void)
301 {
302         char *help;
303
304         if (OPT_GIVEN(DETAILED_HELP))
305                 help = lls_long_help(CMD_PTR);
306         else if (OPT_GIVEN(HELP))
307                 help = lls_short_help(CMD_PTR);
308         else
309                 return;
310         printf("%s\n", help);
311         free(help);
312         exit(EXIT_SUCCESS);
313 }
314
315 /**
316  * The client program to connect to para_audiod.
317  *
318  * \param argc Usual argument count.
319  * \param argv Usual argument vector.
320  *
321  * It connects to the "well-known" local socket to communicate with
322  * para_audiod. Authentication is performed by sending a ucred buffer
323  * containing the user id to the local socket.
324  *
325  * Any data received from the socket is written to stdout.
326  *
327  * \return EXIT_SUCCESS or EXIT_FAILURE.
328  *
329  * \sa \ref send_cred_buffer(), para_audioc(1), para_audiod(1).
330  */
331 int main(int argc, char *argv[])
332 {
333         const struct lls_command *cmd = CMD_PTR;
334         int ret, fd;
335         char *cf = NULL, *buf, *args, *errctx = NULL;
336         size_t bufsize;
337         struct lls_parse_result *lpr1, *lpr2, *lpr3;
338         unsigned num_inputs;
339
340         ret = lls(lls_parse(argc, argv, cmd, &lpr1, &errctx));
341         if (ret < 0)
342                 goto fail;
343         lpr = lpr1;
344         loglevel = OPT_UINT32_VAL(LOGLEVEL);
345         version_handle_flag("audioc", OPT_GIVEN(VERSION));
346         handle_help_flag();
347         cf = configfile_exists();
348         if (cf) {
349                 void *map;
350                 size_t sz;
351                 int cf_argc;
352                 char **cf_argv;
353                 ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL);
354                 if (ret != -E_EMPTY) {
355                         if (ret < 0)
356                                 goto out;
357                         ret = lls(lls_convert_config(map, sz, NULL, &cf_argv,
358                                 &errctx));
359                         para_munmap(map, sz);
360                         if (ret < 0) {
361                                 PARA_ERROR_LOG("syntax error in %s\n", cf);
362                                 goto out;
363                         }
364                         cf_argc = ret;
365                         ret = lls(lls_parse(cf_argc, cf_argv, cmd, &lpr2,
366                                 &errctx));
367                         lls_free_argv(cf_argv);
368                         if (ret < 0) {
369                                 PARA_ERROR_LOG("parse error in %s\n", cf);
370                                 goto out;
371                         }
372                         ret = lls(lls_merge(lpr1, lpr2, cmd, &lpr3, &errctx));
373                         lls_free_parse_result(lpr2, cmd);
374                         if (ret < 0)
375                                 goto out;
376                         lls_free_parse_result(lpr1, cmd);
377                         lpr = lpr3;
378                         loglevel = OPT_UINT32_VAL(LOGLEVEL);
379                 }
380         }
381         if (OPT_GIVEN(COMPLETE))
382                 print_completions();
383         if (OPT_GIVEN(SOCKET))
384                 socket_name = para_strdup(OPT_STRING_VAL(SOCKET));
385         else {
386                 char *hn = para_hostname();
387                 socket_name = make_message("/var/paraslash/audiod_socket.%s",
388                         hn);
389                 free(hn);
390         }
391         num_inputs = lls_num_inputs(lpr);
392         if (num_inputs == 0)
393                 interactive_session();
394
395         args = concat_args(num_inputs, NULL);
396         ret = connect_audiod(socket_name, args);
397         free(socket_name);
398         free(args);
399         if (ret < 0)
400                 goto out;
401         fd = ret;
402         ret = mark_fd_blocking(STDOUT_FILENO);
403         if (ret < 0)
404                 goto out;
405         bufsize = PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE));
406         buf = para_malloc(bufsize);
407         do {
408                 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
409                 if (ret <= 0)
410                         break;
411                 ret = write_all(STDOUT_FILENO, buf, n);
412         } while (ret >= 0);
413         free(buf);
414 out:
415         lls_free_parse_result(lpr, cmd);
416         free(cf);
417 fail:
418         if (errctx)
419                 PARA_ERROR_LOG("%s\n", errctx);
420         free(errctx);
421         if (ret < 0)
422                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
423         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
424 }