blob: 59ad105511d5c2f73fd38123b9f29e31a93e826f [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 * dup203.c
23 *
24 * DESCRIPTION
25 * Testcase to check the basic functionality of dup2().
26 *
27 * ALGORITHM
28 * 1. Attempt to dup2() on an open file descriptor.
29 * 2. Attempt to dup2() on a close file descriptor.
30 *
31 * USAGE: <for command-line>
32 * dup203 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
33 * where, -c n : Run n copies concurrently.
34 * -f : Turn off functionality Testing.
35 * -i n : Execute test n times.
36 * -I x : Execute test for x seconds.
37 * -P x : Pause for x seconds between iterations.
38 * -t : Turn on syscall timing.
39 *
40 * HISTORY
41 * 07/2001 Ported by Wayne Boyer
42 *
43 * RESTRICTIONS
44 * NONE
45 */
46
47#include <fcntl.h>
48#include <sys/param.h>
49#include <errno.h>
robbiew15226cd2002-04-10 16:10:40 +000050#include <string.h>
plars865695b2001-08-27 22:15:12 +000051#include <test.h>
52#include <usctest.h>
53
54void setup(void);
55void cleanup(void);
56
57char *TCID = "dup203";
58int TST_TOTAL = 1;
59extern int Tst_count;
60
robbiew57ce7e52003-03-25 18:44:20 +000061int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000062{
63 int fd0, fd1, fd2, rval;
64 char filename0[40], filename1[40];
65 char buf[40];
66
subrata_modak56207ce2009-03-23 13:35:39 +000067 int lc; /* loop counter */
68 char *msg; /* message returned from parse_opts */
plars865695b2001-08-27 22:15:12 +000069
70 /* parse standard options */
Garrett Cooper80b31102010-12-17 02:46:24 -080071 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080072 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000073
74 setup();
75
plars865695b2001-08-27 22:15:12 +000076 for (lc = 0; TEST_LOOPING(lc); lc++) {
77
plars865695b2001-08-27 22:15:12 +000078 Tst_count = 0;
robbiew57ce7e52003-03-25 18:44:20 +000079//block1:
plars865695b2001-08-27 22:15:12 +000080 tst_resm(TINFO, "Enter block 1");
81 tst_resm(TINFO, "Test duping over an open fd");
82
83 sprintf(filename0, "dup202.file0.%d\n", getpid());
84 sprintf(filename1, "dup202.file1.%d\n", getpid());
85 unlink(filename0);
86 unlink(filename1);
87
Garrett Cooper80b31102010-12-17 02:46:24 -080088 if ((fd0 = creat(filename0, 0666)) == -1)
plars865695b2001-08-27 22:15:12 +000089 tst_brkm(TBROK, cleanup, "cannot create first file");
Garrett Cooper80b31102010-12-17 02:46:24 -080090 if (write(fd0, filename0, strlen(filename0)) == -1)
plars865695b2001-08-27 22:15:12 +000091 tst_brkm(TBROK, cleanup, "filename0: write(2) failed");
plars865695b2001-08-27 22:15:12 +000092
Garrett Cooper80b31102010-12-17 02:46:24 -080093 if ((fd1 = creat(filename1, 0666)) == -1)
plars865695b2001-08-27 22:15:12 +000094 tst_brkm(TBROK, cleanup, "Cannot create second file");
Garrett Cooper80b31102010-12-17 02:46:24 -080095 if (write(fd1, filename1, strlen(filename1)) == -1)
plars865695b2001-08-27 22:15:12 +000096 tst_brkm(TBROK, cleanup, "filename1: write(2) failed");
plars865695b2001-08-27 22:15:12 +000097
Garrett Cooper80b31102010-12-17 02:46:24 -080098 if (close(fd0) == -1)
plars865695b2001-08-27 22:15:12 +000099 tst_brkm(TBROK, cleanup, "close(2) fd0 failed");
Garrett Cooper80b31102010-12-17 02:46:24 -0800100 if ((fd0 = open(filename0, O_RDONLY)) == -1)
plars865695b2001-08-27 22:15:12 +0000101 tst_brkm(TBROK, cleanup, "open(2) on filename0 failed");
plars865695b2001-08-27 22:15:12 +0000102
Garrett Cooper80b31102010-12-17 02:46:24 -0800103 if (close(fd1) == -1)
plars865695b2001-08-27 22:15:12 +0000104 tst_brkm(TBROK, cleanup, "close(2) fd1 failed");
Garrett Cooper80b31102010-12-17 02:46:24 -0800105 if ((fd1 = open(filename1, O_RDONLY)) == -1)
plars865695b2001-08-27 22:15:12 +0000106 tst_brkm(TBROK, cleanup, "open(2) on filename1 failed");
plars865695b2001-08-27 22:15:12 +0000107
108 TEST(dup2(fd0, fd1));
109
110 if ((fd2 = TEST_RETURN) == -1) {
111 tst_resm(TFAIL, "call failed unexpectedly");
112 } else if (STD_FUNCTIONAL_TEST) {
113 if (fd1 != fd2) {
114 tst_resm(TFAIL, "file descriptors don't match");
115 break;
116 }
117
118 memset(buf, 0, sizeof(buf));
Garrett Cooper80b31102010-12-17 02:46:24 -0800119 if (read(fd2, buf, sizeof(buf)) == -1)
plars865695b2001-08-27 22:15:12 +0000120 tst_brkm(TBROK, cleanup, "read(2) failed");
Garrett Cooper80b31102010-12-17 02:46:24 -0800121 if (strcmp(buf, filename0) != 0)
plars865695b2001-08-27 22:15:12 +0000122 tst_resm(TFAIL, "read from file got bad data");
plars865695b2001-08-27 22:15:12 +0000123 tst_resm(TPASS, "dup2 test 1 functionality is correct");
Garrett Cooper80b31102010-12-17 02:46:24 -0800124 } else
plars865695b2001-08-27 22:15:12 +0000125 tst_resm(TPASS, "call succeeded");
plars865695b2001-08-27 22:15:12 +0000126
127 close(fd0);
128 close(fd1);
129 close(fd2);
130 unlink(filename0);
131 unlink(filename1);
132
133 tst_resm(TINFO, "Exit block 1");
134
robbiew57ce7e52003-03-25 18:44:20 +0000135//block2:
plars865695b2001-08-27 22:15:12 +0000136 tst_resm(TINFO, "Enter block 2");
137 tst_resm(TINFO, "Test close on exec flag");
138
139 sprintf(filename0, "dup02.%d\n", getpid());
140 unlink(filename0);
141
142 if ((fd0 = creat(filename0, 0666)) == -1) {
143 tst_brkm(TBROK, cleanup, "Cannot create first file");
Garrett Cooper80b31102010-12-17 02:46:24 -0800144 }
plars865695b2001-08-27 22:15:12 +0000145 if (fcntl(fd0, F_SETFD, 1) == -1) {
146 tst_brkm(TBROK, cleanup, "setting close on exec flag "
Garrett Cooper80b31102010-12-17 02:46:24 -0800147 "on fd0 failed");
148 }
plars865695b2001-08-27 22:15:12 +0000149
subrata_modak6805f2a2008-03-14 19:49:20 +0000150 if ((fd2 = creat(filename1, 0666)) == -1) {
151 tst_brkm(TBROK, cleanup, "Cannot create second file");
Garrett Cooper80b31102010-12-17 02:46:24 -0800152 }
subrata_modak6805f2a2008-03-14 19:49:20 +0000153
154 if (close(fd2) == -1) {
155 tst_brkm(TBROK, cleanup, "close(2) fd_closed failed");
Garrett Cooper80b31102010-12-17 02:46:24 -0800156 }
subrata_modak6805f2a2008-03-14 19:49:20 +0000157
158 TEST(dup2(fd0, fd2));
plars865695b2001-08-27 22:15:12 +0000159
160 if ((fd1 = TEST_RETURN) == -1) {
161 tst_resm(TFAIL, "call failed unexpectedly");
162 } else if (STD_FUNCTIONAL_TEST) {
subrata_modak6805f2a2008-03-14 19:49:20 +0000163 if (fd1 != fd2) {
plars865695b2001-08-27 22:15:12 +0000164 tst_resm(TFAIL, "bad dup2 descriptor %d", fd1);
165 break;
166 }
167
168 if ((rval = fcntl(fd1, F_GETFD, 0)) != 0) {
Garrett Cooper80b31102010-12-17 02:46:24 -0800169 tst_resm(TBROK|TERRNO,
170 "fcntl F_GETFD on fd1 failed; expected a "
171 "return value of 0x0, got %#x", rval);
plars865695b2001-08-27 22:15:12 +0000172 break;
Garrett Cooper80b31102010-12-17 02:46:24 -0800173 }
174 if (((rval = fcntl(fd0, F_GETFL, 0)) != O_WRONLY)) {
plars865695b2001-08-27 22:15:12 +0000175 tst_resm(TFAIL, "fctnl F_GETFL bad rval on fd0 "
Garrett Cooper80b31102010-12-17 02:46:24 -0800176 "Expected %#x got %#x", O_WRONLY, rval);
177 }
plars865695b2001-08-27 22:15:12 +0000178 tst_resm(TPASS, "dup2 test 2 functionality is correct");
179 } else {
180 tst_resm(TPASS, "call succeeded");
181 }
subrata_modakdad6e1a2007-10-30 10:46:58 +0000182 close(fd0);
183 close(fd1);
plars865695b2001-08-27 22:15:12 +0000184
185 unlink(filename0);
subrata_modak6805f2a2008-03-14 19:49:20 +0000186 unlink(filename1);
plars865695b2001-08-27 22:15:12 +0000187 tst_resm(TINFO, "Exit block 2");
188 }
189 cleanup();
190
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800191 tst_exit();
plars865695b2001-08-27 22:15:12 +0000192}
193
194/*
195 * setup() - performs all ONE TIME setup for this test.
196 */
subrata_modak56207ce2009-03-23 13:35:39 +0000197void setup()
plars865695b2001-08-27 22:15:12 +0000198{
Garrett Cooper2c282152010-12-16 00:55:50 -0800199
plars865695b2001-08-27 22:15:12 +0000200 tst_sig(NOFORK, DEF_HANDLER, cleanup);
201
plars865695b2001-08-27 22:15:12 +0000202 TEST_PAUSE;
203
plars865695b2001-08-27 22:15:12 +0000204 tst_tmpdir();
205}
206
plars865695b2001-08-27 22:15:12 +0000207/*
208 * cleanup() - performs all ONE TIME cleanup for this test at
209 * completion or premature exit.
210 */
subrata_modak56207ce2009-03-23 13:35:39 +0000211void cleanup()
plars865695b2001-08-27 22:15:12 +0000212{
plars865695b2001-08-27 22:15:12 +0000213 TEST_CLEANUP;
214
plars865695b2001-08-27 22:15:12 +0000215 tst_rmdir();
Garrett Cooper80b31102010-12-17 02:46:24 -0800216}