plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * dup201.c |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 23 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 24 | * DESCRIPTION |
plars | 119b949 | 2002-01-03 17:35:10 +0000 | [diff] [blame] | 25 | * Negative tests for dup2() with bad fd (EBADF) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | * ALGORITHM |
| 28 | * Setup: |
| 29 | * a. Setup signal handling. |
| 30 | * b. Pause for SIGUSR1 if option specified. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 31 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 38 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
plars | 119b949 | 2002-01-03 17:35:10 +0000 | [diff] [blame] | 53 | * 01/2002 Removed EMFILE test - Paul Larson |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | * |
| 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 Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 66 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
| 68 | void setup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | void cleanup(void); |
| 70 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 71 | char *TCID = "dup201"; |
| 72 | int TST_TOTAL = 4; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | int *ofd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | int *nfd; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | int error; |
| 86 | void (*setupfunc) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | } TC[] = { |
| 88 | /* First fd argument is less than 0 - EBADF */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 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},}; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | |
robbiew | 57ce7e5 | 2003-03-25 18:44:20 +0000 | [diff] [blame] | 101 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 103 | int lc; |
robbiew | 57ce7e5 | 2003-03-25 18:44:20 +0000 | [diff] [blame] | 104 | int i, j; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 105 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 107 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 108 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
| 110 | setup(); |
| 111 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 113 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 114 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
| 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 Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 121 | if (TC[i].setupfunc != NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 122 | (*TC[i].setupfunc) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | |
| 124 | TEST(dup2(*TC[i].ofd, *TC[i].nfd)); |
| 125 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 126 | if (TEST_RETURN != -1) { |
| 127 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 128 | continue; |
| 129 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | if (TEST_ERRNO == TC[i].error) { |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 132 | tst_resm(TPASS, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 133 | "failed as expected - errno = %d : %s", |
| 134 | TEST_ERRNO, strerror(TEST_ERRNO)); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 135 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 136 | tst_resm(TFAIL | TTERRNO, |
| 137 | "failed unexpectedly; " |
| 138 | "expected %d: %s", TC[i].error, |
| 139 | strerror(TC[i].error)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | /* cleanup things in case we are looping */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | for (j = fd1; j < maxfd; j++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | sprintf(fname, "dup201.%d.%d", j, mypid); |
| 145 | (void)close(j); |
| 146 | (void)unlink(fname); |
| 147 | } |
| 148 | } |
| 149 | cleanup(); |
| 150 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 151 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | * setup() - performs all ONE TIME setup for this test. |
| 156 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 157 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 159 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 161 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | TEST_PAUSE; |
| 163 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | 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 Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 175 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 179 | } |