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