sched: Dont use fixed-size buffer for task names.
[paraslash.git] / audioc.c
1 /*
2 * Copyright (C) 2005-2014 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 = task_context(t);
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 = task_context(t);
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, *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 = task_register(&(struct task_info) {
191 .name = "audioc",
192 .pre_select = audioc_pre_select,
193 .post_select = audioc_post_select,
194 .context = at,
195 }, &sched);
196 i9e_attach_to_stdout(at->btrn);
197 return 1;
198 close:
199 close(at->fd);
200 out:
201 free(args);
202 return ret;
203 }
204
205 __noreturn static void interactive_session(void)
206 {
207 int ret;
208 char *history_file;
209 struct sigaction act;
210 struct i9e_client_info ici = {
211 .fds = {0, 1, 2},
212 .prompt = "para_audioc> ",
213 .line_handler = audioc_i9e_line_handler,
214 .loglevel = loglevel,
215 .completers = audiod_completers,
216 };
217 PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
218 if (conf.history_file_given)
219 history_file = para_strdup(conf.history_file_arg);
220 else {
221 char *home = para_homedir();
222 history_file = make_message("%s/.paraslash/audioc.history",
223 home);
224 free(home);
225 }
226 ici.history_file = history_file;
227
228 act.sa_handler = i9e_signal_dispatch;
229 sigemptyset(&act.sa_mask);
230 act.sa_flags = 0;
231 sigaction(SIGINT, &act, NULL);
232 sched.select_function = i9e_select;
233
234 sched.default_timeout.tv_sec = 1;
235 ret = i9e_open(&ici, &sched);
236 if (ret < 0)
237 goto out;
238 para_log = i9e_log;
239 ret = schedule(&sched);
240 sched_shutdown(&sched);
241 i9e_close();
242 para_log = stderr_log;
243 out:
244 free(history_file);
245 audioc_cmdline_parser_free(&conf);
246 if (ret < 0)
247 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
248 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
249 }
250
251 __noreturn static void print_completions(void)
252 {
253 int ret = i9e_print_completions(audiod_completers);
254 exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
255 }
256
257 #else /* HAVE_READLINE */
258
259 __noreturn static void interactive_session(void)
260 {
261 PARA_EMERG_LOG("interactive sessions not available\n");
262 exit(EXIT_FAILURE);
263 }
264
265 __noreturn static void print_completions(void)
266 {
267 PARA_EMERG_LOG("command completion not available\n");
268 exit(EXIT_FAILURE);
269 }
270
271 #endif /* HAVE_READLINE */
272
273 static char *configfile_exists(void)
274 {
275 char *config_file;
276 struct stat statbuf;
277 char *home = para_homedir();
278
279 config_file = make_message("%s/.paraslash/audioc.conf", home);
280 free(home);
281 if (!stat(config_file, &statbuf))
282 return config_file;
283 free(config_file);
284 return NULL;
285 }
286
287 __noreturn static void print_help_and_die(void)
288 {
289 struct ggo_help h = DEFINE_GGO_HELP(audioc);
290 bool d = conf.detailed_help_given;
291
292 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
293 exit(0);
294 }
295
296 /**
297 * The client program to connect to para_audiod.
298 *
299 * \param argc Usual argument count.
300 * \param argv Usual argument vector.
301 *
302 * It connects to the "well-known" local socket to communicate with
303 * para_audiod. Authentication is performed by sending a ucred buffer
304 * containing the user id to the local socket.
305 *
306 * Any data received from the socket is written to stdout.
307 *
308 * \return EXIT_SUCCESS or EXIT_FAILURE.
309 *
310 * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
311 */
312 int main(int argc, char *argv[])
313 {
314 int ret, fd;
315 char *cf, *buf = NULL, *args = NULL;
316 size_t bufsize;
317
318 audioc_cmdline_parser(argc, argv, &conf);
319 loglevel = get_loglevel_by_name(conf.loglevel_arg);
320 version_handle_flag("audioc", conf.version_given);
321 if (conf.help_given || conf.detailed_help_given)
322 print_help_and_die();
323 cf = configfile_exists();
324 if (cf) {
325 struct audioc_cmdline_parser_params params = {
326 .override = 0,
327 .initialize = 0,
328 .check_required = 0,
329 .check_ambiguity = 0,
330 .print_errors = 1,
331
332 };
333 audioc_cmdline_parser_config_file(cf, &conf, &params);
334 free(cf);
335 loglevel = get_loglevel_by_name(conf.loglevel_arg);
336 }
337 if (conf.socket_given)
338 socket_name = para_strdup(conf.socket_arg);
339 else {
340 char *hn = para_hostname();
341 socket_name = make_message("/var/paraslash/audiod_socket.%s",
342 hn);
343 free(hn);
344 }
345
346 if (conf.complete_given)
347 print_completions();
348
349 if (conf.inputs_num == 0)
350 interactive_session();
351 args = concat_args(conf.inputs_num, conf.inputs);
352
353 ret = connect_audiod(socket_name, args);
354 free(socket_name);
355 if (ret < 0)
356 goto out;
357 fd = ret;
358 ret = mark_fd_blocking(STDOUT_FILENO);
359 if (ret < 0)
360 goto out;
361 bufsize = conf.bufsize_arg;
362 buf = para_malloc(bufsize);
363 do {
364 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
365 if (ret <= 0)
366 break;
367 ret = write_all(STDOUT_FILENO, buf, n);
368 } while (ret >= 0);
369 out:
370 free(buf);
371 free(args);
372 if (ret < 0)
373 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
374 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
375 }