blob: c98756cfd75846b35221ddfb851fe1dbf4994682 [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
Garrett Cooper0a643cb2010-12-21 11:21:19 -080022 * fcntl19.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 * Testcase to check locking of regions of a file
plars865695b2001-08-27 22:15:12 +000026 *
27 * CALLS
subrata_modak56207ce2009-03-23 13:35:39 +000028 * fcntl
plars865695b2001-08-27 22:15:12 +000029 *
30 * ALGORITHM
subrata_modak56207ce2009-03-23 13:35:39 +000031 * Test unlocking sections around a write lock
plars865695b2001-08-27 22:15:12 +000032 *
33 * USAGE
subrata_modak56207ce2009-03-23 13:35:39 +000034 * fcntl19
plars865695b2001-08-27 22:15:12 +000035 *
36 * HISTORY
37 * 07/2001 Ported by Wayne Boyer
38 *
39 * RESTRICTIONS
subrata_modak56207ce2009-03-23 13:35:39 +000040 * None
plars865695b2001-08-27 22:15:12 +000041 */
42
43#include <fcntl.h>
44#include <errno.h>
45#include <signal.h>
robbiew5aab8a72003-03-26 18:05:30 +000046#include <sys/stat.h>
47#include <sys/types.h>
mridgedb639212005-01-04 21:04:11 +000048#include <sys/wait.h>
subrata_modak923b23f2009-11-02 13:57:16 +000049#include <inttypes.h>
Garrett Cooper0a643cb2010-12-21 11:21:19 -080050
robbiew5aab8a72003-03-26 18:05:30 +000051#include "test.h"
Garrett Cooper0a643cb2010-12-21 11:21:19 -080052#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000053
54#define STRINGSIZE 27
55#define STRING "abcdefghijklmnopqrstuvwxyz\n"
56#define STOP 0xFFF0
57
58int parent_pipe[2];
59int child_pipe[2];
60int fd;
plarsa1e518d2002-09-09 18:23:52 +000061pid_t parent_pid, child_pid;
plars865695b2001-08-27 22:15:12 +000062
63void parent_put();
64void parent_get();
65void child_put();
66void child_get();
67void stop_child();
plarsa1e518d2002-09-09 18:23:52 +000068void compare_lock(struct flock *, short, short, int, int, pid_t);
plars865695b2001-08-27 22:15:12 +000069void unlock_file();
70void do_test(struct flock *, short, short, int, int);
71void catch_child();
72char *str_type();
subrata_modak56207ce2009-03-23 13:35:39 +000073int do_lock(int, short, short, int, int);
plars865695b2001-08-27 22:15:12 +000074
nstrazfa31d552002-05-14 16:50:06 +000075char *TCID = "fcntl19";
plars865695b2001-08-27 22:15:12 +000076int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000077
78void setup(void);
79void cleanup(void);
80
81int fail = 0;
82
Mike Frysingerc57fba52014-04-09 18:56:30 -040083void setup(void)
plars865695b2001-08-27 22:15:12 +000084{
85 char *buf = STRING;
subrata_modak56207ce2009-03-23 13:35:39 +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
Garrett Cooper0a643cb2010-12-21 11:21:19 -080095 SAFE_PIPE(cleanup, parent_pipe);
96 SAFE_PIPE(cleanup, child_pipe);
plars865695b2001-08-27 22:15:12 +000097 parent_pid = getpid();
robbiew54f7a712003-04-16 19:20:30 +000098
99 tst_tmpdir();
subrata_modakbdbaec52009-02-26 12:14:51 +0000100
robbiewdad22712003-04-16 19:14:05 +0000101 snprintf(template, PATH_MAX, "fcntl19XXXXXX");
plars865695b2001-08-27 22:15:12 +0000102
robbiewdad22712003-04-16 19:14:05 +0000103 if ((fd = mkstemp(template)) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000104 tst_resm(TFAIL, "Couldn't open temp file! errno = %d", errno);
105 }
plars865695b2001-08-27 22:15:12 +0000106
subrata_modak56207ce2009-03-23 13:35:39 +0000107 if (write(fd, buf, STRINGSIZE) < 0) {
108 tst_resm(TFAIL, "Couldn't write to temp file! errno = %d",
109 errno);
110 }
plars865695b2001-08-27 22:15:12 +0000111
robbiew1e4cf0c2005-02-24 17:03:42 +0000112 memset(&act, 0, sizeof(act));
113 act.sa_handler = catch_child;
114 sigemptyset(&act.sa_mask);
115 sigaddset(&act.sa_mask, SIGCLD);
116 if ((sigaction(SIGCLD, &act, NULL)) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000117 tst_resm(TFAIL, "SIGCLD signal setup failed, errno: %d", errno);
plars865695b2001-08-27 22:15:12 +0000118 fail = 1;
119 }
120}
121
Mike Frysingerc57fba52014-04-09 18:56:30 -0400122void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000123{
robbiew54f7a712003-04-16 19:20:30 +0000124 tst_rmdir();
plars865695b2001-08-27 22:15:12 +0000125
plars865695b2001-08-27 22:15:12 +0000126}
127
Mike Frysingerc57fba52014-04-09 18:56:30 -0400128void do_child(void)
plars865695b2001-08-27 22:15:12 +0000129{
130 struct flock fl;
131
132 close(parent_pipe[1]);
133 close(child_pipe[0]);
subrata_modak56207ce2009-03-23 13:35:39 +0000134 while (1) {
plars865695b2001-08-27 22:15:12 +0000135 child_get(&fl);
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800136 if (fcntl(fd, F_GETLK, &fl) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800137 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
plars865695b2001-08-27 22:15:12 +0000138 child_put(&fl);
139 }
140}
141
robbiew5aab8a72003-03-26 18:05:30 +0000142int do_lock(int cmd, short type, short whence, int start, int len)
plars865695b2001-08-27 22:15:12 +0000143{
144 struct flock fl;
145
146 fl.l_type = type;
147 fl.l_whence = whence;
148 fl.l_start = start;
149 fl.l_len = len;
subrata_modak56207ce2009-03-23 13:35:39 +0000150 return (fcntl(fd, cmd, &fl));
plars865695b2001-08-27 22:15:12 +0000151}
152
subrata_modak56207ce2009-03-23 13:35:39 +0000153void do_test(struct flock *fl, short type, short whence, int start, int len)
plars865695b2001-08-27 22:15:12 +0000154{
155 fl->l_type = type;
156 fl->l_whence = whence;
157 fl->l_start = start;
158 fl->l_len = len;
159 fl->l_pid = (short)0;
160
161 parent_put(fl);
162 parent_get(fl);
163}
164
165void
166compare_lock(struct flock *fl, short type, short whence, int start, int len,
plarsa1e518d2002-09-09 18:23:52 +0000167 pid_t pid)
plars865695b2001-08-27 22:15:12 +0000168{
169 if (fl->l_type != type) {
170 tst_resm(TFAIL, "lock type is wrong should be %s is %s",
171 str_type(type), str_type(fl->l_type));
172 fail = 1;
173 }
174
175 if (fl->l_whence != whence) {
176 tst_resm(TFAIL, "lock whence is wrong should be %d is %d",
177 whence, fl->l_whence);
178 fail = 1;
179 }
180
181 if (fl->l_start != start) {
182 tst_resm(TFAIL, "region starts in wrong place, should be"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800183 "%d is %" PRId64, start, (int64_t) fl->l_start);
plars865695b2001-08-27 22:15:12 +0000184 fail = 1;
185 }
186
187 if (fl->l_len != len) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800188 tst_resm(TFAIL,
189 "region length is wrong, should be %d is %" PRId64,
190 len, (int64_t) fl->l_len);
plars865695b2001-08-27 22:15:12 +0000191 fail = 1;
192 }
193
194 if (fl->l_pid != pid) {
195 tst_resm(TFAIL, "locking pid is wrong, should be %d is %d",
196 pid, fl->l_pid);
197 fail = 1;
198 }
199}
200
Mike Frysingerc57fba52014-04-09 18:56:30 -0400201void unlock_file(void)
plars865695b2001-08-27 22:15:12 +0000202{
203 struct flock fl;
204
205 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 0, 0) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000206 tst_resm(TFAIL, "fcntl on file failed, errno =%d", errno);
plars865695b2001-08-27 22:15:12 +0000207 fail = 1;
208 }
209 do_test(&fl, F_WRLCK, 0, 0, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000210 compare_lock(&fl, (short)F_UNLCK, (short)0, 0, 0, (pid_t) 0);
plars865695b2001-08-27 22:15:12 +0000211}
212
subrata_modak56207ce2009-03-23 13:35:39 +0000213char *str_type(int type)
plars865695b2001-08-27 22:15:12 +0000214{
215 static char buf[20];
216
217 switch (type) {
218 case 1:
subrata_modak56207ce2009-03-23 13:35:39 +0000219 return ("F_RDLCK");
plars865695b2001-08-27 22:15:12 +0000220 case 2:
subrata_modak56207ce2009-03-23 13:35:39 +0000221 return ("F_WRLCK");
plars865695b2001-08-27 22:15:12 +0000222 case 3:
subrata_modak56207ce2009-03-23 13:35:39 +0000223 return ("F_UNLCK");
plars865695b2001-08-27 22:15:12 +0000224 default:
225 sprintf(buf, "BAD VALUE: %d", type);
subrata_modak56207ce2009-03-23 13:35:39 +0000226 return (buf);
plars865695b2001-08-27 22:15:12 +0000227 }
228}
229
subrata_modak56207ce2009-03-23 13:35:39 +0000230void parent_put(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000231{
232 if (write(parent_pipe[1], l, sizeof(*l)) != sizeof(*l)) {
233 tst_resm(TFAIL, "couldn't send message to child");
234 fail = 1;
235 }
236}
237
subrata_modak56207ce2009-03-23 13:35:39 +0000238void parent_get(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000239{
240 if (read(child_pipe[0], l, sizeof(*l)) != sizeof(*l)) {
241 tst_resm(TFAIL, "couldn't get message from child");
242 fail = 1;
243 }
244}
245
subrata_modak56207ce2009-03-23 13:35:39 +0000246void child_put(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000247{
248 if (write(child_pipe[1], l, sizeof(*l)) != sizeof(*l)) {
249 tst_resm(TFAIL, "couldn't send message to parent");
250 fail = 1;
251 }
252}
253
subrata_modak56207ce2009-03-23 13:35:39 +0000254void child_get(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000255{
256 if (read(parent_pipe[0], l, sizeof(*l)) != sizeof(*l)) {
257 tst_resm(TFAIL, "couldn't get message from parent");
258 cleanup();
259 } else if (l->l_type == (short)STOP) {
260 exit(0);
261 }
262}
263
Mike Frysingerc57fba52014-04-09 18:56:30 -0400264void stop_child(void)
plars865695b2001-08-27 22:15:12 +0000265{
266 struct flock fl;
267
Mike Frysingere61ddba2014-04-09 23:24:32 -0400268 signal(SIGCLD, SIG_DFL);
plars865695b2001-08-27 22:15:12 +0000269 fl.l_type = STOP;
270 parent_put(&fl);
271 wait(0);
272}
273
Mike Frysingerc57fba52014-04-09 18:56:30 -0400274void catch_child(void)
plars865695b2001-08-27 22:15:12 +0000275{
276 tst_resm(TFAIL, "Unexpected death of child process");
277 cleanup();
278}
robbiew5aab8a72003-03-26 18:05:30 +0000279
280int main(int ac, char **av)
281{
282 struct flock tl;
283
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200284 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200285 const char *msg;
robbiew5aab8a72003-03-26 18:05:30 +0000286
Garrett Cooper45e285d2010-11-22 12:19:25 -0800287 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800288 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew5aab8a72003-03-26 18:05:30 +0000289 }
robbiewd34d5812005-07-11 22:28:09 +0000290#ifdef UCLINUX
291 maybe_run_child(&do_child, "ddddd", &parent_pipe[0], &parent_pipe[1],
292 &child_pipe[0], &child_pipe[1], &fd);
293#endif
294
subrata_modak56207ce2009-03-23 13:35:39 +0000295 setup(); /* global setup */
robbiew5aab8a72003-03-26 18:05:30 +0000296
297 /* Check for looping state if -i option is given */
298 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800299 /* reset tst_count in case we are looping */
300 tst_count = 0;
robbiew5aab8a72003-03-26 18:05:30 +0000301
robbiewd34d5812005-07-11 22:28:09 +0000302 if ((child_pid = FORK_OR_VFORK()) == 0) { /* child */
303#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +0000304 if (self_exec
305 (av[0], "ddddd", parent_pipe[0], parent_pipe[1],
306 child_pipe[0], child_pipe[1], fd) < 0) {
robbiewd34d5812005-07-11 22:28:09 +0000307 tst_resm(TFAIL, "self_exec failed");
308 cleanup();
309 }
310#else
robbiew5aab8a72003-03-26 18:05:30 +0000311 do_child();
robbiewd34d5812005-07-11 22:28:09 +0000312#endif
robbiew5aab8a72003-03-26 18:05:30 +0000313 } else if (child_pid < 0) {
314 tst_resm(TFAIL, "Fork failed");
315 cleanup();
316 }
317
318 /* parent */
319
320 (void)close(parent_pipe[0]);
321 (void)close(child_pipe[1]);
322
mridgedb639212005-01-04 21:04:11 +0000323/* //block1: */
robbiew5aab8a72003-03-26 18:05:30 +0000324 tst_resm(TINFO, "Enter block 1");
325 /*
326 * Add a write lock to the middle of the file and unlock a
327 * section just before the lock
328 */
329 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000330 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
331 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000332 fail = 1;
333 }
334
335 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 5, 5) < 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 write lock
343 */
344 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
345 compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 5, parent_pid);
346
347 /*
348 * Test that the rest of the file is unlocked
349 */
350 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000351 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, (pid_t) 0);
robbiew5aab8a72003-03-26 18:05:30 +0000352
353 /*
354 * remove all the locks set above
355 */
356 unlock_file();
357
358 if (fail) {
359 tst_resm(TINFO, "Test block 1: FAILED");
360 } else {
361 tst_resm(TINFO, "Test block 1: PASSED");
362 }
363 tst_resm(TINFO, "Exit block 1");
364
mridgedb639212005-01-04 21:04:11 +0000365/* //block2: */
robbiew5aab8a72003-03-26 18:05:30 +0000366 tst_resm(TINFO, "Enter block 2");
367 fail = 0;
368 /*
369 * Set a write lock in the middle and do an unlock that
370 * ends at the first byte of the write lock.
371 */
372 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000373 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
374 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000375 fail = 1;
376 }
377
378 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 5, 6) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000379 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
380 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000381 fail = 1;
382 }
383
384 /*
385 * Test write lock
386 */
387 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
388 compare_lock(&tl, (short)F_WRLCK, (short)0, 11, 4, parent_pid);
389
390 /*
391 * Test to make sure the rest of the file is unlocked
392 */
393 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000394 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, (pid_t) 0);
robbiew5aab8a72003-03-26 18:05:30 +0000395
396 /*
397 * remove all the locks set above
398 */
399 unlock_file();
400
401 if (fail) {
402 tst_resm(TINFO, "Test block 2: FAILED");
403 } else {
404 tst_resm(TINFO, "Test block 2: PASSED");
405 }
406 tst_resm(TINFO, "Exit block 2");
407
mridgedb639212005-01-04 21:04:11 +0000408/* //block3: */
robbiew5aab8a72003-03-26 18:05:30 +0000409 tst_resm(TINFO, "Enter block 3");
410 fail = 0;
411
412 /*
413 * Set a write lock on the middle of the file and do an
414 * unlock that overlaps the front of the write
415 */
416 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 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 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 5, 8) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000423 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
424 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000425 fail = 1;
426 }
427
428 /*
429 * Test the write lock
430 */
431 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
432 compare_lock(&tl, (short)F_WRLCK, (short)0, 13, 2, parent_pid);
433
434 /*
435 * Test to make sure the rest of the file is unlocked
436 */
437 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000438 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, (pid_t) 0);
robbiew5aab8a72003-03-26 18:05:30 +0000439
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 blcok 4");
454 fail = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000455
robbiew5aab8a72003-03-26 18:05:30 +0000456 /*
457 * Set a write a lock in the middle of a file and unlock a
458 * section in the middle of it
459 */
460 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 10) < 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_UNLCK, (short)0, 13, 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 first write lock
474 */
475 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
476 compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 3, parent_pid);
477
478 /*
479 * Test the second write lock
480 */
481 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
482 compare_lock(&tl, (short)F_WRLCK, (short)0, 18, 2, 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, 20, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000488 compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, (pid_t) 0);
robbiew5aab8a72003-03-26 18:05:30 +0000489
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 write lock in the middle of the file and do a
508 * unlock that overlaps the end
509 */
510 if (do_lock(F_SETLK, (short)F_WRLCK, (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_UNLCK, (short)0, 13, 5) < 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 the write lock
524 */
525 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
526 compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 3, parent_pid);
527
528 /*
529 * Test to make sure the rest of the file is unlocked
530 */
531 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000532 compare_lock(&tl, (short)F_UNLCK, (short)0, 13, 0, (pid_t) 0);
robbiew5aab8a72003-03-26 18:05:30 +0000533
534 /*
535 * remove all the locks set above
536 */
537 unlock_file();
538
539 if (fail) {
540 tst_resm(TINFO, "Test block 5: FAILED");
541 } else {
542 tst_resm(TINFO, "Test block 5: PASSED");
543 }
544 tst_resm(TINFO, "Exit block 5");
545
mridgedb639212005-01-04 21:04:11 +0000546/* //block6: */
robbiew5aab8a72003-03-26 18:05:30 +0000547 tst_resm(TINFO, "Enter block 6");
548 fail = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000549
robbiew5aab8a72003-03-26 18:05:30 +0000550 /*
551 * Set write lock in the middle of the file and do an unlock
552 * starting at the last byte of the write lock
553 */
554 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0) {
robbiewdad22712003-04-16 19:14:05 +0000555 tst_resm(TFAIL, "fcntl on file failed, errno =%d",
556 errno);
robbiew5aab8a72003-03-26 18:05:30 +0000557 fail = 1;
558 }
559
560 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 14, 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 /*
567 * Test write lock
568 */
569 do_test(&tl, (short)F_WRLCK, (short)0, 10, 0);
570 compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 4, parent_pid);
571
572 /*
573 * Test to make sure the end of the file is unlocked
574 */
575 do_test(&tl, (short)F_WRLCK, (short)0, 14, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000576 compare_lock(&tl, (short)F_UNLCK, (short)0, 14, 0, (pid_t) 0);
robbiew5aab8a72003-03-26 18:05:30 +0000577
578 /*
579 * remove all the locks set above
580 */
581 unlock_file();
582
583 if (fail) {
584 tst_resm(TINFO, "Test block 6: FAILED");
585 } else {
586 tst_resm(TINFO, "Test block 6: PASSED");
587 }
588 tst_resm(TINFO, "Exit block 6");
589
mridgedb639212005-01-04 21:04:11 +0000590/* //block7: */
robbiew5aab8a72003-03-26 18:05:30 +0000591 tst_resm(TINFO, "Enter block 7");
592 fail = 0;
593
594 /*
595 * Set a write lock at the middle of the file and do an
596 * unlock that starts at the byte past the end of the write
597 * lock
598 */
599 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 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_UNLCK, (short)0, 16, 0) < 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 write lock
613 */
614 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
615 compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 5, parent_pid);
616
617 /*
618 * Test to make sure the rest of the file is unlocked
619 */
620 do_test(&tl, (short)F_WRLCK, (short)0, 16, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000621 compare_lock(&tl, (short)F_UNLCK, (short)0, 16, 0, (pid_t) 0);
robbiew5aab8a72003-03-26 18:05:30 +0000622
623 /*
624 * remove all the locks set above
625 */
626 unlock_file();
627
628 if (fail) {
629 tst_resm(TINFO, "Test block 7: FAILED");
630 } else {
631 tst_resm(TINFO, "Test block 7: PASSED");
632 }
633
634 tst_resm(TINFO, "Exit block 7");
635
636 stop_child();
637 close(fd);
638 }
639 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800640 tst_exit();
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800641}