subrata_modak | 7382a06 | 2009-01-16 10:16:53 +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 | 7382a06 | 2009-01-16 10:16:53 +0000 | [diff] [blame] | 19 | /* */ |
| 20 | /******************************************************************************/ |
| 21 | /******************************************************************************/ |
| 22 | /* */ |
| 23 | /* File: dup3_01.c */ |
| 24 | /* */ |
| 25 | /* Description: This Program tests the new system call introduced in 2.6.27. */ |
| 26 | /* Ulrich´s comment as in: */ |
| 27 | /* http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=336dd1f70ff62d7dd8655228caed4c5bfc818c56 */ |
| 28 | /* says: */ |
| 29 | /* This patch adds the new dup3 syscall. It extends the old dup2 syscall by */ |
| 30 | /* one parameter which is meant to hold a flag value. Support for the */ |
| 31 | /* O_CLOEXEC flag is added in this patch. The following test must be adjusted */ |
| 32 | /* for architectures other than x86 and x86-64 and in case the */ |
| 33 | /* syscall numbers changed. */ |
| 34 | /* */ |
| 35 | /* Usage: <for command-line> */ |
| 36 | /* dup3_01 [-c n] [-e][-i n] [-I x] [-p x] [-t] */ |
| 37 | /* where, -c n : Run n copies concurrently. */ |
| 38 | /* -e : Turn on errno logging. */ |
| 39 | /* -i n : Execute test n times. */ |
| 40 | /* -I x : Execute test for x seconds. */ |
| 41 | /* -P x : Pause for x seconds between iterations. */ |
| 42 | /* -t : Turn on syscall timing. */ |
| 43 | /* */ |
| 44 | /* Total Tests: 1 */ |
| 45 | /* */ |
| 46 | /* Test Name: dup3_01 */ |
| 47 | /* */ |
| 48 | /* Author: Ulrich Drepper <drepper@redhat.com> */ |
| 49 | /* */ |
| 50 | /* History: Created - Jan 13 2009 - Ulrich Drepper <drepper@redhat.com> */ |
| 51 | /* Ported to LTP */ |
| 52 | /* - Jan 13 2009 - Subrata <subrata@linux.vnet.ibm.com> */ |
| 53 | /******************************************************************************/ |
| 54 | #include <fcntl.h> |
| 55 | #include <stdio.h> |
| 56 | #include <time.h> |
| 57 | #include <unistd.h> |
| 58 | #include <sys/syscall.h> |
subrata_modak | c488f90 | 2009-02-23 07:17:33 +0000 | [diff] [blame] | 59 | #include <errno.h> |
subrata_modak | 7382a06 | 2009-01-16 10:16:53 +0000 | [diff] [blame] | 60 | |
subrata_modak | 7382a06 | 2009-01-16 10:16:53 +0000 | [diff] [blame] | 61 | #include "test.h" |
Cyril Hrubis | 98f8747 | 2014-01-08 13:32:41 +0100 | [diff] [blame] | 62 | #include "lapi/fcntl.h" |
subrata_modak | 115006c | 2009-02-04 06:16:40 +0000 | [diff] [blame] | 63 | #include "linux_syscall_numbers.h" |
subrata_modak | 7382a06 | 2009-01-16 10:16:53 +0000 | [diff] [blame] | 64 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 65 | char *TCID = "dup3_01"; |
| 66 | int TST_TOTAL = 1; |
subrata_modak | 7382a06 | 2009-01-16 10:16:53 +0000 | [diff] [blame] | 67 | |
| 68 | /* Extern Global Functions */ |
| 69 | /******************************************************************************/ |
| 70 | /* */ |
| 71 | /* Function: cleanup */ |
| 72 | /* */ |
| 73 | /* Description: Performs all one time clean up for this test on successful */ |
| 74 | /* completion, premature exit or failure. Closes all temporary */ |
| 75 | /* files, removes all temporary directories exits the test with */ |
| 76 | /* appropriate return code by calling tst_exit() function. */ |
| 77 | /* */ |
| 78 | /* Input: None. */ |
| 79 | /* */ |
| 80 | /* Output: None. */ |
| 81 | /* */ |
| 82 | /* Return: On failure - Exits calling tst_exit(). Non '0' return code. */ |
| 83 | /* On success - Exits calling tst_exit(). With '0' return code. */ |
| 84 | /* */ |
| 85 | /******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 86 | void cleanup(void) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 87 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | tst_rmdir(); |
subrata_modak | 7382a06 | 2009-01-16 10:16:53 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* Local Functions */ |
| 92 | /******************************************************************************/ |
| 93 | /* */ |
| 94 | /* Function: setup */ |
| 95 | /* */ |
| 96 | /* Description: Performs all one time setup for this test. This function is */ |
| 97 | /* typically used to capture signals, create temporary dirs */ |
| 98 | /* and temporary files that may be used in the course of this */ |
| 99 | /* test. */ |
| 100 | /* */ |
| 101 | /* Input: None. */ |
| 102 | /* */ |
| 103 | /* Output: None. */ |
| 104 | /* */ |
| 105 | /* Return: On failure - Exits by calling cleanup(). */ |
| 106 | /* On success - returns 0. */ |
| 107 | /* */ |
| 108 | /******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 109 | void setup(void) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | { |
| 111 | /* Capture signals if any */ |
| 112 | /* Create temporary directories */ |
| 113 | TEST_PAUSE; |
| 114 | tst_tmpdir(); |
subrata_modak | 7382a06 | 2009-01-16 10:16:53 +0000 | [diff] [blame] | 115 | } |
| 116 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | int main(int argc, char *argv[]) |
| 118 | { |
| 119 | int fd, coe; |
subrata_modak | 7382a06 | 2009-01-16 10:16:53 +0000 | [diff] [blame] | 120 | |
Garrett Cooper | abf8e53 | 2010-12-17 02:57:37 -0800 | [diff] [blame] | 121 | if ((tst_kvercmp(2, 6, 27)) < 0) |
Garrett Cooper | a4f4cec | 2011-02-13 18:25:49 -0800 | [diff] [blame] | 122 | tst_brkm(TCONF, NULL, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 123 | "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] | 124 | setup(); |
subrata_modak | 7382a06 | 2009-01-16 10:16:53 +0000 | [diff] [blame] | 125 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 126 | fd = ltp_syscall(__NR_dup3, 1, 4, 0); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 127 | if (fd == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 128 | tst_brkm(TFAIL | TERRNO, cleanup, "dup3(0) failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | } |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 130 | coe = fcntl(fd, F_GETFD); |
| 131 | if (coe == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 132 | tst_brkm(TBROK | TERRNO, cleanup, "fcntl failed"); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 133 | } |
| 134 | if (coe & FD_CLOEXEC) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 135 | tst_brkm(TFAIL, cleanup, "dup3(0) set close-on-exec flag"); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 136 | } |
| 137 | close(fd); |
| 138 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 139 | fd = ltp_syscall(__NR_dup3, 1, 4, O_CLOEXEC); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 140 | if (fd == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 141 | tst_brkm(TFAIL | TERRNO, cleanup, "dup3(O_CLOEXEC) failed"); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 142 | } |
| 143 | coe = fcntl(fd, F_GETFD); |
| 144 | if (coe == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 145 | tst_brkm(TBROK | TERRNO, cleanup, "fcntl failed"); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 146 | } |
| 147 | if ((coe & FD_CLOEXEC) == 0) { |
| 148 | tst_brkm(TFAIL, cleanup, |
| 149 | "dup3(O_CLOEXEC) set close-on-exec flag"); |
| 150 | } |
| 151 | close(fd); |
| 152 | tst_resm(TPASS, "dup3(O_CLOEXEC) PASSED"); |
| 153 | |
| 154 | cleanup(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | tst_exit(); |
Garrett Cooper | a4f4cec | 2011-02-13 18:25:49 -0800 | [diff] [blame] | 156 | } |