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