blob: 006a6c421c7fd72430c0757a6f944d92c09238ab [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 * fcntl11.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * 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 * ALGORITHM
subrata_modak56207ce2009-03-23 13:35:39 +000028 * Test changing lock sections around a write lock
plars865695b2001-08-27 22:15:12 +000029 *
30 * USAGE
subrata_modak56207ce2009-03-23 13:35:39 +000031 * fcntl11
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>
robbiew4cf80962003-03-25 21:53:41 +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>
robbiew4cf80962003-03-25 21:53:41 +000047#include "test.h"
Garrett Cooper0a643cb2010-12-21 11:21:19 -080048#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000049
50#define STRINGSIZE 27
51#define STRING "abcdefghijklmnopqrstuvwxyz\n"
52#define STOP 0xFFF0
53
54int parent_pipe[2];
55int child_pipe[2];
56int fd;
plarsa1e518d2002-09-09 18:23:52 +000057pid_t parent_pid, child_pid;
plars865695b2001-08-27 22:15:12 +000058
59void parent_put();
60void parent_get();
61void child_put();
62void child_get();
63void stop_child();
plarsa1e518d2002-09-09 18:23:52 +000064void compare_lock(struct flock *, short, short, int, int, pid_t);
plars865695b2001-08-27 22:15:12 +000065void unlock_file();
66void do_test(struct flock *, short, short, int, int);
67void catch_child();
68char *str_type();
69int do_lock(int, short, short, int, int);
70
71char *TCID = "fcntl11";
72int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000073
plars865695b2001-08-27 22:15:12 +000074int fail;
75
Mike Frysingerc57fba52014-04-09 18:56:30 -040076void cleanup(void)
plars865695b2001-08-27 22:15:12 +000077{
subrata_modak56207ce2009-03-23 13:35:39 +000078 tst_rmdir();
plars865695b2001-08-27 22:15:12 +000079
plars865695b2001-08-27 22:15:12 +000080}
81
Mike Frysingerc57fba52014-04-09 18:56:30 -040082void setup(void)
plars865695b2001-08-27 22:15:12 +000083{
84 char *buf = STRING;
robbiew83f259b2003-04-16 18:58:09 +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);
subrata_modak56207ce2009-03-23 13:35:39 +000089 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +000090
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();
robbiew83f259b2003-04-16 18:58:09 +000098 snprintf(template, PATH_MAX, "fcntl11XXXXXX");
plars865695b2001-08-27 22:15:12 +000099
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800100 if ((fd = mkstemp(template)) < 0)
subrata_modak56207ce2009-03-23 13:35:39 +0000101 tst_resm(TFAIL, "Couldn't open temp file! errno = %d", errno);
plars865695b2001-08-27 22:15:12 +0000102
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800103 SAFE_WRITE(cleanup, 0, fd, buf, STRINGSIZE);
plars865695b2001-08-27 22:15:12 +0000104
robbiew1e4cf0c2005-02-24 17:03:42 +0000105 memset(&act, 0, sizeof(act));
106 act.sa_handler = catch_child;
107 sigemptyset(&act.sa_mask);
108 sigaddset(&act.sa_mask, SIGCLD);
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800109 if ((sigaction(SIGCLD, &act, NULL)) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110 tst_brkm(TBROK | TERRNO, cleanup,
111 "sigaction(SIGCLD, ..) failed");
plars865695b2001-08-27 22:15:12 +0000112}
113
Mike Frysingerc57fba52014-04-09 18:56:30 -0400114void do_child(void)
plars865695b2001-08-27 22:15:12 +0000115{
116 struct flock fl;
117
118 close(parent_pipe[1]);
119 close(child_pipe[0]);
subrata_modak56207ce2009-03-23 13:35:39 +0000120 while (1) {
plars865695b2001-08-27 22:15:12 +0000121 child_get(&fl);
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800122 if (fcntl(fd, F_GETLK, &fl) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800123 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
plars865695b2001-08-27 22:15:12 +0000124 child_put(&fl);
125 }
126}
127
robbiew4cf80962003-03-25 21:53:41 +0000128int do_lock(int cmd, short type, short whence, int start, int len)
plars865695b2001-08-27 22:15:12 +0000129{
130 struct flock fl;
131
132 fl.l_type = type;
133 fl.l_whence = whence;
134 fl.l_start = start;
135 fl.l_len = len;
subrata_modak56207ce2009-03-23 13:35:39 +0000136 return (fcntl(fd, cmd, &fl));
plars865695b2001-08-27 22:15:12 +0000137}
138
subrata_modak56207ce2009-03-23 13:35:39 +0000139void do_test(struct flock *fl, short type, short whence, int start, int len)
plars865695b2001-08-27 22:15:12 +0000140{
141 fl->l_type = type;
142 fl->l_whence = whence;
143 fl->l_start = start;
144 fl->l_len = len;
145 fl->l_pid = (short)0;
146
147 parent_put(fl);
148 parent_get(fl);
149}
150
151void
152compare_lock(struct flock *fl, short type, short whence, int start, int len,
plarsa1e518d2002-09-09 18:23:52 +0000153 pid_t pid)
plars865695b2001-08-27 22:15:12 +0000154{
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800155 if (fl->l_type != type)
plars865695b2001-08-27 22:15:12 +0000156 tst_resm(TFAIL, "lock type is wrong should be %s is %s",
157 str_type(type), str_type(fl->l_type));
plars865695b2001-08-27 22:15:12 +0000158
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800159 if (fl->l_whence != whence)
plars865695b2001-08-27 22:15:12 +0000160 tst_resm(TFAIL, "lock whence is wrong should be %d is %d",
161 whence, fl->l_whence);
plars865695b2001-08-27 22:15:12 +0000162
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800163 if (fl->l_start != start)
subrata_modak56207ce2009-03-23 13:35:39 +0000164 tst_resm(TFAIL, "region starts in wrong place, should be "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800165 "%d is %" PRId64, start, (int64_t) fl->l_start);
plars865695b2001-08-27 22:15:12 +0000166
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800167 if (fl->l_len != len)
168 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169 "region length is wrong, should be %d is %" PRId64,
170 len, (int64_t) fl->l_len);
plars865695b2001-08-27 22:15:12 +0000171
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800172 if (fl->l_pid != pid)
plars865695b2001-08-27 22:15:12 +0000173 tst_resm(TFAIL, "locking pid is wrong, should be %d is %d",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174 pid, fl->l_pid);
plars865695b2001-08-27 22:15:12 +0000175}
176
Mike Frysingerc57fba52014-04-09 18:56:30 -0400177void unlock_file(void)
plars865695b2001-08-27 22:15:12 +0000178{
179 struct flock fl;
180
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800181 if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 0, 0) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800182 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
plars865695b2001-08-27 22:15:12 +0000183 do_test(&fl, F_WRLCK, 0, 0, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000184 compare_lock(&fl, (short)F_UNLCK, (short)0, 0, 0, (pid_t) 0);
plars865695b2001-08-27 22:15:12 +0000185}
186
subrata_modak56207ce2009-03-23 13:35:39 +0000187char *str_type(int type)
plars865695b2001-08-27 22:15:12 +0000188{
189 static char buf[20];
190
191 switch (type) {
subrata_modak56207ce2009-03-23 13:35:39 +0000192 case 0:
193 return ("F_RDLCK");
194 case 1:
195 return ("F_WRLCK");
196 case 2:
197 return ("F_UNLCK");
plars865695b2001-08-27 22:15:12 +0000198 default:
199 sprintf(buf, "BAD VALUE: %d", type);
subrata_modak56207ce2009-03-23 13:35:39 +0000200 return (buf);
plars865695b2001-08-27 22:15:12 +0000201 }
202}
203
subrata_modak56207ce2009-03-23 13:35:39 +0000204void parent_put(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000205{
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800206 SAFE_WRITE(cleanup, 1, parent_pipe[1], l, sizeof(*l));
plars865695b2001-08-27 22:15:12 +0000207}
208
subrata_modak56207ce2009-03-23 13:35:39 +0000209void parent_get(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000210{
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800211 SAFE_READ(cleanup, 1, child_pipe[0], l, sizeof(*l));
plars865695b2001-08-27 22:15:12 +0000212}
213
subrata_modak56207ce2009-03-23 13:35:39 +0000214void child_put(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000215{
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800216 SAFE_WRITE(NULL, 1, child_pipe[1], l, sizeof(*l));
plars865695b2001-08-27 22:15:12 +0000217}
218
subrata_modak56207ce2009-03-23 13:35:39 +0000219void child_get(struct flock *l)
plars865695b2001-08-27 22:15:12 +0000220{
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800221 SAFE_READ(NULL, 1, parent_pipe[0], l, sizeof(*l));
222 if (l->l_type == (short)STOP)
plars865695b2001-08-27 22:15:12 +0000223 exit(0);
plars865695b2001-08-27 22:15:12 +0000224}
225
Mike Frysingerc57fba52014-04-09 18:56:30 -0400226void stop_child(void)
plars865695b2001-08-27 22:15:12 +0000227{
228 struct flock fl;
229
Mike Frysingere61ddba2014-04-09 23:24:32 -0400230 signal(SIGCLD, SIG_DFL);
plars865695b2001-08-27 22:15:12 +0000231 fl.l_type = STOP;
232 parent_put(&fl);
233 wait(0);
234}
235
Mike Frysingerc57fba52014-04-09 18:56:30 -0400236void catch_child(void)
plars865695b2001-08-27 22:15:12 +0000237{
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800238 tst_brkm(TFAIL, cleanup, "Unexpected death of child process");
plars865695b2001-08-27 22:15:12 +0000239}
robbiew4cf80962003-03-25 21:53:41 +0000240
241int main(int ac, char **av)
242{
243 struct flock tl;
244
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200245 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200246 const char *msg;
robbiew4cf80962003-03-25 21:53:41 +0000247
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800248 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800249 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewd34d5812005-07-11 22:28:09 +0000250#ifdef UCLINUX
251 maybe_run_child(&do_child, "ddddd", &parent_pipe[0],
252 &parent_pipe[1], &child_pipe[0], &child_pipe[1], &fd);
253#endif
254
subrata_modak56207ce2009-03-23 13:35:39 +0000255 setup(); /* global setup */
robbiew4cf80962003-03-25 21:53:41 +0000256
257 /* Check for looping state if -i option is given */
258 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800259 /* reset tst_count in case we are looping */
260 tst_count = 0;
robbiew4cf80962003-03-25 21:53:41 +0000261
robbiewd34d5812005-07-11 22:28:09 +0000262 if ((child_pid = FORK_OR_VFORK()) == 0) { /* parent */
263#ifdef UCLINUX
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800264 if (self_exec(av[0], "ddddd", parent_pipe[0],
Wanlong Gao354ebb42012-12-07 10:10:04 +0800265 parent_pipe[1], child_pipe[0],
266 child_pipe[1], fd) < 0)
267 tst_brkm(TBROK | TERRNO, cleanup,
268 "self_exec failed");
robbiewd34d5812005-07-11 22:28:09 +0000269#else
robbiew4cf80962003-03-25 21:53:41 +0000270 do_child();
robbiewd34d5812005-07-11 22:28:09 +0000271#endif
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800272 } else if (child_pid == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800273 tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
robbiew4cf80962003-03-25 21:53:41 +0000274
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800275 SAFE_CLOSE(cleanup, parent_pipe[0]);
276 SAFE_CLOSE(cleanup, child_pipe[1]);
robbiew4cf80962003-03-25 21:53:41 +0000277
mridgedb639212005-01-04 21:04:11 +0000278/* //block1: */
robbiew4cf80962003-03-25 21:53:41 +0000279 tst_resm(TINFO, "Enter block 1");
280
281 /*
subrata_modak4bb656a2009-02-26 12:02:09 +0000282 * Add a write lock to the middle of the file and a read
robbiew4cf80962003-03-25 21:53:41 +0000283 * at the begining
284 */
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800285 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800286 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000287
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800288 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 1, 5) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800289 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000290
291 /*
292 * Test read lock
293 */
294 do_test(&tl, F_WRLCK, 0, 0, 0);
295 compare_lock(&tl, (short)F_RDLCK, (short)0, 1, 5, parent_pid);
296
297 /*
298 * Test write lock
299 */
300 do_test(&tl, F_WRLCK, 0, 6, 0);
301 compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 5, parent_pid);
302
303 /*
304 * Test that the rest of the file is unlocked
305 */
306 do_test(&tl, F_WRLCK, 0, 15, 0);
307 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
308
309 /*
310 * remove all the locks set above
311 */
312 unlock_file();
313
robbiew4cf80962003-03-25 21:53:41 +0000314 tst_resm(TINFO, "Exit block 1");
315
mridgedb639212005-01-04 21:04:11 +0000316/* //block2: */
robbiew4cf80962003-03-25 21:53:41 +0000317 tst_resm(TINFO, "Enter block 2");
subrata_modak56207ce2009-03-23 13:35:39 +0000318
robbiew4cf80962003-03-25 21:53:41 +0000319 /*
320 * Set a write lock at the middle of the file and a
321 * read lock just before
322 */
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800323 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800324 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000325
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800326 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 5, 5) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800327 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000328
329 /*
330 * Test the read lock
331 */
332 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
333 compare_lock(&tl, (short)F_RDLCK, (short)0, 5, 5, parent_pid);
334
335 /*
336 * Test the write lock.
337 */
338 do_test(&tl, (short)F_WRLCK, (short)0, 10, 0);
339 compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 5, parent_pid);
340
341 /*
342 * Test to make sure the rest of the file is unlocked.
343 */
344 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
345 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
346
347 /*
348 * remove all the locks set above
349 */
350 unlock_file();
351
robbiew4cf80962003-03-25 21:53:41 +0000352 tst_resm(TINFO, "Exit block 2");
353
mridgedb639212005-01-04 21:04:11 +0000354/* //block3: */
robbiew4cf80962003-03-25 21:53:41 +0000355 tst_resm(TINFO, "Enter block 3");
robbiew4cf80962003-03-25 21:53:41 +0000356
357 /*
358 * Set a write lock in the middle and a read lock that
359 * ends at the first byte of the write lock
360 */
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800361 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800362 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000363
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800364 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 5, 6) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800365 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000366
367 /*
368 * Test read lock
369 */
370 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
371 compare_lock(&tl, (short)F_RDLCK, (short)0, 5, 6, parent_pid);
372
373 /*
374 * Test write lock
375 */
376 do_test(&tl, (short)F_WRLCK, (short)0, 11, 0);
377 compare_lock(&tl, (short)F_WRLCK, (short)0, 11, 4, parent_pid);
378
379 /*
380 * Test to make sure the rest of the file is unlocked.
381 */
382 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
383 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
384
385 /*
386 * remove all the locks set above
387 */
388 unlock_file();
389
robbiew4cf80962003-03-25 21:53:41 +0000390 tst_resm(TINFO, "Exit block 3");
391
mridgedb639212005-01-04 21:04:11 +0000392/* //block4: */
robbiew4cf80962003-03-25 21:53:41 +0000393 tst_resm(TINFO, "Enter block 4");
robbiew4cf80962003-03-25 21:53:41 +0000394
395 /*
396 * Set a write lock on the middle of the file and a read
397 * lock that overlaps the front of the write.
398 */
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800399 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800400 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000401
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800402 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 5, 8) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800403 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000404
405 /*
406 * Test the read lock
407 */
408 do_test(&tl, (short)F_WRLCK, (short)0, 5, 0);
409 compare_lock(&tl, (short)F_RDLCK, (short)0, 5, 8, parent_pid);
410
411 /*
412 * Test the write lock
413 */
414 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
415 compare_lock(&tl, (short)F_WRLCK, (short)0, 13, 2, parent_pid);
416
417 /*
418 * Test to make sure the rest of the file is unlocked.
419 */
420 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
421 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
422
423 /*
424 * remove all the locks set above
425 */
426 unlock_file();
427
robbiew4cf80962003-03-25 21:53:41 +0000428 tst_resm(TINFO, "Exit block 4");
429
mridgedb639212005-01-04 21:04:11 +0000430/* //block5: */
robbiew4cf80962003-03-25 21:53:41 +0000431 tst_resm(TINFO, "Enter block 5");
robbiew4cf80962003-03-25 21:53:41 +0000432
433 /*
434 * Set a write lock in the middle of a file and a read
435 * lock in the middle of it
436 */
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800437 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 10) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800438 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000439
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800440 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 13, 5) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800441 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000442
443 /*
444 * Test the first write lock
445 */
subrata_modak56207ce2009-03-23 13:35:39 +0000446 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
robbiew4cf80962003-03-25 21:53:41 +0000447 compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 3, parent_pid);
448
449 /*
450 * Test the read lock
451 */
452 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
453 compare_lock(&tl, (short)F_RDLCK, (short)0, 13, 5, parent_pid);
454
455 /*
456 * Test the second write lock
457 */
458 do_test(&tl, (short)F_WRLCK, (short)0, 18, 0);
459 compare_lock(&tl, (short)F_WRLCK, (short)0, 18, 2, parent_pid);
460
461 /*
462 * Test to make sure the rest of the file is unlocked
463 */
464 do_test(&tl, (short)F_WRLCK, (short)0, 20, 0);
465 compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, 0);
466
467 /*
468 * remove all the locks set above.
469 */
470 unlock_file();
robbiew4cf80962003-03-25 21:53:41 +0000471 tst_resm(TINFO, "Exit block 5");
472
mridgedb639212005-01-04 21:04:11 +0000473/* //block6: */
robbiew4cf80962003-03-25 21:53:41 +0000474 tst_resm(TINFO, "Enter block 6");
robbiew4cf80962003-03-25 21:53:41 +0000475 /*
476 * Set a write lock in the middle of the file and a read
477 * lock that overlaps the end
478 */
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800479 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800480 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000481
482 /*
483 * Set a read lock on the whole file
484 */
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800485 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 13, 5) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800486 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000487
488 /*
489 * Test the write lock
490 */
491 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
492 compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 3, parent_pid);
493
494 /*
495 * Test the read lock
496 */
497 do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
498 compare_lock(&tl, (short)F_RDLCK, (short)0, 13, 5, parent_pid);
499
500 /*
501 * Test to make sure the rest of the file is unlocked
502 */
503 do_test(&tl, (short)F_WRLCK, (short)0, 18, 0);
504 compare_lock(&tl, (short)F_UNLCK, (short)0, 18, 0, 0);
505
506 /*
507 * remove all the locks set above
508 */
509 unlock_file();
510
robbiew4cf80962003-03-25 21:53:41 +0000511 tst_resm(TINFO, "Exit block 6");
512
mridgedb639212005-01-04 21:04:11 +0000513/* //block7: */
robbiew4cf80962003-03-25 21:53:41 +0000514 tst_resm(TINFO, "Enter block 7");
robbiew4cf80962003-03-25 21:53:41 +0000515
516 /*
517 * Set a write lock in the middle of the file and a read
518 * lock starting at the last byte of the write lock
519 */
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800520 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800521 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000522
523 /*
524 * Set a read lock on the whole file.
525 */
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800526 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 14, 5) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800527 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000528
529 /*
530 * Test write lock
531 */
532 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
533 compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 4, parent_pid);
534
535 /*
536 * Test the read lock
537 */
538 do_test(&tl, (short)F_WRLCK, (short)0, 14, 0);
539 compare_lock(&tl, (short)F_RDLCK, (short)0, 14, 5, parent_pid);
540
541 /*
542 * Test to make sure the end of the file is unlocked
543 */
544 do_test(&tl, (short)F_WRLCK, (short)0, 19, 0);
545 compare_lock(&tl, (short)F_UNLCK, (short)0, 19, 0, 0);
546
547 /*
548 * remove all the locks set above
549 */
550 unlock_file();
551
robbiew4cf80962003-03-25 21:53:41 +0000552 tst_resm(TINFO, "Exit block 7");
553
mridgedb639212005-01-04 21:04:11 +0000554/* //block8: */
robbiew4cf80962003-03-25 21:53:41 +0000555 tst_resm(TINFO, "Enter block 8");
robbiew4cf80962003-03-25 21:53:41 +0000556
557 /*
558 * Set a write lock in the middle of the file and a read
559 * lock that starts just after the last byte of the
560 * write lock.
561 */
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800562 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800563 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000564
565 /*
566 * Set a read lock on the whole file
567 */
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800568 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 15, 5) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800569 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000570
571 /*
572 * Test the write lock
573 */
574 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
575 compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 5, parent_pid);
576
577 /*
578 * Test the read lock
579 */
580 do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
581 compare_lock(&tl, (short)F_RDLCK, (short)0, 15, 5, parent_pid);
582
583 /*
584 * Test to make sure the rest of the file is unlocked
585 */
586 do_test(&tl, (short)F_WRLCK, (short)0, 20, 0);
587 compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, 0);
588
589 /*
590 * remove all the locks set above
591 */
592 unlock_file();
593
robbiew4cf80962003-03-25 21:53:41 +0000594 tst_resm(TINFO, "Exit block 8");
595
mridgedb639212005-01-04 21:04:11 +0000596/* //block9: */
robbiew4cf80962003-03-25 21:53:41 +0000597 tst_resm(TINFO, "Enter block 9");
robbiew4cf80962003-03-25 21:53:41 +0000598
599 /*
600 * Set a write lock at the middle of the file and a read
601 * lock that starts past the end of the write lock.
602 */
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800603 if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800604 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000605
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800606 if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 16, 5) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800607 tst_resm(TFAIL | TERRNO, "fcntl on file failed");
robbiew4cf80962003-03-25 21:53:41 +0000608
609 /*
610 * Test the write lock
611 */
612 do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
613 compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 5, parent_pid);
614
615 /*
616 * Test that byte in between is unlocked
617 */
618 do_test(&tl, (short)F_WRLCK, (short)0, 15, 1);
619 compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 1, 0);
620
621 /*
622 * Test the read lock
623 */
624 do_test(&tl, (short)F_WRLCK, (short)0, 16, 0);
625 compare_lock(&tl, (short)F_RDLCK, (short)0, 16, 5, parent_pid);
626
627 /*
628 * Test to make sure the rest of the file is unlocked
629 */
630 do_test(&tl, (short)F_WRLCK, (short)0, 21, 0);
631 compare_lock(&tl, (short)F_UNLCK, (short)0, 21, 0, 0);
632
633 /*
634 * remove all the locks set above
635 */
636 unlock_file();
637
robbiew4cf80962003-03-25 21:53:41 +0000638 tst_resm(TINFO, "Exit block 9");
639
640 stop_child();
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800641 SAFE_CLOSE(cleanup, fd);
robbiew4cf80962003-03-25 21:53:41 +0000642 }
643 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800644 tst_exit();
Garrett Cooper0a643cb2010-12-21 11:21:19 -0800645}