blob: 73fbee5a6d6e09cdbf48aa43ebc37d842cce4c91 [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
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_modak4bb656a2009-02-26 12:02:09 +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,
46 * Verify the Functionality of system call
47 * 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"
82#include "usctest.h"
83
84#define TEMP_FILE "tmp_file"
85#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
86
nstrazfa31d552002-05-14 16:50:06 +000087char *TCID="lseek09"; /* Test program identifier. */
plars865695b2001-08-27 22:15:12 +000088int TST_TOTAL=1; /* Total number of test cases. */
89extern int Tst_count; /* Test Case counter for tst_* routines */
90int fildes; /* file handle for temp file */
91size_t file_size; /* total size of file after data write */
92
93void setup(); /* Main setup function of test */
94void cleanup(); /* cleanup function for the test */
95
96int
97main(int ac, char **av)
98{
99 int lc; /* loop counter */
plars865695b2001-08-27 22:15:12 +0000100 char *msg; /* message returned from parse_opts */
101 char read_buf[BUFSIZ]; /* data read from temp. file */
subrata_modak4bb656a2009-02-26 12:02:09 +0000102
plars865695b2001-08-27 22:15:12 +0000103 /* Parse standard options given to run the test. */
104 msg = parse_opts(ac, av, (option_t *) NULL, NULL);
105 if (msg != (char *) NULL) {
106 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
107 tst_exit();
108 }
109
110 /* Perform global setup for test */
111 setup();
112
113 /* Check looping state if -i option given */
114 for (lc = 0; TEST_LOOPING(lc); lc++) {
115 /* Reset Tst_count in case we are looping. */
116 Tst_count=0;
117
subrata_modak4bb656a2009-02-26 12:02:09 +0000118 /*
plars865695b2001-08-27 22:15:12 +0000119 * Invoke lseek(2) to set the file
120 * pointer/handle from the current location
121 * of the file descriptor + specified offset.
122 */
123 TEST(lseek(fildes, 1, SEEK_CUR));
124
125 /* check return code of lseek(2) */
126 if (TEST_RETURN == (off_t)-1) {
127 tst_resm(TFAIL, "lseek on (%s) Failed, errno=%d : %s",
128 TEMP_FILE, TEST_ERRNO, strerror(TEST_ERRNO));
129 continue;
130 }
131 /*
132 * Perform functional verification if test
133 * executed without (-f) option.
134 */
135 if (STD_FUNCTIONAL_TEST) {
136 /*
137 * Check if the return value from lseek(2) is equal
138 * to the 3 positions from the beginning of the file.
139 * ie, 2 positions from lseek() in the setup +
140 * 1 position from above above.
141 */
142 if (TEST_RETURN != 3) {
143 tst_resm(TFAIL, "lseek() returned incorrect "
144 "value %d, expected 4", TEST_RETURN);
145 continue;
146 }
147 /*
148 * Read the data byte from this location.
149 */
subrata_modak47a8f752007-07-09 07:00:47 +0000150 memset(read_buf, 0, sizeof(read_buf));
plars865695b2001-08-27 22:15:12 +0000151 if (read(fildes, &read_buf, (file_size - 3)) < 0) {
152 tst_brkm(TFAIL, cleanup,
153 "read() failed on %s, error=%d",
154 TEMP_FILE, errno);
155 } else {
156 /*
157 * Check if read data contains
158 * expected characters
159 * From pos 4 ---> 'defg'.
160 */
161 if (strcmp(read_buf, "defg")) {
162 tst_resm(TFAIL, "Incorrect data read "
163 "from file %s", TEMP_FILE);
164 } else {
165 tst_resm(TPASS, "Functionality of "
166 "lseek() on %s successful",
167 TEMP_FILE);
168 }
169 }
170 } else {
171 tst_resm(TPASS, "call succeeded");
172 }
173
174 /* reset file pointer in case we are looping */
175 if (lseek(fildes, 2, SEEK_SET) == -1) {
176 tst_brkm(TBROK, cleanup, "lseek failed - could not "
177 "reset file pointer");
178 }
179 } /* End for TEST_LOOPING */
180
181 /* Call cleanup() to undo setup done for the test. */
182 cleanup();
183
184 /*NOTREACHED*/
subrata_modak43337a32009-02-26 11:43:51 +0000185 return 0;
plars865695b2001-08-27 22:15:12 +0000186} /* End main */
187
188/*
189 * setup() - performs all ONE TIME setup for this test.
190 * Create a temporary directory and change directory to it.
191 * Create a test file under temporary directory and write some
192 * data into it.
193 * Get the size of the file using fstat().
194 */
subrata_modak4bb656a2009-02-26 12:02:09 +0000195void
plars865695b2001-08-27 22:15:12 +0000196setup()
197{
198 struct stat stat_buf; /* buffer to hold stat info. */
199 char write_buf[BUFSIZ]; /* buffer to hold data */
200
201 /* capture signals */
202 tst_sig(NOFORK, DEF_HANDLER, cleanup);
203
204 /* Pause if that option was specified */
205 TEST_PAUSE;
206
207 /* make a temp directory and cd to it */
208 tst_tmpdir();
209
210 /* Get the data to be written to temporary file */
211 strcpy(write_buf, "abcdefg");
212
213 /* Creat/open a temporary file under above directory */
214 if ((fildes = open(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
215 tst_brkm(TBROK, cleanup,
216 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s",
217 TEMP_FILE, FILE_MODE, errno, strerror(errno));
218 }
219
220 /* Write data into temporary file */
221 if(write(fildes, write_buf, strlen(write_buf)) <= 0) {
222 tst_brkm(TBROK, cleanup, "write(2) on %s Failed, errno=%d : %s",
223 TEMP_FILE, errno, strerror(errno));
224 }
225
226 /* Get the temporary file information */
227 if (fstat(fildes, &stat_buf) < 0) {
228 tst_brkm(TBROK, cleanup, "fstat(2) on %s Failed, errno=%d : %s",
229 TEMP_FILE, errno, strerror(errno));
230 }
231
232 file_size = stat_buf.st_size;
233
234 /*
235 * Reset the file pointer position to the specified offset
236 * from the beginning of the file.
237 */
238 if (lseek(fildes, 2, SEEK_SET) != 2) {
239 tst_brkm(TBROK, cleanup,
240 "lseek() fails to set file ptr. to specified offset");
241 }
242}
243
244/*
245 * cleanup() - performs all ONE TIME cleanup for this test at
246 * completion or premature exit.
247 * Remove the test directory and testfile created in the setup.
248 */
subrata_modak4bb656a2009-02-26 12:02:09 +0000249void
plars865695b2001-08-27 22:15:12 +0000250cleanup()
251{
252 /*
253 * print timing stats if that option was specified.
254 * print errno log if that option was specified.
255 */
256 TEST_CLEANUP;
257
258 /* Close the temporary file created */
259 if (close(fildes) < 0) {
260 tst_brkm(TFAIL, NULL, "close(%s) Failed, errno=%d : %s:",
261 TEMP_FILE, errno, strerror(errno));
262 }
263
264 /* Remove tmp dir and all files in it */
265 tst_rmdir();
266
267 /* exit with return code appropriate for results */
268 tst_exit();
269}