blob: 468f8768a02356e1a22fa01355115c16323c97d4 [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 * creat05.c
23 *
24 * DESCRIPTION
25 * Testcase to check that creat(2) system call returns EMFILE.
26 *
27 * ALGORITHM
28 * A process creates a temporary test directory, then it should
29 * get the system configured NOFILE parameter (ie. the maximum no. of
30 * open files of a process). This can be done using the getdtablesize()
31 * system call. Once this is determined, the process should attempt
32 * to creat(2) the this no. of files (can be done using a "for" loop).
33 * Then, the process should attempt to creat() another file, which should
34 * fail with EMFILE.
35 *
36 * USAGE
37 * creat05
38 *
39 * HISTORY
40 * 07/2001 Ported by Wayne Boyer
41 *
42 * RESTRICTIONS
43 * None
44 */
45
46#include <stdio.h>
47#include <errno.h>
plars74948ad2002-11-14 16:16:14 +000048#include <sys/types.h>
plars865695b2001-08-27 22:15:12 +000049#include <sys/time.h>
50#include <sys/resource.h>
robbiew2c945242002-11-11 19:01:25 +000051#include <sys/stat.h>
52#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000053#include <linux/limits.h>
54#include <unistd.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080055#include "test.h"
plars865695b2001-08-27 22:15:12 +000056
57char *TCID = "creat05";
58int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000059
60#define MODE 0666
61
62void remove_files(int);
63void setup(void);
64void cleanup(void);
65
plars865695b2001-08-27 22:15:12 +000066int fd, ifile, mypid, first;
subrata_modak87fde722007-11-15 06:53:30 +000067int *buf;
plars865695b2001-08-27 22:15:12 +000068char fname[40];
69
subrata_modak56207ce2009-03-23 13:35:39 +000070int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000071{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020072 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020073 const char *msg;
plars865695b2001-08-27 22:15:12 +000074
Garrett Cooper45e285d2010-11-22 12:19:25 -080075 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080076 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000077 }
78
79 setup();
80
plars865695b2001-08-27 22:15:12 +000081 for (lc = 0; TEST_LOOPING(lc); lc++) {
82
Caspar Zhangd59a6592013-03-07 14:59:12 +080083 /* reset tst_count in case we are looping */
84 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000085
86 TEST(creat(fname, MODE));
87
88 if (TEST_RETURN != -1) {
89 tst_resm(TFAIL, "call succeeded unexpectedly");
90 continue;
91 }
92
plars865695b2001-08-27 22:15:12 +000093 if (TEST_ERRNO == EMFILE) {
94 tst_resm(TPASS, "call failed with expected error - "
95 "EMFILE");
96 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +080097 tst_resm(TFAIL | TTERRNO, "Expected EMFILE");
plars865695b2001-08-27 22:15:12 +000098 }
99
100 remove_files(ifile);
101 }
102 cleanup();
103
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800104 tst_exit();
105}
plars865695b2001-08-27 22:15:12 +0000106
107/*
108 * setup() - performs all ONE TIME setup for this test.
109 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400110void setup(void)
plars865695b2001-08-27 22:15:12 +0000111{
robbiew2c945242002-11-11 19:01:25 +0000112 int max_open;
plars865695b2001-08-27 22:15:12 +0000113
plars865695b2001-08-27 22:15:12 +0000114 tst_sig(NOFORK, DEF_HANDLER, cleanup);
115
plars865695b2001-08-27 22:15:12 +0000116 TEST_PAUSE;
117
118 /* make a temporary directory and cd to it */
119 tst_tmpdir();
120
121 umask(0);
122 mypid = getpid();
123 sprintf(fname, "creat05.%d", mypid);
124 unlink(fname);
125
126 /* create a file to get the first file descriptor available */
127 if ((first = fd = creat(fname, MODE)) == -1) {
128 tst_brkm(TFAIL, cleanup, "Cannot open first file");
129 }
130
131 close(fd);
132 unlink(fname);
133 tst_resm(TINFO, "first file is #%d", fd);
134
135 /* get the maximum number of files that we can open */
136 max_open = getdtablesize();
subrata_modak56207ce2009-03-23 13:35:39 +0000137 /* Allocate memory for stat and ustat structure variables */
Cyril Hrubisd218f342014-09-23 13:14:56 +0200138 if ((buf = malloc(sizeof(int) * max_open - first)) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800139 tst_brkm(TBROK, NULL, "Failed to allocate Memory");
subrata_modak56207ce2009-03-23 13:35:39 +0000140 }
plars865695b2001-08-27 22:15:12 +0000141
142 /* now open as many files as we can up to max_open */
143 for (ifile = first; ifile <= max_open; ifile++) {
144 sprintf(fname, "creat05.%d.%d", ifile, mypid);
145 if ((fd = creat(fname, 0666)) == -1) {
146 tst_resm(TINFO, "could not creat file "
147 "#%d", ifile + 1);
148 if (errno != EMFILE) {
149 remove_files(ifile);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800150 tst_brkm(TBROK | TERRNO, cleanup,
151 "Failed unexpectedly (expected EMFILE)");
plars865695b2001-08-27 22:15:12 +0000152 }
153 break;
154 }
subrata_modak56207ce2009-03-23 13:35:39 +0000155 buf[ifile - first] = fd;
plars865695b2001-08-27 22:15:12 +0000156 }
157}
158
159/*
160 * remove_files - remove the temporary files that were created
161 */
subrata_modak56207ce2009-03-23 13:35:39 +0000162void remove_files(int nfiles)
plars865695b2001-08-27 22:15:12 +0000163{
164 int i;
165
subrata_modak56207ce2009-03-23 13:35:39 +0000166 for (i = first; i < nfiles; i++) {
plars865695b2001-08-27 22:15:12 +0000167 sprintf(fname, "creat05.%d.%d", i, mypid);
subrata_modak56207ce2009-03-23 13:35:39 +0000168 close(buf[i - first]);
plars865695b2001-08-27 22:15:12 +0000169 unlink(fname);
170 }
171}
172
173/*
174 * cleanup() - performs all ONE TIME cleanup for this test at
175 * completion or premature exit.
176 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400177void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000178{
179 /*
180 * print timing stats if that option was specified.
181 * print errno log if that option was specified.
182 */
subrata_modakdad6e1a2007-10-30 10:46:58 +0000183 close(first);
mridge12952b02004-08-25 15:17:15 +0000184
plars865695b2001-08-27 22:15:12 +0000185 /* delete the test directory created in setup() */
186 tst_rmdir();
187
Chris Dearmanec6edca2012-10-17 19:54:01 -0700188}