fix potential segfault in para_client
[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
26 struct gengetopt_args_info conf;
27 char *tmpfifo;
28 enum {E_SYNTAX, E_READ, E_WRITE, E_SOCKET, E_INIT_SOCK_ADDR, E_CONNECT, E_CREDENTIALS, E_SELECT, E_OVERRUN};
29
30
31 void para_log(__unused int ll, __unused const char* fmt,...) /* no logging */
32 {
33 }
34
35 /* audioc does not use encryption */
36 void (*crypt_function_recv)(unsigned long len, const unsigned char *indata, unsigned char *outdata) = NULL;
37 void (*crypt_function_send)(unsigned long len, const unsigned char *indata, unsigned char *outdata) = NULL;
38
39 static char *concat_args(const int argc, char * const *argv)
40 {
41 int i; char *buf = NULL;
42 for (i = 0; i < argc; i++) {
43 buf = para_strcat(buf, argv[i]);
44 if (i != argc - 1)
45 buf = para_strcat(buf, "\n");
46 }
47 return buf;
48 }
49
50 static char *configfile_exists(void)
51 {
52 static char *config_file;
53 struct stat statbuf;
54
55
56 if (!config_file) {
57 char *home = para_homedir();
58 config_file = make_message("%s/.paraslash/audioc.conf", home);
59 free(home);
60 }
61 if (!stat(config_file, &statbuf))
62 return config_file;
63 return NULL;
64 }
65
66 int main(int argc, char *argv[])
67 {
68 struct sockaddr_un unix_addr;
69 int ret = -E_SYNTAX, fd, loaded = 0;
70 char *cf, *socket_name, *randname = para_tmpname(), *tmpsocket_name = NULL,
71 *buf = NULL, *hn = para_hostname(), *args, *home = para_homedir();
72
73
74 if (cmdline_parser(argc, argv, &conf))
75 goto out;
76 cf = configfile_exists();
77 if (cf) {
78 if (cmdline_parser_configfile(cf, &conf, 0, 0, 0)) {
79 fprintf(stderr, "parse error in config file\n");
80 exit(EXIT_FAILURE);
81 }
82 }
83 args = conf.inputs_num?
84 concat_args(conf.inputs_num, conf.inputs) :
85 para_strdup("stat");
86 buf = para_malloc(conf.bufsize_arg);
87 if (conf.socket_given)
88 socket_name = para_strdup(conf.socket_arg);
89 else
90 socket_name = make_message(
91 "/var/paraslash/audiod_socket.%s", hn);
92 if (conf.tmpdir_given)
93 tmpsocket_name = make_message("%s/audioc.sock.%s.%s",
94 conf.tmpdir_arg, hn, randname);
95 else
96 tmpsocket_name = make_message("%s/.paraslash/audioc_sock.%s.%s",
97 home, hn, randname);
98
99 ret = -E_SOCKET;
100 fd = create_pf_socket(tmpsocket_name, &unix_addr, S_IRUSR | S_IWUSR);
101 unlink(tmpsocket_name);
102 if (fd < 0)
103 goto out;
104 ret = -E_INIT_SOCK_ADDR;
105 if (init_unix_addr(&unix_addr, socket_name) < 0)
106 goto out;
107 ret = - E_CONNECT;
108 if (connect(fd, (struct sockaddr *)&unix_addr, UNIX_PATH_MAX) < 0)
109 goto out;
110 ret = -E_CREDENTIALS;
111 if (send_cred_buffer(fd, args) < 0)
112 goto out;
113 for (;;) {
114 int max_fileno = -1, check_write = 0;
115 ssize_t len;
116 fd_set rfd, wfd;
117 FD_ZERO(&rfd);
118 FD_ZERO(&wfd);
119 if (loaded && loaded > 10000)
120 fprintf(stderr, "loaded: %d\n", loaded);
121 if (loaded < conf.bufsize_arg) {
122 FD_SET(fd, &rfd);
123 max_fileno = MAX(max_fileno, fd);
124 }
125 if (loaded > 0) {
126 FD_SET(STDOUT_FILENO, &wfd);
127 max_fileno = MAX(max_fileno, STDOUT_FILENO);
128 check_write = 1;
129 }
130 ret = -E_OVERRUN;
131 if (max_fileno < 0)
132 goto out;
133 ret = select(max_fileno + 1, &rfd, &wfd, NULL, NULL);
134 if (ret < 0) {
135 ret = -E_SELECT;
136 goto out;
137 }
138 if (loaded < conf.bufsize_arg && FD_ISSET(fd, &rfd)) {
139 len = recv_bin_buffer(fd, buf + loaded,
140 conf.bufsize_arg - loaded);
141 if (len <= 0) {
142 ret = len < 0? -E_READ : 0;
143 goto out;
144 }
145 loaded += len;
146 }
147 if (check_write && FD_ISSET(STDOUT_FILENO, &wfd)) {
148 ret = write(STDOUT_FILENO, buf, loaded);
149 if (ret < 0) {
150 ret = -E_WRITE;
151 goto out;
152 }
153 loaded -= ret;
154 }
155 }
156 out:
157 if (!ret && loaded && buf)
158 ret = write(STDOUT_FILENO, buf, loaded);
159 return ret < 0? -ret : EXIT_SUCCESS;
160 }