blob: 851654f182eb9da56bffad83c3c583b2c6698963 [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * NAME
nstrazfa31d552002-05-14 16:50:06 +000022 * fcntl21.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
25 * Check locking of regions of a file
26 *
27 * ALGORITHM
28 * Test changing lock sections around a read lock
29 *
30 * USAGE
nstrazfa31d552002-05-14 16:50:06 +000031 * fcntl21
plars865695b2001-08-27 22:15:12 +000032 *
33 * HISTORY
34 * 07/2001 Ported by Wayne Boyer
35 *
36 * RESTRICTIONS
37 * None
38 */
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>
robbiew5aab8a72003-03-26 18:05:30 +000046#include "test.h"
47#include "usctest.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;
72extern int Tst_count;
73
74void setup(void);
75void cleanup(void);
76int fail;
77
plars865695b2001-08-27 22:15:12 +000078
79/*
80 * setup
81 * performs all ONE TIME setup for this test
82 */
83void
84setup()
85{
86 char *buf = STRING;
robbiewdad22712003-04-16 19:14:05 +000087 char template[PATH_MAX];
robbiew1e4cf0c2005-02-24 17:03:42 +000088 struct sigaction act;
plars865695b2001-08-27 22:15:12 +000089
90 /* capture signals */
91 tst_sig(FORK, DEF_HANDLER, cleanup);
92
93 umask(0);
94
95 /* Pause if that option was specified */
96 TEST_PAUSE;
97
98 pipe(parent_pipe);
99 pipe(child_pipe);
100 parent_pid = getpid();
robbiew54f7a712003-04-16 19:20:30 +0000101
102 tst_tmpdir();
103
104 snprintf(template, PATH_MAX, "fcntl21XXXXXX");
plars865695b2001-08-27 22:15:12 +0000105
robbiewdad22712003-04-16 19:14:05 +0000106 if ((fd = mkstemp(template)) < 0) {
107 tst_resm(TFAIL, "Couldn't open temp file! errno = %d", errno);
108 }
plars865695b2001-08-27 22:15:12 +0000109
robbiewdad22712003-04-16 19:14:05 +0000110 if (write(fd, buf, STRINGSIZE) < 0) {
111 tst_resm(TFAIL, "Couldn't write to temp file! errno = %d", errno);
112 }
plars865695b2001-08-27 22:15:12 +0000113
robbiew1e4cf0c2005-02-24 17:03:42 +0000114 memset(&act, 0, sizeof(act));
115 act.sa_handler = catch_child;
116 sigemptyset(&act.sa_mask);
117 sigaddset(&act.sa_mask, SIGCLD);
118 if ((sigaction(SIGCLD, &act, NULL)) < 0) {
plars865695b2001-08-27 22:15:12 +0000119 tst_resm(TFAIL, "SIGCLD signal setup failed, errno: %d",
120 errno);
121 fail = 1;
122 }
123}
124
125/*
126 * cleanup()
127 * performs all ONE TIME cleanup for this test at completion or
128 * premature exit
129 */
130void
131cleanup()
132{
133 /*
134 * print timing stats if that option was specified
135 * print errno log if that option was specified
136 */
137 TEST_CLEANUP;
138
plars865695b2001-08-27 22:15:12 +0000139
robbiew54f7a712003-04-16 19:20:30 +0000140 tst_rmdir();
141
plars865695b2001-08-27 22:15:12 +0000142 /* exit with return code appropriate for results */
143 tst_exit();
144}
145
robbiew5aab8a72003-03-26 18:05:30 +0000146void do_child()
plars865695b2001-08-27 22:15:12 +0000147{
148 struct flock fl;
149
150 close(parent_pipe[1]);
151 close(child_pipe[0]);
152 while(1) {
153 child_get(&fl);
154 if (fcntl(fd, F_GETLK, &fl) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000155 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
156 errno);
plars865695b2001-08-27 22:15:12 +0000157 fail = 1;
158 }
159 child_put(&fl);
160 }
161}
162
robbiew5aab8a72003-03-26 18:05:30 +0000163int do_lock(int cmd, short type, short whence, int start, int len)
plars865695b2001-08-27 22:15:12 +0000164{
165 struct flock fl;
166
167 fl.l_type = type;
168 fl.l_whence = whence;
169 fl.l_start = start;
170 fl.l_len = len;
171 return(fcntl(fd, cmd, &fl));
172}
173
174void
175do_test(struct flock *fl, short type, short whence, int start, int len)
176{
177 fl->l_type = type;
178 fl->l_whence = whence;
179 fl->l_start = start;
180 fl->l_len = len;
181 fl->l_pid = (short)0;
182
183 parent_put(fl);
184 parent_get(fl);
185}
186
187void
188compare_lock(struct flock *fl, short type, short whence, int start, int len,
plarsa1e518d2002-09-09 18:23:52 +0000189 pid_t pid)
plars865695b2001-08-27 22:15:12 +0000190{
191 if (fl->l_type != type) {
192 tst_resm(TFAIL, "lock type is wrong should be %s is %s",
193 str_type(type), str_type(fl->l_type));
194 fail = 1;
195 }
196
197 if (fl->l_whence != whence) {
198 tst_resm(TFAIL, "lock whence is wrong should be %d is %d",
199 whence, fl->l_whence);
200 fail = 1;
201 }
202
203 if (fl->l_start != start) {
204 tst_resm(TFAIL, "region starts in wrong place, should be"
205 "%d is %d", start, fl->l_start);
206 fail = 1;
207 }
208
209 if (fl->l_len != len) {
210 tst_resm(TFAIL, "region length is wrong, should be %d is %d",
211 len, fl->l_len);
212 fail = 1;
213 }
214
215 if (fl->l_pid != pid) {
216 tst_resm(TFAIL, "locking pid is wrong, should be %d is %d",
217 pid, fl->l_pid);
218 fail = 1;
219 }
220}
221
222void
223unlock_file()
224{
225 struct flock fl;
226
227 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 0, 0) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000228 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
plars865695b2001-08-27 22:15:12 +0000229 errno);
230 fail = 1;
231 }
232 do_test(&fl, F_WRLCK, 0, 0, 0);
plarsa1e518d2002-09-09 18:23:52 +0000233 compare_lock(&fl, (short)F_UNLCK, (short)0, 0, 0, (pid_t)0);
plars865695b2001-08-27 22:15:12 +0000234}
235
236char *
237str_type(int type)
238{
239 static char buf[20];
240
241 switch (type) {
242 case 1:
243 return("F_RDLCK");
244 case 2:
245 return("F_WRLCK");
246 case 3:
247 return("F_UNLCK");
248 default:
249 sprintf(buf, "BAD VALUE: %d", type);
250 return(buf);
251 }
252}
253
254void
255parent_put(struct flock *l)
256{
257 if (write(parent_pipe[1], l, sizeof(*l)) != sizeof(*l)) {
258 tst_resm(TFAIL, "couldn't send message to child");
259 fail = 1;
260 }
261}
262
263void
264parent_get(struct flock *l)
265{
266 if (read(child_pipe[0], l, sizeof(*l)) != sizeof(*l)) {
267 tst_resm(TFAIL, "couldn't get message from child");
268 fail = 1;
269 }
270}
271
272void
273child_put(struct flock *l)
274{
275 if (write(child_pipe[1], l, sizeof(*l)) != sizeof(*l)) {
276 tst_resm(TFAIL, "couldn't send message to parent");
277 fail = 1;
278 }
279}
280
281void
282child_get(struct flock *l)
283{
284 if (read(parent_pipe[0], l, sizeof(*l)) != sizeof(*l)) {
285 tst_resm(TFAIL, "couldn't get message from parent");
286 cleanup();
287 } else if (l->l_type == (short)STOP) {
288 exit(0);
289 }
290}
291
292void
293stop_child()
294{
295 struct flock fl;
296
297 (void) signal(SIGCLD, (void (*)())SIG_DFL);
298 fl.l_type = STOP;
299 parent_put(&fl);
300 wait(0);
301}
302
303void
304catch_child()
305{
306 tst_resm(TFAIL, "Unexpected death of child process");
307 cleanup();
308}
robbiew5aab8a72003-03-26 18:05:30 +0000309
310int main(int ac, char **av)
311{
312 struct flock tl;
313
314 int lc; /* loop counter */
315 char *msg; /* message returned from parse_opts */
316
317 /* parse standard options */
318 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
319 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
320 }
321
322 setup(); /* global setup */
323
324 /* Check for looping state if -i option is given */
325 for (lc = 0; TEST_LOOPING(lc); lc++) {
326 /* reset Tst_count in case we are looping */
327 Tst_count = 0;
328
329 if ((child_pid = fork()) == 0) {
330 do_child();
331 }
332 if (child_pid < 0) {
333 tst_resm(TFAIL, "Fork failed");
334 cleanup();
335 }
336
337 (void)close(parent_pipe[0]);
338 (void)close(child_pipe[1]);
339
mridgedb639212005-01-04 21:04:11 +0000340/* //block1: */
robbiew5aab8a72003-03-26 18:05:30 +0000341 tst_resm(TINFO, "Enter block 1");
342 fail = 0;
343 /*
344 * Set a read lock on the whole file
345 */
346 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 0, 0) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000347 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
348 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000349 fail = 1;
350 }
351
352 /*
353 * Test to make sure it's there.
354 */
355 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
356 compare_lock(&tl, (short)F_RDLCK, (short)0, 0, 0, parent_pid);
357
358 /*
359 * remove the lock set above
360 */
361 unlock_file();
362
363 if (fail) {
364 tst_resm(TINFO, "Test block 1: FAILED");
365 } else {
366 tst_resm(TINFO, "Test block 1: PASSED");
367 }
368 tst_resm(TINFO, "Exit block 1");
369
mridgedb639212005-01-04 21:04:11 +0000370/* //block2: */
robbiew5aab8a72003-03-26 18:05:30 +0000371 tst_resm(TINFO, "Enter block 2");
372 fail = 0;
373
374 /*
375 * Set a write lock on the whole file
376 */
377 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 0, 0) < 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 to make sure its there
385 */
386 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
387 compare_lock(&tl, (short)F_WRLCK, (short)0, 0, 0, parent_pid);
388
389 /*
390 * remove the lock set above
391 */
392 unlock_file();
393
394 if (fail) {
395 tst_resm(TINFO, "Test block 2: FAILED");
396 } else {
397 tst_resm(TINFO, "Test block 2: PASSED");
398 }
399
400 tst_resm(TINFO, "Exit block 2");
401
mridgedb639212005-01-04 21:04:11 +0000402/* //block3: */
robbiew5aab8a72003-03-26 18:05:30 +0000403 tst_resm(TINFO, "Enter block 3");
404 fail = 0;
405
406 /*
407 * Add a read lock to the middle of the file and a write
408 * at the begining
409 */
410 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000411 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
412 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000413 fail = 1;
414 }
415
416 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 1, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000417 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
418 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000419 fail = 1;
420 }
421
422 /*
423 * Test write lock
424 */
425 do_test(&tl, F_WRLCK, 0, 0, 0);
426 compare_lock(&tl, (short)F_WRLCK, (short)0, 1, 5, parent_pid);
427
428 /*
429 * Test read lock
430 */
431 do_test(&tl, F_WRLCK, 0, 6, 0);
432 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
433
434 /*
435 * Test that the rest of the file is unlocked
436 */
437 do_test(&tl, F_WRLCK, 0, 15, 0);
438 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
439
440 /*
441 * remove all the locks set above
442 */
443 unlock_file();
444
445 if (fail) {
446 tst_resm(TINFO, "Test block 3: FAILED");
447 } else {
448 tst_resm(TINFO, "Test block 3 : PASSED");
449 }
450 tst_resm(TINFO, "Exit block 3");
451
mridgedb639212005-01-04 21:04:11 +0000452/* //block4: */
robbiew5aab8a72003-03-26 18:05:30 +0000453 tst_resm(TINFO, "Enter block 4");
454 fail = 0;
455
456 /*
457 * Set a read lock at the middle of the file and a
458 * write lock just before
459 */
460 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000461 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
462 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000463 fail = 1;
464 }
465
466 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 5, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000467 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
468 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000469 fail = 1;
470 }
471
472 /*
473 * Test the write lock
474 */
475 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
476 compare_lock(&tl, (short)F_WRLCK, (short)0, 5, 5, parent_pid);
477
478 /*
479 * Test the read lock.
480 */
481 do_test(&tl, (short)F_WRLCK, (short)0, 10, 0);
482 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
483
484 /*
485 * Test to make sure the rest of the file is unlocked.
486 */
487 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
488 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
489
490 /*
491 * remove all the locks set above
492 */
493 unlock_file();
494
495 if (fail) {
496 tst_resm(TINFO, "Test block 4: FAILED");
497 } else {
498 tst_resm(TINFO, "Test block 4: PASSED");
499 }
500 tst_resm(TINFO, "Exit block 4");
501
mridgedb639212005-01-04 21:04:11 +0000502/* //block5: */
robbiew5aab8a72003-03-26 18:05:30 +0000503 tst_resm(TINFO, "Enter block 5");
504 fail = 0;
505
506 /*
507 * Set a read lock in the middle and a write lock that
508 * ends at the first byte of the read lock
509 */
510 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000511 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
512 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000513 fail = 1;
514 }
515
516 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 5, 6) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000517 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
518 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000519 fail = 1;
520 }
521
522 /*
523 * Test write lock
524 */
525 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
526 compare_lock(&tl, (short)F_WRLCK, (short)0, 5, 6, parent_pid);
527
528 /*
529 * Test read lock
530 */
531 do_test(&tl, (short)F_WRLCK, (short)0, 11, 0);
532 compare_lock(&tl, (short)F_RDLCK, (short)0, 11, 4, parent_pid);
533
534 /*
535 * Test to make sure the rest of the file is unlocked.
536 */
537 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
538 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
539
540 /*
541 * remove all the locks set above
542 */
543 unlock_file();
544
545 if (fail) {
546 tst_resm(TINFO, "Test block 5: FAILED");
547 } else {
548 tst_resm(TINFO, "Test block 5: PASSED");
549 }
550 tst_resm(TINFO, "Exit block 5");
551
mridgedb639212005-01-04 21:04:11 +0000552/* //block6: */
robbiew5aab8a72003-03-26 18:05:30 +0000553 tst_resm(TINFO, "Enter block 6");
554 fail = 0;
555
556 /*
557 * Set a read lock on the middle of the file and a write
558 * lock that overlaps the front of the read.
559 */
560 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000561 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
562 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000563 fail = 1;
564 }
565
566 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 5, 8) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000567 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
568 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000569 fail = 1;
570 }
571
572 /*
573 * Test the write lock
574 */
575 do_test(&tl, (short)F_WRLCK, (short)0, 5, 0);
576 compare_lock(&tl, (short)F_WRLCK, (short)0, 5, 8, parent_pid);
577
578 /*
579 * Test the read lock
580 */
581 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
582 compare_lock(&tl, (short)F_RDLCK, (short)0, 13, 2, parent_pid);
583
584 /*
585 * Test to make sure the rest of the file is unlocked.
586 */
587 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
588 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
589
590 /*
591 * remove all the locks set above
592 */
593 unlock_file();
594
595 if (fail) {
596 tst_resm(TINFO, "Test block 6 FAILED");
597 } else {
598 tst_resm(TINFO, "Test block 6 PASSED");
599 }
600 tst_resm(TINFO, "Exit block 6");
601
mridgedb639212005-01-04 21:04:11 +0000602/* //block7: */
robbiew5aab8a72003-03-26 18:05:30 +0000603 tst_resm(TINFO, "Enter block 7");
604 fail = 0;
605
606 /*
607 * Set a read lock in the middle of a file and a write
608 * lock in the middle of it
609 */
610 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 10) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000611 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
612 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000613 fail = 1;
614 }
615
616 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 13, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000617 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
618 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000619 fail = 1;
620 }
621
622 /*
623 * Test the first read lock
624 */
625 do_test(&tl, (short)F_WRLCK, (short)0 , 0, 0);
626 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 3, parent_pid);
627
628 /*
629 * Test the write lock
630 */
631 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
632 compare_lock(&tl, (short)F_WRLCK, (short)0, 13, 5, parent_pid);
633
634 /*
635 * Test the second read lock
636 */
637 do_test(&tl, (short)F_WRLCK, (short)0, 18, 0);
638 compare_lock(&tl, (short)F_RDLCK, (short)0, 18, 2, parent_pid);
639
640 /*
641 * Test to make sure the rest of the file is unlocked
642 */
643 do_test(&tl, (short)F_WRLCK, (short)0, 20, 0);
644 compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, 0);
645
646 /*
647 * remove all the locks set above.
648 */
649 unlock_file();
650 if (fail) {
651 tst_resm(TINFO, "Test block 7: FAILED");
652 } else {
653 tst_resm(TINFO, "Test block 7: PASSED");
654 }
655 tst_resm(TINFO, "Exit block 7");
656
mridgedb639212005-01-04 21:04:11 +0000657/* //block8: */
robbiew5aab8a72003-03-26 18:05:30 +0000658 tst_resm(TINFO, "Enter block 8");
659 fail = 0;
660 /*
661 * Set a read lock in the middle of the file and a write
662 * lock that overlaps the end
663 */
664 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000665 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
666 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000667 fail = 1;
668 }
669
670 /*
671 * Set a write lock on the whole file
672 */
673 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 13, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000674 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
675 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000676 fail = 1;
677 }
678
679 /*
680 * Test the read lock
681 */
682 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
683 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 3, parent_pid);
684
685 /*
686 * Test the write lock
687 */
688 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
689 compare_lock(&tl, (short)F_WRLCK, (short)0, 13, 5, parent_pid);
690
691 /*
692 * Test to make sure the rest of the file is unlocked
693 */
694 do_test(&tl, (short)F_WRLCK, (short)0, 18, 0);
695 compare_lock(&tl, (short)F_UNLCK, (short)0, 18, 0, 0);
696
697 /*
698 * remove all the locks set above
699 */
700 unlock_file();
701
702 if (fail) {
703 tst_resm(TINFO, "Test block 8: FAILED");
704 } else {
705 tst_resm(TINFO, "Test block 8: PASSED");
706 }
707 tst_resm(TINFO, "Exit block 8");
708
mridgedb639212005-01-04 21:04:11 +0000709/* //block9: */
robbiew5aab8a72003-03-26 18:05:30 +0000710 tst_resm(TINFO, "Enter block 9");
711 fail = 0;
712
713 /*
714 * Set a read lock in the middle of the file and a write
715 * lock starting at the last byte of the read lock
716 */
717 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000718 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
719 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000720 fail = 1;
721 }
722
723 /*
724 * Set a write lock on the whole file.
725 */
726 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 14, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000727 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
728 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000729 fail = 1;
730 }
731
732 /*
733 * Test read lock
734 */
735 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
736 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 4, parent_pid);
737
738 /*
739 * Test the write lock
740 */
741 do_test(&tl, (short)F_WRLCK, (short)0, 14, 0);
742 compare_lock(&tl, (short)F_WRLCK, (short)0, 14, 5, parent_pid);
743
744 /*
745 * Test to make sure the end of the file is unlocked
746 */
747 do_test(&tl, (short)F_WRLCK, (short)0, 19, 0);
748 compare_lock(&tl, (short)F_UNLCK, (short)0, 19, 0, 0);
749
750 /*
751 * remove all the locks set above
752 */
753 unlock_file();
754
755 if (fail) {
756 tst_resm(TINFO, "Test block 9: FAILED");
757 } else {
758 tst_resm(TINFO, "Test block 9: PASSED");
759 }
760 tst_resm(TINFO, "Exit block 9");
761
mridgedb639212005-01-04 21:04:11 +0000762/* //block10: */
robbiew5aab8a72003-03-26 18:05:30 +0000763 tst_resm(TINFO, "Enter block 10");
764 fail = 0;
765
766 /*
767 * Set a read lock in the middle of the file and a write
768 * lock that starts just after the last byte of the
769 * read lock.
770 */
771 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000772 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
773 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000774 fail = 1;
775 }
776
777 /*
778 * Set a write lock on the whole file
779 */
780 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 15, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000781 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
782 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000783 fail = 1;
784 }
785
786 /*
787 * Test the read lock
788 */
789 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
790 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
791
792 /*
793 * Test the write lock
794 */
795 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
796 compare_lock(&tl, (short)F_WRLCK, (short)0, 15, 5, parent_pid);
797
798 /*
799 * Test to make sure the rest of the file is unlocked
800 */
801 do_test(&tl, (short)F_WRLCK, (short)0, 20, 0);
802 compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, 0);
803
804 /*
805 * remove all the locks set above
806 */
807 unlock_file();
808
809 if (fail) {
810 tst_resm(TINFO, "Test block 10: FAILED");
811 } else {
812 tst_resm(TINFO, "Test block 10: PASSED");
813 }
814 tst_resm(TINFO, "Exit block 10");
815
mridgedb639212005-01-04 21:04:11 +0000816/* //block11: */
robbiew5aab8a72003-03-26 18:05:30 +0000817 tst_resm(TINFO, "Enter block 11");
818 fail = 0;
819
820 /*
821 * Set a read lock at the middle of the file and a write
822 * lock that starts past the end of the read lock.
823 */
824 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000825 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
826 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000827 fail = 1;
828 }
829
830 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 16, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000831 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
832 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000833 fail = 1;
834 }
835
836 /*
837 * Test the read lock
838 */
839 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
840 compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
841
842 /*
843 * Test that byte in between is unlocked
844 */
845 do_test(&tl, (short)F_WRLCK, (short)0, 15, 1);
846 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 1, 0);
847
848 /*
849 * Test the write lock
850 */
851 do_test(&tl, (short)F_WRLCK, (short)0, 16, 0);
852 compare_lock(&tl, (short)F_WRLCK, (short)0, 16, 5, parent_pid);
853
854 /*
855 * Test to make sure the rest of the file is unlocked
856 */
857 do_test(&tl, (short)F_WRLCK, (short)0, 21, 0);
858 compare_lock(&tl, (short)F_UNLCK, (short)0, 21, 0, 0);
859
860 /*
861 * remove all the locks set above
862 */
863 unlock_file();
864
865 if (fail) {
866 tst_resm(TINFO, "Test block 11: FAILED");
867 } else {
868 tst_resm(TINFO, "Test block 11: PASSED");
869 }
870 tst_resm(TINFO, "Exit block 11");
871
872 stop_child();
873 close(fd);
874 }
875 cleanup();
876 return(0);
877}