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 | * close02.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Check that an invalid file descriptor returns EBADF |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * loop if that option is specified |
| 29 | * call close using the TEST macro and passing in an invalid fd |
| 30 | * if the call succeedes |
| 31 | * issue a FAIL message |
| 32 | * else |
| 33 | * log the errno |
| 34 | * if the errno == EBADF |
| 35 | * issue a PASS message |
| 36 | * else |
| 37 | * issue a FAIL message |
| 38 | * cleanup |
| 39 | * |
| 40 | * USAGE: <for command-line> |
| 41 | * close02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 42 | * where, -c n : Run n copies concurrently. |
| 43 | * -e : Turn on errno logging. |
| 44 | * -i n : Execute test n times. |
| 45 | * -I x : Execute test for x seconds. |
| 46 | * -P x : Pause for x seconds between iterations. |
| 47 | * -t : Turn on syscall timing. |
| 48 | * |
| 49 | * HISTORY |
| 50 | * 07/2001 Ported by Wayne Boyer |
| 51 | * |
| 52 | * RESTRICTIONS |
| 53 | * None |
| 54 | */ |
| 55 | |
| 56 | #include <stdio.h> |
| 57 | #include <errno.h> |
robbiew | 2c94524 | 2002-11-11 19:01:25 +0000 | [diff] [blame] | 58 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | #include "test.h" |
| 60 | #include "usctest.h" |
| 61 | |
| 62 | void cleanup(void); |
| 63 | void setup(void); |
| 64 | |
plars | 48049f3 | 2003-07-05 03:12:42 +0000 | [diff] [blame] | 65 | char *TCID = "close02"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 68 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 70 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 71 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 73 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 74 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 75 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 77 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
| 79 | /* The following loop checks looping state if -i option given */ |
| 80 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 81 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 82 | /* reset tst_count in case we are looping */ |
| 83 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
| 85 | TEST(close(-1)); |
| 86 | |
| 87 | if (TEST_RETURN != -1) { |
| 88 | tst_resm(TFAIL, "Closed a non existent fildes"); |
| 89 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | if (TEST_ERRNO != EBADF) { |
| 91 | tst_resm(TFAIL, "close() FAILED to set errno " |
| 92 | "to EBADF on an invalid fd, got %d", |
| 93 | errno); |
| 94 | } else { |
| 95 | tst_resm(TPASS, "call returned EBADF"); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | cleanup(); |
robbiew | 2c94524 | 2002-11-11 19:01:25 +0000 | [diff] [blame] | 100 | |
Garrett Cooper | 7b98b69 | 2010-12-16 22:20:35 -0800 | [diff] [blame] | 101 | tst_exit(); |
| 102 | |
| 103 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * setup() - performs all ONE TIME setup for this test |
| 107 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 108 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 110 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 112 | |
| 113 | umask(0); |
| 114 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | TEST_PAUSE; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 120 | * or premature exit. |
| 121 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 125 | } |