blob: f89fe73ad8e1a46c8fed3e948d9ad693c15dfa98 [file] [log] [blame]
robbiew6e3fda82002-12-30 20:58:28 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2002
Cyril Hrubisd3547332013-04-23 19:11:59 +02004 * ported from SPIE, section2/iosuite/dup3.c, by Airong Zhang
5 * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
robbiew6e3fda82002-12-30 20:58:28 +00006 *
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 Gao4548c6c2012-10-19 18:03:36 +080019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew6e3fda82002-12-30 20:58:28 +000020 */
21
Cyril Hrubisd3547332013-04-23 19:11:59 +020022/*
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*/
robbiew6e3fda82002-12-30 20:58:28 +000030
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"
robbiew6e3fda82002-12-30 20:58:28 +000038
39char *TCID = "dup07";
Cyril Hrubisd3547332013-04-23 19:11:59 +020040int TST_TOTAL = 3;
robbiew6e3fda82002-12-30 20:58:28 +000041
Cyril Hrubisd3547332013-04-23 19:11:59 +020042static const char *testfile = "dup07";
robbiew6e3fda82002-12-30 20:58:28 +000043
Cyril Hrubisd3547332013-04-23 19:11:59 +020044static void setup(void);
45static void cleanup(void);
robbiew6e3fda82002-12-30 20:58:28 +000046
robbiew6e3fda82002-12-30 20:58:28 +000047int main(int ac, char **av)
48{
subrata_modak56207ce2009-03-23 13:35:39 +000049 struct stat retbuf;
50 struct stat dupbuf;
51 int rdoret, wroret, rdwret;
52 int duprdo, dupwro, duprdwr;
robbiew6e3fda82002-12-30 20:58:28 +000053
Cyril Hrubis89af32a2012-10-24 16:39:11 +020054 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020055 const char *msg;
robbiew6e3fda82002-12-30 20:58:28 +000056
Cyril Hrubisd3547332013-04-23 19:11:59 +020057 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
58 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew6e3fda82002-12-30 20:58:28 +000059
Cyril Hrubisd3547332013-04-23 19:11:59 +020060 setup();
robbiew6e3fda82002-12-30 20:58:28 +000061
62 for (lc = 0; TEST_LOOPING(lc); lc++) {
63
subrata_modak56207ce2009-03-23 13:35:39 +000064 if ((rdoret = creat(testfile, 0444)) == -1) {
65 tst_resm(TFAIL, "Unable to creat file '%s'", testfile);
robbiew6e3fda82002-12-30 20:58:28 +000066 } else {
subrata_modak56207ce2009-03-23 13:35:39 +000067 if ((duprdo = dup(rdoret)) == -1) {
68 tst_resm(TFAIL, "Unable to dup '%s'", testfile);
subrata_modak56207ce2009-03-23 13:35:39 +000069 } 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 Hrubisd3547332013-04-23 19:11:59 +020075 } else {
76 tst_resm(TPASS,
77 "Passed in read mode.");
robbiew6e3fda82002-12-30 20:58:28 +000078 }
Shuang Qiu7053c3b2013-05-24 11:03:39 +080079 close(duprdo);
robbiew6e3fda82002-12-30 20:58:28 +000080 }
Shuang Qiu7053c3b2013-05-24 11:03:39 +080081 close(rdoret);
subrata_modak56207ce2009-03-23 13:35:39 +000082 }
robbiew6e3fda82002-12-30 20:58:28 +000083
84 unlink(testfile);
Cyril Hrubisd3547332013-04-23 19:11:59 +020085
subrata_modak56207ce2009-03-23 13:35:39 +000086 if ((wroret = creat(testfile, 0222)) == -1) {
87 tst_resm(TFAIL, "Unable to creat file '%s'", testfile);
subrata_modak56207ce2009-03-23 13:35:39 +000088 } else {
89 if ((dupwro = dup(wroret)) == -1) {
90 tst_resm(TFAIL, "Unable to dup '%s'", testfile);
subrata_modak56207ce2009-03-23 13:35:39 +000091 } 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 Hrubisd3547332013-04-23 19:11:59 +020097 } else {
98 tst_resm(TPASS,
99 "Passed in write mode.");
robbiew6e3fda82002-12-30 20:58:28 +0000100 }
Shuang Qiu7053c3b2013-05-24 11:03:39 +0800101 close(dupwro);
robbiew6e3fda82002-12-30 20:58:28 +0000102 }
Shuang Qiu7053c3b2013-05-24 11:03:39 +0800103 close(wroret);
104
subrata_modak56207ce2009-03-23 13:35:39 +0000105 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000106
robbiew6e3fda82002-12-30 20:58:28 +0000107 unlink(testfile);
Cyril Hrubisd3547332013-04-23 19:11:59 +0200108
subrata_modak56207ce2009-03-23 13:35:39 +0000109 if ((rdwret = creat(testfile, 0666)) == -1) {
110 tst_resm(TFAIL, "Unable to creat file '%s'", testfile);
robbiew6e3fda82002-12-30 20:58:28 +0000111 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000112 if ((duprdwr = dup(rdwret)) == -1) {
113 tst_resm(TFAIL, "Unable to dup '%s'", testfile);
subrata_modak56207ce2009-03-23 13:35:39 +0000114 } 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 Hrubisd3547332013-04-23 19:11:59 +0200120 } else {
121 tst_resm(TPASS,
122 "Passed in read/write mode.");
robbiew6e3fda82002-12-30 20:58:28 +0000123 }
Shuang Qiu7053c3b2013-05-24 11:03:39 +0800124 close(duprdwr);
robbiew6e3fda82002-12-30 20:58:28 +0000125 }
Shuang Qiu7053c3b2013-05-24 11:03:39 +0800126 close(rdwret);
subrata_modak56207ce2009-03-23 13:35:39 +0000127 }
Cyril Hrubisd3547332013-04-23 19:11:59 +0200128
subrata_modak56207ce2009-03-23 13:35:39 +0000129 unlink(testfile);
Cyril Hrubisd3547332013-04-23 19:11:59 +0200130 }
robbiew6e3fda82002-12-30 20:58:28 +0000131
Cyril Hrubisd3547332013-04-23 19:11:59 +0200132 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800133 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700134}
Cyril Hrubisd3547332013-04-23 19:11:59 +0200135
136static void setup(void)
137{
138 tst_tmpdir();
139}
140
141static void cleanup(void)
142{
143 tst_rmdir();
144}