Merge branch 't/makefile_cleanups'
[paraslash.git] / audioc.c
1 /*
2  * Copyright (C) 2005-2011 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
12 #include "audioc.cmdline.h"
13 #include "para.h"
14 #include "error.h"
15 #include "net.h"
16 #include "string.h"
17 #include "fd.h"
18 #include "version.h"
19
20 INIT_AUDIOC_ERRLISTS;
21
22 /** The gengetopt structure containing command line args. */
23 static struct audioc_args_info conf;
24
25 static int loglevel;
26 INIT_STDERR_LOGGING(loglevel);
27
28 static char *concat_args(unsigned argc, char * const *argv)
29 {
30         int i;
31         char *buf = NULL;
32
33         for (i = 0; i < argc; i++) {
34                 buf = para_strcat(buf, argv[i]);
35                 if (i != argc - 1)
36                         buf = para_strcat(buf, "\n");
37         }
38         return buf;
39 }
40
41 static char *configfile_exists(void)
42 {
43         static char *config_file;
44         struct stat statbuf;
45
46         if (!config_file) {
47                 char *home = para_homedir();
48                 config_file = make_message("%s/.paraslash/audioc.conf", home);
49                 free(home);
50         }
51         if (!stat(config_file, &statbuf))
52                 return config_file;
53         return NULL;
54 }
55
56 /**
57  * The client program to connect to para_audiod.
58  *
59  * \param argc Usual argument count.
60  * \param argv Usual argument vector.
61  *
62  * It creates a temporary local socket in order to communicate with para_audiod.
63  * Authentication consists in sending a ucred buffer that contains the user id.
64  *
65  * Any output received through the local socket is sent to stdout.
66  *
67  * \return EXIT_SUCCESS or EXIT_FAILURE.
68  *
69  * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
70  */
71 int main(int argc, char *argv[])
72 {
73         int ret = -E_AUDIOC_SYNTAX, fd;
74         char *cf, *buf = NULL, *args;
75         size_t bufsize;
76
77         if (audioc_cmdline_parser(argc, argv, &conf))
78                 goto out;
79         HANDLE_VERSION_FLAG("audioc", conf);
80         cf = configfile_exists();
81         if (cf) {
82                 struct audioc_cmdline_parser_params params = {
83                         .override = 0,
84                         .initialize = 0,
85                         .check_required = 0,
86                         .check_ambiguity = 0
87                 };
88                 if (audioc_cmdline_parser_config_file(cf, &conf, &params)) {
89                         fprintf(stderr, "parse error in config file\n");
90                         exit(EXIT_FAILURE);
91                 }
92         }
93         loglevel = get_loglevel_by_name(conf.loglevel_arg);
94         args = conf.inputs_num?
95                 concat_args(conf.inputs_num, conf.inputs) :
96                 para_strdup("stat");
97
98         if (conf.socket_given)
99                 ret = connect_local_socket(conf.socket_arg);
100         else {
101                 char *hn = para_hostname(), *socket_name = make_message(
102                         "/var/paraslash/audiod_socket.%s", hn);
103                 ret = connect_local_socket(socket_name);
104                 free(hn);
105                 free(socket_name);
106         }
107         if (ret < 0) {
108                 PARA_EMERG_LOG("failed to connect to local socket\n");
109                 goto out;
110         }
111         fd = ret;
112         ret = send_cred_buffer(fd, args);
113         if (ret < 0)
114                 goto out;
115         bufsize = conf.bufsize_arg;
116         buf = para_malloc(bufsize);
117         do {
118                 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
119                 if (ret <= 0)
120                         break;
121                 ret = write_all(STDOUT_FILENO, buf, &n);
122         } while (ret >= 0);
123 out:
124         if (ret < 0)
125                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
126         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
127 }