blob: 1cc0ef0b883deabc41f558fecf115fcb595445ed [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"
plars865695b2001-08-27 22:15:12 +000048
49#define STRINGSIZE 27
50#define STRING "abcdefghijklmnopqrstuvwxyz\n"
51#define STOP 0xFFF0
52
53int parent_pipe[2];
54int child_pipe[2];
55int fd;
plarsa1e518d2002-09-09 18:23:52 +000056pid_t parent_pid, child_pid;
plars865695b2001-08-27 22:15:12 +000057
58void parent_put();
59void parent_get();
60void child_put();
61void child_get();
62void stop_child();
plarsa1e518d2002-09-09 18:23:52 +000063void compare_lock(struct flock *, short, short, int, int, pid_t);
plars865695b2001-08-27 22:15:12 +000064void unlock_file();
65void do_test(struct flock *, short, short, int, int);
66void catch_child();
67char *str_type();
68int do_lock(int, short, short, int, int);
69
nstrazfa31d552002-05-14 16:50:06 +000070char *TCID = "fcntl21";
plars865695b2001-08-27 22:15:12 +000071int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000072
73void setup(void);
74void cleanup(void);
75int fail;
76
plars865695b2001-08-27 22:15:12 +000077/*
78 * setup
subrata_modak56207ce2009-03-23 13:35:39 +000079 * performs all ONE TIME setup for this test
plars865695b2001-08-27 22:15:12 +000080 */
Mike Frysingerc57fba52014-04-09 18:56:30 -040081void setup(void)
plars865695b2001-08-27 22:15:12 +000082{
83 char *buf = STRING;
robbiewdad22712003-04-16 19:14:05 +000084 char template[PATH_MAX];
robbiew1e4cf0c2005-02-24 17:03:42 +000085 struct sigaction act;
plars865695b2001-08-27 22:15:12 +000086
plars865695b2001-08-27 22:15:12 +000087 tst_sig(FORK, DEF_HANDLER, cleanup);
88
89 umask(0);
90
plars865695b2001-08-27 22:15:12 +000091 TEST_PAUSE;
92
93 pipe(parent_pipe);
94 pipe(child_pipe);
95 parent_pid = getpid();
robbiew54f7a712003-04-16 19:20:30 +000096
subrata_modakbdbaec52009-02-26 12:14:51 +000097 tst_tmpdir();
robbiew54f7a712003-04-16 19:20:30 +000098
99 snprintf(template, PATH_MAX, "fcntl21XXXXXX");
plars865695b2001-08-27 22:15:12 +0000100
robbiewdad22712003-04-16 19:14:05 +0000101 if ((fd = mkstemp(template)) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000102 tst_resm(TFAIL, "Couldn't open temp file! errno = %d", errno);
103 }
plars865695b2001-08-27 22:15:12 +0000104
subrata_modak56207ce2009-03-23 13:35:39 +0000105 if (write(fd, buf, STRINGSIZE) < 0) {
106 tst_resm(TFAIL, "Couldn't write to temp file! errno = %d",
107 errno);
108 }
plars865695b2001-08-27 22:15:12 +0000109
robbiew1e4cf0c2005-02-24 17:03:42 +0000110 memset(&act, 0, sizeof(act));
111 act.sa_handler = catch_child;
112 sigemptyset(&act.sa_mask);
113 sigaddset(&act.sa_mask, SIGCLD);
114 if ((sigaction(SIGCLD, &act, NULL)) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000115 tst_resm(TFAIL, "SIGCLD signal setup failed, errno: %d", errno);
plars865695b2001-08-27 22:15:12 +0000116 fail = 1;
117 }
118}
119
120/*
121 * cleanup()
subrata_modak56207ce2009-03-23 13:35:39 +0000122 * performs all ONE TIME cleanup for this test at completion or
123 * premature exit
plars865695b2001-08-27 22:15:12 +0000124 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400125void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000126{
plars865695b2001-08-27 22:15:12 +0000127
robbiew54f7a712003-04-16 19:20:30 +0000128 tst_rmdir();
129
plars865695b2001-08-27 22:15:12 +0000130}
131
Mike Frysingerc57fba52014-04-09 18:56:30 -0400132void do_child(void)
plars865695b2001-08-27 22:15:12 +0000133{
134 struct flock fl;
135
136 close(parent_pipe[1]);
137 close(child_pipe[0]);
subrata_modak56207ce2009-03-23 13:35:39 +0000138 while (1) {
plars865695b2001-08-27 22:15:12 +0000139 child_get(&fl);
140 if (fcntl(fd, F_GETLK, &fl) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000141 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
142 errno);
plars865695b2001-08-27 22:15:12 +0000143 fail = 1;
144 }
145 child_put(&fl);
146 }
147}
148
robbiew5aab8a72003-03-26 18:05:30 +0000149int do_lock(int cmd, short type, short whence, int start, int len)
plars865695b2001-08-27 22:15:12 +0000150{
151 struct flock fl;
152
153 fl.l_type = type;
154 fl.l_whence = whence;
155 fl.l_start = start;
156 fl.l_len = len;
subrata_modak56207ce2009-03-23 13:35:39 +0000157 return (fcntl(fd, cmd, &fl));
plars865695b2001-08-27 22:15:12 +0000158}
159
subrata_modak56207ce2009-03-23 13:35:39 +0000160void do_test(struct flock *fl, short type, short whence, int start, int len)
plars865695b2001-08-27 22:15:12 +0000161{
162 fl->l_type = type;
163 fl->l_whence = whence;
164 fl->l_start = start;
165 fl->l_len = len;
166 fl->l_pid = (short)0;
167
168 parent_put(fl);
169 parent_get(fl);
170}
171
172void
173compare_lock(struct flock *fl, short type, short whence, int start, int len,
plarsa1e518d2002-09-09 18:23:52 +0000174 pid_t pid)
plars865695b2001-08-27 22:15:12 +0000175{
176 if (fl->l_type != type) {
177 tst_resm(TFAIL, "lock type is wrong should be %s is %s",
178 str_type(type), str_type(fl->l_type));
179 fail = 1;
180 }
181
182 if (fl->l_whence != whence) {
183 tst_resm(TFAIL, "lock whence is wrong should be %d is %d",
184 whence, fl->l_whence);
185 fail = 1;
186 }
187
188 if (fl->l_start != start) {
189 tst_resm(TFAIL, "region starts in wrong place, should be"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800190 "%d is %" PRId64, start, (int64_t) fl->l_start);
plars865695b2001-08-27 22:15:12 +0000191 fail = 1;
192 }
193
194 if (fl->l_len != len) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800195 tst_resm(TFAIL,
196 "region length is wrong, should be %d is %" PRId64,
197 len, (int64_t) fl->l_len);
plars865695b2001-08-27 22:15:12 +0000198 fail = 1;
199 }
200
201 if (fl->l_pid != pid) {
202 tst_resm(TFAIL, "locking pid is wrong, should be %d is %d",
203 pid, fl->l_pid);
204 fail = 1;
205 }
206}
207
Mike Frysingerc57fba52014-04-09 18:56:30 -0400208void unlock_file(void)
plars865695b2001-08-27 22:15:12 +0000209{
210 struct flock fl;
211
212 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 0, 0) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000213 tst_resm(TFAIL, "fcntl on file failed, errno =%d", errno);
plars865695b2001-08-27 22:15:12 +0000214 fail = 1;
215 }
216 do_test(&fl, F_WRLCK, 0, 0, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000217 compare_lock(&fl, (short)F_UNLCK, (short)0, 0, 0, (pid_t) 0);
plars865695b2001-08-27 22:15:12 +0000218}
219
subrata_modak56207ce2009-03-23 13:35:39 +0000220char *str_type(int type)
plars865695b2001-08-27 22:15:12 +0000221{
222 static char buf[20];
223
224 switch (type) {
225 case 1:
subrata_modak56207ce2009-03-23 13:35:39 +0000226 return ("F_RDLCK");
plars865695b2001-08-27 22:15:12 +0000227 case 2:
subrata_modak56207ce2009-03-23 13:35:39 +0000228 return ("F_WRLCK");
plars865695b2001-08-27 22:15:12 +0000229 case 3:
subrata_modak56207ce2009-03-23 13:35:39 +0000230 return ("F_UNLCK");
plars865695b2001-08-27 22:15:12 +0000231 default:
232 sprintf(buf, "BAD VALUE: %d", type);
subrata_modak56207ce2009-03-23 13:35:39 +0000233 return (buf);
plars865695b2001-08-27 22:15:12 +0000234 }
235}
236
subrata_modak56207ce2009-03-23 13:35:39 +0000237void parent_put(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000238{
239 if (write(parent_pipe[1], l, sizeof(*l)) != sizeof(*l)) {
240 tst_resm(TFAIL, "couldn't send message to child");
241 fail = 1;
242 }
243}
244
subrata_modak56207ce2009-03-23 13:35:39 +0000245void parent_get(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000246{
247 if (read(child_pipe[0], l, sizeof(*l)) != sizeof(*l)) {
248 tst_resm(TFAIL, "couldn't get message from child");
249 fail = 1;
250 }
251}
252
subrata_modak56207ce2009-03-23 13:35:39 +0000253void child_put(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000254{
255 if (write(child_pipe[1], l, sizeof(*l)) != sizeof(*l)) {
256 tst_resm(TFAIL, "couldn't send message to parent");
257 fail = 1;
258 }
259}
260
subrata_modak56207ce2009-03-23 13:35:39 +0000261void child_get(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000262{
263 if (read(parent_pipe[0], l, sizeof(*l)) != sizeof(*l)) {
264 tst_resm(TFAIL, "couldn't get message from parent");
265 cleanup();
266 } else if (l->l_type == (short)STOP) {
267 exit(0);
268 }
269}
270
Mike Frysingerc57fba52014-04-09 18:56:30 -0400271void stop_child(void)
plars865695b2001-08-27 22:15:12 +0000272{
273 struct flock fl;
274
Mike Frysingere61ddba2014-04-09 23:24:32 -0400275 signal(SIGCLD, SIG_DFL);
plars865695b2001-08-27 22:15:12 +0000276 fl.l_type = STOP;
277 parent_put(&fl);
278 wait(0);
279}
280
Mike Frysingerc57fba52014-04-09 18:56:30 -0400281void catch_child(void)
plars865695b2001-08-27 22:15:12 +0000282{
283 tst_resm(TFAIL, "Unexpected death of child process");
284 cleanup();
285}
robbiew5aab8a72003-03-26 18:05:30 +0000286
287int main(int ac, char **av)
288{
289 struct flock tl;
subrata_modakbdbaec52009-02-26 12:14:51 +0000290
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200291 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200292 const char *msg;
robbiew5aab8a72003-03-26 18:05:30 +0000293
Garrett Cooper45e285d2010-11-22 12:19:25 -0800294 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800295 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew5aab8a72003-03-26 18:05:30 +0000296 }
robbiewd34d5812005-07-11 22:28:09 +0000297#ifdef UCLINUX
298 maybe_run_child(&do_child, "ddddd", &parent_pipe[0], &parent_pipe[1],
299 &child_pipe[0], &child_pipe[1], &fd);
300#endif
301
subrata_modak56207ce2009-03-23 13:35:39 +0000302 setup(); /* global setup */
robbiew5aab8a72003-03-26 18:05:30 +0000303
304 /* Check for looping state if -i option is given */
305 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800306 /* reset tst_count in case we are looping */
307 tst_count = 0;
robbiew5aab8a72003-03-26 18:05:30 +0000308
robbiewd34d5812005-07-11 22:28:09 +0000309 if ((child_pid = FORK_OR_VFORK()) == 0) {
310#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +0000311 if (self_exec
312 (av[0], "ddddd", parent_pipe[0], parent_pipe[1],
313 child_pipe[0], child_pipe[1], fd) < 0) {
robbiewd34d5812005-07-11 22:28:09 +0000314 tst_resm(TFAIL, "self_exec failed");
315 cleanup();
316 }
317#else
robbiew5aab8a72003-03-26 18:05:30 +0000318 do_child();
robbiewd34d5812005-07-11 22:28:09 +0000319#endif
robbiew5aab8a72003-03-26 18:05:30 +0000320 }
321 if (child_pid < 0) {
322 tst_resm(TFAIL, "Fork failed");
323 cleanup();
324 }
325
326 (void)close(parent_pipe[0]);
327 (void)close(child_pipe[1]);
328
mridgedb639212005-01-04 21:04:11 +0000329/* //block1: */
robbiew5aab8a72003-03-26 18:05:30 +0000330 tst_resm(TINFO, "Enter block 1");
331 fail = 0;
332 /*
333 * Set a read lock on the whole file
334 */
335 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 0, 0) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000336 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
337 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000338 fail = 1;
339 }
340
341 /*
342 * Test to make sure it's there.
343 */
344 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
345 compare_lock(&tl, (short)F_RDLCK, (short)0, 0, 0, parent_pid);
346
347 /*
348 * remove the lock set above
349 */
350 unlock_file();
subrata_modak56207ce2009-03-23 13:35:39 +0000351
robbiew5aab8a72003-03-26 18:05:30 +0000352 if (fail) {
353 tst_resm(TINFO, "Test block 1: FAILED");
354 } else {
355 tst_resm(TINFO, "Test block 1: PASSED");
356 }
357 tst_resm(TINFO, "Exit block 1");
358
mridgedb639212005-01-04 21:04:11 +0000359/* //block2: */
robbiew5aab8a72003-03-26 18:05:30 +0000360 tst_resm(TINFO, "Enter block 2");
361 fail = 0;
362
363 /*
364 * Set a write lock on the whole file
365 */
366 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 0, 0) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000367 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
368 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000369 fail = 1;
370 }
371
372 /*
373 * Test to make sure its there
374 */
375 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
376 compare_lock(&tl, (short)F_WRLCK, (short)0, 0, 0, parent_pid);
377
378 /*
379 * remove the lock set above
380 */
381 unlock_file();
382
383 if (fail) {
384 tst_resm(TINFO, "Test block 2: FAILED");
385 } else {
386 tst_resm(TINFO, "Test block 2: PASSED");
387 }
388
389 tst_resm(TINFO, "Exit block 2");
390
mridgedb639212005-01-04 21:04:11 +0000391/* //block3: */
robbiew5aab8a72003-03-26 18:05:30 +0000392 tst_resm(TINFO, "Enter block 3");
393 fail = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000394
robbiew5aab8a72003-03-26 18:05:30 +0000395 /*
396 * Add a read lock to the middle of the file and a write
397 * at the begining
398 */
399 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000400 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
401 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000402 fail = 1;
403 }
404
405 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 1, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000406 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
407 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000408 fail = 1;
409 }
410
411 /*
412 * Test write lock
413 */
414 do_test(&tl, F_WRLCK, 0, 0, 0);
415 compare_lock(&tl, (short)F_WRLCK, (short)0, 1, 5, parent_pid);
416
417 /*
418 * Test read lock
419 */
420 do_test(&tl, F_WRLCK, 0, 6, 0);
421 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
422
423 /*
424 * Test that the rest of the file is unlocked
425 */
426 do_test(&tl, F_WRLCK, 0, 15, 0);
427 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
428
429 /*
430 * remove all the locks set above
431 */
432 unlock_file();
433
434 if (fail) {
435 tst_resm(TINFO, "Test block 3: FAILED");
436 } else {
437 tst_resm(TINFO, "Test block 3 : PASSED");
438 }
439 tst_resm(TINFO, "Exit block 3");
440
mridgedb639212005-01-04 21:04:11 +0000441/* //block4: */
robbiew5aab8a72003-03-26 18:05:30 +0000442 tst_resm(TINFO, "Enter block 4");
443 fail = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000444
robbiew5aab8a72003-03-26 18:05:30 +0000445 /*
446 * Set a read lock at the middle of the file and a
447 * write lock just before
448 */
449 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000450 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
451 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000452 fail = 1;
453 }
454
455 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 5, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000456 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
457 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000458 fail = 1;
459 }
460
461 /*
462 * Test the write lock
463 */
464 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
465 compare_lock(&tl, (short)F_WRLCK, (short)0, 5, 5, parent_pid);
466
467 /*
468 * Test the read lock.
469 */
470 do_test(&tl, (short)F_WRLCK, (short)0, 10, 0);
471 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
472
473 /*
474 * Test to make sure the rest of the file is unlocked.
475 */
476 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
477 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
478
479 /*
480 * remove all the locks set above
481 */
482 unlock_file();
483
484 if (fail) {
485 tst_resm(TINFO, "Test block 4: FAILED");
486 } else {
487 tst_resm(TINFO, "Test block 4: PASSED");
488 }
489 tst_resm(TINFO, "Exit block 4");
490
mridgedb639212005-01-04 21:04:11 +0000491/* //block5: */
robbiew5aab8a72003-03-26 18:05:30 +0000492 tst_resm(TINFO, "Enter block 5");
493 fail = 0;
494
495 /*
496 * Set a read lock in the middle and a write lock that
497 * ends at the first byte of the read lock
498 */
499 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000500 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
501 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000502 fail = 1;
503 }
504
505 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 5, 6) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000506 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
507 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000508 fail = 1;
509 }
510
511 /*
512 * Test write lock
513 */
514 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
515 compare_lock(&tl, (short)F_WRLCK, (short)0, 5, 6, parent_pid);
516
517 /*
518 * Test read lock
519 */
520 do_test(&tl, (short)F_WRLCK, (short)0, 11, 0);
521 compare_lock(&tl, (short)F_RDLCK, (short)0, 11, 4, parent_pid);
522
523 /*
524 * Test to make sure the rest of the file is unlocked.
525 */
526 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
527 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
528
529 /*
530 * remove all the locks set above
531 */
532 unlock_file();
533
534 if (fail) {
535 tst_resm(TINFO, "Test block 5: FAILED");
536 } else {
537 tst_resm(TINFO, "Test block 5: PASSED");
538 }
539 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;
544
545 /*
546 * Set a read lock on the middle of the file and a write
547 * lock that overlaps the front of the read.
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_WRLCK, (short)0, 5, 8) < 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 the write lock
563 */
564 do_test(&tl, (short)F_WRLCK, (short)0, 5, 0);
565 compare_lock(&tl, (short)F_WRLCK, (short)0, 5, 8, parent_pid);
566
567 /*
568 * Test the read lock
569 */
570 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
571 compare_lock(&tl, (short)F_RDLCK, (short)0, 13, 2, parent_pid);
572
573 /*
574 * Test to make sure the rest of the file is unlocked.
575 */
576 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
577 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
578
579 /*
580 * remove all the locks set above
581 */
582 unlock_file();
583
584 if (fail) {
585 tst_resm(TINFO, "Test block 6 FAILED");
586 } else {
587 tst_resm(TINFO, "Test block 6 PASSED");
588 }
589 tst_resm(TINFO, "Exit block 6");
590
mridgedb639212005-01-04 21:04:11 +0000591/* //block7: */
robbiew5aab8a72003-03-26 18:05:30 +0000592 tst_resm(TINFO, "Enter block 7");
593 fail = 0;
594
595 /*
596 * Set a read lock in the middle of a file and a write
597 * lock in the middle of it
598 */
599 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 10) < 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 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 13, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000606 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
607 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000608 fail = 1;
609 }
610
611 /*
612 * Test the first read lock
613 */
subrata_modak56207ce2009-03-23 13:35:39 +0000614 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
robbiew5aab8a72003-03-26 18:05:30 +0000615 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 3, parent_pid);
616
617 /*
618 * Test the write lock
619 */
620 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
621 compare_lock(&tl, (short)F_WRLCK, (short)0, 13, 5, parent_pid);
622
623 /*
624 * Test the second read lock
625 */
626 do_test(&tl, (short)F_WRLCK, (short)0, 18, 0);
627 compare_lock(&tl, (short)F_RDLCK, (short)0, 18, 2, parent_pid);
628
629 /*
630 * Test to make sure the rest of the file is unlocked
631 */
632 do_test(&tl, (short)F_WRLCK, (short)0, 20, 0);
633 compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, 0);
634
635 /*
636 * remove all the locks set above.
637 */
638 unlock_file();
639 if (fail) {
640 tst_resm(TINFO, "Test block 7: FAILED");
641 } else {
642 tst_resm(TINFO, "Test block 7: PASSED");
643 }
644 tst_resm(TINFO, "Exit block 7");
645
mridgedb639212005-01-04 21:04:11 +0000646/* //block8: */
robbiew5aab8a72003-03-26 18:05:30 +0000647 tst_resm(TINFO, "Enter block 8");
648 fail = 0;
649 /*
650 * Set a read lock in the middle of the file and a write
651 * lock that overlaps the end
652 */
653 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000654 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
655 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000656 fail = 1;
657 }
658
659 /*
660 * Set a write lock on the whole file
661 */
662 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 13, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000663 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
664 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000665 fail = 1;
666 }
667
668 /*
669 * Test the read lock
670 */
671 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
672 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 3, parent_pid);
673
674 /*
675 * Test the write lock
676 */
677 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
678 compare_lock(&tl, (short)F_WRLCK, (short)0, 13, 5, parent_pid);
679
680 /*
681 * Test to make sure the rest of the file is unlocked
682 */
683 do_test(&tl, (short)F_WRLCK, (short)0, 18, 0);
684 compare_lock(&tl, (short)F_UNLCK, (short)0, 18, 0, 0);
685
686 /*
687 * remove all the locks set above
688 */
689 unlock_file();
690
691 if (fail) {
692 tst_resm(TINFO, "Test block 8: FAILED");
693 } else {
694 tst_resm(TINFO, "Test block 8: PASSED");
695 }
696 tst_resm(TINFO, "Exit block 8");
697
mridgedb639212005-01-04 21:04:11 +0000698/* //block9: */
robbiew5aab8a72003-03-26 18:05:30 +0000699 tst_resm(TINFO, "Enter block 9");
700 fail = 0;
701
702 /*
703 * Set a read lock in the middle of the file and a write
704 * lock starting at the last byte of the read lock
705 */
706 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000707 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
708 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000709 fail = 1;
710 }
711
712 /*
713 * Set a write lock on the whole file.
714 */
715 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 14, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000716 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
717 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000718 fail = 1;
719 }
720
721 /*
722 * Test read lock
723 */
724 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
725 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 4, parent_pid);
726
727 /*
728 * Test the write lock
729 */
730 do_test(&tl, (short)F_WRLCK, (short)0, 14, 0);
731 compare_lock(&tl, (short)F_WRLCK, (short)0, 14, 5, parent_pid);
732
733 /*
734 * Test to make sure the end of the file is unlocked
735 */
736 do_test(&tl, (short)F_WRLCK, (short)0, 19, 0);
737 compare_lock(&tl, (short)F_UNLCK, (short)0, 19, 0, 0);
738
739 /*
740 * remove all the locks set above
741 */
742 unlock_file();
743
744 if (fail) {
745 tst_resm(TINFO, "Test block 9: FAILED");
746 } else {
747 tst_resm(TINFO, "Test block 9: PASSED");
748 }
749 tst_resm(TINFO, "Exit block 9");
750
mridgedb639212005-01-04 21:04:11 +0000751/* //block10: */
robbiew5aab8a72003-03-26 18:05:30 +0000752 tst_resm(TINFO, "Enter block 10");
753 fail = 0;
754
755 /*
756 * Set a read lock in the middle of the file and a write
757 * lock that starts just after the last byte of the
758 * read lock.
759 */
760 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000761 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
762 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000763 fail = 1;
764 }
765
766 /*
767 * Set a write lock on the whole file
768 */
769 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 15, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000770 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
771 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000772 fail = 1;
773 }
774
775 /*
776 * Test the read lock
777 */
778 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
779 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
780
781 /*
782 * Test the write lock
783 */
784 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
785 compare_lock(&tl, (short)F_WRLCK, (short)0, 15, 5, parent_pid);
786
787 /*
788 * Test to make sure the rest of the file is unlocked
789 */
790 do_test(&tl, (short)F_WRLCK, (short)0, 20, 0);
791 compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, 0);
792
793 /*
794 * remove all the locks set above
795 */
796 unlock_file();
797
798 if (fail) {
799 tst_resm(TINFO, "Test block 10: FAILED");
800 } else {
801 tst_resm(TINFO, "Test block 10: PASSED");
802 }
803 tst_resm(TINFO, "Exit block 10");
804
mridgedb639212005-01-04 21:04:11 +0000805/* //block11: */
robbiew5aab8a72003-03-26 18:05:30 +0000806 tst_resm(TINFO, "Enter block 11");
807 fail = 0;
808
809 /*
810 * Set a read lock at the middle of the file and a write
811 * lock that starts past the end of the read lock.
812 */
813 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000814 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
815 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000816 fail = 1;
817 }
818
819 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 16, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000820 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
821 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000822 fail = 1;
823 }
824
825 /*
826 * Test the read lock
827 */
828 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
829 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
830
831 /*
832 * Test that byte in between is unlocked
833 */
834 do_test(&tl, (short)F_WRLCK, (short)0, 15, 1);
835 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 1, 0);
836
837 /*
838 * Test the write lock
839 */
840 do_test(&tl, (short)F_WRLCK, (short)0, 16, 0);
841 compare_lock(&tl, (short)F_WRLCK, (short)0, 16, 5, parent_pid);
842
843 /*
844 * Test to make sure the rest of the file is unlocked
845 */
846 do_test(&tl, (short)F_WRLCK, (short)0, 21, 0);
847 compare_lock(&tl, (short)F_UNLCK, (short)0, 21, 0, 0);
848
849 /*
850 * remove all the locks set above
851 */
852 unlock_file();
853
854 if (fail) {
855 tst_resm(TINFO, "Test block 11: FAILED");
856 } else {
857 tst_resm(TINFO, "Test block 11: PASSED");
858 }
859 tst_resm(TINFO, "Exit block 11");
860
861 stop_child();
862 close(fd);
863 }
864 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800865 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700866}