1/*
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/*
21 * Test Name: lseek09
22 *
23 * Test Description:
24 *  Verify that, lseek() call succeeds to set the file pointer position
25 *  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:
30 *  lseek() should return the specified offset from the beginning of the file
31 *  measured in bytes. The data read from this location should match the
32 *  expected data.
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)
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>
56 *  lseek09 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
57 *     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
83#define TEMP_FILE	"tmp_file"
84#define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
85
86char *TCID = "lseek09";
87int TST_TOTAL = 1;
88int 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
94int main(int ac, char **av)
95{
96	int lc;
97	char read_buf[BUFSIZ];	/* data read from temp. file */
98
99	tst_parse_opts(ac, av, NULL, NULL);
100
101	setup();
102
103	for (lc = 0; TEST_LOOPING(lc); lc++) {
104
105		tst_count = 0;
106
107		/*
108		 * Invoke lseek(2) to set the file
109		 * pointer/handle from the current location
110		 * of the file descriptor + specified offset.
111		 */
112		TEST(lseek(fildes, 1, SEEK_CUR));
113
114		if (TEST_RETURN == (off_t) - 1) {
115			tst_resm(TFAIL, "lseek on (%s) Failed, errno=%d : %s",
116				 TEMP_FILE, TEST_ERRNO, strerror(TEST_ERRNO));
117			continue;
118		}
119		/*
120		 * Check if the return value from lseek(2) is equal
121		 * to the 3 positions from the beginning of the file.
122		 * ie, 2 positions from lseek() in the setup +
123		 * 1 position from above above.
124		 */
125		if (TEST_RETURN != 3) {
126			tst_resm(TFAIL, "lseek() returned incorrect "
127				 "value %ld, expected 4", TEST_RETURN);
128			continue;
129		}
130		/*
131		 * Read the data byte from this location.
132		 */
133		memset(read_buf, 0, sizeof(read_buf));
134		if (read(fildes, &read_buf, (file_size - 3)) < 0) {
135			tst_brkm(TFAIL, cleanup,
136				 "read() failed on %s, error=%d",
137				 TEMP_FILE, errno);
138		} else {
139			/*
140			 * Check if read data contains
141			 * expected characters
142			 * From pos 4 ---> 'defg'.
143			 */
144			if (strcmp(read_buf, "defg")) {
145				tst_resm(TFAIL, "Incorrect data read "
146					 "from file %s", TEMP_FILE);
147			} else {
148				tst_resm(TPASS, "Functionality of "
149					 "lseek() on %s successful",
150					 TEMP_FILE);
151			}
152		}
153
154		/* reset file pointer in case we are looping */
155		if (lseek(fildes, 2, SEEK_SET) == -1) {
156			tst_brkm(TBROK, cleanup, "lseek failed - could not "
157				 "reset file pointer");
158		}
159	}
160
161	cleanup();
162	tst_exit();
163}
164
165/*
166 * setup() - performs all ONE TIME setup for this test.
167 *	     Create a temporary directory and change directory to it.
168 *	     Create a test file under temporary directory and write some
169 *	     data into it.
170 *	     Get the size of the file using fstat().
171 */
172void setup(void)
173{
174	struct stat stat_buf;	/* buffer to hold stat info. */
175	char write_buf[BUFSIZ];	/* buffer to hold data */
176
177	tst_sig(NOFORK, DEF_HANDLER, cleanup);
178
179	TEST_PAUSE;
180
181	tst_tmpdir();
182
183	/* Get the data to be written to temporary file */
184	strcpy(write_buf, "abcdefg");
185
186	/* Creat/open a temporary file under above directory */
187	if ((fildes = open(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
188		tst_brkm(TBROK, cleanup,
189			 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s",
190			 TEMP_FILE, FILE_MODE, errno, strerror(errno));
191	}
192
193	/* Write data into temporary file */
194	if (write(fildes, write_buf, strlen(write_buf)) <= 0) {
195		tst_brkm(TBROK, cleanup, "write(2) on %s Failed, errno=%d : %s",
196			 TEMP_FILE, errno, strerror(errno));
197	}
198
199	/* Get the temporary file information */
200	if (fstat(fildes, &stat_buf) < 0) {
201		tst_brkm(TBROK, cleanup, "fstat(2) on %s Failed, errno=%d : %s",
202			 TEMP_FILE, errno, strerror(errno));
203	}
204
205	file_size = stat_buf.st_size;
206
207	/*
208	 * Reset the file pointer position to the specified offset
209	 * from the beginning of the file.
210	 */
211	if (lseek(fildes, 2, SEEK_SET) != 2) {
212		tst_brkm(TBROK, cleanup,
213			 "lseek() fails to set file ptr. to specified offset");
214	}
215}
216
217/*
218 * cleanup() - performs all ONE TIME cleanup for this test at
219 *             completion or premature exit.
220 *	       Remove the test directory and testfile created in the setup.
221 */
222void cleanup(void)
223{
224
225	/* Close the temporary file created */
226	if (close(fildes) < 0) {
227		tst_brkm(TFAIL, NULL, "close(%s) Failed, errno=%d : %s:",
228			 TEMP_FILE, errno, strerror(errno));
229	}
230
231	tst_rmdir();
232
233}
234