blob: 1c9366181774678ac24b7a5f52e1c60f501e8365 [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: llseek02
robbiew6a6069e2003-03-14 16:15:19 +000022 * Note that glibc exports the llseek syscall as lseek64.
plars865695b2001-08-27 22:15:12 +000023 *
24 * Test Description:
25 * Verify that,
26 * 1. llseek() returns -1 and sets errno to EINVAL, if the 'Whence' argument
27 * is not a proper value.
28 * 2. llseek() returns -1 and sets errno to EBADF, if the file handle of
29 * the specified file is not valid.
30 *
31 * Expected Result:
32 * llseek() should fail with return value -1 and set expected errno.
33 *
34 * Algorithm:
35 * Setup:
36 * Setup signal handling.
37 * Create temporary directory.
38 * Pause for SIGUSR1 if option specified.
39 *
40 * Test:
41 * Loop if the proper options are given.
42 * Execute system call
43 * Check return code, if system call failed (return=-1)
subrata_modak56207ce2009-03-23 13:35:39 +000044 * if errno set == expected errno
45 * Issue sys call fails with expected return value and errno.
46 * Otherwise,
plars865695b2001-08-27 22:15:12 +000047 * Issue sys call fails with unexpected errno.
48 * Otherwise,
49 * Issue sys call returns unexpected value.
50 *
51 * Cleanup:
52 * Print errno log and/or timing stats if options given
53 * Delete the temporary directory(s)/file(s) created.
54 *
55 * Usage: <for command-line>
56 * llseek02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
57 * where, -c n : Run n copies concurrently.
58 * -i n : Execute test n times.
59 * -I x : Execute test for x seconds.
60 * -P x : Pause for x seconds between iterations.
61 * -t : Turn on syscall timing.
62 *
63 * HISTORY
64 * 07/2001 Ported by Wayne Boyer
65 *
66 * RESTRICTIONS:
67 * None.
68 */
69
vapier5b31d1b2006-08-21 06:46:26 +000070#ifndef _GNU_SOURCE
Wanlong Gao354ebb42012-12-07 10:10:04 +080071#define _GNU_SOURCE
vapier5b31d1b2006-08-21 06:46:26 +000072#endif
73
plars865695b2001-08-27 22:15:12 +000074#include <stdio.h>
75#include <unistd.h>
76#include <sys/types.h>
77#include <errno.h>
78#include <fcntl.h>
79#include <utime.h>
80#include <string.h>
81#include <sys/stat.h>
82#include <signal.h>
83
84#include "test.h"
plars865695b2001-08-27 22:15:12 +000085
86#define TEMP_FILE1 "tmp_file1"
87#define TEMP_FILE2 "tmp_file2"
88#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
89#define SEEK_TOP 10
90
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020091char *TCID = "llseek02";
92int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000093
94int no_setup();
95int setup1(); /* setup function to test llseek() for EINVAL */
96int setup2(); /* setup function to test llseek() for EBADF */
97
98int fd1; /* file handle for testfile1 */
99int fd2; /* file handle for testfile2 */
100
subrata_modak56207ce2009-03-23 13:35:39 +0000101struct test_case_t { /* test case struct. to hold ref. test cond's */
plars865695b2001-08-27 22:15:12 +0000102 int fd;
103 int Whence;
104 char *desc;
105 int exp_errno;
subrata_modak56207ce2009-03-23 13:35:39 +0000106 int (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +0000107} Test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000108 {
109 1, SEEK_TOP, "'whence' argument is not valid", EINVAL, setup1}, {
110 2, SEEK_SET, "'fd' is not an open file descriptor", EBADF, setup2},
111 {
112 0, 0, NULL, 0, no_setup}
plars865695b2001-08-27 22:15:12 +0000113};
114
plars865695b2001-08-27 22:15:12 +0000115void setup(); /* Main setup function of test */
116void cleanup(); /* cleanup function for the test */
117
subrata_modak56207ce2009-03-23 13:35:39 +0000118int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000119{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200120 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200121 const char *msg;
plars865695b2001-08-27 22:15:12 +0000122 int fildes; /* file handle for testfile */
123 int whence; /* position of file handle in the file */
124 char *test_desc; /* test specific error message */
plars865695b2001-08-27 22:15:12 +0000125 int ind; /* counter to test different test conditions */
vapier59c097e2006-05-26 06:13:40 +0000126
Garrett Cooper45e285d2010-11-22 12:19:25 -0800127 msg = parse_opts(ac, av, NULL, NULL);
128 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000129 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800130
plars865695b2001-08-27 22:15:12 +0000131 }
132
plars865695b2001-08-27 22:15:12 +0000133 setup();
134
plars865695b2001-08-27 22:15:12 +0000135 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800136
Caspar Zhangd59a6592013-03-07 14:59:12 +0800137 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000138
139 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
140 fildes = Test_cases[ind].fd;
141 test_desc = Test_cases[ind].desc;
142 whence = Test_cases[ind].Whence;
vapier59c097e2006-05-26 06:13:40 +0000143
plars865695b2001-08-27 22:15:12 +0000144 /* Assign the 'fd' values appropriatly */
145 if (fildes == 1) {
146 fildes = fd1;
147 } else {
148 fildes = fd2;
149 }
150
vapier59c097e2006-05-26 06:13:40 +0000151 /*
152 * Invoke llseek(2) to test different test conditions.
153 * Verify that it fails with -1 return value and
plars865695b2001-08-27 22:15:12 +0000154 * sets appropriate errno.
vapier59c097e2006-05-26 06:13:40 +0000155 */
subrata_modak56207ce2009-03-23 13:35:39 +0000156 TEST(lseek64(fildes, (loff_t) 0, whence));
plars865695b2001-08-27 22:15:12 +0000157
subrata_modak56207ce2009-03-23 13:35:39 +0000158 if (TEST_RETURN != (loff_t) - 1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800159 tst_resm(TFAIL,
160 "llseek() returned %ld, expected"
plars865695b2001-08-27 22:15:12 +0000161 " -1, errno:%d", TEST_RETURN,
162 Test_cases[ind].exp_errno);
163 continue;
164 }
plars865695b2001-08-27 22:15:12 +0000165 if (TEST_ERRNO == Test_cases[ind].exp_errno) {
166 tst_resm(TPASS, "llseek() fails, %s, errno:%d",
167 test_desc, TEST_ERRNO);
168 } else {
169 tst_resm(TFAIL, "llseek() fails, %s, errno:%d, "
170 "expected errno:%d", test_desc,
171 TEST_ERRNO, Test_cases[ind].exp_errno);
172 }
173 }
174 }
175
plars865695b2001-08-27 22:15:12 +0000176 cleanup();
177
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800178 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800179}
plars865695b2001-08-27 22:15:12 +0000180
181/*
182 * setup() - performs all ONE TIME setup for this test.
vapier59c097e2006-05-26 06:13:40 +0000183 * Create a temporary directory and change directory to it.
184 * Invoke individual test setup functions according to the order
185 * set in test struct. definition.
plars865695b2001-08-27 22:15:12 +0000186 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400187void setup(void)
plars865695b2001-08-27 22:15:12 +0000188{
Garrett Cooper2c282152010-12-16 00:55:50 -0800189 int ind;
plars865695b2001-08-27 22:15:12 +0000190
plars865695b2001-08-27 22:15:12 +0000191 tst_sig(NOFORK, DEF_HANDLER, cleanup);
192
plars865695b2001-08-27 22:15:12 +0000193 TEST_PAUSE;
194
plars865695b2001-08-27 22:15:12 +0000195 tst_tmpdir();
196
197 /* call individual setup functions */
198 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
199 Test_cases[ind].setupfunc();
200 }
201}
202
203/*
204 * no_setup() - This is a dummy function which simply returns 0.
205 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400206int no_setup(void)
plars865695b2001-08-27 22:15:12 +0000207{
208 return 0;
209}
210
211/*
212 * setup1() - setup function for a test condition for which llseek(2)
vapier59c097e2006-05-26 06:13:40 +0000213 * returns -1 and sets errno to EINVAL.
214 * Creat a temporary file for reading/writing and write some data
215 * into it.
216 * This function returns 0 on success.
plars865695b2001-08-27 22:15:12 +0000217 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400218int setup1(void)
plars865695b2001-08-27 22:15:12 +0000219{
220 char write_buff[BUFSIZ]; /* buffer to hold data */
221
222 /* Get the data to be written to temporary file */
223 strcpy(write_buff, "abcdefg");
224
225 /* Creat/open a temporary file under above directory */
226 if ((fd1 = open(TEMP_FILE1, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
227 tst_brkm(TBROK, cleanup,
228 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s",
229 TEMP_FILE1, FILE_MODE, errno, strerror(errno));
230 }
231
232 /* Write data into temporary file */
vapier59c097e2006-05-26 06:13:40 +0000233 if (write(fd1, write_buff, sizeof(write_buff)) <= 0) {
plars865695b2001-08-27 22:15:12 +0000234 tst_brkm(TBROK, cleanup, "write(2) on %s Failed, errno=%d : %s",
235 TEMP_FILE1, errno, strerror(errno));
236 }
237
238 return 0;
239}
240
241/*
242 * setup2() - setup function for a test condition for which llseek(2)
vapier59c097e2006-05-26 06:13:40 +0000243 * returns -1 and sets errno to EBADF.
244 * Creat a temporary file for reading/writing and close it.
245 * This function returns 0 on success.
plars865695b2001-08-27 22:15:12 +0000246 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400247int setup2(void)
plars865695b2001-08-27 22:15:12 +0000248{
249 /* Creat/open a temporary file under above directory */
250 if ((fd2 = open(TEMP_FILE2, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
251 tst_brkm(TBROK, cleanup,
252 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s",
253 TEMP_FILE2, FILE_MODE, errno, strerror(errno));
254 }
255
256 /* Close the temporary file created above */
257 if (close(fd2) < 0) {
258 tst_brkm(TBROK, cleanup, "close(%s) Failed, errno=%d : %s:",
259 TEMP_FILE2, errno, strerror(errno));
260 }
261
262 return 0;
263}
subrata_modak56207ce2009-03-23 13:35:39 +0000264
plars865695b2001-08-27 22:15:12 +0000265/*
266 * cleanup() - performs all ONE TIME cleanup for this test at
267 * completion or premature exit.
vapier59c097e2006-05-26 06:13:40 +0000268 * Close the temporary file.
269 * Remove the test directory and testfile created in the setup.
plars865695b2001-08-27 22:15:12 +0000270 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400271void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000272{
plars865695b2001-08-27 22:15:12 +0000273
subrata_modak56207ce2009-03-23 13:35:39 +0000274 /* Close the temporary file(s) created in setup1/setup2 */
plars865695b2001-08-27 22:15:12 +0000275 if (close(fd1) < 0) {
276 tst_brkm(TFAIL, NULL, "close(%s) Failed, errno=%d : %s:",
277 TEMP_FILE1, errno, strerror(errno));
278 }
279
plars865695b2001-08-27 22:15:12 +0000280 tst_rmdir();
281
Chris Dearmanec6edca2012-10-17 19:54:01 -0700282}