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: llseek01
22 *
23 * Test Description:
24 * Verify that, llseek() call succeeds to set the file pointer position
25 * to an offset larger than file size. Also, verify that any attempt
26 * to write to this location fails.
27 *
28 * Expected result:
29 * llseek() should return the offset from the beginning of the file measured
30 * in bytes. Attempt to write to the location ( > file size) should fail.
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 * llseek01 [-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 #ifndef _GNU_SOURCE
70 #define _GNU_SOURCE
71 #endif
72
73 #include <unistd.h>
74 #include <errno.h>
75 #include <unistd.h>
76 #include <fcntl.h>
77 #include <utime.h>
78 #include <string.h>
79 #include <signal.h>
80 #include <sys/resource.h>
81 #include <sys/stat.h>
82 #include <sys/types.h>
83 #include <inttypes.h>
84
85 #include "test.h"
86 #include "safe_macros.h"
87
88 #define TEMP_FILE "tmp_file"
89 #define FILE_MODE 0644
90
91 char *TCID = "llseek01";
92 int TST_TOTAL = 1;
93 char write_buff[BUFSIZ]; /* buffer to hold data */
94 int fildes; /* file handle for temp file */
95
96 void setup(); /* Main setup function of test */
97 void cleanup(); /* cleanup function for the test */
98
main(int ac,char ** av)99 int main(int ac, char **av)
100 {
101 int lc;
102 loff_t offset; /* Ret value from llseek */
103
104 tst_parse_opts(ac, av, NULL, NULL);
105
106 offset = -1;
107
108 setup();
109
110 for (lc = 0; TEST_LOOPING(lc); lc++) {
111
112 tst_count = 0;
113
114 /*
115 * set file size limit, seek to a file using llseek.
116 */
117 TEST(lseek64(fildes, (loff_t) (80 * BUFSIZ), SEEK_SET));
118
119 if (TEST_RETURN == (loff_t) - 1) {
120 tst_resm(TFAIL, "llseek on (%s) Failed, errno=%d : %s",
121 TEMP_FILE, TEST_ERRNO, strerror(TEST_ERRNO));
122 continue;
123 }
124
125 if (TEST_RETURN != (loff_t) (80 * BUFSIZ)) {
126 tst_resm(TFAIL, "llseek() returned incorrect "
127 "value %" PRId64 ", expected %d",
128 (int64_t) offset, BUFSIZ);
129 continue;
130 }
131
132 /*
133 * llseek() successful. Now attempt to write past
134 * file size limit.
135 */
136 if (write(fildes, write_buff, BUFSIZ) != -1) {
137 tst_brkm(TFAIL, cleanup, "write successful "
138 "after file size limit");
139 }
140
141 /* Seeking to end of last valid write */
142 offset = lseek64(fildes, (loff_t) BUFSIZ, SEEK_SET);
143 if (offset != (loff_t) BUFSIZ) {
144 tst_brkm(TFAIL, cleanup,
145 "llseek under file size limit");
146 }
147
148 /*
149 * llseek() successful. Now, attempt to write to
150 * file size limit.
151 */
152 if (write(fildes, write_buff, BUFSIZ) != BUFSIZ) {
153 tst_brkm(TFAIL, cleanup, "write failed to "
154 "write to file size limit");
155 }
156
157 /*
158 * Again, attempt to write past file size limit.
159 */
160 if (write(fildes, write_buff, BUFSIZ) != -1) {
161 tst_brkm(TFAIL, cleanup, "write past file "
162 "size limit successful");
163 }
164
165 tst_resm(TPASS, "Functionality of llseek() on %s "
166 "successful", TEMP_FILE);
167 }
168
169 cleanup();
170 tst_exit();
171 }
172
setup(void)173 void setup(void)
174 {
175 struct sigaction act; /* struct. to hold signal */
176 struct rlimit rlp; /* resource for file size limit */
177
178 tst_sig(NOFORK, DEF_HANDLER, cleanup);
179
180 TEST_PAUSE;
181
182 act.sa_handler = SIG_IGN;
183 act.sa_flags = 0;
184 sigemptyset(&act.sa_mask);
185 if (sigaction(SIGXFSZ, &act, NULL) == -1) {
186 tst_brkm(TFAIL, NULL, "sigaction() Failed to ignore SIGXFSZ");
187 }
188
189 tst_tmpdir();
190
191 /* Set limit low, argument is # bytes */
192 rlp.rlim_cur = rlp.rlim_max = 2 * BUFSIZ;
193
194 if (setrlimit(RLIMIT_FSIZE, &rlp) == -1)
195 tst_brkm(TBROK, cleanup,
196 "Cannot set max. file size using setrlimit");
197
198 /* Creat/open a temporary file under above directory */
199 if ((fildes = open(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
200 tst_brkm(TBROK, cleanup,
201 "open(%s, O_RDWR|O_CREAT, 0644) Failed, errno=%d :%s",
202 TEMP_FILE, errno, strerror(errno));
203 }
204
205 SAFE_WRITE(cleanup, 1, fildes, write_buff, BUFSIZ);
206 }
207
cleanup(void)208 void cleanup(void)
209 {
210 SAFE_CLOSE(NULL, fildes);
211
212 tst_rmdir();
213 }
214