blob: 2e7af299d989260ead6685fe06960f86f13f48b3 [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 * fcntl21.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * 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 changing lock sections around a read lock
plars865695b2001-08-27 22:15:12 +000029 *
30 * USAGE
subrata_modak56207ce2009-03-23 13:35:39 +000031 * fcntl21
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/types.h>
44#include <sys/stat.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"
48#include "usctest.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 = "fcntl21";
plars865695b2001-08-27 22:15:12 +000072int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000073
74void setup(void);
75void cleanup(void);
76int fail;
77
plars865695b2001-08-27 22:15:12 +000078/*
79 * setup
subrata_modak56207ce2009-03-23 13:35:39 +000080 * performs all ONE TIME setup for this test
plars865695b2001-08-27 22:15:12 +000081 */
Mike Frysingerc57fba52014-04-09 18:56:30 -040082void setup(void)
plars865695b2001-08-27 22:15:12 +000083{
84 char *buf = STRING;
robbiewdad22712003-04-16 19:14:05 +000085 char template[PATH_MAX];
robbiew1e4cf0c2005-02-24 17:03:42 +000086 struct sigaction act;
plars865695b2001-08-27 22:15:12 +000087
plars865695b2001-08-27 22:15:12 +000088 tst_sig(FORK, DEF_HANDLER, cleanup);
89
90 umask(0);
91
plars865695b2001-08-27 22:15:12 +000092 TEST_PAUSE;
93
94 pipe(parent_pipe);
95 pipe(child_pipe);
96 parent_pid = getpid();
robbiew54f7a712003-04-16 19:20:30 +000097
subrata_modakbdbaec52009-02-26 12:14:51 +000098 tst_tmpdir();
robbiew54f7a712003-04-16 19:20:30 +000099
100 snprintf(template, PATH_MAX, "fcntl21XXXXXX");
plars865695b2001-08-27 22:15:12 +0000101
robbiewdad22712003-04-16 19:14:05 +0000102 if ((fd = mkstemp(template)) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000103 tst_resm(TFAIL, "Couldn't open temp file! errno = %d", errno);
104 }
plars865695b2001-08-27 22:15:12 +0000105
subrata_modak56207ce2009-03-23 13:35:39 +0000106 if (write(fd, buf, STRINGSIZE) < 0) {
107 tst_resm(TFAIL, "Couldn't write to temp file! errno = %d",
108 errno);
109 }
plars865695b2001-08-27 22:15:12 +0000110
robbiew1e4cf0c2005-02-24 17:03:42 +0000111 memset(&act, 0, sizeof(act));
112 act.sa_handler = catch_child;
113 sigemptyset(&act.sa_mask);
114 sigaddset(&act.sa_mask, SIGCLD);
115 if ((sigaction(SIGCLD, &act, NULL)) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000116 tst_resm(TFAIL, "SIGCLD signal setup failed, errno: %d", errno);
plars865695b2001-08-27 22:15:12 +0000117 fail = 1;
118 }
119}
120
121/*
122 * cleanup()
subrata_modak56207ce2009-03-23 13:35:39 +0000123 * performs all ONE TIME cleanup for this test at completion or
124 * premature exit
plars865695b2001-08-27 22:15:12 +0000125 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400126void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000127{
plars865695b2001-08-27 22:15:12 +0000128
robbiew54f7a712003-04-16 19:20:30 +0000129 tst_rmdir();
130
plars865695b2001-08-27 22:15:12 +0000131}
132
Mike Frysingerc57fba52014-04-09 18:56:30 -0400133void do_child(void)
plars865695b2001-08-27 22:15:12 +0000134{
135 struct flock fl;
136
137 close(parent_pipe[1]);
138 close(child_pipe[0]);
subrata_modak56207ce2009-03-23 13:35:39 +0000139 while (1) {
plars865695b2001-08-27 22:15:12 +0000140 child_get(&fl);
141 if (fcntl(fd, F_GETLK, &fl) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000142 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
143 errno);
plars865695b2001-08-27 22:15:12 +0000144 fail = 1;
145 }
146 child_put(&fl);
147 }
148}
149
robbiew5aab8a72003-03-26 18:05:30 +0000150int do_lock(int cmd, short type, short whence, int start, int len)
plars865695b2001-08-27 22:15:12 +0000151{
152 struct flock fl;
153
154 fl.l_type = type;
155 fl.l_whence = whence;
156 fl.l_start = start;
157 fl.l_len = len;
subrata_modak56207ce2009-03-23 13:35:39 +0000158 return (fcntl(fd, cmd, &fl));
plars865695b2001-08-27 22:15:12 +0000159}
160
subrata_modak56207ce2009-03-23 13:35:39 +0000161void do_test(struct flock *fl, short type, short whence, int start, int len)
plars865695b2001-08-27 22:15:12 +0000162{
163 fl->l_type = type;
164 fl->l_whence = whence;
165 fl->l_start = start;
166 fl->l_len = len;
167 fl->l_pid = (short)0;
168
169 parent_put(fl);
170 parent_get(fl);
171}
172
173void
174compare_lock(struct flock *fl, short type, short whence, int start, int len,
plarsa1e518d2002-09-09 18:23:52 +0000175 pid_t pid)
plars865695b2001-08-27 22:15:12 +0000176{
177 if (fl->l_type != type) {
178 tst_resm(TFAIL, "lock type is wrong should be %s is %s",
179 str_type(type), str_type(fl->l_type));
180 fail = 1;
181 }
182
183 if (fl->l_whence != whence) {
184 tst_resm(TFAIL, "lock whence is wrong should be %d is %d",
185 whence, fl->l_whence);
186 fail = 1;
187 }
188
189 if (fl->l_start != start) {
190 tst_resm(TFAIL, "region starts in wrong place, should be"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800191 "%d is %" PRId64, start, (int64_t) fl->l_start);
plars865695b2001-08-27 22:15:12 +0000192 fail = 1;
193 }
194
195 if (fl->l_len != len) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800196 tst_resm(TFAIL,
197 "region length is wrong, should be %d is %" PRId64,
198 len, (int64_t) fl->l_len);
plars865695b2001-08-27 22:15:12 +0000199 fail = 1;
200 }
201
202 if (fl->l_pid != pid) {
203 tst_resm(TFAIL, "locking pid is wrong, should be %d is %d",
204 pid, fl->l_pid);
205 fail = 1;
206 }
207}
208
Mike Frysingerc57fba52014-04-09 18:56:30 -0400209void unlock_file(void)
plars865695b2001-08-27 22:15:12 +0000210{
211 struct flock fl;
212
213 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 0, 0) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000214 tst_resm(TFAIL, "fcntl on file failed, errno =%d", errno);
plars865695b2001-08-27 22:15:12 +0000215 fail = 1;
216 }
217 do_test(&fl, F_WRLCK, 0, 0, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000218 compare_lock(&fl, (short)F_UNLCK, (short)0, 0, 0, (pid_t) 0);
plars865695b2001-08-27 22:15:12 +0000219}
220
subrata_modak56207ce2009-03-23 13:35:39 +0000221char *str_type(int type)
plars865695b2001-08-27 22:15:12 +0000222{
223 static char buf[20];
224
225 switch (type) {
226 case 1:
subrata_modak56207ce2009-03-23 13:35:39 +0000227 return ("F_RDLCK");
plars865695b2001-08-27 22:15:12 +0000228 case 2:
subrata_modak56207ce2009-03-23 13:35:39 +0000229 return ("F_WRLCK");
plars865695b2001-08-27 22:15:12 +0000230 case 3:
subrata_modak56207ce2009-03-23 13:35:39 +0000231 return ("F_UNLCK");
plars865695b2001-08-27 22:15:12 +0000232 default:
233 sprintf(buf, "BAD VALUE: %d", type);
subrata_modak56207ce2009-03-23 13:35:39 +0000234 return (buf);
plars865695b2001-08-27 22:15:12 +0000235 }
236}
237
subrata_modak56207ce2009-03-23 13:35:39 +0000238void parent_put(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000239{
240 if (write(parent_pipe[1], l, sizeof(*l)) != sizeof(*l)) {
241 tst_resm(TFAIL, "couldn't send message to child");
242 fail = 1;
243 }
244}
245
subrata_modak56207ce2009-03-23 13:35:39 +0000246void parent_get(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000247{
248 if (read(child_pipe[0], l, sizeof(*l)) != sizeof(*l)) {
249 tst_resm(TFAIL, "couldn't get message from child");
250 fail = 1;
251 }
252}
253
subrata_modak56207ce2009-03-23 13:35:39 +0000254void child_put(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000255{
256 if (write(child_pipe[1], l, sizeof(*l)) != sizeof(*l)) {
257 tst_resm(TFAIL, "couldn't send message to parent");
258 fail = 1;
259 }
260}
261
subrata_modak56207ce2009-03-23 13:35:39 +0000262void child_get(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000263{
264 if (read(parent_pipe[0], l, sizeof(*l)) != sizeof(*l)) {
265 tst_resm(TFAIL, "couldn't get message from parent");
266 cleanup();
267 } else if (l->l_type == (short)STOP) {
268 exit(0);
269 }
270}
271
Mike Frysingerc57fba52014-04-09 18:56:30 -0400272void stop_child(void)
plars865695b2001-08-27 22:15:12 +0000273{
274 struct flock fl;
275
Mike Frysingere61ddba2014-04-09 23:24:32 -0400276 signal(SIGCLD, SIG_DFL);
plars865695b2001-08-27 22:15:12 +0000277 fl.l_type = STOP;
278 parent_put(&fl);
279 wait(0);
280}
281
Mike Frysingerc57fba52014-04-09 18:56:30 -0400282void catch_child(void)
plars865695b2001-08-27 22:15:12 +0000283{
284 tst_resm(TFAIL, "Unexpected death of child process");
285 cleanup();
286}
robbiew5aab8a72003-03-26 18:05:30 +0000287
288int main(int ac, char **av)
289{
290 struct flock tl;
subrata_modakbdbaec52009-02-26 12:14:51 +0000291
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200292 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200293 const char *msg;
robbiew5aab8a72003-03-26 18:05:30 +0000294
Garrett Cooper45e285d2010-11-22 12:19:25 -0800295 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800296 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew5aab8a72003-03-26 18:05:30 +0000297 }
robbiewd34d5812005-07-11 22:28:09 +0000298#ifdef UCLINUX
299 maybe_run_child(&do_child, "ddddd", &parent_pipe[0], &parent_pipe[1],
300 &child_pipe[0], &child_pipe[1], &fd);
301#endif
302
subrata_modak56207ce2009-03-23 13:35:39 +0000303 setup(); /* global setup */
robbiew5aab8a72003-03-26 18:05:30 +0000304
305 /* Check for looping state if -i option is given */
306 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800307 /* reset tst_count in case we are looping */
308 tst_count = 0;
robbiew5aab8a72003-03-26 18:05:30 +0000309
robbiewd34d5812005-07-11 22:28:09 +0000310 if ((child_pid = FORK_OR_VFORK()) == 0) {
311#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +0000312 if (self_exec
313 (av[0], "ddddd", parent_pipe[0], parent_pipe[1],
314 child_pipe[0], child_pipe[1], fd) < 0) {
robbiewd34d5812005-07-11 22:28:09 +0000315 tst_resm(TFAIL, "self_exec failed");
316 cleanup();
317 }
318#else
robbiew5aab8a72003-03-26 18:05:30 +0000319 do_child();
robbiewd34d5812005-07-11 22:28:09 +0000320#endif
robbiew5aab8a72003-03-26 18:05:30 +0000321 }
322 if (child_pid < 0) {
323 tst_resm(TFAIL, "Fork failed");
324 cleanup();
325 }
326
327 (void)close(parent_pipe[0]);
328 (void)close(child_pipe[1]);
329
mridgedb639212005-01-04 21:04:11 +0000330/* //block1: */
robbiew5aab8a72003-03-26 18:05:30 +0000331 tst_resm(TINFO, "Enter block 1");
332 fail = 0;
333 /*
334 * Set a read lock on the whole file
335 */
336 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 0, 0) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000337 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
338 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000339 fail = 1;
340 }
341
342 /*
343 * Test to make sure it's there.
344 */
345 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
346 compare_lock(&tl, (short)F_RDLCK, (short)0, 0, 0, parent_pid);
347
348 /*
349 * remove the lock set above
350 */
351 unlock_file();
subrata_modak56207ce2009-03-23 13:35:39 +0000352
robbiew5aab8a72003-03-26 18:05:30 +0000353 if (fail) {
354 tst_resm(TINFO, "Test block 1: FAILED");
355 } else {
356 tst_resm(TINFO, "Test block 1: PASSED");
357 }
358 tst_resm(TINFO, "Exit block 1");
359
mridgedb639212005-01-04 21:04:11 +0000360/* //block2: */
robbiew5aab8a72003-03-26 18:05:30 +0000361 tst_resm(TINFO, "Enter block 2");
362 fail = 0;
363
364 /*
365 * Set a write lock on the whole file
366 */
367 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 0, 0) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000368 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
369 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000370 fail = 1;
371 }
372
373 /*
374 * Test to make sure its there
375 */
376 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
377 compare_lock(&tl, (short)F_WRLCK, (short)0, 0, 0, parent_pid);
378
379 /*
380 * remove the lock set above
381 */
382 unlock_file();
383
384 if (fail) {
385 tst_resm(TINFO, "Test block 2: FAILED");
386 } else {
387 tst_resm(TINFO, "Test block 2: PASSED");
388 }
389
390 tst_resm(TINFO, "Exit block 2");
391
mridgedb639212005-01-04 21:04:11 +0000392/* //block3: */
robbiew5aab8a72003-03-26 18:05:30 +0000393 tst_resm(TINFO, "Enter block 3");
394 fail = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000395
robbiew5aab8a72003-03-26 18:05:30 +0000396 /*
397 * Add a read lock to the middle of the file and a write
398 * at the begining
399 */
400 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000401 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
402 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000403 fail = 1;
404 }
405
406 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 1, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000407 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
408 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000409 fail = 1;
410 }
411
412 /*
413 * Test write lock
414 */
415 do_test(&tl, F_WRLCK, 0, 0, 0);
416 compare_lock(&tl, (short)F_WRLCK, (short)0, 1, 5, parent_pid);
417
418 /*
419 * Test read lock
420 */
421 do_test(&tl, F_WRLCK, 0, 6, 0);
422 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
423
424 /*
425 * Test that the rest of the file is unlocked
426 */
427 do_test(&tl, F_WRLCK, 0, 15, 0);
428 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
429
430 /*
431 * remove all the locks set above
432 */
433 unlock_file();
434
435 if (fail) {
436 tst_resm(TINFO, "Test block 3: FAILED");
437 } else {
438 tst_resm(TINFO, "Test block 3 : PASSED");
439 }
440 tst_resm(TINFO, "Exit block 3");
441
mridgedb639212005-01-04 21:04:11 +0000442/* //block4: */
robbiew5aab8a72003-03-26 18:05:30 +0000443 tst_resm(TINFO, "Enter block 4");
444 fail = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000445
robbiew5aab8a72003-03-26 18:05:30 +0000446 /*
447 * Set a read lock at the middle of the file and a
448 * write lock just before
449 */
450 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000451 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
452 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000453 fail = 1;
454 }
455
456 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 5, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000457 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
458 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000459 fail = 1;
460 }
461
462 /*
463 * Test the write lock
464 */
465 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
466 compare_lock(&tl, (short)F_WRLCK, (short)0, 5, 5, parent_pid);
467
468 /*
469 * Test the read lock.
470 */
471 do_test(&tl, (short)F_WRLCK, (short)0, 10, 0);
472 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
473
474 /*
475 * Test to make sure the rest of the file is unlocked.
476 */
477 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
478 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
479
480 /*
481 * remove all the locks set above
482 */
483 unlock_file();
484
485 if (fail) {
486 tst_resm(TINFO, "Test block 4: FAILED");
487 } else {
488 tst_resm(TINFO, "Test block 4: PASSED");
489 }
490 tst_resm(TINFO, "Exit block 4");
491
mridgedb639212005-01-04 21:04:11 +0000492/* //block5: */
robbiew5aab8a72003-03-26 18:05:30 +0000493 tst_resm(TINFO, "Enter block 5");
494 fail = 0;
495
496 /*
497 * Set a read lock in the middle and a write lock that
498 * ends at the first byte of the read lock
499 */
500 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000501 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
502 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000503 fail = 1;
504 }
505
506 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 5, 6) < 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 /*
513 * Test write lock
514 */
515 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
516 compare_lock(&tl, (short)F_WRLCK, (short)0, 5, 6, parent_pid);
517
518 /*
519 * Test read lock
520 */
521 do_test(&tl, (short)F_WRLCK, (short)0, 11, 0);
522 compare_lock(&tl, (short)F_RDLCK, (short)0, 11, 4, 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, 15, 0);
528 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
529
530 /*
531 * remove all the locks set above
532 */
533 unlock_file();
534
535 if (fail) {
536 tst_resm(TINFO, "Test block 5: FAILED");
537 } else {
538 tst_resm(TINFO, "Test block 5: PASSED");
539 }
540 tst_resm(TINFO, "Exit block 5");
541
mridgedb639212005-01-04 21:04:11 +0000542/* //block6: */
robbiew5aab8a72003-03-26 18:05:30 +0000543 tst_resm(TINFO, "Enter block 6");
544 fail = 0;
545
546 /*
547 * Set a read lock on the middle of the file and a write
548 * lock that overlaps the front of the read.
549 */
550 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000551 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
552 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000553 fail = 1;
554 }
555
556 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 5, 8) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000557 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
558 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000559 fail = 1;
560 }
561
562 /*
563 * Test the write lock
564 */
565 do_test(&tl, (short)F_WRLCK, (short)0, 5, 0);
566 compare_lock(&tl, (short)F_WRLCK, (short)0, 5, 8, parent_pid);
567
568 /*
569 * Test the read lock
570 */
571 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
572 compare_lock(&tl, (short)F_RDLCK, (short)0, 13, 2, parent_pid);
573
574 /*
575 * Test to make sure the rest of the file is unlocked.
576 */
577 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
578 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
579
580 /*
581 * remove all the locks set above
582 */
583 unlock_file();
584
585 if (fail) {
586 tst_resm(TINFO, "Test block 6 FAILED");
587 } else {
588 tst_resm(TINFO, "Test block 6 PASSED");
589 }
590 tst_resm(TINFO, "Exit block 6");
591
mridgedb639212005-01-04 21:04:11 +0000592/* //block7: */
robbiew5aab8a72003-03-26 18:05:30 +0000593 tst_resm(TINFO, "Enter block 7");
594 fail = 0;
595
596 /*
597 * Set a read lock in the middle of a file and a write
598 * lock in the middle of it
599 */
600 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 10) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000601 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
602 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000603 fail = 1;
604 }
605
606 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 13, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000607 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
608 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000609 fail = 1;
610 }
611
612 /*
613 * Test the first read lock
614 */
subrata_modak56207ce2009-03-23 13:35:39 +0000615 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
robbiew5aab8a72003-03-26 18:05:30 +0000616 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 3, parent_pid);
617
618 /*
619 * Test the write lock
620 */
621 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
622 compare_lock(&tl, (short)F_WRLCK, (short)0, 13, 5, parent_pid);
623
624 /*
625 * Test the second read lock
626 */
627 do_test(&tl, (short)F_WRLCK, (short)0, 18, 0);
628 compare_lock(&tl, (short)F_RDLCK, (short)0, 18, 2, parent_pid);
629
630 /*
631 * Test to make sure the rest of the file is unlocked
632 */
633 do_test(&tl, (short)F_WRLCK, (short)0, 20, 0);
634 compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, 0);
635
636 /*
637 * remove all the locks set above.
638 */
639 unlock_file();
640 if (fail) {
641 tst_resm(TINFO, "Test block 7: FAILED");
642 } else {
643 tst_resm(TINFO, "Test block 7: PASSED");
644 }
645 tst_resm(TINFO, "Exit block 7");
646
mridgedb639212005-01-04 21:04:11 +0000647/* //block8: */
robbiew5aab8a72003-03-26 18:05:30 +0000648 tst_resm(TINFO, "Enter block 8");
649 fail = 0;
650 /*
651 * Set a read lock in the middle of the file and a write
652 * lock that overlaps the end
653 */
654 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000655 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
656 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000657 fail = 1;
658 }
659
660 /*
661 * Set a write lock on the whole file
662 */
663 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 13, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000664 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
665 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000666 fail = 1;
667 }
668
669 /*
670 * Test the read lock
671 */
672 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
673 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 3, parent_pid);
674
675 /*
676 * Test the write lock
677 */
678 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
679 compare_lock(&tl, (short)F_WRLCK, (short)0, 13, 5, parent_pid);
680
681 /*
682 * Test to make sure the rest of the file is unlocked
683 */
684 do_test(&tl, (short)F_WRLCK, (short)0, 18, 0);
685 compare_lock(&tl, (short)F_UNLCK, (short)0, 18, 0, 0);
686
687 /*
688 * remove all the locks set above
689 */
690 unlock_file();
691
692 if (fail) {
693 tst_resm(TINFO, "Test block 8: FAILED");
694 } else {
695 tst_resm(TINFO, "Test block 8: PASSED");
696 }
697 tst_resm(TINFO, "Exit block 8");
698
mridgedb639212005-01-04 21:04:11 +0000699/* //block9: */
robbiew5aab8a72003-03-26 18:05:30 +0000700 tst_resm(TINFO, "Enter block 9");
701 fail = 0;
702
703 /*
704 * Set a read lock in the middle of the file and a write
705 * lock starting at the last byte of the read lock
706 */
707 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000708 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
709 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000710 fail = 1;
711 }
712
713 /*
714 * Set a write lock on the whole file.
715 */
716 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 14, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000717 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
718 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000719 fail = 1;
720 }
721
722 /*
723 * Test read lock
724 */
725 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
726 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 4, parent_pid);
727
728 /*
729 * Test the write lock
730 */
731 do_test(&tl, (short)F_WRLCK, (short)0, 14, 0);
732 compare_lock(&tl, (short)F_WRLCK, (short)0, 14, 5, parent_pid);
733
734 /*
735 * Test to make sure the end of the file is unlocked
736 */
737 do_test(&tl, (short)F_WRLCK, (short)0, 19, 0);
738 compare_lock(&tl, (short)F_UNLCK, (short)0, 19, 0, 0);
739
740 /*
741 * remove all the locks set above
742 */
743 unlock_file();
744
745 if (fail) {
746 tst_resm(TINFO, "Test block 9: FAILED");
747 } else {
748 tst_resm(TINFO, "Test block 9: PASSED");
749 }
750 tst_resm(TINFO, "Exit block 9");
751
mridgedb639212005-01-04 21:04:11 +0000752/* //block10: */
robbiew5aab8a72003-03-26 18:05:30 +0000753 tst_resm(TINFO, "Enter block 10");
754 fail = 0;
755
756 /*
757 * Set a read lock in the middle of the file and a write
758 * lock that starts just after the last byte of the
759 * read lock.
760 */
761 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000762 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
763 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000764 fail = 1;
765 }
766
767 /*
768 * Set a write lock on the whole file
769 */
770 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 15, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000771 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
772 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000773 fail = 1;
774 }
775
776 /*
777 * Test the read lock
778 */
779 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
780 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
781
782 /*
783 * Test the write lock
784 */
785 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
786 compare_lock(&tl, (short)F_WRLCK, (short)0, 15, 5, parent_pid);
787
788 /*
789 * Test to make sure the rest of the file is unlocked
790 */
791 do_test(&tl, (short)F_WRLCK, (short)0, 20, 0);
792 compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, 0);
793
794 /*
795 * remove all the locks set above
796 */
797 unlock_file();
798
799 if (fail) {
800 tst_resm(TINFO, "Test block 10: FAILED");
801 } else {
802 tst_resm(TINFO, "Test block 10: PASSED");
803 }
804 tst_resm(TINFO, "Exit block 10");
805
mridgedb639212005-01-04 21:04:11 +0000806/* //block11: */
robbiew5aab8a72003-03-26 18:05:30 +0000807 tst_resm(TINFO, "Enter block 11");
808 fail = 0;
809
810 /*
811 * Set a read lock at the middle of the file and a write
812 * lock that starts past the end of the read lock.
813 */
814 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000815 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
816 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000817 fail = 1;
818 }
819
820 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 16, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000821 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
822 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000823 fail = 1;
824 }
825
826 /*
827 * Test the read lock
828 */
829 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
830 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
831
832 /*
833 * Test that byte in between is unlocked
834 */
835 do_test(&tl, (short)F_WRLCK, (short)0, 15, 1);
836 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 1, 0);
837
838 /*
839 * Test the write lock
840 */
841 do_test(&tl, (short)F_WRLCK, (short)0, 16, 0);
842 compare_lock(&tl, (short)F_WRLCK, (short)0, 16, 5, parent_pid);
843
844 /*
845 * Test to make sure the rest of the file is unlocked
846 */
847 do_test(&tl, (short)F_WRLCK, (short)0, 21, 0);
848 compare_lock(&tl, (short)F_UNLCK, (short)0, 21, 0, 0);
849
850 /*
851 * remove all the locks set above
852 */
853 unlock_file();
854
855 if (fail) {
856 tst_resm(TINFO, "Test block 11: FAILED");
857 } else {
858 tst_resm(TINFO, "Test block 11: PASSED");
859 }
860 tst_resm(TINFO, "Exit block 11");
861
862 stop_child();
863 close(fd);
864 }
865 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800866 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700867}