blob: f425a8afbcb050f4299ac37204b9ab81f0d3f52d [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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
subrata_modak56207ce2009-03-23 13:35:39 +000022 * fcntl20.c
subrata_modak4bb656a2009-02-26 12:02:09 +000023 *
plars865695b2001-08-27 22:15:12 +000024 * DESCRIPTION
subrata_modak56207ce2009-03-23 13:35:39 +000025 * Check locking of regions of a file
plars865695b2001-08-27 22:15:12 +000026 *
27 * ALGORITHM
subrata_modak56207ce2009-03-23 13:35:39 +000028 * Test unlocking sections around a read lock
plars865695b2001-08-27 22:15:12 +000029 *
30 * USAGE
subrata_modak56207ce2009-03-23 13:35:39 +000031 * fcntl20
plars865695b2001-08-27 22:15:12 +000032 *
33 * HISTORY
34 * 07/2001 Ported by Wayne Boyer
35 *
36 * RESTRICTIONS
subrata_modak56207ce2009-03-23 13:35:39 +000037 * None
plars865695b2001-08-27 22:15:12 +000038 */
39
40#include <fcntl.h>
41#include <errno.h>
42#include <signal.h>
robbiew5aab8a72003-03-26 18:05:30 +000043#include <sys/stat.h>
44#include <sys/types.h>
mridgedb639212005-01-04 21:04:11 +000045#include <sys/wait.h>
subrata_modak923b23f2009-11-02 13:57:16 +000046#include <inttypes.h>
robbiew5aab8a72003-03-26 18:05:30 +000047#include "test.h"
Garrett Cooper0a643cb2010-12-21 11:21:19 -080048#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000049
50#define STRINGSIZE 27
51#define STRING "abcdefghijklmnopqrstuvwxyz\n"
52#define STOP 0xFFF0
53
54int parent_pipe[2];
55int child_pipe[2];
56int fd;
plarsa1e518d2002-09-09 18:23:52 +000057pid_t parent_pid, child_pid;
plars865695b2001-08-27 22:15:12 +000058
59void parent_put();
60void parent_get();
61void child_put();
62void child_get();
63void stop_child();
plarsa1e518d2002-09-09 18:23:52 +000064void compare_lock(struct flock *, short, short, int, int, pid_t);
plars865695b2001-08-27 22:15:12 +000065void unlock_file();
66void do_test(struct flock *, short, short, int, int);
67void catch_child();
68char *str_type();
69int do_lock(int, short, short, int, int);
70
nstrazfa31d552002-05-14 16:50:06 +000071char *TCID = "fcntl20";
plars865695b2001-08-27 22:15:12 +000072int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000073
74void setup(void);
75void cleanup(void);
76
77int fail = 0;
78
plars865695b2001-08-27 22:15:12 +000079/*
80 * setup
subrata_modak56207ce2009-03-23 13:35:39 +000081 * performs all ONE TIME setup for this test
plars865695b2001-08-27 22:15:12 +000082 */
Mike Frysingerc57fba52014-04-09 18:56:30 -040083void setup(void)
plars865695b2001-08-27 22:15:12 +000084{
85 char *buf = STRING;
robbiewdad22712003-04-16 19:14:05 +000086 char template[PATH_MAX];
robbiew1e4cf0c2005-02-24 17:03:42 +000087 struct sigaction act;
plars865695b2001-08-27 22:15:12 +000088
plars865695b2001-08-27 22:15:12 +000089 tst_sig(FORK, DEF_HANDLER, cleanup);
90
91 umask(0);
92
plars865695b2001-08-27 22:15:12 +000093 TEST_PAUSE;
94
plars865695b2001-08-27 22:15:12 +000095 parent_pid = getpid();
robbiew54f7a712003-04-16 19:20:30 +000096
Garrett Cooper0a643cb2010-12-21 11:21:19 -080097 SAFE_PIPE(NULL, parent_pipe);
98 SAFE_PIPE(NULL, child_pipe);
99
robbiew54f7a712003-04-16 19:20:30 +0000100 tst_tmpdir();
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800101
robbiewdad22712003-04-16 19:14:05 +0000102 snprintf(template, PATH_MAX, "fcntl20XXXXXX");
plars865695b2001-08-27 22:15:12 +0000103
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800104 if ((fd = mkstemp(template)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800105 tst_resm(TFAIL | TERRNO, "mkstemp failed");
plars865695b2001-08-27 22:15:12 +0000106
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800107 SAFE_WRITE(cleanup, 0, fd, buf, STRINGSIZE);
plars865695b2001-08-27 22:15:12 +0000108
robbiew1e4cf0c2005-02-24 17:03:42 +0000109 memset(&act, 0, sizeof(act));
110 act.sa_handler = catch_child;
111 sigemptyset(&act.sa_mask);
112 sigaddset(&act.sa_mask, SIGCLD);
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800113 if (sigaction(SIGCLD, &act, NULL) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800114 tst_brkm(TFAIL | TERRNO, cleanup, "SIGCLD signal setup failed");
plars865695b2001-08-27 22:15:12 +0000115}
116
Mike Frysingerc57fba52014-04-09 18:56:30 -0400117void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000118{
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800119 SAFE_CLOSE(NULL, fd);
120
robbiew54f7a712003-04-16 19:20:30 +0000121 tst_rmdir();
Garrett Cooper2c282152010-12-16 00:55:50 -0800122
plars865695b2001-08-27 22:15:12 +0000123}
124
Mike Frysingerc57fba52014-04-09 18:56:30 -0400125void do_child(void)
plars865695b2001-08-27 22:15:12 +0000126{
127 struct flock fl;
128
129 close(parent_pipe[1]);
130 close(child_pipe[0]);
subrata_modak56207ce2009-03-23 13:35:39 +0000131 while (1) {
plars865695b2001-08-27 22:15:12 +0000132 child_get(&fl);
133 if (fcntl(fd, F_GETLK, &fl) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800134 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
plars865695b2001-08-27 22:15:12 +0000135 fail = 1;
136 }
137 child_put(&fl);
138 }
139}
140
robbiew5aab8a72003-03-26 18:05:30 +0000141int do_lock(int cmd, short type, short whence, int start, int len)
plars865695b2001-08-27 22:15:12 +0000142{
143 struct flock fl;
144
145 fl.l_type = type;
146 fl.l_whence = whence;
147 fl.l_start = start;
148 fl.l_len = len;
subrata_modak56207ce2009-03-23 13:35:39 +0000149 return (fcntl(fd, cmd, &fl));
plars865695b2001-08-27 22:15:12 +0000150}
151
subrata_modak56207ce2009-03-23 13:35:39 +0000152void do_test(struct flock *fl, short type, short whence, int start, int len)
plars865695b2001-08-27 22:15:12 +0000153{
154 fl->l_type = type;
155 fl->l_whence = whence;
156 fl->l_start = start;
157 fl->l_len = len;
158 fl->l_pid = (short)0;
159
160 parent_put(fl);
161 parent_get(fl);
162}
163
164void
165compare_lock(struct flock *fl, short type, short whence, int start, int len,
plarsa1e518d2002-09-09 18:23:52 +0000166 pid_t pid)
plars865695b2001-08-27 22:15:12 +0000167{
168 if (fl->l_type != type) {
169 tst_resm(TFAIL, "lock type is wrong should be %s is %s",
170 str_type(type), str_type(fl->l_type));
171 fail = 1;
172 }
173
174 if (fl->l_whence != whence) {
175 tst_resm(TFAIL, "lock whence is wrong should be %d is %d",
176 whence, fl->l_whence);
177 fail = 1;
178 }
179
180 if (fl->l_start != start) {
181 tst_resm(TFAIL, "region starts in wrong place, should be"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800182 "%d is %" PRId64, start, (int64_t) fl->l_start);
plars865695b2001-08-27 22:15:12 +0000183 fail = 1;
184 }
185
186 if (fl->l_len != len) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187 tst_resm(TFAIL,
188 "region length is wrong, should be %d is %" PRId64,
189 len, (int64_t) fl->l_len);
plars865695b2001-08-27 22:15:12 +0000190 fail = 1;
191 }
192
193 if (fl->l_pid != pid) {
194 tst_resm(TFAIL, "locking pid is wrong, should be %d is %d",
195 pid, fl->l_pid);
196 fail = 1;
197 }
198}
199
Mike Frysingerc57fba52014-04-09 18:56:30 -0400200void unlock_file(void)
plars865695b2001-08-27 22:15:12 +0000201{
202 struct flock fl;
203
204 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 0, 0) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000205 tst_resm(TFAIL, "fcntl on file failed, errno =%d", errno);
plars865695b2001-08-27 22:15:12 +0000206 fail = 1;
207 }
208 do_test(&fl, F_WRLCK, 0, 0, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000209 compare_lock(&fl, (short)F_UNLCK, (short)0, 0, 0, (pid_t) 0);
plars865695b2001-08-27 22:15:12 +0000210}
211
subrata_modak56207ce2009-03-23 13:35:39 +0000212char *str_type(int type)
plars865695b2001-08-27 22:15:12 +0000213{
214 static char buf[20];
215
216 switch (type) {
217 case 1:
subrata_modak56207ce2009-03-23 13:35:39 +0000218 return ("F_RDLCK");
plars865695b2001-08-27 22:15:12 +0000219 case 2:
subrata_modak56207ce2009-03-23 13:35:39 +0000220 return ("F_WRLCK");
plars865695b2001-08-27 22:15:12 +0000221 case 3:
subrata_modak56207ce2009-03-23 13:35:39 +0000222 return ("F_UNLCK");
plars865695b2001-08-27 22:15:12 +0000223 default:
224 sprintf(buf, "BAD VALUE: %d", type);
subrata_modak56207ce2009-03-23 13:35:39 +0000225 return (buf);
plars865695b2001-08-27 22:15:12 +0000226 }
227}
228
subrata_modak56207ce2009-03-23 13:35:39 +0000229void parent_put(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000230{
231 if (write(parent_pipe[1], l, sizeof(*l)) != sizeof(*l)) {
232 tst_resm(TFAIL, "couldn't send message to child");
233 fail = 1;
234 }
235}
236
subrata_modak56207ce2009-03-23 13:35:39 +0000237void parent_get(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000238{
239 if (read(child_pipe[0], l, sizeof(*l)) != sizeof(*l)) {
240 tst_resm(TFAIL, "couldn't get message from child");
241 fail = 1;
242 }
243}
244
subrata_modak56207ce2009-03-23 13:35:39 +0000245void child_put(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000246{
247 if (write(child_pipe[1], l, sizeof(*l)) != sizeof(*l)) {
248 tst_resm(TFAIL, "couldn't send message to parent");
249 fail = 1;
250 }
251}
252
subrata_modak56207ce2009-03-23 13:35:39 +0000253void child_get(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000254{
255 if (read(parent_pipe[0], l, sizeof(*l)) != sizeof(*l)) {
256 tst_resm(TFAIL, "couldn't get message from parent");
257 cleanup();
258 } else if (l->l_type == (short)STOP) {
259 exit(0);
260 }
261}
262
Mike Frysingerc57fba52014-04-09 18:56:30 -0400263void stop_child(void)
plars865695b2001-08-27 22:15:12 +0000264{
265 struct flock fl;
266
Mike Frysingere61ddba2014-04-09 23:24:32 -0400267 signal(SIGCLD, SIG_DFL);
plars865695b2001-08-27 22:15:12 +0000268 fl.l_type = STOP;
269 parent_put(&fl);
270 wait(0);
271}
272
Mike Frysingerc57fba52014-04-09 18:56:30 -0400273void catch_child(void)
plars865695b2001-08-27 22:15:12 +0000274{
275 tst_resm(TFAIL, "Unexpected death of child process");
276 cleanup();
277}
robbiew5aab8a72003-03-26 18:05:30 +0000278
279int main(int ac, char **av)
280{
281 struct flock tl;
282
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200283 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200284 const char *msg;
robbiew5aab8a72003-03-26 18:05:30 +0000285
Garrett Cooper45e285d2010-11-22 12:19:25 -0800286 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800287 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew5aab8a72003-03-26 18:05:30 +0000288 }
robbiewd34d5812005-07-11 22:28:09 +0000289#ifdef UCLINUX
290 maybe_run_child(&do_child, "ddddd", &parent_pipe[0], &parent_pipe[1],
291 &child_pipe[0], &child_pipe[1], &fd);
292#endif
293
subrata_modak56207ce2009-03-23 13:35:39 +0000294 setup(); /* global setup */
robbiew5aab8a72003-03-26 18:05:30 +0000295
296 /* Check for looping state if -i option is given */
297 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800298 /* reset tst_count in case we are looping */
299 tst_count = 0;
robbiew5aab8a72003-03-26 18:05:30 +0000300
robbiewd34d5812005-07-11 22:28:09 +0000301 if ((child_pid = FORK_OR_VFORK()) == 0) { /* child */
302#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +0000303 if (self_exec
304 (av[0], "ddddd", parent_pipe[0], parent_pipe[1],
305 child_pipe[0], child_pipe[1], fd) < 0) {
robbiewd34d5812005-07-11 22:28:09 +0000306 tst_resm(TFAIL, "self_exec failed");
307 cleanup();
308 }
309#else
robbiew5aab8a72003-03-26 18:05:30 +0000310 do_child();
robbiewd34d5812005-07-11 22:28:09 +0000311#endif
robbiew5aab8a72003-03-26 18:05:30 +0000312 }
313
314 if (child_pid < 0) {
315 tst_resm(TFAIL, "Fork failed");
316 cleanup();
317 }
318
319 (void)close(parent_pipe[0]);
320 (void)close(child_pipe[1]);
321
mridgedb639212005-01-04 21:04:11 +0000322/* //block1: */
robbiew5aab8a72003-03-26 18:05:30 +0000323 tst_resm(TINFO, "Enter block 1");
324 /*
325 * Add a read lock to the middle of the file and unlock a
326 * section just before the lock
327 */
328 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000329 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
330 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000331 fail = 1;
332 }
333
334 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 5, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000335 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
336 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000337 fail = 1;
338 }
339
340 /*
341 * Test read lock
342 */
343 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
344 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
345
346 /*
347 * Test that the rest of the file is unlocked
348 */
349 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000350 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, (pid_t) 0);
robbiew5aab8a72003-03-26 18:05:30 +0000351
352 /*
353 * remove all the locks set above
354 */
355 unlock_file();
356
357 if (fail) {
358 tst_resm(TINFO, "Test block 1: FAILED");
359 } else {
360 tst_resm(TINFO, "Test block 1: PASSED");
361 }
362 tst_resm(TINFO, "Exit block 1");
363
mridgedb639212005-01-04 21:04:11 +0000364/* //block2: */
robbiew5aab8a72003-03-26 18:05:30 +0000365 tst_resm(TINFO, "Enter block 2");
366 fail = 0;
367 /*
368 * Set a read lock in the middle and do an unlock that
369 * ends at the first byte of the read lock.
370 */
371 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000372 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
373 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000374 fail = 1;
375 }
376
377 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 5, 6) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000378 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
379 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000380 fail = 1;
381 }
382
383 /*
384 * Test read lock
385 */
386 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
387 compare_lock(&tl, (short)F_RDLCK, (short)0, 11, 4, parent_pid);
388
389 /*
390 * Test to make sure the rest of the file is unlocked
391 */
392 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000393 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, (pid_t) 0);
robbiew5aab8a72003-03-26 18:05:30 +0000394
395 /*
396 * remove all the locks set above
397 */
398 unlock_file();
399
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800400 if (fail)
robbiew5aab8a72003-03-26 18:05:30 +0000401 tst_resm(TINFO, "Test block 2: FAILED");
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800402 else
robbiew5aab8a72003-03-26 18:05:30 +0000403 tst_resm(TINFO, "Test block 2: PASSED");
robbiew5aab8a72003-03-26 18:05:30 +0000404 tst_resm(TINFO, "Exit block 2");
405
mridgedb639212005-01-04 21:04:11 +0000406/* //block3: */
robbiew5aab8a72003-03-26 18:05:30 +0000407 tst_resm(TINFO, "Enter block 3");
408 fail = 0;
409
410 /*
411 * Set a read lock on the middle of the file and do an
412 * unlock that overlaps the front of the read
413 */
414 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000415 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
416 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000417 fail = 1;
418 }
419
420 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 5, 8) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000421 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
422 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000423 fail = 1;
424 }
425
426 /*
427 * Test the read lock
428 */
429 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
430 compare_lock(&tl, (short)F_RDLCK, (short)0, 13, 2, parent_pid);
431
432 /*
433 * Test to make sure the rest of the file is unlocked
434 */
435 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000436 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, (pid_t) 0);
robbiew5aab8a72003-03-26 18:05:30 +0000437
438 /*
439 * remove all the locks set above
440 */
441 unlock_file();
442
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800443 if (fail)
robbiew5aab8a72003-03-26 18:05:30 +0000444 tst_resm(TINFO, "Test block 3: FAILED");
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800445 else
robbiew5aab8a72003-03-26 18:05:30 +0000446 tst_resm(TINFO, "Test block 3: PASSED");
robbiew5aab8a72003-03-26 18:05:30 +0000447 tst_resm(TINFO, "Exit block 3");
448
mridgedb639212005-01-04 21:04:11 +0000449/* //block4: */
robbiew5aab8a72003-03-26 18:05:30 +0000450 tst_resm(TINFO, "Enter blcok 4");
451 fail = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000452
robbiew5aab8a72003-03-26 18:05:30 +0000453 /*
454 * Set a read lock in the middle of a file and unlock a
455 * section in the middle of it
456 */
457 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 10) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000458 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
459 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000460 fail = 1;
461 }
462
463 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 13, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000464 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
465 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000466 fail = 1;
467 }
468
469 /*
470 * Test the first read lock
471 */
472 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
473 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 3, parent_pid);
474
475 /*
476 * Test the second read lock
477 */
478 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
479 compare_lock(&tl, (short)F_RDLCK, (short)0, 18, 2, parent_pid);
480
481 /*
482 * Test to make sure the rest of the file is unlocked
483 */
484 do_test(&tl, (short)F_WRLCK, (short)0, 20, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000485 compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, (pid_t) 0);
robbiew5aab8a72003-03-26 18:05:30 +0000486
487 /*
488 * remove all the locks set above
489 */
490 unlock_file();
491
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800492 if (fail)
robbiew5aab8a72003-03-26 18:05:30 +0000493 tst_resm(TINFO, "Test block 4: FAILED");
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800494 else
robbiew5aab8a72003-03-26 18:05:30 +0000495 tst_resm(TINFO, "Test block 4: PASSED");
robbiew5aab8a72003-03-26 18:05:30 +0000496 tst_resm(TINFO, "Exit block 4");
497
mridgedb639212005-01-04 21:04:11 +0000498/* //block5: */
robbiew5aab8a72003-03-26 18:05:30 +0000499 tst_resm(TINFO, "Enter block 5");
500 fail = 0;
501
502 /*
503 * Set a read lock in the middle of the file and do a
504 * unlock that overlaps the end
505 */
506 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000507 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
508 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000509 fail = 1;
510 }
511
512 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 13, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000513 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
514 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000515 fail = 1;
516 }
517
518 /*
519 * Test the read lock
520 */
521 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
522 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 3, parent_pid);
523
524 /*
525 * Test to make sure the rest of the file is unlocked
526 */
527 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000528 compare_lock(&tl, (short)F_UNLCK, (short)0, 13, 0, (pid_t) 0);
robbiew5aab8a72003-03-26 18:05:30 +0000529
530 /*
531 * remove all the locks set above
532 */
533 unlock_file();
534
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800535 if (fail)
robbiew5aab8a72003-03-26 18:05:30 +0000536 tst_resm(TINFO, "Test block 5: FAILED");
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800537 else
robbiew5aab8a72003-03-26 18:05:30 +0000538 tst_resm(TINFO, "Test block 5: PASSED");
robbiew5aab8a72003-03-26 18:05:30 +0000539 tst_resm(TINFO, "Exit block 5");
540
mridgedb639212005-01-04 21:04:11 +0000541/* //block6: */
robbiew5aab8a72003-03-26 18:05:30 +0000542 tst_resm(TINFO, "Enter block 6");
543 fail = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000544
robbiew5aab8a72003-03-26 18:05:30 +0000545 /*
546 * Set read lock in the middle of the file and do an unlock
547 * starting at the last byte of the read lock
548 */
549 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000550 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
551 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000552 fail = 1;
553 }
554
555 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 14, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000556 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
557 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000558 fail = 1;
559 }
560
561 /*
562 * Test read lock
563 */
564 do_test(&tl, (short)F_WRLCK, (short)0, 10, 0);
565 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 4, parent_pid);
566
567 /*
568 * Test to make sure the end of the file is unlocked
569 */
570 do_test(&tl, (short)F_WRLCK, (short)0, 14, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000571 compare_lock(&tl, (short)F_UNLCK, (short)0, 14, 0, (pid_t) 0);
robbiew5aab8a72003-03-26 18:05:30 +0000572
573 /*
574 * remove all the locks set above
575 */
576 unlock_file();
577
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800578 if (fail)
robbiew5aab8a72003-03-26 18:05:30 +0000579 tst_resm(TINFO, "Test block 6: FAILED");
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800580 else
robbiew5aab8a72003-03-26 18:05:30 +0000581 tst_resm(TINFO, "Test block 6: PASSED");
robbiew5aab8a72003-03-26 18:05:30 +0000582 tst_resm(TINFO, "Exit block 6");
583
mridgedb639212005-01-04 21:04:11 +0000584/* //block7: */
robbiew5aab8a72003-03-26 18:05:30 +0000585 tst_resm(TINFO, "Enter block 7");
586 fail = 0;
587
588 /*
589 * Set a read lock at the middle of the file and do an
590 * unlock that starts at the byte past the end of the read
591 * lock
592 */
593 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000594 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
595 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000596 fail = 1;
597 }
598
599 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 16, 0) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000600 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
601 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000602 fail = 1;
603 }
604
605 /*
606 * Test the read lock
607 */
608 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
609 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
610
611 /*
612 * Test to make sure the rest of the file is unlocked
613 */
614 do_test(&tl, (short)F_WRLCK, (short)0, 16, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000615 compare_lock(&tl, (short)F_UNLCK, (short)0, 16, 0, (pid_t) 0);
robbiew5aab8a72003-03-26 18:05:30 +0000616
617 /*
618 * remove all the locks set above
619 */
620 unlock_file();
621
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800622 if (fail)
robbiew5aab8a72003-03-26 18:05:30 +0000623 tst_resm(TINFO, "Test block 7: FAILED");
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800624 else
robbiew5aab8a72003-03-26 18:05:30 +0000625 tst_resm(TINFO, "Test block 7: PASSED");
robbiew5aab8a72003-03-26 18:05:30 +0000626
627 tst_resm(TINFO, "Exit block 7");
628
629 stop_child();
robbiew5aab8a72003-03-26 18:05:30 +0000630 }
631 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800632 tst_exit();
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800633}