plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 4 | * Copyright (c) Cyril Hrubis <chrubis@suse.cz> 2012 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Test Name: sendto01 |
| 23 | * |
| 24 | * Test Description: |
| 25 | * Verify that sendto() returns the proper errno for various failure cases |
| 26 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | * HISTORY |
| 28 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | #include <stdio.h> |
| 32 | #include <unistd.h> |
| 33 | #include <errno.h> |
plars | 393f634 | 2002-03-26 20:45:06 +0000 | [diff] [blame] | 34 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 35 | |
| 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 45 | char *TCID = "sendto01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | int testno; |
| 47 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 48 | static char buf[1024], bigbuf[128 * 1024]; |
| 49 | static int s; |
| 50 | static struct sockaddr_in sin1, sin2; |
| 51 | static int sfd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | |
| 53 | struct test_case_t { /* test case structure */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 54 | 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 */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | struct sockaddr_in *to; /* destination */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 61 | int tolen; /* length of "to" buffer */ |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 62 | int retval; |
| 63 | int experrno; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 64 | void (*setup) (void); |
| 65 | void (*cleanup) (void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | char *desc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 69 | static void setup(void); |
| 70 | static void setup0(void); |
| 71 | static void setup1(void); |
| 72 | static void setup2(void); |
| 73 | static void setup3(void); |
| 74 | static void cleanup(void); |
| 75 | static void cleanup0(void); |
| 76 | static void cleanup1(void); |
| 77 | static void do_child(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 79 | struct 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 92 | .desc = "bad file descriptor"} |
| 93 | , |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 94 | {.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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | .desc = "invalid socket"} |
| 107 | , |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 108 | #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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 122 | .desc = "invalid send buffer"} |
| 123 | , |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 124 | #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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 137 | .desc = "connected TCP"} |
| 138 | , |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 139 | {.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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 151 | .desc = "not connected TCP"} |
| 152 | , |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 153 | {.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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 165 | .desc = "invalid to buffer length"} |
| 166 | , |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 167 | #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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 181 | .desc = "invalid to buffer"} |
| 182 | , |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 183 | #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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 196 | .desc = "UDP message too big"} |
| 197 | , |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 198 | {.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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 210 | .desc = "local endpoint shutdown"} |
| 211 | , |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 212 | {.domain = PF_INET, |
Jan Stancek | 1c6354e | 2013-11-29 11:39:34 +0100 | [diff] [blame] | 213 | .type = SOCK_DGRAM, |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 214 | .proto = 0, |
| 215 | .buf = buf, |
| 216 | .buflen = sizeof(buf), |
Jan Stancek | 1c6354e | 2013-11-29 11:39:34 +0100 | [diff] [blame] | 217 | .flags = MSG_OOB, |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 218 | .to = &sin1, |
| 219 | .tolen = sizeof(sin1), |
Jan Stancek | 1c6354e | 2013-11-29 11:39:34 +0100 | [diff] [blame] | 220 | .retval = -1, |
| 221 | .experrno = EOPNOTSUPP, |
| 222 | .setup = setup1, |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 223 | .cleanup = cleanup1, |
| 224 | .desc = "invalid flags set"} |
| 225 | }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 226 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 227 | int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); |
| 228 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 229 | #ifdef UCLINUX |
| 230 | static char *argv0; |
| 231 | #endif |
| 232 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 233 | static pid_t start_server(struct sockaddr_in *sin0) |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 234 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 235 | pid_t pid; |
Jan Stancek | 6e4e77a | 2014-03-07 11:39:56 +0100 | [diff] [blame] | 236 | 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; |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 241 | |
| 242 | sfd = socket(PF_INET, SOCK_STREAM, 0); |
| 243 | if (sfd < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 244 | tst_brkm(TBROK | TERRNO, cleanup, "server socket failed"); |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 245 | return -1; |
| 246 | } |
Jan Stancek | 6e4e77a | 2014-03-07 11:39:56 +0100 | [diff] [blame] | 247 | if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 248 | tst_brkm(TBROK | TERRNO, cleanup, "server bind failed"); |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 249 | return -1; |
| 250 | } |
| 251 | if (listen(sfd, 10) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 252 | tst_brkm(TBROK | TERRNO, cleanup, "server listen failed"); |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 253 | return -1; |
| 254 | } |
Jan Stancek | 6e4e77a | 2014-03-07 11:39:56 +0100 | [diff] [blame] | 255 | if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1) |
| 256 | tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed"); |
| 257 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 258 | switch ((pid = FORK_OR_VFORK())) { |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 259 | case 0: |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 260 | #ifdef UCLINUX |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 261 | if (self_exec(argv0, "d", sfd) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 262 | tst_brkm(TBROK | TERRNO, cleanup, |
| 263 | "server self_exec failed"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 264 | #else |
| 265 | do_child(); |
| 266 | #endif |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 267 | break; |
| 268 | case -1: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 269 | tst_brkm(TBROK | TERRNO, cleanup, "server fork failed"); |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 270 | default: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 271 | (void)close(sfd); |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 272 | return pid; |
| 273 | } |
| 274 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 275 | exit(1); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 278 | static void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 279 | { |
| 280 | struct sockaddr_in fsin; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 281 | fd_set afds, rfds; |
| 282 | int nfds, cc, fd; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 283 | |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 284 | FD_ZERO(&afds); |
| 285 | FD_SET(sfd, &afds); |
| 286 | |
Cyril Hrubis | 7855d09 | 2014-10-09 16:13:05 +0200 | [diff] [blame] | 287 | nfds = sfd + 1; |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 288 | |
| 289 | /* accept connections until killed */ |
| 290 | while (1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 291 | socklen_t fromlen; |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 292 | |
| 293 | memcpy(&rfds, &afds, sizeof(rfds)); |
| 294 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 295 | if (select(nfds, &rfds, NULL, NULL, NULL) < 0 && errno != EINTR) |
| 296 | exit(1); |
| 297 | |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 298 | if (FD_ISSET(sfd, &rfds)) { |
| 299 | int newfd; |
| 300 | |
| 301 | fromlen = sizeof(fsin); |
| 302 | newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen); |
Cyril Hrubis | 7855d09 | 2014-10-09 16:13:05 +0200 | [diff] [blame] | 303 | if (newfd >= 0) { |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 304 | FD_SET(newfd, &afds); |
Cyril Hrubis | 7855d09 | 2014-10-09 16:13:05 +0200 | [diff] [blame] | 305 | nfds = MAX(nfds, newfd + 1); |
| 306 | } |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 307 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 308 | for (fd = 0; fd < nfds; ++fd) { |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 309 | if (fd != sfd && FD_ISSET(fd, &rfds)) { |
| 310 | cc = read(fd, buf, sizeof(buf)); |
| 311 | if (cc == 0 || (cc < 0 && errno != EINTR)) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 312 | (void)close(fd); |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 313 | FD_CLR(fd, &afds); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 320 | int main(int ac, char *av[]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 321 | { |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 322 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 323 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 324 | |
Garrett Cooper | a9e49f1 | 2010-12-16 10:54:03 -0800 | [diff] [blame] | 325 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 326 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 327 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 328 | #ifdef UCLINUX |
| 329 | argv0 = av[0]; |
| 330 | maybe_run_child(&do_child, "d", &sfd); |
| 331 | #endif |
| 332 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 333 | setup(); |
| 334 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 335 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
| 336 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 337 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 338 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 339 | tdat[testno].setup(); |
| 340 | |
| 341 | TEST(sendto(s, tdat[testno].buf, tdat[testno].buflen, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 342 | tdat[testno].flags, |
| 343 | (const struct sockaddr *)tdat[testno].to, |
| 344 | tdat[testno].tolen)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 345 | |
| 346 | if (TEST_RETURN > 0) |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 347 | TEST_RETURN = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 348 | |
| 349 | if (TEST_RETURN != tdat[testno].retval || |
| 350 | (TEST_RETURN < 0 && |
| 351 | TEST_ERRNO != tdat[testno].experrno)) { |
| 352 | tst_resm(TFAIL, "%s ; returned" |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 353 | " %ld (expected %d), errno %d (expected" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 354 | " %d)", tdat[testno].desc, |
| 355 | TEST_RETURN, tdat[testno].retval, |
| 356 | TEST_ERRNO, tdat[testno].experrno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 357 | } else { |
| 358 | tst_resm(TPASS, "%s successful", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 359 | tdat[testno].desc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 360 | } |
| 361 | tdat[testno].cleanup(); |
| 362 | } |
| 363 | } |
| 364 | cleanup(); |
| 365 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 366 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 369 | static pid_t server_pid; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 370 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 371 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 372 | { |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 373 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 374 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 375 | server_pid = start_server(&sin1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 376 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 377 | signal(SIGPIPE, SIG_IGN); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 380 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 381 | { |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 382 | kill(server_pid, SIGKILL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 385 | static void setup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 386 | { |
| 387 | if (tdat[testno].experrno == EBADF) |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 388 | s = 400; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 389 | else if ((s = open("/dev/null", O_WRONLY)) == -1) |
| 390 | tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 393 | static void cleanup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 394 | { |
| 395 | s = -1; |
| 396 | } |
| 397 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 398 | static void setup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 399 | { |
| 400 | s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto); |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 401 | if (s < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 402 | tst_brkm(TBROK | TERRNO, cleanup, "socket setup failed"); |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 403 | if (connect(s, (const struct sockaddr *)&sin1, sizeof(sin1)) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 404 | tst_brkm(TBROK | TERRNO, cleanup, "connect failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 407 | static void cleanup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 408 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 409 | (void)close(s); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 410 | s = -1; |
| 411 | } |
| 412 | |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 413 | static void setup2(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 414 | { |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 415 | setup1(); |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 416 | if (shutdown(s, 1) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 417 | tst_brkm(TBROK | TERRNO, cleanup, "socket setup failed connect " |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 418 | "test %d", testno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 419 | } |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 420 | |
| 421 | static void setup3(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 422 | { |
| 423 | s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto); |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 424 | if (s < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 425 | tst_brkm(TBROK | TERRNO, cleanup, "socket setup failed"); |
Cyril Hrubis | 10c580b | 2012-10-10 12:49:02 +0200 | [diff] [blame] | 426 | } |