66ff2a267dc5228056cce913055afc177f9c6560
[paraslash.git] / audioc.c
1 /*
2 * Copyright (C) 2005-2007 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 <sys/types.h>
10 #include <dirent.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
19 INIT_AUDIOC_ERRLISTS;
20
21 /** the gengetopt structure containing command line args */
22 struct audioc_args_info conf;
23
24 INIT_STDERR_LOGGING(conf.loglevel_arg);
25
26 static char *concat_args(unsigned argc, char * const *argv)
27 {
28 int i; char *buf = NULL;
29 for (i = 0; i < argc; i++) {
30 buf = para_strcat(buf, argv[i]);
31 if (i != argc - 1)
32 buf = para_strcat(buf, "\n");
33 }
34 return buf;
35 }
36
37 static char *configfile_exists(void)
38 {
39 static char *config_file;
40 struct stat statbuf;
41
42
43 if (!config_file) {
44 char *home = para_homedir();
45 config_file = make_message("%s/.paraslash/audioc.conf", home);
46 free(home);
47 }
48 if (!stat(config_file, &statbuf))
49 return config_file;
50 return NULL;
51 }
52
53 /**
54 * the client program to connect to para_audiod
55 *
56 * \param argc usual argument count
57 * \param argv usual argument vector
58 *
59 * It creates a temporary local socket in order to communicate with para_audiod.
60 * Authentication consists in sending a ucred buffer that contains the user id.
61 *
62 * Any output received through the local socket is sent to stdout.
63 *
64 * \return EXIT_SUCCESS or EXIT_FAILURE
65 *
66 * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
67 */
68 int main(int argc, char *argv[])
69 {
70 struct sockaddr_un unix_addr;
71 int ret = -E_AUDIOC_SYNTAX, fd;
72 char *cf, *socket_name, *randname = para_tmpname(), *tmpsocket_name = NULL,
73 *buf = NULL, *hn = para_hostname(), *args, *home = para_homedir();
74 size_t bufsize, loaded = 0;
75
76 if (audioc_cmdline_parser(argc, argv, &conf))
77 goto out;
78 HANDLE_VERSION_FLAG("audioc", conf);
79 cf = configfile_exists();
80 if (cf) {
81 struct audioc_cmdline_parser_params params = {
82 .override = 0,
83 .initialize = 0,
84 .check_required = 0,
85 .check_ambiguity = 0
86 };
87 if (audioc_cmdline_parser_config_file(cf, &conf, &params)) {
88 fprintf(stderr, "parse error in config file\n");
89 exit(EXIT_FAILURE);
90 }
91 }
92 args = conf.inputs_num?
93 concat_args(conf.inputs_num, conf.inputs) :
94 para_strdup("stat");
95 bufsize = conf.bufsize_arg;
96 buf = para_malloc(bufsize);
97 if (conf.socket_given)
98 socket_name = para_strdup(conf.socket_arg);
99 else
100 socket_name = make_message(
101 "/var/paraslash/audiod_socket.%s", hn);
102 if (conf.tmpdir_given)
103 tmpsocket_name = make_message("%s/audioc.sock.%s.%s",
104 conf.tmpdir_arg, hn, randname);
105 else
106 tmpsocket_name = make_message("%s/.paraslash/audioc_sock.%s.%s",
107 home, hn, randname);
108
109 ret = create_local_socket(tmpsocket_name, &unix_addr, S_IRUSR | S_IWUSR);
110 unlink(tmpsocket_name);
111 free(tmpsocket_name);
112 if (ret < 0)
113 goto out;
114 fd = ret;
115 ret = -E_INIT_SOCK_ADDR;
116 if (init_unix_addr(&unix_addr, socket_name) < 0)
117 goto out;
118 ret = PARA_CONNECT(fd, &unix_addr);
119 if (ret < 0)
120 goto out;
121 ret = send_cred_buffer(fd, args);
122 if (ret < 0)
123 goto out;
124 for (;;) {
125 int max_fileno = -1, check_write = 0;
126 ssize_t len;
127 fd_set rfd, wfd;
128 FD_ZERO(&rfd);
129 FD_ZERO(&wfd);
130 if (loaded < bufsize)
131 para_fd_set(fd, &rfd, &max_fileno);
132 if (loaded > 0) {
133 para_fd_set(STDOUT_FILENO, &wfd, &max_fileno);
134 check_write = 1;
135 }
136 ret = -E_AUDIOC_OVERRUN;
137 if (max_fileno < 0)
138 goto out;
139 ret = para_select(max_fileno + 1, &rfd, &wfd, NULL);
140 if (ret < 0)
141 goto out;
142 if (loaded < bufsize && FD_ISSET(fd, &rfd)) {
143 len = recv_bin_buffer(fd, buf + loaded,
144 bufsize - loaded);
145 if (len <= 0) {
146 ret = len < 0? -E_AUDIOC_READ : 0;
147 goto out;
148 }
149 loaded += len;
150 }
151 if (check_write && FD_ISSET(STDOUT_FILENO, &wfd)) {
152 ret = write(STDOUT_FILENO, buf, loaded);
153 if (ret < 0) {
154 ret = -E_AUDIOC_WRITE;
155 goto out;
156 }
157 loaded -= ret;
158 }
159 }
160 out:
161 if (!ret && loaded && buf)
162 ret = write(STDOUT_FILENO, buf, loaded);
163 if (ret < 0)
164 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
165 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
166 }