/*
- * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005-2014 Andre Noll <maan@systemlinux.org>
*
* Licensed under the GPL v2. For licencing details see COPYING.
*/
-/** \file audioc.c the client program used to connect to para_audiod */
+/** \file audioc.c The client program used to connect to para_audiod. */
+
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <regex.h>
+#include <sys/types.h>
+#include <arpa/inet.h>
+#include <sys/un.h>
+#include <netdb.h>
+#include <stdbool.h>
+#include <signal.h>
#include "audioc.cmdline.h"
#include "para.h"
+#include "error.h"
#include "net.h"
#include "string.h"
#include "fd.h"
-#include "error.h"
+#include "ggo.h"
+#include "version.h"
INIT_AUDIOC_ERRLISTS;
-/** the gengetopt structure containing command line args */
-struct audioc_args_info conf;
+/** The gengetopt structure containing command line args. */
+static struct audioc_args_info conf;
+static char *socket_name;
-INIT_STDERR_LOGGING(conf.loglevel_arg);
+
+static int loglevel;
+INIT_STDERR_LOGGING(loglevel);
static char *concat_args(unsigned argc, char * const *argv)
{
- int i; char *buf = NULL;
+ int i;
+ char *buf = NULL;
+
for (i = 0; i < argc; i++) {
buf = para_strcat(buf, argv[i]);
if (i != argc - 1)
return buf;
}
-static char *configfile_exists(void)
+static int connect_audiod(const char *sname, char *args)
{
- static char *config_file;
- struct stat statbuf;
+ int fd = -1, ret;
+
+ ret = connect_local_socket(sname);
+ if (ret < 0)
+ goto fail;
+ fd = ret;
+ ret = send_cred_buffer(fd, args);
+ if (ret < 0)
+ goto fail;
+ return fd;
+fail:
+ PARA_ERROR_LOG("could not connect %s\n", sname);
+ if (fd >= 0)
+ close(fd);
+ return ret;
+}
+
+#ifdef HAVE_READLINE
+#include "list.h"
+#include "sched.h"
+#include "buffer_tree.h"
+#include "interactive.h"
+#include "audiod_completion.h"
+
+static struct sched sched;
+
+struct audioc_task {
+ int fd;
+ struct btr_node *btrn;
+ struct task task;
+};
+
+static struct i9e_completer audiod_completers[];
+
+I9E_DUMMY_COMPLETER(cycle);
+I9E_DUMMY_COMPLETER(off);
+I9E_DUMMY_COMPLETER(on);
+I9E_DUMMY_COMPLETER(sb);
+I9E_DUMMY_COMPLETER(tasks);
+I9E_DUMMY_COMPLETER(term);
+
+static void help_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *result)
+{
+ result->matches = i9e_complete_commands(ci->word, audiod_completers);
+}
+
+static void stat_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ char *sia[] = {STATUS_ITEM_ARRAY NULL};
+ char *opts[] = {"-p", NULL};
+
+ if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
+ i9e_complete_option(opts, ci, cr);
+ else
+ i9e_extract_completions(ci->word, sia, &cr->matches);
+}
+
+static void grab_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ char *opts[] = {"-ms", "-ms", "-ma", "-p=", "-n=", "-o", NULL};
+ i9e_complete_option(opts, ci, cr);
+}
+
+static struct i9e_completer audiod_completers[] = {
+ AUDIOD_COMPLETERS
+ {.name = NULL}
+};
+
+static void audioc_pre_select(struct sched *s, struct task *t)
+{
+ struct audioc_task *at = container_of(t, struct audioc_task, task);
+ int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
+
+ if (ret < 0)
+ sched_min_delay(s);
+ para_fd_set(at->fd, &s->rfds, &s->max_fileno);
+}
+
+static int audioc_post_select(struct sched *s, struct task *t)
+{
+ char *buf = NULL;
+ struct audioc_task *at = container_of(t, struct audioc_task, task);
+ int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
+
+ if (ret < 0)
+ goto out;
+ if (!FD_ISSET(at->fd, &s->rfds))
+ return 0;
+ buf = para_malloc(conf.bufsize_arg);
+ ret = recv_bin_buffer(at->fd, buf, conf.bufsize_arg);
+ PARA_DEBUG_LOG("recv: %d\n", ret);
+ if (ret == 0)
+ ret = -E_AUDIOC_EOF;
+ if (ret < 0)
+ goto out;
+ btr_add_output(buf, ret, at->btrn);
+ return 0;
+out:
+ if (ret < 0) {
+ free(buf);
+ btr_remove_node(&at->btrn);
+ close(at->fd);
+ }
+ return ret;
+}
+static struct audioc_task audioc_task = {
+ .task = {
+ .pre_select = audioc_pre_select,
+ .post_select = audioc_post_select,
+ .status = "audioc task"
+ },
+}, *at = &audioc_task;
- if (!config_file) {
+static int audioc_i9e_line_handler(char *line)
+{
+ char *args = NULL;
+ int ret;
+
+ PARA_DEBUG_LOG("line: %s\n", line);
+ ret = create_argv(line, " ", &conf.inputs);
+ if (ret < 0)
+ return ret;
+ conf.inputs_num = ret;
+ args = concat_args(conf.inputs_num, conf.inputs);
+ free_argv(conf.inputs);
+ if (!args)
+ return 0;
+ conf.inputs_num = 0; /* required for audioc_cmdline_parser_free() */
+ ret = connect_audiod(socket_name, args);
+ if (ret < 0)
+ goto out;
+ at->fd = ret;
+ ret = mark_fd_nonblocking(at->fd);
+ if (ret < 0)
+ goto close;
+ free(args);
+ args = NULL;
+ at->btrn = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = "audioc line handler"));
+ at->task.error = 0;
+ register_task(&sched, &at->task);
+ i9e_attach_to_stdout(at->btrn);
+ return 1;
+close:
+ close(at->fd);
+out:
+ free(args);
+ return ret;
+}
+
+__noreturn static void interactive_session(void)
+{
+ int ret;
+ char *history_file;
+ struct sigaction act;
+ struct i9e_client_info ici = {
+ .fds = {0, 1, 2},
+ .prompt = "para_audioc> ",
+ .line_handler = audioc_i9e_line_handler,
+ .loglevel = loglevel,
+ .completers = audiod_completers,
+ };
+ PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
+ if (conf.history_file_given)
+ history_file = para_strdup(conf.history_file_arg);
+ else {
char *home = para_homedir();
- config_file = make_message("%s/.paraslash/audioc.conf", home);
+ history_file = make_message("%s/.paraslash/audioc.history",
+ home);
free(home);
}
+ ici.history_file = history_file;
+
+ act.sa_handler = i9e_signal_dispatch;
+ sigemptyset(&act.sa_mask);
+ act.sa_flags = 0;
+ sigaction(SIGINT, &act, NULL);
+ sched.select_function = i9e_select;
+
+ sched.default_timeout.tv_sec = 1;
+ ret = i9e_open(&ici, &sched);
+ if (ret < 0)
+ goto out;
+ para_log = i9e_log;
+ ret = schedule(&sched);
+ i9e_close();
+ para_log = stderr_log;
+out:
+ free(history_file);
+ audioc_cmdline_parser_free(&conf);
+ if (ret < 0)
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
+__noreturn static void print_completions(void)
+{
+ int ret = i9e_print_completions(audiod_completers);
+ exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
+#else /* HAVE_READLINE */
+
+__noreturn static void interactive_session(void)
+{
+ PARA_EMERG_LOG("interactive sessions not available\n");
+ exit(EXIT_FAILURE);
+}
+
+__noreturn static void print_completions(void)
+{
+ PARA_EMERG_LOG("command completion not available\n");
+ exit(EXIT_FAILURE);
+}
+
+#endif /* HAVE_READLINE */
+
+static char *configfile_exists(void)
+{
+ char *config_file;
+ struct stat statbuf;
+ char *home = para_homedir();
+
+ config_file = make_message("%s/.paraslash/audioc.conf", home);
+ free(home);
if (!stat(config_file, &statbuf))
return config_file;
+ free(config_file);
return NULL;
}
+__noreturn static void print_help_and_die(void)
+{
+ struct ggo_help h = DEFINE_GGO_HELP(audioc);
+ bool d = conf.detailed_help_given;
+
+ ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
+ exit(0);
+}
+
/**
- * the client program to connect to para_audiod
+ * The client program to connect to para_audiod.
*
- * \param argc usual argument count
- * \param argv usual argument vector
+ * \param argc Usual argument count.
+ * \param argv Usual argument vector.
*
- * It creates a temporary local socket in order to communicate with para_audiod.
- * Authentication consists in sending a ucred buffer that contains the user id.
+ * It connects to the "well-known" local socket to communicate with
+ * para_audiod. Authentication is performed by sending a ucred buffer
+ * containing the user id to the local socket.
*
- * Any output received through the local socket is sent to stdout.
+ * Any data received from the socket is written to stdout.
*
- * \return EXIT_SUCCESS or EXIT_FAILURE
+ * \return EXIT_SUCCESS or EXIT_FAILURE.
*
* \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
*/
int main(int argc, char *argv[])
{
- struct sockaddr_un unix_addr;
- int ret = -E_AUDIOC_SYNTAX, fd;
- char *cf, *socket_name, *randname = para_tmpname(), *tmpsocket_name = NULL,
- *buf = NULL, *hn = para_hostname(), *args, *home = para_homedir();
- size_t bufsize = conf.bufsize_arg, loaded = 0;
+ int ret, fd;
+ char *cf, *buf = NULL, *args = NULL;
+ size_t bufsize;
- if (audioc_cmdline_parser(argc, argv, &conf))
- goto out;
- HANDLE_VERSION_FLAG("audioc", conf);
+ audioc_cmdline_parser(argc, argv, &conf);
+ loglevel = get_loglevel_by_name(conf.loglevel_arg);
+ version_handle_flag("audioc", conf.version_given);
+ if (conf.help_given || conf.detailed_help_given)
+ print_help_and_die();
cf = configfile_exists();
if (cf) {
- if (audioc_cmdline_parser_configfile(cf, &conf, 0, 0, 0)) {
- fprintf(stderr, "parse error in config file\n");
- exit(EXIT_FAILURE);
- }
+ struct audioc_cmdline_parser_params params = {
+ .override = 0,
+ .initialize = 0,
+ .check_required = 0,
+ .check_ambiguity = 0,
+ .print_errors = 1,
+
+ };
+ audioc_cmdline_parser_config_file(cf, &conf, ¶ms);
+ free(cf);
+ loglevel = get_loglevel_by_name(conf.loglevel_arg);
}
- args = conf.inputs_num?
- concat_args(conf.inputs_num, conf.inputs) :
- para_strdup("stat");
- buf = para_malloc(bufsize);
if (conf.socket_given)
socket_name = para_strdup(conf.socket_arg);
- else
- socket_name = make_message(
- "/var/paraslash/audiod_socket.%s", hn);
- if (conf.tmpdir_given)
- tmpsocket_name = make_message("%s/audioc.sock.%s.%s",
- conf.tmpdir_arg, hn, randname);
- else
- tmpsocket_name = make_message("%s/.paraslash/audioc_sock.%s.%s",
- home, hn, randname);
+ else {
+ char *hn = para_hostname();
+ socket_name = make_message("/var/paraslash/audiod_socket.%s",
+ hn);
+ free(hn);
+ }
+
+ if (conf.complete_given)
+ print_completions();
+
+ if (conf.inputs_num == 0)
+ interactive_session();
+ args = concat_args(conf.inputs_num, conf.inputs);
- ret = create_pf_socket(tmpsocket_name, &unix_addr, S_IRUSR | S_IWUSR);
- unlink(tmpsocket_name);
+ ret = connect_audiod(socket_name, args);
+ free(socket_name);
if (ret < 0)
goto out;
fd = ret;
- ret = -E_INIT_SOCK_ADDR;
- if (init_unix_addr(&unix_addr, socket_name) < 0)
- goto out;
- ret = -E_AUDIOC_CONNECT;
- if (connect(fd, (struct sockaddr *)&unix_addr, UNIX_PATH_MAX) < 0)
- goto out;
- ret = send_cred_buffer(fd, args);
+ ret = mark_fd_blocking(STDOUT_FILENO);
if (ret < 0)
goto out;
- for (;;) {
- int max_fileno = -1, check_write = 0;
- ssize_t len;
- fd_set rfd, wfd;
- FD_ZERO(&rfd);
- FD_ZERO(&wfd);
- if (loaded < bufsize)
- para_fd_set(fd, &rfd, &max_fileno);
- if (loaded > 0) {
- para_fd_set(STDOUT_FILENO, &wfd, &max_fileno);
- check_write = 1;
- }
- ret = -E_AUDIOC_OVERRUN;
- if (max_fileno < 0)
- goto out;
- ret = para_select(max_fileno + 1, &rfd, &wfd, NULL);
- if (ret < 0)
- goto out;
- if (loaded < bufsize && FD_ISSET(fd, &rfd)) {
- len = recv_bin_buffer(fd, buf + loaded,
- bufsize - loaded);
- if (len <= 0) {
- ret = len < 0? -E_AUDIOC_READ : 0;
- goto out;
- }
- loaded += len;
- }
- if (check_write && FD_ISSET(STDOUT_FILENO, &wfd)) {
- ret = write(STDOUT_FILENO, buf, loaded);
- if (ret < 0) {
- ret = -E_AUDIOC_WRITE;
- goto out;
- }
- loaded -= ret;
- }
- }
+ bufsize = conf.bufsize_arg;
+ buf = para_malloc(bufsize);
+ do {
+ size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
+ if (ret <= 0)
+ break;
+ ret = write_all(STDOUT_FILENO, buf, n);
+ } while (ret >= 0);
out:
- if (!ret && loaded && buf)
- ret = write(STDOUT_FILENO, buf, loaded);
+ free(buf);
+ free(args);
if (ret < 0)
- PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
}