robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2002 |
| 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 |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* Ported from SPIE, section2/iosuite/dup6.c, by Airong Zhang */ |
| 21 | |
| 22 | /*====================================================================== |
| 23 | =================== TESTPLAN SEGMENT =================== |
| 24 | >KEYS: < dup2() |
| 25 | >WHAT: < Does dup return -1 on the 21st file? |
| 26 | >HOW: < Create up to _NFILE files and check for -1 return on the |
| 27 | < next attempt |
| 28 | < Should check NOFILE as well as _NFILE. 19-Jun-84 Dale. |
| 29 | >BUGS: < |
| 30 | ======================================================================*/ |
| 31 | |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 32 | #include <sys/param.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 35 | #include <errno.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <stdio.h> |
| 38 | #include <unistd.h> |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 39 | #include "test.h" |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 40 | |
| 41 | char *TCID = "dup205"; |
| 42 | int TST_TOTAL = 1; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 43 | int local_flag; |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 44 | |
| 45 | #define PASSED 1 |
| 46 | #define FAILED 0 |
| 47 | |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 48 | static void setup(void); |
| 49 | static void cleanup(void); |
| 50 | |
| 51 | int main(int ac, char *av[]) |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 52 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 53 | int *fildes; |
| 54 | int ifile; |
| 55 | char pfilname[40]; |
| 56 | int min; |
| 57 | int serrno; |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 58 | |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 59 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 60 | const char *msg; |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 61 | |
Garrett Cooper | 38d3de1 | 2010-12-20 11:39:36 -0800 | [diff] [blame] | 62 | ifile = -1; |
| 63 | |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 64 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
| 65 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 66 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 67 | local_flag = PASSED; |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 68 | |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 69 | setup(); |
| 70 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 72 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | min = getdtablesize(); /* get number of files allowed open */ |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 74 | |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 75 | fildes = malloc((min + 10) * sizeof(int)); |
| 76 | if (fildes == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | tst_brkm(TBROK | TERRNO, cleanup, "malloc error"); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 78 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | sprintf(pfilname, "./dup205.%d\n", getpid()); |
| 80 | unlink(pfilname); |
| 81 | serrno = 0; |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 82 | if ((fildes[0] = creat(pfilname, 0666)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 83 | tst_brkm(TBROK | TERRNO, cleanup, "creat failed"); |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 84 | else { |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 85 | fildes[fildes[0]] = fildes[0]; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | for (ifile = fildes[0] + 1; ifile < min + 10; ifile++) { |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 87 | if ((fildes[ifile] = dup2(fildes[ifile - 1], |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 88 | ifile)) == -1) { |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 89 | serrno = errno; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | break; |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 91 | } else { |
| 92 | if (fildes[ifile] != ifile) { |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 93 | tst_brkm(TFAIL, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 94 | "got wrong descriptor " |
| 95 | "number back (%d != %d)", |
| 96 | fildes[ifile], ifile); |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | } /* end for */ |
| 100 | if (ifile < min) { |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 101 | tst_resm(TFAIL, "Not enough files duped"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | local_flag = FAILED; |
| 103 | } else if (ifile > min) { |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 104 | tst_resm(TFAIL, "Too many files duped"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | local_flag = FAILED; |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 106 | } |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 107 | if (serrno != EBADF && serrno != EMFILE && |
| 108 | serrno != EINVAL) { |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 109 | tst_resm(TFAIL, "bad errno on dup2 failure"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | local_flag = FAILED; |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 111 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | unlink(pfilname); |
Eryu Guan | 60c6ce2 | 2013-06-24 23:49:43 +0800 | [diff] [blame] | 114 | for (ifile = fildes[0]; ifile < min + 10; ifile++) |
| 115 | close(fildes[ifile]); |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 116 | if (local_flag == PASSED) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | tst_resm(TPASS, "Test passed."); |
| 118 | } else { |
| 119 | tst_resm(TFAIL, "Test failed."); |
| 120 | } |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 121 | |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 122 | } |
| 123 | cleanup(); |
robbiew | c7861b5 | 2002-12-30 20:21:04 +0000 | [diff] [blame] | 124 | tst_exit(); |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 127 | static void setup(void) |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 128 | { |
| 129 | tst_tmpdir(); |
| 130 | } |
| 131 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 132 | static void cleanup(void) |
Garrett Cooper | 8afc5e2 | 2010-12-17 02:56:19 -0800 | [diff] [blame] | 133 | { |
| 134 | tst_rmdir(); |
Garrett Cooper | 38d3de1 | 2010-12-20 11:39:36 -0800 | [diff] [blame] | 135 | } |