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 | /* |
| 21 | * Test Name: pwrite01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify the functionality of pwrite() by writing known data using pwrite() |
| 25 | * to the file at various specified offsets and later read from the file from |
| 26 | * various specified offsets, comparing the data written aganist the data |
| 27 | * read using read(). |
| 28 | * |
| 29 | * Expected Result: |
| 30 | * pwrite() should succeed to write the expected no. of bytes of data and |
| 31 | * the data written at specified offsets should match aganist the data read |
| 32 | * from that offset. |
| 33 | * |
| 34 | * Algorithm: |
| 35 | * Setup: |
| 36 | * Setup signal handling. |
| 37 | * Create temporary directory. |
| 38 | * Pause for SIGUSR1 if option specified. |
| 39 | * |
| 40 | * Test: |
| 41 | * Loop if the proper options are given. |
| 42 | * Execute system call |
| 43 | * Check return code, if system call failed (return=-1) |
| 44 | * Log the errno and Issue a FAIL message. |
| 45 | * Otherwise, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 46 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | * if successful, |
| 48 | * Issue Functionality-Pass message. |
| 49 | * Otherwise, |
| 50 | * Issue Functionality-Fail message. |
| 51 | * Cleanup: |
| 52 | * Print errno log and/or timing stats if options given |
| 53 | * Delete the temporary directory created. |
| 54 | * |
| 55 | * Usage: <for command-line> |
| 56 | * pwrite01 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 57 | * where, -c n : Run n copies concurrently. |
| 58 | * -f : Turn off functionality Testing. |
| 59 | * -i n : Execute test n times. |
| 60 | * -I x : Execute test for x seconds. |
| 61 | * -P x : Pause for x seconds between iterations. |
| 62 | * -t : Turn on syscall timing. |
| 63 | * |
| 64 | * HISTORY |
| 65 | * 07/2001 Ported by Wayne Boyer |
| 66 | * |
| 67 | * RESTRICTIONS: |
| 68 | * None. |
| 69 | */ |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 70 | |
| 71 | #define _XOPEN_SOURCE 500 |
| 72 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | #include <errno.h> |
| 74 | #include <unistd.h> |
| 75 | #include <fcntl.h> |
robbiew | d6df93c | 2002-05-20 20:31:17 +0000 | [diff] [blame] | 76 | #include <stdlib.h> |
| 77 | #include <string.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 | |
robbiew | d6df93c | 2002-05-20 20:31:17 +0000 | [diff] [blame] | 82 | #define _XOPEN_SOURCE 500 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | #define TEMPFILE "pwrite_file" |
| 84 | #define K1 1024 |
| 85 | #define K2 (K1 * 2) |
| 86 | #define K3 (K1 * 3) |
| 87 | #define K4 (K1 * 4) |
| 88 | #define NBUFS 4 |
| 89 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 90 | char *TCID = "pwrite01"; |
| 91 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | int fildes; /* file descriptor for tempfile */ |
| 93 | char *write_buf[NBUFS]; /* buffer to hold data to be written */ |
| 94 | |
| 95 | void setup(); /* Main setup function of test */ |
| 96 | void cleanup(); /* cleanup function for the test */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | void l_seek(int, off_t, int, off_t); /* function to call lseek() */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | void init_buffers(); /* function to initialize/allocate buffers */ |
| 99 | void check_file_contents(); /* function to verify the contents of file */ |
| 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 103 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 104 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | int nwrite; /* no. of bytes written by pwrite() */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 106 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 107 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 108 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | setup(); |
| 111 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 113 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 114 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 116 | /* |
| 117 | * Call pwrite() to write K1 bytes of data (0's) at offset 0 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | * of temporary file |
| 119 | */ |
| 120 | nwrite = pwrite(fildes, write_buf[0], K1, 0); |
| 121 | |
| 122 | /* Check for the return value of pwrite() */ |
| 123 | if (nwrite != K1) { |
| 124 | tst_resm(TFAIL, "pwrite() at offset 0 failed, errno=%d " |
| 125 | ": %s", errno, strerror(errno)); |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | /* We should still be at offset 0. */ |
| 130 | l_seek(fildes, 0, SEEK_CUR, 0); |
| 131 | |
| 132 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 133 | * Now, lseek() to a non K boundary, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | * just to be different. |
| 135 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 136 | l_seek(fildes, K1 / 2, SEEK_SET, K1 / 2); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | |
| 138 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 139 | * Again, pwrite() K1 of data (2's) at offset K2 of |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | * temporary file. |
| 141 | */ |
| 142 | nwrite = pwrite(fildes, write_buf[2], K1, K2); |
| 143 | if (nwrite != K1) { |
| 144 | tst_resm(TFAIL, "pwrite() failed to " |
| 145 | "write at %d off. on %s, errno=%d : %s", |
| 146 | K2, TEMPFILE, errno, strerror(errno)); |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | /* We should still be at our non K boundary. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | l_seek(fildes, 0, SEEK_CUR, K1 / 2); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | |
| 153 | /* lseek() to an offset of K3. */ |
| 154 | l_seek(fildes, K3, SEEK_SET, K3); |
| 155 | |
| 156 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 157 | * Using write(), write of K1 of data (3's) which |
| 158 | * should take place at an offset of K3, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | * moving the file pointer to K4. |
| 160 | */ |
| 161 | nwrite = write(fildes, write_buf[3], K1); |
| 162 | if (nwrite != K1) { |
| 163 | tst_resm(TFAIL, "write() failed: nwrite=%d, errno=%d : " |
| 164 | "%s", nwrite, errno, strerror(errno)); |
| 165 | continue; |
| 166 | } |
| 167 | |
| 168 | /* We should be at offset K4. */ |
| 169 | l_seek(fildes, 0, SEEK_CUR, K4); |
| 170 | |
| 171 | /* Again, pwrite() K1 of data (1's) at offset K1. */ |
| 172 | nwrite = pwrite(fildes, write_buf[1], K1, K1); |
| 173 | if (nwrite != K1) { |
| 174 | tst_resm(TFAIL, "pwrite() failed to write at " |
| 175 | "%d off. on %s, errno=%d : %s", |
| 176 | K1, TEMPFILE, errno, strerror(errno)); |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 181 | * Check the contents of temporary file |
| 182 | * to which data written using pwrite(). |
| 183 | * Compare the data read with the original |
| 184 | * write_buf[] contents. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 186 | check_file_contents(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | |
| 188 | /* reset to offset 0 in case we are looping */ |
| 189 | l_seek(fildes, 0, SEEK_SET, 0); |
| 190 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 191 | } |
| 192 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 193 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 194 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 195 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | |
| 197 | /* |
| 198 | * setup() - performs all ONE TIME setup for this test. |
| 199 | * |
| 200 | * Initialize/allocate read/write buffers. |
| 201 | * Create a temporary directory and a file under it and |
| 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 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 205 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 207 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | TEST_PAUSE; |
| 209 | |
| 210 | /* Allocate/Initialize the write buffer with known data */ |
| 211 | init_buffers(); |
| 212 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 213 | tst_tmpdir(); |
| 214 | |
| 215 | /* Creat a temporary file used for mapping */ |
| 216 | if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) { |
| 217 | tst_brkm(TBROK, cleanup, "open() on %s Failed, errno=%d : %s", |
| 218 | TEMPFILE, errno, strerror(errno)); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * init_buffers() - allocate/Initialize write_buf array. |
| 224 | * |
| 225 | * Allocate write buffer. |
| 226 | * Fill the write buffer with the following data like, |
| 227 | * write_buf[0] has 0's, write_buf[1] has 1's, write_buf[2] has 2's |
| 228 | * write_buf[3] has 3's. |
| 229 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 230 | void init_buffers(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 231 | { |
| 232 | int count; /* counter variable for loop */ |
| 233 | |
| 234 | /* Allocate and Initialize write buffer with known data */ |
| 235 | for (count = 0; count < NBUFS; count++) { |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 236 | write_buf[count] = malloc(K1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 237 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 238 | if (write_buf[count] == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 239 | tst_brkm(TBROK, NULL, "malloc() failed "); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 240 | } |
| 241 | memset(write_buf[count], count, K1); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * l_seek() - local front end to lseek(). |
| 247 | * |
| 248 | * "checkoff" is the offset at which we believe we should be at. |
| 249 | * Used to validate pread/pwrite don't move the offset. |
| 250 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 251 | void l_seek(int fdesc, off_t offset, int whence, off_t checkoff) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 252 | { |
| 253 | off_t offloc; /* offset ret. from lseek() */ |
| 254 | |
| 255 | if ((offloc = lseek(fdesc, offset, whence)) != checkoff) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 256 | tst_resm(TWARN, "lseek returned %" PRId64 ", expected %" PRId64, |
| 257 | (int64_t) offloc, (int64_t) checkoff); |
| 258 | tst_brkm(TBROK | TERRNO, cleanup, "lseek() on %s Failed", |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 259 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 264 | * check_file_contents() - Check the contents of the file we wrote with |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 265 | * pwrite()'s. |
| 266 | * The contents of the file are verified by using a plain read() and |
| 267 | * Compare the data read with the original write_buf[] contents. |
| 268 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 269 | void check_file_contents(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 270 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 271 | int count, err_flg = 0; /* index variable and error flag */ |
| 272 | int nread; /* return value of read() */ |
| 273 | off_t offloc; /* offset. ret. by lseek() */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 274 | char *read_buf; /* buffer to hold read data */ |
| 275 | |
| 276 | /* Allocate space for read buffer */ |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 277 | read_buf = malloc(K1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 278 | if (read_buf == NULL) { |
| 279 | tst_brkm(TBROK, cleanup, "malloc() failed on read buffer"); |
| 280 | } |
| 281 | |
| 282 | /* Seek to app. location of file and read the data, compare it */ |
| 283 | for (count = 0; count < NBUFS; count++) { |
| 284 | /* Seek to specified offset position from beginning */ |
| 285 | offloc = lseek(fildes, count * K1, SEEK_SET); |
| 286 | if (offloc != (count * K1)) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 287 | tst_brkm(TBROK | TERRNO, cleanup, |
| 288 | "lseek() failed: offloc=%" PRId64, |
| 289 | (int64_t) offloc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /* Read the data from file into a buffer */ |
| 293 | nread = read(fildes, read_buf, K1); |
| 294 | if (nread != K1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 295 | tst_brkm(TBROK | TERRNO, cleanup, |
| 296 | "read() failed: nread=%d", nread); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* Compare the read data with the data written using pwrite */ |
| 300 | if (memcmp(write_buf[count], read_buf, K1) != 0) { |
| 301 | tst_resm(TFAIL, "read/write buffer data mismatch"); |
| 302 | err_flg++; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /* If no erros, Test successful */ |
| 307 | if (!err_flg) { |
| 308 | tst_resm(TPASS, "Functionality of pwrite() successful"); |
| 309 | } |
| 310 | |
| 311 | /* Release the memory allocated before return */ |
| 312 | free(read_buf); |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 317 | * completion or premature exit. |
| 318 | * |
| 319 | * Deallocate the memory allocated to write buffer. |
| 320 | * Close the temporary file. |
| 321 | * Remove the temporary directory created. |
| 322 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 323 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 324 | { |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 325 | int count; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 326 | |
| 327 | /* Free the memory allocated for the write buffer */ |
| 328 | for (count = 0; count < NBUFS; count++) { |
| 329 | free(write_buf[count]); |
| 330 | } |
| 331 | |
| 332 | /* Close the temporary file */ |
| 333 | if (close(fildes) < 0) { |
| 334 | tst_brkm(TBROK, NULL, "close() on %s Failed, errno=%d : %s", |
| 335 | TEMPFILE, errno, strerror(errno)); |
| 336 | } |
| 337 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 338 | tst_rmdir(); |
| 339 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 340 | } |