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 | * NAME |
| 22 | * dup203.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check the basic functionality of dup2(). |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * 1. Attempt to dup2() on an open file descriptor. |
| 29 | * 2. Attempt to dup2() on a close file descriptor. |
| 30 | * |
| 31 | * USAGE: <for command-line> |
| 32 | * dup203 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 33 | * where, -c n : Run n copies concurrently. |
| 34 | * -f : Turn off functionality Testing. |
| 35 | * -i n : Execute test n times. |
| 36 | * -I x : Execute test for x seconds. |
| 37 | * -P x : Pause for x seconds between iterations. |
| 38 | * -t : Turn on syscall timing. |
| 39 | * |
| 40 | * HISTORY |
| 41 | * 07/2001 Ported by Wayne Boyer |
| 42 | * |
| 43 | * RESTRICTIONS |
| 44 | * NONE |
| 45 | */ |
| 46 | |
| 47 | #include <fcntl.h> |
| 48 | #include <sys/param.h> |
| 49 | #include <errno.h> |
robbiew | 15226cd | 2002-04-10 16:10:40 +0000 | [diff] [blame] | 50 | #include <string.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | #include <test.h> |
| 52 | #include <usctest.h> |
| 53 | |
| 54 | void setup(void); |
| 55 | void cleanup(void); |
| 56 | |
| 57 | char *TCID = "dup203"; |
| 58 | int TST_TOTAL = 1; |
| 59 | extern int Tst_count; |
| 60 | |
robbiew | 57ce7e5 | 2003-03-25 18:44:20 +0000 | [diff] [blame] | 61 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | { |
| 63 | int fd0, fd1, fd2, rval; |
| 64 | char filename0[40], filename1[40]; |
| 65 | char buf[40]; |
| 66 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 67 | int lc; /* loop counter */ |
| 68 | char *msg; /* message returned from parse_opts */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | |
| 70 | /* parse standard options */ |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 71 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 72 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
| 74 | setup(); |
| 75 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 77 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | Tst_count = 0; |
robbiew | 57ce7e5 | 2003-03-25 18:44:20 +0000 | [diff] [blame] | 79 | //block1: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | tst_resm(TINFO, "Enter block 1"); |
| 81 | tst_resm(TINFO, "Test duping over an open fd"); |
| 82 | |
| 83 | sprintf(filename0, "dup202.file0.%d\n", getpid()); |
| 84 | sprintf(filename1, "dup202.file1.%d\n", getpid()); |
| 85 | unlink(filename0); |
| 86 | unlink(filename1); |
| 87 | |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 88 | if ((fd0 = creat(filename0, 0666)) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | tst_brkm(TBROK, cleanup, "cannot create first file"); |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 90 | if (write(fd0, filename0, strlen(filename0)) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | tst_brkm(TBROK, cleanup, "filename0: write(2) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 93 | if ((fd1 = creat(filename1, 0666)) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | tst_brkm(TBROK, cleanup, "Cannot create second file"); |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 95 | if (write(fd1, filename1, strlen(filename1)) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | tst_brkm(TBROK, cleanup, "filename1: write(2) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 98 | if (close(fd0) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | tst_brkm(TBROK, cleanup, "close(2) fd0 failed"); |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 100 | if ((fd0 = open(filename0, O_RDONLY)) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | tst_brkm(TBROK, cleanup, "open(2) on filename0 failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 103 | if (close(fd1) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | tst_brkm(TBROK, cleanup, "close(2) fd1 failed"); |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 105 | if ((fd1 = open(filename1, O_RDONLY)) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | tst_brkm(TBROK, cleanup, "open(2) on filename1 failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
| 108 | TEST(dup2(fd0, fd1)); |
| 109 | |
| 110 | if ((fd2 = TEST_RETURN) == -1) { |
| 111 | tst_resm(TFAIL, "call failed unexpectedly"); |
| 112 | } else if (STD_FUNCTIONAL_TEST) { |
| 113 | if (fd1 != fd2) { |
| 114 | tst_resm(TFAIL, "file descriptors don't match"); |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | memset(buf, 0, sizeof(buf)); |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 119 | if (read(fd2, buf, sizeof(buf)) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | tst_brkm(TBROK, cleanup, "read(2) failed"); |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 121 | if (strcmp(buf, filename0) != 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | tst_resm(TFAIL, "read from file got bad data"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | tst_resm(TPASS, "dup2 test 1 functionality is correct"); |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 124 | } else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | tst_resm(TPASS, "call succeeded"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | |
| 127 | close(fd0); |
| 128 | close(fd1); |
| 129 | close(fd2); |
| 130 | unlink(filename0); |
| 131 | unlink(filename1); |
| 132 | |
| 133 | tst_resm(TINFO, "Exit block 1"); |
| 134 | |
robbiew | 57ce7e5 | 2003-03-25 18:44:20 +0000 | [diff] [blame] | 135 | //block2: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | tst_resm(TINFO, "Enter block 2"); |
| 137 | tst_resm(TINFO, "Test close on exec flag"); |
| 138 | |
| 139 | sprintf(filename0, "dup02.%d\n", getpid()); |
| 140 | unlink(filename0); |
| 141 | |
| 142 | if ((fd0 = creat(filename0, 0666)) == -1) { |
| 143 | tst_brkm(TBROK, cleanup, "Cannot create first file"); |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 144 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | if (fcntl(fd0, F_SETFD, 1) == -1) { |
| 146 | tst_brkm(TBROK, cleanup, "setting close on exec flag " |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 147 | "on fd0 failed"); |
| 148 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | |
subrata_modak | 6805f2a | 2008-03-14 19:49:20 +0000 | [diff] [blame] | 150 | if ((fd2 = creat(filename1, 0666)) == -1) { |
| 151 | tst_brkm(TBROK, cleanup, "Cannot create second file"); |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 152 | } |
subrata_modak | 6805f2a | 2008-03-14 19:49:20 +0000 | [diff] [blame] | 153 | |
| 154 | if (close(fd2) == -1) { |
| 155 | tst_brkm(TBROK, cleanup, "close(2) fd_closed failed"); |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 156 | } |
subrata_modak | 6805f2a | 2008-03-14 19:49:20 +0000 | [diff] [blame] | 157 | |
| 158 | TEST(dup2(fd0, fd2)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | |
| 160 | if ((fd1 = TEST_RETURN) == -1) { |
| 161 | tst_resm(TFAIL, "call failed unexpectedly"); |
| 162 | } else if (STD_FUNCTIONAL_TEST) { |
subrata_modak | 6805f2a | 2008-03-14 19:49:20 +0000 | [diff] [blame] | 163 | if (fd1 != fd2) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | tst_resm(TFAIL, "bad dup2 descriptor %d", fd1); |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | if ((rval = fcntl(fd1, F_GETFD, 0)) != 0) { |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 169 | tst_resm(TBROK|TERRNO, |
| 170 | "fcntl F_GETFD on fd1 failed; expected a " |
| 171 | "return value of 0x0, got %#x", rval); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | break; |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 173 | } |
| 174 | if (((rval = fcntl(fd0, F_GETFL, 0)) != O_WRONLY)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | tst_resm(TFAIL, "fctnl F_GETFL bad rval on fd0 " |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 176 | "Expected %#x got %#x", O_WRONLY, rval); |
| 177 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | tst_resm(TPASS, "dup2 test 2 functionality is correct"); |
| 179 | } else { |
| 180 | tst_resm(TPASS, "call succeeded"); |
| 181 | } |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 182 | close(fd0); |
| 183 | close(fd1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | |
| 185 | unlink(filename0); |
subrata_modak | 6805f2a | 2008-03-14 19:49:20 +0000 | [diff] [blame] | 186 | unlink(filename1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | tst_resm(TINFO, "Exit block 2"); |
| 188 | } |
| 189 | cleanup(); |
| 190 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 191 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* |
| 195 | * setup() - performs all ONE TIME setup for this test. |
| 196 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 197 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 199 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 201 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | TEST_PAUSE; |
| 203 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | tst_tmpdir(); |
| 205 | } |
| 206 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 207 | /* |
| 208 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 209 | * completion or premature exit. |
| 210 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 211 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 212 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 213 | TEST_CLEANUP; |
| 214 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 215 | tst_rmdir(); |
Garrett Cooper | 80b3110 | 2010-12-17 02:46:24 -0800 | [diff] [blame^] | 216 | } |