blob: 20e6a87672e82126be0eb553bd98f5897ad54e38 [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: lseek09
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 current specified location, when 'whence' value is set to
26 * SEEK_CUR and the data read from the specified location should match
27 * the expected data.
28 *
29 * Expected Result:
subrata_modak4bb656a2009-02-26 12:02:09 +000030 * lseek() should return the specified offset from the beginning of the file
plars865695b2001-08-27 22:15:12 +000031 * measured in bytes. The data read from this location should match the
32 * expected data.
subrata_modak56207ce2009-03-23 13:35:39 +000033 *$
plars865695b2001-08-27 22:15:12 +000034 * 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)
44 * Log the errno and Issue a FAIL message.
45 * Otherwise,
subrata_modakbdbaec52009-02-26 12:14:51 +000046 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000047 * if successful,
48 * Issue Functionality-Pass message.
49 * Otherwise,
50 * Issue Functionality-Fail message.
51 * Cleanup:
52 * Print errno log and/or timing stats if options given
53 * Delete the temporary directory created.
54 *
55 * Usage: <for command-line>
nstrazfa31d552002-05-14 16:50:06 +000056 * lseek09 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000057 * where, -c n : Run n copies concurrently.
58 * -f : Turn off functionality Testing.
59 * -i n : Execute test n times.
60 * -I x : Execute test for x seconds.
61 * -P x : Pause for x seconds between iterations.
62 * -t : Turn on syscall timing.
63 *
64 * HISTORY
65 * 07/2001 Ported by Wayne Boyer
66 *
67 * RESTRICTIONS:
68 * None.
69 */
70
71#include <stdio.h>
72#include <unistd.h>
73#include <sys/types.h>
74#include <errno.h>
75#include <fcntl.h>
76#include <utime.h>
77#include <string.h>
78#include <sys/stat.h>
79#include <signal.h>
80
81#include "test.h"
plars865695b2001-08-27 22:15:12 +000082
83#define TEMP_FILE "tmp_file"
84#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
85
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020086char *TCID = "lseek09";
87int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000088int fildes; /* file handle for temp file */
89size_t file_size; /* total size of file after data write */
90
91void setup(); /* Main setup function of test */
92void cleanup(); /* cleanup function for the test */
93
subrata_modak56207ce2009-03-23 13:35:39 +000094int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000095{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020096 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020097 const char *msg;
plars865695b2001-08-27 22:15:12 +000098 char read_buf[BUFSIZ]; /* data read from temp. file */
subrata_modak56207ce2009-03-23 13:35:39 +000099
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800100 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000101 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800102
plars865695b2001-08-27 22:15:12 +0000103 setup();
104
plars865695b2001-08-27 22:15:12 +0000105 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800106
Caspar Zhangd59a6592013-03-07 14:59:12 +0800107 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000108
subrata_modak4bb656a2009-02-26 12:02:09 +0000109 /*
plars865695b2001-08-27 22:15:12 +0000110 * Invoke lseek(2) to set the file
111 * pointer/handle from the current location
112 * of the file descriptor + specified offset.
113 */
114 TEST(lseek(fildes, 1, SEEK_CUR));
115
subrata_modak56207ce2009-03-23 13:35:39 +0000116 if (TEST_RETURN == (off_t) - 1) {
plars865695b2001-08-27 22:15:12 +0000117 tst_resm(TFAIL, "lseek on (%s) Failed, errno=%d : %s",
118 TEMP_FILE, TEST_ERRNO, strerror(TEST_ERRNO));
119 continue;
120 }
121 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200122 * Check if the return value from lseek(2) is equal
123 * to the 3 positions from the beginning of the file.
124 * ie, 2 positions from lseek() in the setup +
125 * 1 position from above above.
plars865695b2001-08-27 22:15:12 +0000126 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200127 if (TEST_RETURN != 3) {
128 tst_resm(TFAIL, "lseek() returned incorrect "
129 "value %ld, expected 4", TEST_RETURN);
130 continue;
131 }
132 /*
133 * Read the data byte from this location.
134 */
135 memset(read_buf, 0, sizeof(read_buf));
136 if (read(fildes, &read_buf, (file_size - 3)) < 0) {
137 tst_brkm(TFAIL, cleanup,
138 "read() failed on %s, error=%d",
139 TEMP_FILE, errno);
plars865695b2001-08-27 22:15:12 +0000140 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200141 /*
142 * Check if read data contains
143 * expected characters
144 * From pos 4 ---> 'defg'.
145 */
146 if (strcmp(read_buf, "defg")) {
147 tst_resm(TFAIL, "Incorrect data read "
148 "from file %s", TEMP_FILE);
149 } else {
150 tst_resm(TPASS, "Functionality of "
151 "lseek() on %s successful",
152 TEMP_FILE);
153 }
plars865695b2001-08-27 22:15:12 +0000154 }
155
156 /* reset file pointer in case we are looping */
157 if (lseek(fildes, 2, SEEK_SET) == -1) {
158 tst_brkm(TBROK, cleanup, "lseek failed - could not "
159 "reset file pointer");
160 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800161 }
plars865695b2001-08-27 22:15:12 +0000162
plars865695b2001-08-27 22:15:12 +0000163 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800164 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800165}
plars865695b2001-08-27 22:15:12 +0000166
167/*
168 * setup() - performs all ONE TIME setup for this test.
169 * Create a temporary directory and change directory to it.
170 * Create a test file under temporary directory and write some
171 * data into it.
172 * Get the size of the file using fstat().
173 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400174void setup(void)
plars865695b2001-08-27 22:15:12 +0000175{
subrata_modak56207ce2009-03-23 13:35:39 +0000176 struct stat stat_buf; /* buffer to hold stat info. */
177 char write_buf[BUFSIZ]; /* buffer to hold data */
plars865695b2001-08-27 22:15:12 +0000178
plars865695b2001-08-27 22:15:12 +0000179 tst_sig(NOFORK, DEF_HANDLER, cleanup);
180
plars865695b2001-08-27 22:15:12 +0000181 TEST_PAUSE;
182
plars865695b2001-08-27 22:15:12 +0000183 tst_tmpdir();
184
185 /* Get the data to be written to temporary file */
186 strcpy(write_buf, "abcdefg");
187
188 /* Creat/open a temporary file under above directory */
189 if ((fildes = open(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
190 tst_brkm(TBROK, cleanup,
191 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s",
192 TEMP_FILE, FILE_MODE, errno, strerror(errno));
193 }
194
195 /* Write data into temporary file */
subrata_modak56207ce2009-03-23 13:35:39 +0000196 if (write(fildes, write_buf, strlen(write_buf)) <= 0) {
plars865695b2001-08-27 22:15:12 +0000197 tst_brkm(TBROK, cleanup, "write(2) on %s Failed, errno=%d : %s",
198 TEMP_FILE, errno, strerror(errno));
199 }
200
201 /* Get the temporary file information */
202 if (fstat(fildes, &stat_buf) < 0) {
203 tst_brkm(TBROK, cleanup, "fstat(2) on %s Failed, errno=%d : %s",
204 TEMP_FILE, errno, strerror(errno));
205 }
206
207 file_size = stat_buf.st_size;
208
209 /*
210 * Reset the file pointer position to the specified offset
211 * from the beginning of the file.
212 */
213 if (lseek(fildes, 2, SEEK_SET) != 2) {
214 tst_brkm(TBROK, cleanup,
215 "lseek() fails to set file ptr. to specified offset");
216 }
217}
218
219/*
220 * cleanup() - performs all ONE TIME cleanup for this test at
221 * completion or premature exit.
222 * Remove the test directory and testfile created in the setup.
223 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400224void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000225{
plars865695b2001-08-27 22:15:12 +0000226
227 /* Close the temporary file created */
228 if (close(fildes) < 0) {
229 tst_brkm(TFAIL, NULL, "close(%s) Failed, errno=%d : %s:",
230 TEMP_FILE, errno, strerror(errno));
231 }
232
plars865695b2001-08-27 22:15:12 +0000233 tst_rmdir();
234
Chris Dearmanec6edca2012-10-17 19:54:01 -0700235}