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 int fd, fd1;
79 int mypid;
80 char fname[20];
81
82 struct test_case_t {
83 int *ofd;
84 int *nfd;
85 int error;
86 void (*setupfunc) ();
87 } TC[] = {
88 /* First fd argument is less than 0 - EBADF */
89 {
90 &badfd, &goodfd, EBADF, NULL},
91 /* First fd argument is getdtablesize() - EBADF */
92 {
93 &maxfd, &goodfd, EBADF, NULL},
94 /* Second fd argument is less than 0 - EBADF */
95 {
96 &mystdout, &badfd, EBADF, NULL},
97 /* Second fd argument is getdtablesize() - EBADF */
98 {
99 &mystdout, &maxfd, EBADF, NULL},};
100
main(int ac,char ** av)101 int main(int ac, char **av)
102 {
103 int lc;
104 int i, j;
105
106 tst_parse_opts(ac, av, NULL, NULL);
107
108 setup();
109
110 for (lc = 0; TEST_LOOPING(lc); lc++) {
111
112 tst_count = 0;
113
114 /* loop through the test cases */
115
116 for (i = 0; i < TST_TOTAL; i++) {
117
118 /* call the test case setup routine if necessary */
119 if (TC[i].setupfunc != NULL)
120 (*TC[i].setupfunc) ();
121
122 TEST(dup2(*TC[i].ofd, *TC[i].nfd));
123
124 if (TEST_RETURN != -1) {
125 tst_resm(TFAIL, "call succeeded unexpectedly");
126 continue;
127 }
128
129 if (TEST_ERRNO == TC[i].error) {
130 tst_resm(TPASS,
131 "failed as expected - errno = %d : %s",
132 TEST_ERRNO, strerror(TEST_ERRNO));
133 } else {
134 tst_resm(TFAIL | TTERRNO,
135 "failed unexpectedly; "
136 "expected %d: %s", TC[i].error,
137 strerror(TC[i].error));
138 }
139 }
140 /* cleanup things in case we are looping */
141 for (j = fd1; j < maxfd; j++) {
142 sprintf(fname, "dup201.%d.%d", j, mypid);
143 (void)close(j);
144 (void)unlink(fname);
145 }
146 }
147 cleanup();
148
149 tst_exit();
150 }
151
152 /*
153 * setup() - performs all ONE TIME setup for this test.
154 */
setup(void)155 void setup(void)
156 {
157
158 tst_sig(NOFORK, DEF_HANDLER, cleanup);
159
160 TEST_PAUSE;
161
162 tst_tmpdir();
163
164 /* get some test specific values */
165 maxfd = getdtablesize();
166 mypid = getpid();
167 }
168
169 /*
170 * cleanup() - performs all ONE TIME cleanup for this test at
171 * completion or premature exit.
172 */
cleanup(void)173 void cleanup(void)
174 {
175
176 tst_rmdir();
177 }
178