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