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