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