blob: 7213d4b0f1da2f04272442c08c462636198813e1 [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/*
nstrazfa31d552002-05-14 16:50:06 +000021 * Test Name: lseek10
plars865695b2001-08-27 22:15:12 +000022 *
23 * Test Description:
24 * Verify that,
25 * 1. lseek() returns -1 and sets errno to ESPIPE, if the file handle of
subrata_modakbdbaec52009-02-26 12:14:51 +000026 * the specified file is associated with a pipe, socket, or FIFO.
plars865695b2001-08-27 22:15:12 +000027 * 2. lseek() returns -1 and sets errno to EINVAL, if the 'Whence' argument
28 * is not a proper value.
29 * 3. lseek() returns -1 and sets errno to EBADF, if the file handle of
30 * the specified file is not valid.
31 *
32 * Expected Result:
33 * lseek() should fail with return value -1 and set expected errno.
34 *
35 * Algorithm:
36 * Setup:
37 * Setup signal handling.
38 * Create temporary directory.
39 * Pause for SIGUSR1 if option specified.
40 *
41 * Test:
42 * Loop if the proper options are given.
43 * Execute system call
44 * Check return code, if system call failed (return=-1)
45 * if errno set == expected errno
46 * Issue sys call fails with expected return value and errno.
47 * Otherwise,
48 * Issue sys call fails with unexpected errno.
49 * Otherwise,
50 * Issue sys call returns unexpected value.
51 *
52 * Cleanup:
53 * Print errno log and/or timing stats if options given
54 * Delete the temporary directory(s)/file(s) created.
55 *
56 * Usage: <for command-line>
nstrazfa31d552002-05-14 16:50:06 +000057 * lseek10 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
plars865695b2001-08-27 22:15:12 +000058 * where, -c n : Run n copies concurrently.
59 * -e : Turn on errno logging.
60 * -i n : Execute test n times.
61 * -I x : Execute test for x seconds.
62 * -P x : Pause for x seconds between iterations.
63 * -t : Turn on syscall timing.
64 *
65 * HISTORY
66 * 07/2001 Ported by Wayne Boyer
67 *
68 * RESTRICTIONS:
69 * None.
70 */
71
72#include <stdio.h>
73#include <unistd.h>
74#include <sys/types.h>
75#include <errno.h>
76#include <unistd.h>
77#include <fcntl.h>
78#include <utime.h>
79#include <string.h>
80#include <sys/stat.h>
81#include <signal.h>
82
83#include "test.h"
84#include "usctest.h"
85
86#define TEMP_FILE1 "tmp_file1"
87#define TEMP_FILE2 "tmp_file2"
88#define TEMP_FILE3 "tmp_file3"
89#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
90#define PIPE_MODE S_IFIFO | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
91#define SEEK_TOP 10
92
subrata_modak56207ce2009-03-23 13:35:39 +000093char *TCID = "lseek10"; /* Test program identifier. */
94int TST_TOTAL = 3; /* Total number of test cases. */
subrata_modak56207ce2009-03-23 13:35:39 +000095int exp_enos[] = { ESPIPE, EINVAL, EBADF, 0 };
plars865695b2001-08-27 22:15:12 +000096
97int no_setup();
98int setup1(); /* setup function to test lseek() for ESPIPE */
99int setup2(); /* setup function to test lseek() for EINVAL */
100int setup3(); /* setup function to test lseek() for EBADF */
101
102int fd1; /* file handle for testfile1 */
103int fd2; /* file handle for testfile2 */
104int fd3; /* file handle for testfile3 */
105
subrata_modak56207ce2009-03-23 13:35:39 +0000106struct test_case_t { /* test case struct. to hold ref. test cond's */
plars865695b2001-08-27 22:15:12 +0000107 int fd;
108 int Whence;
109 char *desc;
110 int exp_errno;
subrata_modak56207ce2009-03-23 13:35:39 +0000111 int (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +0000112} Test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000113 {
114 1, SEEK_SET, "'fd' associated with a pipe/fifo", ESPIPE, setup1}, {
115 2, SEEK_TOP, "'whence' argument is not valid", EINVAL, setup2}, {
116 3, SEEK_SET, "'fd' is not an open file descriptor", EBADF, setup3},
117 {
118 0, 0, NULL, 0, no_setup}
plars865695b2001-08-27 22:15:12 +0000119};
120
121void setup(); /* Main setup function of test */
122void cleanup(); /* cleanup function for the test */
123
subrata_modak56207ce2009-03-23 13:35:39 +0000124int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000125{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200126 int lc;
127 char *msg;
plars865695b2001-08-27 22:15:12 +0000128 int fildes; /* file handle for testfile */
129 int whence; /* position of file handle in the file */
130 char *test_desc; /* test specific error message */
131 int ind; /* counter to test different test conditions */
subrata_modak56207ce2009-03-23 13:35:39 +0000132
plars865695b2001-08-27 22:15:12 +0000133 /* Parse standard options given to run the test. */
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800134 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000135 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800136
plars865695b2001-08-27 22:15:12 +0000137 setup();
138
139 /* set the expected errnos... */
140 TEST_EXP_ENOS(exp_enos);
141
plars865695b2001-08-27 22:15:12 +0000142 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800143
Caspar Zhangd59a6592013-03-07 14:59:12 +0800144 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000145
146 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
147 fildes = Test_cases[ind].fd;
148 test_desc = Test_cases[ind].desc;
149 whence = Test_cases[ind].Whence;
subrata_modakbdbaec52009-02-26 12:14:51 +0000150
plars865695b2001-08-27 22:15:12 +0000151 /* Assign the 'fd' values appropriatly */
152 if (fildes == 1) {
153 fildes = fd1;
154 } else if (fildes == 2) {
155 fildes = fd2;
156 } else {
157 fildes = fd3;
158 }
subrata_modak4bb656a2009-02-26 12:02:09 +0000159 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000160 * Invoke lseek(2) to test different test conditions.
161 * Verify that it fails with -1 return value and
plars865695b2001-08-27 22:15:12 +0000162 * sets appropriate errno.
subrata_modak56207ce2009-03-23 13:35:39 +0000163 */
plars865695b2001-08-27 22:15:12 +0000164 TEST(lseek(fildes, 0, whence));
165
subrata_modak56207ce2009-03-23 13:35:39 +0000166 if (TEST_RETURN != (off_t) - 1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800167 tst_resm(TFAIL,
168 "lseek() returned %ld, expected "
plars865695b2001-08-27 22:15:12 +0000169 "-1, errno:%d", TEST_RETURN,
170 Test_cases[ind].exp_errno);
171 continue;
172 }
173 TEST_ERROR_LOG(TEST_ERRNO);
174 if (TEST_ERRNO == Test_cases[ind].exp_errno) {
175 tst_resm(TPASS, "lseek() fails, %s, errno:%d",
176 test_desc, TEST_ERRNO);
177 } else {
178 tst_resm(TFAIL, "lseek() fails, %s, errno:%d, "
179 "expected errno:%d", test_desc,
180 TEST_ERRNO, Test_cases[ind].exp_errno);
181 }
182 }
183 }
184
plars865695b2001-08-27 22:15:12 +0000185 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800186 tst_exit();
plars865695b2001-08-27 22:15:12 +0000187
Garrett Cooper2c282152010-12-16 00:55:50 -0800188}
plars865695b2001-08-27 22:15:12 +0000189
190/*
191 * setup() - performs all ONE TIME setup for this test.
192 * Create a temporary directory and change directory to it.
193 * Invoke individual test setup functions according to the order
194 * set in test struct. definition.
195 */
subrata_modak56207ce2009-03-23 13:35:39 +0000196void setup()
plars865695b2001-08-27 22:15:12 +0000197{
Garrett Cooper2c282152010-12-16 00:55:50 -0800198 int ind;
plars865695b2001-08-27 22:15:12 +0000199
plars865695b2001-08-27 22:15:12 +0000200 tst_sig(NOFORK, DEF_HANDLER, cleanup);
201
plars865695b2001-08-27 22:15:12 +0000202 TEST_PAUSE;
203
plars865695b2001-08-27 22:15:12 +0000204 tst_tmpdir();
205
206 /* call individual setup functions */
207 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
208 Test_cases[ind].setupfunc();
209 }
210}
211
212/*
213 * no_setup() - This is a dummy function which simply returns 0.
214 */
subrata_modak56207ce2009-03-23 13:35:39 +0000215int no_setup()
plars865695b2001-08-27 22:15:12 +0000216{
217 return 0;
218}
219
220/*
subrata_modak4bb656a2009-02-26 12:02:09 +0000221 * setup1() - setup function for a test condition for which lseek(2)
plars865695b2001-08-27 22:15:12 +0000222 * returns -1 and sets errno to ESPIPE.
223 * Creat a named pipe/fifo using mknod() and open it for
224 * reading/writing.
225 * This function returns 0 on success.
226 */
subrata_modak56207ce2009-03-23 13:35:39 +0000227int setup1()
plars865695b2001-08-27 22:15:12 +0000228{
229 /* Creat a named pipe/fifo using mknod() */
230 if (mknod(TEMP_FILE1, PIPE_MODE, 0) < 0) {
231 tst_brkm(TBROK, cleanup,
232 "mknod(%s, %#o, 0) Failed, errno=%d :%s",
233 TEMP_FILE1, FILE_MODE, errno, strerror(errno));
234 }
235
236 /* Open the named pipe/fifo for reading/writing */
237 if ((fd1 = open(TEMP_FILE1, O_RDWR)) < 0) {
238 tst_brkm(TBROK, cleanup,
239 "open(%s, O_RDWR) Failed, errno=%d, :%s",
240 TEMP_FILE1, errno, strerror(errno));
241 }
242
243 return 0;
244}
245
246/*
247 * setup2() - setup function for a test condition for which lseek(2)
248 * returns -1 and sets errno to EINVAL.
249 * Creat a temporary file for reading/writing and write some data
250 * into it.
251 * This function returns 0 on success.
252 */
subrata_modak56207ce2009-03-23 13:35:39 +0000253int setup2()
plars865695b2001-08-27 22:15:12 +0000254{
255 char write_buff[BUFSIZ]; /* buffer to hold data */
256
257 /* Get the data to be written to temporary file */
258 strcpy(write_buff, "abcdefg");
259
260 /* Creat/open a temporary file under above directory */
261 if ((fd2 = open(TEMP_FILE2, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
262 tst_brkm(TBROK, cleanup,
263 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s",
264 TEMP_FILE2, FILE_MODE, errno, strerror(errno));
265 }
266
267 /* Write data into temporary file */
subrata_modak56207ce2009-03-23 13:35:39 +0000268 if (write(fd2, write_buff, sizeof(write_buff)) <= 0) {
plars865695b2001-08-27 22:15:12 +0000269 tst_brkm(TBROK, cleanup,
270 "write(2) on %s Failed, errno=%d : %s",
271 TEMP_FILE2, errno, strerror(errno));
272 }
273
274 return 0;
275}
276
277/*
278 * setup3() - setup function for a test condition for which lseek(2)
279 * returns -1 and sets errno to EBADF.
280 * Creat a temporary file for reading/writing and close it.
281 * This function returns 0 on success.
282 */
subrata_modak56207ce2009-03-23 13:35:39 +0000283int setup3()
plars865695b2001-08-27 22:15:12 +0000284{
285 /* Creat/open a temporary file under above directory */
286 if ((fd3 = open(TEMP_FILE3, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
287 tst_brkm(TBROK, cleanup,
288 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s",
289 TEMP_FILE3, FILE_MODE, errno, strerror(errno));
290 }
291
292 /* Close the temporary file created above */
293 if (close(fd3) < 0) {
294 tst_brkm(TBROK, cleanup,
295 "close(%s) Failed, errno=%d : %s:",
296 TEMP_FILE3, errno, strerror(errno));
297 }
298
299 return 0;
300}
301
302/*
303 * cleanup() - performs all ONE TIME cleanup for this test at
304 * completion or premature exit.
305 * Remove the test directory and testfile(s) created in the setup.
306 */
subrata_modak56207ce2009-03-23 13:35:39 +0000307void cleanup()
plars865695b2001-08-27 22:15:12 +0000308{
309 /*
310 * print timing stats if that option was specified.
311 * print errno log if that option was specified.
312 */
313 TEST_CLEANUP;
314
subrata_modak56207ce2009-03-23 13:35:39 +0000315 /* Close the temporary file(s) created in setup1/setup2 */
plars865695b2001-08-27 22:15:12 +0000316 if (close(fd1) < 0) {
317 tst_brkm(TFAIL, NULL,
318 "close(%s) Failed, errno=%d : %s:",
319 TEMP_FILE1, errno, strerror(errno));
320 }
321 if (close(fd2) < 0) {
322 tst_brkm(TFAIL, NULL,
323 "close(%s) Failed, errno=%d : %s:",
324 TEMP_FILE2, errno, strerror(errno));
325 }
326
plars865695b2001-08-27 22:15:12 +0000327 tst_rmdir();
328
Chris Dearmanec6edca2012-10-17 19:54:01 -0700329}