• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <fcntl.h>
48 #include <sys/stat.h>
49 #include <errno.h>
50 #include <signal.h>
51 #include <string.h>
52 #include "test.h"
53 #include "safe_macros.h"
54 
55 void setup();
56 void cleanup();
57 
58 char *TCID = "dup204";
59 int TST_TOTAL = 2;
60 
61 int fd[2];
62 int nfd[2];
63 
main(int ac,char ** av)64 int main(int ac, char **av)
65 {
66 	int lc;
67 	int i;
68 	struct stat oldbuf, newbuf;
69 
70 	tst_parse_opts(ac, av, NULL, NULL);
71 
72 	setup();
73 
74 	for (lc = 0; TEST_LOOPING(lc); lc++) {
75 
76 		tst_count = 0;
77 
78 		/* loop through the test cases */
79 		for (i = 0; i < TST_TOTAL; i++) {
80 			TEST(dup2(fd[i], nfd[i]));
81 
82 			if (TEST_RETURN == -1) {
83 				tst_resm(TFAIL, "call failed unexpectedly");
84 				continue;
85 			}
86 
87 			SAFE_FSTAT(cleanup, fd[i], &oldbuf);
88 			SAFE_FSTAT(cleanup, nfd[i], &newbuf);
89 
90 			if (oldbuf.st_ino != newbuf.st_ino)
91 				tst_resm(TFAIL, "original and duped "
92 					 "inodes do not match");
93 			else
94 				tst_resm(TPASS, "original and duped "
95 					 "inodes are the same");
96 
97 			SAFE_CLOSE(cleanup, TEST_RETURN);
98 		}
99 	}
100 
101 	cleanup();
102 	tst_exit();
103 }
104 
setup(void)105 void setup(void)
106 {
107 	fd[0] = -1;
108 
109 	tst_sig(FORK, DEF_HANDLER, cleanup);
110 
111 	TEST_PAUSE;
112 
113 	tst_tmpdir();
114 
115 	SAFE_PIPE(cleanup, fd);
116 }
117 
cleanup(void)118 void cleanup(void)
119 {
120 	int i;
121 
122 	for (i = 0; i < ARRAY_SIZE(fd); i++) {
123 		close(fd[i]);
124 		close(nfd[i]);
125 	}
126 
127 	tst_rmdir();
128 }
129