blob: 3577f432fb838416a5fb21981816d5bb05543107 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
Cyril Hrubis10c580b2012-10-10 12:49:02 +02004 * Copyright (c) Cyril Hrubis <chrubis@suse.cz> 2012
plars865695b2001-08-27 22:15:12 +00005 *
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000019 */
20
21/*
22 * Test Name: sendto01
23 *
24 * Test Description:
25 * Verify that sendto() returns the proper errno for various failure cases
26 *
plars865695b2001-08-27 22:15:12 +000027 * HISTORY
28 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +000029 */
30
31#include <stdio.h>
32#include <unistd.h>
33#include <errno.h>
plars393f6342002-03-26 20:45:06 +000034#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000035
36#include <sys/types.h>
37#include <sys/socket.h>
38#include <sys/signal.h>
39#include <sys/un.h>
40
41#include <netinet/in.h>
42
43#include "test.h"
plars865695b2001-08-27 22:15:12 +000044
Cyril Hrubis10c580b2012-10-10 12:49:02 +020045char *TCID = "sendto01";
plars865695b2001-08-27 22:15:12 +000046int testno;
47
Cyril Hrubis10c580b2012-10-10 12:49:02 +020048static char buf[1024], bigbuf[128 * 1024];
49static int s;
50static struct sockaddr_in sin1, sin2;
51static int sfd;
plars865695b2001-08-27 22:15:12 +000052
53struct test_case_t { /* test case structure */
subrata_modak56207ce2009-03-23 13:35:39 +000054 int domain; /* PF_INET, PF_UNIX, ... */
55 int type; /* SOCK_STREAM, SOCK_DGRAM ... */
56 int proto; /* protocol number (usually 0 = default) */
57 void *buf; /* send data buffer */
58 int buflen; /* send's 3rd argument */
59 unsigned flags; /* send's 4th argument */
plars865695b2001-08-27 22:15:12 +000060 struct sockaddr_in *to; /* destination */
subrata_modak56207ce2009-03-23 13:35:39 +000061 int tolen; /* length of "to" buffer */
Cyril Hrubis10c580b2012-10-10 12:49:02 +020062 int retval;
63 int experrno;
Wanlong Gao354ebb42012-12-07 10:10:04 +080064 void (*setup) (void);
65 void (*cleanup) (void);
plars865695b2001-08-27 22:15:12 +000066 char *desc;
plars865695b2001-08-27 22:15:12 +000067};
68
Cyril Hrubis10c580b2012-10-10 12:49:02 +020069static void setup(void);
70static void setup0(void);
71static void setup1(void);
72static void setup2(void);
73static void setup3(void);
74static void cleanup(void);
75static void cleanup0(void);
76static void cleanup1(void);
77static void do_child(void);
plars865695b2001-08-27 22:15:12 +000078
Cyril Hrubis10c580b2012-10-10 12:49:02 +020079struct test_case_t tdat[] = {
80 {.domain = PF_INET,
81 .type = SOCK_STREAM,
82 .proto = 0,
83 .buf = buf,
84 .buflen = sizeof(buf),
85 .flags = 0,
86 .to = &sin1,
87 .tolen = sizeof(sin1),
88 .retval = -1,
89 .experrno = EBADF,
90 .setup = setup0,
91 .cleanup = cleanup0,
Wanlong Gao354ebb42012-12-07 10:10:04 +080092 .desc = "bad file descriptor"}
93 ,
Cyril Hrubis10c580b2012-10-10 12:49:02 +020094 {.domain = 0,
95 .type = 0,
96 .proto = 0,
97 .buf = buf,
98 .buflen = sizeof(buf),
99 .flags = 0,
100 .to = &sin1,
101 .tolen = sizeof(sin1),
102 .retval = -1,
103 .experrno = ENOTSOCK,
104 .setup = setup0,
105 .cleanup = cleanup0,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 .desc = "invalid socket"}
107 ,
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200108#ifndef UCLINUX
109 /* Skip since uClinux does not implement memory protection */
110 {.domain = PF_INET,
111 .type = SOCK_DGRAM,
112 .proto = 0,
113 .buf = (void *)-1,
114 .buflen = sizeof(buf),
115 .flags = 0,
116 .to = &sin1,
117 .tolen = sizeof(sin1),
118 .retval = -1,
119 .experrno = EFAULT,
120 .setup = setup1,
121 .cleanup = cleanup1,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 .desc = "invalid send buffer"}
123 ,
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200124#endif
125 {.domain = PF_INET,
126 .type = SOCK_STREAM,
127 .proto = 0,
128 .buf = buf,
129 .buflen = sizeof(buf),
130 .flags = 0,
131 .to = &sin2,
132 .tolen = sizeof(sin2),
133 .retval = 0,
134 .experrno = EFAULT,
135 .setup = setup1,
136 .cleanup = cleanup1,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800137 .desc = "connected TCP"}
138 ,
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200139 {.domain = PF_INET,
140 .type = SOCK_STREAM,
141 .proto = 0,
142 .buf = buf,
143 .buflen = sizeof(buf),
144 .flags = 0,
145 .to = &sin1,
146 .tolen = sizeof(sin1),
147 .retval = -1,
148 .experrno = EPIPE,
149 .setup = setup3,
150 .cleanup = cleanup1,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 .desc = "not connected TCP"}
152 ,
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200153 {.domain = PF_INET,
154 .type = SOCK_DGRAM,
155 .proto = 0,
156 .buf = buf,
157 .buflen = sizeof(buf),
158 .flags = 0,
159 .to = &sin1,
160 .tolen = -1,
161 .retval = -1,
162 .experrno = EINVAL,
163 .setup = setup1,
164 .cleanup = cleanup1,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800165 .desc = "invalid to buffer length"}
166 ,
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200167#ifndef UCLINUX
168 /* Skip since uClinux does not implement memory protection */
169 {.domain = PF_INET,
170 .type = SOCK_DGRAM,
171 .proto = 0,
172 .buf = buf,
173 .buflen = sizeof(buf),
174 .flags = 0,
175 .to = (struct sockaddr_in *)-1,
176 .tolen = sizeof(sin1),
177 .retval = -1,
178 .experrno = EFAULT,
179 .setup = setup1,
180 .cleanup = cleanup1,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800181 .desc = "invalid to buffer"}
182 ,
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200183#endif
184 {.domain = PF_INET,
185 .type = SOCK_DGRAM,
186 .proto = 0,
187 .buf = bigbuf,
188 .buflen = sizeof(bigbuf),
189 .flags = 0,
190 .to = &sin1,
191 .tolen = sizeof(sin1),
192 .retval = -1,
193 .experrno = EMSGSIZE,
194 .setup = setup1,
195 .cleanup = cleanup1,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800196 .desc = "UDP message too big"}
197 ,
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200198 {.domain = PF_INET,
199 .type = SOCK_STREAM,
200 .proto = 0,
201 .buf = buf,
202 .buflen = sizeof(buf),
203 .flags = 0,
204 .to = &sin1,
205 .tolen = sizeof(sin1),
206 .retval = -1,
207 .experrno = EPIPE,
208 .setup = setup2,
209 .cleanup = cleanup1,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800210 .desc = "local endpoint shutdown"}
211 ,
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200212 {.domain = PF_INET,
Jan Stancek1c6354e2013-11-29 11:39:34 +0100213 .type = SOCK_DGRAM,
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200214 .proto = 0,
215 .buf = buf,
216 .buflen = sizeof(buf),
Jan Stancek1c6354e2013-11-29 11:39:34 +0100217 .flags = MSG_OOB,
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200218 .to = &sin1,
219 .tolen = sizeof(sin1),
Jan Stancek1c6354e2013-11-29 11:39:34 +0100220 .retval = -1,
221 .experrno = EOPNOTSUPP,
222 .setup = setup1,
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200223 .cleanup = cleanup1,
224 .desc = "invalid flags set"}
225};
plars865695b2001-08-27 22:15:12 +0000226
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200227int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
228
robbiewd34d5812005-07-11 22:28:09 +0000229#ifdef UCLINUX
230static char *argv0;
231#endif
232
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200233static pid_t start_server(struct sockaddr_in *sin0)
robbiew8541eeb2003-03-27 19:59:32 +0000234{
subrata_modak56207ce2009-03-23 13:35:39 +0000235 pid_t pid;
Jan Stancek6e4e77a2014-03-07 11:39:56 +0100236 socklen_t slen = sizeof(*sin0);
237
238 sin0->sin_family = AF_INET;
239 sin0->sin_port = 0; /* pick random free port */
240 sin0->sin_addr.s_addr = INADDR_ANY;
robbiew8541eeb2003-03-27 19:59:32 +0000241
242 sfd = socket(PF_INET, SOCK_STREAM, 0);
243 if (sfd < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800244 tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
robbiew8541eeb2003-03-27 19:59:32 +0000245 return -1;
246 }
Jan Stancek6e4e77a2014-03-07 11:39:56 +0100247 if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800248 tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
robbiew8541eeb2003-03-27 19:59:32 +0000249 return -1;
250 }
251 if (listen(sfd, 10) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800252 tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
robbiew8541eeb2003-03-27 19:59:32 +0000253 return -1;
254 }
Jan Stancek6e4e77a2014-03-07 11:39:56 +0100255 if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1)
256 tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
257
robbiewd34d5812005-07-11 22:28:09 +0000258 switch ((pid = FORK_OR_VFORK())) {
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200259 case 0:
robbiewd34d5812005-07-11 22:28:09 +0000260#ifdef UCLINUX
vapierc450e062009-08-28 14:28:52 +0000261 if (self_exec(argv0, "d", sfd) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800262 tst_brkm(TBROK | TERRNO, cleanup,
263 "server self_exec failed");
robbiewd34d5812005-07-11 22:28:09 +0000264#else
265 do_child();
266#endif
robbiew8541eeb2003-03-27 19:59:32 +0000267 break;
268 case -1:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800269 tst_brkm(TBROK | TERRNO, cleanup, "server fork failed");
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200270 default:
subrata_modak56207ce2009-03-23 13:35:39 +0000271 (void)close(sfd);
robbiew8541eeb2003-03-27 19:59:32 +0000272 return pid;
273 }
274
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200275 exit(1);
robbiewd34d5812005-07-11 22:28:09 +0000276}
277
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200278static void do_child(void)
robbiewd34d5812005-07-11 22:28:09 +0000279{
280 struct sockaddr_in fsin;
subrata_modak56207ce2009-03-23 13:35:39 +0000281 fd_set afds, rfds;
282 int nfds, cc, fd;
robbiewd34d5812005-07-11 22:28:09 +0000283
robbiew8541eeb2003-03-27 19:59:32 +0000284 FD_ZERO(&afds);
285 FD_SET(sfd, &afds);
286
Cyril Hrubis7855d092014-10-09 16:13:05 +0200287 nfds = sfd + 1;
robbiew8541eeb2003-03-27 19:59:32 +0000288
289 /* accept connections until killed */
290 while (1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000291 socklen_t fromlen;
robbiew8541eeb2003-03-27 19:59:32 +0000292
293 memcpy(&rfds, &afds, sizeof(rfds));
294
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200295 if (select(nfds, &rfds, NULL, NULL, NULL) < 0 && errno != EINTR)
296 exit(1);
297
robbiew8541eeb2003-03-27 19:59:32 +0000298 if (FD_ISSET(sfd, &rfds)) {
299 int newfd;
300
301 fromlen = sizeof(fsin);
302 newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen);
Cyril Hrubis7855d092014-10-09 16:13:05 +0200303 if (newfd >= 0) {
robbiew8541eeb2003-03-27 19:59:32 +0000304 FD_SET(newfd, &afds);
Cyril Hrubis7855d092014-10-09 16:13:05 +0200305 nfds = MAX(nfds, newfd + 1);
306 }
robbiew8541eeb2003-03-27 19:59:32 +0000307 }
subrata_modak56207ce2009-03-23 13:35:39 +0000308 for (fd = 0; fd < nfds; ++fd) {
robbiew8541eeb2003-03-27 19:59:32 +0000309 if (fd != sfd && FD_ISSET(fd, &rfds)) {
310 cc = read(fd, buf, sizeof(buf));
311 if (cc == 0 || (cc < 0 && errno != EINTR)) {
subrata_modak56207ce2009-03-23 13:35:39 +0000312 (void)close(fd);
robbiew8541eeb2003-03-27 19:59:32 +0000313 FD_CLR(fd, &afds);
314 }
315 }
316 }
317 }
318}
319
subrata_modak56207ce2009-03-23 13:35:39 +0000320int main(int ac, char *av[])
plars865695b2001-08-27 22:15:12 +0000321{
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200322 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200323 const char *msg;
plars865695b2001-08-27 22:15:12 +0000324
Garrett Coopera9e49f12010-12-16 10:54:03 -0800325 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800326 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200327
robbiewd34d5812005-07-11 22:28:09 +0000328#ifdef UCLINUX
329 argv0 = av[0];
330 maybe_run_child(&do_child, "d", &sfd);
331#endif
332
plars865695b2001-08-27 22:15:12 +0000333 setup();
334
plars865695b2001-08-27 22:15:12 +0000335 for (lc = 0; TEST_LOOPING(lc); ++lc) {
336
Caspar Zhangd59a6592013-03-07 14:59:12 +0800337 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000338 for (testno = 0; testno < TST_TOTAL; ++testno) {
plars865695b2001-08-27 22:15:12 +0000339 tdat[testno].setup();
340
341 TEST(sendto(s, tdat[testno].buf, tdat[testno].buflen,
subrata_modak56207ce2009-03-23 13:35:39 +0000342 tdat[testno].flags,
343 (const struct sockaddr *)tdat[testno].to,
344 tdat[testno].tolen));
plars865695b2001-08-27 22:15:12 +0000345
346 if (TEST_RETURN > 0)
Cyril Hrubis605fa332015-02-04 13:11:20 +0100347 TEST_RETURN = 0;
plars865695b2001-08-27 22:15:12 +0000348
349 if (TEST_RETURN != tdat[testno].retval ||
350 (TEST_RETURN < 0 &&
351 TEST_ERRNO != tdat[testno].experrno)) {
352 tst_resm(TFAIL, "%s ; returned"
vapierc450e062009-08-28 14:28:52 +0000353 " %ld (expected %d), errno %d (expected"
subrata_modak56207ce2009-03-23 13:35:39 +0000354 " %d)", tdat[testno].desc,
355 TEST_RETURN, tdat[testno].retval,
356 TEST_ERRNO, tdat[testno].experrno);
plars865695b2001-08-27 22:15:12 +0000357 } else {
358 tst_resm(TPASS, "%s successful",
subrata_modak56207ce2009-03-23 13:35:39 +0000359 tdat[testno].desc);
plars865695b2001-08-27 22:15:12 +0000360 }
361 tdat[testno].cleanup();
362 }
363 }
364 cleanup();
365
Garrett Cooper53740502010-12-16 00:04:01 -0800366 tst_exit();
plars865695b2001-08-27 22:15:12 +0000367}
368
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200369static pid_t server_pid;
plars865695b2001-08-27 22:15:12 +0000370
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200371static void setup(void)
plars865695b2001-08-27 22:15:12 +0000372{
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200373 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000374
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200375 server_pid = start_server(&sin1);
plars865695b2001-08-27 22:15:12 +0000376
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200377 signal(SIGPIPE, SIG_IGN);
plars865695b2001-08-27 22:15:12 +0000378}
379
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200380static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000381{
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200382 kill(server_pid, SIGKILL);
plars865695b2001-08-27 22:15:12 +0000383}
384
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200385static void setup0(void)
plars865695b2001-08-27 22:15:12 +0000386{
387 if (tdat[testno].experrno == EBADF)
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200388 s = 400;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800389 else if ((s = open("/dev/null", O_WRONLY)) == -1)
390 tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null) failed");
plars865695b2001-08-27 22:15:12 +0000391}
392
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200393static void cleanup0(void)
plars865695b2001-08-27 22:15:12 +0000394{
395 s = -1;
396}
397
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200398static void setup1(void)
plars865695b2001-08-27 22:15:12 +0000399{
400 s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
vapierc450e062009-08-28 14:28:52 +0000401 if (s < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800402 tst_brkm(TBROK | TERRNO, cleanup, "socket setup failed");
vapierc450e062009-08-28 14:28:52 +0000403 if (connect(s, (const struct sockaddr *)&sin1, sizeof(sin1)) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800404 tst_brkm(TBROK | TERRNO, cleanup, "connect failed");
plars865695b2001-08-27 22:15:12 +0000405}
406
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200407static void cleanup1(void)
plars865695b2001-08-27 22:15:12 +0000408{
subrata_modak56207ce2009-03-23 13:35:39 +0000409 (void)close(s);
plars865695b2001-08-27 22:15:12 +0000410 s = -1;
411}
412
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200413static void setup2(void)
plars865695b2001-08-27 22:15:12 +0000414{
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200415 setup1();
vapierc450e062009-08-28 14:28:52 +0000416 if (shutdown(s, 1) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800417 tst_brkm(TBROK | TERRNO, cleanup, "socket setup failed connect "
vapierc450e062009-08-28 14:28:52 +0000418 "test %d", testno);
plars865695b2001-08-27 22:15:12 +0000419}
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200420
421static void setup3(void)
plars865695b2001-08-27 22:15:12 +0000422{
423 s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
vapierc450e062009-08-28 14:28:52 +0000424 if (s < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800425 tst_brkm(TBROK | TERRNO, cleanup, "socket setup failed");
Cyril Hrubis10c580b2012-10-10 12:49:02 +0200426}