plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 21 | * Test Name: lseek07 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 44 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 54 | * lseek07 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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> |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 78 | #include <inttypes.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
| 80 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
| 82 | #define TEMP_FILE "tmp_file" |
| 83 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 84 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 85 | char *TCID = "lseek07"; |
| 86 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | int fildes; /* file handle for temp file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | size_t file_size; /* size of temporary file */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 97 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 98 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | char read_buf[BUFSIZ]; /* data read from temp. file */ |
| 100 | off_t offset; /* byte position in temporary file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 102 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | setup(); |
| 106 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 108 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 109 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
| 111 | /* Set the offset position */ |
| 112 | offset = file_size + (lc * strlen(write_buf2)); |
| 113 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 114 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | * Invoke lseek(2) to move the write file |
| 116 | * pointer/handle by the specified offset value. |
| 117 | */ |
| 118 | TEST(lseek(fildes, offset, SEEK_SET)); |
| 119 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | if (TEST_RETURN == (off_t) - 1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | tst_resm(TFAIL | TTERRNO, "lseek on (%s) failed", |
| 122 | TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | continue; |
| 124 | } |
| 125 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 126 | * Check if the return value from lseek(2) |
| 127 | * is equal to the specified offset value. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 129 | if (TEST_RETURN != offset) { |
| 130 | tst_resm(TFAIL, "lseek() returned " |
| 131 | "incorrect value %ld, expected " |
| 132 | "%" PRId64, TEST_RETURN, |
| 133 | (int64_t) offset); |
| 134 | continue; |
| 135 | } |
| 136 | /* |
| 137 | * The return value is okay, now write some data at |
| 138 | * the current offset position. |
| 139 | */ |
| 140 | if (write(fildes, write_buf2, strlen(write_buf2)) != |
| 141 | strlen(write_buf2)) { |
| 142 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 143 | "write() failed to write additional data"); |
| 144 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 146 | /* |
| 147 | * Now close the file and open it again |
| 148 | * and read all of the data. |
| 149 | */ |
| 150 | if (close(fildes) < 0) { |
| 151 | tst_brkm(TFAIL, cleanup, "close() on %s Failed," |
| 152 | " errno = %d", TEMP_FILE, errno); |
| 153 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 155 | /* Open the file again in read/write mode */ |
| 156 | if ((fildes = open(TEMP_FILE, O_RDWR)) < 0) { |
| 157 | tst_brkm(TFAIL, cleanup, "Could not open the " |
| 158 | "%s readonly, error = %d", |
| 159 | TEMP_FILE, errno); |
| 160 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 162 | /* |
| 163 | * Now read all of the data. The size should be the |
| 164 | * offset + strlen(write_buf2). |
| 165 | */ |
| 166 | if (read(fildes, &read_buf, (offset + |
| 167 | strlen(write_buf2))) < 0) { |
| 168 | tst_brkm(TFAIL, cleanup, "read() failed on %s, " |
| 169 | "error=%d", TEMP_FILE, errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 171 | /* |
| 172 | * Check data read is the complete data and not |
| 173 | * the only portion written. |
| 174 | */ |
| 175 | if ((strncmp(read_buf, write_buf1, |
| 176 | strlen(write_buf1))) != 0) { |
| 177 | tst_brkm(TFAIL, cleanup, |
| 178 | "Incorrect data read #1 from " |
| 179 | "file %s", TEMP_FILE); |
| 180 | } |
| 181 | if ((strncmp(&read_buf[offset], write_buf2, |
| 182 | strlen(write_buf2))) != 0) { |
| 183 | tst_brkm(TFAIL, cleanup, |
| 184 | "Incorrect data read #2 from " |
| 185 | "file %s", TEMP_FILE); |
| 186 | } |
| 187 | tst_resm(TPASS, "Functionality of " |
| 188 | "lseek() on %s successful", TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 190 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 193 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 194 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | |
| 196 | /* |
| 197 | * setup() - performs all ONE TIME setup for this test. |
| 198 | * Create a temporary directory and change directory to it. |
| 199 | * Create a test file under temporary directory and write some |
| 200 | * data into it. |
| 201 | * Get the size of the file using fstat(). |
| 202 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 203 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 205 | struct stat stat_buf; /* struct buffer for stat(2) */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 207 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 208 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | TEST_PAUSE; |
| 210 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 211 | tst_tmpdir(); |
| 212 | |
| 213 | /* Get the data to be written to temporary file */ |
| 214 | strcpy(write_buf1, "abcdefg"); |
| 215 | strcpy(write_buf2, "ijk"); |
| 216 | |
| 217 | /* Creat/open a temporary file for writing under above directory */ |
| 218 | if ((fildes = open(TEMP_FILE, O_WRONLY | O_CREAT, FILE_MODE)) == -1) { |
| 219 | tst_brkm(TBROK, cleanup, |
| 220 | "open(%s, O_WRONLY|O_CREAT, %#o) Failed, errno=%d :%s", |
| 221 | TEMP_FILE, FILE_MODE, errno, strerror(errno)); |
| 222 | } |
| 223 | |
| 224 | /* Write data into temporary file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 225 | if (write(fildes, write_buf1, strlen(write_buf1)) != strlen(write_buf1)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 226 | tst_brkm(TBROK, cleanup, "write(2) on %s Failed, errno=%d : %s", |
| 227 | TEMP_FILE, errno, strerror(errno)); |
| 228 | } |
| 229 | |
| 230 | /* Get the size of the temporary file after writing data */ |
| 231 | if (fstat(fildes, &stat_buf) < 0) { |
| 232 | tst_brkm(TBROK, cleanup, "fstat() on %s Failed, errno=%d : %s", |
| 233 | TEMP_FILE, errno, strerror(errno)); |
| 234 | } |
| 235 | |
| 236 | file_size = stat_buf.st_size; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 241 | * completion or premature exit. |
| 242 | * Remove the test directory and testfile created in the setup. |
| 243 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 244 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 245 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 246 | |
| 247 | /* Close the temporary file created */ |
| 248 | if (close(fildes) < 0) { |
| 249 | tst_brkm(TFAIL, NULL, "close(%s) Failed, errno=%d : %s:", |
| 250 | TEMP_FILE, errno, strerror(errno)); |
| 251 | } |
| 252 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 253 | tst_rmdir(); |
| 254 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 255 | } |