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 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | */ |
Wanlong Gao | 68f73c3 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 31 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <errno.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <unistd.h> |
| 36 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | |
| 38 | char *TCID = "open04"; |
| 39 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | |
Wanlong Gao | 68f73c3 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 41 | static int fd, ifile, mypid, first; |
| 42 | static int nfile; |
| 43 | static int *buf; |
| 44 | static char fname[40]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | |
Wanlong Gao | 68f73c3 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 46 | static void setup(void); |
| 47 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 49 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 51 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 52 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
Wanlong Gao | 68f73c3 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 54 | msg = parse_opts(ac, av, NULL, NULL); |
| 55 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 56 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
| 58 | setup(); |
| 59 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 61 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
subrata_modak | e4cf63d | 2008-01-21 11:16:15 +0000 | [diff] [blame] | 63 | TEST(open(fname, O_RDWR | O_CREAT, 0777)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
| 65 | if (TEST_RETURN != -1) { |
| 66 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 67 | continue; |
| 68 | } |
| 69 | |
Wanlong Gao | 68f73c3 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 70 | if (TEST_ERRNO != EMFILE) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | tst_resm(TFAIL, "Expected EMFILE, got %d", TEST_ERRNO); |
Wanlong Gao | 68f73c3 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 72 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | tst_resm(TPASS, "call returned expected EMFILE error"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | } |
Wanlong Gao | 68f73c3 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 75 | |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 76 | close(first); |
| 77 | close(fd); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 79 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Wanlong Gao | 68f73c3 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 82 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 85 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | 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 Gao | 68f73c3 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 95 | first = fd = open(fname, O_RDWR | O_CREAT, 0777); |
| 96 | if (first == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | tst_brkm(TBROK, cleanup, "Cannot open first file"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 99 | close(fd); |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 100 | close(first); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | unlink(fname); |
| 102 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | /* Allocate memory for stat and ustat structure variables */ |
Wanlong Gao | 68f73c3 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 104 | buf = malloc(sizeof(int) * nfile - first); |
| 105 | if (buf == NULL) |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 106 | tst_brkm(TBROK, NULL, "Failed to allocate Memory"); |
subrata_modak | 3fddbf6 | 2007-11-14 15:27:56 +0000 | [diff] [blame] | 107 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | for (ifile = first; ifile <= nfile; ifile++) { |
| 109 | sprintf(fname, "open04.%d.%d", ifile, mypid); |
Wanlong Gao | 68f73c3 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 110 | fd = open(fname, O_RDWR | O_CREAT, 0777); |
| 111 | if (fd == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | if (errno != EMFILE) { |
| 113 | tst_brkm(TBROK, cleanup, "Expected EMFILE got " |
| 114 | "%d", errno); |
| 115 | } |
| 116 | break; |
| 117 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 118 | buf[ifile - first] = fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Wanlong Gao | 68f73c3 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 122 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | close(first); |
mridge | eb93f64 | 2004-08-25 15:51:51 +0000 | [diff] [blame] | 125 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | for (ifile = first; ifile < nfile; ifile++) { |
| 127 | sprintf(fname, "open04.%d.%d", ifile, mypid); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 128 | close(buf[ifile - first]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | unlink(fname); |
| 130 | } |
| 131 | |
| 132 | /* delete the test directory created in setup() */ |
| 133 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 134 | } |