1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker *
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker */
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker * Test Name: recvfrom01
22*49cdfc7eSAndroid Build Coastguard Worker *
23*49cdfc7eSAndroid Build Coastguard Worker * Test Description:
24*49cdfc7eSAndroid Build Coastguard Worker * Verify that recvfrom() returns the proper errno for various failure cases
25*49cdfc7eSAndroid Build Coastguard Worker *
26*49cdfc7eSAndroid Build Coastguard Worker * Usage: <for command-line>
27*49cdfc7eSAndroid Build Coastguard Worker * recvfrom01 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
28*49cdfc7eSAndroid Build Coastguard Worker * where, -c n : Run n copies concurrently.
29*49cdfc7eSAndroid Build Coastguard Worker * -e : Turn on errno logging.
30*49cdfc7eSAndroid Build Coastguard Worker * -i n : Execute test n times.
31*49cdfc7eSAndroid Build Coastguard Worker * -I x : Execute test for x seconds.
32*49cdfc7eSAndroid Build Coastguard Worker * -P x : Pause for x seconds between iterations.
33*49cdfc7eSAndroid Build Coastguard Worker * -t : Turn on syscall timing.
34*49cdfc7eSAndroid Build Coastguard Worker *
35*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
36*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
37*49cdfc7eSAndroid Build Coastguard Worker *
38*49cdfc7eSAndroid Build Coastguard Worker * RESTRICTIONS:
39*49cdfc7eSAndroid Build Coastguard Worker * None.
40*49cdfc7eSAndroid Build Coastguard Worker *
41*49cdfc7eSAndroid Build Coastguard Worker */
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
49*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
50*49cdfc7eSAndroid Build Coastguard Worker #include <sys/signal.h>
51*49cdfc7eSAndroid Build Coastguard Worker #include <sys/un.h>
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
56*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "recvfrom01";
59*49cdfc7eSAndroid Build Coastguard Worker int testno;
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker char buf[1024];
62*49cdfc7eSAndroid Build Coastguard Worker int s; /* socket descriptor */
63*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in sin1, from;
64*49cdfc7eSAndroid Build Coastguard Worker static int sfd; /* shared between do_child and start_server */
65*49cdfc7eSAndroid Build Coastguard Worker socklen_t fromlen;
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker void do_child(void);
68*49cdfc7eSAndroid Build Coastguard Worker void setup(void);
69*49cdfc7eSAndroid Build Coastguard Worker void setup0(void);
70*49cdfc7eSAndroid Build Coastguard Worker void setup1(void);
71*49cdfc7eSAndroid Build Coastguard Worker void setup2(void);
72*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void);
73*49cdfc7eSAndroid Build Coastguard Worker void cleanup0(void);
74*49cdfc7eSAndroid Build Coastguard Worker void cleanup1(void);
75*49cdfc7eSAndroid Build Coastguard Worker pid_t start_server(struct sockaddr_in *);
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t { /* test case structure */
78*49cdfc7eSAndroid Build Coastguard Worker int domain; /* PF_INET, PF_UNIX, ... */
79*49cdfc7eSAndroid Build Coastguard Worker int type; /* SOCK_STREAM, SOCK_DGRAM ... */
80*49cdfc7eSAndroid Build Coastguard Worker int proto; /* protocol number (usually 0 = default) */
81*49cdfc7eSAndroid Build Coastguard Worker void *buf; /* recv data buffer */
82*49cdfc7eSAndroid Build Coastguard Worker size_t buflen; /* recv's 3rd argument */
83*49cdfc7eSAndroid Build Coastguard Worker unsigned flags; /* recv's 4th argument */
84*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr *from; /* from address */
85*49cdfc7eSAndroid Build Coastguard Worker socklen_t *salen; /* from address value/result buffer length */
86*49cdfc7eSAndroid Build Coastguard Worker int retval; /* syscall return value */
87*49cdfc7eSAndroid Build Coastguard Worker int experrno; /* expected errno */
88*49cdfc7eSAndroid Build Coastguard Worker void (*setup) (void);
89*49cdfc7eSAndroid Build Coastguard Worker void (*cleanup) (void);
90*49cdfc7eSAndroid Build Coastguard Worker char *desc;
91*49cdfc7eSAndroid Build Coastguard Worker } tdat[] = {
92*49cdfc7eSAndroid Build Coastguard Worker /* 1 */
93*49cdfc7eSAndroid Build Coastguard Worker {
94*49cdfc7eSAndroid Build Coastguard Worker PF_INET, SOCK_STREAM, 0, buf, sizeof(buf), 0,
95*49cdfc7eSAndroid Build Coastguard Worker (struct sockaddr *)&from, &fromlen,
96*49cdfc7eSAndroid Build Coastguard Worker -1, EBADF, setup0, cleanup0, "bad file descriptor"},
97*49cdfc7eSAndroid Build Coastguard Worker /* 2 */
98*49cdfc7eSAndroid Build Coastguard Worker {
99*49cdfc7eSAndroid Build Coastguard Worker 0, 0, 0, buf, sizeof(buf), 0, (struct sockaddr *)&from,
100*49cdfc7eSAndroid Build Coastguard Worker &fromlen, -1, ENOTSOCK, setup0, cleanup0, "invalid socket"},
101*49cdfc7eSAndroid Build Coastguard Worker /* 3 */
102*49cdfc7eSAndroid Build Coastguard Worker {
103*49cdfc7eSAndroid Build Coastguard Worker PF_INET, SOCK_STREAM, 0, (void *)buf, sizeof(buf), 0,
104*49cdfc7eSAndroid Build Coastguard Worker (struct sockaddr *)-1, &fromlen,
105*49cdfc7eSAndroid Build Coastguard Worker 0, ENOTSOCK, setup1, cleanup1, "invalid socket buffer"},
106*49cdfc7eSAndroid Build Coastguard Worker /* 4 */
107*49cdfc7eSAndroid Build Coastguard Worker {
108*49cdfc7eSAndroid Build Coastguard Worker PF_INET, SOCK_STREAM, 0, (void *)buf, sizeof(buf), 0,
109*49cdfc7eSAndroid Build Coastguard Worker (struct sockaddr *)&from, &fromlen,
110*49cdfc7eSAndroid Build Coastguard Worker -1, EINVAL, setup2, cleanup1, "invalid socket addr length"},
111*49cdfc7eSAndroid Build Coastguard Worker /* 5 */
112*49cdfc7eSAndroid Build Coastguard Worker {
113*49cdfc7eSAndroid Build Coastguard Worker PF_INET, SOCK_STREAM, 0, (void *)-1, sizeof(buf), 0,
114*49cdfc7eSAndroid Build Coastguard Worker (struct sockaddr *)&from, &fromlen,
115*49cdfc7eSAndroid Build Coastguard Worker -1, EFAULT, setup1, cleanup1, "invalid recv buffer"},
116*49cdfc7eSAndroid Build Coastguard Worker /* 6 */
117*49cdfc7eSAndroid Build Coastguard Worker {
118*49cdfc7eSAndroid Build Coastguard Worker PF_INET, SOCK_STREAM, 0, (void *)buf, sizeof(buf), MSG_OOB,
119*49cdfc7eSAndroid Build Coastguard Worker (struct sockaddr *)&from, &fromlen,
120*49cdfc7eSAndroid Build Coastguard Worker -1, EINVAL, setup1, cleanup1, "invalid MSG_OOB flag set"},
121*49cdfc7eSAndroid Build Coastguard Worker /* 7 */
122*49cdfc7eSAndroid Build Coastguard Worker {
123*49cdfc7eSAndroid Build Coastguard Worker PF_INET, SOCK_STREAM, 0, (void *)buf, sizeof(buf), MSG_ERRQUEUE,
124*49cdfc7eSAndroid Build Coastguard Worker (struct sockaddr *)&from, &fromlen,
125*49cdfc7eSAndroid Build Coastguard Worker -1, EAGAIN, setup1, cleanup1, "invalid MSG_ERRQUEUE flag set"},};
126*49cdfc7eSAndroid Build Coastguard Worker
127*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
128*49cdfc7eSAndroid Build Coastguard Worker
main(int argc,char * argv[])129*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
130*49cdfc7eSAndroid Build Coastguard Worker {
131*49cdfc7eSAndroid Build Coastguard Worker int lc;
132*49cdfc7eSAndroid Build Coastguard Worker
133*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(argc, argv, NULL, NULL);
134*49cdfc7eSAndroid Build Coastguard Worker
135*49cdfc7eSAndroid Build Coastguard Worker setup();
136*49cdfc7eSAndroid Build Coastguard Worker
137*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); ++lc) {
138*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
139*49cdfc7eSAndroid Build Coastguard Worker for (testno = 0; testno < TST_TOTAL; ++testno) {
140*49cdfc7eSAndroid Build Coastguard Worker if ((tst_kvercmp(3, 17, 0) < 0)
141*49cdfc7eSAndroid Build Coastguard Worker && (tdat[testno].flags & MSG_ERRQUEUE)
142*49cdfc7eSAndroid Build Coastguard Worker && (tdat[testno].type & SOCK_STREAM)) {
143*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TCONF, "skip MSG_ERRQUEUE test, "
144*49cdfc7eSAndroid Build Coastguard Worker "it's supported from 3.17");
145*49cdfc7eSAndroid Build Coastguard Worker continue;
146*49cdfc7eSAndroid Build Coastguard Worker }
147*49cdfc7eSAndroid Build Coastguard Worker
148*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].setup();
149*49cdfc7eSAndroid Build Coastguard Worker TEST(recvfrom(s, tdat[testno].buf, tdat[testno].buflen,
150*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].flags, tdat[testno].from,
151*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].salen));
152*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN >= 0)
153*49cdfc7eSAndroid Build Coastguard Worker TEST_RETURN = 0; /* all nonzero equal here */
154*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN != tdat[testno].retval ||
155*49cdfc7eSAndroid Build Coastguard Worker (TEST_RETURN < 0 &&
156*49cdfc7eSAndroid Build Coastguard Worker TEST_ERRNO != tdat[testno].experrno)) {
157*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "%s ; returned"
158*49cdfc7eSAndroid Build Coastguard Worker " %ld (expected %d), errno %d (expected"
159*49cdfc7eSAndroid Build Coastguard Worker " %d)", tdat[testno].desc,
160*49cdfc7eSAndroid Build Coastguard Worker TEST_RETURN, tdat[testno].retval,
161*49cdfc7eSAndroid Build Coastguard Worker TEST_ERRNO, tdat[testno].experrno);
162*49cdfc7eSAndroid Build Coastguard Worker } else {
163*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "%s successful",
164*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].desc);
165*49cdfc7eSAndroid Build Coastguard Worker }
166*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].cleanup();
167*49cdfc7eSAndroid Build Coastguard Worker }
168*49cdfc7eSAndroid Build Coastguard Worker }
169*49cdfc7eSAndroid Build Coastguard Worker cleanup();
170*49cdfc7eSAndroid Build Coastguard Worker
171*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
172*49cdfc7eSAndroid Build Coastguard Worker }
173*49cdfc7eSAndroid Build Coastguard Worker
174*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
175*49cdfc7eSAndroid Build Coastguard Worker
setup(void)176*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
177*49cdfc7eSAndroid Build Coastguard Worker {
178*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
179*49cdfc7eSAndroid Build Coastguard Worker
180*49cdfc7eSAndroid Build Coastguard Worker pid = start_server(&sin1);
181*49cdfc7eSAndroid Build Coastguard Worker }
182*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)183*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
184*49cdfc7eSAndroid Build Coastguard Worker {
185*49cdfc7eSAndroid Build Coastguard Worker (void)kill(pid, SIGKILL);
186*49cdfc7eSAndroid Build Coastguard Worker
187*49cdfc7eSAndroid Build Coastguard Worker }
188*49cdfc7eSAndroid Build Coastguard Worker
setup0(void)189*49cdfc7eSAndroid Build Coastguard Worker void setup0(void)
190*49cdfc7eSAndroid Build Coastguard Worker {
191*49cdfc7eSAndroid Build Coastguard Worker if (tdat[testno].experrno == EBADF)
192*49cdfc7eSAndroid Build Coastguard Worker s = 400; /* anything not an open file */
193*49cdfc7eSAndroid Build Coastguard Worker else if ((s = open("/dev/null", O_WRONLY)) == -1)
194*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null) failed");
195*49cdfc7eSAndroid Build Coastguard Worker fromlen = sizeof(from);
196*49cdfc7eSAndroid Build Coastguard Worker }
197*49cdfc7eSAndroid Build Coastguard Worker
cleanup0(void)198*49cdfc7eSAndroid Build Coastguard Worker void cleanup0(void)
199*49cdfc7eSAndroid Build Coastguard Worker {
200*49cdfc7eSAndroid Build Coastguard Worker s = -1;
201*49cdfc7eSAndroid Build Coastguard Worker }
202*49cdfc7eSAndroid Build Coastguard Worker
setup1(void)203*49cdfc7eSAndroid Build Coastguard Worker void setup1(void)
204*49cdfc7eSAndroid Build Coastguard Worker {
205*49cdfc7eSAndroid Build Coastguard Worker fd_set rdfds;
206*49cdfc7eSAndroid Build Coastguard Worker struct timeval timeout;
207*49cdfc7eSAndroid Build Coastguard Worker int n;
208*49cdfc7eSAndroid Build Coastguard Worker
209*49cdfc7eSAndroid Build Coastguard Worker s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
210*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].proto);
211*49cdfc7eSAndroid Build Coastguard Worker if (tdat[testno].type == SOCK_STREAM &&
212*49cdfc7eSAndroid Build Coastguard Worker connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
213*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "connect failed");
214*49cdfc7eSAndroid Build Coastguard Worker }
215*49cdfc7eSAndroid Build Coastguard Worker /* Wait for something to be readable, else we won't detect EFAULT on recv */
216*49cdfc7eSAndroid Build Coastguard Worker FD_ZERO(&rdfds);
217*49cdfc7eSAndroid Build Coastguard Worker FD_SET(s, &rdfds);
218*49cdfc7eSAndroid Build Coastguard Worker timeout.tv_sec = 2;
219*49cdfc7eSAndroid Build Coastguard Worker timeout.tv_usec = 0;
220*49cdfc7eSAndroid Build Coastguard Worker n = select(s + 1, &rdfds, 0, 0, &timeout);
221*49cdfc7eSAndroid Build Coastguard Worker if (n != 1 || !FD_ISSET(s, &rdfds))
222*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup,
223*49cdfc7eSAndroid Build Coastguard Worker "client setup1 failed - no message ready in 2 sec");
224*49cdfc7eSAndroid Build Coastguard Worker fromlen = sizeof(from);
225*49cdfc7eSAndroid Build Coastguard Worker }
226*49cdfc7eSAndroid Build Coastguard Worker
setup2(void)227*49cdfc7eSAndroid Build Coastguard Worker void setup2(void)
228*49cdfc7eSAndroid Build Coastguard Worker {
229*49cdfc7eSAndroid Build Coastguard Worker setup1();
230*49cdfc7eSAndroid Build Coastguard Worker fromlen = -1;
231*49cdfc7eSAndroid Build Coastguard Worker }
232*49cdfc7eSAndroid Build Coastguard Worker
cleanup1(void)233*49cdfc7eSAndroid Build Coastguard Worker void cleanup1(void)
234*49cdfc7eSAndroid Build Coastguard Worker {
235*49cdfc7eSAndroid Build Coastguard Worker (void)close(s);
236*49cdfc7eSAndroid Build Coastguard Worker s = -1;
237*49cdfc7eSAndroid Build Coastguard Worker }
238*49cdfc7eSAndroid Build Coastguard Worker
start_server(struct sockaddr_in * sin0)239*49cdfc7eSAndroid Build Coastguard Worker pid_t start_server(struct sockaddr_in *sin0)
240*49cdfc7eSAndroid Build Coastguard Worker {
241*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
242*49cdfc7eSAndroid Build Coastguard Worker socklen_t slen = sizeof(*sin0);
243*49cdfc7eSAndroid Build Coastguard Worker
244*49cdfc7eSAndroid Build Coastguard Worker sin0->sin_family = AF_INET;
245*49cdfc7eSAndroid Build Coastguard Worker sin0->sin_port = 0; /* pick random free port */
246*49cdfc7eSAndroid Build Coastguard Worker sin0->sin_addr.s_addr = INADDR_ANY;
247*49cdfc7eSAndroid Build Coastguard Worker
248*49cdfc7eSAndroid Build Coastguard Worker sfd = socket(PF_INET, SOCK_STREAM, 0);
249*49cdfc7eSAndroid Build Coastguard Worker if (sfd < 0) {
250*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
251*49cdfc7eSAndroid Build Coastguard Worker return -1;
252*49cdfc7eSAndroid Build Coastguard Worker }
253*49cdfc7eSAndroid Build Coastguard Worker if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
254*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
255*49cdfc7eSAndroid Build Coastguard Worker return -1;
256*49cdfc7eSAndroid Build Coastguard Worker }
257*49cdfc7eSAndroid Build Coastguard Worker if (listen(sfd, 10) < 0) {
258*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
259*49cdfc7eSAndroid Build Coastguard Worker return -1;
260*49cdfc7eSAndroid Build Coastguard Worker }
261*49cdfc7eSAndroid Build Coastguard Worker SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
262*49cdfc7eSAndroid Build Coastguard Worker
263*49cdfc7eSAndroid Build Coastguard Worker switch ((pid = tst_fork())) {
264*49cdfc7eSAndroid Build Coastguard Worker case 0: /* child */
265*49cdfc7eSAndroid Build Coastguard Worker do_child();
266*49cdfc7eSAndroid Build Coastguard Worker break;
267*49cdfc7eSAndroid Build Coastguard Worker case -1:
268*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "server fork failed");
269*49cdfc7eSAndroid Build Coastguard Worker /* fall through */
270*49cdfc7eSAndroid Build Coastguard Worker default: /* parent */
271*49cdfc7eSAndroid Build Coastguard Worker (void)close(sfd);
272*49cdfc7eSAndroid Build Coastguard Worker return pid;
273*49cdfc7eSAndroid Build Coastguard Worker }
274*49cdfc7eSAndroid Build Coastguard Worker
275*49cdfc7eSAndroid Build Coastguard Worker exit(1);
276*49cdfc7eSAndroid Build Coastguard Worker }
277*49cdfc7eSAndroid Build Coastguard Worker
do_child(void)278*49cdfc7eSAndroid Build Coastguard Worker void do_child(void)
279*49cdfc7eSAndroid Build Coastguard Worker {
280*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in fsin;
281*49cdfc7eSAndroid Build Coastguard Worker fd_set afds, rfds;
282*49cdfc7eSAndroid Build Coastguard Worker int nfds, cc, fd;
283*49cdfc7eSAndroid Build Coastguard Worker
284*49cdfc7eSAndroid Build Coastguard Worker FD_ZERO(&afds);
285*49cdfc7eSAndroid Build Coastguard Worker FD_SET(sfd, &afds);
286*49cdfc7eSAndroid Build Coastguard Worker
287*49cdfc7eSAndroid Build Coastguard Worker nfds = sfd + 1;
288*49cdfc7eSAndroid Build Coastguard Worker
289*49cdfc7eSAndroid Build Coastguard Worker /* accept connections until killed */
290*49cdfc7eSAndroid Build Coastguard Worker while (1) {
291*49cdfc7eSAndroid Build Coastguard Worker socklen_t fromlen;
292*49cdfc7eSAndroid Build Coastguard Worker
293*49cdfc7eSAndroid Build Coastguard Worker memcpy(&rfds, &afds, sizeof(rfds));
294*49cdfc7eSAndroid Build Coastguard Worker
295*49cdfc7eSAndroid Build Coastguard Worker if (select(nfds, &rfds, NULL, NULL,
296*49cdfc7eSAndroid Build Coastguard Worker NULL) < 0)
297*49cdfc7eSAndroid Build Coastguard Worker if (errno != EINTR)
298*49cdfc7eSAndroid Build Coastguard Worker exit(1);
299*49cdfc7eSAndroid Build Coastguard Worker if (FD_ISSET(sfd, &rfds)) {
300*49cdfc7eSAndroid Build Coastguard Worker int newfd;
301*49cdfc7eSAndroid Build Coastguard Worker
302*49cdfc7eSAndroid Build Coastguard Worker fromlen = sizeof(fsin);
303*49cdfc7eSAndroid Build Coastguard Worker newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen);
304*49cdfc7eSAndroid Build Coastguard Worker if (newfd >= 0) {
305*49cdfc7eSAndroid Build Coastguard Worker FD_SET(newfd, &afds);
306*49cdfc7eSAndroid Build Coastguard Worker nfds = MAX(nfds, newfd + 1);
307*49cdfc7eSAndroid Build Coastguard Worker /* send something back */
308*49cdfc7eSAndroid Build Coastguard Worker (void)write(newfd, "hoser\n", 6);
309*49cdfc7eSAndroid Build Coastguard Worker }
310*49cdfc7eSAndroid Build Coastguard Worker }
311*49cdfc7eSAndroid Build Coastguard Worker for (fd = 0; fd < nfds; ++fd)
312*49cdfc7eSAndroid Build Coastguard Worker if (fd != sfd && FD_ISSET(fd, &rfds)) {
313*49cdfc7eSAndroid Build Coastguard Worker cc = read(fd, buf, sizeof(buf));
314*49cdfc7eSAndroid Build Coastguard Worker if (cc == 0 || (cc < 0 && errno != EINTR)) {
315*49cdfc7eSAndroid Build Coastguard Worker (void)close(fd);
316*49cdfc7eSAndroid Build Coastguard Worker FD_CLR(fd, &afds);
317*49cdfc7eSAndroid Build Coastguard Worker }
318*49cdfc7eSAndroid Build Coastguard Worker }
319*49cdfc7eSAndroid Build Coastguard Worker }
320*49cdfc7eSAndroid Build Coastguard Worker }
321