blob: 0616ad097c536a80c43121814c0629c8f0e55db8 [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"
plars865695b2001-08-27 22:15:12 +000084
85#define TEMP_FILE1 "tmp_file1"
86#define TEMP_FILE2 "tmp_file2"
87#define TEMP_FILE3 "tmp_file3"
88#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
89#define PIPE_MODE S_IFIFO | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
90#define SEEK_TOP 10
91
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020092char *TCID = "lseek10";
93int TST_TOTAL = 3;
plars865695b2001-08-27 22:15:12 +000094
95int no_setup();
96int setup1(); /* setup function to test lseek() for ESPIPE */
97int setup2(); /* setup function to test lseek() for EINVAL */
98int setup3(); /* setup function to test lseek() for EBADF */
99
100int fd1; /* file handle for testfile1 */
101int fd2; /* file handle for testfile2 */
102int fd3; /* file handle for testfile3 */
103
subrata_modak56207ce2009-03-23 13:35:39 +0000104struct test_case_t { /* test case struct. to hold ref. test cond's */
plars865695b2001-08-27 22:15:12 +0000105 int fd;
106 int Whence;
107 char *desc;
108 int exp_errno;
subrata_modak56207ce2009-03-23 13:35:39 +0000109 int (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +0000110} Test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000111 {
112 1, SEEK_SET, "'fd' associated with a pipe/fifo", ESPIPE, setup1}, {
113 2, SEEK_TOP, "'whence' argument is not valid", EINVAL, setup2}, {
114 3, SEEK_SET, "'fd' is not an open file descriptor", EBADF, setup3},
115 {
116 0, 0, NULL, 0, no_setup}
plars865695b2001-08-27 22:15:12 +0000117};
118
119void setup(); /* Main setup function of test */
120void cleanup(); /* cleanup function for the test */
121
subrata_modak56207ce2009-03-23 13:35:39 +0000122int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000123{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200124 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200125 const char *msg;
plars865695b2001-08-27 22:15:12 +0000126 int fildes; /* file handle for testfile */
127 int whence; /* position of file handle in the file */
128 char *test_desc; /* test specific error message */
129 int ind; /* counter to test different test conditions */
subrata_modak56207ce2009-03-23 13:35:39 +0000130
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800131 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000132 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800133
plars865695b2001-08-27 22:15:12 +0000134 setup();
135
plars865695b2001-08-27 22:15:12 +0000136 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800137
Caspar Zhangd59a6592013-03-07 14:59:12 +0800138 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000139
140 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
141 fildes = Test_cases[ind].fd;
142 test_desc = Test_cases[ind].desc;
143 whence = Test_cases[ind].Whence;
subrata_modakbdbaec52009-02-26 12:14:51 +0000144
plars865695b2001-08-27 22:15:12 +0000145 /* Assign the 'fd' values appropriatly */
146 if (fildes == 1) {
147 fildes = fd1;
148 } else if (fildes == 2) {
149 fildes = fd2;
150 } else {
151 fildes = fd3;
152 }
subrata_modak4bb656a2009-02-26 12:02:09 +0000153 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000154 * Invoke lseek(2) to test different test conditions.
155 * Verify that it fails with -1 return value and
plars865695b2001-08-27 22:15:12 +0000156 * sets appropriate errno.
subrata_modak56207ce2009-03-23 13:35:39 +0000157 */
plars865695b2001-08-27 22:15:12 +0000158 TEST(lseek(fildes, 0, whence));
159
subrata_modak56207ce2009-03-23 13:35:39 +0000160 if (TEST_RETURN != (off_t) - 1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800161 tst_resm(TFAIL,
162 "lseek() returned %ld, expected "
plars865695b2001-08-27 22:15:12 +0000163 "-1, errno:%d", TEST_RETURN,
164 Test_cases[ind].exp_errno);
165 continue;
166 }
plars865695b2001-08-27 22:15:12 +0000167 if (TEST_ERRNO == Test_cases[ind].exp_errno) {
168 tst_resm(TPASS, "lseek() fails, %s, errno:%d",
169 test_desc, TEST_ERRNO);
170 } else {
171 tst_resm(TFAIL, "lseek() fails, %s, errno:%d, "
172 "expected errno:%d", test_desc,
173 TEST_ERRNO, Test_cases[ind].exp_errno);
174 }
175 }
176 }
177
plars865695b2001-08-27 22:15:12 +0000178 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800179 tst_exit();
plars865695b2001-08-27 22:15:12 +0000180
Garrett Cooper2c282152010-12-16 00:55:50 -0800181}
plars865695b2001-08-27 22:15:12 +0000182
183/*
184 * setup() - performs all ONE TIME setup for this test.
185 * Create a temporary directory and change directory to it.
186 * Invoke individual test setup functions according to the order
187 * set in test struct. definition.
188 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400189void setup(void)
plars865695b2001-08-27 22:15:12 +0000190{
Garrett Cooper2c282152010-12-16 00:55:50 -0800191 int ind;
plars865695b2001-08-27 22:15:12 +0000192
plars865695b2001-08-27 22:15:12 +0000193 tst_sig(NOFORK, DEF_HANDLER, cleanup);
194
plars865695b2001-08-27 22:15:12 +0000195 TEST_PAUSE;
196
plars865695b2001-08-27 22:15:12 +0000197 tst_tmpdir();
198
199 /* call individual setup functions */
200 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
201 Test_cases[ind].setupfunc();
202 }
203}
204
205/*
206 * no_setup() - This is a dummy function which simply returns 0.
207 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400208int no_setup(void)
plars865695b2001-08-27 22:15:12 +0000209{
210 return 0;
211}
212
213/*
subrata_modak4bb656a2009-02-26 12:02:09 +0000214 * setup1() - setup function for a test condition for which lseek(2)
plars865695b2001-08-27 22:15:12 +0000215 * returns -1 and sets errno to ESPIPE.
216 * Creat a named pipe/fifo using mknod() and open it for
217 * reading/writing.
218 * This function returns 0 on success.
219 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400220int setup1(void)
plars865695b2001-08-27 22:15:12 +0000221{
222 /* Creat a named pipe/fifo using mknod() */
223 if (mknod(TEMP_FILE1, PIPE_MODE, 0) < 0) {
224 tst_brkm(TBROK, cleanup,
225 "mknod(%s, %#o, 0) Failed, errno=%d :%s",
226 TEMP_FILE1, FILE_MODE, errno, strerror(errno));
227 }
228
229 /* Open the named pipe/fifo for reading/writing */
230 if ((fd1 = open(TEMP_FILE1, O_RDWR)) < 0) {
231 tst_brkm(TBROK, cleanup,
232 "open(%s, O_RDWR) Failed, errno=%d, :%s",
233 TEMP_FILE1, errno, strerror(errno));
234 }
235
236 return 0;
237}
238
239/*
240 * setup2() - setup function for a test condition for which lseek(2)
241 * returns -1 and sets errno to EINVAL.
242 * Creat a temporary file for reading/writing and write some data
243 * into it.
244 * This function returns 0 on success.
245 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400246int setup2(void)
plars865695b2001-08-27 22:15:12 +0000247{
248 char write_buff[BUFSIZ]; /* buffer to hold data */
249
250 /* Get the data to be written to temporary file */
251 strcpy(write_buff, "abcdefg");
252
253 /* Creat/open a temporary file under above directory */
254 if ((fd2 = open(TEMP_FILE2, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
255 tst_brkm(TBROK, cleanup,
256 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s",
257 TEMP_FILE2, FILE_MODE, errno, strerror(errno));
258 }
259
260 /* Write data into temporary file */
subrata_modak56207ce2009-03-23 13:35:39 +0000261 if (write(fd2, write_buff, sizeof(write_buff)) <= 0) {
plars865695b2001-08-27 22:15:12 +0000262 tst_brkm(TBROK, cleanup,
263 "write(2) on %s Failed, errno=%d : %s",
264 TEMP_FILE2, errno, strerror(errno));
265 }
266
267 return 0;
268}
269
270/*
271 * setup3() - setup function for a test condition for which lseek(2)
272 * returns -1 and sets errno to EBADF.
273 * Creat a temporary file for reading/writing and close it.
274 * This function returns 0 on success.
275 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400276int setup3(void)
plars865695b2001-08-27 22:15:12 +0000277{
278 /* Creat/open a temporary file under above directory */
279 if ((fd3 = open(TEMP_FILE3, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
280 tst_brkm(TBROK, cleanup,
281 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s",
282 TEMP_FILE3, FILE_MODE, errno, strerror(errno));
283 }
284
285 /* Close the temporary file created above */
286 if (close(fd3) < 0) {
287 tst_brkm(TBROK, cleanup,
288 "close(%s) Failed, errno=%d : %s:",
289 TEMP_FILE3, errno, strerror(errno));
290 }
291
292 return 0;
293}
294
295/*
296 * cleanup() - performs all ONE TIME cleanup for this test at
297 * completion or premature exit.
298 * Remove the test directory and testfile(s) created in the setup.
299 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400300void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000301{
plars865695b2001-08-27 22:15:12 +0000302
subrata_modak56207ce2009-03-23 13:35:39 +0000303 /* Close the temporary file(s) created in setup1/setup2 */
plars865695b2001-08-27 22:15:12 +0000304 if (close(fd1) < 0) {
305 tst_brkm(TFAIL, NULL,
306 "close(%s) Failed, errno=%d : %s:",
307 TEMP_FILE1, errno, strerror(errno));
308 }
309 if (close(fd2) < 0) {
310 tst_brkm(TFAIL, NULL,
311 "close(%s) Failed, errno=%d : %s:",
312 TEMP_FILE2, errno, strerror(errno));
313 }
314
plars865695b2001-08-27 22:15:12 +0000315 tst_rmdir();
316
Chris Dearmanec6edca2012-10-17 19:54:01 -0700317}