blob: 4435b6606e10bfb9fe47e5f92f977c8c656cce2e [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 * dup201.c
subrata_modak4bb656a2009-02-26 12:02:09 +000023 *
plars865695b2001-08-27 22:15:12 +000024 * DESCRIPTION
plars119b9492002-01-03 17:35:10 +000025 * Negative tests for dup2() with bad fd (EBADF)
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
plars865695b2001-08-27 22:15:12 +000027 * ALGORITHM
28 * Setup:
29 * a. Setup signal handling.
30 * b. Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000031 *
plars865695b2001-08-27 22:15:12 +000032 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000038 *
plars865695b2001-08-27 22:15:12 +000039 * 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
plars119b9492002-01-03 17:35:10 +000053 * 01/2002 Removed EMFILE test - Paul Larson
plars865695b2001-08-27 22:15:12 +000054 *
55 * RESTRICTIONS
56 * NONE
57 */
58
59#include <sys/types.h>
60#include <sys/fcntl.h>
61#include <errno.h>
62#include <sys/time.h>
63#include <sys/resource.h>
64#include <unistd.h>
65#include <signal.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080066#include "test.h"
plars865695b2001-08-27 22:15:12 +000067
68void setup(void);
plars865695b2001-08-27 22:15:12 +000069void cleanup(void);
70
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020071char *TCID = "dup201";
72int TST_TOTAL = 4;
plars865695b2001-08-27 22:15:12 +000073
74int maxfd;
75int goodfd = 5;
76int badfd = -1;
77int mystdout = 0;
78int fd, fd1;
79int mypid;
80char fname[20];
81
plars865695b2001-08-27 22:15:12 +000082struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000083 int *ofd;
plars865695b2001-08-27 22:15:12 +000084 int *nfd;
subrata_modak56207ce2009-03-23 13:35:39 +000085 int error;
86 void (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +000087} TC[] = {
88 /* First fd argument is less than 0 - EBADF */
Wanlong Gao354ebb42012-12-07 10:10:04 +080089 {
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},};
plars865695b2001-08-27 22:15:12 +0000100
robbiew57ce7e52003-03-25 18:44:20 +0000101int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000102{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200103 int lc;
robbiew57ce7e52003-03-25 18:44:20 +0000104 int i, j;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200105 const char *msg;
plars865695b2001-08-27 22:15:12 +0000106
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800107 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800108 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000109
110 setup();
111
plars865695b2001-08-27 22:15:12 +0000112 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800113
Caspar Zhangd59a6592013-03-07 14:59:12 +0800114 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000115
116 /* loop through the test cases */
117
118 for (i = 0; i < TST_TOTAL; i++) {
119
120 /* call the test case setup routine if necessary */
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800121 if (TC[i].setupfunc != NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 (*TC[i].setupfunc) ();
plars865695b2001-08-27 22:15:12 +0000123
124 TEST(dup2(*TC[i].ofd, *TC[i].nfd));
125
subrata_modak56207ce2009-03-23 13:35:39 +0000126 if (TEST_RETURN != -1) {
127 tst_resm(TFAIL, "call succeeded unexpectedly");
128 continue;
129 }
plars865695b2001-08-27 22:15:12 +0000130
subrata_modak56207ce2009-03-23 13:35:39 +0000131 if (TEST_ERRNO == TC[i].error) {
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800132 tst_resm(TPASS,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 "failed as expected - errno = %d : %s",
134 TEST_ERRNO, strerror(TEST_ERRNO));
subrata_modak56207ce2009-03-23 13:35:39 +0000135 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800136 tst_resm(TFAIL | TTERRNO,
137 "failed unexpectedly; "
138 "expected %d: %s", TC[i].error,
139 strerror(TC[i].error));
plars865695b2001-08-27 22:15:12 +0000140 }
141 }
142 /* cleanup things in case we are looping */
subrata_modak56207ce2009-03-23 13:35:39 +0000143 for (j = fd1; j < maxfd; j++) {
plars865695b2001-08-27 22:15:12 +0000144 sprintf(fname, "dup201.%d.%d", j, mypid);
145 (void)close(j);
146 (void)unlink(fname);
147 }
148 }
149 cleanup();
150
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800151 tst_exit();
plars865695b2001-08-27 22:15:12 +0000152}
153
154/*
plars865695b2001-08-27 22:15:12 +0000155 * setup() - performs all ONE TIME setup for this test.
156 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400157void setup(void)
plars865695b2001-08-27 22:15:12 +0000158{
Garrett Cooper2c282152010-12-16 00:55:50 -0800159
plars865695b2001-08-27 22:15:12 +0000160 tst_sig(NOFORK, DEF_HANDLER, cleanup);
161
plars865695b2001-08-27 22:15:12 +0000162 TEST_PAUSE;
163
plars865695b2001-08-27 22:15:12 +0000164 tst_tmpdir();
165
166 /* get some test specific values */
167 maxfd = getdtablesize();
168 mypid = getpid();
169}
170
171/*
172 * cleanup() - performs all ONE TIME cleanup for this test at
173 * completion or premature exit.
174 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400175void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000176{
plars865695b2001-08-27 22:15:12 +0000177
plars865695b2001-08-27 22:15:12 +0000178 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700179}