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 #include "safe_macros.h" 61 62 #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH 63 #define TEST_FILE "testfile" 64 65 char *TCID = "fstat03"; 66 int TST_TOTAL = 1; 67 68 int fildes; /* testfile descriptor */ 69 70 void setup(); /* Main setup function for the tests */ 71 void cleanup(); /* cleanup function for the test */ 72 73 int main(int ac, char **av) 74 { 75 struct stat stat_buf; /* stat structure buffer */ 76 int lc; 77 78 tst_parse_opts(ac, av, NULL, NULL); 79 80 /* 81 * Invoke setup function to create a testfile under temporary 82 * directory. 83 */ 84 setup(); 85 86 for (lc = 0; TEST_LOOPING(lc); lc++) { 87 88 tst_count = 0; 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)); 96 97 /* Check return code from fstat(2) */ 98 if (TEST_RETURN == -1) { 99 if (TEST_ERRNO == EBADF) { 100 tst_resm(TPASS, 101 "fstat() fails with expected error EBADF"); 102 } else { 103 tst_resm(TFAIL | TTERRNO, 104 "fstat() did not fail with EBADF"); 105 } 106 } else { 107 tst_resm(TFAIL, "fstat() returned %ld, expected -1", 108 TEST_RETURN); 109 } 110 } 111 112 /* 113 * Invoke cleanup() to delete the test directory/file(s) created 114 * in the setup(). 115 */ 116 cleanup(); 117 118 tst_exit(); 119 } 120 121 /* 122 * void 123 * setup(void) - performs all ONE TIME setup for this test. 124 * Exit the test program on receipt of unexpected signals. 125 * Create a temporary directory and change directory to it. 126 * Create a testfile under temporary directory. 127 * Close the testfile. 128 */ 129 void setup(void) 130 { 131 /* Capture unexpected signals */ 132 tst_sig(NOFORK, DEF_HANDLER, cleanup); 133 134 TEST_PAUSE; 135 136 /* Make a temp dir and cd to it */ 137 tst_tmpdir(); 138 139 /* Create a testfile under temporary directory */ 140 fildes = SAFE_OPEN(cleanup, TEST_FILE, O_RDWR | O_CREAT, 0666); 141 142 SAFE_CLOSE(cleanup, fildes); 143 } 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 */ 155 void cleanup(void) 156 { 157 158 tst_rmdir(); 159 160 } 161