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
129 #ifdef UCLINUX
130 static char *argv0;
131 #endif
132
main(int argc,char * argv[])133 int main(int argc, char *argv[])
134 {
135 int lc;
136
137 tst_parse_opts(argc, argv, NULL, NULL);
138
139 #ifdef UCLINUX
140 argv0 = argv[0];
141 maybe_run_child(&do_child, "d", &sfd);
142 #endif
143
144 setup();
145
146 for (lc = 0; TEST_LOOPING(lc); ++lc) {
147 tst_count = 0;
148 for (testno = 0; testno < TST_TOTAL; ++testno) {
149 if ((tst_kvercmp(3, 17, 0) < 0)
150 && (tdat[testno].flags & MSG_ERRQUEUE)
151 && (tdat[testno].type & SOCK_STREAM)) {
152 tst_resm(TCONF, "skip MSG_ERRQUEUE test, "
153 "it's supported from 3.17");
154 continue;
155 }
156
157 tdat[testno].setup();
158 TEST(recvfrom(s, tdat[testno].buf, tdat[testno].buflen,
159 tdat[testno].flags, tdat[testno].from,
160 tdat[testno].salen));
161 if (TEST_RETURN >= 0)
162 TEST_RETURN = 0; /* all nonzero equal here */
163 if (TEST_RETURN != tdat[testno].retval ||
164 (TEST_RETURN < 0 &&
165 TEST_ERRNO != tdat[testno].experrno)) {
166 tst_resm(TFAIL, "%s ; returned"
167 " %ld (expected %d), errno %d (expected"
168 " %d)", tdat[testno].desc,
169 TEST_RETURN, tdat[testno].retval,
170 TEST_ERRNO, tdat[testno].experrno);
171 } else {
172 tst_resm(TPASS, "%s successful",
173 tdat[testno].desc);
174 }
175 tdat[testno].cleanup();
176 }
177 }
178 cleanup();
179
180 tst_exit();
181 }
182
183 pid_t pid;
184
setup(void)185 void setup(void)
186 {
187 TEST_PAUSE;
188
189 pid = start_server(&sin1);
190 }
191
cleanup(void)192 void cleanup(void)
193 {
194 (void)kill(pid, SIGKILL);
195
196 }
197
setup0(void)198 void setup0(void)
199 {
200 if (tdat[testno].experrno == EBADF)
201 s = 400; /* anything not an open file */
202 else if ((s = open("/dev/null", O_WRONLY)) == -1)
203 tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null) failed");
204 fromlen = sizeof(from);
205 }
206
cleanup0(void)207 void cleanup0(void)
208 {
209 s = -1;
210 }
211
setup1(void)212 void setup1(void)
213 {
214 fd_set rdfds;
215 struct timeval timeout;
216 int n;
217
218 s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
219 tdat[testno].proto);
220 if (tdat[testno].type == SOCK_STREAM &&
221 connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
222 tst_brkm(TBROK | TERRNO, cleanup, "connect failed");
223 }
224 /* Wait for something to be readable, else we won't detect EFAULT on recv */
225 FD_ZERO(&rdfds);
226 FD_SET(s, &rdfds);
227 timeout.tv_sec = 2;
228 timeout.tv_usec = 0;
229 n = select(s + 1, &rdfds, 0, 0, &timeout);
230 if (n != 1 || !FD_ISSET(s, &rdfds))
231 tst_brkm(TBROK, cleanup,
232 "client setup1 failed - no message ready in 2 sec");
233 fromlen = sizeof(from);
234 }
235
setup2(void)236 void setup2(void)
237 {
238 setup1();
239 fromlen = -1;
240 }
241
cleanup1(void)242 void cleanup1(void)
243 {
244 (void)close(s);
245 s = -1;
246 }
247
start_server(struct sockaddr_in * sin0)248 pid_t start_server(struct sockaddr_in *sin0)
249 {
250 pid_t pid;
251 socklen_t slen = sizeof(*sin0);
252
253 sin0->sin_family = AF_INET;
254 sin0->sin_port = 0; /* pick random free port */
255 sin0->sin_addr.s_addr = INADDR_ANY;
256
257 sfd = socket(PF_INET, SOCK_STREAM, 0);
258 if (sfd < 0) {
259 tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
260 return -1;
261 }
262 if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
263 tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
264 return -1;
265 }
266 if (listen(sfd, 10) < 0) {
267 tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
268 return -1;
269 }
270 SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
271
272 switch ((pid = FORK_OR_VFORK())) {
273 case 0: /* child */
274 #ifdef UCLINUX
275 if (self_exec(argv0, "d", sfd) < 0) {
276 tst_brkm(TBROK, cleanup, "server self_exec failed");
277 }
278 #else
279 do_child();
280 #endif
281 break;
282 case -1:
283 tst_brkm(TBROK | TERRNO, cleanup, "server fork failed");
284 /* fall through */
285 default: /* parent */
286 (void)close(sfd);
287 return pid;
288 }
289
290 exit(1);
291 }
292
do_child(void)293 void do_child(void)
294 {
295 struct sockaddr_in fsin;
296 fd_set afds, rfds;
297 int nfds, cc, fd;
298
299 FD_ZERO(&afds);
300 FD_SET(sfd, &afds);
301
302 nfds = sfd + 1;
303
304 /* accept connections until killed */
305 while (1) {
306 socklen_t fromlen;
307
308 memcpy(&rfds, &afds, sizeof(rfds));
309
310 if (select(nfds, &rfds, NULL, NULL,
311 NULL) < 0)
312 if (errno != EINTR)
313 exit(1);
314 if (FD_ISSET(sfd, &rfds)) {
315 int newfd;
316
317 fromlen = sizeof(fsin);
318 newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen);
319 if (newfd >= 0) {
320 FD_SET(newfd, &afds);
321 nfds = MAX(nfds, newfd + 1);
322 /* send something back */
323 (void)write(newfd, "hoser\n", 6);
324 }
325 }
326 for (fd = 0; fd < nfds; ++fd)
327 if (fd != sfd && FD_ISSET(fd, &rfds)) {
328 cc = read(fd, buf, sizeof(buf));
329 if (cc == 0 || (cc < 0 && errno != EINTR)) {
330 (void)close(fd);
331 FD_CLR(fd, &afds);
332 }
333 }
334 }
335 }
336