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 * Copyright (c) Cyril Hrubis <[email protected]> 2012
5*49cdfc7eSAndroid Build Coastguard Worker *
6*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
7*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
8*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
9*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
12*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
15*49cdfc7eSAndroid Build Coastguard Worker *
16*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
17*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
18*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*49cdfc7eSAndroid Build Coastguard Worker */
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker /*
22*49cdfc7eSAndroid Build Coastguard Worker * Test Name: sendto01
23*49cdfc7eSAndroid Build Coastguard Worker *
24*49cdfc7eSAndroid Build Coastguard Worker * Test Description:
25*49cdfc7eSAndroid Build Coastguard Worker * Verify that sendto() returns the proper errno for various failure cases
26*49cdfc7eSAndroid Build Coastguard Worker *
27*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
28*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
29*49cdfc7eSAndroid Build Coastguard Worker */
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <sys/signal.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <sys/un.h>
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
44*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "sendto01";
47*49cdfc7eSAndroid Build Coastguard Worker int testno;
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker static char buf[1024], bigbuf[128 * 1024];
50*49cdfc7eSAndroid Build Coastguard Worker static int s;
51*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in sin1, sin2;
52*49cdfc7eSAndroid Build Coastguard Worker static int sfd;
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t { /* test case structure */
55*49cdfc7eSAndroid Build Coastguard Worker int domain; /* PF_INET, PF_UNIX, ... */
56*49cdfc7eSAndroid Build Coastguard Worker int type; /* SOCK_STREAM, SOCK_DGRAM ... */
57*49cdfc7eSAndroid Build Coastguard Worker int proto; /* protocol number (usually 0 = default) */
58*49cdfc7eSAndroid Build Coastguard Worker void *buf; /* send data buffer */
59*49cdfc7eSAndroid Build Coastguard Worker int buflen; /* send's 3rd argument */
60*49cdfc7eSAndroid Build Coastguard Worker unsigned flags; /* send's 4th argument */
61*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in *to; /* destination */
62*49cdfc7eSAndroid Build Coastguard Worker int tolen; /* length of "to" buffer */
63*49cdfc7eSAndroid Build Coastguard Worker int retval;
64*49cdfc7eSAndroid Build Coastguard Worker int experrno;
65*49cdfc7eSAndroid Build Coastguard Worker void (*setup) (void);
66*49cdfc7eSAndroid Build Coastguard Worker void (*cleanup) (void);
67*49cdfc7eSAndroid Build Coastguard Worker char *desc;
68*49cdfc7eSAndroid Build Coastguard Worker };
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
71*49cdfc7eSAndroid Build Coastguard Worker static void setup0(void);
72*49cdfc7eSAndroid Build Coastguard Worker static void setup1(void);
73*49cdfc7eSAndroid Build Coastguard Worker static void setup2(void);
74*49cdfc7eSAndroid Build Coastguard Worker static void setup3(void);
75*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
76*49cdfc7eSAndroid Build Coastguard Worker static void cleanup0(void);
77*49cdfc7eSAndroid Build Coastguard Worker static void cleanup1(void);
78*49cdfc7eSAndroid Build Coastguard Worker static void do_child(void);
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t tdat[] = {
81*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
82*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_STREAM,
83*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
84*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
85*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
86*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
87*49cdfc7eSAndroid Build Coastguard Worker .to = &sin1,
88*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
89*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
90*49cdfc7eSAndroid Build Coastguard Worker .experrno = EBADF,
91*49cdfc7eSAndroid Build Coastguard Worker .setup = setup0,
92*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup0,
93*49cdfc7eSAndroid Build Coastguard Worker .desc = "bad file descriptor"}
94*49cdfc7eSAndroid Build Coastguard Worker ,
95*49cdfc7eSAndroid Build Coastguard Worker {.domain = 0,
96*49cdfc7eSAndroid Build Coastguard Worker .type = 0,
97*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
98*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
99*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
100*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
101*49cdfc7eSAndroid Build Coastguard Worker .to = &sin1,
102*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
103*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
104*49cdfc7eSAndroid Build Coastguard Worker .experrno = ENOTSOCK,
105*49cdfc7eSAndroid Build Coastguard Worker .setup = setup0,
106*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup0,
107*49cdfc7eSAndroid Build Coastguard Worker .desc = "invalid socket"}
108*49cdfc7eSAndroid Build Coastguard Worker ,
109*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
110*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_DGRAM,
111*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
112*49cdfc7eSAndroid Build Coastguard Worker .buf = (void *)-1,
113*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
114*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
115*49cdfc7eSAndroid Build Coastguard Worker .to = &sin1,
116*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
117*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
118*49cdfc7eSAndroid Build Coastguard Worker .experrno = EFAULT,
119*49cdfc7eSAndroid Build Coastguard Worker .setup = setup1,
120*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
121*49cdfc7eSAndroid Build Coastguard Worker .desc = "invalid send buffer"}
122*49cdfc7eSAndroid Build Coastguard Worker ,
123*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
124*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_STREAM,
125*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
126*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
127*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
128*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
129*49cdfc7eSAndroid Build Coastguard Worker .to = &sin2,
130*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin2),
131*49cdfc7eSAndroid Build Coastguard Worker .retval = 0,
132*49cdfc7eSAndroid Build Coastguard Worker .experrno = EFAULT,
133*49cdfc7eSAndroid Build Coastguard Worker .setup = setup1,
134*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
135*49cdfc7eSAndroid Build Coastguard Worker .desc = "connected TCP"}
136*49cdfc7eSAndroid Build Coastguard Worker ,
137*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
138*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_STREAM,
139*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
140*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
141*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
142*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
143*49cdfc7eSAndroid Build Coastguard Worker .to = &sin1,
144*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
145*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
146*49cdfc7eSAndroid Build Coastguard Worker .experrno = EPIPE,
147*49cdfc7eSAndroid Build Coastguard Worker .setup = setup3,
148*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
149*49cdfc7eSAndroid Build Coastguard Worker .desc = "not connected TCP"}
150*49cdfc7eSAndroid Build Coastguard Worker ,
151*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
152*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_DGRAM,
153*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
154*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
155*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
156*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
157*49cdfc7eSAndroid Build Coastguard Worker .to = &sin1,
158*49cdfc7eSAndroid Build Coastguard Worker .tolen = -1,
159*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
160*49cdfc7eSAndroid Build Coastguard Worker .experrno = EINVAL,
161*49cdfc7eSAndroid Build Coastguard Worker .setup = setup1,
162*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
163*49cdfc7eSAndroid Build Coastguard Worker .desc = "invalid to buffer length"}
164*49cdfc7eSAndroid Build Coastguard Worker ,
165*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
166*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_DGRAM,
167*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
168*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
169*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
170*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
171*49cdfc7eSAndroid Build Coastguard Worker .to = (struct sockaddr_in *)-1,
172*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
173*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
174*49cdfc7eSAndroid Build Coastguard Worker .experrno = EFAULT,
175*49cdfc7eSAndroid Build Coastguard Worker .setup = setup1,
176*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
177*49cdfc7eSAndroid Build Coastguard Worker .desc = "invalid to buffer"}
178*49cdfc7eSAndroid Build Coastguard Worker ,
179*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
180*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_DGRAM,
181*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
182*49cdfc7eSAndroid Build Coastguard Worker .buf = bigbuf,
183*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(bigbuf),
184*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
185*49cdfc7eSAndroid Build Coastguard Worker .to = &sin1,
186*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
187*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
188*49cdfc7eSAndroid Build Coastguard Worker .experrno = EMSGSIZE,
189*49cdfc7eSAndroid Build Coastguard Worker .setup = setup1,
190*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
191*49cdfc7eSAndroid Build Coastguard Worker .desc = "UDP message too big"}
192*49cdfc7eSAndroid Build Coastguard Worker ,
193*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
194*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_STREAM,
195*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
196*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
197*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
198*49cdfc7eSAndroid Build Coastguard Worker .flags = 0,
199*49cdfc7eSAndroid Build Coastguard Worker .to = &sin1,
200*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
201*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
202*49cdfc7eSAndroid Build Coastguard Worker .experrno = EPIPE,
203*49cdfc7eSAndroid Build Coastguard Worker .setup = setup2,
204*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
205*49cdfc7eSAndroid Build Coastguard Worker .desc = "local endpoint shutdown"}
206*49cdfc7eSAndroid Build Coastguard Worker ,
207*49cdfc7eSAndroid Build Coastguard Worker {.domain = PF_INET,
208*49cdfc7eSAndroid Build Coastguard Worker .type = SOCK_DGRAM,
209*49cdfc7eSAndroid Build Coastguard Worker .proto = 0,
210*49cdfc7eSAndroid Build Coastguard Worker .buf = buf,
211*49cdfc7eSAndroid Build Coastguard Worker .buflen = sizeof(buf),
212*49cdfc7eSAndroid Build Coastguard Worker .flags = MSG_OOB,
213*49cdfc7eSAndroid Build Coastguard Worker .to = &sin1,
214*49cdfc7eSAndroid Build Coastguard Worker .tolen = sizeof(sin1),
215*49cdfc7eSAndroid Build Coastguard Worker .retval = -1,
216*49cdfc7eSAndroid Build Coastguard Worker .experrno = EOPNOTSUPP,
217*49cdfc7eSAndroid Build Coastguard Worker .setup = setup1,
218*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup1,
219*49cdfc7eSAndroid Build Coastguard Worker .desc = "invalid flags set"}
220*49cdfc7eSAndroid Build Coastguard Worker };
221*49cdfc7eSAndroid Build Coastguard Worker
222*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
223*49cdfc7eSAndroid Build Coastguard Worker
start_server(struct sockaddr_in * sin0)224*49cdfc7eSAndroid Build Coastguard Worker static pid_t start_server(struct sockaddr_in *sin0)
225*49cdfc7eSAndroid Build Coastguard Worker {
226*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
227*49cdfc7eSAndroid Build Coastguard Worker socklen_t slen = sizeof(*sin0);
228*49cdfc7eSAndroid Build Coastguard Worker
229*49cdfc7eSAndroid Build Coastguard Worker sin0->sin_family = AF_INET;
230*49cdfc7eSAndroid Build Coastguard Worker sin0->sin_port = 0; /* pick random free port */
231*49cdfc7eSAndroid Build Coastguard Worker sin0->sin_addr.s_addr = INADDR_ANY;
232*49cdfc7eSAndroid Build Coastguard Worker
233*49cdfc7eSAndroid Build Coastguard Worker sfd = socket(PF_INET, SOCK_STREAM, 0);
234*49cdfc7eSAndroid Build Coastguard Worker if (sfd < 0) {
235*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
236*49cdfc7eSAndroid Build Coastguard Worker return -1;
237*49cdfc7eSAndroid Build Coastguard Worker }
238*49cdfc7eSAndroid Build Coastguard Worker if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
239*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
240*49cdfc7eSAndroid Build Coastguard Worker return -1;
241*49cdfc7eSAndroid Build Coastguard Worker }
242*49cdfc7eSAndroid Build Coastguard Worker if (listen(sfd, 10) < 0) {
243*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
244*49cdfc7eSAndroid Build Coastguard Worker return -1;
245*49cdfc7eSAndroid Build Coastguard Worker }
246*49cdfc7eSAndroid Build Coastguard Worker SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
247*49cdfc7eSAndroid Build Coastguard Worker
248*49cdfc7eSAndroid Build Coastguard Worker switch ((pid = tst_fork())) {
249*49cdfc7eSAndroid Build Coastguard Worker case 0:
250*49cdfc7eSAndroid Build Coastguard Worker do_child();
251*49cdfc7eSAndroid Build Coastguard Worker break;
252*49cdfc7eSAndroid Build Coastguard Worker case -1:
253*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "server fork failed");
254*49cdfc7eSAndroid Build Coastguard Worker default:
255*49cdfc7eSAndroid Build Coastguard Worker (void)close(sfd);
256*49cdfc7eSAndroid Build Coastguard Worker return pid;
257*49cdfc7eSAndroid Build Coastguard Worker }
258*49cdfc7eSAndroid Build Coastguard Worker
259*49cdfc7eSAndroid Build Coastguard Worker exit(1);
260*49cdfc7eSAndroid Build Coastguard Worker }
261*49cdfc7eSAndroid Build Coastguard Worker
do_child(void)262*49cdfc7eSAndroid Build Coastguard Worker static void do_child(void)
263*49cdfc7eSAndroid Build Coastguard Worker {
264*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in fsin;
265*49cdfc7eSAndroid Build Coastguard Worker fd_set afds, rfds;
266*49cdfc7eSAndroid Build Coastguard Worker int nfds, cc, fd;
267*49cdfc7eSAndroid Build Coastguard Worker
268*49cdfc7eSAndroid Build Coastguard Worker FD_ZERO(&afds);
269*49cdfc7eSAndroid Build Coastguard Worker FD_SET(sfd, &afds);
270*49cdfc7eSAndroid Build Coastguard Worker
271*49cdfc7eSAndroid Build Coastguard Worker nfds = sfd + 1;
272*49cdfc7eSAndroid Build Coastguard Worker
273*49cdfc7eSAndroid Build Coastguard Worker /* accept connections until killed */
274*49cdfc7eSAndroid Build Coastguard Worker while (1) {
275*49cdfc7eSAndroid Build Coastguard Worker socklen_t fromlen;
276*49cdfc7eSAndroid Build Coastguard Worker
277*49cdfc7eSAndroid Build Coastguard Worker memcpy(&rfds, &afds, sizeof(rfds));
278*49cdfc7eSAndroid Build Coastguard Worker
279*49cdfc7eSAndroid Build Coastguard Worker if (select(nfds, &rfds, NULL, NULL, NULL) < 0 && errno != EINTR)
280*49cdfc7eSAndroid Build Coastguard Worker exit(1);
281*49cdfc7eSAndroid Build Coastguard Worker
282*49cdfc7eSAndroid Build Coastguard Worker if (FD_ISSET(sfd, &rfds)) {
283*49cdfc7eSAndroid Build Coastguard Worker int newfd;
284*49cdfc7eSAndroid Build Coastguard Worker
285*49cdfc7eSAndroid Build Coastguard Worker fromlen = sizeof(fsin);
286*49cdfc7eSAndroid Build Coastguard Worker newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen);
287*49cdfc7eSAndroid Build Coastguard Worker if (newfd >= 0) {
288*49cdfc7eSAndroid Build Coastguard Worker FD_SET(newfd, &afds);
289*49cdfc7eSAndroid Build Coastguard Worker nfds = MAX(nfds, newfd + 1);
290*49cdfc7eSAndroid Build Coastguard Worker }
291*49cdfc7eSAndroid Build Coastguard Worker }
292*49cdfc7eSAndroid Build Coastguard Worker for (fd = 0; fd < nfds; ++fd) {
293*49cdfc7eSAndroid Build Coastguard Worker if (fd != sfd && FD_ISSET(fd, &rfds)) {
294*49cdfc7eSAndroid Build Coastguard Worker cc = read(fd, buf, sizeof(buf));
295*49cdfc7eSAndroid Build Coastguard Worker if (cc == 0 || (cc < 0 && errno != EINTR)) {
296*49cdfc7eSAndroid Build Coastguard Worker (void)close(fd);
297*49cdfc7eSAndroid Build Coastguard Worker FD_CLR(fd, &afds);
298*49cdfc7eSAndroid Build Coastguard Worker }
299*49cdfc7eSAndroid Build Coastguard Worker }
300*49cdfc7eSAndroid Build Coastguard Worker }
301*49cdfc7eSAndroid Build Coastguard Worker }
302*49cdfc7eSAndroid Build Coastguard Worker }
303*49cdfc7eSAndroid Build Coastguard Worker
main(int ac,char * av[])304*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char *av[])
305*49cdfc7eSAndroid Build Coastguard Worker {
306*49cdfc7eSAndroid Build Coastguard Worker int lc;
307*49cdfc7eSAndroid Build Coastguard Worker
308*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(ac, av, NULL, NULL);
309*49cdfc7eSAndroid Build Coastguard Worker
310*49cdfc7eSAndroid Build Coastguard Worker setup();
311*49cdfc7eSAndroid Build Coastguard Worker
312*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); ++lc) {
313*49cdfc7eSAndroid Build Coastguard Worker
314*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
315*49cdfc7eSAndroid Build Coastguard Worker for (testno = 0; testno < TST_TOTAL; ++testno) {
316*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].setup();
317*49cdfc7eSAndroid Build Coastguard Worker
318*49cdfc7eSAndroid Build Coastguard Worker TEST(sendto(s, tdat[testno].buf, tdat[testno].buflen,
319*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].flags,
320*49cdfc7eSAndroid Build Coastguard Worker (const struct sockaddr *)tdat[testno].to,
321*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].tolen));
322*49cdfc7eSAndroid Build Coastguard Worker
323*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN > 0)
324*49cdfc7eSAndroid Build Coastguard Worker TEST_RETURN = 0;
325*49cdfc7eSAndroid Build Coastguard Worker
326*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN != tdat[testno].retval ||
327*49cdfc7eSAndroid Build Coastguard Worker (TEST_RETURN < 0 &&
328*49cdfc7eSAndroid Build Coastguard Worker TEST_ERRNO != tdat[testno].experrno)) {
329*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "%s ; returned"
330*49cdfc7eSAndroid Build Coastguard Worker " %ld (expected %d), errno %d (expected"
331*49cdfc7eSAndroid Build Coastguard Worker " %d)", tdat[testno].desc,
332*49cdfc7eSAndroid Build Coastguard Worker TEST_RETURN, tdat[testno].retval,
333*49cdfc7eSAndroid Build Coastguard Worker TEST_ERRNO, tdat[testno].experrno);
334*49cdfc7eSAndroid Build Coastguard Worker } else {
335*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "%s successful",
336*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].desc);
337*49cdfc7eSAndroid Build Coastguard Worker }
338*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].cleanup();
339*49cdfc7eSAndroid Build Coastguard Worker }
340*49cdfc7eSAndroid Build Coastguard Worker }
341*49cdfc7eSAndroid Build Coastguard Worker cleanup();
342*49cdfc7eSAndroid Build Coastguard Worker
343*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
344*49cdfc7eSAndroid Build Coastguard Worker }
345*49cdfc7eSAndroid Build Coastguard Worker
346*49cdfc7eSAndroid Build Coastguard Worker static pid_t server_pid;
347*49cdfc7eSAndroid Build Coastguard Worker
setup(void)348*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
349*49cdfc7eSAndroid Build Coastguard Worker {
350*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
351*49cdfc7eSAndroid Build Coastguard Worker
352*49cdfc7eSAndroid Build Coastguard Worker server_pid = start_server(&sin1);
353*49cdfc7eSAndroid Build Coastguard Worker
354*49cdfc7eSAndroid Build Coastguard Worker signal(SIGPIPE, SIG_IGN);
355*49cdfc7eSAndroid Build Coastguard Worker }
356*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)357*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
358*49cdfc7eSAndroid Build Coastguard Worker {
359*49cdfc7eSAndroid Build Coastguard Worker kill(server_pid, SIGKILL);
360*49cdfc7eSAndroid Build Coastguard Worker }
361*49cdfc7eSAndroid Build Coastguard Worker
setup0(void)362*49cdfc7eSAndroid Build Coastguard Worker static void setup0(void)
363*49cdfc7eSAndroid Build Coastguard Worker {
364*49cdfc7eSAndroid Build Coastguard Worker if (tdat[testno].experrno == EBADF)
365*49cdfc7eSAndroid Build Coastguard Worker s = 400;
366*49cdfc7eSAndroid Build Coastguard Worker else if ((s = open("/dev/null", O_WRONLY)) == -1)
367*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null) failed");
368*49cdfc7eSAndroid Build Coastguard Worker }
369*49cdfc7eSAndroid Build Coastguard Worker
cleanup0(void)370*49cdfc7eSAndroid Build Coastguard Worker static void cleanup0(void)
371*49cdfc7eSAndroid Build Coastguard Worker {
372*49cdfc7eSAndroid Build Coastguard Worker s = -1;
373*49cdfc7eSAndroid Build Coastguard Worker }
374*49cdfc7eSAndroid Build Coastguard Worker
setup1(void)375*49cdfc7eSAndroid Build Coastguard Worker static void setup1(void)
376*49cdfc7eSAndroid Build Coastguard Worker {
377*49cdfc7eSAndroid Build Coastguard Worker s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
378*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].proto);
379*49cdfc7eSAndroid Build Coastguard Worker SAFE_CONNECT(cleanup, s, (const struct sockaddr *)&sin1, sizeof(sin1));
380*49cdfc7eSAndroid Build Coastguard Worker }
381*49cdfc7eSAndroid Build Coastguard Worker
cleanup1(void)382*49cdfc7eSAndroid Build Coastguard Worker static void cleanup1(void)
383*49cdfc7eSAndroid Build Coastguard Worker {
384*49cdfc7eSAndroid Build Coastguard Worker (void)close(s);
385*49cdfc7eSAndroid Build Coastguard Worker s = -1;
386*49cdfc7eSAndroid Build Coastguard Worker }
387*49cdfc7eSAndroid Build Coastguard Worker
setup2(void)388*49cdfc7eSAndroid Build Coastguard Worker static void setup2(void)
389*49cdfc7eSAndroid Build Coastguard Worker {
390*49cdfc7eSAndroid Build Coastguard Worker setup1();
391*49cdfc7eSAndroid Build Coastguard Worker if (shutdown(s, 1) < 0)
392*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "socket setup failed connect "
393*49cdfc7eSAndroid Build Coastguard Worker "test %d", testno);
394*49cdfc7eSAndroid Build Coastguard Worker }
395*49cdfc7eSAndroid Build Coastguard Worker
setup3(void)396*49cdfc7eSAndroid Build Coastguard Worker static void setup3(void)
397*49cdfc7eSAndroid Build Coastguard Worker {
398*49cdfc7eSAndroid Build Coastguard Worker s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
399*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].proto);
400*49cdfc7eSAndroid Build Coastguard Worker }
401