blob: 200c58412e951cb6552f85e1b7a02c540f182e4f [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: lseek08
plars865695b2001-08-27 22:15:12 +000022 *
23 * Test Description:
subrata_modak4bb656a2009-02-26 12:02:09 +000024 * Verify that, lseek() call succeeds to set the file pointer position
plars865695b2001-08-27 22:15:12 +000025 * to the end of the file when 'whence' value set to SEEK_END and any
26 * attempts to read from that position should fail.
27 *
28 * Expected Result:
subrata_modak4bb656a2009-02-26 12:02:09 +000029 * lseek() should return the offset which is set to the file size measured
plars865695b2001-08-27 22:15:12 +000030 * in bytes. read() attempt should fail with -1 return value.
subrata_modak56207ce2009-03-23 13:35:39 +000031 * $
plars865695b2001-08-27 22:15:12 +000032 * Algorithm:
33 * Setup:
34 * Setup signal handling.
35 * Create 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 * Log the errno and Issue a FAIL message.
43 * Otherwise,
subrata_modakbdbaec52009-02-26 12:14:51 +000044 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000045 * if successful,
46 * Issue Functionality-Pass message.
47 * Otherwise,
48 * Issue Functionality-Fail message.
49 * Cleanup:
50 * Print errno log and/or timing stats if options given
51 * Delete the temporary directory created.
52 *
53 * Usage: <for command-line>
nstrazfa31d552002-05-14 16:50:06 +000054 * lseek08 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000055 * where, -c n : Run n copies concurrently.
56 * -f : Turn off functionality Testing.
57 * -i n : Execute test n times.
58 * -I x : Execute test for x seconds.
59 * -P x : Pause for x seconds between iterations.
60 * -t : Turn on syscall timing.
61 *
62 * HISTORY
63 * 07/2001 Ported by Wayne Boyer
64 *
65 * RESTRICTIONS:
66 * None.
67 */
68
69#include <stdio.h>
70#include <unistd.h>
71#include <sys/types.h>
72#include <errno.h>
73#include <fcntl.h>
74#include <utime.h>
75#include <string.h>
76#include <sys/stat.h>
77#include <signal.h>
78
79#include "test.h"
plars865695b2001-08-27 22:15:12 +000080
81#define TEMP_FILE "tmp_file"
82#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
83
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020084char *TCID = "lseek08";
85int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000086int fildes; /* file handle for temp file */
87size_t file_size; /* size of the temporary file */
88
89void setup(); /* Main setup function of test */
90void cleanup(); /* cleanup function for the test */
91
subrata_modak56207ce2009-03-23 13:35:39 +000092int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000093{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020094 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020095 const char *msg;
plars865695b2001-08-27 22:15:12 +000096 char read_buf[1]; /* data read from temp. file */
subrata_modak56207ce2009-03-23 13:35:39 +000097
Garrett Cooper7d0a4a52010-12-16 10:05:08 -080098 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +000099 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800100
plars865695b2001-08-27 22:15:12 +0000101 setup();
102
plars865695b2001-08-27 22:15:12 +0000103 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800104
Caspar Zhangd59a6592013-03-07 14:59:12 +0800105 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000106
subrata_modak4bb656a2009-02-26 12:02:09 +0000107 /*
plars865695b2001-08-27 22:15:12 +0000108 * Invoke lseek(2) to move the read/write file
109 * pointer/handle to the END of the file.
110 */
111 TEST(lseek(fildes, 0, SEEK_END));
112
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800113 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800114 tst_resm(TFAIL | TTERRNO,
115 "lseek of %s failed", TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000116 continue;
117 }
118 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200119 * Check if the return value from lseek(2)
120 * is equal to the file_size.
plars865695b2001-08-27 22:15:12 +0000121 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200122 if (TEST_RETURN != file_size) {
123 tst_resm(TFAIL, "lseek() returned incorrect "
124 "value %ld, expected %zu",
125 TEST_RETURN, file_size);
126 continue;
127 }
128 /*
129 * The return value is okay, now attempt to read data
130 * from the file. This should fail as the file pointer
131 * should be pointing to END OF FILE.
132 */
133 read_buf[0] = '\0';
134 if (read(fildes, &read_buf, sizeof(read_buf)) > 0) {
135 tst_resm(TFAIL, "read() successful on %s",
136 TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000137 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200138 tst_resm(TPASS, "Functionality of lseek() on "
139 "%s successful", TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000140 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800141 }
plars865695b2001-08-27 22:15:12 +0000142
plars865695b2001-08-27 22:15:12 +0000143 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800144 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800145}
plars865695b2001-08-27 22:15:12 +0000146
147/*
148 * setup() - performs all ONE TIME setup for this test.
149 * Create a temporary directory and change directory to it.
150 * Create a test file under temporary directory and write some
151 * data into it.
152 * Get the size of the file using fstat().
153 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400154void setup(void)
plars865695b2001-08-27 22:15:12 +0000155{
subrata_modak56207ce2009-03-23 13:35:39 +0000156 struct stat stat_buf; /* struct. buffer for stat(2) */
157 char write_buf[BUFSIZ]; /* buffer to hold data */
plars865695b2001-08-27 22:15:12 +0000158
plars865695b2001-08-27 22:15:12 +0000159 tst_sig(NOFORK, DEF_HANDLER, cleanup);
160
plars865695b2001-08-27 22:15:12 +0000161 TEST_PAUSE;
162
plars865695b2001-08-27 22:15:12 +0000163 tst_tmpdir();
164
165 /* Get the data to be written to temporary file */
166 strcpy(write_buf, "abcdefg\n");
167
168 /* Creat/open a temporary file under above directory */
169 if ((fildes = open(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800170 tst_brkm(TBROK | TERRNO, cleanup,
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800171 "open(%s, O_RDWR|O_CREAT, %#o) failed",
172 TEMP_FILE, FILE_MODE);
plars865695b2001-08-27 22:15:12 +0000173 }
174
175 /* Write data into temporary file */
subrata_modak56207ce2009-03-23 13:35:39 +0000176 if (write(fildes, write_buf, strlen(write_buf)) <= 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800177 tst_brkm(TBROK | TERRNO, cleanup,
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800178 "writing to %s failed", TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000179 }
180
181 /* Get the size of the file using fstat */
182 if (fstat(fildes, &stat_buf) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800183 tst_brkm(TBROK | TERRNO, cleanup,
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800184 "fstat of %s failed", TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000185 }
186
187 file_size = stat_buf.st_size;
188}
189
190/*
191 * cleanup() - performs all ONE TIME cleanup for this test at
192 * completion or premature exit.
193 * Remove the test directory and testfile created in the setup.
194 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400195void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000196{
plars865695b2001-08-27 22:15:12 +0000197
198 /* Close the temporary file created */
199 if (close(fildes) < 0) {
200 tst_brkm(TFAIL, NULL,
201 "close(%s) Failed, errno=%d : %s:",
202 TEMP_FILE, errno, strerror(errno));
203 }
204
plars865695b2001-08-27 22:15:12 +0000205 tst_rmdir();
206
Markos Chandrasf4539c62012-01-03 09:41:10 +0000207}