blob: 35c47d69a042e799ddb3fd2f0247e8326d2f5f37 [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/*
plars865695b2001-08-27 22:15:12 +000021 * DESCRIPTION
22 * Testcase to check that open(2) sets EMFILE if a process opens files
23 * more than its descriptor size
24 *
25 * ALGORITHM
26 * First get the file descriptor table size which is set for a process.
27 * Use open(2) for creating files till the descriptor table becomes full.
28 * These open(2)s should succeed. Finally use open(2) to open another
29 * file. This attempt should fail with EMFILE.
plars865695b2001-08-27 22:15:12 +000030 */
Wanlong Gao68f73c32013-02-04 17:11:18 +080031
plars865695b2001-08-27 22:15:12 +000032#include <stdio.h>
33#include <errno.h>
34#include <fcntl.h>
35#include <unistd.h>
36#include "test.h"
plars865695b2001-08-27 22:15:12 +000037
38char *TCID = "open04";
39int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000040
Wanlong Gao68f73c32013-02-04 17:11:18 +080041static int fd, ifile, mypid, first;
42static int nfile;
43static int *buf;
44static char fname[40];
plars865695b2001-08-27 22:15:12 +000045
Wanlong Gao68f73c32013-02-04 17:11:18 +080046static void setup(void);
47static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000048
plars74948ad2002-11-14 16:16:14 +000049int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000050{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020051 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020052 const char *msg;
plars865695b2001-08-27 22:15:12 +000053
Wanlong Gao68f73c32013-02-04 17:11:18 +080054 msg = parse_opts(ac, av, NULL, NULL);
55 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080056 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000057
58 setup();
59
plars865695b2001-08-27 22:15:12 +000060 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080061 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000062
subrata_modake4cf63d2008-01-21 11:16:15 +000063 TEST(open(fname, O_RDWR | O_CREAT, 0777));
plars865695b2001-08-27 22:15:12 +000064
65 if (TEST_RETURN != -1) {
66 tst_resm(TFAIL, "call succeeded unexpectedly");
67 continue;
68 }
69
Wanlong Gao68f73c32013-02-04 17:11:18 +080070 if (TEST_ERRNO != EMFILE)
plars865695b2001-08-27 22:15:12 +000071 tst_resm(TFAIL, "Expected EMFILE, got %d", TEST_ERRNO);
Wanlong Gao68f73c32013-02-04 17:11:18 +080072 else
plars865695b2001-08-27 22:15:12 +000073 tst_resm(TPASS, "call returned expected EMFILE error");
plars865695b2001-08-27 22:15:12 +000074 }
Wanlong Gao68f73c32013-02-04 17:11:18 +080075
subrata_modakdad6e1a2007-10-30 10:46:58 +000076 close(first);
77 close(fd);
plars865695b2001-08-27 22:15:12 +000078 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080079 tst_exit();
plars865695b2001-08-27 22:15:12 +000080}
81
Wanlong Gao68f73c32013-02-04 17:11:18 +080082static void setup(void)
plars865695b2001-08-27 22:15:12 +000083{
plars865695b2001-08-27 22:15:12 +000084 tst_sig(NOFORK, DEF_HANDLER, cleanup);
85
plars865695b2001-08-27 22:15:12 +000086 TEST_PAUSE;
87
88 /* make a temporary directory and cd to it */
89 tst_tmpdir();
90
91 mypid = getpid();
92 nfile = getdtablesize();
93 sprintf(fname, "open04.%d", mypid);
94
Wanlong Gao68f73c32013-02-04 17:11:18 +080095 first = fd = open(fname, O_RDWR | O_CREAT, 0777);
96 if (first == -1)
plars865695b2001-08-27 22:15:12 +000097 tst_brkm(TBROK, cleanup, "Cannot open first file");
plars865695b2001-08-27 22:15:12 +000098
subrata_modak4bb656a2009-02-26 12:02:09 +000099 close(fd);
subrata_modakdad6e1a2007-10-30 10:46:58 +0000100 close(first);
plars865695b2001-08-27 22:15:12 +0000101 unlink(fname);
102
subrata_modak56207ce2009-03-23 13:35:39 +0000103 /* Allocate memory for stat and ustat structure variables */
Wanlong Gao68f73c32013-02-04 17:11:18 +0800104 buf = malloc(sizeof(int) * nfile - first);
105 if (buf == NULL)
Garrett Cooper53740502010-12-16 00:04:01 -0800106 tst_brkm(TBROK, NULL, "Failed to allocate Memory");
subrata_modak3fddbf62007-11-14 15:27:56 +0000107
plars865695b2001-08-27 22:15:12 +0000108 for (ifile = first; ifile <= nfile; ifile++) {
109 sprintf(fname, "open04.%d.%d", ifile, mypid);
Wanlong Gao68f73c32013-02-04 17:11:18 +0800110 fd = open(fname, O_RDWR | O_CREAT, 0777);
111 if (fd == -1) {
plars865695b2001-08-27 22:15:12 +0000112 if (errno != EMFILE) {
113 tst_brkm(TBROK, cleanup, "Expected EMFILE got "
114 "%d", errno);
115 }
116 break;
117 }
subrata_modak56207ce2009-03-23 13:35:39 +0000118 buf[ifile - first] = fd;
plars865695b2001-08-27 22:15:12 +0000119 }
120}
121
Wanlong Gao68f73c32013-02-04 17:11:18 +0800122static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000123{
subrata_modak56207ce2009-03-23 13:35:39 +0000124 close(first);
mridgeeb93f642004-08-25 15:51:51 +0000125
plars865695b2001-08-27 22:15:12 +0000126 for (ifile = first; ifile < nfile; ifile++) {
127 sprintf(fname, "open04.%d.%d", ifile, mypid);
subrata_modak56207ce2009-03-23 13:35:39 +0000128 close(buf[ifile - first]);
plars865695b2001-08-27 22:15:12 +0000129 unlink(fname);
130 }
131
132 /* delete the test directory created in setup() */
133 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700134}