blob: 9e1b49ad63528a5fc87215eba512b198c76db889 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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_modakbdbaec52009-02-26 12:14:51 +000046 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000047 * 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 */
plars74948ad2002-11-14 16:16:14 +000070
71#define _XOPEN_SOURCE 500
72
plars865695b2001-08-27 22:15:12 +000073#include <errno.h>
74#include <unistd.h>
75#include <fcntl.h>
robbiewd6df93c2002-05-20 20:31:17 +000076#include <stdlib.h>
77#include <string.h>
subrata_modak923b23f2009-11-02 13:57:16 +000078#include <inttypes.h>
plars865695b2001-08-27 22:15:12 +000079
80#include "test.h"
plars865695b2001-08-27 22:15:12 +000081
robbiewd6df93c2002-05-20 20:31:17 +000082#define _XOPEN_SOURCE 500
plars865695b2001-08-27 22:15:12 +000083#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 Hrubisfdce7d52013-04-04 18:35:48 +020090char *TCID = "pwrite01";
91int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000092int fildes; /* file descriptor for tempfile */
93char *write_buf[NBUFS]; /* buffer to hold data to be written */
94
95void setup(); /* Main setup function of test */
96void cleanup(); /* cleanup function for the test */
subrata_modak56207ce2009-03-23 13:35:39 +000097void l_seek(int, off_t, int, off_t); /* function to call lseek() */
plars865695b2001-08-27 22:15:12 +000098void init_buffers(); /* function to initialize/allocate buffers */
99void check_file_contents(); /* function to verify the contents of file */
100
subrata_modak56207ce2009-03-23 13:35:39 +0000101int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000102{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200103 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200104 const char *msg;
plars865695b2001-08-27 22:15:12 +0000105 int nwrite; /* no. of bytes written by pwrite() */
subrata_modakbdbaec52009-02-26 12:14:51 +0000106
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800107 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800108 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000109
plars865695b2001-08-27 22:15:12 +0000110 setup();
111
plars865695b2001-08-27 22:15:12 +0000112 for (lc = 0; TEST_LOOPING(lc); lc++) {
113
Caspar Zhangd59a6592013-03-07 14:59:12 +0800114 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000115
subrata_modak4bb656a2009-02-26 12:02:09 +0000116 /*
117 * Call pwrite() to write K1 bytes of data (0's) at offset 0
plars865695b2001-08-27 22:15:12 +0000118 * 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_modak4bb656a2009-02-26 12:02:09 +0000133 * Now, lseek() to a non K boundary,
plars865695b2001-08-27 22:15:12 +0000134 * just to be different.
135 */
subrata_modak56207ce2009-03-23 13:35:39 +0000136 l_seek(fildes, K1 / 2, SEEK_SET, K1 / 2);
plars865695b2001-08-27 22:15:12 +0000137
138 /*
subrata_modak4bb656a2009-02-26 12:02:09 +0000139 * Again, pwrite() K1 of data (2's) at offset K2 of
plars865695b2001-08-27 22:15:12 +0000140 * 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_modak56207ce2009-03-23 13:35:39 +0000151 l_seek(fildes, 0, SEEK_CUR, K1 / 2);
plars865695b2001-08-27 22:15:12 +0000152
153 /* lseek() to an offset of K3. */
154 l_seek(fildes, K3, SEEK_SET, K3);
155
156 /*
subrata_modak4bb656a2009-02-26 12:02:09 +0000157 * Using write(), write of K1 of data (3's) which
158 * should take place at an offset of K3,
plars865695b2001-08-27 22:15:12 +0000159 * 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 Hrubise38b9612014-06-02 17:20:57 +0200181 * 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.
plars865695b2001-08-27 22:15:12 +0000185 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200186 check_file_contents();
plars865695b2001-08-27 22:15:12 +0000187
188 /* reset to offset 0 in case we are looping */
189 l_seek(fildes, 0, SEEK_SET, 0);
190
Garrett Cooper2c282152010-12-16 00:55:50 -0800191 }
192
plars865695b2001-08-27 22:15:12 +0000193 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800194 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800195}
plars865695b2001-08-27 22:15:12 +0000196
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 Frysingerc57fba52014-04-09 18:56:30 -0400203void setup(void)
plars865695b2001-08-27 22:15:12 +0000204{
Garrett Cooper2c282152010-12-16 00:55:50 -0800205
plars865695b2001-08-27 22:15:12 +0000206 tst_sig(NOFORK, DEF_HANDLER, cleanup);
207
plars865695b2001-08-27 22:15:12 +0000208 TEST_PAUSE;
209
210 /* Allocate/Initialize the write buffer with known data */
211 init_buffers();
212
plars865695b2001-08-27 22:15:12 +0000213 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 Frysingerc57fba52014-04-09 18:56:30 -0400230void init_buffers(void)
plars865695b2001-08-27 22:15:12 +0000231{
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 Hrubisd218f342014-09-23 13:14:56 +0200236 write_buf[count] = malloc(K1);
plars865695b2001-08-27 22:15:12 +0000237
subrata_modak56207ce2009-03-23 13:35:39 +0000238 if (write_buf[count] == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800239 tst_brkm(TBROK, NULL, "malloc() failed ");
plars865695b2001-08-27 22:15:12 +0000240 }
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_modak56207ce2009-03-23 13:35:39 +0000251void l_seek(int fdesc, off_t offset, int whence, off_t checkoff)
plars865695b2001-08-27 22:15:12 +0000252{
253 off_t offloc; /* offset ret. from lseek() */
254
255 if ((offloc = lseek(fdesc, offset, whence)) != checkoff) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800256 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_modak923b23f2009-11-02 13:57:16 +0000259 TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000260 }
261}
262
263/*
subrata_modak4bb656a2009-02-26 12:02:09 +0000264 * check_file_contents() - Check the contents of the file we wrote with
plars865695b2001-08-27 22:15:12 +0000265 * 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 Frysingerc57fba52014-04-09 18:56:30 -0400269void check_file_contents(void)
plars865695b2001-08-27 22:15:12 +0000270{
subrata_modak56207ce2009-03-23 13:35:39 +0000271 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() */
plars865695b2001-08-27 22:15:12 +0000274 char *read_buf; /* buffer to hold read data */
275
276 /* Allocate space for read buffer */
Cyril Hrubisd218f342014-09-23 13:14:56 +0200277 read_buf = malloc(K1);
plars865695b2001-08-27 22:15:12 +0000278 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 Gao354ebb42012-12-07 10:10:04 +0800287 tst_brkm(TBROK | TERRNO, cleanup,
288 "lseek() failed: offloc=%" PRId64,
289 (int64_t) offloc);
plars865695b2001-08-27 22:15:12 +0000290 }
291
292 /* Read the data from file into a buffer */
293 nread = read(fildes, read_buf, K1);
294 if (nread != K1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800295 tst_brkm(TBROK | TERRNO, cleanup,
296 "read() failed: nread=%d", nread);
plars865695b2001-08-27 22:15:12 +0000297 }
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 Frysingerc57fba52014-04-09 18:56:30 -0400323void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000324{
Cyril Hrubis605fa332015-02-04 13:11:20 +0100325 int count;
plars865695b2001-08-27 22:15:12 +0000326
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
plars865695b2001-08-27 22:15:12 +0000338 tst_rmdir();
339
Chris Dearmanec6edca2012-10-17 19:54:01 -0700340}