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