blob: 4ae1bceb195f2746c987d216a8bf5baeb2cc5c9a [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: lseek07
plars865695b2001-08-27 22:15:12 +000022 *
23 * Test Description:
24 * Verify that, lseek() call succeeds to set the file pointer position
25 * to more than the file size, when a file is opened for reading/writing.
26 *
27 * Expected Result:
28 * lseek() should return n+1, where n is the size of the file.
29 * Also when some data is written into this file it should start
30 * from that offset.
31 *
32 * Algorithm:
33 * Setup:
34 * Setup signal handling.
35 * Pause for SIGUSR1 if option specified.
36 * Create temporary directory.
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 * lseek07 [-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>
subrata_modak923b23f2009-11-02 13:57:16 +000078#include <inttypes.h>
plars865695b2001-08-27 22:15:12 +000079
80#include "test.h"
plars865695b2001-08-27 22:15:12 +000081
82#define TEMP_FILE "tmp_file"
83#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
84
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020085char *TCID = "lseek07";
86int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000087int fildes; /* file handle for temp file */
subrata_modak56207ce2009-03-23 13:35:39 +000088size_t file_size; /* size of temporary file */
plars865695b2001-08-27 22:15:12 +000089char write_buf1[BUFSIZ]; /* buffer to hold data */
90char write_buf2[BUFSIZ]; /* buffer to hold data */
91
92void setup(); /* Main setup function of test */
93void cleanup(); /* cleanup function for the test */
94
subrata_modak56207ce2009-03-23 13:35:39 +000095int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000096{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020097 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020098 const char *msg;
plars865695b2001-08-27 22:15:12 +000099 char read_buf[BUFSIZ]; /* data read from temp. file */
100 off_t offset; /* byte position in temporary file */
subrata_modak56207ce2009-03-23 13:35:39 +0000101
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800102 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000103 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800104
plars865695b2001-08-27 22:15:12 +0000105 setup();
106
plars865695b2001-08-27 22:15:12 +0000107 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800108
Caspar Zhangd59a6592013-03-07 14:59:12 +0800109 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000110
111 /* Set the offset position */
112 offset = file_size + (lc * strlen(write_buf2));
113
subrata_modak4bb656a2009-02-26 12:02:09 +0000114 /*
plars865695b2001-08-27 22:15:12 +0000115 * Invoke lseek(2) to move the write file
116 * pointer/handle by the specified offset value.
117 */
118 TEST(lseek(fildes, offset, SEEK_SET));
119
subrata_modak56207ce2009-03-23 13:35:39 +0000120 if (TEST_RETURN == (off_t) - 1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121 tst_resm(TFAIL | TTERRNO, "lseek on (%s) failed",
122 TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000123 continue;
124 }
125 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200126 * Check if the return value from lseek(2)
127 * is equal to the specified offset value.
plars865695b2001-08-27 22:15:12 +0000128 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200129 if (TEST_RETURN != offset) {
130 tst_resm(TFAIL, "lseek() returned "
131 "incorrect value %ld, expected "
132 "%" PRId64, TEST_RETURN,
133 (int64_t) offset);
134 continue;
135 }
136 /*
137 * The return value is okay, now write some data at
138 * the current offset position.
139 */
140 if (write(fildes, write_buf2, strlen(write_buf2)) !=
141 strlen(write_buf2)) {
142 tst_brkm(TFAIL | TERRNO, cleanup,
143 "write() failed to write additional data");
144 }
plars865695b2001-08-27 22:15:12 +0000145
Cyril Hrubise38b9612014-06-02 17:20:57 +0200146 /*
147 * Now close the file and open it again
148 * and read all of the data.
149 */
150 if (close(fildes) < 0) {
151 tst_brkm(TFAIL, cleanup, "close() on %s Failed,"
152 " errno = %d", TEMP_FILE, errno);
153 }
plars865695b2001-08-27 22:15:12 +0000154
Cyril Hrubise38b9612014-06-02 17:20:57 +0200155 /* Open the file again in read/write mode */
156 if ((fildes = open(TEMP_FILE, O_RDWR)) < 0) {
157 tst_brkm(TFAIL, cleanup, "Could not open the "
158 "%s readonly, error = %d",
159 TEMP_FILE, errno);
160 }
plars865695b2001-08-27 22:15:12 +0000161
Cyril Hrubise38b9612014-06-02 17:20:57 +0200162 /*
163 * Now read all of the data. The size should be the
164 * offset + strlen(write_buf2).
165 */
166 if (read(fildes, &read_buf, (offset +
167 strlen(write_buf2))) < 0) {
168 tst_brkm(TFAIL, cleanup, "read() failed on %s, "
169 "error=%d", TEMP_FILE, errno);
plars865695b2001-08-27 22:15:12 +0000170 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200171 /*
172 * Check data read is the complete data and not
173 * the only portion written.
174 */
175 if ((strncmp(read_buf, write_buf1,
176 strlen(write_buf1))) != 0) {
177 tst_brkm(TFAIL, cleanup,
178 "Incorrect data read #1 from "
179 "file %s", TEMP_FILE);
180 }
181 if ((strncmp(&read_buf[offset], write_buf2,
182 strlen(write_buf2))) != 0) {
183 tst_brkm(TFAIL, cleanup,
184 "Incorrect data read #2 from "
185 "file %s", TEMP_FILE);
186 }
187 tst_resm(TPASS, "Functionality of "
188 "lseek() on %s successful", TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000189 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800190 }
plars865695b2001-08-27 22:15:12 +0000191
plars865695b2001-08-27 22:15:12 +0000192 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800193 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800194}
plars865695b2001-08-27 22:15:12 +0000195
196/*
197 * setup() - performs all ONE TIME setup for this test.
198 * Create a temporary directory and change directory to it.
199 * Create a test file under temporary directory and write some
200 * data into it.
201 * Get the size of the file using fstat().
202 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400203void setup(void)
plars865695b2001-08-27 22:15:12 +0000204{
subrata_modak56207ce2009-03-23 13:35:39 +0000205 struct stat stat_buf; /* struct buffer for stat(2) */
plars865695b2001-08-27 22:15:12 +0000206
plars865695b2001-08-27 22:15:12 +0000207 tst_sig(NOFORK, DEF_HANDLER, cleanup);
208
plars865695b2001-08-27 22:15:12 +0000209 TEST_PAUSE;
210
plars865695b2001-08-27 22:15:12 +0000211 tst_tmpdir();
212
213 /* Get the data to be written to temporary file */
214 strcpy(write_buf1, "abcdefg");
215 strcpy(write_buf2, "ijk");
216
217 /* Creat/open a temporary file for writing under above directory */
218 if ((fildes = open(TEMP_FILE, O_WRONLY | O_CREAT, FILE_MODE)) == -1) {
219 tst_brkm(TBROK, cleanup,
220 "open(%s, O_WRONLY|O_CREAT, %#o) Failed, errno=%d :%s",
221 TEMP_FILE, FILE_MODE, errno, strerror(errno));
222 }
223
224 /* Write data into temporary file */
subrata_modak56207ce2009-03-23 13:35:39 +0000225 if (write(fildes, write_buf1, strlen(write_buf1)) != strlen(write_buf1)) {
plars865695b2001-08-27 22:15:12 +0000226 tst_brkm(TBROK, cleanup, "write(2) on %s Failed, errno=%d : %s",
227 TEMP_FILE, errno, strerror(errno));
228 }
229
230 /* Get the size of the temporary file after writing data */
231 if (fstat(fildes, &stat_buf) < 0) {
232 tst_brkm(TBROK, cleanup, "fstat() on %s Failed, errno=%d : %s",
233 TEMP_FILE, errno, strerror(errno));
234 }
235
236 file_size = stat_buf.st_size;
237}
238
239/*
240 * cleanup() - performs all ONE TIME cleanup for this test at
241 * completion or premature exit.
242 * Remove the test directory and testfile created in the setup.
243 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400244void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000245{
plars865695b2001-08-27 22:15:12 +0000246
247 /* Close the temporary file created */
248 if (close(fildes) < 0) {
249 tst_brkm(TFAIL, NULL, "close(%s) Failed, errno=%d : %s:",
250 TEMP_FILE, errno, strerror(errno));
251 }
252
plars865695b2001-08-27 22:15:12 +0000253 tst_rmdir();
254
Chris Dearmanec6edca2012-10-17 19:54:01 -0700255}