plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +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: send01 |
| 23 | * |
| 24 | * Test Description: |
| 25 | * Verify that send() 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 |
| 29 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <unistd.h> |
| 34 | #include <errno.h> |
plars | 0c850b7 | 2002-03-26 20:39:19 +0000 | [diff] [blame] | 35 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 46 | char *TCID = "send01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | int testno; |
| 48 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 49 | static char buf[1024], bigbuf[128 * 1024]; |
| 50 | static int s; |
| 51 | static struct sockaddr_in sin1; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 52 | static int sfd; /* shared between do_child and start_server */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
| 54 | struct test_case_t { /* test case structure */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 55 | int domain; /* PF_INET, PF_UNIX, ... */ |
| 56 | int type; /* SOCK_STREAM, SOCK_DGRAM ... */ |
| 57 | int proto; /* protocol number (usually 0 = default) */ |
| 58 | void *buf; /* send data buffer */ |
| 59 | int buflen; /* send's 3rd argument */ |
| 60 | unsigned flags; /* send's 4th argument */ |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 61 | int retval; |
| 62 | int experrno; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 63 | void (*setup) (void); |
| 64 | void (*cleanup) (void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | char *desc; |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static void cleanup(void); |
| 69 | static void do_child(void); |
| 70 | static void setup(void); |
| 71 | static void setup0(void); |
| 72 | static void setup1(void); |
| 73 | static void setup2(void); |
| 74 | static void cleanup0(void); |
| 75 | static void cleanup1(void); |
| 76 | |
| 77 | static struct test_case_t tdat[] = { |
| 78 | {.domain = PF_INET, |
| 79 | .type = SOCK_STREAM, |
| 80 | .proto = 0, |
| 81 | .buf = buf, |
| 82 | .buflen = sizeof(buf), |
| 83 | .flags = 0, |
| 84 | .retval = -1, |
| 85 | .experrno = EBADF, |
| 86 | .setup = setup0, |
| 87 | .cleanup = cleanup0, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 88 | .desc = "bad file descriptor"} |
| 89 | , |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 90 | {.domain = 0, |
| 91 | .type = 0, |
| 92 | .proto = 0, |
| 93 | .buf = buf, |
| 94 | .buflen = sizeof(buf), |
| 95 | .flags = 0, |
| 96 | .retval = -1, |
| 97 | .experrno = ENOTSOCK, |
| 98 | .setup = setup0, |
| 99 | .cleanup = cleanup0, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 100 | .desc = "invalid socket"} |
| 101 | , |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 102 | #ifndef UCLINUX |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 103 | /* Skip since uClinux does not implement memory protection */ |
| 104 | {.domain = PF_INET, |
| 105 | .type = SOCK_STREAM, |
| 106 | .proto = 0, |
| 107 | .buf = (void *)-1, |
| 108 | .buflen = sizeof(buf), |
| 109 | .flags = 0, |
| 110 | .retval = -1, |
| 111 | .experrno = EFAULT, |
| 112 | .setup = setup1, |
| 113 | .cleanup = cleanup1, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 114 | .desc = "invalid send buffer"} |
| 115 | , |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 116 | #endif |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 117 | {.domain = PF_INET, |
| 118 | .type = SOCK_DGRAM, |
| 119 | .proto = 0, |
| 120 | .buf = bigbuf, |
| 121 | .buflen = sizeof(bigbuf), |
| 122 | .flags = 0, |
| 123 | .retval = -1, |
| 124 | .experrno = EMSGSIZE, |
| 125 | .setup = setup1, |
| 126 | .cleanup = cleanup1, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 127 | .desc = "UDP message too big"} |
| 128 | , |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 129 | {.domain = PF_INET, |
| 130 | .type = SOCK_STREAM, |
| 131 | .proto = 0, |
| 132 | .buf = buf, |
| 133 | .buflen = sizeof(buf), |
| 134 | .flags = 0, |
| 135 | .retval = -1, |
| 136 | .experrno = EPIPE, |
| 137 | .setup = setup2, |
| 138 | .cleanup = cleanup1, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 139 | .desc = "local endpoint shutdown"} |
| 140 | , |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 141 | #ifndef UCLINUX |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 142 | /* Skip since uClinux does not implement memory protection */ |
| 143 | {.domain = PF_INET, |
Jan Stancek | 1c6354e | 2013-11-29 11:39:34 +0100 | [diff] [blame] | 144 | .type = SOCK_DGRAM, |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 145 | .proto = 0, |
Jan Stancek | 1c6354e | 2013-11-29 11:39:34 +0100 | [diff] [blame] | 146 | .buf = buf, |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 147 | .buflen = sizeof(buf), |
Jan Stancek | 1c6354e | 2013-11-29 11:39:34 +0100 | [diff] [blame] | 148 | .flags = MSG_OOB, |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 149 | .retval = -1, |
Jan Stancek | 1c6354e | 2013-11-29 11:39:34 +0100 | [diff] [blame] | 150 | .experrno = EOPNOTSUPP, |
| 151 | .setup = setup1, |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 152 | .cleanup = cleanup1, |
| 153 | .desc = "invalid flags set"} |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 154 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 157 | int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 159 | #ifdef UCLINUX |
| 160 | static char *argv0; |
| 161 | #endif |
| 162 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 163 | static pid_t start_server(struct sockaddr_in *sin0) |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 164 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 165 | pid_t pid; |
Jan Stancek | 6e4e77a | 2014-03-07 11:39:56 +0100 | [diff] [blame] | 166 | socklen_t slen = sizeof(*sin0); |
| 167 | |
| 168 | sin0->sin_family = AF_INET; |
| 169 | sin0->sin_port = 0; /* pick random free port */ |
| 170 | sin0->sin_addr.s_addr = INADDR_ANY; |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 171 | |
| 172 | sfd = socket(PF_INET, SOCK_STREAM, 0); |
| 173 | if (sfd < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 174 | tst_brkm(TBROK | TERRNO, cleanup, "server socket failed"); |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 175 | return -1; |
| 176 | } |
Jan Stancek | 6e4e77a | 2014-03-07 11:39:56 +0100 | [diff] [blame] | 177 | if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 178 | tst_brkm(TBROK | TERRNO, cleanup, "server bind failed"); |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 179 | return -1; |
| 180 | } |
| 181 | if (listen(sfd, 10) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 182 | tst_brkm(TBROK | TERRNO, cleanup, "server listen failed"); |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 183 | return -1; |
| 184 | } |
Jan Stancek | 6e4e77a | 2014-03-07 11:39:56 +0100 | [diff] [blame] | 185 | if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1) |
| 186 | tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed"); |
| 187 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 188 | switch ((pid = FORK_OR_VFORK())) { |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 189 | case 0: |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 190 | #ifdef UCLINUX |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 191 | if (self_exec(argv0, "d", sfd) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 192 | tst_brkm(TBROK | TERRNO, cleanup, |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 193 | "server self_exec failed"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 194 | #else |
| 195 | do_child(); |
| 196 | #endif |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 197 | break; |
| 198 | case -1: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 199 | tst_brkm(TBROK | TERRNO, cleanup, "server fork failed"); |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 200 | default: |
| 201 | close(sfd); |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 202 | return pid; |
| 203 | } |
| 204 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 205 | exit(1); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 208 | static void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 209 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 210 | fd_set afds, rfds; |
| 211 | int nfds, cc, fd; |
| 212 | struct sockaddr_in fsin; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 213 | |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 214 | FD_ZERO(&afds); |
| 215 | FD_SET(sfd, &afds); |
| 216 | |
Cyril Hrubis | 7855d09 | 2014-10-09 16:13:05 +0200 | [diff] [blame] | 217 | nfds = sfd + 1; |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 218 | |
| 219 | /* accept connections until killed */ |
| 220 | while (1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 221 | socklen_t fromlen; |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 222 | |
| 223 | memcpy(&rfds, &afds, sizeof(rfds)); |
| 224 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 225 | if (select(nfds, &rfds, NULL, NULL, NULL) < 0) |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 226 | if (errno != EINTR) |
| 227 | exit(1); |
| 228 | if (FD_ISSET(sfd, &rfds)) { |
| 229 | int newfd; |
| 230 | |
| 231 | fromlen = sizeof(fsin); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 232 | newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen); |
Cyril Hrubis | 7855d09 | 2014-10-09 16:13:05 +0200 | [diff] [blame] | 233 | if (newfd >= 0) { |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 234 | FD_SET(newfd, &afds); |
Cyril Hrubis | 7855d09 | 2014-10-09 16:13:05 +0200 | [diff] [blame] | 235 | nfds = MAX(nfds, newfd + 1); |
| 236 | } |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 237 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 238 | for (fd = 0; fd < nfds; ++fd) { |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 239 | if (fd != sfd && FD_ISSET(fd, &rfds)) { |
| 240 | cc = read(fd, buf, sizeof(buf)); |
| 241 | if (cc == 0 || (cc < 0 && errno != EINTR)) { |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 242 | close(fd); |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 243 | FD_CLR(fd, &afds); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 250 | int main(int ac, char *av[]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 251 | { |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 252 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 253 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 254 | |
Garrett Cooper | a9e49f1 | 2010-12-16 10:54:03 -0800 | [diff] [blame] | 255 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 256 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 257 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 258 | #ifdef UCLINUX |
| 259 | argv0 = av[0]; |
| 260 | maybe_run_child(&do_child, "d", &sfd); |
| 261 | #endif |
| 262 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 263 | setup(); |
| 264 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 265 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
| 266 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 267 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 268 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 269 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 270 | tdat[testno].setup(); |
| 271 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 272 | TEST(send(s, tdat[testno].buf, tdat[testno].buflen, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 273 | tdat[testno].flags)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 274 | |
| 275 | if (TEST_RETURN != -1) { |
| 276 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 277 | continue; |
| 278 | } |
| 279 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 280 | if (TEST_ERRNO != tdat[testno].experrno) { |
| 281 | tst_resm(TFAIL, "%s ; returned" |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 282 | " %ld (expected %d), errno %d (expected" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 283 | " %d)", tdat[testno].desc, |
| 284 | TEST_RETURN, tdat[testno].retval, |
| 285 | TEST_ERRNO, tdat[testno].experrno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 286 | } else { |
| 287 | tst_resm(TPASS, "%s successful", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 288 | tdat[testno].desc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 289 | } |
| 290 | tdat[testno].cleanup(); |
| 291 | } |
| 292 | } |
| 293 | cleanup(); |
| 294 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 295 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 298 | static pid_t server_pid; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 299 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 300 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 301 | { |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 302 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 303 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 304 | server_pid = start_server(&sin1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 305 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 306 | signal(SIGPIPE, SIG_IGN); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 309 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 310 | { |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 311 | kill(server_pid, SIGKILL); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 312 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 315 | static void setup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 316 | { |
| 317 | if (tdat[testno].experrno == EBADF) |
| 318 | s = 400; /* anything not an open file */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 319 | else if ((s = open("/dev/null", O_WRONLY)) == -1) |
| 320 | tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 323 | static void cleanup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 324 | { |
| 325 | s = -1; |
| 326 | } |
| 327 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 328 | static void setup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 329 | { |
| 330 | s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto); |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 331 | if (s < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 332 | tst_brkm(TBROK | TERRNO, cleanup, "socket setup failed"); |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 333 | if (connect(s, (const struct sockaddr *)&sin1, sizeof(sin1)) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 334 | tst_brkm(TBROK | TERRNO, cleanup, "connect failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 337 | static void cleanup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 338 | { |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 339 | close(s); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 340 | s = -1; |
| 341 | } |
| 342 | |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 343 | static void setup2(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 344 | { |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 345 | setup1(); |
| 346 | |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 347 | if (shutdown(s, 1) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 348 | tst_brkm(TBROK | TERRNO, cleanup, "socket setup failed connect " |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 349 | "test %d", testno); |
Cyril Hrubis | e90b03c | 2012-10-10 11:40:24 +0200 | [diff] [blame] | 350 | } |