blob: 2889fbe0bd36384b089efd0886a682c459850353 [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 * fcntl12.c
23 *
24 * DESCRIPTION
25 * Testcase to test that fcntl() sets EMFILE for F_DUPFD command.
26 *
27 * ALGORITHM
28 * Get the size of the descriptor table of a process, by calling the
29 * getdtablesize() system call. Then attempt to use the F_DUPFD command
30 * for fcntl(), which should fail with EMFILE.
31 *
32 * USAGE
33 * fcntl12
34 *
35 * HISTORY
36 * 07/2001 Ported by Wayne Boyer
37 *
38 * RESTRICTIONS
39 * NONE
40 */
41
42#include <fcntl.h>
mridgedb639212005-01-04 21:04:11 +000043#include <sys/types.h>
44#include <sys/wait.h>
plars865695b2001-08-27 22:15:12 +000045#include <errno.h>
Garrett Cooper0a643cb2010-12-21 11:21:19 -080046#include "test.h"
plars865695b2001-08-27 22:15:12 +000047
48char *TCID = "fcntl12";
49int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000050
51int fail;
52char fname[20];
53void setup(void);
54void cleanup(void);
55
robbiew4cf80962003-03-25 21:53:41 +000056int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000057{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020058 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020059 const char *msg;
plars865695b2001-08-27 22:15:12 +000060
61 pid_t pid;
62 int fd, i, status, max_files;
subrata_modakbdbaec52009-02-26 12:14:51 +000063
Garrett Cooper45e285d2010-11-22 12:19:25 -080064 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080065 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000066 }
67
68 setup();
69
70 /* check for looping state if -i option is given */
71 for (lc = 0; TEST_LOOPING(lc); lc++) {
72
Caspar Zhangd59a6592013-03-07 14:59:12 +080073 /* reset tst_count in case we are looping */
74 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000075
mridgedb639212005-01-04 21:04:11 +000076/* //block1: */
plars865695b2001-08-27 22:15:12 +000077 tst_resm(TINFO, "Enter block 1");
78 tst_resm(TINFO, "Test for errno EMFILE");
79 fail = 0;
80
robbiewd34d5812005-07-11 22:28:09 +000081 pid = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +000082 if (pid < 0) {
83 tst_resm(TFAIL, "Fork failed");
84 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +080085 } else if (pid == 0) { /* child */
plars865695b2001-08-27 22:15:12 +000086 max_files = getdtablesize();
subrata_modak56207ce2009-03-23 13:35:39 +000087 for (i = 0; i < max_files; i++) {
plars865695b2001-08-27 22:15:12 +000088 if ((fd = open(fname, O_CREAT | O_RDONLY,
subrata_modak56207ce2009-03-23 13:35:39 +000089 0444)) == -1) {
plars865695b2001-08-27 22:15:12 +000090 break;
91 }
92 }
93
94 if (fcntl(1, F_DUPFD, 1) != -1) {
95 tst_resm(TFAIL, "fcntl failed to FAIL");
96 exit(1);
97 } else if (errno != EMFILE) {
98 tst_resm(TFAIL, "Expected EMFILE got %d",
99 errno);
100 exit(1);
101 }
102 exit(0);
103 }
104 waitpid(pid, &status, 0);
105 if (WEXITSTATUS(status) == 0) {
106 tst_resm(TINFO, "block 1 PASSED");
107 } else {
108 tst_resm(TINFO, "block 1 FAILED");
109 }
110 tst_resm(TINFO, "Exit block 1");
111 }
112 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800113 tst_exit();
plars865695b2001-08-27 22:15:12 +0000114}
115
116/*
117 * setup()
118 * performs all ONE TIME setup for this test
119 */
subrata_modak56207ce2009-03-23 13:35:39 +0000120void setup(void)
plars865695b2001-08-27 22:15:12 +0000121{
Garrett Cooper2c282152010-12-16 00:55:50 -0800122
plars865695b2001-08-27 22:15:12 +0000123 tst_sig(FORK, DEF_HANDLER, cleanup);
124
plars865695b2001-08-27 22:15:12 +0000125 TEST_PAUSE;
126
127 sprintf(fname, "fcnlt12.%d", getpid());
128 tst_tmpdir();
129}
130
131/*
132 * cleanup()
133 * performs all ONE TIME cleanup for this test at
134 * completion or premature exit
135 */
subrata_modak56207ce2009-03-23 13:35:39 +0000136void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000137{
plars865695b2001-08-27 22:15:12 +0000138
139 unlink(fname);
140 tst_rmdir();
robbiew29081682002-07-11 18:13:05 +0000141
Chris Dearmanec6edca2012-10-17 19:54:01 -0700142}