plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 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> |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 48 | #include <sys/types.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | #include <sys/time.h> |
| 50 | #include <sys/resource.h> |
robbiew | 2c94524 | 2002-11-11 19:01:25 +0000 | [diff] [blame] | 51 | #include <sys/stat.h> |
| 52 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | #include <linux/limits.h> |
| 54 | #include <unistd.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 55 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
| 57 | char *TCID = "creat05"; |
| 58 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
| 60 | #define MODE 0666 |
| 61 | |
| 62 | void remove_files(int); |
| 63 | void setup(void); |
| 64 | void cleanup(void); |
| 65 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | int fd, ifile, mypid, first; |
subrata_modak | 87fde72 | 2007-11-15 06:53:30 +0000 | [diff] [blame] | 67 | int *buf; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | char fname[40]; |
| 69 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 70 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 72 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 73 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 75 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 76 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | setup(); |
| 80 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 82 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 83 | /* reset tst_count in case we are looping */ |
| 84 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | |
| 86 | TEST(creat(fname, MODE)); |
| 87 | |
| 88 | if (TEST_RETURN != -1) { |
| 89 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 90 | continue; |
| 91 | } |
| 92 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | if (TEST_ERRNO == EMFILE) { |
| 94 | tst_resm(TPASS, "call failed with expected error - " |
| 95 | "EMFILE"); |
| 96 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 97 | tst_resm(TFAIL | TTERRNO, "Expected EMFILE"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | remove_files(ifile); |
| 101 | } |
| 102 | cleanup(); |
| 103 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 104 | tst_exit(); |
| 105 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * setup() - performs all ONE TIME setup for this test. |
| 109 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 110 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | { |
robbiew | 2c94524 | 2002-11-11 19:01:25 +0000 | [diff] [blame] | 112 | int max_open; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 115 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 137 | /* Allocate memory for stat and ustat structure variables */ |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 138 | if ((buf = malloc(sizeof(int) * max_open - first)) == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 139 | tst_brkm(TBROK, NULL, "Failed to allocate Memory"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 150 | tst_brkm(TBROK | TERRNO, cleanup, |
| 151 | "Failed unexpectedly (expected EMFILE)"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | } |
| 153 | break; |
| 154 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | buf[ifile - first] = fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * remove_files - remove the temporary files that were created |
| 161 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | void remove_files(int nfiles) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | { |
| 164 | int i; |
| 165 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 166 | for (i = first; i < nfiles; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | sprintf(fname, "creat05.%d.%d", i, mypid); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 168 | close(buf[i - first]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | unlink(fname); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 175 | * completion or premature exit. |
| 176 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 177 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | { |
| 179 | /* |
| 180 | * print timing stats if that option was specified. |
| 181 | * print errno log if that option was specified. |
| 182 | */ |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 183 | close(first); |
mridge | 12952b0 | 2004-08-25 15:17:15 +0000 | [diff] [blame] | 184 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | /* delete the test directory created in setup() */ |
| 186 | tst_rmdir(); |
| 187 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 188 | } |