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 | * dup202.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Is the access mode the same for both file descriptors? |
| 26 | * 0: read only ? "0444" |
| 27 | * 1: write only ? "0222" |
| 28 | * 2: read/write ? "0666" |
| 29 | * |
| 30 | * ALGORITHM |
| 31 | * Creat a file with each access mode; dup each file descriptor; |
| 32 | * stat each file descriptor and compare modes of each pair |
| 33 | * |
| 34 | * USAGE: <for command-line> |
| 35 | * dup202 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 36 | * where, -c n : Run n copies concurrently. |
| 37 | * -f : Turn off functionality Testing. |
| 38 | * -i n : Execute test n times. |
| 39 | * -I x : Execute test for x seconds. |
| 40 | * -P x : Pause for x seconds between iterations. |
| 41 | * -t : Turn on syscall timing. |
| 42 | * |
| 43 | * HISTORY |
| 44 | * 07/2001 Ported by Wayne Boyer |
| 45 | * |
| 46 | * RESTRICTIONS |
| 47 | * None |
| 48 | */ |
| 49 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | #include <sys/types.h> |
| 51 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | #include <errno.h> |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 53 | #include <fcntl.h> |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 54 | #include <stdio.h> |
| 55 | #include "test.h" |
| 56 | #include "usctest.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
| 58 | char *TCID = "dup202"; |
| 59 | int TST_TOTAL = 3; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
| 61 | void setup(void); |
| 62 | void cleanup(void); |
| 63 | |
| 64 | char testfile[40]; |
| 65 | int fail; |
| 66 | int newfd; |
| 67 | |
| 68 | /* set these to a known index into our local file descriptor table */ |
| 69 | int duprdo = 10, dupwro = 20, duprdwr = 30; |
| 70 | |
| 71 | struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 72 | int *nfd; |
| 73 | mode_t mode; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | } TC[] = { |
| 75 | /* The first test creat(es) a file with mode 0444 */ |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 76 | { &duprdo, (S_IRUSR | S_IRGRP | S_IROTH) }, |
| 77 | /* The second test creat(es) a file with mode 0222 */ |
| 78 | { &dupwro, (S_IWUSR | S_IWGRP | S_IWOTH) }, |
| 79 | /* The third test creat(es) a file with mode 0666 */ |
| 80 | { &duprdwr, (S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR|S_IWGRP|S_IWOTH) } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
robbiew | 57ce7e5 | 2003-03-25 18:44:20 +0000 | [diff] [blame] | 83 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | int lc; /* loop counter */ |
| 86 | char *msg; /* message returned from parse_opts */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | int i, ofd; |
| 88 | struct stat oldbuf, newbuf; |
| 89 | |
| 90 | /* parse standard options */ |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 91 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
| 94 | setup(); |
| 95 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 97 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | Tst_count = 0; |
| 99 | |
| 100 | /* loop through the test cases */ |
| 101 | for (i = 0; i < TST_TOTAL; i++) { |
| 102 | |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 103 | if ((ofd = creat(testfile, TC[i].mode)) == -1) |
| 104 | tst_brkm(TBROK|TERRNO, cleanup, "creat failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | |
| 106 | TEST(dup2(ofd, *TC[i].nfd)); |
| 107 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 108 | if (TEST_RETURN == -1) { |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 109 | tst_resm(TFAIL|TERRNO, |
| 110 | "call failed unexpectedly"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 111 | continue; |
| 112 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | if (STD_FUNCTIONAL_TEST) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
| 116 | /* stat the original file */ |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 117 | if (fstat(ofd, &oldbuf) == -1) |
| 118 | tst_brkm(TBROK|TERRNO, cleanup, |
| 119 | "fstat #1 failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
| 121 | /* stat the duped file */ |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 122 | if (fstat(*TC[i].nfd, &newbuf) == -1) |
| 123 | tst_brkm(TBROK|TERRNO, cleanup, |
| 124 | "fstat #2 failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 126 | if (oldbuf.st_mode != newbuf.st_mode) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | tst_resm(TFAIL, "original and dup " |
| 128 | "modes do not match"); |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 129 | else |
| 130 | tst_resm(TPASS, "fstat shows new and " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | "old modes are the same"); |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 132 | } else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | tst_resm(TPASS, "call succeeded"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | |
| 135 | /* remove the file so that we can use it again */ |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 136 | if (close(*TC[i].nfd) == -1) |
| 137 | perror("close failed"); |
| 138 | if (close(ofd) == -1) |
| 139 | perror("close failed"); |
| 140 | if (unlink(testfile) == -1) |
| 141 | perror("unlink failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | cleanup(); |
| 145 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 146 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* |
| 150 | * setup() - performs all ONE TIME setup for this test. |
| 151 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 154 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 156 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | TEST_PAUSE; |
| 158 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | tst_tmpdir(); |
| 160 | |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 161 | (void)umask(0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | sprintf(testfile, "dup202.%d", getpid()); |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 168 | * completion or premature exit. |
| 169 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 170 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | TEST_CLEANUP; |
| 173 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 175 | } |