blob: b818030e571021d73f632ef1a98859fda8f6d1d9 [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 * 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>
Garrett Coopere8530df2010-12-21 11:37:57 -080051#include "test.h"
plars865695b2001-08-27 22:15:12 +000052
53void setup(void);
54void cleanup(void);
55
56char *TCID = "dup203";
57int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000058
robbiew57ce7e52003-03-25 18:44:20 +000059int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000060{
61 int fd0, fd1, fd2, rval;
62 char filename0[40], filename1[40];
63 char buf[40];
64
Cyril Hrubis89af32a2012-10-24 16:39:11 +020065 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020066 const char *msg;
plars865695b2001-08-27 22:15:12 +000067
Garrett Cooper80b31102010-12-17 02:46:24 -080068 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080069 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000070
71 setup();
72
plars865695b2001-08-27 22:15:12 +000073 for (lc = 0; TEST_LOOPING(lc); lc++) {
74
Caspar Zhangd59a6592013-03-07 14:59:12 +080075 tst_count = 0;
robbiew57ce7e52003-03-25 18:44:20 +000076//block1:
plars865695b2001-08-27 22:15:12 +000077 tst_resm(TINFO, "Enter block 1");
78 tst_resm(TINFO, "Test duping over an open fd");
79
80 sprintf(filename0, "dup202.file0.%d\n", getpid());
81 sprintf(filename1, "dup202.file1.%d\n", getpid());
82 unlink(filename0);
83 unlink(filename1);
84
Garrett Cooper80b31102010-12-17 02:46:24 -080085 if ((fd0 = creat(filename0, 0666)) == -1)
plars865695b2001-08-27 22:15:12 +000086 tst_brkm(TBROK, cleanup, "cannot create first file");
Garrett Cooper80b31102010-12-17 02:46:24 -080087 if (write(fd0, filename0, strlen(filename0)) == -1)
plars865695b2001-08-27 22:15:12 +000088 tst_brkm(TBROK, cleanup, "filename0: write(2) failed");
plars865695b2001-08-27 22:15:12 +000089
Garrett Cooper80b31102010-12-17 02:46:24 -080090 if ((fd1 = creat(filename1, 0666)) == -1)
plars865695b2001-08-27 22:15:12 +000091 tst_brkm(TBROK, cleanup, "Cannot create second file");
Garrett Cooper80b31102010-12-17 02:46:24 -080092 if (write(fd1, filename1, strlen(filename1)) == -1)
plars865695b2001-08-27 22:15:12 +000093 tst_brkm(TBROK, cleanup, "filename1: write(2) failed");
plars865695b2001-08-27 22:15:12 +000094
Garrett Cooper80b31102010-12-17 02:46:24 -080095 if (close(fd0) == -1)
plars865695b2001-08-27 22:15:12 +000096 tst_brkm(TBROK, cleanup, "close(2) fd0 failed");
Garrett Cooper80b31102010-12-17 02:46:24 -080097 if ((fd0 = open(filename0, O_RDONLY)) == -1)
plars865695b2001-08-27 22:15:12 +000098 tst_brkm(TBROK, cleanup, "open(2) on filename0 failed");
plars865695b2001-08-27 22:15:12 +000099
Garrett Cooper80b31102010-12-17 02:46:24 -0800100 if (close(fd1) == -1)
plars865695b2001-08-27 22:15:12 +0000101 tst_brkm(TBROK, cleanup, "close(2) fd1 failed");
Garrett Cooper80b31102010-12-17 02:46:24 -0800102 if ((fd1 = open(filename1, O_RDONLY)) == -1)
plars865695b2001-08-27 22:15:12 +0000103 tst_brkm(TBROK, cleanup, "open(2) on filename1 failed");
plars865695b2001-08-27 22:15:12 +0000104
105 TEST(dup2(fd0, fd1));
106
107 if ((fd2 = TEST_RETURN) == -1) {
108 tst_resm(TFAIL, "call failed unexpectedly");
Cyril Hrubise38b9612014-06-02 17:20:57 +0200109 } else {
plars865695b2001-08-27 22:15:12 +0000110 if (fd1 != fd2) {
111 tst_resm(TFAIL, "file descriptors don't match");
112 break;
113 }
114
115 memset(buf, 0, sizeof(buf));
Garrett Cooper80b31102010-12-17 02:46:24 -0800116 if (read(fd2, buf, sizeof(buf)) == -1)
plars865695b2001-08-27 22:15:12 +0000117 tst_brkm(TBROK, cleanup, "read(2) failed");
Garrett Cooper80b31102010-12-17 02:46:24 -0800118 if (strcmp(buf, filename0) != 0)
plars865695b2001-08-27 22:15:12 +0000119 tst_resm(TFAIL, "read from file got bad data");
plars865695b2001-08-27 22:15:12 +0000120 tst_resm(TPASS, "dup2 test 1 functionality is correct");
Cyril Hrubise38b9612014-06-02 17:20:57 +0200121 }
plars865695b2001-08-27 22:15:12 +0000122
123 close(fd0);
124 close(fd1);
125 close(fd2);
126 unlink(filename0);
127 unlink(filename1);
128
129 tst_resm(TINFO, "Exit block 1");
130
robbiew57ce7e52003-03-25 18:44:20 +0000131//block2:
plars865695b2001-08-27 22:15:12 +0000132 tst_resm(TINFO, "Enter block 2");
133 tst_resm(TINFO, "Test close on exec flag");
134
135 sprintf(filename0, "dup02.%d\n", getpid());
136 unlink(filename0);
137
138 if ((fd0 = creat(filename0, 0666)) == -1) {
139 tst_brkm(TBROK, cleanup, "Cannot create first file");
Garrett Cooper80b31102010-12-17 02:46:24 -0800140 }
plars865695b2001-08-27 22:15:12 +0000141 if (fcntl(fd0, F_SETFD, 1) == -1) {
142 tst_brkm(TBROK, cleanup, "setting close on exec flag "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800143 "on fd0 failed");
Garrett Cooper80b31102010-12-17 02:46:24 -0800144 }
plars865695b2001-08-27 22:15:12 +0000145
subrata_modak6805f2a2008-03-14 19:49:20 +0000146 if ((fd2 = creat(filename1, 0666)) == -1) {
147 tst_brkm(TBROK, cleanup, "Cannot create second file");
Garrett Cooper80b31102010-12-17 02:46:24 -0800148 }
subrata_modak6805f2a2008-03-14 19:49:20 +0000149
150 if (close(fd2) == -1) {
151 tst_brkm(TBROK, cleanup, "close(2) fd_closed failed");
Garrett Cooper80b31102010-12-17 02:46:24 -0800152 }
subrata_modak6805f2a2008-03-14 19:49:20 +0000153
154 TEST(dup2(fd0, fd2));
plars865695b2001-08-27 22:15:12 +0000155
156 if ((fd1 = TEST_RETURN) == -1) {
157 tst_resm(TFAIL, "call failed unexpectedly");
Cyril Hrubise38b9612014-06-02 17:20:57 +0200158 } else {
subrata_modak6805f2a2008-03-14 19:49:20 +0000159 if (fd1 != fd2) {
plars865695b2001-08-27 22:15:12 +0000160 tst_resm(TFAIL, "bad dup2 descriptor %d", fd1);
161 break;
162 }
163
164 if ((rval = fcntl(fd1, F_GETFD, 0)) != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800165 tst_resm(TBROK | TERRNO,
166 "fcntl F_GETFD on fd1 failed; expected a "
167 "return value of 0x0, got %#x", rval);
plars865695b2001-08-27 22:15:12 +0000168 break;
Garrett Cooper80b31102010-12-17 02:46:24 -0800169 }
Garrett Coopera2a72742011-01-18 23:45:24 -0800170 if ((rval = (fcntl(fd0, F_GETFL, 0) & O_ACCMODE)) !=
171 O_WRONLY) {
plars865695b2001-08-27 22:15:12 +0000172 tst_resm(TFAIL, "fctnl F_GETFL bad rval on fd0 "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800173 "Expected %#x got %#x", O_WRONLY,
174 rval);
Garrett Cooper80b31102010-12-17 02:46:24 -0800175 }
plars865695b2001-08-27 22:15:12 +0000176 tst_resm(TPASS, "dup2 test 2 functionality is correct");
plars865695b2001-08-27 22:15:12 +0000177 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200178
subrata_modakdad6e1a2007-10-30 10:46:58 +0000179 close(fd0);
180 close(fd1);
plars865695b2001-08-27 22:15:12 +0000181
182 unlink(filename0);
subrata_modak6805f2a2008-03-14 19:49:20 +0000183 unlink(filename1);
plars865695b2001-08-27 22:15:12 +0000184 tst_resm(TINFO, "Exit block 2");
185 }
plars865695b2001-08-27 22:15:12 +0000186
Cyril Hrubise38b9612014-06-02 17:20:57 +0200187 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800188 tst_exit();
plars865695b2001-08-27 22:15:12 +0000189}
190
191/*
192 * setup() - performs all ONE TIME setup for this test.
193 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400194void setup(void)
plars865695b2001-08-27 22:15:12 +0000195{
Garrett Cooper2c282152010-12-16 00:55:50 -0800196
plars865695b2001-08-27 22:15:12 +0000197 tst_sig(NOFORK, DEF_HANDLER, cleanup);
198
plars865695b2001-08-27 22:15:12 +0000199 TEST_PAUSE;
200
plars865695b2001-08-27 22:15:12 +0000201 tst_tmpdir();
202}
203
plars865695b2001-08-27 22:15:12 +0000204/*
205 * cleanup() - performs all ONE TIME cleanup for this test at
206 * completion or premature exit.
207 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400208void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000209{
plars865695b2001-08-27 22:15:12 +0000210 tst_rmdir();
Garrett Coopera2a72742011-01-18 23:45:24 -0800211}