blob: a958f7dd4fb802884f30f4d809daf181763e03be [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"
75#include "usctest.h"
76
77#define TEMPFILE "pread_file"
78#define K1 1024
79#define NBUFS 4
80
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020081char *TCID = "pread02";
82int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000083
84char *write_buf[NBUFS]; /* buffer to hold data to be written */
85char *read_buf[NBUFS]; /* buffer to hold data read from file */
86int pfd[2]; /* pair of file descriptors */
Cyril Hrubis605fa332015-02-04 13:11:20 +010087int fd1;
plars865695b2001-08-27 22:15:12 +000088
89void setup(); /* Main setup function of test */
90void cleanup(); /* cleanup function for the test */
91int setup1(); /* setup function for test #1 */
92int setup2(); /* setup function for test #2 */
93int no_setup();
94void init_buffers(); /* function to initialize/allocate buffers */
95
subrata_modak56207ce2009-03-23 13:35:39 +000096struct test_case_t { /* test case struct. to hold ref. test cond's */
plars865695b2001-08-27 22:15:12 +000097 int fd;
98 size_t nb;
99 off_t offst;
100 char *desc;
101 int exp_errno;
subrata_modak56207ce2009-03-23 13:35:39 +0000102 int (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +0000103} Test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000104 {
105 1, K1, 0, "file descriptor is a PIPE or FIFO", ESPIPE, setup1}, {
106 2, K1, -1, "specified offset is -ve or invalid", EINVAL, setup2}, {
107 0, 0, 0, NULL, 0, no_setup}
plars865695b2001-08-27 22:15:12 +0000108};
109
subrata_modak56207ce2009-03-23 13:35:39 +0000110int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000111{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200112 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200113 const char *msg;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200114 int i;
plars865695b2001-08-27 22:15:12 +0000115 int fildes; /* file descriptor of test file */
116 size_t nbytes; /* no. of bytes to be written */
117 off_t offset; /* offset position in the specified file */
118 char *test_desc; /* test specific error message */
vapier22cf2042006-04-10 23:53:29 +0000119
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800120 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800121 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000122
plars865695b2001-08-27 22:15:12 +0000123 setup();
124
plars865695b2001-08-27 22:15:12 +0000125 for (lc = 0; TEST_LOOPING(lc); lc++) {
126
Caspar Zhangd59a6592013-03-07 14:59:12 +0800127 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000128
129 /* loop through the test cases */
subrata_modak56207ce2009-03-23 13:35:39 +0000130 for (i = 0; Test_cases[i].desc != NULL; i++) {
plars865695b2001-08-27 22:15:12 +0000131 fildes = Test_cases[i].fd;
132 test_desc = Test_cases[i].desc;
133 nbytes = Test_cases[i].nb;
134 offset = Test_cases[i].offst;
135
136 if (fildes == 1) {
137 fildes = pfd[0];
138 } else if (fildes == 2) {
139 fildes = fd1;
140 }
vapier22cf2042006-04-10 23:53:29 +0000141
plars865695b2001-08-27 22:15:12 +0000142 /*
143 * Call pread() with the specified file descriptor,
144 * no. of bytes to be read from specified offset.
145 * and verify that call should fail with appropriate
146 * errno set.
147 */
148 TEST(pread(fildes, read_buf[0], nbytes, offset));
149
150 /* Check for the return code of pread() */
151 if (TEST_RETURN != -1) {
plars24acd6e2002-02-20 23:45:31 +0000152 tst_brkm(TFAIL, cleanup, "pread() returned "
subrata_modak923b23f2009-11-02 13:57:16 +0000153 "%ld, expected -1, errno:%d",
subrata_modak56207ce2009-03-23 13:35:39 +0000154 TEST_RETURN, Test_cases[i].exp_errno);
plars865695b2001-08-27 22:15:12 +0000155 }
156
plars865695b2001-08-27 22:15:12 +0000157 /*
158 * Verify whether expected errno is set.
159 */
160 if (TEST_ERRNO == Test_cases[i].exp_errno) {
161 tst_resm(TPASS, "pread() fails, %s, errno:%d",
162 test_desc, TEST_ERRNO);
163 } else {
164 tst_resm(TFAIL, "pread() fails, %s, unexpected "
165 "errno:%d, expected:%d", test_desc,
166 TEST_ERRNO, Test_cases[i].exp_errno);
167 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800168 }
169 }
plars865695b2001-08-27 22:15:12 +0000170
plars865695b2001-08-27 22:15:12 +0000171 cleanup();
172
Garrett Cooper53740502010-12-16 00:04:01 -0800173 tst_exit();
plars865695b2001-08-27 22:15:12 +0000174}
175
176/*
177 * setup() - performs all ONE TIME setup for this test.
vapier22cf2042006-04-10 23:53:29 +0000178 * Initialize/allocate write buffer.
179 * Call individual setup function.
plars865695b2001-08-27 22:15:12 +0000180 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400181void setup(void)
plars865695b2001-08-27 22:15:12 +0000182{
Garrett Cooper2c282152010-12-16 00:55:50 -0800183 int i;
plars865695b2001-08-27 22:15:12 +0000184
plars865695b2001-08-27 22:15:12 +0000185 tst_sig(FORK, DEF_HANDLER, cleanup);
186
plars865695b2001-08-27 22:15:12 +0000187 TEST_PAUSE;
188
189 /* Allocate/Initialize the read/write buffer with known data */
190 init_buffers();
191
192 /* Call individual setup functions */
subrata_modak56207ce2009-03-23 13:35:39 +0000193 for (i = 0; Test_cases[i].desc != NULL; i++) {
plars865695b2001-08-27 22:15:12 +0000194 Test_cases[i].setupfunc();
195 }
196}
197
198/*
199 * no_setup() - This function simply returns.
200 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400201int no_setup(void)
plars865695b2001-08-27 22:15:12 +0000202{
203 return 0;
204}
205
206/*
207 * setup1() - setup function for a test condition for which pread()
vapier22cf2042006-04-10 23:53:29 +0000208 * returns -ve value and sets errno to ESPIPE.
plars865695b2001-08-27 22:15:12 +0000209 *
210 * Create an unnamed pipe using pipe().
211 * Write some known data to the write end of the pipe.
212 * return 0.
213 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400214int setup1(void)
plars865695b2001-08-27 22:15:12 +0000215{
216 /* Create a pair of unnamed pipe */
217 if (pipe(pfd) < 0) {
218 tst_brkm(TBROK, cleanup, "pipe() failed to create pair of "
219 "pipe, error:%d", errno);
220 }
221
222 /* Write known data (0's) of K1 bytes */
223 if (write(pfd[1], write_buf[0], K1) != K1) {
224 tst_brkm(TBROK, cleanup, "write to pipe failed: errno=%d : %s",
225 errno, strerror(errno));
226 }
227
228 return 0;
229}
230
231/*
232 * setup2 - setup function for a test condition for which pread()
vapier22cf2042006-04-10 23:53:29 +0000233 * returns -ve value and sets errno to EINVAL.
plars865695b2001-08-27 22:15:12 +0000234 *
235 * Create a temporary directory and a file under it.
236 * return 0.
237 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400238int setup2(void)
plars865695b2001-08-27 22:15:12 +0000239{
Garrett Cooper2c282152010-12-16 00:55:50 -0800240
plars865695b2001-08-27 22:15:12 +0000241 tst_tmpdir();
242
243 /* Creat a temporary file used for mapping */
244 if ((fd1 = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) {
245 tst_brkm(TBROK, cleanup, "open() on %s Failed, errno=%d : %s",
246 TEMPFILE, errno, strerror(errno));
247 }
248
249 return 0;
250}
251
252/*
253 * init_buffers() - allocate/Initialize write_buf array.
254 *
255 * Allocate read/write buffer.
256 * Fill the write buffer with the following data like,
257 * write_buf[0] has 0's, write_buf[1] has 1's, write_buf[2] has 2's
258 * write_buf[3] has 3's.
259 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400260void init_buffers(void)
plars865695b2001-08-27 22:15:12 +0000261{
262 int count; /* counter variable for loop */
263
264 /* Allocate and Initialize write buffer with known data */
265 for (count = 0; count < NBUFS; count++) {
Cyril Hrubisd218f342014-09-23 13:14:56 +0200266 write_buf[count] = malloc(K1);
267 read_buf[count] = malloc(K1);
plars865695b2001-08-27 22:15:12 +0000268
269 if ((write_buf[count] == NULL) || (read_buf[count] == NULL)) {
Garrett Cooper53740502010-12-16 00:04:01 -0800270 tst_brkm(TBROK, NULL,
plars865695b2001-08-27 22:15:12 +0000271 "malloc() failed on read/write buffers");
272 }
273 memset(write_buf[count], count, K1);
274 }
275}
276
277/*
278 * cleanup() - performs all ONE TIME cleanup for this test at
279 * completion or premature exit.
280 *
281 * Deallocate the memory allocated to read/write buffers.
282 * Close the temporary file.
283 * Remove the temporary directory created.
284 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400285void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000286{
Cyril Hrubis605fa332015-02-04 13:11:20 +0100287 int count;
plars865695b2001-08-27 22:15:12 +0000288
289 /* Free the memory allocated for the read/write buffer */
290 for (count = 0; count < NBUFS; count++) {
291 free(write_buf[count]);
292 free(read_buf[count]);
293 }
294
295 /* Close the temporary file created in setup2 */
296 if (close(fd1) < 0) {
297 tst_brkm(TBROK, NULL, "close() on %s Failed, errno=%d : %s",
298 TEMPFILE, errno, strerror(errno));
299 }
300
plars865695b2001-08-27 22:15:12 +0000301 tst_rmdir();
plars24acd6e2002-02-20 23:45:31 +0000302
Chris Dearmanec6edca2012-10-17 19:54:01 -0700303}