• 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  *	dup201.c
23  *
24  * DESCRIPTION
25  *	Negative tests for dup2() with bad fd (EBADF)
26  *
27  * ALGORITHM
28  * 	Setup:
29  *	a.	Setup signal handling.
30  *	b.	Pause for SIGUSR1 if option specified.
31  *
32  * 	Test:
33  *	a.	Loop if the proper options are given.
34  *	b.	Loop through the test cases
35  * 	c.	Execute dup2() system call
36  *	d.	Check return code, if system call failed (return=-1), test
37  *		for EBADF.
38  *
39  * 	Cleanup:
40  * 	a.	Print errno log and/or timing stats if options given
41  *
42  * USAGE:  <for command-line>
43  *  dup201 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
44  *     where,  -c n : Run n copies concurrently.
45  *             -e   : Turn on errno logging.
46  *             -i n : Execute test n times.
47  *             -I x : Execute test for x seconds.
48  *             -P x : Pause for x seconds between iterations.
49  *             -t   : Turn on syscall timing.
50  *
51  * HISTORY
52  *	07/2001 Ported by Wayne Boyer
53  *	01/2002 Removed EMFILE test - Paul Larson
54  *
55  * RESTRICTIONS
56  * 	NONE
57  */
58 
59 #include <sys/types.h>
60 #include <fcntl.h>
61 #include <errno.h>
62 #include <sys/time.h>
63 #include <sys/resource.h>
64 #include <unistd.h>
65 #include <signal.h>
66 #include "test.h"
67 
68 void setup(void);
69 void cleanup(void);
70 
71 char *TCID = "dup201";
72 int TST_TOTAL = 4;
73 
74 int maxfd;
75 int goodfd = 5;
76 int badfd = -1;
77 int mystdout = 0;
78 
79 struct test_case_t {
80 	int *ofd;
81 	int *nfd;
82 	int error;
83 	void (*setupfunc) ();
84 } TC[] = {
85 	/* First fd argument is less than 0 - EBADF */
86 	{&badfd, &goodfd, EBADF, NULL},
87 	    /* First fd argument is getdtablesize() - EBADF */
88 	{&maxfd, &goodfd, EBADF, NULL},
89 	    /* Second fd argument is less than 0 - EBADF */
90 	{&mystdout, &badfd, EBADF, NULL},
91 	    /* Second fd argument is getdtablesize() - EBADF */
92 	{&mystdout, &maxfd, EBADF, NULL},
93 };
94 
main(int ac,char ** av)95 int main(int ac, char **av)
96 {
97 	int lc;
98 	int i;
99 
100 	tst_parse_opts(ac, av, NULL, NULL);
101 
102 	setup();
103 
104 	for (lc = 0; TEST_LOOPING(lc); lc++) {
105 
106 		tst_count = 0;
107 
108 		/* loop through the test cases */
109 
110 		for (i = 0; i < TST_TOTAL; i++) {
111 
112 			/* call the test case setup routine if necessary */
113 			if (TC[i].setupfunc != NULL)
114 				(*TC[i].setupfunc) ();
115 
116 			TEST(dup2(*TC[i].ofd, *TC[i].nfd));
117 
118 			if (TEST_RETURN != -1) {
119 				tst_resm(TFAIL, "call succeeded unexpectedly");
120 				continue;
121 			}
122 
123 			if (TEST_ERRNO == TC[i].error) {
124 				tst_resm(TPASS,
125 					 "failed as expected - errno = %d : %s",
126 					 TEST_ERRNO, strerror(TEST_ERRNO));
127 			} else {
128 				tst_resm(TFAIL | TTERRNO,
129 					 "failed unexpectedly; "
130 					 "expected %d: %s", TC[i].error,
131 					 strerror(TC[i].error));
132 			}
133 		}
134 	}
135 	cleanup();
136 
137 	tst_exit();
138 }
139 
140 /*
141  * setup() - performs all ONE TIME setup for this test.
142  */
setup(void)143 void setup(void)
144 {
145 
146 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
147 
148 	TEST_PAUSE;
149 
150 	tst_tmpdir();
151 
152 	/* get some test specific values */
153 	maxfd = getdtablesize();
154 }
155 
156 /*
157  * cleanup() - performs all ONE TIME cleanup for this test at
158  *	       completion or premature exit.
159  */
cleanup(void)160 void cleanup(void)
161 {
162 	tst_rmdir();
163 }
164