plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Name: recvmsg01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that recvmsg() returns the proper errno for various failure cases |
| 25 | * |
| 26 | * Usage: <for command-line> |
| 27 | * recvmsg01 [-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 | |
robbiew | daafaa0 | 2003-04-28 14:45:42 +0000 | [diff] [blame] | 43 | /* The #ifndef code below is for 2.5 64-bit kernels, where */ |
| 44 | /* the MSG_CMSG_COMPAT flag must be 0 in order for the syscall */ |
| 45 | /* and this test to function correctly. */ |
| 46 | #ifndef MSG_CMSG_COMPAT |
robbiew | a26ed81 | 2003-06-17 21:26:54 +0000 | [diff] [blame] | 47 | |
subrata_modak | bea91c2 | 2008-10-29 06:16:59 +0000 | [diff] [blame] | 48 | #if defined (__powerpc64__) || defined (__mips64) || defined (__x86_64__) || defined (__sparc64__) |
robbiew | a26ed81 | 2003-06-17 21:26:54 +0000 | [diff] [blame] | 49 | #define MSG_CMSG_COMPAT 0x80000000 |
| 50 | #else |
robbiew | daafaa0 | 2003-04-28 14:45:42 +0000 | [diff] [blame] | 51 | #define MSG_CMSG_COMPAT 0 |
robbiew | 869fdf7 | 2003-06-18 17:00:05 +0000 | [diff] [blame] | 52 | #endif |
robbiew | a26ed81 | 2003-06-17 21:26:54 +0000 | [diff] [blame] | 53 | |
robbiew | daafaa0 | 2003-04-28 14:45:42 +0000 | [diff] [blame] | 54 | #endif |
| 55 | /***************************************************/ |
| 56 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | #include <stdio.h> |
| 58 | #include <unistd.h> |
vapier | 32cc0ac | 2006-06-22 04:28:32 +0000 | [diff] [blame] | 59 | #include <string.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | #include <errno.h> |
plars | d8afa78 | 2002-05-06 21:55:58 +0000 | [diff] [blame] | 61 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
| 63 | #include <sys/types.h> |
| 64 | #include <sys/socket.h> |
| 65 | #include <sys/signal.h> |
| 66 | #include <sys/uio.h> |
| 67 | #include <sys/un.h> |
| 68 | #include <sys/file.h> |
| 69 | |
| 70 | #include <netinet/in.h> |
| 71 | |
| 72 | #include "test.h" |
| 73 | #include "usctest.h" |
| 74 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 75 | char *TCID = "recvmsg01"; /* Test program identifier. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | int testno; |
| 77 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 78 | char buf[1024], cbuf[1024]; |
| 79 | int s; /* socket descriptor */ |
| 80 | int passed_fd = -1; /* rights-passing test descriptor */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | struct sockaddr_in sin1, from; |
| 82 | struct sockaddr_un sun1; |
| 83 | struct msghdr msgdat; |
| 84 | struct cmsghdr *control = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | int controllen = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | struct iovec iov[1]; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 87 | static int sfd; /* shared between do_child and start_server */ |
| 88 | static int ufd; /* shared between do_child and start_server */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 90 | void setup(void); |
| 91 | void setup0(void); |
| 92 | void setup1(void); |
| 93 | void setup2(void); |
| 94 | void setup3(void); |
| 95 | void setup4(void); |
| 96 | void cleanup(void); |
| 97 | void cleanup0(void); |
| 98 | void cleanup1(void); |
| 99 | void cleanup2(void); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 100 | void do_child(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
| 102 | void sender(int); |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 103 | pid_t start_server(struct sockaddr_in *, struct sockaddr_un *); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | |
| 105 | struct test_case_t { /* test case structure */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | int domain; /* PF_INET, PF_UNIX, ... */ |
| 107 | int type; /* SOCK_STREAM, SOCK_DGRAM ... */ |
| 108 | int proto; /* protocol number (usually 0 = default) */ |
| 109 | struct iovec *iov; |
| 110 | int iovcnt; |
| 111 | void *buf; /* recv data buffer */ |
| 112 | int buflen; /* recv buffer length */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | struct msghdr *msg; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 114 | unsigned flags; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | struct sockaddr *from; /* from address */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | int fromlen; /* from address value/result buffer length */ |
| 117 | int retval; /* syscall return value */ |
| 118 | int experrno; /* expected errno */ |
| 119 | void (*setup) (void); |
| 120 | void (*cleanup) (void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | char *desc; |
| 122 | } tdat[] = { |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 123 | /* 1 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | { |
| 125 | PF_INET, SOCK_STREAM, 0, iov, 1, buf, sizeof(buf), &msgdat, 0, |
| 126 | (struct sockaddr *)&from, sizeof(from), |
| 127 | -1, EBADF, setup0, cleanup0, "bad file descriptor"} |
| 128 | , |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 129 | /* 2 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | { |
| 131 | 0, 0, 0, iov, 1, (void *)buf, sizeof(buf), &msgdat, 0, |
| 132 | (struct sockaddr *)&from, sizeof(from), |
| 133 | -1, ENOTSOCK, setup0, cleanup0, "invalid socket"} |
| 134 | , |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 135 | /* 3 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 136 | { |
| 137 | PF_INET, SOCK_STREAM, 0, iov, 1, (void *)buf, sizeof(buf), |
| 138 | &msgdat, 0, (struct sockaddr *)-1, sizeof(from), 0, |
| 139 | ENOTSOCK, setup1, cleanup1, "invalid socket buffer"} |
| 140 | , |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 141 | /* 4 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 142 | { |
| 143 | PF_INET, SOCK_STREAM, 0, iov, 1, (void *)buf, sizeof(buf), |
| 144 | &msgdat, -1, (struct sockaddr *)&from, -1, -1, |
| 145 | EINVAL, setup1, cleanup1, "invalid socket length"}, |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 146 | /* 5 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 147 | { |
| 148 | PF_INET, SOCK_STREAM, 0, iov, 1, (void *)-1, sizeof(buf), |
| 149 | &msgdat, 0, (struct sockaddr *)&from, sizeof(from), |
| 150 | -1, EFAULT, setup1, cleanup1, "invalid recv buffer"} |
| 151 | , |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 152 | /* 6 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 153 | { |
| 154 | PF_INET, SOCK_STREAM, 0, 0, 1, (void *)buf, sizeof(buf), |
| 155 | &msgdat, 0, (struct sockaddr *)&from, sizeof(from), |
| 156 | -1, EFAULT, setup1, cleanup1, "invalid iovec buffer"} |
| 157 | , |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 158 | /* 7 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | { |
| 160 | PF_INET, SOCK_STREAM, 0, iov, -1, (void *)buf, sizeof(buf), |
| 161 | &msgdat, 0, (struct sockaddr *)&from, sizeof(from), |
| 162 | -1, EMSGSIZE, setup1, cleanup1, "invalid iovec count"} |
| 163 | , |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 164 | /* 8 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 165 | { |
| 166 | PF_UNIX, SOCK_STREAM, 0, iov, 1, (void *)buf, sizeof(buf), |
| 167 | &msgdat, 0, (struct sockaddr *)&from, sizeof(from), |
| 168 | 0, 0, setup2, cleanup2, "rights reception"} |
| 169 | , |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 170 | /* 9 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | { |
| 172 | PF_INET, SOCK_STREAM, 0, iov, 1, (void *)buf, sizeof(buf), |
| 173 | &msgdat, ~MSG_CMSG_COMPAT, (struct sockaddr *)&from, |
| 174 | sizeof(from), -1, EINVAL, setup1, cleanup1, |
| 175 | "invalid flags set"} |
| 176 | , |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 177 | /* 10 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 178 | { |
| 179 | PF_UNIX, SOCK_STREAM, 0, iov, 1, (void *)buf, sizeof(buf), |
| 180 | &msgdat, 0, (struct sockaddr *)&from, sizeof(from), |
| 181 | 0, EINVAL, setup3, cleanup2, "invalid cmsg length"} |
| 182 | , |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 183 | /* 11 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 184 | { |
| 185 | PF_UNIX, SOCK_STREAM, 0, iov, 1, (void *)buf, sizeof(buf), |
| 186 | &msgdat, 0, (struct sockaddr *)&from, sizeof(from), |
| 187 | 0, 0, setup4, cleanup2, "large cmesg length"} |
| 188 | ,}; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 190 | int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); /* Total number of test cases. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 192 | int exp_enos[] = { EBADF, ENOTSOCK, EFAULT, EINVAL, 0 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 193 | |
| 194 | extern int Tst_count; |
| 195 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 196 | #ifdef UCLINUX |
| 197 | static char *argv0; |
| 198 | #endif |
| 199 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 200 | int main(int argc, char *argv[]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | { |
| 202 | int lc; /* loop counter */ |
| 203 | char *msg; /* message returned from parse_opts */ |
| 204 | |
| 205 | /* Parse standard options given to run the test. */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 206 | msg = parse_opts(argc, argv, NULL, NULL); |
| 207 | if (msg != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame^] | 208 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 210 | #ifdef UCLINUX |
| 211 | argv0 = argv[0]; |
| 212 | maybe_run_child(&do_child, "dd", &sfd, &ufd); |
| 213 | #endif |
| 214 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 215 | setup(); |
| 216 | |
| 217 | TEST_EXP_ENOS(exp_enos); |
| 218 | |
| 219 | /* Check looping state if -i option given */ |
| 220 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
| 221 | Tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 222 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 223 | tdat[testno].setup(); |
| 224 | |
| 225 | /* setup common to all tests */ |
| 226 | |
| 227 | iov[0].iov_base = tdat[testno].buf; |
| 228 | iov[0].iov_len = tdat[testno].buflen; |
| 229 | msgdat.msg_name = tdat[testno].from; |
| 230 | msgdat.msg_namelen = tdat[testno].fromlen; |
| 231 | msgdat.msg_iov = tdat[testno].iov; |
| 232 | msgdat.msg_iovlen = tdat[testno].iovcnt; |
| 233 | msgdat.msg_control = control; |
| 234 | msgdat.msg_controllen = controllen; |
| 235 | msgdat.msg_flags = 0; |
| 236 | |
| 237 | TEST(recvmsg(s, tdat[testno].msg, tdat[testno].flags)); |
| 238 | if (TEST_RETURN >= 0) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 239 | TEST_RETURN = 0; /* all nonzero equal here */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 240 | if (TEST_RETURN != tdat[testno].retval || |
| 241 | (TEST_RETURN < 0 && |
| 242 | TEST_ERRNO != tdat[testno].experrno)) { |
| 243 | tst_resm(TFAIL, "%s ; returned" |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 244 | " %ld (expected %d), errno %d (expected" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 245 | " %d)", tdat[testno].desc, |
| 246 | TEST_RETURN, tdat[testno].retval, |
| 247 | TEST_ERRNO, tdat[testno].experrno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 248 | } else { |
| 249 | TEST_ERROR_LOG(TEST_ERRNO); |
| 250 | tst_resm(TPASS, "%s successful", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 251 | tdat[testno].desc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 252 | } |
| 253 | tdat[testno].cleanup(); |
| 254 | } |
| 255 | } |
| 256 | cleanup(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 257 | /*NOT REACHED */ |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 258 | return 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 259 | } /* End main */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 260 | |
| 261 | pid_t pid; |
| 262 | char tmpsunpath[1024]; |
| 263 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 264 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 265 | { |
plars | 4c78d16 | 2002-09-04 18:56:14 +0000 | [diff] [blame] | 266 | int tfd; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 267 | TEST_PAUSE; /* if -P option specified */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 268 | |
| 269 | /* initialize sockaddr's */ |
| 270 | sin1.sin_family = AF_INET; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 271 | sin1.sin_port = htons((getpid() % 32768) + 11000); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 272 | sin1.sin_addr.s_addr = INADDR_ANY; |
robbiew | de02ee4 | 2003-04-16 21:15:20 +0000 | [diff] [blame] | 273 | tst_tmpdir(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 274 | (void)strcpy(tmpsunpath, "udsockXXXXXX"); |
plars | 4c78d16 | 2002-09-04 18:56:14 +0000 | [diff] [blame] | 275 | tfd = mkstemp(tmpsunpath); |
| 276 | close(tfd); |
| 277 | unlink(tmpsunpath); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 278 | sun1.sun_family = AF_UNIX; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 279 | (void)strcpy(sun1.sun_path, tmpsunpath); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 280 | |
| 281 | signal(SIGPIPE, SIG_IGN); |
| 282 | |
| 283 | pid = start_server(&sin1, &sun1); |
| 284 | } |
| 285 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 286 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 287 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 288 | (void)kill(pid, SIGKILL); /* kill server */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 289 | if (tmpsunpath[0] != '\0') |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 290 | (void)unlink(tmpsunpath); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 291 | TEST_CLEANUP; |
robbiew | de02ee4 | 2003-04-16 21:15:20 +0000 | [diff] [blame] | 292 | tst_rmdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 293 | tst_exit(); |
| 294 | } |
| 295 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 296 | void setup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 297 | { |
| 298 | if (tdat[testno].experrno == EBADF) |
| 299 | s = 400; /* anything not an open file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 300 | else if ((s = open("/dev/null", O_WRONLY)) == -1) |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 301 | tst_brkm(TBROK|TERRNO, cleanup, "open(/dev/null) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 302 | } |
| 303 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 304 | void cleanup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 305 | { |
| 306 | s = -1; |
| 307 | } |
| 308 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 309 | void setup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 310 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 311 | fd_set rdfds; |
| 312 | struct timeval timeout; |
| 313 | int n; |
robbiew | ef95266 | 2003-03-27 15:10:06 +0000 | [diff] [blame] | 314 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 315 | s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto); |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 316 | if (s < 0) |
| 317 | tst_brkm(TBROK|TERRNO, cleanup, "socket setup failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 318 | if (tdat[testno].type == SOCK_STREAM) { |
| 319 | if (tdat[testno].domain == PF_INET) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 320 | if (connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) < |
| 321 | 0) |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 322 | tst_brkm(TBROK|TERRNO, cleanup, "connect failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 323 | /* Wait for something to be readable, else we won't detect EFAULT on recv */ |
| 324 | FD_ZERO(&rdfds); |
| 325 | FD_SET(s, &rdfds); |
| 326 | timeout.tv_sec = 2; |
| 327 | timeout.tv_usec = 0; |
| 328 | n = select(s + 1, &rdfds, 0, 0, &timeout); |
| 329 | if (n != 1 || !FD_ISSET(s, &rdfds)) |
| 330 | tst_brkm(TBROK, cleanup, |
| 331 | "client setup1 failed - no message ready in 2 sec"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 332 | } else if (tdat[testno].domain == PF_UNIX) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 333 | if (connect(s, (struct sockaddr *)&sun1, sizeof(sun1)) < |
| 334 | 0) |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 335 | tst_brkm(TBROK|TERRNO, cleanup, "UD connect failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 340 | void setup2(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 341 | { |
| 342 | setup1(); |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 343 | if (write(s, "R", 1) < 0) |
| 344 | tst_brkm(TBROK|TERRNO, cleanup, "test setup failed: write:"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 345 | control = (struct cmsghdr *)cbuf; |
| 346 | controllen = control->cmsg_len = sizeof(cbuf); |
| 347 | } |
| 348 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 349 | void setup3(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 350 | { |
| 351 | setup2(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 352 | controllen = sizeof(struct cmsghdr) - 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 353 | } |
| 354 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 355 | void setup4(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 356 | { |
| 357 | setup2(); |
| 358 | controllen = 128 * 1024; |
| 359 | } |
| 360 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 361 | void cleanup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 362 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 363 | (void)close(s); |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 364 | close(ufd); |
| 365 | close(sfd); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 366 | s = -1; |
| 367 | } |
| 368 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 369 | void cleanup2(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 370 | { |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 371 | close(ufd); |
| 372 | close(sfd); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 373 | (void)close(s); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 374 | s = -1; |
| 375 | |
| 376 | if (passed_fd >= 0) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 377 | (void)close(passed_fd); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 378 | passed_fd = -1; |
| 379 | control = 0; |
| 380 | controllen = 0; |
| 381 | } |
| 382 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 383 | pid_t start_server(struct sockaddr_in *ssin, struct sockaddr_un *ssun) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 384 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 385 | pid_t pid; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 386 | |
| 387 | /* set up inet socket */ |
| 388 | sfd = socket(PF_INET, SOCK_STREAM, 0); |
| 389 | if (sfd < 0) { |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 390 | tst_brkm(TBROK|TERRNO, cleanup, "server socket failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 391 | return -1; |
| 392 | } |
| 393 | if (bind(sfd, (struct sockaddr *)ssin, sizeof(*ssin)) < 0) { |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 394 | tst_brkm(TBROK|TERRNO, cleanup, "server bind failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 395 | return -1; |
| 396 | } |
| 397 | if (listen(sfd, 10) < 0) { |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 398 | tst_brkm(TBROK|TERRNO, cleanup, "server listen failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 399 | return -1; |
| 400 | } |
| 401 | /* set up UNIX-domain socket */ |
| 402 | ufd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 403 | if (ufd < 0) { |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 404 | tst_brkm(TBROK|TERRNO, cleanup, "server UD socket failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 405 | return -1; |
| 406 | } |
| 407 | if (bind(ufd, (struct sockaddr *)ssun, sizeof(*ssun))) { |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 408 | tst_brkm(TBROK|TERRNO, cleanup, "server UD bind failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 409 | return -1; |
| 410 | } |
| 411 | if (listen(ufd, 10) < 0) { |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 412 | tst_brkm(TBROK|TERRNO, cleanup, "server UD listen failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 413 | return -1; |
| 414 | } |
| 415 | |
vapier | fed9da3 | 2006-02-19 09:25:08 +0000 | [diff] [blame] | 416 | switch ((pid = FORK_OR_VFORK())) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 417 | case 0: /* child */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 418 | #ifdef UCLINUX |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 419 | if (self_exec(argv0, "dd", sfd, ufd) < 0) |
| 420 | tst_brkm(TBROK|TERRNO, cleanup, "server self_exec failed"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 421 | #else |
| 422 | do_child(); |
| 423 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 424 | break; |
| 425 | case -1: |
vapier | c450e06 | 2009-08-28 14:28:52 +0000 | [diff] [blame] | 426 | tst_brkm(TBROK|TERRNO, cleanup, "server fork failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 427 | /* fall through */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 428 | default: /* parent */ |
| 429 | (void)close(sfd); |
| 430 | (void)close(ufd); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 431 | return pid; |
| 432 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 433 | /*NOTREACHED*/ exit(1); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 434 | } |
| 435 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 436 | void do_child() |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 437 | { |
| 438 | struct sockaddr_in fsin; |
| 439 | struct sockaddr_un fsun; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 440 | fd_set afds, rfds; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 441 | int nfds, cc, fd; |
| 442 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 443 | FD_ZERO(&afds); |
| 444 | FD_SET(sfd, &afds); |
| 445 | FD_SET(ufd, &afds); |
| 446 | |
| 447 | nfds = getdtablesize(); |
| 448 | |
| 449 | /* accept connections until killed */ |
| 450 | while (1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 451 | socklen_t fromlen; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 452 | |
| 453 | memcpy(&rfds, &afds, sizeof(rfds)); |
| 454 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 455 | if (select(nfds, &rfds, (fd_set *) 0, (fd_set *) 0, |
| 456 | (struct timeval *)0) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 457 | if (errno != EINTR) { |
| 458 | perror("server select"); |
| 459 | exit(1); |
| 460 | } |
| 461 | continue; |
| 462 | } |
| 463 | if (FD_ISSET(sfd, &rfds)) { |
| 464 | int newfd; |
| 465 | |
| 466 | fromlen = sizeof(fsin); |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 467 | newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 468 | if (newfd >= 0) { |
| 469 | FD_SET(newfd, &afds); |
| 470 | /* send something back */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 471 | (void)write(newfd, "hoser\n", 6); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | if (FD_ISSET(ufd, &rfds)) { |
| 475 | int newfd; |
| 476 | |
| 477 | fromlen = sizeof(fsun); |
plars | 8525f2f | 2003-01-28 17:32:52 +0000 | [diff] [blame] | 478 | newfd = accept(ufd, (struct sockaddr *)&fsun, &fromlen); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 479 | if (newfd >= 0) |
| 480 | FD_SET(newfd, &afds); |
| 481 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 482 | for (fd = 0; fd < nfds; ++fd) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 483 | if (fd != sfd && fd != ufd && FD_ISSET(fd, &rfds)) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 484 | char rbuf[1024]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 485 | |
| 486 | cc = read(fd, rbuf, sizeof(rbuf)); |
| 487 | if (cc && rbuf[0] == 'R') |
| 488 | sender(fd); |
| 489 | if (cc == 0 || (cc < 0 && errno != EINTR)) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 490 | (void)close(fd); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 491 | FD_CLR(fd, &afds); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | #define TM "from recvmsg01 server" |
| 498 | |
| 499 | /* special for rights-passing test */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 500 | void sender(int fd) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 501 | { |
| 502 | struct msghdr mh; |
| 503 | struct cmsghdr *control; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 504 | char tmpfn[1024], snd_cbuf[1024]; |
| 505 | int tfd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 506 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 507 | (void)strcpy(tmpfn, "smtXXXXXX"); |
plars | 4c78d16 | 2002-09-04 18:56:14 +0000 | [diff] [blame] | 508 | tfd = mkstemp(tmpfn); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 509 | if (tfd < 0) |
| 510 | return; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 511 | |
vapier | 32cc0ac | 2006-06-22 04:28:32 +0000 | [diff] [blame] | 512 | memset(&mh, 0x00, sizeof(mh)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 513 | |
| 514 | /* set up cmsghdr */ |
| 515 | control = (struct cmsghdr *)snd_cbuf; |
vapier | 32cc0ac | 2006-06-22 04:28:32 +0000 | [diff] [blame] | 516 | memset(control, 0x00, sizeof(struct cmsghdr)); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 517 | control->cmsg_len = sizeof(struct cmsghdr) + 4; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 518 | control->cmsg_level = SOL_SOCKET; |
| 519 | control->cmsg_type = SCM_RIGHTS; |
| 520 | *(int *)CMSG_DATA(control) = tfd; |
| 521 | |
| 522 | /* set up msghdr */ |
| 523 | iov[0].iov_base = TM; |
| 524 | iov[0].iov_len = sizeof(TM); |
| 525 | mh.msg_iov = iov; |
| 526 | mh.msg_iovlen = 1; |
| 527 | mh.msg_flags = 0; |
| 528 | mh.msg_control = control; |
| 529 | mh.msg_controllen = control->cmsg_len; |
| 530 | |
| 531 | /* do it */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 532 | (void)sendmsg(fd, &mh, 0); |
| 533 | (void)close(tfd); |
| 534 | (void)unlink(tmpfn); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 535 | } |