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 {
91 PF_INET, SOCK_STREAM, 0, (void *)-1, sizeof(buf), 0,
92 -1, EFAULT, setup1, cleanup1, "invalid recv buffer"}
93 ,
94 {
95 PF_INET, SOCK_STREAM, 0, buf, sizeof(buf), MSG_OOB,
96 -1, EINVAL, setup1, cleanup1, "invalid MSG_OOB flag set"}
97 ,
98 {
99 PF_INET, SOCK_STREAM, 0, buf, sizeof(buf), MSG_ERRQUEUE,
100 -1, EAGAIN, setup1, cleanup1, "invalid MSG_ERRQUEUE flag set"}
101 ,};
102
103 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
104
main(int argc,char * argv[])105 int main(int argc, char *argv[])
106 {
107 int lc;
108
109 tst_parse_opts(argc, argv, NULL, NULL);
110
111 setup();
112
113 for (lc = 0; TEST_LOOPING(lc); ++lc) {
114 tst_count = 0;
115 for (testno = 0; testno < TST_TOTAL; ++testno) {
116 if ((tst_kvercmp(3, 17, 0) < 0)
117 && (tdat[testno].flags & MSG_ERRQUEUE)
118 && (tdat[testno].type & SOCK_STREAM)) {
119 tst_resm(TCONF, "skip MSG_ERRQUEUE test, "
120 "it's supported from 3.17");
121 continue;
122 }
123
124 tdat[testno].setup();
125 TEST(recv(s, tdat[testno].buf, tdat[testno].buflen,
126 tdat[testno].flags));
127 if (TEST_RETURN > 0)
128 TEST_RETURN = 0; /* all nonzero equal here */
129 if (TEST_RETURN != tdat[testno].retval ||
130 (TEST_RETURN < 0 &&
131 TEST_ERRNO != tdat[testno].experrno)) {
132 tst_resm(TFAIL, "%s ; returned"
133 " %ld (expected %d), errno %d (expected"
134 " %d)", tdat[testno].desc,
135 TEST_RETURN, tdat[testno].retval,
136 TEST_ERRNO, tdat[testno].experrno);
137 } else {
138 tst_resm(TPASS, "%s successful",
139 tdat[testno].desc);
140 }
141 tdat[testno].cleanup();
142 }
143 }
144 cleanup();
145
146 tst_exit();
147 }
148
149 pid_t pid;
150
setup(void)151 void setup(void)
152 {
153 TEST_PAUSE;
154
155 pid = start_server(&sin1);
156
157 (void)signal(SIGPIPE, SIG_IGN);
158 }
159
cleanup(void)160 void cleanup(void)
161 {
162 (void)kill(pid, SIGKILL);
163
164 }
165
setup0(void)166 void setup0(void)
167 {
168 if (tdat[testno].experrno == EBADF)
169 s = 400; /* anything not an open file */
170 else if ((s = open("/dev/null", O_WRONLY)) == -1)
171 tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null)");
172 }
173
cleanup0(void)174 void cleanup0(void)
175 {
176 s = -1;
177 }
178
setup1(void)179 void setup1(void)
180 {
181 fd_set rdfds;
182 struct timeval timeout;
183 int n;
184
185 s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
186 tdat[testno].proto);
187 if (tdat[testno].type == SOCK_STREAM &&
188 connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
189 tst_brkm(TBROK | TERRNO, cleanup, "connect failed");
190 }
191 /* Wait for something to be readable, else we won't detect EFAULT on recv */
192 FD_ZERO(&rdfds);
193 FD_SET(s, &rdfds);
194 timeout.tv_sec = 2;
195 timeout.tv_usec = 0;
196 n = select(s + 1, &rdfds, 0, 0, &timeout);
197 if (n != 1 || !FD_ISSET(s, &rdfds))
198 tst_brkm(TBROK, cleanup,
199 "client setup1 failed - no message ready in 2 sec");
200 }
201
cleanup1(void)202 void cleanup1(void)
203 {
204 (void)close(s);
205 s = -1;
206 }
207
start_server(struct sockaddr_in * sin0)208 pid_t start_server(struct sockaddr_in *sin0)
209 {
210 pid_t pid;
211 socklen_t slen = sizeof(*sin0);
212
213 sin0->sin_family = AF_INET;
214 sin0->sin_port = 0; /* pick random free port */
215 sin0->sin_addr.s_addr = INADDR_ANY;
216
217 sfd = socket(PF_INET, SOCK_STREAM, 0);
218 if (sfd < 0) {
219 tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
220 return -1;
221 }
222 if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
223 tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
224 return -1;
225 }
226 if (listen(sfd, 10) < 0) {
227 tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
228 return -1;
229 }
230 SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
231
232 switch ((pid = tst_fork())) {
233 case 0: /* child */
234 do_child();
235 break;
236 case -1:
237 tst_brkm(TBROK | TERRNO, cleanup, "server fork failed");
238 /* fall through */
239 default: /* parent */
240 (void)close(sfd);
241 return pid;
242 }
243
244 exit(1);
245 }
246
do_child(void)247 void do_child(void)
248 {
249 struct sockaddr_in fsin;
250 fd_set afds, rfds;
251 int nfds, cc, fd;
252
253 FD_ZERO(&afds);
254 FD_SET(sfd, &afds);
255
256 nfds = sfd + 1;
257
258 /* accept connections until killed */
259 while (1) {
260 socklen_t fromlen;
261
262 memcpy(&rfds, &afds, sizeof(rfds));
263
264 if (select(nfds, &rfds, NULL, NULL,
265 NULL) < 0)
266 if (errno != EINTR)
267 exit(1);
268 if (FD_ISSET(sfd, &rfds)) {
269 int newfd;
270
271 fromlen = sizeof(fsin);
272 newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen);
273 if (newfd >= 0) {
274 FD_SET(newfd, &afds);
275 nfds = MAX(nfds, newfd + 1);
276 /* send something back */
277 (void)write(newfd, "hoser\n", 6);
278 }
279 }
280 for (fd = 0; fd < nfds; ++fd)
281 if (fd != sfd && FD_ISSET(fd, &rfds)) {
282 cc = read(fd, buf, sizeof(buf));
283 if (cc == 0 || (cc < 0 && errno != EINTR)) {
284 (void)close(fd);
285 FD_CLR(fd, &afds);
286 }
287 }
288 }
289 }
290