blob: b0431f951251e7a2879a438bd75919f91b198cba [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: pread02
22 *
23 * Test Description:
24 * Verify that,
25 * 1) pread() fails when attempted to read from an unnamed pipe.
26 * 2) pread() fails if the specified offset position was invalid.
27 *
28 * Expected Result:
29 * 1) pread() should return -1 and set errno to ESPIPE.
30 * 2) pread() should return -1 and set errno to EINVAL.
31 *
32 * Algorithm:
33 * Setup:
34 * Setup signal handling.
35 * Create a temporary directory.
36 * Pause for SIGUSR1 if option specified.
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 * if errno set == expected errno
43 * Issue sys call fails with expected return value and errno.
44 * Otherwise,
45 * Issue sys call fails with unexpected errno.
46 * Otherwise,
47 * Issue sys call returns unexpected value.
48 *
49 * Cleanup:
50 * Print errno log and/or timing stats if options given
51 * Delete the temporary directory(s)/file(s) created.
52 *
53 * Usage: <for command-line>
54 * pread02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
55 * where, -c n : Run n copies concurrently.
vapier22cf2042006-04-10 23:53:29 +000056 * -i n : Execute test n times.
57 * -I x : Execute test for x seconds.
58 * -P x : Pause for x seconds between iterations.
59 * -t : Turn on syscall timing.
plars865695b2001-08-27 22:15:12 +000060 *
61 * HISTORY
62 * 07/2001 Ported by Wayne Boyer
63 *
64 * RESTRICTIONS:
65 * None.
66 */
plars74948ad2002-11-14 16:16:14 +000067
68#define _XOPEN_SOURCE 500
69
plars865695b2001-08-27 22:15:12 +000070#include <errno.h>
71#include <unistd.h>
72#include <fcntl.h>
73
74#include "test.h"
plars865695b2001-08-27 22:15:12 +000075
76#define TEMPFILE "pread_file"
77#define K1 1024
78#define NBUFS 4
79
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020080char *TCID = "pread02";
81int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000082
83char *write_buf[NBUFS]; /* buffer to hold data to be written */
84char *read_buf[NBUFS]; /* buffer to hold data read from file */
85int pfd[2]; /* pair of file descriptors */
Cyril Hrubis605fa332015-02-04 13:11:20 +010086int fd1;
plars865695b2001-08-27 22:15:12 +000087
88void setup(); /* Main setup function of test */
89void cleanup(); /* cleanup function for the test */
90int setup1(); /* setup function for test #1 */
91int setup2(); /* setup function for test #2 */
92int no_setup();
93void init_buffers(); /* function to initialize/allocate buffers */
94
subrata_modak56207ce2009-03-23 13:35:39 +000095struct test_case_t { /* test case struct. to hold ref. test cond's */
plars865695b2001-08-27 22:15:12 +000096 int fd;
97 size_t nb;
98 off_t offst;
99 char *desc;
100 int exp_errno;
subrata_modak56207ce2009-03-23 13:35:39 +0000101 int (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +0000102} Test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000103 {
104 1, K1, 0, "file descriptor is a PIPE or FIFO", ESPIPE, setup1}, {
105 2, K1, -1, "specified offset is -ve or invalid", EINVAL, setup2}, {
106 0, 0, 0, NULL, 0, no_setup}
plars865695b2001-08-27 22:15:12 +0000107};
108
subrata_modak56207ce2009-03-23 13:35:39 +0000109int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000110{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200111 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200112 const char *msg;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200113 int i;
plars865695b2001-08-27 22:15:12 +0000114 int fildes; /* file descriptor of test file */
115 size_t nbytes; /* no. of bytes to be written */
116 off_t offset; /* offset position in the specified file */
117 char *test_desc; /* test specific error message */
vapier22cf2042006-04-10 23:53:29 +0000118
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800119 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800120 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000121
plars865695b2001-08-27 22:15:12 +0000122 setup();
123
plars865695b2001-08-27 22:15:12 +0000124 for (lc = 0; TEST_LOOPING(lc); lc++) {
125
Caspar Zhangd59a6592013-03-07 14:59:12 +0800126 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000127
128 /* loop through the test cases */
subrata_modak56207ce2009-03-23 13:35:39 +0000129 for (i = 0; Test_cases[i].desc != NULL; i++) {
plars865695b2001-08-27 22:15:12 +0000130 fildes = Test_cases[i].fd;
131 test_desc = Test_cases[i].desc;
132 nbytes = Test_cases[i].nb;
133 offset = Test_cases[i].offst;
134
135 if (fildes == 1) {
136 fildes = pfd[0];
137 } else if (fildes == 2) {
138 fildes = fd1;
139 }
vapier22cf2042006-04-10 23:53:29 +0000140
plars865695b2001-08-27 22:15:12 +0000141 /*
142 * Call pread() with the specified file descriptor,
143 * no. of bytes to be read from specified offset.
144 * and verify that call should fail with appropriate
145 * errno set.
146 */
147 TEST(pread(fildes, read_buf[0], nbytes, offset));
148
149 /* Check for the return code of pread() */
150 if (TEST_RETURN != -1) {
plars24acd6e2002-02-20 23:45:31 +0000151 tst_brkm(TFAIL, cleanup, "pread() returned "
subrata_modak923b23f2009-11-02 13:57:16 +0000152 "%ld, expected -1, errno:%d",
subrata_modak56207ce2009-03-23 13:35:39 +0000153 TEST_RETURN, Test_cases[i].exp_errno);
plars865695b2001-08-27 22:15:12 +0000154 }
155
plars865695b2001-08-27 22:15:12 +0000156 /*
157 * Verify whether expected errno is set.
158 */
159 if (TEST_ERRNO == Test_cases[i].exp_errno) {
160 tst_resm(TPASS, "pread() fails, %s, errno:%d",
161 test_desc, TEST_ERRNO);
162 } else {
163 tst_resm(TFAIL, "pread() fails, %s, unexpected "
164 "errno:%d, expected:%d", test_desc,
165 TEST_ERRNO, Test_cases[i].exp_errno);
166 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800167 }
168 }
plars865695b2001-08-27 22:15:12 +0000169
plars865695b2001-08-27 22:15:12 +0000170 cleanup();
171
Garrett Cooper53740502010-12-16 00:04:01 -0800172 tst_exit();
plars865695b2001-08-27 22:15:12 +0000173}
174
175/*
176 * setup() - performs all ONE TIME setup for this test.
vapier22cf2042006-04-10 23:53:29 +0000177 * Initialize/allocate write buffer.
178 * Call individual setup function.
plars865695b2001-08-27 22:15:12 +0000179 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400180void setup(void)
plars865695b2001-08-27 22:15:12 +0000181{
Garrett Cooper2c282152010-12-16 00:55:50 -0800182 int i;
plars865695b2001-08-27 22:15:12 +0000183
plars865695b2001-08-27 22:15:12 +0000184 tst_sig(FORK, DEF_HANDLER, cleanup);
185
plars865695b2001-08-27 22:15:12 +0000186 TEST_PAUSE;
187
188 /* Allocate/Initialize the read/write buffer with known data */
189 init_buffers();
190
191 /* Call individual setup functions */
subrata_modak56207ce2009-03-23 13:35:39 +0000192 for (i = 0; Test_cases[i].desc != NULL; i++) {
plars865695b2001-08-27 22:15:12 +0000193 Test_cases[i].setupfunc();
194 }
195}
196
197/*
198 * no_setup() - This function simply returns.
199 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400200int no_setup(void)
plars865695b2001-08-27 22:15:12 +0000201{
202 return 0;
203}
204
205/*
206 * setup1() - setup function for a test condition for which pread()
vapier22cf2042006-04-10 23:53:29 +0000207 * returns -ve value and sets errno to ESPIPE.
plars865695b2001-08-27 22:15:12 +0000208 *
209 * Create an unnamed pipe using pipe().
210 * Write some known data to the write end of the pipe.
211 * return 0.
212 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400213int setup1(void)
plars865695b2001-08-27 22:15:12 +0000214{
215 /* Create a pair of unnamed pipe */
216 if (pipe(pfd) < 0) {
217 tst_brkm(TBROK, cleanup, "pipe() failed to create pair of "
218 "pipe, error:%d", errno);
219 }
220
221 /* Write known data (0's) of K1 bytes */
222 if (write(pfd[1], write_buf[0], K1) != K1) {
223 tst_brkm(TBROK, cleanup, "write to pipe failed: errno=%d : %s",
224 errno, strerror(errno));
225 }
226
227 return 0;
228}
229
230/*
231 * setup2 - setup function for a test condition for which pread()
vapier22cf2042006-04-10 23:53:29 +0000232 * returns -ve value and sets errno to EINVAL.
plars865695b2001-08-27 22:15:12 +0000233 *
234 * Create a temporary directory and a file under it.
235 * return 0.
236 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400237int setup2(void)
plars865695b2001-08-27 22:15:12 +0000238{
Garrett Cooper2c282152010-12-16 00:55:50 -0800239
plars865695b2001-08-27 22:15:12 +0000240 tst_tmpdir();
241
242 /* Creat a temporary file used for mapping */
243 if ((fd1 = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) {
244 tst_brkm(TBROK, cleanup, "open() on %s Failed, errno=%d : %s",
245 TEMPFILE, errno, strerror(errno));
246 }
247
248 return 0;
249}
250
251/*
252 * init_buffers() - allocate/Initialize write_buf array.
253 *
254 * Allocate read/write buffer.
255 * Fill the write buffer with the following data like,
256 * write_buf[0] has 0's, write_buf[1] has 1's, write_buf[2] has 2's
257 * write_buf[3] has 3's.
258 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400259void init_buffers(void)
plars865695b2001-08-27 22:15:12 +0000260{
261 int count; /* counter variable for loop */
262
263 /* Allocate and Initialize write buffer with known data */
264 for (count = 0; count < NBUFS; count++) {
Cyril Hrubisd218f342014-09-23 13:14:56 +0200265 write_buf[count] = malloc(K1);
266 read_buf[count] = malloc(K1);
plars865695b2001-08-27 22:15:12 +0000267
268 if ((write_buf[count] == NULL) || (read_buf[count] == NULL)) {
Garrett Cooper53740502010-12-16 00:04:01 -0800269 tst_brkm(TBROK, NULL,
plars865695b2001-08-27 22:15:12 +0000270 "malloc() failed on read/write buffers");
271 }
272 memset(write_buf[count], count, K1);
273 }
274}
275
276/*
277 * cleanup() - performs all ONE TIME cleanup for this test at
278 * completion or premature exit.
279 *
280 * Deallocate the memory allocated to read/write buffers.
281 * Close the temporary file.
282 * Remove the temporary directory created.
283 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400284void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000285{
Cyril Hrubis605fa332015-02-04 13:11:20 +0100286 int count;
plars865695b2001-08-27 22:15:12 +0000287
288 /* Free the memory allocated for the read/write buffer */
289 for (count = 0; count < NBUFS; count++) {
290 free(write_buf[count]);
291 free(read_buf[count]);
292 }
293
294 /* Close the temporary file created in setup2 */
295 if (close(fd1) < 0) {
296 tst_brkm(TBROK, NULL, "close() on %s Failed, errno=%d : %s",
297 TEMPFILE, errno, strerror(errno));
298 }
299
plars865695b2001-08-27 22:15:12 +0000300 tst_rmdir();
plars24acd6e2002-02-20 23:45:31 +0000301
Chris Dearmanec6edca2012-10-17 19:54:01 -0700302}