blob: b56569c601d80642f440e40795c8647377f540ff [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"
plars865695b2001-08-27 22:15:12 +000060
61void cleanup(void);
62void setup(void);
63
plars48049f32003-07-05 03:12:42 +000064char *TCID = "close02";
plars865695b2001-08-27 22:15:12 +000065int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000066
subrata_modak56207ce2009-03-23 13:35:39 +000067int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000068{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020069 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020070 const char *msg;
plars865695b2001-08-27 22:15:12 +000071
Garrett Cooper45e285d2010-11-22 12:19:25 -080072 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080073 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +080074 }
plars865695b2001-08-27 22:15:12 +000075
Cyril Hrubis605fa332015-02-04 13:11:20 +010076 setup();
plars865695b2001-08-27 22:15:12 +000077
78 /* The following loop checks looping state if -i option given */
79 for (lc = 0; TEST_LOOPING(lc); lc++) {
80
Caspar Zhangd59a6592013-03-07 14:59:12 +080081 /* reset tst_count in case we are looping */
82 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000083
84 TEST(close(-1));
85
86 if (TEST_RETURN != -1) {
87 tst_resm(TFAIL, "Closed a non existent fildes");
88 } else {
plars865695b2001-08-27 22:15:12 +000089 if (TEST_ERRNO != EBADF) {
90 tst_resm(TFAIL, "close() FAILED to set errno "
91 "to EBADF on an invalid fd, got %d",
92 errno);
93 } else {
94 tst_resm(TPASS, "call returned EBADF");
95 }
96 }
97 }
98 cleanup();
robbiew2c945242002-11-11 19:01:25 +000099
Garrett Cooper7b98b692010-12-16 22:20:35 -0800100 tst_exit();
101
102}
plars865695b2001-08-27 22:15:12 +0000103
104/*
105 * setup() - performs all ONE TIME setup for this test
106 */
subrata_modak56207ce2009-03-23 13:35:39 +0000107void setup(void)
plars865695b2001-08-27 22:15:12 +0000108{
Garrett Cooper2c282152010-12-16 00:55:50 -0800109
plars865695b2001-08-27 22:15:12 +0000110 tst_sig(FORK, DEF_HANDLER, cleanup);
111
112 umask(0);
113
plars865695b2001-08-27 22:15:12 +0000114 TEST_PAUSE;
115}
116
117/*
118 * cleanup() - performs all the ONE TIME cleanup for this test at completion
119 * or premature exit.
120 */
subrata_modak56207ce2009-03-23 13:35:39 +0000121void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000122{
plars865695b2001-08-27 22:15:12 +0000123
Chris Dearmanec6edca2012-10-17 19:54:01 -0700124}