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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
| 57 | char *TCID = "dup202"; |
| 58 | int TST_TOTAL = 3; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
| 60 | void setup(void); |
| 61 | void cleanup(void); |
| 62 | |
| 63 | char testfile[40]; |
| 64 | int fail; |
| 65 | int newfd; |
| 66 | |
| 67 | /* set these to a known index into our local file descriptor table */ |
| 68 | int duprdo = 10, dupwro = 20, duprdwr = 30; |
| 69 | |
| 70 | struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | int *nfd; |
| 72 | mode_t mode; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | } TC[] = { |
| 74 | /* The first test creat(es) a file with mode 0444 */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 75 | { |
| 76 | &duprdo, (S_IRUSR | S_IRGRP | S_IROTH)}, |
| 77 | /* The second test creat(es) a file with mode 0222 */ |
| 78 | { |
| 79 | &dupwro, (S_IWUSR | S_IWGRP | S_IWOTH)}, |
| 80 | /* The third test creat(es) a file with mode 0666 */ |
| 81 | { |
| 82 | &duprdwr, |
| 83 | (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH)} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
robbiew | 57ce7e5 | 2003-03-25 18:44:20 +0000 | [diff] [blame] | 86 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 88 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 89 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | int i, ofd; |
| 91 | struct stat oldbuf, newbuf; |
| 92 | |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 93 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
| 96 | setup(); |
| 97 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 99 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 100 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
| 102 | /* loop through the test cases */ |
| 103 | for (i = 0; i < TST_TOTAL; i++) { |
| 104 | |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 105 | if ((ofd = creat(testfile, TC[i].mode)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | tst_brkm(TBROK | TERRNO, cleanup, |
| 107 | "creat failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
| 109 | TEST(dup2(ofd, *TC[i].nfd)); |
| 110 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 111 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 112 | tst_resm(TFAIL | TERRNO, |
| 113 | "call failed unexpectedly"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | continue; |
| 115 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 117 | /* stat the original file */ |
| 118 | if (fstat(ofd, &oldbuf) == -1) |
| 119 | tst_brkm(TBROK | TERRNO, cleanup, |
| 120 | "fstat #1 failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 122 | /* stat the duped file */ |
| 123 | if (fstat(*TC[i].nfd, &newbuf) == -1) |
| 124 | tst_brkm(TBROK | TERRNO, cleanup, |
| 125 | "fstat #2 failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 127 | if (oldbuf.st_mode != newbuf.st_mode) |
| 128 | tst_resm(TFAIL, "original and dup " |
| 129 | "modes do not match"); |
| 130 | else |
| 131 | tst_resm(TPASS, "fstat shows new and " |
| 132 | "old modes are the same"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | |
| 134 | /* remove the file so that we can use it again */ |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 135 | if (close(*TC[i].nfd) == -1) |
| 136 | perror("close failed"); |
| 137 | if (close(ofd) == -1) |
| 138 | perror("close failed"); |
| 139 | if (unlink(testfile) == -1) |
| 140 | perror("unlink failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 144 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 145 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * setup() - performs all ONE TIME setup for this test. |
| 150 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 151 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 153 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 155 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | TEST_PAUSE; |
| 157 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | tst_tmpdir(); |
| 159 | |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 160 | (void)umask(0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | sprintf(testfile, "dup202.%d", getpid()); |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 167 | * completion or premature exit. |
| 168 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 169 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 172 | } |