xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/send/send01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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: send01
23*49cdfc7eSAndroid Build Coastguard Worker  *
24*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
25*49cdfc7eSAndroid Build Coastguard Worker  *  Verify that send() 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 
32*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <sys/signal.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <sys/un.h>
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
45*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "send01";
48*49cdfc7eSAndroid Build Coastguard Worker int testno;
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker static char buf[1024], bigbuf[128 * 1024];
51*49cdfc7eSAndroid Build Coastguard Worker static int s;
52*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in sin1;
53*49cdfc7eSAndroid Build Coastguard Worker static int sfd;			/* shared between do_child and start_server */
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t {		/* test case structure */
56*49cdfc7eSAndroid Build Coastguard Worker 	int domain;		/* PF_INET, PF_UNIX, ... */
57*49cdfc7eSAndroid Build Coastguard Worker 	int type;		/* SOCK_STREAM, SOCK_DGRAM ... */
58*49cdfc7eSAndroid Build Coastguard Worker 	int proto;		/* protocol number (usually 0 = default) */
59*49cdfc7eSAndroid Build Coastguard Worker 	void *buf;		/* send data buffer */
60*49cdfc7eSAndroid Build Coastguard Worker 	int buflen;		/* send's 3rd argument */
61*49cdfc7eSAndroid Build Coastguard Worker 	unsigned flags;		/* send's 4th argument */
62*49cdfc7eSAndroid Build Coastguard Worker 	int retval;
63*49cdfc7eSAndroid Build Coastguard Worker 	int experrno;
64*49cdfc7eSAndroid Build Coastguard Worker 	void (*setup) (void);
65*49cdfc7eSAndroid Build Coastguard Worker 	void (*cleanup) (void);
66*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
67*49cdfc7eSAndroid Build Coastguard Worker };
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
70*49cdfc7eSAndroid Build Coastguard Worker static void do_child(void);
71*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
72*49cdfc7eSAndroid Build Coastguard Worker static void setup0(void);
73*49cdfc7eSAndroid Build Coastguard Worker static void setup1(void);
74*49cdfc7eSAndroid Build Coastguard Worker static void setup2(void);
75*49cdfc7eSAndroid Build Coastguard Worker static void cleanup0(void);
76*49cdfc7eSAndroid Build Coastguard Worker static void cleanup1(void);
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t tdat[] = {
79*49cdfc7eSAndroid Build Coastguard Worker 	{.domain = PF_INET,
80*49cdfc7eSAndroid Build Coastguard Worker 	 .type = SOCK_STREAM,
81*49cdfc7eSAndroid Build Coastguard Worker 	 .proto = 0,
82*49cdfc7eSAndroid Build Coastguard Worker 	 .buf = buf,
83*49cdfc7eSAndroid Build Coastguard Worker 	 .buflen = sizeof(buf),
84*49cdfc7eSAndroid Build Coastguard Worker 	 .flags = 0,
85*49cdfc7eSAndroid Build Coastguard Worker 	 .retval = -1,
86*49cdfc7eSAndroid Build Coastguard Worker 	 .experrno = EBADF,
87*49cdfc7eSAndroid Build Coastguard Worker 	 .setup = setup0,
88*49cdfc7eSAndroid Build Coastguard Worker 	 .cleanup = cleanup0,
89*49cdfc7eSAndroid Build Coastguard Worker 	 .desc = "bad file descriptor"}
90*49cdfc7eSAndroid Build Coastguard Worker 	,
91*49cdfc7eSAndroid Build Coastguard Worker 	{.domain = 0,
92*49cdfc7eSAndroid Build Coastguard Worker 	 .type = 0,
93*49cdfc7eSAndroid Build Coastguard Worker 	 .proto = 0,
94*49cdfc7eSAndroid Build Coastguard Worker 	 .buf = buf,
95*49cdfc7eSAndroid Build Coastguard Worker 	 .buflen = sizeof(buf),
96*49cdfc7eSAndroid Build Coastguard Worker 	 .flags = 0,
97*49cdfc7eSAndroid Build Coastguard Worker 	 .retval = -1,
98*49cdfc7eSAndroid Build Coastguard Worker 	 .experrno = ENOTSOCK,
99*49cdfc7eSAndroid Build Coastguard Worker 	 .setup = setup0,
100*49cdfc7eSAndroid Build Coastguard Worker 	 .cleanup = cleanup0,
101*49cdfc7eSAndroid Build Coastguard Worker 	 .desc = "invalid socket"}
102*49cdfc7eSAndroid Build Coastguard Worker 	,
103*49cdfc7eSAndroid Build Coastguard Worker 	{.domain = PF_INET,
104*49cdfc7eSAndroid Build Coastguard Worker 	 .type = SOCK_STREAM,
105*49cdfc7eSAndroid Build Coastguard Worker 	 .proto = 0,
106*49cdfc7eSAndroid Build Coastguard Worker 	 .buf = (void *)-1,
107*49cdfc7eSAndroid Build Coastguard Worker 	 .buflen = sizeof(buf),
108*49cdfc7eSAndroid Build Coastguard Worker 	 .flags = 0,
109*49cdfc7eSAndroid Build Coastguard Worker 	 .retval = -1,
110*49cdfc7eSAndroid Build Coastguard Worker 	 .experrno = EFAULT,
111*49cdfc7eSAndroid Build Coastguard Worker 	 .setup = setup1,
112*49cdfc7eSAndroid Build Coastguard Worker 	 .cleanup = cleanup1,
113*49cdfc7eSAndroid Build Coastguard Worker 	 .desc = "invalid send buffer"}
114*49cdfc7eSAndroid Build Coastguard Worker 	,
115*49cdfc7eSAndroid Build Coastguard Worker 	{.domain = PF_INET,
116*49cdfc7eSAndroid Build Coastguard Worker 	 .type = SOCK_DGRAM,
117*49cdfc7eSAndroid Build Coastguard Worker 	 .proto = 0,
118*49cdfc7eSAndroid Build Coastguard Worker 	 .buf = bigbuf,
119*49cdfc7eSAndroid Build Coastguard Worker 	 .buflen = sizeof(bigbuf),
120*49cdfc7eSAndroid Build Coastguard Worker 	 .flags = 0,
121*49cdfc7eSAndroid Build Coastguard Worker 	 .retval = -1,
122*49cdfc7eSAndroid Build Coastguard Worker 	 .experrno = EMSGSIZE,
123*49cdfc7eSAndroid Build Coastguard Worker 	 .setup = setup1,
124*49cdfc7eSAndroid Build Coastguard Worker 	 .cleanup = cleanup1,
125*49cdfc7eSAndroid Build Coastguard Worker 	 .desc = "UDP message too big"}
126*49cdfc7eSAndroid Build Coastguard Worker 	,
127*49cdfc7eSAndroid Build Coastguard Worker 	{.domain = PF_INET,
128*49cdfc7eSAndroid Build Coastguard Worker 	 .type = SOCK_STREAM,
129*49cdfc7eSAndroid Build Coastguard Worker 	 .proto = 0,
130*49cdfc7eSAndroid Build Coastguard Worker 	 .buf = buf,
131*49cdfc7eSAndroid Build Coastguard Worker 	 .buflen = sizeof(buf),
132*49cdfc7eSAndroid Build Coastguard Worker 	 .flags = 0,
133*49cdfc7eSAndroid Build Coastguard Worker 	 .retval = -1,
134*49cdfc7eSAndroid Build Coastguard Worker 	 .experrno = EPIPE,
135*49cdfc7eSAndroid Build Coastguard Worker 	 .setup = setup2,
136*49cdfc7eSAndroid Build Coastguard Worker 	 .cleanup = cleanup1,
137*49cdfc7eSAndroid Build Coastguard Worker 	 .desc = "local endpoint shutdown"}
138*49cdfc7eSAndroid Build Coastguard Worker 	,
139*49cdfc7eSAndroid Build Coastguard Worker 	{.domain = PF_INET,
140*49cdfc7eSAndroid Build Coastguard Worker 	 .type = SOCK_DGRAM,
141*49cdfc7eSAndroid Build Coastguard Worker 	 .proto = 0,
142*49cdfc7eSAndroid Build Coastguard Worker 	 .buf = buf,
143*49cdfc7eSAndroid Build Coastguard Worker 	 .buflen = sizeof(buf),
144*49cdfc7eSAndroid Build Coastguard Worker 	 .flags = MSG_OOB,
145*49cdfc7eSAndroid Build Coastguard Worker 	 .retval = -1,
146*49cdfc7eSAndroid Build Coastguard Worker 	 .experrno = EOPNOTSUPP,
147*49cdfc7eSAndroid Build Coastguard Worker 	 .setup = setup1,
148*49cdfc7eSAndroid Build Coastguard Worker 	 .cleanup = cleanup1,
149*49cdfc7eSAndroid Build Coastguard Worker 	 .desc = "invalid flags set"}
150*49cdfc7eSAndroid Build Coastguard Worker };
151*49cdfc7eSAndroid Build Coastguard Worker 
152*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
153*49cdfc7eSAndroid Build Coastguard Worker 
start_server(struct sockaddr_in * sin0)154*49cdfc7eSAndroid Build Coastguard Worker static pid_t start_server(struct sockaddr_in *sin0)
155*49cdfc7eSAndroid Build Coastguard Worker {
156*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
157*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t slen = sizeof(*sin0);
158*49cdfc7eSAndroid Build Coastguard Worker 
159*49cdfc7eSAndroid Build Coastguard Worker 	sin0->sin_family = AF_INET;
160*49cdfc7eSAndroid Build Coastguard Worker 	sin0->sin_port = 0; /* pick random free port */
161*49cdfc7eSAndroid Build Coastguard Worker 	sin0->sin_addr.s_addr = INADDR_ANY;
162*49cdfc7eSAndroid Build Coastguard Worker 
163*49cdfc7eSAndroid Build Coastguard Worker 	sfd = socket(PF_INET, SOCK_STREAM, 0);
164*49cdfc7eSAndroid Build Coastguard Worker 	if (sfd < 0) {
165*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
166*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
167*49cdfc7eSAndroid Build Coastguard Worker 	}
168*49cdfc7eSAndroid Build Coastguard Worker 	if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
169*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
170*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
171*49cdfc7eSAndroid Build Coastguard Worker 	}
172*49cdfc7eSAndroid Build Coastguard Worker 	if (listen(sfd, 10) < 0) {
173*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
174*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
175*49cdfc7eSAndroid Build Coastguard Worker 	}
176*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
177*49cdfc7eSAndroid Build Coastguard Worker 
178*49cdfc7eSAndroid Build Coastguard Worker 	switch ((pid = tst_fork())) {
179*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
180*49cdfc7eSAndroid Build Coastguard Worker 		do_child();
181*49cdfc7eSAndroid Build Coastguard Worker 		break;
182*49cdfc7eSAndroid Build Coastguard Worker 	case -1:
183*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "server fork failed");
184*49cdfc7eSAndroid Build Coastguard Worker 	default:
185*49cdfc7eSAndroid Build Coastguard Worker 		close(sfd);
186*49cdfc7eSAndroid Build Coastguard Worker 		return pid;
187*49cdfc7eSAndroid Build Coastguard Worker 	}
188*49cdfc7eSAndroid Build Coastguard Worker 
189*49cdfc7eSAndroid Build Coastguard Worker 	exit(1);
190*49cdfc7eSAndroid Build Coastguard Worker }
191*49cdfc7eSAndroid Build Coastguard Worker 
do_child(void)192*49cdfc7eSAndroid Build Coastguard Worker static void do_child(void)
193*49cdfc7eSAndroid Build Coastguard Worker {
194*49cdfc7eSAndroid Build Coastguard Worker 	fd_set afds, rfds;
195*49cdfc7eSAndroid Build Coastguard Worker 	int nfds, cc, fd;
196*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in fsin;
197*49cdfc7eSAndroid Build Coastguard Worker 
198*49cdfc7eSAndroid Build Coastguard Worker 	FD_ZERO(&afds);
199*49cdfc7eSAndroid Build Coastguard Worker 	FD_SET(sfd, &afds);
200*49cdfc7eSAndroid Build Coastguard Worker 
201*49cdfc7eSAndroid Build Coastguard Worker 	nfds = sfd + 1;
202*49cdfc7eSAndroid Build Coastguard Worker 
203*49cdfc7eSAndroid Build Coastguard Worker 	/* accept connections until killed */
204*49cdfc7eSAndroid Build Coastguard Worker 	while (1) {
205*49cdfc7eSAndroid Build Coastguard Worker 		socklen_t fromlen;
206*49cdfc7eSAndroid Build Coastguard Worker 
207*49cdfc7eSAndroid Build Coastguard Worker 		memcpy(&rfds, &afds, sizeof(rfds));
208*49cdfc7eSAndroid Build Coastguard Worker 
209*49cdfc7eSAndroid Build Coastguard Worker 		if (select(nfds, &rfds, NULL, NULL, NULL) < 0)
210*49cdfc7eSAndroid Build Coastguard Worker 			if (errno != EINTR)
211*49cdfc7eSAndroid Build Coastguard Worker 				exit(1);
212*49cdfc7eSAndroid Build Coastguard Worker 		if (FD_ISSET(sfd, &rfds)) {
213*49cdfc7eSAndroid Build Coastguard Worker 			int newfd;
214*49cdfc7eSAndroid Build Coastguard Worker 
215*49cdfc7eSAndroid Build Coastguard Worker 			fromlen = sizeof(fsin);
216*49cdfc7eSAndroid Build Coastguard Worker 			newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen);
217*49cdfc7eSAndroid Build Coastguard Worker 			if (newfd >= 0) {
218*49cdfc7eSAndroid Build Coastguard Worker 				FD_SET(newfd, &afds);
219*49cdfc7eSAndroid Build Coastguard Worker 				nfds = MAX(nfds, newfd + 1);
220*49cdfc7eSAndroid Build Coastguard Worker 			}
221*49cdfc7eSAndroid Build Coastguard Worker 		}
222*49cdfc7eSAndroid Build Coastguard Worker 		for (fd = 0; fd < nfds; ++fd) {
223*49cdfc7eSAndroid Build Coastguard Worker 			if (fd != sfd && FD_ISSET(fd, &rfds)) {
224*49cdfc7eSAndroid Build Coastguard Worker 				cc = read(fd, buf, sizeof(buf));
225*49cdfc7eSAndroid Build Coastguard Worker 				if (cc == 0 || (cc < 0 && errno != EINTR)) {
226*49cdfc7eSAndroid Build Coastguard Worker 					close(fd);
227*49cdfc7eSAndroid Build Coastguard Worker 					FD_CLR(fd, &afds);
228*49cdfc7eSAndroid Build Coastguard Worker 				}
229*49cdfc7eSAndroid Build Coastguard Worker 			}
230*49cdfc7eSAndroid Build Coastguard Worker 		}
231*49cdfc7eSAndroid Build Coastguard Worker 	}
232*49cdfc7eSAndroid Build Coastguard Worker }
233*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char * av[])234*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char *av[])
235*49cdfc7eSAndroid Build Coastguard Worker {
236*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
237*49cdfc7eSAndroid Build Coastguard Worker 
238*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
239*49cdfc7eSAndroid Build Coastguard Worker 
240*49cdfc7eSAndroid Build Coastguard Worker 	setup();
241*49cdfc7eSAndroid Build Coastguard Worker 
242*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
243*49cdfc7eSAndroid Build Coastguard Worker 
244*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
245*49cdfc7eSAndroid Build Coastguard Worker 
246*49cdfc7eSAndroid Build Coastguard Worker 		for (testno = 0; testno < TST_TOTAL; ++testno) {
247*49cdfc7eSAndroid Build Coastguard Worker 			tdat[testno].setup();
248*49cdfc7eSAndroid Build Coastguard Worker 
249*49cdfc7eSAndroid Build Coastguard Worker 			TEST(send(s, tdat[testno].buf, tdat[testno].buflen,
250*49cdfc7eSAndroid Build Coastguard Worker 				  tdat[testno].flags));
251*49cdfc7eSAndroid Build Coastguard Worker 
252*49cdfc7eSAndroid Build Coastguard Worker 			if (TEST_RETURN != -1) {
253*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "call succeeded unexpectedly");
254*49cdfc7eSAndroid Build Coastguard Worker 				continue;
255*49cdfc7eSAndroid Build Coastguard Worker 			}
256*49cdfc7eSAndroid Build Coastguard Worker 
257*49cdfc7eSAndroid Build Coastguard Worker 			if (TEST_ERRNO != tdat[testno].experrno) {
258*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "%s ; returned"
259*49cdfc7eSAndroid Build Coastguard Worker 					 " %ld (expected %d), errno %d (expected"
260*49cdfc7eSAndroid Build Coastguard Worker 					 " %d)", tdat[testno].desc,
261*49cdfc7eSAndroid Build Coastguard Worker 					 TEST_RETURN, tdat[testno].retval,
262*49cdfc7eSAndroid Build Coastguard Worker 					 TEST_ERRNO, tdat[testno].experrno);
263*49cdfc7eSAndroid Build Coastguard Worker 			} else {
264*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TPASS, "%s successful",
265*49cdfc7eSAndroid Build Coastguard Worker 					 tdat[testno].desc);
266*49cdfc7eSAndroid Build Coastguard Worker 			}
267*49cdfc7eSAndroid Build Coastguard Worker 			tdat[testno].cleanup();
268*49cdfc7eSAndroid Build Coastguard Worker 		}
269*49cdfc7eSAndroid Build Coastguard Worker 	}
270*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
271*49cdfc7eSAndroid Build Coastguard Worker 
272*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
273*49cdfc7eSAndroid Build Coastguard Worker }
274*49cdfc7eSAndroid Build Coastguard Worker 
275*49cdfc7eSAndroid Build Coastguard Worker static pid_t server_pid;
276*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)277*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
278*49cdfc7eSAndroid Build Coastguard Worker {
279*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
280*49cdfc7eSAndroid Build Coastguard Worker 
281*49cdfc7eSAndroid Build Coastguard Worker 	server_pid = start_server(&sin1);
282*49cdfc7eSAndroid Build Coastguard Worker 
283*49cdfc7eSAndroid Build Coastguard Worker 	signal(SIGPIPE, SIG_IGN);
284*49cdfc7eSAndroid Build Coastguard Worker }
285*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)286*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
287*49cdfc7eSAndroid Build Coastguard Worker {
288*49cdfc7eSAndroid Build Coastguard Worker 	kill(server_pid, SIGKILL);
289*49cdfc7eSAndroid Build Coastguard Worker 
290*49cdfc7eSAndroid Build Coastguard Worker }
291*49cdfc7eSAndroid Build Coastguard Worker 
setup0(void)292*49cdfc7eSAndroid Build Coastguard Worker static void setup0(void)
293*49cdfc7eSAndroid Build Coastguard Worker {
294*49cdfc7eSAndroid Build Coastguard Worker 	if (tdat[testno].experrno == EBADF)
295*49cdfc7eSAndroid Build Coastguard Worker 		s = 400;	/* anything not an open file */
296*49cdfc7eSAndroid Build Coastguard Worker 	else if ((s = open("/dev/null", O_WRONLY)) == -1)
297*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null) failed");
298*49cdfc7eSAndroid Build Coastguard Worker }
299*49cdfc7eSAndroid Build Coastguard Worker 
cleanup0(void)300*49cdfc7eSAndroid Build Coastguard Worker static void cleanup0(void)
301*49cdfc7eSAndroid Build Coastguard Worker {
302*49cdfc7eSAndroid Build Coastguard Worker 	s = -1;
303*49cdfc7eSAndroid Build Coastguard Worker }
304*49cdfc7eSAndroid Build Coastguard Worker 
setup1(void)305*49cdfc7eSAndroid Build Coastguard Worker static void setup1(void)
306*49cdfc7eSAndroid Build Coastguard Worker {
307*49cdfc7eSAndroid Build Coastguard Worker 	s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
308*49cdfc7eSAndroid Build Coastguard Worker 			tdat[testno].proto);
309*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CONNECT(cleanup, s, (const struct sockaddr *)&sin1, sizeof(sin1));
310*49cdfc7eSAndroid Build Coastguard Worker }
311*49cdfc7eSAndroid Build Coastguard Worker 
cleanup1(void)312*49cdfc7eSAndroid Build Coastguard Worker static void cleanup1(void)
313*49cdfc7eSAndroid Build Coastguard Worker {
314*49cdfc7eSAndroid Build Coastguard Worker 	close(s);
315*49cdfc7eSAndroid Build Coastguard Worker 	s = -1;
316*49cdfc7eSAndroid Build Coastguard Worker }
317*49cdfc7eSAndroid Build Coastguard Worker 
setup2(void)318*49cdfc7eSAndroid Build Coastguard Worker static void setup2(void)
319*49cdfc7eSAndroid Build Coastguard Worker {
320*49cdfc7eSAndroid Build Coastguard Worker 	setup1();
321*49cdfc7eSAndroid Build Coastguard Worker 
322*49cdfc7eSAndroid Build Coastguard Worker 	if (shutdown(s, 1) < 0)
323*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "socket setup failed connect "
324*49cdfc7eSAndroid Build Coastguard Worker 			 "test %d", testno);
325*49cdfc7eSAndroid Build Coastguard Worker }
326