audioc: Abstract out connection code.
authorAndre Noll <maan@systemlinux.org>
Tue, 2 Apr 2013 02:02:21 +0000 (02:02 +0000)
committerAndre Noll <maan@systemlinux.org>
Sat, 8 Jun 2013 12:21:59 +0000 (14:21 +0200)
This way we get a nice error message in interactive mode when
para_audiod is down. Before:

para_audioc> help
i9e_line_handler: No such file or directory

After:

para_audioc> help
connect_audiod: could not connect /var/paraslash/audiod_socket.schubert
i9e_line_handler: No such file or directory

audioc.c

index 74fb11c..f68aee5 100644 (file)
--- a/audioc.c
+++ b/audioc.c
@@ -42,6 +42,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"
@@ -157,14 +176,11 @@ static int audioc_i9e_line_handler(char *line)
        args = concat_args(conf.inputs_num, conf.inputs);
        free_argv(conf.inputs);
        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);
@@ -320,21 +336,16 @@ 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;
        buf = para_malloc(bufsize);
-       ret = mark_fd_blocking(STDOUT_FILENO);
-       if (ret < 0)
-               goto out;
        do {
                size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
                if (ret <= 0)