Make crypo a per fd feature
[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 static char *concat_args(const int argc, char * const *argv)
50 {
51 int i; char *buf = NULL;
52 for (i = 0; i < argc; i++) {
53 buf = para_strcat(buf, argv[i]);
54 if (i != argc - 1)
55 buf = para_strcat(buf, "\n");
56 }
57 return buf;
58 }
59
60 static char *configfile_exists(void)
61 {
62 static char *config_file;
63 struct stat statbuf;
64
65
66 if (!config_file) {
67 char *home = para_homedir();
68 config_file = make_message("%s/.paraslash/audioc.conf", home);
69 free(home);
70 }
71 if (!stat(config_file, &statbuf))
72 return config_file;
73 return NULL;
74 }
75
76 int main(int argc, char *argv[])
77 {
78 struct sockaddr_un unix_addr;
79 int ret = -E_AUDIOC_SYNTAX, fd, loaded = 0;
80 char *cf, *socket_name, *randname = para_tmpname(), *tmpsocket_name = NULL,
81 *buf = NULL, *hn = para_hostname(), *args, *home = para_homedir();
82
83
84 if (cmdline_parser(argc, argv, &conf))
85 goto out;
86 cf = configfile_exists();
87 if (cf) {
88 if (cmdline_parser_configfile(cf, &conf, 0, 0, 0)) {
89 fprintf(stderr, "parse error in config file\n");
90 exit(EXIT_FAILURE);
91 }
92 }
93 args = conf.inputs_num?
94 concat_args(conf.inputs_num, conf.inputs) :
95 para_strdup("stat");
96 buf = para_malloc(conf.bufsize_arg);
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_pf_socket(tmpsocket_name, &unix_addr, S_IRUSR | S_IWUSR);
110 unlink(tmpsocket_name);
111 if (ret < 0)
112 goto out;
113 fd = ret;
114 ret = -E_INIT_SOCK_ADDR;
115 if (init_unix_addr(&unix_addr, socket_name) < 0)
116 goto out;
117 ret = - E_AUDIOC_CONNECT;
118 if (connect(fd, (struct sockaddr *)&unix_addr, UNIX_PATH_MAX) < 0)
119 goto out;
120 fprintf(stderr, "loaded: %d\n", loaded);
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 && loaded > 10000)
131 fprintf(stderr, "loaded: %d\n", loaded);
132 if (loaded < conf.bufsize_arg)
133 para_fd_set(fd, &rfd, &max_fileno);
134 if (loaded > 0) {
135 para_fd_set(STDOUT_FILENO, &wfd, &max_fileno);
136 check_write = 1;
137 }
138 ret = -E_AUDIOC_OVERRUN;
139 if (max_fileno < 0)
140 goto out;
141 ret = para_select(max_fileno + 1, &rfd, &wfd, NULL);
142 if (ret < 0)
143 goto out;
144 if (loaded < conf.bufsize_arg && FD_ISSET(fd, &rfd)) {
145 len = recv_bin_buffer(fd, buf + loaded,
146 conf.bufsize_arg - loaded);
147 if (len <= 0) {
148 ret = len < 0? -E_AUDIOC_READ : 0;
149 goto out;
150 }
151 loaded += len;
152 }
153 if (check_write && FD_ISSET(STDOUT_FILENO, &wfd)) {
154 ret = write(STDOUT_FILENO, buf, loaded);
155 if (ret < 0) {
156 ret = -E_AUDIOC_WRITE;
157 goto out;
158 }
159 loaded -= ret;
160 }
161 }
162 out:
163 if (!ret && loaded && buf)
164 ret = write(STDOUT_FILENO, buf, loaded);
165 if (ret < 0)
166 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
167 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
168 }