blob: ad6d992f593a91158d0f852beef2be65c7b289c3 [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"
47#include "usctest.h"
plars865695b2001-08-27 22:15:12 +000048
49char *TCID = "fcntl12";
50int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000051
52int fail;
53char fname[20];
54void setup(void);
55void cleanup(void);
56
robbiew4cf80962003-03-25 21:53:41 +000057int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000058{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020059 int lc;
60 char *msg;
plars865695b2001-08-27 22:15:12 +000061
62 pid_t pid;
63 int fd, i, status, max_files;
subrata_modakbdbaec52009-02-26 12:14:51 +000064
Garrett Cooper45e285d2010-11-22 12:19:25 -080065 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080066 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000067 }
68
69 setup();
70
71 /* check for looping state if -i option is given */
72 for (lc = 0; TEST_LOOPING(lc); lc++) {
73
74 /* reset Tst_count in case we are looping */
75 Tst_count = 0;
76
mridgedb639212005-01-04 21:04:11 +000077/* //block1: */
plars865695b2001-08-27 22:15:12 +000078 tst_resm(TINFO, "Enter block 1");
79 tst_resm(TINFO, "Test for errno EMFILE");
80 fail = 0;
81
robbiewd34d5812005-07-11 22:28:09 +000082 pid = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +000083 if (pid < 0) {
84 tst_resm(TFAIL, "Fork failed");
85 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +080086 } else if (pid == 0) { /* child */
plars865695b2001-08-27 22:15:12 +000087 max_files = getdtablesize();
subrata_modak56207ce2009-03-23 13:35:39 +000088 for (i = 0; i < max_files; i++) {
plars865695b2001-08-27 22:15:12 +000089 if ((fd = open(fname, O_CREAT | O_RDONLY,
subrata_modak56207ce2009-03-23 13:35:39 +000090 0444)) == -1) {
plars865695b2001-08-27 22:15:12 +000091 break;
92 }
93 }
94
95 if (fcntl(1, F_DUPFD, 1) != -1) {
96 tst_resm(TFAIL, "fcntl failed to FAIL");
97 exit(1);
98 } else if (errno != EMFILE) {
99 tst_resm(TFAIL, "Expected EMFILE got %d",
100 errno);
101 exit(1);
102 }
103 exit(0);
104 }
105 waitpid(pid, &status, 0);
106 if (WEXITSTATUS(status) == 0) {
107 tst_resm(TINFO, "block 1 PASSED");
108 } else {
109 tst_resm(TINFO, "block 1 FAILED");
110 }
111 tst_resm(TINFO, "Exit block 1");
112 }
113 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800114 tst_exit();
plars865695b2001-08-27 22:15:12 +0000115}
116
117/*
118 * setup()
119 * performs all ONE TIME setup for this test
120 */
subrata_modak56207ce2009-03-23 13:35:39 +0000121void setup(void)
plars865695b2001-08-27 22:15:12 +0000122{
Garrett Cooper2c282152010-12-16 00:55:50 -0800123
plars865695b2001-08-27 22:15:12 +0000124 tst_sig(FORK, DEF_HANDLER, cleanup);
125
plars865695b2001-08-27 22:15:12 +0000126 TEST_PAUSE;
127
128 sprintf(fname, "fcnlt12.%d", getpid());
129 tst_tmpdir();
130}
131
132/*
133 * cleanup()
134 * performs all ONE TIME cleanup for this test at
135 * completion or premature exit
136 */
subrata_modak56207ce2009-03-23 13:35:39 +0000137void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000138{
139 /*
140 * print timing stats if that option was specified.
141 * print errno log if that option was specified.
142 */
143 TEST_CLEANUP;
144
145 unlink(fname);
146 tst_rmdir();
robbiew29081682002-07-11 18:13:05 +0000147
Chris Dearmanec6edca2012-10-17 19:54:01 -0700148}