Merge branch 'master' into my-osx
[paraslash.git] / audioc.c
1 /*
2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file audioc.c the client program used to connect to para_audiod */
20
21 #include "audioc.cmdline.h"
22 #include "para.h"
23 #include "net.h"
24 #include "string.h"
25 #include "fd.h"
26 #include "error.h"
27
28 INIT_AUDIOC_ERRLISTS;
29
30 struct gengetopt_args_info conf;
31 char *tmpfifo;
32
33
34 /*
35 * client log function
36 */
37 void para_log(int ll, const char* fmt,...)
38 {
39 va_list argp;
40
41 /* ignore log message if loglevel is not high enough */
42 if (ll < conf.loglevel_arg)
43 return;
44 va_start(argp, fmt);
45 vfprintf(stderr, fmt, argp);
46 va_end(argp);
47 }
48
49 /* audioc does not use encryption */
50 void (*crypt_function_recv)(unsigned long len, const unsigned char *indata, unsigned char *outdata) = NULL;
51 void (*crypt_function_send)(unsigned long len, const unsigned char *indata, unsigned char *outdata) = NULL;
52
53 static char *concat_args(const int argc, char * const *argv)
54 {
55 int i; char *buf = NULL;
56 for (i = 0; i < argc; i++) {
57 buf = para_strcat(buf, argv[i]);
58 if (i != argc - 1)
59 buf = para_strcat(buf, "\n");
60 }
61 return buf;
62 }
63
64 static char *configfile_exists(void)
65 {
66 static char *config_file;
67 struct stat statbuf;
68
69
70 if (!config_file) {
71 char *home = para_homedir();
72 config_file = make_message("%s/.paraslash/audioc.conf", home);
73 free(home);
74 }
75 if (!stat(config_file, &statbuf))
76 return config_file;
77 return NULL;
78 }
79
80 int main(int argc, char *argv[])
81 {
82 struct sockaddr_un unix_addr;
83 int ret = -E_AUDIOC_SYNTAX, fd, loaded = 0;
84 char *cf, *socket_name, *randname = para_tmpname(), *tmpsocket_name = NULL,
85 *buf = NULL, *hn = para_hostname(), *args, *home = para_homedir();
86
87
88 if (cmdline_parser(argc, argv, &conf))
89 goto out;
90 cf = configfile_exists();
91 if (cf) {
92 if (cmdline_parser_configfile(cf, &conf, 0, 0, 0)) {
93 fprintf(stderr, "parse error in config file\n");
94 exit(EXIT_FAILURE);
95 }
96 }
97 args = conf.inputs_num?
98 concat_args(conf.inputs_num, conf.inputs) :
99 para_strdup("stat");
100 buf = para_malloc(conf.bufsize_arg);
101 if (conf.socket_given)
102 socket_name = para_strdup(conf.socket_arg);
103 else
104 socket_name = make_message(
105 "/var/paraslash/audiod_socket.%s", hn);
106 if (conf.tmpdir_given)
107 tmpsocket_name = make_message("%s/audioc.sock.%s.%s",
108 conf.tmpdir_arg, hn, randname);
109 else
110 tmpsocket_name = make_message("%s/.paraslash/audioc_sock.%s.%s",
111 home, hn, randname);
112
113 ret = create_pf_socket(tmpsocket_name, &unix_addr, S_IRUSR | S_IWUSR);
114 unlink(tmpsocket_name);
115 if (ret < 0)
116 goto out;
117 fd = ret;
118 ret = -E_INIT_SOCK_ADDR;
119 if (init_unix_addr(&unix_addr, socket_name) < 0)
120 goto out;
121 ret = - E_AUDIOC_CONNECT;
122 if (connect(fd, (struct sockaddr *)&unix_addr, UNIX_PATH_MAX) < 0)
123 goto out;
124 fprintf(stderr, "loaded: %d\n", loaded);
125 ret = send_cred_buffer(fd, args);
126 if (ret < 0)
127 goto out;
128 for (;;) {
129 int max_fileno = -1, check_write = 0;
130 ssize_t len;
131 fd_set rfd, wfd;
132 FD_ZERO(&rfd);
133 FD_ZERO(&wfd);
134 if (loaded && loaded > 10000)
135 fprintf(stderr, "loaded: %d\n", loaded);
136 if (loaded < conf.bufsize_arg)
137 para_fd_set(fd, &rfd, &max_fileno);
138 if (loaded > 0) {
139 para_fd_set(STDOUT_FILENO, &wfd, &max_fileno);
140 check_write = 1;
141 }
142 ret = -E_AUDIOC_OVERRUN;
143 if (max_fileno < 0)
144 goto out;
145 ret = para_select(max_fileno + 1, &rfd, &wfd, NULL);
146 if (ret < 0)
147 goto out;
148 if (loaded < conf.bufsize_arg && FD_ISSET(fd, &rfd)) {
149 len = recv_bin_buffer(fd, buf + loaded,
150 conf.bufsize_arg - loaded);
151 if (len <= 0) {
152 ret = len < 0? -E_AUDIOC_READ : 0;
153 goto out;
154 }
155 loaded += len;
156 }
157 if (check_write && FD_ISSET(STDOUT_FILENO, &wfd)) {
158 ret = write(STDOUT_FILENO, buf, loaded);
159 if (ret < 0) {
160 ret = -E_AUDIOC_WRITE;
161 goto out;
162 }
163 loaded -= ret;
164 }
165 }
166 out:
167 if (!ret && loaded && buf)
168 ret = write(STDOUT_FILENO, buf, loaded);
169 if (ret < 0)
170 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
171 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
172 }