plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Test Name: fstat03 |
| 3 | * |
| 4 | * Test Description: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 5 | * Verify that, fstat(2) returns -1 and sets errno to EBADF if the file |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 6 | * 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 21 | * if errno set == expected errno |
| 22 | * Issue sys call fails with expected return value and errno. |
| 23 | * Otherwise, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 24 | * 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 Hrubis | df00754 | 2017-10-03 13:29:15 +0200 | [diff] [blame] | 60 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
| 62 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 63 | #define TEST_FILE "testfile" |
| 64 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 65 | char *TCID = "fstat03"; |
| 66 | int TST_TOTAL = 1; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 67 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | int fildes; /* testfile descriptor */ |
| 69 | |
| 70 | void setup(); /* Main setup function for the tests */ |
| 71 | void cleanup(); /* cleanup function for the test */ |
| 72 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | { |
| 75 | struct stat stat_buf; /* stat structure buffer */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 76 | int lc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
Cyril Hrubis | d6d11d0 | 2015-03-09 17:35:43 +0100 | [diff] [blame] | 78 | tst_parse_opts(ac, av, NULL, NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * Invoke setup function to create a testfile under temporary |
| 82 | * directory. |
| 83 | */ |
| 84 | setup(); |
| 85 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 87 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 88 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | /* |
| 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 96 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | /* Check return code from fstat(2) */ |
| 98 | if (TEST_RETURN == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | if (TEST_ERRNO == EBADF) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 100 | tst_resm(TPASS, |
| 101 | "fstat() fails with expected error EBADF"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 103 | tst_resm(TFAIL | TTERRNO, |
| 104 | "fstat() did not fail with EBADF"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | } |
| 106 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 107 | tst_resm(TFAIL, "fstat() returned %ld, expected -1", |
| 108 | TEST_RETURN); |
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 | |
| 112 | /* |
| 113 | * Invoke cleanup() to delete the test directory/file(s) created |
| 114 | * in the setup(). |
| 115 | */ |
| 116 | cleanup(); |
| 117 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 118 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 119 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
| 121 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 122 | * void |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | * setup(void) - performs all ONE TIME setup for this test. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | * Exit the test program on receipt of unexpected signals. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | * Create a temporary directory and change directory to it. |
| 126 | * Create a testfile under temporary directory. |
| 127 | * Close the testfile. |
| 128 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 129 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | { |
| 131 | /* Capture unexpected signals */ |
| 132 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 133 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | TEST_PAUSE; |
| 135 | |
| 136 | /* Make a temp dir and cd to it */ |
| 137 | tst_tmpdir(); |
| 138 | |
| 139 | /* Create a testfile under temporary directory */ |
Cyril Hrubis | d8cd45c | 2017-10-03 16:50:35 +0200 | [diff] [blame] | 140 | fildes = SAFE_OPEN(cleanup, TEST_FILE, O_RDWR | O_CREAT, 0666); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
Cyril Hrubis | df00754 | 2017-10-03 13:29:15 +0200 | [diff] [blame] | 142 | SAFE_CLOSE(cleanup, fildes); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 143 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | |
| 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 Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 155 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | { |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 157 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | tst_rmdir(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 160 | } |