1 /*
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * NAME
22  *	dup204.c
23  *
24  * DESCRIPTION
25  *	Testcase to check the basic functionality of dup2(2).
26  *
27  * ALGORITHM
28  *	attempt to call dup2() on read/write ends of a pipe
29  *
30  * USAGE:  <for command-line>
31  *  dup204 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
32  *     where,  -c n : Run n copies concurrently.
33  *             -f   : Turn off functionality Testing.
34  *             -i n : Execute test n times.
35  *             -I x : Execute test for x seconds.
36  *             -P x : Pause for x seconds between iterations.
37  *             -t   : Turn on syscall timing.
38  *
39  * RESTRICTION
40  *	NONE
41  */
42 
43 #ifndef _GNU_SOURCE
44 #define _GNU_SOURCE
45 #endif
46 #include <sys/types.h>
47 #include <sys/fcntl.h>
48 #include <sys/stat.h>
49 #include <errno.h>
50 #include <signal.h>
51 #include <string.h>
52 #include "test.h"
53 
54 void setup();
55 void cleanup();
56 
57 char *TCID = "dup204";
58 int TST_TOTAL = 2;
59 
60 int fd[2];
61 int nfd[2];
62 
main(int ac,char ** av)63 int main(int ac, char **av)
64 {
65 	int lc;
66 	int i;
67 	struct stat oldbuf, newbuf;
68 
69 	tst_parse_opts(ac, av, NULL, NULL);
70 
71 	setup();
72 
73 	for (lc = 0; TEST_LOOPING(lc); lc++) {
74 
75 		tst_count = 0;
76 
77 		/* loop through the test cases */
78 		for (i = 0; i < TST_TOTAL; i++) {
79 			TEST(dup2(fd[i], nfd[i]));
80 
81 			if (TEST_RETURN == -1) {
82 				tst_resm(TFAIL, "call failed unexpectedly");
83 				continue;
84 			}
85 
86 			if (fstat(fd[i], &oldbuf) == -1)
87 				tst_brkm(TBROK, cleanup, "fstat() #1 "
88 					 "failed");
89 			if (fstat(nfd[i], &newbuf) == -1)
90 				tst_brkm(TBROK, cleanup, "fstat() #2 "
91 					 "failed");
92 
93 			if (oldbuf.st_ino != newbuf.st_ino)
94 				tst_resm(TFAIL, "original and duped "
95 					 "inodes do not match");
96 			else
97 				tst_resm(TPASS, "original and duped "
98 					 "inodes are the same");
99 
100 			if (close(TEST_RETURN) == -1)
101 				tst_brkm(TBROK | TERRNO, cleanup,
102 					 "close failed");
103 		}
104 	}
105 
106 	cleanup();
107 	tst_exit();
108 }
109 
setup(void)110 void setup(void)
111 {
112 	fd[0] = -1;
113 
114 	tst_sig(FORK, DEF_HANDLER, cleanup);
115 
116 	TEST_PAUSE;
117 
118 	tst_tmpdir();
119 
120 	if (pipe(fd) == -1)
121 		tst_brkm(TBROK | TERRNO, cleanup, "pipe failed");
122 }
123 
cleanup(void)124 void cleanup(void)
125 {
126 	int i;
127 
128 	for (i = 0; i < ARRAY_SIZE(fd); i++) {
129 		close(fd[i]);
130 		close(nfd[i]);
131 	}
132 
133 	tst_rmdir();
134 }
135