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