blob: 5e8d14aa8e09252430a293e1ceb07bed103da32d [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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
plars865695b2001-08-27 22:15:12 +000050#include <sys/types.h>
51#include <sys/stat.h>
plars865695b2001-08-27 22:15:12 +000052#include <errno.h>
robbiewc7861b52002-12-30 20:21:04 +000053#include <fcntl.h>
Garrett Cooper8afc5e22010-12-17 02:56:19 -080054#include <stdio.h>
55#include "test.h"
plars865695b2001-08-27 22:15:12 +000056
57char *TCID = "dup202";
58int TST_TOTAL = 3;
plars865695b2001-08-27 22:15:12 +000059
60void setup(void);
61void cleanup(void);
62
63char testfile[40];
64int fail;
65int newfd;
66
67/* set these to a known index into our local file descriptor table */
68int duprdo = 10, dupwro = 20, duprdwr = 30;
69
70struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000071 int *nfd;
72 mode_t mode;
plars865695b2001-08-27 22:15:12 +000073} TC[] = {
74 /* The first test creat(es) a file with mode 0444 */
Wanlong Gao354ebb42012-12-07 10:10:04 +080075 {
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)}
plars865695b2001-08-27 22:15:12 +000084};
85
robbiew57ce7e52003-03-25 18:44:20 +000086int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000087{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020088 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020089 const char *msg;
plars865695b2001-08-27 22:15:12 +000090 int i, ofd;
91 struct stat oldbuf, newbuf;
92
Garrett Cooper8afc5e22010-12-17 02:56:19 -080093 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +000094 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000095
96 setup();
97
plars865695b2001-08-27 22:15:12 +000098 for (lc = 0; TEST_LOOPING(lc); lc++) {
99
Caspar Zhangd59a6592013-03-07 14:59:12 +0800100 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000101
102 /* loop through the test cases */
103 for (i = 0; i < TST_TOTAL; i++) {
104
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800105 if ((ofd = creat(testfile, TC[i].mode)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 tst_brkm(TBROK | TERRNO, cleanup,
107 "creat failed");
plars865695b2001-08-27 22:15:12 +0000108
109 TEST(dup2(ofd, *TC[i].nfd));
110
subrata_modak56207ce2009-03-23 13:35:39 +0000111 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800112 tst_resm(TFAIL | TERRNO,
113 "call failed unexpectedly");
subrata_modak56207ce2009-03-23 13:35:39 +0000114 continue;
115 }
plars865695b2001-08-27 22:15:12 +0000116
Cyril Hrubise38b9612014-06-02 17:20:57 +0200117 /* stat the original file */
118 if (fstat(ofd, &oldbuf) == -1)
119 tst_brkm(TBROK | TERRNO, cleanup,
120 "fstat #1 failed");
plars865695b2001-08-27 22:15:12 +0000121
Cyril Hrubise38b9612014-06-02 17:20:57 +0200122 /* stat the duped file */
123 if (fstat(*TC[i].nfd, &newbuf) == -1)
124 tst_brkm(TBROK | TERRNO, cleanup,
125 "fstat #2 failed");
plars865695b2001-08-27 22:15:12 +0000126
Cyril Hrubise38b9612014-06-02 17:20:57 +0200127 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");
plars865695b2001-08-27 22:15:12 +0000133
134 /* remove the file so that we can use it again */
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800135 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");
plars865695b2001-08-27 22:15:12 +0000141 }
142 }
plars865695b2001-08-27 22:15:12 +0000143
Cyril Hrubise38b9612014-06-02 17:20:57 +0200144 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800145 tst_exit();
plars865695b2001-08-27 22:15:12 +0000146}
147
148/*
149 * setup() - performs all ONE TIME setup for this test.
150 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400151void setup(void)
plars865695b2001-08-27 22:15:12 +0000152{
Garrett Cooper2c282152010-12-16 00:55:50 -0800153
plars865695b2001-08-27 22:15:12 +0000154 tst_sig(NOFORK, DEF_HANDLER, cleanup);
155
plars865695b2001-08-27 22:15:12 +0000156 TEST_PAUSE;
157
plars865695b2001-08-27 22:15:12 +0000158 tst_tmpdir();
159
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800160 (void)umask(0);
plars865695b2001-08-27 22:15:12 +0000161
plars865695b2001-08-27 22:15:12 +0000162 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 Frysingerc57fba52014-04-09 18:56:30 -0400169void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000170{
plars865695b2001-08-27 22:15:12 +0000171 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700172}