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: recv01
22 *
23 * Test Description:
24 * Verify that recv() returns the proper errno for various failure cases
25 *
26 * Usage: <for command-line>
27 * recv01 [-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 = "recv01";
59 int testno;
60
61 char buf[1024];
62 int s; /* socket descriptor */
63 struct sockaddr_in sin1, sin2, sin3, sin4;
64 static int sfd; /* shared between do_child and start_server */
65
66 void do_child(void), setup(void), setup0(void), setup1(void),
67 cleanup(void), cleanup0(void), cleanup1(void);
68 pid_t start_server(struct sockaddr_in *);
69
70 struct test_case_t { /* test case structure */
71 int domain; /* PF_INET, PF_UNIX, ... */
72 int type; /* SOCK_STREAM, SOCK_DGRAM ... */
73 int proto; /* protocol number (usually 0 = default) */
74 void *buf; /* recv data buffer */
75 int buflen; /* recv's 3rd argument */
76 unsigned flags; /* recv's 4th argument */
77 int retval; /* syscall return value */
78 int experrno; /* expected errno */
79 void (*setup) (void);
80 void (*cleanup) (void);
81 char *desc;
82 } tdat[] = {
83 {
84 PF_INET, SOCK_STREAM, 0, buf, sizeof(buf), 0,
85 -1, EBADF, setup0, cleanup0, "bad file descriptor"}
86 , {
87 0, 0, 0, buf, sizeof(buf), 0,
88 -1, ENOTSOCK, setup0, cleanup0, "invalid socket"}
89 ,
90 #ifndef UCLINUX
91 /* Skip since uClinux does not implement memory protection */
92 {
93 PF_INET, SOCK_STREAM, 0, (void *)-1, sizeof(buf), 0,
94 -1, EFAULT, setup1, cleanup1, "invalid recv buffer"}
95 ,
96 #endif
97 {
98 PF_INET, SOCK_STREAM, 0, buf, sizeof(buf), MSG_OOB,
99 -1, EINVAL, setup1, cleanup1, "invalid MSG_OOB flag set"}
100 ,
101 {
102 PF_INET, SOCK_STREAM, 0, buf, sizeof(buf), MSG_ERRQUEUE,
103 -1, EAGAIN, setup1, cleanup1, "invalid MSG_ERRQUEUE flag set"}
104 ,};
105
106 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
107
108 #ifdef UCLINUX
109 static char *argv0;
110 #endif
111
main(int argc,char * argv[])112 int main(int argc, char *argv[])
113 {
114 int lc;
115
116 tst_parse_opts(argc, argv, NULL, NULL);
117 #ifdef UCLINUX
118 argv0 = argv[0];
119 maybe_run_child(&do_child, "d", &sfd);
120 #endif
121
122 setup();
123
124 for (lc = 0; TEST_LOOPING(lc); ++lc) {
125 tst_count = 0;
126 for (testno = 0; testno < TST_TOTAL; ++testno) {
127 if ((tst_kvercmp(3, 17, 0) < 0)
128 && (tdat[testno].flags & MSG_ERRQUEUE)
129 && (tdat[testno].type & SOCK_STREAM)) {
130 tst_resm(TCONF, "skip MSG_ERRQUEUE test, "
131 "it's supported from 3.17");
132 continue;
133 }
134
135 tdat[testno].setup();
136 TEST(recv(s, tdat[testno].buf, tdat[testno].buflen,
137 tdat[testno].flags));
138 if (TEST_RETURN > 0)
139 TEST_RETURN = 0; /* all nonzero equal here */
140 if (TEST_RETURN != tdat[testno].retval ||
141 (TEST_RETURN < 0 &&
142 TEST_ERRNO != tdat[testno].experrno)) {
143 tst_resm(TFAIL, "%s ; returned"
144 " %ld (expected %d), errno %d (expected"
145 " %d)", tdat[testno].desc,
146 TEST_RETURN, tdat[testno].retval,
147 TEST_ERRNO, tdat[testno].experrno);
148 } else {
149 tst_resm(TPASS, "%s successful",
150 tdat[testno].desc);
151 }
152 tdat[testno].cleanup();
153 }
154 }
155 cleanup();
156
157 tst_exit();
158 }
159
160 pid_t pid;
161
setup(void)162 void setup(void)
163 {
164 TEST_PAUSE;
165
166 pid = start_server(&sin1);
167
168 (void)signal(SIGPIPE, SIG_IGN);
169 }
170
cleanup(void)171 void cleanup(void)
172 {
173 (void)kill(pid, SIGKILL);
174
175 }
176
setup0(void)177 void setup0(void)
178 {
179 if (tdat[testno].experrno == EBADF)
180 s = 400; /* anything not an open file */
181 else if ((s = open("/dev/null", O_WRONLY)) == -1)
182 tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null)");
183 }
184
cleanup0(void)185 void cleanup0(void)
186 {
187 s = -1;
188 }
189
setup1(void)190 void setup1(void)
191 {
192 fd_set rdfds;
193 struct timeval timeout;
194 int n;
195
196 s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
197 tdat[testno].proto);
198 if (tdat[testno].type == SOCK_STREAM &&
199 connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
200 tst_brkm(TBROK | TERRNO, cleanup, "connect failed");
201 }
202 /* Wait for something to be readable, else we won't detect EFAULT on recv */
203 FD_ZERO(&rdfds);
204 FD_SET(s, &rdfds);
205 timeout.tv_sec = 2;
206 timeout.tv_usec = 0;
207 n = select(s + 1, &rdfds, 0, 0, &timeout);
208 if (n != 1 || !FD_ISSET(s, &rdfds))
209 tst_brkm(TBROK, cleanup,
210 "client setup1 failed - no message ready in 2 sec");
211 }
212
cleanup1(void)213 void cleanup1(void)
214 {
215 (void)close(s);
216 s = -1;
217 }
218
start_server(struct sockaddr_in * sin0)219 pid_t start_server(struct sockaddr_in *sin0)
220 {
221 pid_t pid;
222 socklen_t slen = sizeof(*sin0);
223
224 sin0->sin_family = AF_INET;
225 sin0->sin_port = 0; /* pick random free port */
226 sin0->sin_addr.s_addr = INADDR_ANY;
227
228 sfd = socket(PF_INET, SOCK_STREAM, 0);
229 if (sfd < 0) {
230 tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
231 return -1;
232 }
233 if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
234 tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
235 return -1;
236 }
237 if (listen(sfd, 10) < 0) {
238 tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
239 return -1;
240 }
241 SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
242
243 switch ((pid = FORK_OR_VFORK())) {
244 case 0: /* child */
245 #ifdef UCLINUX
246 if (self_exec(argv0, "d", sfd) < 0)
247 tst_brkm(TBROK | TERRNO, cleanup,
248 "server self_exec failed");
249 #else
250 do_child();
251 #endif
252 break;
253 case -1:
254 tst_brkm(TBROK | TERRNO, cleanup, "server fork failed");
255 /* fall through */
256 default: /* parent */
257 (void)close(sfd);
258 return pid;
259 }
260
261 exit(1);
262 }
263
do_child(void)264 void do_child(void)
265 {
266 struct sockaddr_in fsin;
267 fd_set afds, rfds;
268 int nfds, cc, fd;
269
270 FD_ZERO(&afds);
271 FD_SET(sfd, &afds);
272
273 nfds = sfd + 1;
274
275 /* accept connections until killed */
276 while (1) {
277 socklen_t fromlen;
278
279 memcpy(&rfds, &afds, sizeof(rfds));
280
281 if (select(nfds, &rfds, NULL, NULL,
282 NULL) < 0)
283 if (errno != EINTR)
284 exit(1);
285 if (FD_ISSET(sfd, &rfds)) {
286 int newfd;
287
288 fromlen = sizeof(fsin);
289 newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen);
290 if (newfd >= 0) {
291 FD_SET(newfd, &afds);
292 nfds = MAX(nfds, newfd + 1);
293 /* send something back */
294 (void)write(newfd, "hoser\n", 6);
295 }
296 }
297 for (fd = 0; fd < nfds; ++fd)
298 if (fd != sfd && FD_ISSET(fd, &rfds)) {
299 cc = read(fd, buf, sizeof(buf));
300 if (cc == 0 || (cc < 0 && errno != EINTR)) {
301 (void)close(fd);
302 FD_CLR(fd, &afds);
303 }
304 }
305 }
306 }
307