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: 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
26 * various specified offsets, comparing the data read aganist the data
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)
43 * Issue a FAIL message.
44 * Otherwise,
45 * Verify the Functionality of system call
46 * if successful,
47 * Issue Functionality-Pass message.
48 * Otherwise,
49 * Issue Functionality-Fail message.
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.
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.
62 *
63 * HISTORY
64 * 07/2001 Ported by Wayne Boyer
65 *
66 * RESTRICTIONS:
67 * None.
68 */
69
70 #define _XOPEN_SOURCE 500
71
72 #include <stdlib.h>
73 #include <errno.h>
74 #include <unistd.h>
75 #include <fcntl.h>
76 #include <inttypes.h>
77
78 #include "test.h"
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
87 char *TCID = "pread01";
88 int TST_TOTAL = 1;
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 */
96 void l_seek(int, off_t, int, off_t); /* function to call lseek() */
97 void init_buffers(); /* function to initialize/allocate buffers */
98 void compare_bufers(); /* function to compare o/p of pread/pwrite */
99
main(int ac,char ** av)100 int main(int ac, char **av)
101 {
102 int lc;
103 int nread; /* no. of bytes read by pread() */
104
105 tst_parse_opts(ac, av, NULL, NULL);
106
107 setup();
108
109 for (lc = 0; TEST_LOOPING(lc); lc++) {
110
111 /* Reset tst_count in case we are looping */
112 tst_count = 0;
113
114 /*
115 * Call pread() of K1 data (should be 2's) at offset K2.
116 */
117 nread = pread(fildes, read_buf[2], K1, K2);
118
119 /* Check for the return value of pread() */
120 if (nread != K1) {
121 tst_brkm(TFAIL, cleanup, "pread() at off. K2 failed: "
122 "nread=%d, error:%d", nread, errno);
123 }
124
125 /*
126 * We should still be at offset K4,
127 * which we were at the end of block 0.
128 */
129 l_seek(fildes, 0, SEEK_CUR, K4);
130
131 /* Now lseek() to offset 0. */
132 l_seek(fildes, 0, SEEK_SET, 0);
133
134 /* pread() K1 of data (should be 3's) at offset K3. */
135 nread = pread(fildes, read_buf[3], K1, K3);
136 if (nread != K1) {
137 tst_brkm(TFAIL, cleanup, "pread() at off. K3 failed: "
138 "nread=%d, error:%d", nread, errno);
139 }
140
141 /* We should still be at offset 0. */
142 l_seek(fildes, 0, SEEK_CUR, 0);
143
144 /*
145 * Do a normal read() of K1 data (should be 0's)
146 * which should take place at offset 0 and move the
147 * file pointer to an offset of K1.
148 */
149 if ((nread = read(fildes, read_buf[0], K1)) != K1) {
150 tst_brkm(TFAIL, cleanup, "read() at off. 0 failed: "
151 "nread=%d, errno=%d", nread, errno);
152 }
153
154 /* We should now be at an offset of K1. */
155 l_seek(fildes, 0, SEEK_CUR, K1);
156
157 /* pread() of K1 data (should be 1's) at offset K1. */
158 nread = pread(fildes, read_buf[1], K1, K1);
159 if (nread != K1) {
160 tst_brkm(TFAIL, cleanup, "pread() at off. K1 failed: "
161 "nread=%d, error:%d", nread, errno);
162 }
163
164 /* We should still be at offset K1. */
165 l_seek(fildes, 0, SEEK_CUR, K1);
166
167 /*
168 * Compare the read buffer data read
169 * with the data written to write buffer
170 * in the setup.
171 */
172 compare_bufers();
173
174 /* reset our location to offset K4 in case we are looping */
175 l_seek(fildes, K4, SEEK_SET, K4);
176 }
177
178 cleanup();
179 tst_exit();
180 }
181
182 /*
183 * setup() - performs all ONE TIME setup for this test.
184 *
185 * Initialize/allocate read/write buffers.
186 * Create a temporary directory and a file under it and
187 * write know data at different offset positions.
188 */
setup(void)189 void setup(void)
190 {
191 int nwrite = 0; /* no. of bytes written by pwrite() */
192
193 tst_sig(FORK, DEF_HANDLER, cleanup);
194
195 TEST_PAUSE;
196
197 /* Allocate/Initialize the read/write buffer with know data */
198 init_buffers();
199
200 tst_tmpdir();
201
202 /* Creat a temporary file used for mapping */
203 if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) {
204 tst_brkm(TBROK, cleanup, "open() on %s failed, errno=%d : %s",
205 TEMPFILE, errno, strerror(errno));
206 }
207
208 /* pwrite() K1 of data (0's) at offset 0 of temporary file */
209 if ((nwrite = pwrite(fildes, write_buf[0], K1, 0)) != K1) {
210 tst_brkm(TBROK, cleanup, "pwrite() failed to write on %s, "
211 "errno=%d : %s", TEMPFILE, errno, strerror(errno));
212 }
213
214 /* We should still be at offset 0. */
215 l_seek(fildes, 0, SEEK_CUR, 0);
216
217 /* Now, lseek() to a non K boundary, just to be different. */
218 l_seek(fildes, K1 / 2, SEEK_SET, K1 / 2);
219
220 /* Again, pwrite() K1 of data (2's) at offset K2 of temporary file */
221 if ((nwrite = pwrite(fildes, write_buf[2], K1, K2)) != K1) {
222 tst_brkm(TBROK, cleanup, "pwrite() failed to write at %d off. "
223 "on %s, errno=%d : %s", K2, TEMPFILE, errno,
224 strerror(errno));
225 }
226
227 /* We should still be at our non K boundary. */
228 l_seek(fildes, 0, SEEK_CUR, K1 / 2);
229
230 /* lseek() to an offset of K3. */
231 l_seek(fildes, K3, SEEK_SET, K3);
232
233 /*
234 * Using write(), write of K1 of data (3's) which should take
235 * place at an offset of K3, moving the file pointer to K4.
236 */
237 if ((nwrite = write(fildes, write_buf[3], K1)) != K1) {
238 tst_brkm(TBROK, cleanup, "write() failed: nwrite=%d, errno=%d "
239 ": %s", nwrite, errno, strerror(errno));
240 }
241
242 /* We should be at offset K4. */
243 l_seek(fildes, 0, SEEK_CUR, K4);
244
245 /* Again, pwrite() K1 of data (1's) at offset K1. */
246 if ((nwrite = pwrite(fildes, write_buf[1], K1, K1)) != K1) {
247 tst_brkm(TBROK, cleanup, "pwrite() failed to write at %d off. "
248 "on %s, errno=%d : %s", K1, TEMPFILE, errno,
249 strerror(errno));
250 }
251 }
252
253 /*
254 * init_buffers - allocates both write_buf and read_buf arrays.
255 *
256 * Allocate the read and write buffers.
257 * Fill the write buffer with the following data like,
258 * write_buf[0] has 0's, write_buf[1] has 1's, write_buf[2] has 2's
259 * write_buf[3] has 3's.
260 */
init_buffers(void)261 void init_buffers(void)
262 {
263 int count; /* counter variable for loop */
264
265 /* Allocate and Initialize read/write buffer */
266 for (count = 0; count < NBUFS; count++) {
267 write_buf[count] = malloc(K1);
268 read_buf[count] = malloc(K1);
269
270 if ((write_buf[count] == NULL) || (read_buf[count] == NULL)) {
271 tst_brkm(TBROK, NULL,
272 "malloc() failed on read/write buffers");
273 }
274 memset(write_buf[count], count, K1);
275 }
276 }
277
278 /*
279 * l_seek() - local front end to lseek().
280 *
281 * "checkoff" is the offset at which we believe we should be at.
282 * Used to validate pread/pwrite don't move the offset.
283 */
l_seek(int fdesc,off_t offset,int whence,off_t checkoff)284 void l_seek(int fdesc, off_t offset, int whence, off_t checkoff)
285 {
286 off_t offloc; /* offset ret. from lseek() */
287
288 if ((offloc = lseek(fdesc, offset, whence)) != checkoff) {
289 tst_resm(TWARN, "return = %" PRId64 ", expected %" PRId64,
290 (int64_t) offloc, (int64_t) checkoff);
291 tst_brkm(TBROK | TERRNO, cleanup, "lseek() on %s failed",
292 TEMPFILE);
293 }
294 }
295
296 /*
297 * compare_bufers() - Compare the contents of read buffer aganist the
298 * write buffer contents.
299 *
300 * The contents of the index of each buffer should be as follows:
301 * [0] has 0's, [1] has 1's, [2] has 2's, and [3] has 3's.
302 *
303 * This function does memcmp of read/write buffer and display message
304 * about the functionality of pread().
305 */
compare_bufers(void)306 void compare_bufers(void)
307 {
308 int count; /* index for the loop */
309 int err_flg = 0; /* flag to indicate error */
310
311 for (count = 0; count < NBUFS; count++) {
312 if (memcmp(write_buf[count], read_buf[count], K1) != 0) {
313 tst_resm(TFAIL, "read/write buffer data mismatch");
314 err_flg++;
315 }
316 }
317
318 /* If no erros, Test successful */
319 if (!err_flg) {
320 tst_resm(TPASS, "Functionality of pread() is correct");
321 }
322 }
323
324 /*
325 * cleanup() - performs all ONE TIME cleanup for this test at
326 * completion or premature exit.
327 *
328 * Deallocate the memory allocated to read/write buffers.
329 * Close the temporary file.
330 * Remove the temporary directory created.
331 */
cleanup(void)332 void cleanup(void)
333 {
334 int count;
335
336 /* Free the memory allocated for the read/write buffer */
337 for (count = 0; count < NBUFS; count++) {
338 free(write_buf[count]);
339 free(read_buf[count]);
340 }
341
342 /* Close the temporary file */
343 if (close(fildes) < 0) {
344 tst_brkm(TBROK, NULL, "close() on %s Failed, errno=%d : %s",
345 TEMPFILE, errno, strerror(errno));
346 }
347
348 tst_rmdir();
349
350 }
351