12
|
1 |
/*
|
|
2 |
stdin mix - a mixer/multiplexer for stdin to processes
|
|
3 |
Copyright (C) 2007 LluĂs Batlle i Rossell
|
|
4 |
|
|
5 |
Please find the license in the provided COPYING file.
|
|
6 |
*/
|
0
|
7 |
#include <stdio.h>
|
|
8 |
#include <stdlib.h>
|
|
9 |
#include <sys/types.h>
|
|
10 |
#include <unistd.h>
|
5
|
11 |
#include <errno.h>
|
0
|
12 |
|
3
|
13 |
#include "main.h"
|
0
|
14 |
|
14
|
15 |
static const char version[] = "0.9.1";
|
12
|
16 |
|
0
|
17 |
static int max(int a, int b)
|
|
18 |
{
|
|
19 |
if (a > b)
|
|
20 |
return a;
|
|
21 |
return b;
|
|
22 |
}
|
|
23 |
|
15
|
24 |
static int fork_app(int *opipe /*[3]*/, char * const command[])
|
0
|
25 |
{
|
|
26 |
int p_input[2]; /* from mpg321 to us */
|
|
27 |
int p_output[2]; /* from us to mpg321 */
|
15
|
28 |
int p_error[2]; /* from mpg321 to us, its stderr */
|
0
|
29 |
int pid;
|
|
30 |
int res;
|
|
31 |
|
|
32 |
pipe(p_input);
|
|
33 |
pipe(p_output);
|
15
|
34 |
pipe(p_error);
|
0
|
35 |
opipe[0] = p_input[0]; /* For us to read */
|
|
36 |
opipe[1] = p_output[1]; /* For us to write */
|
15
|
37 |
opipe[2] = p_error[0]; /* For us to read */
|
0
|
38 |
|
|
39 |
pid = fork();
|
|
40 |
|
|
41 |
switch(pid)
|
|
42 |
{
|
|
43 |
case 0: /* child */
|
|
44 |
close(p_input[0]);
|
|
45 |
res = dup2(p_input[1], 1);
|
|
46 |
if (res == -1) perror("Dup2 1");
|
15
|
47 |
close(p_error[0]);
|
|
48 |
res = dup2(p_error[1], 2);
|
0
|
49 |
if (res == -1) perror("Dup2 2");
|
|
50 |
close(p_input[1]);
|
15
|
51 |
close(p_error[1]);
|
|
52 |
|
0
|
53 |
close(p_output[1]);
|
|
54 |
res = dup2(p_output[0], 0);
|
|
55 |
if (res == -1) perror("Dup2 3");
|
|
56 |
close(p_output[0]);
|
|
57 |
|
|
58 |
execvp(command[0], command);
|
|
59 |
|
14
|
60 |
error("Cannot execlp %s", command[0]);
|
0
|
61 |
case -1:
|
14
|
62 |
error("Failed fork");
|
0
|
63 |
default: /* parent */
|
|
64 |
close(p_input[1]);
|
15
|
65 |
close(p_error[1]);
|
0
|
66 |
close(p_output[0]);
|
|
67 |
}
|
7
|
68 |
|
|
69 |
return pid;
|
0
|
70 |
}
|
|
71 |
|
9
|
72 |
static int forward_app_data(int in, int out)
|
0
|
73 |
{
|
|
74 |
char buf[100];
|
|
75 |
int res;
|
|
76 |
|
|
77 |
res = read(in, buf, sizeof(buf));
|
|
78 |
if (res > 0)
|
|
79 |
write(out, buf, res);
|
|
80 |
|
|
81 |
return res;
|
|
82 |
}
|
|
83 |
|
9
|
84 |
static void loop(const int *child_pipe, int lsocket)
|
0
|
85 |
{
|
|
86 |
char buf[100];
|
|
87 |
fd_set read_set;
|
15
|
88 |
int child_read, child_write, child_read_error;
|
0
|
89 |
int maxfd;
|
|
90 |
int opened_socket;
|
|
91 |
int stdin_opened;
|
15
|
92 |
int child_read_opened, child_read_error_opened;
|
5
|
93 |
int res;
|
0
|
94 |
|
|
95 |
child_read = child_pipe[0];
|
15
|
96 |
child_read_error = child_pipe[2];
|
0
|
97 |
child_write = child_pipe[1];
|
|
98 |
|
|
99 |
stdin_opened = 1;
|
1
|
100 |
opened_socket = -1; /* no socket opened */
|
15
|
101 |
child_read_opened = 1;
|
|
102 |
child_read_error_opened = 1;
|
0
|
103 |
do
|
|
104 |
{
|
|
105 |
FD_ZERO(&read_set);
|
15
|
106 |
maxfd = 0;
|
0
|
107 |
|
|
108 |
if (stdin_opened)
|
|
109 |
FD_SET(0, &read_set);
|
15
|
110 |
|
|
111 |
if (child_read_opened)
|
|
112 |
{
|
|
113 |
FD_SET(child_read, &read_set);
|
|
114 |
maxfd = max(maxfd, child_read);
|
|
115 |
}
|
|
116 |
|
|
117 |
if (child_read_error_opened)
|
|
118 |
{
|
|
119 |
FD_SET(child_read_error, &read_set);
|
|
120 |
maxfd = max(maxfd, child_read_error);
|
|
121 |
}
|
|
122 |
|
1
|
123 |
if (opened_socket >= 0)
|
|
124 |
{
|
|
125 |
FD_SET(opened_socket, &read_set);
|
|
126 |
maxfd = max(maxfd, opened_socket);
|
|
127 |
}
|
|
128 |
else
|
|
129 |
{
|
|
130 |
/* We only accept if we don't have any
|
|
131 |
* connetion opened. */
|
|
132 |
FD_SET(lsocket, &read_set);
|
|
133 |
maxfd = max(maxfd, lsocket);
|
|
134 |
}
|
0
|
135 |
|
1
|
136 |
/* Will block */
|
5
|
137 |
res = select(maxfd + 1, &read_set, 0, 0, 0);
|
|
138 |
if (res == -1)
|
|
139 |
{
|
|
140 |
if (errno == EINTR)
|
|
141 |
continue;
|
|
142 |
else
|
|
143 |
error("Error in select()");
|
|
144 |
}
|
0
|
145 |
|
15
|
146 |
if (child_read_opened && FD_ISSET(child_read, &read_set))
|
0
|
147 |
{
|
|
148 |
res = forward_app_data(child_read, 1);
|
|
149 |
if (res == 0)
|
15
|
150 |
{
|
|
151 |
close(1);
|
|
152 |
child_read_opened = 0;
|
|
153 |
if (child_read_error_opened == 0)
|
|
154 |
break;
|
|
155 |
}
|
|
156 |
}
|
|
157 |
if (child_read_error_opened && FD_ISSET(child_read_error, &read_set))
|
|
158 |
{
|
|
159 |
res = forward_app_data(child_read_error, 2);
|
|
160 |
if (res == 0)
|
|
161 |
{
|
|
162 |
close(2);
|
|
163 |
child_read_error_opened = 0;
|
|
164 |
if (child_read_opened == 0)
|
|
165 |
break;
|
|
166 |
}
|
0
|
167 |
}
|
|
168 |
if (FD_ISSET(0, &read_set))
|
|
169 |
{
|
|
170 |
res = forward_app_data(0, child_write);
|
|
171 |
if (res == 0)
|
|
172 |
{
|
|
173 |
close(child_write);
|
|
174 |
stdin_opened = 0;
|
|
175 |
}
|
|
176 |
}
|
1
|
177 |
if (opened_socket >= 0 && FD_ISSET(opened_socket, &read_set))
|
|
178 |
{
|
|
179 |
res = forward_app_data(opened_socket, child_write);
|
|
180 |
if (res == 0)
|
|
181 |
{
|
|
182 |
close(opened_socket);
|
|
183 |
opened_socket = -1; /* no socket open */
|
|
184 |
}
|
|
185 |
}
|
|
186 |
if (opened_socket == -1 && FD_ISSET(lsocket, &read_set))
|
|
187 |
{
|
|
188 |
opened_socket = accept_connection(lsocket);
|
|
189 |
}
|
0
|
190 |
} while(1);
|
|
191 |
}
|
|
192 |
|
9
|
193 |
static int server(int argn, char * const argv[])
|
0
|
194 |
{
|
15
|
195 |
int p[3];
|
0
|
196 |
int lsocket;
|
7
|
197 |
int child;
|
0
|
198 |
|
7
|
199 |
child = fork_app(p, &argv[1]);
|
|
200 |
|
|
201 |
install_signal_forwarders(child);
|
0
|
202 |
|
|
203 |
lsocket = serve_socket();
|
|
204 |
|
|
205 |
loop(p, lsocket);
|
|
206 |
|
|
207 |
remove_socket(lsocket);
|
|
208 |
|
|
209 |
return 0;
|
|
210 |
}
|
1
|
211 |
|
9
|
212 |
static int client()
|
1
|
213 |
{
|
|
214 |
int cs;
|
|
215 |
int res;
|
5
|
216 |
fd_set read_set;
|
|
217 |
int maxfd;
|
|
218 |
|
1
|
219 |
cs = connect_socket();
|
|
220 |
|
|
221 |
do
|
|
222 |
{
|
5
|
223 |
FD_ZERO(&read_set);
|
|
224 |
|
|
225 |
FD_SET(cs, &read_set); /* For reading other side's close() */
|
|
226 |
maxfd = cs;
|
|
227 |
FD_SET(0, &read_set); /* stdin */
|
|
228 |
maxfd = max(maxfd, cs);
|
|
229 |
|
|
230 |
res = select(maxfd + 1, &read_set, 0, 0, 0);
|
|
231 |
if (res == -1)
|
|
232 |
{
|
|
233 |
if (errno == EINTR)
|
|
234 |
continue;
|
|
235 |
else
|
|
236 |
error("Error in select()");
|
|
237 |
}
|
|
238 |
|
|
239 |
if (FD_ISSET(cs, &read_set))
|
|
240 |
{
|
|
241 |
/* assuming close() on the other side, even
|
|
242 |
* without read(). */
|
|
243 |
break;
|
|
244 |
}
|
|
245 |
if (FD_ISSET(0, &read_set))
|
|
246 |
{
|
|
247 |
res = forward_app_data(0, cs);
|
|
248 |
if (res == 0) /* EOF */
|
|
249 |
break;
|
|
250 |
}
|
|
251 |
} while (1);
|
1
|
252 |
|
|
253 |
close(cs);
|
|
254 |
return 0;
|
|
255 |
}
|
|
256 |
|
6
|
257 |
static int showhelp(const char *pname)
|
|
258 |
{
|
12
|
259 |
printf("sdtdin-mix v%s - Copyright (C) 2007 Lluis Batlle i Rossell\n",
|
|
260 |
version);
|
6
|
261 |
printf("usage: %s [appcommand] [param1] [param2] ...\n", pname);
|
|
262 |
printf(" If you give _appcommand_, it starts the application and\n");
|
|
263 |
printf(" a stdin server on $SM_SOCKET or /tmp/socket-sm.UID.\n");
|
|
264 |
printf(" If not given, it starts a stdin client for the same socket.\n");
|
|
265 |
return 0;
|
|
266 |
}
|
|
267 |
|
9
|
268 |
int main(int argn, char * const * argv)
|
1
|
269 |
{
|
|
270 |
int res;
|
6
|
271 |
if (argn > 1 && strcmp(argv[1], "-h") == 0)
|
|
272 |
res = showhelp(argv[0]);
|
|
273 |
else if (argn > 1)
|
1
|
274 |
res = server(argn, argv);
|
|
275 |
else
|
|
276 |
res = client();
|
|
277 |
return res;
|
|
278 |
}
|