1 /* 2 * 3 * Copyright (c) International Business Machines Corp., 2001 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13 * the GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 /* 21 * Test Name: lseek07 22 * 23 * Test Description: 24 * Verify that, lseek() call succeeds to set the file pointer position 25 * to more than the file size, when a file is opened for reading/writing. 26 * 27 * Expected Result: 28 * lseek() should return n+1, where n is the size of the file. 29 * Also when some data is written into this file it should start 30 * from that offset. 31 * 32 * Algorithm: 33 * Setup: 34 * Setup signal handling. 35 * Pause for SIGUSR1 if option specified. 36 * Create temporary directory. 37 * 38 * Test: 39 * Loop if the proper options are given. 40 * Execute system call 41 * Check return code, if system call failed (return=-1) 42 * Log the errno and Issue a FAIL message. 43 * Otherwise, 44 * Verify the Functionality of system call 45 * if successful, 46 * Issue Functionality-Pass message. 47 * Otherwise, 48 * Issue Functionality-Fail message. 49 * Cleanup: 50 * Print errno log and/or timing stats if options given 51 * Delete the temporary directory created. 52 * 53 * Usage: <for command-line> 54 * lseek07 [-c n] [-f] [-i n] [-I x] [-P x] [-t] 55 * where, -c n : Run n copies concurrently. 56 * -f : Turn off functionality Testing. 57 * -i n : Execute test n times. 58 * -I x : Execute test for x seconds. 59 * -P x : Pause for x seconds between iterations. 60 * -t : Turn on syscall timing. 61 * 62 * HISTORY 63 * 07/2001 Ported by Wayne Boyer 64 * 65 * RESTRICTIONS: 66 * None. 67 */ 68 69 #include <stdio.h> 70 #include <unistd.h> 71 #include <sys/types.h> 72 #include <errno.h> 73 #include <fcntl.h> 74 #include <utime.h> 75 #include <string.h> 76 #include <sys/stat.h> 77 #include <signal.h> 78 #include <inttypes.h> 79 80 #include "test.h" 81 82 #define TEMP_FILE "tmp_file" 83 #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH 84 85 char *TCID = "lseek07"; 86 int TST_TOTAL = 1; 87 int fildes; /* file handle for temp file */ 88 size_t file_size; /* size of temporary file */ 89 char write_buf1[BUFSIZ]; /* buffer to hold data */ 90 char write_buf2[BUFSIZ]; /* buffer to hold data */ 91 92 void setup(); /* Main setup function of test */ 93 void cleanup(); /* cleanup function for the test */ 94 95 int main(int ac, char **av) 96 { 97 int lc; 98 char read_buf[BUFSIZ]; /* data read from temp. file */ 99 off_t offset; /* byte position in temporary file */ 100 101 tst_parse_opts(ac, av, NULL, NULL); 102 103 setup(); 104 105 for (lc = 0; TEST_LOOPING(lc); lc++) { 106 107 tst_count = 0; 108 109 /* Set the offset position */ 110 offset = file_size + (lc * strlen(write_buf2)); 111 112 /* 113 * Invoke lseek(2) to move the write file 114 * pointer/handle by the specified offset value. 115 */ 116 TEST(lseek(fildes, offset, SEEK_SET)); 117 118 if (TEST_RETURN == (off_t) - 1) { 119 tst_resm(TFAIL | TTERRNO, "lseek on (%s) failed", 120 TEMP_FILE); 121 continue; 122 } 123 /* 124 * Check if the return value from lseek(2) 125 * is equal to the specified offset value. 126 */ 127 if (TEST_RETURN != offset) { 128 tst_resm(TFAIL, "lseek() returned " 129 "incorrect value %ld, expected " 130 "%" PRId64, TEST_RETURN, 131 (int64_t) offset); 132 continue; 133 } 134 /* 135 * The return value is okay, now write some data at 136 * the current offset position. 137 */ 138 if (write(fildes, write_buf2, strlen(write_buf2)) != 139 strlen(write_buf2)) { 140 tst_brkm(TFAIL | TERRNO, cleanup, 141 "write() failed to write additional data"); 142 } 143 144 /* 145 * Now close the file and open it again 146 * and read all of the data. 147 */ 148 if (close(fildes) < 0) { 149 tst_brkm(TFAIL, cleanup, "close() on %s Failed," 150 " errno = %d", TEMP_FILE, errno); 151 } 152 153 /* Open the file again in read/write mode */ 154 if ((fildes = open(TEMP_FILE, O_RDWR)) < 0) { 155 tst_brkm(TFAIL, cleanup, "Could not open the " 156 "%s readonly, error = %d", 157 TEMP_FILE, errno); 158 } 159 160 /* 161 * Now read all of the data. The size should be the 162 * offset + strlen(write_buf2). 163 */ 164 if (read(fildes, &read_buf, (offset + 165 strlen(write_buf2))) < 0) { 166 tst_brkm(TFAIL, cleanup, "read() failed on %s, " 167 "error=%d", TEMP_FILE, errno); 168 } else { 169 /* 170 * Check data read is the complete data and not 171 * the only portion written. 172 */ 173 if ((strncmp(read_buf, write_buf1, 174 strlen(write_buf1))) != 0) { 175 tst_brkm(TFAIL, cleanup, 176 "Incorrect data read #1 from " 177 "file %s", TEMP_FILE); 178 } 179 if ((strncmp(&read_buf[offset], write_buf2, 180 strlen(write_buf2))) != 0) { 181 tst_brkm(TFAIL, cleanup, 182 "Incorrect data read #2 from " 183 "file %s", TEMP_FILE); 184 } 185 tst_resm(TPASS, "Functionality of " 186 "lseek() on %s successful", TEMP_FILE); 187 } 188 } 189 190 cleanup(); 191 tst_exit(); 192 } 193 194 /* 195 * setup() - performs all ONE TIME setup for this test. 196 * Create a temporary directory and change directory to it. 197 * Create a test file under temporary directory and write some 198 * data into it. 199 * Get the size of the file using fstat(). 200 */ 201 void setup(void) 202 { 203 struct stat stat_buf; /* struct buffer for stat(2) */ 204 205 tst_sig(NOFORK, DEF_HANDLER, cleanup); 206 207 TEST_PAUSE; 208 209 tst_tmpdir(); 210 211 /* Get the data to be written to temporary file */ 212 strcpy(write_buf1, "abcdefg"); 213 strcpy(write_buf2, "ijk"); 214 215 /* Creat/open a temporary file for writing under above directory */ 216 if ((fildes = open(TEMP_FILE, O_WRONLY | O_CREAT, FILE_MODE)) == -1) { 217 tst_brkm(TBROK, cleanup, 218 "open(%s, O_WRONLY|O_CREAT, %#o) Failed, errno=%d :%s", 219 TEMP_FILE, FILE_MODE, errno, strerror(errno)); 220 } 221 222 /* Write data into temporary file */ 223 if (write(fildes, write_buf1, strlen(write_buf1)) != strlen(write_buf1)) { 224 tst_brkm(TBROK, cleanup, "write(2) on %s Failed, errno=%d : %s", 225 TEMP_FILE, errno, strerror(errno)); 226 } 227 228 /* Get the size of the temporary file after writing data */ 229 if (fstat(fildes, &stat_buf) < 0) { 230 tst_brkm(TBROK, cleanup, "fstat() on %s Failed, errno=%d : %s", 231 TEMP_FILE, errno, strerror(errno)); 232 } 233 234 file_size = stat_buf.st_size; 235 } 236 237 /* 238 * cleanup() - performs all ONE TIME cleanup for this test at 239 * completion or premature exit. 240 * Remove the test directory and testfile created in the setup. 241 */ 242 void cleanup(void) 243 { 244 245 /* Close the temporary file created */ 246 if (close(fildes) < 0) { 247 tst_brkm(TFAIL, NULL, "close(%s) Failed, errno=%d : %s:", 248 TEMP_FILE, errno, strerror(errno)); 249 } 250 251 tst_rmdir(); 252 253 } 254