blob: 8bd04a6d320eea512b5925d26681c7a7733bbfc4 [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
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
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"
56#include "usctest.h"
plars865695b2001-08-27 22:15:12 +000057
58char *TCID = "dup202";
59int TST_TOTAL = 3;
plars865695b2001-08-27 22:15:12 +000060
61void setup(void);
62void cleanup(void);
63
64char testfile[40];
65int fail;
66int newfd;
67
68/* set these to a known index into our local file descriptor table */
69int duprdo = 10, dupwro = 20, duprdwr = 30;
70
71struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000072 int *nfd;
73 mode_t mode;
plars865695b2001-08-27 22:15:12 +000074} TC[] = {
75 /* The first test creat(es) a file with mode 0444 */
Garrett Cooper8afc5e22010-12-17 02:56:19 -080076 { &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) }
plars865695b2001-08-27 22:15:12 +000081};
82
robbiew57ce7e52003-03-25 18:44:20 +000083int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000084{
subrata_modak56207ce2009-03-23 13:35:39 +000085 int lc; /* loop counter */
86 char *msg; /* message returned from parse_opts */
plars865695b2001-08-27 22:15:12 +000087 int i, ofd;
88 struct stat oldbuf, newbuf;
89
90 /* parse standard options */
Garrett Cooper8afc5e22010-12-17 02:56:19 -080091 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +000092 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000093
94 setup();
95
plars865695b2001-08-27 22:15:12 +000096 for (lc = 0; TEST_LOOPING(lc); lc++) {
97
plars865695b2001-08-27 22:15:12 +000098 Tst_count = 0;
99
100 /* loop through the test cases */
101 for (i = 0; i < TST_TOTAL; i++) {
102
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800103 if ((ofd = creat(testfile, TC[i].mode)) == -1)
104 tst_brkm(TBROK|TERRNO, cleanup, "creat failed");
plars865695b2001-08-27 22:15:12 +0000105
106 TEST(dup2(ofd, *TC[i].nfd));
107
subrata_modak56207ce2009-03-23 13:35:39 +0000108 if (TEST_RETURN == -1) {
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800109 tst_resm(TFAIL|TERRNO,
110 "call failed unexpectedly");
subrata_modak56207ce2009-03-23 13:35:39 +0000111 continue;
112 }
plars865695b2001-08-27 22:15:12 +0000113
subrata_modak56207ce2009-03-23 13:35:39 +0000114 if (STD_FUNCTIONAL_TEST) {
plars865695b2001-08-27 22:15:12 +0000115
116 /* stat the original file */
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800117 if (fstat(ofd, &oldbuf) == -1)
118 tst_brkm(TBROK|TERRNO, cleanup,
119 "fstat #1 failed");
plars865695b2001-08-27 22:15:12 +0000120
121 /* stat the duped file */
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800122 if (fstat(*TC[i].nfd, &newbuf) == -1)
123 tst_brkm(TBROK|TERRNO, cleanup,
124 "fstat #2 failed");
plars865695b2001-08-27 22:15:12 +0000125
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800126 if (oldbuf.st_mode != newbuf.st_mode)
plars865695b2001-08-27 22:15:12 +0000127 tst_resm(TFAIL, "original and dup "
128 "modes do not match");
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800129 else
130 tst_resm(TPASS, "fstat shows new and "
plars865695b2001-08-27 22:15:12 +0000131 "old modes are the same");
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800132 } else
plars865695b2001-08-27 22:15:12 +0000133 tst_resm(TPASS, "call succeeded");
plars865695b2001-08-27 22:15:12 +0000134
135 /* remove the file so that we can use it again */
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800136 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");
plars865695b2001-08-27 22:15:12 +0000142 }
143 }
144 cleanup();
145
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800146 tst_exit();
plars865695b2001-08-27 22:15:12 +0000147}
148
149/*
150 * setup() - performs all ONE TIME setup for this test.
151 */
subrata_modak56207ce2009-03-23 13:35:39 +0000152void setup()
plars865695b2001-08-27 22:15:12 +0000153{
Garrett Cooper2c282152010-12-16 00:55:50 -0800154
plars865695b2001-08-27 22:15:12 +0000155 tst_sig(NOFORK, DEF_HANDLER, cleanup);
156
plars865695b2001-08-27 22:15:12 +0000157 TEST_PAUSE;
158
plars865695b2001-08-27 22:15:12 +0000159 tst_tmpdir();
160
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800161 (void)umask(0);
plars865695b2001-08-27 22:15:12 +0000162
plars865695b2001-08-27 22:15:12 +0000163 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_modak56207ce2009-03-23 13:35:39 +0000170void cleanup()
plars865695b2001-08-27 22:15:12 +0000171{
plars865695b2001-08-27 22:15:12 +0000172 TEST_CLEANUP;
173
plars865695b2001-08-27 22:15:12 +0000174 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700175}