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