blob: 0431021cf625ff94b5a684ac2474d9bfd75727af [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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>
robbiew2c945242002-11-11 19:01:25 +000058#include <sys/stat.h>
plars865695b2001-08-27 22:15:12 +000059#include "test.h"
60#include "usctest.h"
61
62void cleanup(void);
63void setup(void);
64
plars48049f32003-07-05 03:12:42 +000065char *TCID = "close02";
plars865695b2001-08-27 22:15:12 +000066int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000067
subrata_modak56207ce2009-03-23 13:35:39 +000068int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000069{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020070 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020071 const char *msg;
plars865695b2001-08-27 22:15:12 +000072
Garrett Cooper45e285d2010-11-22 12:19:25 -080073 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080074 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +080075 }
plars865695b2001-08-27 22:15:12 +000076
Cyril Hrubis605fa332015-02-04 13:11:20 +010077 setup();
plars865695b2001-08-27 22:15:12 +000078
79 /* The following loop checks looping state if -i option given */
80 for (lc = 0; TEST_LOOPING(lc); lc++) {
81
Caspar Zhangd59a6592013-03-07 14:59:12 +080082 /* reset tst_count in case we are looping */
83 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000084
85 TEST(close(-1));
86
87 if (TEST_RETURN != -1) {
88 tst_resm(TFAIL, "Closed a non existent fildes");
89 } else {
plars865695b2001-08-27 22:15:12 +000090 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();
robbiew2c945242002-11-11 19:01:25 +0000100
Garrett Cooper7b98b692010-12-16 22:20:35 -0800101 tst_exit();
102
103}
plars865695b2001-08-27 22:15:12 +0000104
105/*
106 * setup() - performs all ONE TIME setup for this test
107 */
subrata_modak56207ce2009-03-23 13:35:39 +0000108void setup(void)
plars865695b2001-08-27 22:15:12 +0000109{
Garrett Cooper2c282152010-12-16 00:55:50 -0800110
plars865695b2001-08-27 22:15:12 +0000111 tst_sig(FORK, DEF_HANDLER, cleanup);
112
113 umask(0);
114
plars865695b2001-08-27 22:15:12 +0000115 TEST_PAUSE;
116}
117
118/*
119 * cleanup() - performs all the ONE TIME cleanup for this test at completion
120 * or premature exit.
121 */
subrata_modak56207ce2009-03-23 13:35:39 +0000122void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000123{
plars865695b2001-08-27 22:15:12 +0000124
Chris Dearmanec6edca2012-10-17 19:54:01 -0700125}