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 | * fcntl12.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to test that fcntl() sets EMFILE for F_DUPFD command. |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * Get the size of the descriptor table of a process, by calling the |
| 29 | * getdtablesize() system call. Then attempt to use the F_DUPFD command |
| 30 | * for fcntl(), which should fail with EMFILE. |
| 31 | * |
| 32 | * USAGE |
| 33 | * fcntl12 |
| 34 | * |
| 35 | * HISTORY |
| 36 | * 07/2001 Ported by Wayne Boyer |
| 37 | * |
| 38 | * RESTRICTIONS |
| 39 | * NONE |
| 40 | */ |
| 41 | |
| 42 | #include <fcntl.h> |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 43 | #include <sys/types.h> |
| 44 | #include <sys/wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | #include <errno.h> |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 46 | #include "test.h" |
| 47 | #include "usctest.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
| 49 | char *TCID = "fcntl12"; |
| 50 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
| 52 | int fail; |
| 53 | char fname[20]; |
| 54 | void setup(void); |
| 55 | void cleanup(void); |
| 56 | |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 57 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 59 | int lc; |
| 60 | char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
| 62 | pid_t pid; |
| 63 | int fd, i, status, max_files; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 64 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 65 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 66 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | setup(); |
| 70 | |
| 71 | /* check for looping state if -i option is given */ |
| 72 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 73 | |
| 74 | /* reset Tst_count in case we are looping */ |
| 75 | Tst_count = 0; |
| 76 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 77 | /* //block1: */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | tst_resm(TINFO, "Enter block 1"); |
| 79 | tst_resm(TINFO, "Test for errno EMFILE"); |
| 80 | fail = 0; |
| 81 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 82 | pid = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | if (pid < 0) { |
| 84 | tst_resm(TFAIL, "Fork failed"); |
| 85 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 86 | } else if (pid == 0) { /* child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | max_files = getdtablesize(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | for (i = 0; i < max_files; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | if ((fd = open(fname, O_CREAT | O_RDONLY, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | 0444)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (fcntl(1, F_DUPFD, 1) != -1) { |
| 96 | tst_resm(TFAIL, "fcntl failed to FAIL"); |
| 97 | exit(1); |
| 98 | } else if (errno != EMFILE) { |
| 99 | tst_resm(TFAIL, "Expected EMFILE got %d", |
| 100 | errno); |
| 101 | exit(1); |
| 102 | } |
| 103 | exit(0); |
| 104 | } |
| 105 | waitpid(pid, &status, 0); |
| 106 | if (WEXITSTATUS(status) == 0) { |
| 107 | tst_resm(TINFO, "block 1 PASSED"); |
| 108 | } else { |
| 109 | tst_resm(TINFO, "block 1 FAILED"); |
| 110 | } |
| 111 | tst_resm(TINFO, "Exit block 1"); |
| 112 | } |
| 113 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 114 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* |
| 118 | * setup() |
| 119 | * performs all ONE TIME setup for this test |
| 120 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 121 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 125 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | TEST_PAUSE; |
| 127 | |
| 128 | sprintf(fname, "fcnlt12.%d", getpid()); |
| 129 | tst_tmpdir(); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * cleanup() |
| 134 | * performs all ONE TIME cleanup for this test at |
| 135 | * completion or premature exit |
| 136 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 137 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | { |
| 139 | /* |
| 140 | * print timing stats if that option was specified. |
| 141 | * print errno log if that option was specified. |
| 142 | */ |
| 143 | TEST_CLEANUP; |
| 144 | |
| 145 | unlink(fname); |
| 146 | tst_rmdir(); |
robbiew | 2908168 | 2002-07-11 18:13:05 +0000 | [diff] [blame] | 147 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 148 | } |