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 |
| 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 | |
| 57 | char *TCID = "open04"; |
| 58 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
| 60 | int fd, ifile, mypid, first; |
| 61 | int nfile; |
subrata_modak | 3fddbf6 | 2007-11-14 15:27:56 +0000 | [diff] [blame] | 62 | int *buf; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | char fname[40]; |
| 64 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 65 | int exp_enos[] = { EMFILE, 0 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
| 67 | void setup(void); |
| 68 | void cleanup(void); |
| 69 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 70 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 72 | int lc; /* loop counter */ |
| 73 | char *msg; /* message returned from parse_opts */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | |
| 75 | /* parse standard options */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 76 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 77 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | setup(); |
| 81 | |
| 82 | TEST_EXP_ENOS(exp_enos); |
| 83 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 85 | /* reset Tst_count in case we are looping */ |
| 86 | Tst_count = 0; |
| 87 | |
subrata_modak | e4cf63d | 2008-01-21 11:16:15 +0000 | [diff] [blame] | 88 | TEST(open(fname, O_RDWR | O_CREAT, 0777)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
| 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_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 103 | close(first); |
| 104 | close(fd); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 106 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* |
| 111 | * setup() - performs all ONE TIME setup for this test. |
| 112 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 115 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 117 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | 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_modak | e4cf63d | 2008-01-21 11:16:15 +0000 | [diff] [blame] | 127 | if ((first = fd = open(fname, O_RDWR | O_CREAT, 0777)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | tst_brkm(TBROK, cleanup, "Cannot open first file"); |
| 129 | } |
| 130 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 131 | close(fd); |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 132 | close(first); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | unlink(fname); |
| 134 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 135 | /* Allocate memory for stat and ustat structure variables */ |
| 136 | if ((buf = (int *)malloc(sizeof(int) * nfile - first)) == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 137 | tst_brkm(TBROK, NULL, "Failed to allocate Memory"); |
subrata_modak | 3fddbf6 | 2007-11-14 15:27:56 +0000 | [diff] [blame] | 138 | } |
| 139 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | for (ifile = first; ifile <= nfile; ifile++) { |
| 141 | sprintf(fname, "open04.%d.%d", ifile, mypid); |
subrata_modak | e4cf63d | 2008-01-21 11:16:15 +0000 | [diff] [blame] | 142 | if ((fd = open(fname, O_RDWR | O_CREAT, 0777)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | if (errno != EMFILE) { |
| 144 | tst_brkm(TBROK, cleanup, "Expected EMFILE got " |
| 145 | "%d", errno); |
| 146 | } |
| 147 | break; |
| 148 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 149 | buf[ifile - first] = fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 155 | * completion or premature exit. |
| 156 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 157 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | { |
| 159 | /* |
| 160 | * print timing stats if that option was specified. |
| 161 | * print errno log if that option was specified. |
| 162 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | close(first); |
mridge | eb93f64 | 2004-08-25 15:51:51 +0000 | [diff] [blame] | 164 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | TEST_CLEANUP; |
| 166 | |
| 167 | for (ifile = first; ifile < nfile; ifile++) { |
| 168 | sprintf(fname, "open04.%d.%d", ifile, mypid); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | close(buf[ifile - first]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | unlink(fname); |
| 171 | } |
| 172 | |
| 173 | /* delete the test directory created in setup() */ |
| 174 | tst_rmdir(); |
| 175 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 176 | } |