• 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: lseek06
22  *
23  * Test Description:
24  *  Verify that, lseek() call succeeds to set the file pointer position
25  *  to less  than  or equal to the file size, when a file is opened for
26  *  read or write.
27  *
28  * Expected Result:
29  *  lseek() should return the offset from the beginning of the file measured
30  *  in bytes. Also check if able to read valid data from this location.
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,
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  *  lseek06 [-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 OFFSET		4
82 #define TEMP_FILE	"tmp_file"
83 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
84 
85 char *TCID = "lseek06";
86 int TST_TOTAL = 1;
87 int fildes;			/* file handle for temp 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 by OFFSET bytes.
108 		 */
109 		TEST(lseek(fildes, OFFSET, SEEK_SET));
110 
111 		if (TEST_RETURN == (off_t) - 1) {
112 			tst_resm(TFAIL, "lseek on (%s) Failed, errno=%d : %s",
113 				 TEMP_FILE, TEST_ERRNO, strerror(TEST_ERRNO));
114 			continue;
115 		}
116 		/*
117 		 * Check if the return value from lseek(2)
118 		 * is equal to the specified OFFSET value.
119 		 */
120 		if (TEST_RETURN != OFFSET) {
121 			tst_resm(TFAIL, "lseek() returned incorrect "
122 				 "value %ld, expected %d",
123 				 TEST_RETURN, OFFSET);
124 			continue;
125 		}
126 		/*
127 		 * The return value is good, now check data.
128 		 * Read the data byte from this location.
129 		 */
130 		if (read(fildes, &read_buf, sizeof(read_buf)) < 0) {
131 			tst_brkm(TFAIL, cleanup, "read() failed "
132 				 "on %s, error=%d", TEMP_FILE, errno);
133 		} else {
134 			/*
135 			 * Check if read byte is the expected character.
136 			 * For pos 4 ---> 'e'
137 			 */
138 			if (read_buf[0] != 'e') {
139 				tst_resm(TFAIL, "Incorrect data read "
140 					 "from file %s", TEMP_FILE);
141 			} else {
142 				tst_resm(TPASS, "Functionality "
143 					 "of lseek() on %s "
144 					 "successful", TEMP_FILE);
145 			}
146 		}
147 	}
148 
149 	cleanup();
150 	tst_exit();
151 }
152 
153 /*
154  * setup() - performs all ONE TIME setup for this test.
155  *	     Create a temporary directory and change directory to it.
156  *	     Create a test file under temporary directory and write some
157  *	     data into it.
158  */
setup(void)159 void setup(void)
160 {
161 	char write_buf[BUFSIZ];	/* buffer to hold data */
162 
163 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
164 
165 	TEST_PAUSE;
166 
167 	tst_tmpdir();
168 
169 	/* Get the data to be written to temporary file */
170 	strcpy(write_buf, "abcdefg");
171 
172 	/* Creat/open a temporary file under above directory */
173 	if ((fildes = open(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
174 		tst_brkm(TBROK, cleanup,
175 			 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s",
176 			 TEMP_FILE, FILE_MODE, errno, strerror(errno));
177 	}
178 
179 	/* Write data into temporary file */
180 	if (write(fildes, write_buf, strlen(write_buf)) != strlen(write_buf)) {
181 		tst_brkm(TBROK, cleanup, "write(2) on %s Failed, errno=%d : %s",
182 			 TEMP_FILE, errno, strerror(errno));
183 	}
184 }
185 
186 /*
187  * cleanup() - performs all ONE TIME cleanup for this test at
188  *             completion or premature exit.
189  *	       Remove the test directory and testfile created in the setup.
190  */
cleanup(void)191 void cleanup(void)
192 {
193 
194 	/* Close the temporary file created */
195 	if (close(fildes) < 0) {
196 		tst_brkm(TFAIL, NULL, "close(%s) Failed, errno=%d : %s:",
197 			 TEMP_FILE, errno, strerror(errno));
198 	}
199 
200 	tst_rmdir();
201 
202 }
203