blob: 8e6b398350ccc0561b72fb7190c3b7e4f5faa23e [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * NAME
22 * open04.c
23 *
24 * DESCRIPTION
25 * Testcase to check that open(2) sets EMFILE if a process opens files
26 * more than its descriptor size
27 *
28 * ALGORITHM
29 * First get the file descriptor table size which is set for a process.
30 * Use open(2) for creating files till the descriptor table becomes full.
31 * These open(2)s should succeed. Finally use open(2) to open another
32 * file. This attempt should fail with EMFILE.
33 *
34 * USAGE: <for command-line>
35 * open04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
36 * where, -c n : Run n copies concurrently.
37 * -e : Turn on errno logging.
38 * -i n : Execute test n times.
39 * -I x : Execute test for x seconds.
40 * -P x : Pause for x seconds between iterations.
41 * -t : Turn on syscall timing.
42 *
43 * HISTORY
44 * 07/2001 Ported by Wayne Boyer
45 *
46 * RESTRICTIONS
47 * NONE
48 *
49 */
50#include <stdio.h>
51#include <errno.h>
52#include <fcntl.h>
53#include <unistd.h>
54#include "test.h"
55#include "usctest.h"
56
57char *TCID = "open04";
58int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000059
60int fd, ifile, mypid, first;
61int nfile;
subrata_modak3fddbf62007-11-14 15:27:56 +000062int *buf;
plars865695b2001-08-27 22:15:12 +000063char fname[40];
64
subrata_modak56207ce2009-03-23 13:35:39 +000065int exp_enos[] = { EMFILE, 0 };
plars865695b2001-08-27 22:15:12 +000066
67void setup(void);
68void cleanup(void);
69
plars74948ad2002-11-14 16:16:14 +000070int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000071{
subrata_modak56207ce2009-03-23 13:35:39 +000072 int lc; /* loop counter */
73 char *msg; /* message returned from parse_opts */
plars865695b2001-08-27 22:15:12 +000074
75 /* parse standard options */
Garrett Cooper45e285d2010-11-22 12:19:25 -080076 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080077 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000078 }
79
80 setup();
81
82 TEST_EXP_ENOS(exp_enos);
83
plars865695b2001-08-27 22:15:12 +000084 for (lc = 0; TEST_LOOPING(lc); lc++) {
85 /* reset Tst_count in case we are looping */
86 Tst_count = 0;
87
subrata_modake4cf63d2008-01-21 11:16:15 +000088 TEST(open(fname, O_RDWR | O_CREAT, 0777));
plars865695b2001-08-27 22:15:12 +000089
90 if (TEST_RETURN != -1) {
91 tst_resm(TFAIL, "call succeeded unexpectedly");
92 continue;
93 }
94
95 TEST_ERROR_LOG(TEST_ERRNO);
96
97 if (TEST_ERRNO != EMFILE) {
98 tst_resm(TFAIL, "Expected EMFILE, got %d", TEST_ERRNO);
99 } else {
100 tst_resm(TPASS, "call returned expected EMFILE error");
101 }
102 }
subrata_modakdad6e1a2007-10-30 10:46:58 +0000103 close(first);
104 close(fd);
plars865695b2001-08-27 22:15:12 +0000105 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800106 tst_exit();
plars865695b2001-08-27 22:15:12 +0000107
plars865695b2001-08-27 22:15:12 +0000108}
109
110/*
111 * setup() - performs all ONE TIME setup for this test.
112 */
subrata_modak56207ce2009-03-23 13:35:39 +0000113void setup()
plars865695b2001-08-27 22:15:12 +0000114{
Garrett Cooper2c282152010-12-16 00:55:50 -0800115
plars865695b2001-08-27 22:15:12 +0000116 tst_sig(NOFORK, DEF_HANDLER, cleanup);
117
plars865695b2001-08-27 22:15:12 +0000118 TEST_PAUSE;
119
120 /* make a temporary directory and cd to it */
121 tst_tmpdir();
122
123 mypid = getpid();
124 nfile = getdtablesize();
125 sprintf(fname, "open04.%d", mypid);
126
subrata_modake4cf63d2008-01-21 11:16:15 +0000127 if ((first = fd = open(fname, O_RDWR | O_CREAT, 0777)) == -1) {
plars865695b2001-08-27 22:15:12 +0000128 tst_brkm(TBROK, cleanup, "Cannot open first file");
129 }
130
subrata_modak4bb656a2009-02-26 12:02:09 +0000131 close(fd);
subrata_modakdad6e1a2007-10-30 10:46:58 +0000132 close(first);
plars865695b2001-08-27 22:15:12 +0000133 unlink(fname);
134
subrata_modak56207ce2009-03-23 13:35:39 +0000135 /* Allocate memory for stat and ustat structure variables */
136 if ((buf = (int *)malloc(sizeof(int) * nfile - first)) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800137 tst_brkm(TBROK, NULL, "Failed to allocate Memory");
subrata_modak3fddbf62007-11-14 15:27:56 +0000138 }
139
plars865695b2001-08-27 22:15:12 +0000140 for (ifile = first; ifile <= nfile; ifile++) {
141 sprintf(fname, "open04.%d.%d", ifile, mypid);
subrata_modake4cf63d2008-01-21 11:16:15 +0000142 if ((fd = open(fname, O_RDWR | O_CREAT, 0777)) == -1) {
plars865695b2001-08-27 22:15:12 +0000143 if (errno != EMFILE) {
144 tst_brkm(TBROK, cleanup, "Expected EMFILE got "
145 "%d", errno);
146 }
147 break;
148 }
subrata_modak56207ce2009-03-23 13:35:39 +0000149 buf[ifile - first] = fd;
plars865695b2001-08-27 22:15:12 +0000150 }
151}
152
153/*
154 * cleanup() - performs all ONE TIME cleanup for this test at
155 * completion or premature exit.
156 */
subrata_modak56207ce2009-03-23 13:35:39 +0000157void cleanup()
plars865695b2001-08-27 22:15:12 +0000158{
159 /*
160 * print timing stats if that option was specified.
161 * print errno log if that option was specified.
162 */
subrata_modak56207ce2009-03-23 13:35:39 +0000163 close(first);
mridgeeb93f642004-08-25 15:51:51 +0000164
plars865695b2001-08-27 22:15:12 +0000165 TEST_CLEANUP;
166
167 for (ifile = first; ifile < nfile; ifile++) {
168 sprintf(fname, "open04.%d.%d", ifile, mypid);
subrata_modak56207ce2009-03-23 13:35:39 +0000169 close(buf[ifile - first]);
plars865695b2001-08-27 22:15:12 +0000170 unlink(fname);
171 }
172
173 /* delete the test directory created in setup() */
174 tst_rmdir();
175
Garrett Cooper2c282152010-12-16 00:55:50 -0800176}