blob: 91ddfff79896d3be832ab20bf0bc2904c1c0d1da [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Test Name: fstat03
3 *
4 * Test Description:
subrata_modak4bb656a2009-02-26 12:02:09 +00005 * Verify that, fstat(2) returns -1 and sets errno to EBADF if the file
plars865695b2001-08-27 22:15:12 +00006 * pointed to by file descriptor is not valid.
7 *
8 * Expected Result:
9 * fstat() should fail with return value -1 and set expected errno.
10 *
11 * Algorithm:
12 * Setup:
13 * Setup signal handling.
14 * Create temporary directory.
15 * Pause for SIGUSR1 if option specified.
16 *
17 * Test:
18 * Loop if the proper options are given.
19 * Execute system call
20 * Check return code, if system call failed (return=-1)
subrata_modak56207ce2009-03-23 13:35:39 +000021 * if errno set == expected errno
22 * Issue sys call fails with expected return value and errno.
23 * Otherwise,
plars865695b2001-08-27 22:15:12 +000024 * Issue sys call fails with unexpected errno.
25 * Otherwise,
26 * Issue sys call returns unexpected value.
27 *
28 * Cleanup:
29 * Print errno log and/or timing stats if options given
30 * Delete the temporary directory(s)/file(s) created.
31 *
32 * Usage: <for command-line>
33 * fstat03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
34 * where, -c n : Run n copies concurrently.
35 * -e : Turn on errno logging.
36 * -i n : Execute test n times.
37 * -I x : Execute test for x seconds.
38 * -P x : Pause for x seconds between iterations.
39 * -t : Turn on syscall timing.
40 *
41 * HISTORY
42 * 07/2001 Ported by Wayne Boyer
43 *
44 * RESTRICTIONS:
45 * This test should be executed by 'non-super-user' only.
46 *
47 */
48
49#include <stdio.h>
50#include <stdlib.h>
51#include <unistd.h>
52#include <fcntl.h>
53#include <errno.h>
54#include <string.h>
55#include <signal.h>
56#include <sys/types.h>
57#include <sys/stat.h>
58
59#include "test.h"
plars865695b2001-08-27 22:15:12 +000060
61#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
62#define TEST_FILE "testfile"
63
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020064char *TCID = "fstat03";
65int TST_TOTAL = 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +080066
plars865695b2001-08-27 22:15:12 +000067int fildes; /* testfile descriptor */
68
69void setup(); /* Main setup function for the tests */
70void cleanup(); /* cleanup function for the test */
71
subrata_modak56207ce2009-03-23 13:35:39 +000072int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000073{
74 struct stat stat_buf; /* stat structure buffer */
Cyril Hrubis89af32a2012-10-24 16:39:11 +020075 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020076 const char *msg;
plars865695b2001-08-27 22:15:12 +000077
Garrett Cooper45e285d2010-11-22 12:19:25 -080078 msg = parse_opts(ac, av, NULL, NULL);
79 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +000080 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080081
plars865695b2001-08-27 22:15:12 +000082 }
83
84 /*
85 * Invoke setup function to create a testfile under temporary
86 * directory.
87 */
88 setup();
89
plars865695b2001-08-27 22:15:12 +000090 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -080091
Caspar Zhangd59a6592013-03-07 14:59:12 +080092 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000093 /*
94 * Call fstat(2) to get the status information
95 * of a closed testfile pointed to by 'fd'.
96 * verify that fstat fails with -1 return value and
97 * sets appropriate errno.
98 */
99 TEST(fstat(fildes, &stat_buf));
subrata_modakbdbaec52009-02-26 12:14:51 +0000100
plars865695b2001-08-27 22:15:12 +0000101 /* Check return code from fstat(2) */
102 if (TEST_RETURN == -1) {
plars865695b2001-08-27 22:15:12 +0000103 if (TEST_ERRNO == EBADF) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104 tst_resm(TPASS,
105 "fstat() fails with expected error EBADF");
plars865695b2001-08-27 22:15:12 +0000106 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800107 tst_resm(TFAIL | TTERRNO,
108 "fstat() did not fail with EBADF");
plars865695b2001-08-27 22:15:12 +0000109 }
110 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800111 tst_resm(TFAIL, "fstat() returned %ld, expected -1",
112 TEST_RETURN);
plars865695b2001-08-27 22:15:12 +0000113 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800114 }
plars865695b2001-08-27 22:15:12 +0000115
116 /*
117 * Invoke cleanup() to delete the test directory/file(s) created
118 * in the setup().
119 */
120 cleanup();
121
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800122 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800123}
plars865695b2001-08-27 22:15:12 +0000124
125/*
subrata_modak4bb656a2009-02-26 12:02:09 +0000126 * void
plars865695b2001-08-27 22:15:12 +0000127 * setup(void) - performs all ONE TIME setup for this test.
subrata_modak56207ce2009-03-23 13:35:39 +0000128 * Exit the test program on receipt of unexpected signals.
plars865695b2001-08-27 22:15:12 +0000129 * Create a temporary directory and change directory to it.
130 * Create a testfile under temporary directory.
131 * Close the testfile.
132 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400133void setup(void)
plars865695b2001-08-27 22:15:12 +0000134{
135 /* Capture unexpected signals */
136 tst_sig(NOFORK, DEF_HANDLER, cleanup);
137
plars865695b2001-08-27 22:15:12 +0000138 TEST_PAUSE;
139
140 /* Make a temp dir and cd to it */
141 tst_tmpdir();
142
143 /* Create a testfile under temporary directory */
vapier293f19b2009-08-28 12:51:34 +0000144 fildes = open(TEST_FILE, O_RDWR | O_CREAT, 0666);
145 if (fildes == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800146 tst_brkm(TBROK | TERRNO, cleanup,
vapier293f19b2009-08-28 12:51:34 +0000147 "open(%s, O_RDWR|O_CREAT, 0666) failed", TEST_FILE);
plars865695b2001-08-27 22:15:12 +0000148
vapier293f19b2009-08-28 12:51:34 +0000149 if (close(fildes) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800150 tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed",
151 TEST_FILE);
Garrett Cooper2c282152010-12-16 00:55:50 -0800152}
plars865695b2001-08-27 22:15:12 +0000153
154/*
155 * void
156 * cleanup() - Performs all ONE TIME cleanup for this test at
157 * completion or premature exit.
158 * Print test timing stats and errno log if test executed with options.
159 * Close the testfile if still opened.
160 * Remove temporary directory and sub-directories/files under it
161 * created during setup().
162 * Exit the test program with normal exit code.
163 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400164void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000165{
subrata_modakbdbaec52009-02-26 12:14:51 +0000166
plars865695b2001-08-27 22:15:12 +0000167 tst_rmdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000168
Chris Dearmanec6edca2012-10-17 19:54:01 -0700169}