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