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; |
| 63 | char fname[40]; |
| 64 | |
| 65 | int exp_enos[] = {EMFILE, 0}; |
| 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 | { |
| 72 | int lc; /* loop counter */ |
| 73 | char *msg; /* message returned from parse_opts */ |
| 74 | |
| 75 | /* parse standard options */ |
| 76 | if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){ |
| 77 | tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); |
| 78 | } |
| 79 | |
| 80 | setup(); |
| 81 | |
| 82 | TEST_EXP_ENOS(exp_enos); |
| 83 | |
| 84 | /* check looping state if -i option given */ |
| 85 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 86 | /* reset Tst_count in case we are looping */ |
| 87 | Tst_count = 0; |
| 88 | |
| 89 | TEST(open(fname, O_RDWR | O_CREAT)); |
| 90 | |
| 91 | if (TEST_RETURN != -1) { |
| 92 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | TEST_ERROR_LOG(TEST_ERRNO); |
| 97 | |
| 98 | if (TEST_ERRNO != EMFILE) { |
| 99 | tst_resm(TFAIL, "Expected EMFILE, got %d", TEST_ERRNO); |
| 100 | } else { |
| 101 | tst_resm(TPASS, "call returned expected EMFILE error"); |
| 102 | } |
| 103 | } |
| 104 | cleanup(); |
| 105 | |
| 106 | /*NOTREACHED*/ |
robbiew | a775110 | 2003-03-27 16:38:59 +0000 | [diff] [blame] | 107 | return(0); |
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 | */ |
| 113 | void |
| 114 | setup() |
| 115 | { |
| 116 | /* capture signals */ |
| 117 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 118 | |
| 119 | /* Pause if that option was specified */ |
| 120 | TEST_PAUSE; |
| 121 | |
| 122 | /* make a temporary directory and cd to it */ |
| 123 | tst_tmpdir(); |
| 124 | |
| 125 | mypid = getpid(); |
| 126 | nfile = getdtablesize(); |
| 127 | sprintf(fname, "open04.%d", mypid); |
| 128 | |
| 129 | if ((first = fd = open(fname, O_RDWR | O_CREAT)) == -1) { |
| 130 | tst_brkm(TBROK, cleanup, "Cannot open first file"); |
| 131 | } |
| 132 | |
| 133 | close(fd); |
| 134 | unlink(fname); |
| 135 | |
| 136 | for (ifile = first; ifile <= nfile; ifile++) { |
| 137 | sprintf(fname, "open04.%d.%d", ifile, mypid); |
| 138 | if ((fd = open(fname, O_RDWR | O_CREAT)) == -1) { |
| 139 | if (errno != EMFILE) { |
| 140 | tst_brkm(TBROK, cleanup, "Expected EMFILE got " |
| 141 | "%d", errno); |
| 142 | } |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 150 | * completion or premature exit. |
| 151 | */ |
| 152 | void |
| 153 | cleanup() |
| 154 | { |
| 155 | /* |
| 156 | * print timing stats if that option was specified. |
| 157 | * print errno log if that option was specified. |
| 158 | */ |
mridge | eb93f64 | 2004-08-25 15:51:51 +0000 | [diff] [blame^] | 159 | close(first); |
| 160 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | TEST_CLEANUP; |
| 162 | |
| 163 | for (ifile = first; ifile < nfile; ifile++) { |
| 164 | sprintf(fname, "open04.%d.%d", ifile, mypid); |
| 165 | unlink(fname); |
| 166 | } |
| 167 | |
| 168 | /* delete the test directory created in setup() */ |
| 169 | tst_rmdir(); |
| 170 | |
| 171 | /* exit with return code appropriate for results */ |
| 172 | tst_exit(); |
| 173 | } |