1 /* 2 * Test Name: fstat03 3 * 4 * Test Description: 5 * Verify that, fstat(2) returns -1 and sets errno to EBADF if the file 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) 21 * if errno set == expected errno 22 * Issue sys call fails with expected return value and errno. 23 * Otherwise, 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" 60 61 #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH 62 #define TEST_FILE "testfile" 63 64 char *TCID = "fstat03"; 65 int TST_TOTAL = 1; 66 67 int fildes; /* testfile descriptor */ 68 69 void setup(); /* Main setup function for the tests */ 70 void cleanup(); /* cleanup function for the test */ 71 72 int main(int ac, char **av) 73 { 74 struct stat stat_buf; /* stat structure buffer */ 75 int lc; 76 77 tst_parse_opts(ac, av, NULL, NULL); 78 79 /* 80 * Invoke setup function to create a testfile under temporary 81 * directory. 82 */ 83 setup(); 84 85 for (lc = 0; TEST_LOOPING(lc); lc++) { 86 87 tst_count = 0; 88 /* 89 * Call fstat(2) to get the status information 90 * of a closed testfile pointed to by 'fd'. 91 * verify that fstat fails with -1 return value and 92 * sets appropriate errno. 93 */ 94 TEST(fstat(fildes, &stat_buf)); 95 96 /* Check return code from fstat(2) */ 97 if (TEST_RETURN == -1) { 98 if (TEST_ERRNO == EBADF) { 99 tst_resm(TPASS, 100 "fstat() fails with expected error EBADF"); 101 } else { 102 tst_resm(TFAIL | TTERRNO, 103 "fstat() did not fail with EBADF"); 104 } 105 } else { 106 tst_resm(TFAIL, "fstat() returned %ld, expected -1", 107 TEST_RETURN); 108 } 109 } 110 111 /* 112 * Invoke cleanup() to delete the test directory/file(s) created 113 * in the setup(). 114 */ 115 cleanup(); 116 117 tst_exit(); 118 } 119 120 /* 121 * void 122 * setup(void) - performs all ONE TIME setup for this test. 123 * Exit the test program on receipt of unexpected signals. 124 * Create a temporary directory and change directory to it. 125 * Create a testfile under temporary directory. 126 * Close the testfile. 127 */ 128 void setup(void) 129 { 130 /* Capture unexpected signals */ 131 tst_sig(NOFORK, DEF_HANDLER, cleanup); 132 133 TEST_PAUSE; 134 135 /* Make a temp dir and cd to it */ 136 tst_tmpdir(); 137 138 /* Create a testfile under temporary directory */ 139 fildes = open(TEST_FILE, O_RDWR | O_CREAT, 0666); 140 if (fildes == -1) 141 tst_brkm(TBROK | TERRNO, cleanup, 142 "open(%s, O_RDWR|O_CREAT, 0666) failed", TEST_FILE); 143 144 if (close(fildes) == -1) 145 tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed", 146 TEST_FILE); 147 } 148 149 /* 150 * void 151 * cleanup() - Performs all ONE TIME cleanup for this test at 152 * completion or premature exit. 153 * Print test timing stats and errno log if test executed with options. 154 * Close the testfile if still opened. 155 * Remove temporary directory and sub-directories/files under it 156 * created during setup(). 157 * Exit the test program with normal exit code. 158 */ 159 void cleanup(void) 160 { 161 162 tst_rmdir(); 163 164 } 165