• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: lseek08
22  *
23  * Test Description:
24  *  Verify that, lseek() call succeeds to set the file pointer position
25  *  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:
29  *  lseek() should return the offset which is set to the file size measured
30  *  in bytes. read() attempt should fail with -1 return value.
31  * $
32  * 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,
44  *   	Verify the Functionality of system call
45  *      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>
54  *  lseek08 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
55  *     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"
80 
81 #define TEMP_FILE	"tmp_file"
82 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
83 
84 char *TCID = "lseek08";
85 int TST_TOTAL = 1;
86 int fildes;			/* file handle for temp file */
87 size_t file_size;		/* size of the temporary file */
88 
89 void setup();			/* Main setup function of test */
90 void cleanup();			/* cleanup function for the test */
91 
main(int ac,char ** av)92 int main(int ac, char **av)
93 {
94 	int lc;
95 	char read_buf[1];	/* data read from temp. file */
96 
97 	tst_parse_opts(ac, av, NULL, NULL);
98 
99 	setup();
100 
101 	for (lc = 0; TEST_LOOPING(lc); lc++) {
102 
103 		tst_count = 0;
104 
105 		/*
106 		 * Invoke lseek(2) to move the read/write file
107 		 * pointer/handle to the END of the file.
108 		 */
109 		TEST(lseek(fildes, 0, SEEK_END));
110 
111 		if (TEST_RETURN == -1) {
112 			tst_resm(TFAIL | TTERRNO,
113 				 "lseek of %s failed", TEMP_FILE);
114 			continue;
115 		}
116 		/*
117 		 * Check if the return value from lseek(2)
118 		 * is equal to the file_size.
119 		 */
120 		if (TEST_RETURN != file_size) {
121 			tst_resm(TFAIL, "lseek() returned incorrect "
122 				 "value %ld, expected %zu",
123 				 TEST_RETURN, file_size);
124 			continue;
125 		}
126 		/*
127 		 * The return value is okay, now attempt to read data
128 		 * from the file.  This should fail as the file pointer
129 		 * should be pointing to END OF FILE.
130 		 */
131 		read_buf[0] = '\0';
132 		if (read(fildes, &read_buf, sizeof(read_buf)) > 0) {
133 			tst_resm(TFAIL, "read() successful on %s",
134 				 TEMP_FILE);
135 		} else {
136 			tst_resm(TPASS, "Functionality of lseek() on "
137 				 "%s successful", TEMP_FILE);
138 		}
139 	}
140 
141 	cleanup();
142 	tst_exit();
143 }
144 
145 /*
146  * setup() - performs all ONE TIME setup for this test.
147  *	     Create a temporary directory and change directory to it.
148  *	     Create a test file under temporary directory and write some
149  *	     data into it.
150  *	     Get the size of the file using fstat().
151  */
setup(void)152 void setup(void)
153 {
154 	struct stat stat_buf;	/* struct. buffer for stat(2) */
155 	char write_buf[BUFSIZ];	/* buffer to hold data */
156 
157 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
158 
159 	TEST_PAUSE;
160 
161 	tst_tmpdir();
162 
163 	/* Get the data to be written to temporary file */
164 	strcpy(write_buf, "abcdefg\n");
165 
166 	/* Creat/open a temporary file under above directory */
167 	if ((fildes = open(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
168 		tst_brkm(TBROK | TERRNO, cleanup,
169 			 "open(%s, O_RDWR|O_CREAT, %#o) failed",
170 			 TEMP_FILE, FILE_MODE);
171 	}
172 
173 	/* Write data into temporary file */
174 	if (write(fildes, write_buf, strlen(write_buf)) <= 0) {
175 		tst_brkm(TBROK | TERRNO, cleanup,
176 			 "writing to %s failed", TEMP_FILE);
177 	}
178 
179 	/* Get the size of the file using fstat */
180 	if (fstat(fildes, &stat_buf) < 0) {
181 		tst_brkm(TBROK | TERRNO, cleanup,
182 			 "fstat of %s failed", TEMP_FILE);
183 	}
184 
185 	file_size = stat_buf.st_size;
186 }
187 
188 /*
189  * cleanup() - performs all ONE TIME cleanup for this test at
190  *             completion or premature exit.
191  *	       Remove the test directory and testfile created in the setup.
192  */
cleanup(void)193 void cleanup(void)
194 {
195 
196 	/* Close the temporary file created */
197 	if (close(fildes) < 0) {
198 		tst_brkm(TFAIL, NULL,
199 			 "close(%s) Failed, errno=%d : %s:",
200 			 TEMP_FILE, errno, strerror(errno));
201 	}
202 
203 	tst_rmdir();
204 
205 }
206