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: 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.
56  *             -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.
60  *
61  * HISTORY
62  *	07/2001 Ported by Wayne Boyer
63  *
64  * RESTRICTIONS:
65  *  None.
66  */
67 
68 #define _XOPEN_SOURCE 500
69 
70 #include <errno.h>
71 #include <unistd.h>
72 #include <fcntl.h>
73 
74 #include "test.h"
75 
76 #define TEMPFILE	"pread_file"
77 #define K1              1024
78 #define NBUFS           4
79 
80 char *TCID = "pread02";
81 int TST_TOTAL = 2;
82 
83 char *write_buf[NBUFS];		/* buffer to hold data to be written */
84 char *read_buf[NBUFS];		/* buffer to hold data read from file */
85 int pfd[2];			/* pair of file descriptors */
86 int fd1;
87 
88 void setup();			/* Main setup function of test */
89 void cleanup();			/* cleanup function for the test */
90 int setup1();			/* setup function for test #1 */
91 int setup2();			/* setup function for test #2 */
92 int no_setup();
93 void init_buffers();		/* function to initialize/allocate buffers */
94 
95 struct test_case_t {		/* test case struct. to hold ref. test cond's */
96 	int fd;
97 	size_t nb;
98 	off_t offst;
99 	char *desc;
100 	int exp_errno;
101 	int (*setupfunc) ();
102 } Test_cases[] = {
103 	{
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}
107 };
108 
main(int ac,char ** av)109 int main(int ac, char **av)
110 {
111 	int lc;
112 	int i;
113 	int fildes;		/* file descriptor of test file */
114 	size_t nbytes;		/* no. of bytes to be written */
115 	off_t offset;		/* offset position in the specified file */
116 	char *test_desc;	/* test specific error message */
117 
118 	tst_parse_opts(ac, av, NULL, NULL);
119 
120 	setup();
121 
122 	for (lc = 0; TEST_LOOPING(lc); lc++) {
123 
124 		tst_count = 0;
125 
126 		/* loop through the test cases */
127 		for (i = 0; Test_cases[i].desc != NULL; i++) {
128 			fildes = Test_cases[i].fd;
129 			test_desc = Test_cases[i].desc;
130 			nbytes = Test_cases[i].nb;
131 			offset = Test_cases[i].offst;
132 
133 			if (fildes == 1) {
134 				fildes = pfd[0];
135 			} else if (fildes == 2) {
136 				fildes = fd1;
137 			}
138 
139 			/*
140 			 * Call pread() with the specified file descriptor,
141 			 * no. of bytes to be read from specified offset.
142 			 * and verify that call should fail with appropriate
143 			 * errno set.
144 			 */
145 			TEST(pread(fildes, read_buf[0], nbytes, offset));
146 
147 			/* Check for the return code of pread() */
148 			if (TEST_RETURN != -1) {
149 				tst_brkm(TFAIL, cleanup, "pread() returned "
150 					 "%ld, expected -1, errno:%d",
151 					 TEST_RETURN, Test_cases[i].exp_errno);
152 			}
153 
154 			/*
155 			 * Verify whether expected errno is set.
156 			 */
157 			if (TEST_ERRNO == Test_cases[i].exp_errno) {
158 				tst_resm(TPASS, "pread() fails, %s, errno:%d",
159 					 test_desc, TEST_ERRNO);
160 			} else {
161 				tst_resm(TFAIL, "pread() fails, %s, unexpected "
162 					 "errno:%d, expected:%d", test_desc,
163 					 TEST_ERRNO, Test_cases[i].exp_errno);
164 			}
165 		}
166 	}
167 
168 	cleanup();
169 
170 	tst_exit();
171 }
172 
173 /*
174  * setup() - performs all ONE TIME setup for this test.
175  *           Initialize/allocate write buffer.
176  *           Call individual setup function.
177  */
setup(void)178 void setup(void)
179 {
180 	int i;
181 
182 	tst_sig(FORK, DEF_HANDLER, cleanup);
183 
184 	TEST_PAUSE;
185 
186 	/* Allocate/Initialize the read/write buffer with known data */
187 	init_buffers();
188 
189 	/* Call individual setup functions */
190 	for (i = 0; Test_cases[i].desc != NULL; i++) {
191 		Test_cases[i].setupfunc();
192 	}
193 }
194 
195 /*
196  * no_setup() - This function simply returns.
197  */
no_setup(void)198 int no_setup(void)
199 {
200 	return 0;
201 }
202 
203 /*
204  * setup1() - setup function for a test condition for which pread()
205  *            returns -ve value and sets errno to ESPIPE.
206  *
207  *  Create an unnamed pipe using pipe().
208  *  Write some known data to the write end of the pipe.
209  *  return 0.
210  */
setup1(void)211 int setup1(void)
212 {
213 	/* Create a pair of unnamed pipe */
214 	if (pipe(pfd) < 0) {
215 		tst_brkm(TBROK, cleanup, "pipe() failed to create pair of "
216 			 "pipe, error:%d", errno);
217 	}
218 
219 	/* Write known data (0's) of K1 bytes */
220 	if (write(pfd[1], write_buf[0], K1) != K1) {
221 		tst_brkm(TBROK, cleanup, "write to pipe failed: errno=%d : %s",
222 			 errno, strerror(errno));
223 	}
224 
225 	return 0;
226 }
227 
228 /*
229  * setup2 - setup function for a test condition for which pread()
230  *          returns -ve value and sets errno to EINVAL.
231  *
232  *  Create a temporary directory and a file under it.
233  *  return 0.
234  */
setup2(void)235 int setup2(void)
236 {
237 
238 	tst_tmpdir();
239 
240 	/* Creat a temporary file used for mapping */
241 	if ((fd1 = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) {
242 		tst_brkm(TBROK, cleanup, "open() on %s Failed, errno=%d : %s",
243 			 TEMPFILE, errno, strerror(errno));
244 	}
245 
246 	return 0;
247 }
248 
249 /*
250  * init_buffers() - allocate/Initialize write_buf array.
251  *
252  *  Allocate read/write buffer.
253  *  Fill the write buffer with the following data like,
254  *    write_buf[0] has 0's, write_buf[1] has 1's, write_buf[2] has 2's
255  *    write_buf[3] has 3's.
256  */
init_buffers(void)257 void init_buffers(void)
258 {
259 	int count;		/* counter variable for loop */
260 
261 	/* Allocate and Initialize write buffer with known data */
262 	for (count = 0; count < NBUFS; count++) {
263 		write_buf[count] = malloc(K1);
264 		read_buf[count] = malloc(K1);
265 
266 		if ((write_buf[count] == NULL) || (read_buf[count] == NULL)) {
267 			tst_brkm(TBROK, NULL,
268 				 "malloc() failed on read/write buffers");
269 		}
270 		memset(write_buf[count], count, K1);
271 	}
272 }
273 
274 /*
275  * cleanup() - performs all ONE TIME cleanup for this test at
276  *             completion or premature exit.
277  *
278  *  Deallocate the memory allocated to read/write buffers.
279  *  Close the temporary file.
280  *  Remove the temporary directory created.
281  */
cleanup(void)282 void cleanup(void)
283 {
284 	int count;
285 
286 	/* Free the memory allocated for the read/write buffer */
287 	for (count = 0; count < NBUFS; count++) {
288 		free(write_buf[count]);
289 		free(read_buf[count]);
290 	}
291 
292 	/* Close the temporary file created in setup2 */
293 	if (close(fd1) < 0) {
294 		tst_brkm(TBROK, NULL, "close() on %s Failed, errno=%d : %s",
295 			 TEMPFILE, errno, strerror(errno));
296 	}
297 
298 	tst_rmdir();
299 
300 }
301