blob: 6cf30e5254a68d22d560088b714ce0476c8a3b8e [file] [log] [blame]
robbiewc7861b52002-12-30 20:21:04 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewc7861b52002-12-30 20:21:04 +000018 */
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
robbiewc7861b52002-12-30 20:21:04 +000032#include <sys/param.h>
33#include <sys/types.h>
34#include <sys/stat.h>
Garrett Cooper8afc5e22010-12-17 02:56:19 -080035#include <errno.h>
36#include <fcntl.h>
37#include <stdio.h>
38#include <unistd.h>
robbiewc7861b52002-12-30 20:21:04 +000039#include "test.h"
robbiewc7861b52002-12-30 20:21:04 +000040
41char *TCID = "dup205";
42int TST_TOTAL = 1;
subrata_modak56207ce2009-03-23 13:35:39 +000043int local_flag;
robbiewc7861b52002-12-30 20:21:04 +000044
45#define PASSED 1
46#define FAILED 0
47
Garrett Cooper8afc5e22010-12-17 02:56:19 -080048static void setup(void);
49static void cleanup(void);
50
51int main(int ac, char *av[])
robbiewc7861b52002-12-30 20:21:04 +000052{
subrata_modak56207ce2009-03-23 13:35:39 +000053 int *fildes;
54 int ifile;
55 char pfilname[40];
56 int min;
57 int serrno;
robbiewc7861b52002-12-30 20:21:04 +000058
Cyril Hrubis89af32a2012-10-24 16:39:11 +020059 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020060 const char *msg;
robbiewc7861b52002-12-30 20:21:04 +000061
Garrett Cooper38d3de12010-12-20 11:39:36 -080062 ifile = -1;
63
Garrett Cooper8afc5e22010-12-17 02:56:19 -080064 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
65 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewc7861b52002-12-30 20:21:04 +000066
subrata_modak56207ce2009-03-23 13:35:39 +000067 local_flag = PASSED;
robbiewc7861b52002-12-30 20:21:04 +000068
Garrett Cooper8afc5e22010-12-17 02:56:19 -080069 setup();
70
subrata_modak56207ce2009-03-23 13:35:39 +000071 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiewc7861b52002-12-30 20:21:04 +000072
subrata_modak56207ce2009-03-23 13:35:39 +000073 min = getdtablesize(); /* get number of files allowed open */
robbiewc7861b52002-12-30 20:21:04 +000074
Garrett Cooper8afc5e22010-12-17 02:56:19 -080075 fildes = malloc((min + 10) * sizeof(int));
76 if (fildes == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +080077 tst_brkm(TBROK | TERRNO, cleanup, "malloc error");
subrata_modakbdbaec52009-02-26 12:14:51 +000078
subrata_modak56207ce2009-03-23 13:35:39 +000079 sprintf(pfilname, "./dup205.%d\n", getpid());
80 unlink(pfilname);
81 serrno = 0;
Garrett Cooper8afc5e22010-12-17 02:56:19 -080082 if ((fildes[0] = creat(pfilname, 0666)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080083 tst_brkm(TBROK | TERRNO, cleanup, "creat failed");
Garrett Cooper8afc5e22010-12-17 02:56:19 -080084 else {
robbiewc7861b52002-12-30 20:21:04 +000085 fildes[fildes[0]] = fildes[0];
subrata_modak56207ce2009-03-23 13:35:39 +000086 for (ifile = fildes[0] + 1; ifile < min + 10; ifile++) {
Garrett Cooper8afc5e22010-12-17 02:56:19 -080087 if ((fildes[ifile] = dup2(fildes[ifile - 1],
Wanlong Gao354ebb42012-12-07 10:10:04 +080088 ifile)) == -1) {
robbiewc7861b52002-12-30 20:21:04 +000089 serrno = errno;
subrata_modak56207ce2009-03-23 13:35:39 +000090 break;
robbiewc7861b52002-12-30 20:21:04 +000091 } else {
92 if (fildes[ifile] != ifile) {
Garrett Cooper8afc5e22010-12-17 02:56:19 -080093 tst_brkm(TFAIL, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +080094 "got wrong descriptor "
95 "number back (%d != %d)",
96 fildes[ifile], ifile);
robbiewc7861b52002-12-30 20:21:04 +000097 }
98 }
subrata_modak56207ce2009-03-23 13:35:39 +000099 } /* end for */
100 if (ifile < min) {
robbiewc7861b52002-12-30 20:21:04 +0000101 tst_resm(TFAIL, "Not enough files duped");
subrata_modak56207ce2009-03-23 13:35:39 +0000102 local_flag = FAILED;
103 } else if (ifile > min) {
robbiewc7861b52002-12-30 20:21:04 +0000104 tst_resm(TFAIL, "Too many files duped");
subrata_modak56207ce2009-03-23 13:35:39 +0000105 local_flag = FAILED;
robbiewc7861b52002-12-30 20:21:04 +0000106 }
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800107 if (serrno != EBADF && serrno != EMFILE &&
108 serrno != EINVAL) {
robbiewc7861b52002-12-30 20:21:04 +0000109 tst_resm(TFAIL, "bad errno on dup2 failure");
subrata_modak56207ce2009-03-23 13:35:39 +0000110 local_flag = FAILED;
robbiewc7861b52002-12-30 20:21:04 +0000111 }
subrata_modak56207ce2009-03-23 13:35:39 +0000112 }
subrata_modak56207ce2009-03-23 13:35:39 +0000113 unlink(pfilname);
Eryu Guan60c6ce22013-06-24 23:49:43 +0800114 for (ifile = fildes[0]; ifile < min + 10; ifile++)
115 close(fildes[ifile]);
robbiewc7861b52002-12-30 20:21:04 +0000116 if (local_flag == PASSED) {
subrata_modak56207ce2009-03-23 13:35:39 +0000117 tst_resm(TPASS, "Test passed.");
118 } else {
119 tst_resm(TFAIL, "Test failed.");
120 }
robbiewc7861b52002-12-30 20:21:04 +0000121
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800122 }
123 cleanup();
robbiewc7861b52002-12-30 20:21:04 +0000124 tst_exit();
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800125}
126
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127static void setup(void)
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800128{
129 tst_tmpdir();
130}
131
Wanlong Gao354ebb42012-12-07 10:10:04 +0800132static void cleanup(void)
Garrett Cooper8afc5e22010-12-17 02:56:19 -0800133{
134 tst_rmdir();
Garrett Cooper38d3de12010-12-20 11:39:36 -0800135}