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