blob: 4f634aeb7e6a2d41cff07ea30d53d72d90692a96 [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;
59extern int Tst_count;
60
61int fd, ifile, mypid, first;
62int nfile;
subrata_modak3fddbf62007-11-14 15:27:56 +000063int *buf;
plars865695b2001-08-27 22:15:12 +000064char fname[40];
65
subrata_modak56207ce2009-03-23 13:35:39 +000066int exp_enos[] = { EMFILE, 0 };
plars865695b2001-08-27 22:15:12 +000067
68void setup(void);
69void cleanup(void);
70
plars74948ad2002-11-14 16:16:14 +000071int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000072{
subrata_modak56207ce2009-03-23 13:35:39 +000073 int lc; /* loop counter */
74 char *msg; /* message returned from parse_opts */
plars865695b2001-08-27 22:15:12 +000075
76 /* parse standard options */
Garrett Cooper45e285d2010-11-22 12:19:25 -080077 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
plars865695b2001-08-27 22:15:12 +000078 tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
79 }
80
81 setup();
82
83 TEST_EXP_ENOS(exp_enos);
84
85 /* check looping state if -i option given */
86 for (lc = 0; TEST_LOOPING(lc); lc++) {
87 /* reset Tst_count in case we are looping */
88 Tst_count = 0;
89
subrata_modake4cf63d2008-01-21 11:16:15 +000090 TEST(open(fname, O_RDWR | O_CREAT, 0777));
plars865695b2001-08-27 22:15:12 +000091
92 if (TEST_RETURN != -1) {
93 tst_resm(TFAIL, "call succeeded unexpectedly");
94 continue;
95 }
96
97 TEST_ERROR_LOG(TEST_ERRNO);
98
99 if (TEST_ERRNO != EMFILE) {
100 tst_resm(TFAIL, "Expected EMFILE, got %d", TEST_ERRNO);
101 } else {
102 tst_resm(TPASS, "call returned expected EMFILE error");
103 }
104 }
subrata_modakdad6e1a2007-10-30 10:46:58 +0000105 close(first);
106 close(fd);
plars865695b2001-08-27 22:15:12 +0000107 cleanup();
108
subrata_modak56207ce2009-03-23 13:35:39 +0000109 /*NOTREACHED*/ return 0;
plars865695b2001-08-27 22:15:12 +0000110}
111
112/*
113 * setup() - performs all ONE TIME setup for this test.
114 */
subrata_modak56207ce2009-03-23 13:35:39 +0000115void setup()
plars865695b2001-08-27 22:15:12 +0000116{
117 /* capture signals */
118 tst_sig(NOFORK, DEF_HANDLER, cleanup);
119
120 /* Pause if that option was specified */
121 TEST_PAUSE;
122
123 /* make a temporary directory and cd to it */
124 tst_tmpdir();
125
126 mypid = getpid();
127 nfile = getdtablesize();
128 sprintf(fname, "open04.%d", mypid);
129
subrata_modake4cf63d2008-01-21 11:16:15 +0000130 if ((first = fd = open(fname, O_RDWR | O_CREAT, 0777)) == -1) {
plars865695b2001-08-27 22:15:12 +0000131 tst_brkm(TBROK, cleanup, "Cannot open first file");
132 }
133
subrata_modak4bb656a2009-02-26 12:02:09 +0000134 close(fd);
subrata_modakdad6e1a2007-10-30 10:46:58 +0000135 close(first);
plars865695b2001-08-27 22:15:12 +0000136 unlink(fname);
137
subrata_modak56207ce2009-03-23 13:35:39 +0000138 /* Allocate memory for stat and ustat structure variables */
139 if ((buf = (int *)malloc(sizeof(int) * nfile - first)) == NULL) {
subrata_modak3fddbf62007-11-14 15:27:56 +0000140 tst_brkm(TBROK, tst_exit, "Failed to allocate Memory");
141 }
142
plars865695b2001-08-27 22:15:12 +0000143 for (ifile = first; ifile <= nfile; ifile++) {
144 sprintf(fname, "open04.%d.%d", ifile, mypid);
subrata_modake4cf63d2008-01-21 11:16:15 +0000145 if ((fd = open(fname, O_RDWR | O_CREAT, 0777)) == -1) {
plars865695b2001-08-27 22:15:12 +0000146 if (errno != EMFILE) {
147 tst_brkm(TBROK, cleanup, "Expected EMFILE got "
148 "%d", errno);
149 }
150 break;
151 }
subrata_modak56207ce2009-03-23 13:35:39 +0000152 buf[ifile - first] = fd;
plars865695b2001-08-27 22:15:12 +0000153 }
154}
155
156/*
157 * cleanup() - performs all ONE TIME cleanup for this test at
158 * completion or premature exit.
159 */
subrata_modak56207ce2009-03-23 13:35:39 +0000160void cleanup()
plars865695b2001-08-27 22:15:12 +0000161{
162 /*
163 * print timing stats if that option was specified.
164 * print errno log if that option was specified.
165 */
subrata_modak56207ce2009-03-23 13:35:39 +0000166 close(first);
mridgeeb93f642004-08-25 15:51:51 +0000167
plars865695b2001-08-27 22:15:12 +0000168 TEST_CLEANUP;
169
170 for (ifile = first; ifile < nfile; ifile++) {
171 sprintf(fname, "open04.%d.%d", ifile, mypid);
subrata_modak56207ce2009-03-23 13:35:39 +0000172 close(buf[ifile - first]);
plars865695b2001-08-27 22:15:12 +0000173 unlink(fname);
174 }
175
176 /* delete the test directory created in setup() */
177 tst_rmdir();
178
179 /* exit with return code appropriate for results */
180 tst_exit();
181}