blob: 0fa62b8475ae2d8aeea7646655e20b2adb444800 [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"
Cyril Hrubisdf007542017-10-03 13:29:15 +020060#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000061
62#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
63#define TEST_FILE "testfile"
64
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020065char *TCID = "fstat03";
66int TST_TOTAL = 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +080067
plars865695b2001-08-27 22:15:12 +000068int fildes; /* testfile descriptor */
69
70void setup(); /* Main setup function for the tests */
71void cleanup(); /* cleanup function for the test */
72
subrata_modak56207ce2009-03-23 13:35:39 +000073int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000074{
75 struct stat stat_buf; /* stat structure buffer */
Cyril Hrubis89af32a2012-10-24 16:39:11 +020076 int lc;
plars865695b2001-08-27 22:15:12 +000077
Cyril Hrubisd6d11d02015-03-09 17:35:43 +010078 tst_parse_opts(ac, av, NULL, NULL);
plars865695b2001-08-27 22:15:12 +000079
80 /*
81 * Invoke setup function to create a testfile under temporary
82 * directory.
83 */
84 setup();
85
plars865695b2001-08-27 22:15:12 +000086 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -080087
Caspar Zhangd59a6592013-03-07 14:59:12 +080088 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000089 /*
90 * Call fstat(2) to get the status information
91 * of a closed testfile pointed to by 'fd'.
92 * verify that fstat fails with -1 return value and
93 * sets appropriate errno.
94 */
95 TEST(fstat(fildes, &stat_buf));
subrata_modakbdbaec52009-02-26 12:14:51 +000096
plars865695b2001-08-27 22:15:12 +000097 /* Check return code from fstat(2) */
98 if (TEST_RETURN == -1) {
plars865695b2001-08-27 22:15:12 +000099 if (TEST_ERRNO == EBADF) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800100 tst_resm(TPASS,
101 "fstat() fails with expected error EBADF");
plars865695b2001-08-27 22:15:12 +0000102 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800103 tst_resm(TFAIL | TTERRNO,
104 "fstat() did not fail with EBADF");
plars865695b2001-08-27 22:15:12 +0000105 }
106 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800107 tst_resm(TFAIL, "fstat() returned %ld, expected -1",
108 TEST_RETURN);
plars865695b2001-08-27 22:15:12 +0000109 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800110 }
plars865695b2001-08-27 22:15:12 +0000111
112 /*
113 * Invoke cleanup() to delete the test directory/file(s) created
114 * in the setup().
115 */
116 cleanup();
117
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800118 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800119}
plars865695b2001-08-27 22:15:12 +0000120
121/*
subrata_modak4bb656a2009-02-26 12:02:09 +0000122 * void
plars865695b2001-08-27 22:15:12 +0000123 * setup(void) - performs all ONE TIME setup for this test.
subrata_modak56207ce2009-03-23 13:35:39 +0000124 * Exit the test program on receipt of unexpected signals.
plars865695b2001-08-27 22:15:12 +0000125 * Create a temporary directory and change directory to it.
126 * Create a testfile under temporary directory.
127 * Close the testfile.
128 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400129void setup(void)
plars865695b2001-08-27 22:15:12 +0000130{
131 /* Capture unexpected signals */
132 tst_sig(NOFORK, DEF_HANDLER, cleanup);
133
plars865695b2001-08-27 22:15:12 +0000134 TEST_PAUSE;
135
136 /* Make a temp dir and cd to it */
137 tst_tmpdir();
138
139 /* Create a testfile under temporary directory */
Cyril Hrubisd8cd45c2017-10-03 16:50:35 +0200140 fildes = SAFE_OPEN(cleanup, TEST_FILE, O_RDWR | O_CREAT, 0666);
plars865695b2001-08-27 22:15:12 +0000141
Cyril Hrubisdf007542017-10-03 13:29:15 +0200142 SAFE_CLOSE(cleanup, fildes);
Garrett Cooper2c282152010-12-16 00:55:50 -0800143}
plars865695b2001-08-27 22:15:12 +0000144
145/*
146 * void
147 * cleanup() - Performs all ONE TIME cleanup for this test at
148 * completion or premature exit.
149 * Print test timing stats and errno log if test executed with options.
150 * Close the testfile if still opened.
151 * Remove temporary directory and sub-directories/files under it
152 * created during setup().
153 * Exit the test program with normal exit code.
154 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400155void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000156{
subrata_modakbdbaec52009-02-26 12:14:51 +0000157
plars865695b2001-08-27 22:15:12 +0000158 tst_rmdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000159
Chris Dearmanec6edca2012-10-17 19:54:01 -0700160}