robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2002 |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 4 | * ported from SPIE, section2/iosuite/dup3.c, by Airong Zhang |
| 5 | * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz> |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 15 | * the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 22 | /* |
| 23 | WHAT: Is the access mode the same for both file descriptors? |
| 24 | 0: read only? |
| 25 | 1: write only? |
| 26 | 2: read/write? |
| 27 | HOW: Creat a file with each access mode; dup each file descriptor; |
| 28 | stat each file descriptor and compare mode of each pair |
| 29 | */ |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 30 | |
| 31 | #include <stdio.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include "test.h" |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 38 | |
| 39 | char *TCID = "dup07"; |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 40 | int TST_TOTAL = 3; |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 41 | |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 42 | static const char *testfile = "dup07"; |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 43 | |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 44 | static void setup(void); |
| 45 | static void cleanup(void); |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 46 | |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 47 | int main(int ac, char **av) |
| 48 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 49 | struct stat retbuf; |
| 50 | struct stat dupbuf; |
| 51 | int rdoret, wroret, rdwret; |
| 52 | int duprdo, dupwro, duprdwr; |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 53 | |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 54 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 55 | const char *msg; |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 56 | |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 57 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
| 58 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 59 | |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 60 | setup(); |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 61 | |
| 62 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 63 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 64 | if ((rdoret = creat(testfile, 0444)) == -1) { |
| 65 | tst_resm(TFAIL, "Unable to creat file '%s'", testfile); |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 66 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 67 | if ((duprdo = dup(rdoret)) == -1) { |
| 68 | tst_resm(TFAIL, "Unable to dup '%s'", testfile); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | } else { |
| 70 | fstat(rdoret, &retbuf); |
| 71 | fstat(duprdo, &dupbuf); |
| 72 | if (retbuf.st_mode != dupbuf.st_mode) { |
| 73 | tst_resm(TFAIL, |
| 74 | "rdonly and dup do not match"); |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 75 | } else { |
| 76 | tst_resm(TPASS, |
| 77 | "Passed in read mode."); |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 78 | } |
Shuang Qiu | 7053c3b | 2013-05-24 11:03:39 +0800 | [diff] [blame] | 79 | close(duprdo); |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 80 | } |
Shuang Qiu | 7053c3b | 2013-05-24 11:03:39 +0800 | [diff] [blame] | 81 | close(rdoret); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | } |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 83 | |
| 84 | unlink(testfile); |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 85 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | if ((wroret = creat(testfile, 0222)) == -1) { |
| 87 | tst_resm(TFAIL, "Unable to creat file '%s'", testfile); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | } else { |
| 89 | if ((dupwro = dup(wroret)) == -1) { |
| 90 | tst_resm(TFAIL, "Unable to dup '%s'", testfile); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | } else { |
| 92 | fstat(wroret, &retbuf); |
| 93 | fstat(dupwro, &dupbuf); |
| 94 | if (retbuf.st_mode != dupbuf.st_mode) { |
| 95 | tst_resm(TFAIL, |
| 96 | "wronly and dup do not match"); |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 97 | } else { |
| 98 | tst_resm(TPASS, |
| 99 | "Passed in write mode."); |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 100 | } |
Shuang Qiu | 7053c3b | 2013-05-24 11:03:39 +0800 | [diff] [blame] | 101 | close(dupwro); |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 102 | } |
Shuang Qiu | 7053c3b | 2013-05-24 11:03:39 +0800 | [diff] [blame] | 103 | close(wroret); |
| 104 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 106 | |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 107 | unlink(testfile); |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 108 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | if ((rdwret = creat(testfile, 0666)) == -1) { |
| 110 | tst_resm(TFAIL, "Unable to creat file '%s'", testfile); |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 111 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | if ((duprdwr = dup(rdwret)) == -1) { |
| 113 | tst_resm(TFAIL, "Unable to dup '%s'", testfile); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | } else { |
| 115 | fstat(rdwret, &retbuf); |
| 116 | fstat(duprdwr, &dupbuf); |
| 117 | if (retbuf.st_mode != dupbuf.st_mode) { |
| 118 | tst_resm(TFAIL, |
| 119 | "rdwr and dup do not match"); |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 120 | } else { |
| 121 | tst_resm(TPASS, |
| 122 | "Passed in read/write mode."); |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 123 | } |
Shuang Qiu | 7053c3b | 2013-05-24 11:03:39 +0800 | [diff] [blame] | 124 | close(duprdwr); |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 125 | } |
Shuang Qiu | 7053c3b | 2013-05-24 11:03:39 +0800 | [diff] [blame] | 126 | close(rdwret); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | } |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 128 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | unlink(testfile); |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 130 | } |
robbiew | 6e3fda8 | 2002-12-30 20:58:28 +0000 | [diff] [blame] | 131 | |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 132 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 133 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 134 | } |
Cyril Hrubis | d354733 | 2013-04-23 19:11:59 +0200 | [diff] [blame] | 135 | |
| 136 | static void setup(void) |
| 137 | { |
| 138 | tst_tmpdir(); |
| 139 | } |
| 140 | |
| 141 | static void cleanup(void) |
| 142 | { |
| 143 | tst_rmdir(); |
| 144 | } |