subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 1 | /******************************************************************************/ |
| 2 | /* */ |
| 3 | /* Copyright (c) Ulrich Drepper <drepper@redhat.com> */ |
| 4 | /* Copyright (c) International Business Machines Corp., 2009 */ |
| 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 */ |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 19 | /* */ |
| 20 | /******************************************************************************/ |
| 21 | /******************************************************************************/ |
| 22 | /* */ |
| 23 | /* File: socket02.c */ |
| 24 | /* */ |
| 25 | /* Description: This program tests the new flag SOCK_CLOEXEC introduced in */ |
| 26 | /* socket() & socketpair() and in kernel 2.6.27. Ulrich´s comment*/ |
| 27 | /* as in: */ |
| 28 | /* http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=a677a039be7243357d93502bff2b40850c942e2d */ |
| 29 | /* says: */ |
| 30 | /* */ |
| 31 | /* flag parameters: socket and socketpair */ |
| 32 | /* This patch adds support for flag values which are ORed to the */ |
| 33 | /* type passwd to socket and socketpair. The additional code is */ |
| 34 | /* minimal. The flag values in this implementation can and must */ |
| 35 | /* match the O_* flags. This avoids overhead in the conversion. */ |
| 36 | /* The internal functions sock_alloc_fd and sock_map_fd get a new*/ |
| 37 | /* parameters and all callers are changed. */ |
| 38 | /* */ |
| 39 | /* Usage: <for command-line> */ |
| 40 | /* socket02 [-c n] [-e][-i n] [-I x] [-p x] [-t] */ |
| 41 | /* where, -c n : Run n copies concurrently. */ |
| 42 | /* -e : Turn on errno logging. */ |
| 43 | /* -i n : Execute test n times. */ |
| 44 | /* -I x : Execute test for x seconds. */ |
| 45 | /* -P x : Pause for x seconds between iterations. */ |
| 46 | /* -t : Turn on syscall timing. */ |
| 47 | /* */ |
| 48 | /* Total Tests: 1 */ |
| 49 | /* */ |
| 50 | /* Test Name: socket02 */ |
| 51 | /* */ |
| 52 | /* Author: Ulrich Drepper <drepper@redhat.com> */ |
| 53 | /* */ |
| 54 | /* History: Created - Jan 05 2009 - Ulrich Drepper <drepper@redhat.com> */ |
| 55 | /* Ported to LTP */ |
| 56 | /* - Jan 05 2009 - Subrata <subrata@linux.vnet.ibm.com> */ |
| 57 | /******************************************************************************/ |
| 58 | |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 59 | #include <fcntl.h> |
| 60 | #include <stdio.h> |
| 61 | #include <unistd.h> |
| 62 | #include <netinet/in.h> |
| 63 | #include <sys/socket.h> |
| 64 | |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 65 | #include "test.h" |
| 66 | #include "usctest.h" |
Cyril Hrubis | 98f8747 | 2014-01-08 13:32:41 +0100 | [diff] [blame] | 67 | #include "lapi/fcntl.h" |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 68 | |
| 69 | #define PORT 57392 |
| 70 | |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 71 | /* For Linux these must be the same. */ |
subrata_modak | 8d4fee7 | 2009-01-27 14:48:38 +0000 | [diff] [blame] | 72 | #ifndef SOCK_CLOEXEC |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 73 | #define SOCK_CLOEXEC O_CLOEXEC |
subrata_modak | 8d4fee7 | 2009-01-27 14:48:38 +0000 | [diff] [blame] | 74 | #endif |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 75 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 76 | char *TCID = "socket02"; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 77 | int testno; |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 78 | int TST_TOTAL = 1; |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 79 | |
| 80 | /* Extern Global Functions */ |
| 81 | /******************************************************************************/ |
| 82 | /* */ |
| 83 | /* Function: cleanup */ |
| 84 | /* */ |
| 85 | /* Description: Performs all one time clean up for this test on successful */ |
| 86 | /* completion, premature exit or failure. Closes all temporary */ |
| 87 | /* files, removes all temporary directories exits the test with */ |
| 88 | /* appropriate return code by calling tst_exit() function. */ |
| 89 | /* */ |
| 90 | /* Input: None. */ |
| 91 | /* */ |
| 92 | /* Output: None. */ |
| 93 | /* */ |
| 94 | /* Return: On failure - Exits calling tst_exit(). Non '0' return code. */ |
| 95 | /* On success - Exits calling tst_exit(). With '0' return code. */ |
| 96 | /* */ |
| 97 | /******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 98 | void cleanup(void) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | TEST_CLEANUP; |
| 102 | tst_rmdir(); |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 103 | |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /* Local Functions */ |
| 107 | /******************************************************************************/ |
| 108 | /* */ |
| 109 | /* Function: setup */ |
| 110 | /* */ |
| 111 | /* Description: Performs all one time setup for this test. This function is */ |
| 112 | /* typically used to capture signals, create temporary dirs */ |
| 113 | /* and temporary files that may be used in the course of this */ |
| 114 | /* test. */ |
| 115 | /* */ |
| 116 | /* Input: None. */ |
| 117 | /* */ |
| 118 | /* Output: None. */ |
| 119 | /* */ |
| 120 | /* Return: On failure - Exits by calling cleanup(). */ |
| 121 | /* On success - returns 0. */ |
| 122 | /* */ |
| 123 | /******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 124 | void setup(void) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 125 | { |
| 126 | /* Capture signals if any */ |
| 127 | /* Create temporary directories */ |
| 128 | TEST_PAUSE; |
| 129 | tst_tmpdir(); |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 130 | } |
| 131 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | int main(int argc, char *argv[]) |
| 133 | { |
| 134 | int fd, fds[2], i, coe; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 135 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 136 | const char *msg; |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 137 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 138 | msg = parse_opts(argc, argv, NULL, NULL); |
| 139 | if (msg != NULL) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | } |
| 142 | if ((tst_kvercmp(2, 6, 27)) < 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 143 | tst_brkm(TCONF, |
| 144 | NULL, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 145 | "This test can only run on kernels that are 2.6.27 and higher"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 146 | } |
| 147 | setup(); |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 148 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 149 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 150 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
| 152 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 153 | if (fd == -1) { |
| 154 | tst_brkm(TBROK, cleanup, "socket(0) failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | } |
| 156 | coe = fcntl(fd, F_GETFD); |
| 157 | if (coe == -1) { |
| 158 | tst_brkm(TBROK, cleanup, "fcntl failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | } |
| 160 | if (coe & FD_CLOEXEC) { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 161 | tst_brkm(TFAIL, |
| 162 | cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | "socket(0) set close-on-exec flag"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 164 | } |
| 165 | close(fd); |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 166 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | fd = socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 168 | if (fd == -1) { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 169 | tst_brkm(TFAIL, cleanup, |
| 170 | "socket(SOCK_CLOEXEC) failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | } |
| 172 | coe = fcntl(fd, F_GETFD); |
| 173 | if (coe == -1) { |
| 174 | tst_brkm(TBROK, cleanup, "fcntl failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 175 | } |
| 176 | if ((coe & FD_CLOEXEC) == 0) { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 177 | tst_brkm(TFAIL, |
| 178 | cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 179 | "socket(SOCK_CLOEXEC) does not set close-on-exec flag"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 180 | } |
| 181 | close(fd); |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 182 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 183 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, fds) == -1) { |
| 184 | tst_brkm(TBROK, cleanup, |
| 185 | "socketpair(0) failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 186 | } |
| 187 | for (i = 0; i < 2; ++i) { |
| 188 | coe = fcntl(fds[i], F_GETFD); |
| 189 | if (coe == -1) { |
| 190 | tst_brkm(TBROK, cleanup, |
| 191 | "fcntl failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 192 | } |
| 193 | if (coe & FD_CLOEXEC) { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 194 | tst_brkm(TFAIL, |
| 195 | cleanup, "socketpair(0) set close-on-exec flag for fds[%d]\n", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 196 | i); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 197 | } |
| 198 | close(fds[i]); |
| 199 | } |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 200 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 201 | if (socketpair |
| 202 | (PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, |
| 203 | fds) == -1) { |
| 204 | tst_brkm(TBROK, cleanup, |
| 205 | "socketpair(SOCK_CLOEXEC) failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 206 | } |
| 207 | for (i = 0; i < 2; ++i) { |
| 208 | coe = fcntl(fds[i], F_GETFD); |
| 209 | if (coe == -1) { |
| 210 | tst_brkm(TBROK, cleanup, |
| 211 | "fcntl failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 212 | } |
| 213 | if ((coe & FD_CLOEXEC) == 0) { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 214 | tst_brkm(TFAIL, |
| 215 | cleanup, "socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 216 | i); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 217 | } |
| 218 | close(fds[i]); |
| 219 | } |
| 220 | tst_resm(TPASS, "socket(SOCK_CLOEXEC) PASSED"); |
| 221 | cleanup(); |
| 222 | } |
| 223 | } |
| 224 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 225 | } |