server: Lookup user only once.
[paraslash.git] / audioc.c
index 73bc2cf36bc0bbd51f43490548c2241e65bf13b0..0edab366efe6ef6fa50f99c22b9ffa3492494640 100644 (file)
--- a/audioc.c
+++ b/audioc.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005-2013 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -9,6 +9,7 @@
 #include <regex.h>
 #include <sys/types.h>
 #include <stdbool.h>
+#include <signal.h>
 
 #include "audioc.cmdline.h"
 #include "para.h"
@@ -16,6 +17,7 @@
 #include "net.h"
 #include "string.h"
 #include "fd.h"
+#include "ggo.h"
 #include "version.h"
 
 INIT_AUDIOC_ERRLISTS;
@@ -41,6 +43,25 @@ static char *concat_args(unsigned argc, char * const *argv)
        return buf;
 }
 
+static int connect_audiod(const char *sname, char *args)
+{
+       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"
@@ -105,7 +126,7 @@ static void audioc_pre_select(struct sched *s, struct task *t)
        para_fd_set(at->fd, &s->rfds, &s->max_fileno);
 }
 
-static void audioc_post_select(struct sched *s, struct task *t)
+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);
@@ -114,7 +135,7 @@ static void audioc_post_select(struct sched *s, struct task *t)
        if (ret < 0)
                goto out;
        if (!FD_ISSET(at->fd, &s->rfds))
-               return;
+               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);
@@ -123,16 +144,14 @@ static void audioc_post_select(struct sched *s, struct task *t)
        if (ret < 0)
                goto out;
        btr_add_output(buf, ret, at->btrn);
-       return;
+       return 0;
 out:
        if (ret < 0) {
                free(buf);
-               btr_remove_node(at->btrn);
-               btr_free_node(at->btrn);
-               at->btrn = NULL;
+               btr_remove_node(&at->btrn);
                close(at->fd);
        }
-       t->error = ret;
+       return ret;
 }
 
 static struct audioc_task audioc_task = {
@@ -147,8 +166,6 @@ static int audioc_i9e_line_handler(char *line)
 {
        char *args = NULL;
        int ret;
-       if (!line || !*line)
-               return 0;
 
        PARA_DEBUG_LOG("line: %s\n", line);
        ret = create_argv(line, " ", &conf.inputs);
@@ -157,15 +174,14 @@ static int audioc_i9e_line_handler(char *line)
        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_local_socket(socket_name);
+       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;
-       ret = send_cred_buffer(at->fd, args);
        if (ret < 0)
                goto close;
        free(args);
@@ -195,7 +211,7 @@ __noreturn static void interactive_session(void)
                .loglevel = loglevel,
                .completers = audiod_completers,
        };
-       PARA_NOTICE_LOG("\n%s\n", VERSION_TEXT("audioc"));
+       PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
        if (conf.history_file_given)
                history_file = para_strdup(conf.history_file_arg);
        else {
@@ -252,29 +268,38 @@ __noreturn static void print_completions(void)
 
 static char *configfile_exists(void)
 {
-       static char *config_file;
+       char *config_file;
        struct stat statbuf;
+       char *home = para_homedir();
 
-       if (!config_file) {
-               char *home = para_homedir();
-               config_file = make_message("%s/.paraslash/audioc.conf", home);
-               free(home);
-       }
+       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.
  *
  * \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.
  *
@@ -282,27 +307,29 @@ static char *configfile_exists(void)
  */
 int main(int argc, char *argv[])
 {
-       int ret = -E_AUDIOC_SYNTAX, fd;
-       char *cf, *buf = NULL, *args;
+       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) {
                struct audioc_cmdline_parser_params params = {
                        .override = 0,
                        .initialize = 0,
                        .check_required = 0,
-                       .check_ambiguity = 0
+                       .check_ambiguity = 0,
+                       .print_errors = 1,
+
                };
-               if (audioc_cmdline_parser_config_file(cf, &conf, &params)) {
-                       fprintf(stderr, "parse error in config file\n");
-                       exit(EXIT_FAILURE);
-               }
+               audioc_cmdline_parser_config_file(cf, &conf, &params);
+               free(cf);
+               loglevel = get_loglevel_by_name(conf.loglevel_arg);
        }
-       loglevel = get_loglevel_by_name(conf.loglevel_arg);
        if (conf.socket_given)
                socket_name = para_strdup(conf.socket_arg);
        else {
@@ -319,14 +346,12 @@ int main(int argc, char *argv[])
                interactive_session();
        args = concat_args(conf.inputs_num, conf.inputs);
 
-       ret = connect_local_socket(socket_name);
+       ret = connect_audiod(socket_name, args);
        free(socket_name);
-       if (ret < 0) {
-               PARA_EMERG_LOG("failed to connect to local socket\n");
+       if (ret < 0)
                goto out;
-       }
        fd = ret;
-       ret = send_cred_buffer(fd, args);
+       ret = mark_fd_blocking(STDOUT_FILENO);
        if (ret < 0)
                goto out;
        bufsize = conf.bufsize_arg;
@@ -335,9 +360,11 @@ int main(int argc, char *argv[])
                size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
                if (ret <= 0)
                        break;
-               ret = write_all(STDOUT_FILENO, buf, &n);
+               ret = write_all(STDOUT_FILENO, buf, n);
        } while (ret >= 0);
 out:
+       free(buf);
+       free(args);
        if (ret < 0)
                PARA_ERROR_LOG("%s\n", para_strerror(-ret));
        return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;