net: Clarify code flow of makesock_addrinfo().
[paraslash.git] / audioc.c
1 /*
2  * Copyright (C) 2005-2013 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file audioc.c The client program used to connect to para_audiod. */
8
9 #include <netinet/in.h>
10 #include <sys/socket.h>
11 #include <regex.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
14 #include <sys/un.h>
15 #include <netdb.h>
16 #include <stdbool.h>
17 #include <signal.h>
18
19 #include "audioc.cmdline.h"
20 #include "para.h"
21 #include "error.h"
22 #include "net.h"
23 #include "string.h"
24 #include "fd.h"
25 #include "ggo.h"
26 #include "version.h"
27
28 INIT_AUDIOC_ERRLISTS;
29
30 /** The gengetopt structure containing command line args. */
31 static struct audioc_args_info conf;
32 static char *socket_name;
33
34
35 static int loglevel;
36 INIT_STDERR_LOGGING(loglevel);
37
38 static char *concat_args(unsigned argc, char * const *argv)
39 {
40         int i;
41         char *buf = NULL;
42
43         for (i = 0; i < argc; i++) {
44                 buf = para_strcat(buf, argv[i]);
45                 if (i != argc - 1)
46                         buf = para_strcat(buf, "\n");
47         }
48         return buf;
49 }
50
51 static int connect_audiod(const char *sname, char *args)
52 {
53         int fd = -1, ret;
54
55         ret = connect_local_socket(sname);
56         if (ret < 0)
57                 goto fail;
58         fd = ret;
59         ret = send_cred_buffer(fd, args);
60         if (ret < 0)
61                 goto fail;
62         return fd;
63 fail:
64         PARA_ERROR_LOG("could not connect %s\n", sname);
65         if (fd >= 0)
66                 close(fd);
67         return ret;
68 }
69
70 #ifdef HAVE_READLINE
71 #include "list.h"
72 #include "sched.h"
73 #include "buffer_tree.h"
74 #include "interactive.h"
75 #include "audiod_completion.h"
76
77 static struct sched sched;
78
79 struct audioc_task {
80         int fd;
81         struct btr_node *btrn;
82         struct task task;
83 };
84
85 static struct i9e_completer audiod_completers[];
86
87 I9E_DUMMY_COMPLETER(cycle);
88 I9E_DUMMY_COMPLETER(off);
89 I9E_DUMMY_COMPLETER(on);
90 I9E_DUMMY_COMPLETER(sb);
91 I9E_DUMMY_COMPLETER(tasks);
92 I9E_DUMMY_COMPLETER(term);
93
94 static void help_completer(struct i9e_completion_info *ci,
95                 struct i9e_completion_result *result)
96 {
97         result->matches = i9e_complete_commands(ci->word, audiod_completers);
98 }
99
100 static void stat_completer(struct i9e_completion_info *ci,
101                 struct i9e_completion_result *cr)
102 {
103         char *sia[] = {STATUS_ITEM_ARRAY NULL};
104         char *opts[] = {"-p", NULL};
105
106         if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
107                 i9e_complete_option(opts, ci, cr);
108         else
109                 i9e_extract_completions(ci->word, sia, &cr->matches);
110 }
111
112 static void grab_completer(struct i9e_completion_info *ci,
113                 struct i9e_completion_result *cr)
114 {
115         char *opts[] = {"-ms", "-ms", "-ma", "-p=", "-n=", "-o", NULL};
116         i9e_complete_option(opts, ci, cr);
117 }
118
119 static struct i9e_completer audiod_completers[] = {
120         AUDIOD_COMPLETERS
121         {.name = NULL}
122 };
123
124 static void audioc_pre_select(struct sched *s, struct task *t)
125 {
126         struct audioc_task *at = container_of(t, struct audioc_task, task);
127         int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
128
129         if (ret < 0)
130                 sched_min_delay(s);
131         para_fd_set(at->fd, &s->rfds, &s->max_fileno);
132 }
133
134 static int audioc_post_select(struct sched *s, struct task *t)
135 {
136         char *buf = NULL;
137         struct audioc_task *at = container_of(t, struct audioc_task, task);
138         int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
139
140         if (ret < 0)
141                 goto out;
142         if (!FD_ISSET(at->fd, &s->rfds))
143                 return 0;
144         buf = para_malloc(conf.bufsize_arg);
145         ret = recv_bin_buffer(at->fd, buf, conf.bufsize_arg);
146         PARA_DEBUG_LOG("recv: %d\n", ret);
147         if (ret == 0)
148                 ret = -E_AUDIOC_EOF;
149         if (ret < 0)
150                 goto out;
151         btr_add_output(buf, ret, at->btrn);
152         return 0;
153 out:
154         if (ret < 0) {
155                 free(buf);
156                 btr_remove_node(&at->btrn);
157                 close(at->fd);
158         }
159         return ret;
160 }
161
162 static struct audioc_task audioc_task = {
163         .task = {
164                 .pre_select = audioc_pre_select,
165                 .post_select = audioc_post_select,
166                 .status = "audioc task"
167         },
168 }, *at = &audioc_task;
169
170 static int audioc_i9e_line_handler(char *line)
171 {
172         char *args = NULL;
173         int ret;
174
175         PARA_DEBUG_LOG("line: %s\n", line);
176         ret = create_argv(line, " ", &conf.inputs);
177         if (ret < 0)
178                 return ret;
179         conf.inputs_num = ret;
180         args = concat_args(conf.inputs_num, conf.inputs);
181         free_argv(conf.inputs);
182         if (!args)
183                 return 0;
184         conf.inputs_num = 0; /* required for audioc_cmdline_parser_free() */
185         ret = connect_audiod(socket_name, args);
186         if (ret < 0)
187                 goto out;
188         at->fd = ret;
189         ret = mark_fd_nonblocking(at->fd);
190         if (ret < 0)
191                 goto close;
192         free(args);
193         args = NULL;
194         at->btrn = btr_new_node(&(struct btr_node_description)
195                 EMBRACE(.name = "audioc line handler"));
196         at->task.error = 0;
197         register_task(&sched, &at->task);
198         i9e_attach_to_stdout(at->btrn);
199         return 1;
200 close:
201         close(at->fd);
202 out:
203         free(args);
204         return ret;
205 }
206
207 __noreturn static void interactive_session(void)
208 {
209         int ret;
210         char *history_file;
211         struct sigaction act;
212         struct i9e_client_info ici = {
213                 .fds = {0, 1, 2},
214                 .prompt = "para_audioc> ",
215                 .line_handler = audioc_i9e_line_handler,
216                 .loglevel = loglevel,
217                 .completers = audiod_completers,
218         };
219         PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
220         if (conf.history_file_given)
221                 history_file = para_strdup(conf.history_file_arg);
222         else {
223                 char *home = para_homedir();
224                 history_file = make_message("%s/.paraslash/audioc.history",
225                         home);
226                 free(home);
227         }
228         ici.history_file = history_file;
229
230         act.sa_handler = i9e_signal_dispatch;
231         sigemptyset(&act.sa_mask);
232         act.sa_flags = 0;
233         sigaction(SIGINT, &act, NULL);
234         sched.select_function = i9e_select;
235
236         sched.default_timeout.tv_sec = 1;
237         ret = i9e_open(&ici, &sched);
238         if (ret < 0)
239                 goto out;
240         para_log = i9e_log;
241         ret = schedule(&sched);
242         i9e_close();
243         para_log = stderr_log;
244 out:
245         free(history_file);
246         audioc_cmdline_parser_free(&conf);
247         if (ret < 0)
248                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
249         exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
250 }
251
252 __noreturn static void print_completions(void)
253 {
254         int ret = i9e_print_completions(audiod_completers);
255         exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
256 }
257
258 #else /* HAVE_READLINE */
259
260 __noreturn static void interactive_session(void)
261 {
262         PARA_EMERG_LOG("interactive sessions not available\n");
263         exit(EXIT_FAILURE);
264 }
265
266 __noreturn static void print_completions(void)
267 {
268         PARA_EMERG_LOG("command completion not available\n");
269         exit(EXIT_FAILURE);
270 }
271
272 #endif /* HAVE_READLINE */
273
274 static char *configfile_exists(void)
275 {
276         char *config_file;
277         struct stat statbuf;
278         char *home = para_homedir();
279
280         config_file = make_message("%s/.paraslash/audioc.conf", home);
281         free(home);
282         if (!stat(config_file, &statbuf))
283                 return config_file;
284         free(config_file);
285         return NULL;
286 }
287
288 __noreturn static void print_help_and_die(void)
289 {
290         struct ggo_help h = DEFINE_GGO_HELP(audioc);
291         bool d = conf.detailed_help_given;
292
293         ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
294         exit(0);
295 }
296
297 /**
298  * The client program to connect to para_audiod.
299  *
300  * \param argc Usual argument count.
301  * \param argv Usual argument vector.
302  *
303  * It connects to the "well-known" local socket to communicate with
304  * para_audiod. Authentication is performed by sending a ucred buffer
305  * containing the user id to the local socket.
306  *
307  * Any data received from the socket is written to stdout.
308  *
309  * \return EXIT_SUCCESS or EXIT_FAILURE.
310  *
311  * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
312  */
313 int main(int argc, char *argv[])
314 {
315         int ret, fd;
316         char *cf, *buf = NULL, *args = NULL;
317         size_t bufsize;
318
319         audioc_cmdline_parser(argc, argv, &conf);
320         loglevel = get_loglevel_by_name(conf.loglevel_arg);
321         version_handle_flag("audioc", conf.version_given);
322         if (conf.help_given || conf.detailed_help_given)
323                 print_help_and_die();
324         cf = configfile_exists();
325         if (cf) {
326                 struct audioc_cmdline_parser_params params = {
327                         .override = 0,
328                         .initialize = 0,
329                         .check_required = 0,
330                         .check_ambiguity = 0,
331                         .print_errors = 1,
332
333                 };
334                 audioc_cmdline_parser_config_file(cf, &conf, &params);
335                 free(cf);
336                 loglevel = get_loglevel_by_name(conf.loglevel_arg);
337         }
338         if (conf.socket_given)
339                 socket_name = para_strdup(conf.socket_arg);
340         else {
341                 char *hn = para_hostname();
342                 socket_name = make_message("/var/paraslash/audiod_socket.%s",
343                         hn);
344                 free(hn);
345         }
346
347         if (conf.complete_given)
348                 print_completions();
349
350         if (conf.inputs_num == 0)
351                 interactive_session();
352         args = concat_args(conf.inputs_num, conf.inputs);
353
354         ret = connect_audiod(socket_name, args);
355         free(socket_name);
356         if (ret < 0)
357                 goto out;
358         fd = ret;
359         ret = mark_fd_blocking(STDOUT_FILENO);
360         if (ret < 0)
361                 goto out;
362         bufsize = conf.bufsize_arg;
363         buf = para_malloc(bufsize);
364         do {
365                 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
366                 if (ret <= 0)
367                         break;
368                 ret = write_all(STDOUT_FILENO, buf, n);
369         } while (ret >= 0);
370 out:
371         free(buf);
372         free(args);
373         if (ret < 0)
374                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
375         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
376 }