xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fcntl/fcntl19.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker  * NAME
22*49cdfc7eSAndroid Build Coastguard Worker  *	fcntl19.c
23*49cdfc7eSAndroid Build Coastguard Worker  *
24*49cdfc7eSAndroid Build Coastguard Worker  * DESCRIPTION
25*49cdfc7eSAndroid Build Coastguard Worker  *	Testcase to check locking of regions of a file
26*49cdfc7eSAndroid Build Coastguard Worker  *
27*49cdfc7eSAndroid Build Coastguard Worker  * CALLS
28*49cdfc7eSAndroid Build Coastguard Worker  *	fcntl
29*49cdfc7eSAndroid Build Coastguard Worker  *
30*49cdfc7eSAndroid Build Coastguard Worker  * ALGORITHM
31*49cdfc7eSAndroid Build Coastguard Worker  *	Test unlocking sections around a write lock
32*49cdfc7eSAndroid Build Coastguard Worker  *
33*49cdfc7eSAndroid Build Coastguard Worker  * USAGE
34*49cdfc7eSAndroid Build Coastguard Worker  *	fcntl19
35*49cdfc7eSAndroid Build Coastguard Worker  *
36*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
37*49cdfc7eSAndroid Build Coastguard Worker  *	07/2001 Ported by Wayne Boyer
38*49cdfc7eSAndroid Build Coastguard Worker  *
39*49cdfc7eSAndroid Build Coastguard Worker  * RESTRICTIONS
40*49cdfc7eSAndroid Build Coastguard Worker  *	None
41*49cdfc7eSAndroid Build Coastguard Worker  */
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
48*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
49*49cdfc7eSAndroid Build Coastguard Worker #include <inttypes.h>
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
52*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker #define STRINGSIZE	27
55*49cdfc7eSAndroid Build Coastguard Worker #define STRING		"abcdefghijklmnopqrstuvwxyz\n"
56*49cdfc7eSAndroid Build Coastguard Worker #define STOP		0xFFF0
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker int parent_pipe[2];
59*49cdfc7eSAndroid Build Coastguard Worker int child_pipe[2];
60*49cdfc7eSAndroid Build Coastguard Worker int fd;
61*49cdfc7eSAndroid Build Coastguard Worker pid_t parent_pid, child_pid;
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker void parent_put();
64*49cdfc7eSAndroid Build Coastguard Worker void parent_get();
65*49cdfc7eSAndroid Build Coastguard Worker void child_put();
66*49cdfc7eSAndroid Build Coastguard Worker void child_get();
67*49cdfc7eSAndroid Build Coastguard Worker void stop_child();
68*49cdfc7eSAndroid Build Coastguard Worker void compare_lock(struct flock *, short, short, int, int, pid_t);
69*49cdfc7eSAndroid Build Coastguard Worker void unlock_file();
70*49cdfc7eSAndroid Build Coastguard Worker void do_test(struct flock *, short, short, int, int);
71*49cdfc7eSAndroid Build Coastguard Worker void catch_child();
72*49cdfc7eSAndroid Build Coastguard Worker char *str_type();
73*49cdfc7eSAndroid Build Coastguard Worker int do_lock(int, short, short, int, int);
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "fcntl19";
76*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker void setup(void);
79*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void);
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker int fail = 0;
82*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)83*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
84*49cdfc7eSAndroid Build Coastguard Worker {
85*49cdfc7eSAndroid Build Coastguard Worker 	char *buf = STRING;
86*49cdfc7eSAndroid Build Coastguard Worker 	char template[PATH_MAX];
87*49cdfc7eSAndroid Build Coastguard Worker 	struct sigaction act;
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(FORK, DEF_HANDLER, cleanup);
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 	umask(0);
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(cleanup, parent_pipe);
96*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(cleanup, child_pipe);
97*49cdfc7eSAndroid Build Coastguard Worker 	parent_pid = getpid();
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 	snprintf(template, PATH_MAX, "fcntl19XXXXXX");
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd = mkstemp(template)) < 0) {
104*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "Couldn't open temp file! errno = %d", errno);
105*49cdfc7eSAndroid Build Coastguard Worker 	}
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 	if (write(fd, buf, STRINGSIZE) < 0) {
108*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "Couldn't write to temp file! errno = %d",
109*49cdfc7eSAndroid Build Coastguard Worker 			 errno);
110*49cdfc7eSAndroid Build Coastguard Worker 	}
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 	memset(&act, 0, sizeof(act));
113*49cdfc7eSAndroid Build Coastguard Worker 	act.sa_handler = catch_child;
114*49cdfc7eSAndroid Build Coastguard Worker 	sigemptyset(&act.sa_mask);
115*49cdfc7eSAndroid Build Coastguard Worker 	sigaddset(&act.sa_mask, SIGCHLD);
116*49cdfc7eSAndroid Build Coastguard Worker 	if ((sigaction(SIGCHLD, &act, NULL)) < 0) {
117*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "SIGCHLD signal setup failed, errno: %d", errno);
118*49cdfc7eSAndroid Build Coastguard Worker 		fail = 1;
119*49cdfc7eSAndroid Build Coastguard Worker 	}
120*49cdfc7eSAndroid Build Coastguard Worker }
121*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)122*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
123*49cdfc7eSAndroid Build Coastguard Worker {
124*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
125*49cdfc7eSAndroid Build Coastguard Worker 
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker 
do_child(void)128*49cdfc7eSAndroid Build Coastguard Worker void do_child(void)
129*49cdfc7eSAndroid Build Coastguard Worker {
130*49cdfc7eSAndroid Build Coastguard Worker 	struct flock fl;
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker 	close(parent_pipe[1]);
133*49cdfc7eSAndroid Build Coastguard Worker 	close(child_pipe[0]);
134*49cdfc7eSAndroid Build Coastguard Worker 	while (1) {
135*49cdfc7eSAndroid Build Coastguard Worker 		child_get(&fl);
136*49cdfc7eSAndroid Build Coastguard Worker 		if (fcntl(fd, F_GETLK, &fl) < 0)
137*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL | TERRNO, "fcntl on file failed");
138*49cdfc7eSAndroid Build Coastguard Worker 		child_put(&fl);
139*49cdfc7eSAndroid Build Coastguard Worker 	}
140*49cdfc7eSAndroid Build Coastguard Worker }
141*49cdfc7eSAndroid Build Coastguard Worker 
do_lock(int cmd,short type,short whence,int start,int len)142*49cdfc7eSAndroid Build Coastguard Worker int do_lock(int cmd, short type, short whence, int start, int len)
143*49cdfc7eSAndroid Build Coastguard Worker {
144*49cdfc7eSAndroid Build Coastguard Worker 	struct flock fl;
145*49cdfc7eSAndroid Build Coastguard Worker 
146*49cdfc7eSAndroid Build Coastguard Worker 	fl.l_type = type;
147*49cdfc7eSAndroid Build Coastguard Worker 	fl.l_whence = whence;
148*49cdfc7eSAndroid Build Coastguard Worker 	fl.l_start = start;
149*49cdfc7eSAndroid Build Coastguard Worker 	fl.l_len = len;
150*49cdfc7eSAndroid Build Coastguard Worker 	return (fcntl(fd, cmd, &fl));
151*49cdfc7eSAndroid Build Coastguard Worker }
152*49cdfc7eSAndroid Build Coastguard Worker 
do_test(struct flock * fl,short type,short whence,int start,int len)153*49cdfc7eSAndroid Build Coastguard Worker void do_test(struct flock *fl, short type, short whence, int start, int len)
154*49cdfc7eSAndroid Build Coastguard Worker {
155*49cdfc7eSAndroid Build Coastguard Worker 	fl->l_type = type;
156*49cdfc7eSAndroid Build Coastguard Worker 	fl->l_whence = whence;
157*49cdfc7eSAndroid Build Coastguard Worker 	fl->l_start = start;
158*49cdfc7eSAndroid Build Coastguard Worker 	fl->l_len = len;
159*49cdfc7eSAndroid Build Coastguard Worker 	fl->l_pid = (short)0;
160*49cdfc7eSAndroid Build Coastguard Worker 
161*49cdfc7eSAndroid Build Coastguard Worker 	parent_put(fl);
162*49cdfc7eSAndroid Build Coastguard Worker 	parent_get(fl);
163*49cdfc7eSAndroid Build Coastguard Worker }
164*49cdfc7eSAndroid Build Coastguard Worker 
165*49cdfc7eSAndroid Build Coastguard Worker void
compare_lock(struct flock * fl,short type,short whence,int start,int len,pid_t pid)166*49cdfc7eSAndroid Build Coastguard Worker compare_lock(struct flock *fl, short type, short whence, int start, int len,
167*49cdfc7eSAndroid Build Coastguard Worker 	     pid_t pid)
168*49cdfc7eSAndroid Build Coastguard Worker {
169*49cdfc7eSAndroid Build Coastguard Worker 	if (fl->l_type != type) {
170*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "lock type is wrong should be %s is %s",
171*49cdfc7eSAndroid Build Coastguard Worker 			 str_type(type), str_type(fl->l_type));
172*49cdfc7eSAndroid Build Coastguard Worker 		fail = 1;
173*49cdfc7eSAndroid Build Coastguard Worker 	}
174*49cdfc7eSAndroid Build Coastguard Worker 
175*49cdfc7eSAndroid Build Coastguard Worker 	if (fl->l_whence != whence) {
176*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "lock whence is wrong should be %d is %d",
177*49cdfc7eSAndroid Build Coastguard Worker 			 whence, fl->l_whence);
178*49cdfc7eSAndroid Build Coastguard Worker 		fail = 1;
179*49cdfc7eSAndroid Build Coastguard Worker 	}
180*49cdfc7eSAndroid Build Coastguard Worker 
181*49cdfc7eSAndroid Build Coastguard Worker 	if (fl->l_start != start) {
182*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "region starts in wrong place, should be"
183*49cdfc7eSAndroid Build Coastguard Worker 			 "%d is %" PRId64, start, (int64_t) fl->l_start);
184*49cdfc7eSAndroid Build Coastguard Worker 		fail = 1;
185*49cdfc7eSAndroid Build Coastguard Worker 	}
186*49cdfc7eSAndroid Build Coastguard Worker 
187*49cdfc7eSAndroid Build Coastguard Worker 	if (fl->l_len != len) {
188*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL,
189*49cdfc7eSAndroid Build Coastguard Worker 			 "region length is wrong, should be %d is %" PRId64,
190*49cdfc7eSAndroid Build Coastguard Worker 			 len, (int64_t) fl->l_len);
191*49cdfc7eSAndroid Build Coastguard Worker 		fail = 1;
192*49cdfc7eSAndroid Build Coastguard Worker 	}
193*49cdfc7eSAndroid Build Coastguard Worker 
194*49cdfc7eSAndroid Build Coastguard Worker 	if (fl->l_pid != pid) {
195*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "locking pid is wrong, should be %d is %d",
196*49cdfc7eSAndroid Build Coastguard Worker 			 pid, fl->l_pid);
197*49cdfc7eSAndroid Build Coastguard Worker 		fail = 1;
198*49cdfc7eSAndroid Build Coastguard Worker 	}
199*49cdfc7eSAndroid Build Coastguard Worker }
200*49cdfc7eSAndroid Build Coastguard Worker 
unlock_file(void)201*49cdfc7eSAndroid Build Coastguard Worker void unlock_file(void)
202*49cdfc7eSAndroid Build Coastguard Worker {
203*49cdfc7eSAndroid Build Coastguard Worker 	struct flock fl;
204*49cdfc7eSAndroid Build Coastguard Worker 
205*49cdfc7eSAndroid Build Coastguard Worker 	if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 0, 0) < 0) {
206*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "fcntl on file failed, errno =%d", errno);
207*49cdfc7eSAndroid Build Coastguard Worker 		fail = 1;
208*49cdfc7eSAndroid Build Coastguard Worker 	}
209*49cdfc7eSAndroid Build Coastguard Worker 	do_test(&fl, F_WRLCK, 0, 0, 0);
210*49cdfc7eSAndroid Build Coastguard Worker 	compare_lock(&fl, (short)F_UNLCK, (short)0, 0, 0, (pid_t) 0);
211*49cdfc7eSAndroid Build Coastguard Worker }
212*49cdfc7eSAndroid Build Coastguard Worker 
str_type(int type)213*49cdfc7eSAndroid Build Coastguard Worker char *str_type(int type)
214*49cdfc7eSAndroid Build Coastguard Worker {
215*49cdfc7eSAndroid Build Coastguard Worker 	static char buf[20];
216*49cdfc7eSAndroid Build Coastguard Worker 
217*49cdfc7eSAndroid Build Coastguard Worker 	switch (type) {
218*49cdfc7eSAndroid Build Coastguard Worker 	case F_RDLCK:
219*49cdfc7eSAndroid Build Coastguard Worker 		return ("F_RDLCK");
220*49cdfc7eSAndroid Build Coastguard Worker 	case F_WRLCK:
221*49cdfc7eSAndroid Build Coastguard Worker 		return ("F_WRLCK");
222*49cdfc7eSAndroid Build Coastguard Worker 	case F_UNLCK:
223*49cdfc7eSAndroid Build Coastguard Worker 		return ("F_UNLCK");
224*49cdfc7eSAndroid Build Coastguard Worker 	default:
225*49cdfc7eSAndroid Build Coastguard Worker 		sprintf(buf, "BAD VALUE: %d", type);
226*49cdfc7eSAndroid Build Coastguard Worker 		return (buf);
227*49cdfc7eSAndroid Build Coastguard Worker 	}
228*49cdfc7eSAndroid Build Coastguard Worker }
229*49cdfc7eSAndroid Build Coastguard Worker 
parent_put(struct flock * l)230*49cdfc7eSAndroid Build Coastguard Worker void parent_put(struct flock *l)
231*49cdfc7eSAndroid Build Coastguard Worker {
232*49cdfc7eSAndroid Build Coastguard Worker 	if (write(parent_pipe[1], l, sizeof(*l)) != sizeof(*l)) {
233*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "couldn't send message to child");
234*49cdfc7eSAndroid Build Coastguard Worker 		fail = 1;
235*49cdfc7eSAndroid Build Coastguard Worker 	}
236*49cdfc7eSAndroid Build Coastguard Worker }
237*49cdfc7eSAndroid Build Coastguard Worker 
parent_get(struct flock * l)238*49cdfc7eSAndroid Build Coastguard Worker void parent_get(struct flock *l)
239*49cdfc7eSAndroid Build Coastguard Worker {
240*49cdfc7eSAndroid Build Coastguard Worker 	if (read(child_pipe[0], l, sizeof(*l)) != sizeof(*l)) {
241*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "couldn't get message from child");
242*49cdfc7eSAndroid Build Coastguard Worker 		fail = 1;
243*49cdfc7eSAndroid Build Coastguard Worker 	}
244*49cdfc7eSAndroid Build Coastguard Worker }
245*49cdfc7eSAndroid Build Coastguard Worker 
child_put(struct flock * l)246*49cdfc7eSAndroid Build Coastguard Worker void child_put(struct flock *l)
247*49cdfc7eSAndroid Build Coastguard Worker {
248*49cdfc7eSAndroid Build Coastguard Worker 	if (write(child_pipe[1], l, sizeof(*l)) != sizeof(*l)) {
249*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "couldn't send message to parent");
250*49cdfc7eSAndroid Build Coastguard Worker 		fail = 1;
251*49cdfc7eSAndroid Build Coastguard Worker 	}
252*49cdfc7eSAndroid Build Coastguard Worker }
253*49cdfc7eSAndroid Build Coastguard Worker 
child_get(struct flock * l)254*49cdfc7eSAndroid Build Coastguard Worker void child_get(struct flock *l)
255*49cdfc7eSAndroid Build Coastguard Worker {
256*49cdfc7eSAndroid Build Coastguard Worker 	if (read(parent_pipe[0], l, sizeof(*l)) != sizeof(*l)) {
257*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "couldn't get message from parent");
258*49cdfc7eSAndroid Build Coastguard Worker 		cleanup();
259*49cdfc7eSAndroid Build Coastguard Worker 	} else if (l->l_type == (short)STOP) {
260*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
261*49cdfc7eSAndroid Build Coastguard Worker 	}
262*49cdfc7eSAndroid Build Coastguard Worker }
263*49cdfc7eSAndroid Build Coastguard Worker 
stop_child(void)264*49cdfc7eSAndroid Build Coastguard Worker void stop_child(void)
265*49cdfc7eSAndroid Build Coastguard Worker {
266*49cdfc7eSAndroid Build Coastguard Worker 	struct flock fl;
267*49cdfc7eSAndroid Build Coastguard Worker 
268*49cdfc7eSAndroid Build Coastguard Worker 	signal(SIGCHLD, SIG_DFL);
269*49cdfc7eSAndroid Build Coastguard Worker 	fl.l_type = STOP;
270*49cdfc7eSAndroid Build Coastguard Worker 	parent_put(&fl);
271*49cdfc7eSAndroid Build Coastguard Worker 	wait(0);
272*49cdfc7eSAndroid Build Coastguard Worker }
273*49cdfc7eSAndroid Build Coastguard Worker 
catch_child(void)274*49cdfc7eSAndroid Build Coastguard Worker void catch_child(void)
275*49cdfc7eSAndroid Build Coastguard Worker {
276*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TFAIL, "Unexpected death of child process");
277*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
278*49cdfc7eSAndroid Build Coastguard Worker }
279*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)280*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
281*49cdfc7eSAndroid Build Coastguard Worker {
282*49cdfc7eSAndroid Build Coastguard Worker 	struct flock tl;
283*49cdfc7eSAndroid Build Coastguard Worker 
284*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
285*49cdfc7eSAndroid Build Coastguard Worker 
286*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
287*49cdfc7eSAndroid Build Coastguard Worker 
288*49cdfc7eSAndroid Build Coastguard Worker 	setup();		/* global setup */
289*49cdfc7eSAndroid Build Coastguard Worker 
290*49cdfc7eSAndroid Build Coastguard Worker 	/* Check for looping state if -i option is given */
291*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
292*49cdfc7eSAndroid Build Coastguard Worker 		/* reset tst_count in case we are looping */
293*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
294*49cdfc7eSAndroid Build Coastguard Worker 
295*49cdfc7eSAndroid Build Coastguard Worker 		if ((child_pid = tst_fork()) == 0) {	/* child */
296*49cdfc7eSAndroid Build Coastguard Worker 			do_child();
297*49cdfc7eSAndroid Build Coastguard Worker 		} else if (child_pid < 0) {
298*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Fork failed");
299*49cdfc7eSAndroid Build Coastguard Worker 			cleanup();
300*49cdfc7eSAndroid Build Coastguard Worker 		}
301*49cdfc7eSAndroid Build Coastguard Worker 
302*49cdfc7eSAndroid Build Coastguard Worker 		/* parent */
303*49cdfc7eSAndroid Build Coastguard Worker 
304*49cdfc7eSAndroid Build Coastguard Worker 		(void)close(parent_pipe[0]);
305*49cdfc7eSAndroid Build Coastguard Worker 		(void)close(child_pipe[1]);
306*49cdfc7eSAndroid Build Coastguard Worker 
307*49cdfc7eSAndroid Build Coastguard Worker /* //block1: */
308*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Enter block 1");
309*49cdfc7eSAndroid Build Coastguard Worker 		/*
310*49cdfc7eSAndroid Build Coastguard Worker 		 * Add a write lock to the middle of the file and unlock a
311*49cdfc7eSAndroid Build Coastguard Worker 		 * section just before the lock
312*49cdfc7eSAndroid Build Coastguard Worker 		 */
313*49cdfc7eSAndroid Build Coastguard Worker 		if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0) {
314*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "fcntl on file failed, errno =%d",
315*49cdfc7eSAndroid Build Coastguard Worker 				 errno);
316*49cdfc7eSAndroid Build Coastguard Worker 			fail = 1;
317*49cdfc7eSAndroid Build Coastguard Worker 		}
318*49cdfc7eSAndroid Build Coastguard Worker 
319*49cdfc7eSAndroid Build Coastguard Worker 		if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 5, 5) < 0) {
320*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "fcntl on file failed, errno =%d",
321*49cdfc7eSAndroid Build Coastguard Worker 				 errno);
322*49cdfc7eSAndroid Build Coastguard Worker 			fail = 1;
323*49cdfc7eSAndroid Build Coastguard Worker 		}
324*49cdfc7eSAndroid Build Coastguard Worker 
325*49cdfc7eSAndroid Build Coastguard Worker 		/*
326*49cdfc7eSAndroid Build Coastguard Worker 		 * Test write lock
327*49cdfc7eSAndroid Build Coastguard Worker 		 */
328*49cdfc7eSAndroid Build Coastguard Worker 		do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
329*49cdfc7eSAndroid Build Coastguard Worker 		compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 5, parent_pid);
330*49cdfc7eSAndroid Build Coastguard Worker 
331*49cdfc7eSAndroid Build Coastguard Worker 		/*
332*49cdfc7eSAndroid Build Coastguard Worker 		 * Test that the rest of the file is unlocked
333*49cdfc7eSAndroid Build Coastguard Worker 		 */
334*49cdfc7eSAndroid Build Coastguard Worker 		do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
335*49cdfc7eSAndroid Build Coastguard Worker 		compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, (pid_t) 0);
336*49cdfc7eSAndroid Build Coastguard Worker 
337*49cdfc7eSAndroid Build Coastguard Worker 		/*
338*49cdfc7eSAndroid Build Coastguard Worker 		 * remove all the locks set above
339*49cdfc7eSAndroid Build Coastguard Worker 		 */
340*49cdfc7eSAndroid Build Coastguard Worker 		unlock_file();
341*49cdfc7eSAndroid Build Coastguard Worker 
342*49cdfc7eSAndroid Build Coastguard Worker 		if (fail) {
343*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Test block 1: FAILED");
344*49cdfc7eSAndroid Build Coastguard Worker 		} else {
345*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Test block 1: PASSED");
346*49cdfc7eSAndroid Build Coastguard Worker 		}
347*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Exit block 1");
348*49cdfc7eSAndroid Build Coastguard Worker 
349*49cdfc7eSAndroid Build Coastguard Worker /* //block2: */
350*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Enter block 2");
351*49cdfc7eSAndroid Build Coastguard Worker 		fail = 0;
352*49cdfc7eSAndroid Build Coastguard Worker 		/*
353*49cdfc7eSAndroid Build Coastguard Worker 		 * Set a write lock in the middle and do an unlock that
354*49cdfc7eSAndroid Build Coastguard Worker 		 * ends at the first byte of the write lock.
355*49cdfc7eSAndroid Build Coastguard Worker 		 */
356*49cdfc7eSAndroid Build Coastguard Worker 		if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0) {
357*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "fcntl on file failed, errno =%d",
358*49cdfc7eSAndroid Build Coastguard Worker 				 errno);
359*49cdfc7eSAndroid Build Coastguard Worker 			fail = 1;
360*49cdfc7eSAndroid Build Coastguard Worker 		}
361*49cdfc7eSAndroid Build Coastguard Worker 
362*49cdfc7eSAndroid Build Coastguard Worker 		if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 5, 6) < 0) {
363*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "fcntl on file failed, errno =%d",
364*49cdfc7eSAndroid Build Coastguard Worker 				 errno);
365*49cdfc7eSAndroid Build Coastguard Worker 			fail = 1;
366*49cdfc7eSAndroid Build Coastguard Worker 		}
367*49cdfc7eSAndroid Build Coastguard Worker 
368*49cdfc7eSAndroid Build Coastguard Worker 		/*
369*49cdfc7eSAndroid Build Coastguard Worker 		 * Test write lock
370*49cdfc7eSAndroid Build Coastguard Worker 		 */
371*49cdfc7eSAndroid Build Coastguard Worker 		do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
372*49cdfc7eSAndroid Build Coastguard Worker 		compare_lock(&tl, (short)F_WRLCK, (short)0, 11, 4, parent_pid);
373*49cdfc7eSAndroid Build Coastguard Worker 
374*49cdfc7eSAndroid Build Coastguard Worker 		/*
375*49cdfc7eSAndroid Build Coastguard Worker 		 * Test to make sure the rest of the file is unlocked
376*49cdfc7eSAndroid Build Coastguard Worker 		 */
377*49cdfc7eSAndroid Build Coastguard Worker 		do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
378*49cdfc7eSAndroid Build Coastguard Worker 		compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, (pid_t) 0);
379*49cdfc7eSAndroid Build Coastguard Worker 
380*49cdfc7eSAndroid Build Coastguard Worker 		/*
381*49cdfc7eSAndroid Build Coastguard Worker 		 * remove all the locks set above
382*49cdfc7eSAndroid Build Coastguard Worker 		 */
383*49cdfc7eSAndroid Build Coastguard Worker 		unlock_file();
384*49cdfc7eSAndroid Build Coastguard Worker 
385*49cdfc7eSAndroid Build Coastguard Worker 		if (fail) {
386*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Test block 2: FAILED");
387*49cdfc7eSAndroid Build Coastguard Worker 		} else {
388*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Test block 2: PASSED");
389*49cdfc7eSAndroid Build Coastguard Worker 		}
390*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Exit block 2");
391*49cdfc7eSAndroid Build Coastguard Worker 
392*49cdfc7eSAndroid Build Coastguard Worker /* //block3: */
393*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Enter block 3");
394*49cdfc7eSAndroid Build Coastguard Worker 		fail = 0;
395*49cdfc7eSAndroid Build Coastguard Worker 
396*49cdfc7eSAndroid Build Coastguard Worker 		/*
397*49cdfc7eSAndroid Build Coastguard Worker 		 * Set a write lock on the middle of the file and do an
398*49cdfc7eSAndroid Build Coastguard Worker 		 * unlock that overlaps the front of the write
399*49cdfc7eSAndroid Build Coastguard Worker 		 */
400*49cdfc7eSAndroid Build Coastguard Worker 		if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0) {
401*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "fcntl on file failed, errno =%d",
402*49cdfc7eSAndroid Build Coastguard Worker 				 errno);
403*49cdfc7eSAndroid Build Coastguard Worker 			fail = 1;
404*49cdfc7eSAndroid Build Coastguard Worker 		}
405*49cdfc7eSAndroid Build Coastguard Worker 
406*49cdfc7eSAndroid Build Coastguard Worker 		if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 5, 8) < 0) {
407*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "fcntl on file failed, errno =%d",
408*49cdfc7eSAndroid Build Coastguard Worker 				 errno);
409*49cdfc7eSAndroid Build Coastguard Worker 			fail = 1;
410*49cdfc7eSAndroid Build Coastguard Worker 		}
411*49cdfc7eSAndroid Build Coastguard Worker 
412*49cdfc7eSAndroid Build Coastguard Worker 		/*
413*49cdfc7eSAndroid Build Coastguard Worker 		 * Test the write lock
414*49cdfc7eSAndroid Build Coastguard Worker 		 */
415*49cdfc7eSAndroid Build Coastguard Worker 		do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
416*49cdfc7eSAndroid Build Coastguard Worker 		compare_lock(&tl, (short)F_WRLCK, (short)0, 13, 2, parent_pid);
417*49cdfc7eSAndroid Build Coastguard Worker 
418*49cdfc7eSAndroid Build Coastguard Worker 		/*
419*49cdfc7eSAndroid Build Coastguard Worker 		 * Test to make sure the rest of the file is unlocked
420*49cdfc7eSAndroid Build Coastguard Worker 		 */
421*49cdfc7eSAndroid Build Coastguard Worker 		do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
422*49cdfc7eSAndroid Build Coastguard Worker 		compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, (pid_t) 0);
423*49cdfc7eSAndroid Build Coastguard Worker 
424*49cdfc7eSAndroid Build Coastguard Worker 		/*
425*49cdfc7eSAndroid Build Coastguard Worker 		 * remove all the locks set above
426*49cdfc7eSAndroid Build Coastguard Worker 		 */
427*49cdfc7eSAndroid Build Coastguard Worker 		unlock_file();
428*49cdfc7eSAndroid Build Coastguard Worker 
429*49cdfc7eSAndroid Build Coastguard Worker 		if (fail) {
430*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Test block 3: FAILED");
431*49cdfc7eSAndroid Build Coastguard Worker 		} else {
432*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Test block 3: PASSED");
433*49cdfc7eSAndroid Build Coastguard Worker 		}
434*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Exit block 3");
435*49cdfc7eSAndroid Build Coastguard Worker 
436*49cdfc7eSAndroid Build Coastguard Worker /* //block4: */
437*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Enter blcok 4");
438*49cdfc7eSAndroid Build Coastguard Worker 		fail = 0;
439*49cdfc7eSAndroid Build Coastguard Worker 
440*49cdfc7eSAndroid Build Coastguard Worker 		/*
441*49cdfc7eSAndroid Build Coastguard Worker 		 * Set a write a lock in the middle of a file and unlock a
442*49cdfc7eSAndroid Build Coastguard Worker 		 * section in the middle of it
443*49cdfc7eSAndroid Build Coastguard Worker 		 */
444*49cdfc7eSAndroid Build Coastguard Worker 		if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 10) < 0) {
445*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "fcntl on file failed, errno =%d",
446*49cdfc7eSAndroid Build Coastguard Worker 				 errno);
447*49cdfc7eSAndroid Build Coastguard Worker 			fail = 1;
448*49cdfc7eSAndroid Build Coastguard Worker 		}
449*49cdfc7eSAndroid Build Coastguard Worker 
450*49cdfc7eSAndroid Build Coastguard Worker 		if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 13, 5) < 0) {
451*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "fcntl on file failed, errno =%d",
452*49cdfc7eSAndroid Build Coastguard Worker 				 errno);
453*49cdfc7eSAndroid Build Coastguard Worker 			fail = 1;
454*49cdfc7eSAndroid Build Coastguard Worker 		}
455*49cdfc7eSAndroid Build Coastguard Worker 
456*49cdfc7eSAndroid Build Coastguard Worker 		/*
457*49cdfc7eSAndroid Build Coastguard Worker 		 * Test the first write lock
458*49cdfc7eSAndroid Build Coastguard Worker 		 */
459*49cdfc7eSAndroid Build Coastguard Worker 		do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
460*49cdfc7eSAndroid Build Coastguard Worker 		compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 3, parent_pid);
461*49cdfc7eSAndroid Build Coastguard Worker 
462*49cdfc7eSAndroid Build Coastguard Worker 		/*
463*49cdfc7eSAndroid Build Coastguard Worker 		 * Test the second write lock
464*49cdfc7eSAndroid Build Coastguard Worker 		 */
465*49cdfc7eSAndroid Build Coastguard Worker 		do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
466*49cdfc7eSAndroid Build Coastguard Worker 		compare_lock(&tl, (short)F_WRLCK, (short)0, 18, 2, parent_pid);
467*49cdfc7eSAndroid Build Coastguard Worker 
468*49cdfc7eSAndroid Build Coastguard Worker 		/*
469*49cdfc7eSAndroid Build Coastguard Worker 		 * Test to make sure the rest of the file is unlocked
470*49cdfc7eSAndroid Build Coastguard Worker 		 */
471*49cdfc7eSAndroid Build Coastguard Worker 		do_test(&tl, (short)F_WRLCK, (short)0, 20, 0);
472*49cdfc7eSAndroid Build Coastguard Worker 		compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, (pid_t) 0);
473*49cdfc7eSAndroid Build Coastguard Worker 
474*49cdfc7eSAndroid Build Coastguard Worker 		/*
475*49cdfc7eSAndroid Build Coastguard Worker 		 * remove all the locks set above
476*49cdfc7eSAndroid Build Coastguard Worker 		 */
477*49cdfc7eSAndroid Build Coastguard Worker 		unlock_file();
478*49cdfc7eSAndroid Build Coastguard Worker 
479*49cdfc7eSAndroid Build Coastguard Worker 		if (fail) {
480*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Test block 4: FAILED");
481*49cdfc7eSAndroid Build Coastguard Worker 		} else {
482*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Test block 4: PASSED");
483*49cdfc7eSAndroid Build Coastguard Worker 		}
484*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Exit block 4");
485*49cdfc7eSAndroid Build Coastguard Worker 
486*49cdfc7eSAndroid Build Coastguard Worker /* //block5: */
487*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Enter block 5");
488*49cdfc7eSAndroid Build Coastguard Worker 		fail = 0;
489*49cdfc7eSAndroid Build Coastguard Worker 
490*49cdfc7eSAndroid Build Coastguard Worker 		/*
491*49cdfc7eSAndroid Build Coastguard Worker 		 * Set a write lock in the middle of the file and do a
492*49cdfc7eSAndroid Build Coastguard Worker 		 * unlock that overlaps the end
493*49cdfc7eSAndroid Build Coastguard Worker 		 */
494*49cdfc7eSAndroid Build Coastguard Worker 		if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0) {
495*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "fcntl on file failed, errno =%d",
496*49cdfc7eSAndroid Build Coastguard Worker 				 errno);
497*49cdfc7eSAndroid Build Coastguard Worker 			fail = 1;
498*49cdfc7eSAndroid Build Coastguard Worker 		}
499*49cdfc7eSAndroid Build Coastguard Worker 
500*49cdfc7eSAndroid Build Coastguard Worker 		if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 13, 5) < 0) {
501*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "fcntl on file failed, errno =%d",
502*49cdfc7eSAndroid Build Coastguard Worker 				 errno);
503*49cdfc7eSAndroid Build Coastguard Worker 			fail = 1;
504*49cdfc7eSAndroid Build Coastguard Worker 		}
505*49cdfc7eSAndroid Build Coastguard Worker 
506*49cdfc7eSAndroid Build Coastguard Worker 		/*
507*49cdfc7eSAndroid Build Coastguard Worker 		 * Test the write lock
508*49cdfc7eSAndroid Build Coastguard Worker 		 */
509*49cdfc7eSAndroid Build Coastguard Worker 		do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
510*49cdfc7eSAndroid Build Coastguard Worker 		compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 3, parent_pid);
511*49cdfc7eSAndroid Build Coastguard Worker 
512*49cdfc7eSAndroid Build Coastguard Worker 		/*
513*49cdfc7eSAndroid Build Coastguard Worker 		 * Test to make sure the rest of the file is unlocked
514*49cdfc7eSAndroid Build Coastguard Worker 		 */
515*49cdfc7eSAndroid Build Coastguard Worker 		do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
516*49cdfc7eSAndroid Build Coastguard Worker 		compare_lock(&tl, (short)F_UNLCK, (short)0, 13, 0, (pid_t) 0);
517*49cdfc7eSAndroid Build Coastguard Worker 
518*49cdfc7eSAndroid Build Coastguard Worker 		/*
519*49cdfc7eSAndroid Build Coastguard Worker 		 * remove all the locks set above
520*49cdfc7eSAndroid Build Coastguard Worker 		 */
521*49cdfc7eSAndroid Build Coastguard Worker 		unlock_file();
522*49cdfc7eSAndroid Build Coastguard Worker 
523*49cdfc7eSAndroid Build Coastguard Worker 		if (fail) {
524*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Test block 5: FAILED");
525*49cdfc7eSAndroid Build Coastguard Worker 		} else {
526*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Test block 5: PASSED");
527*49cdfc7eSAndroid Build Coastguard Worker 		}
528*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Exit block 5");
529*49cdfc7eSAndroid Build Coastguard Worker 
530*49cdfc7eSAndroid Build Coastguard Worker /* //block6: */
531*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Enter block 6");
532*49cdfc7eSAndroid Build Coastguard Worker 		fail = 0;
533*49cdfc7eSAndroid Build Coastguard Worker 
534*49cdfc7eSAndroid Build Coastguard Worker 		/*
535*49cdfc7eSAndroid Build Coastguard Worker 		 * Set write lock in the middle of the file and do an unlock
536*49cdfc7eSAndroid Build Coastguard Worker 		 * starting at the last byte of the write lock
537*49cdfc7eSAndroid Build Coastguard Worker 		 */
538*49cdfc7eSAndroid Build Coastguard Worker 		if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0) {
539*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "fcntl on file failed, errno =%d",
540*49cdfc7eSAndroid Build Coastguard Worker 				 errno);
541*49cdfc7eSAndroid Build Coastguard Worker 			fail = 1;
542*49cdfc7eSAndroid Build Coastguard Worker 		}
543*49cdfc7eSAndroid Build Coastguard Worker 
544*49cdfc7eSAndroid Build Coastguard Worker 		if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 14, 5) < 0) {
545*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "fcntl on file failed, errno =%d",
546*49cdfc7eSAndroid Build Coastguard Worker 				 errno);
547*49cdfc7eSAndroid Build Coastguard Worker 			fail = 1;
548*49cdfc7eSAndroid Build Coastguard Worker 		}
549*49cdfc7eSAndroid Build Coastguard Worker 
550*49cdfc7eSAndroid Build Coastguard Worker 		/*
551*49cdfc7eSAndroid Build Coastguard Worker 		 * Test write lock
552*49cdfc7eSAndroid Build Coastguard Worker 		 */
553*49cdfc7eSAndroid Build Coastguard Worker 		do_test(&tl, (short)F_WRLCK, (short)0, 10, 0);
554*49cdfc7eSAndroid Build Coastguard Worker 		compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 4, parent_pid);
555*49cdfc7eSAndroid Build Coastguard Worker 
556*49cdfc7eSAndroid Build Coastguard Worker 		/*
557*49cdfc7eSAndroid Build Coastguard Worker 		 * Test to make sure the end of the file is unlocked
558*49cdfc7eSAndroid Build Coastguard Worker 		 */
559*49cdfc7eSAndroid Build Coastguard Worker 		do_test(&tl, (short)F_WRLCK, (short)0, 14, 0);
560*49cdfc7eSAndroid Build Coastguard Worker 		compare_lock(&tl, (short)F_UNLCK, (short)0, 14, 0, (pid_t) 0);
561*49cdfc7eSAndroid Build Coastguard Worker 
562*49cdfc7eSAndroid Build Coastguard Worker 		/*
563*49cdfc7eSAndroid Build Coastguard Worker 		 * remove all the locks set above
564*49cdfc7eSAndroid Build Coastguard Worker 		 */
565*49cdfc7eSAndroid Build Coastguard Worker 		unlock_file();
566*49cdfc7eSAndroid Build Coastguard Worker 
567*49cdfc7eSAndroid Build Coastguard Worker 		if (fail) {
568*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Test block 6: FAILED");
569*49cdfc7eSAndroid Build Coastguard Worker 		} else {
570*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Test block 6: PASSED");
571*49cdfc7eSAndroid Build Coastguard Worker 		}
572*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Exit block 6");
573*49cdfc7eSAndroid Build Coastguard Worker 
574*49cdfc7eSAndroid Build Coastguard Worker /* //block7: */
575*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Enter block 7");
576*49cdfc7eSAndroid Build Coastguard Worker 		fail = 0;
577*49cdfc7eSAndroid Build Coastguard Worker 
578*49cdfc7eSAndroid Build Coastguard Worker 		/*
579*49cdfc7eSAndroid Build Coastguard Worker 		 * Set a write lock at the middle of the file and do an
580*49cdfc7eSAndroid Build Coastguard Worker 		 * unlock that starts at the byte past the end of the write
581*49cdfc7eSAndroid Build Coastguard Worker 		 * lock
582*49cdfc7eSAndroid Build Coastguard Worker 		 */
583*49cdfc7eSAndroid Build Coastguard Worker 		if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 10, 5) < 0) {
584*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "fcntl on file failed, errno =%d",
585*49cdfc7eSAndroid Build Coastguard Worker 				 errno);
586*49cdfc7eSAndroid Build Coastguard Worker 			fail = 1;
587*49cdfc7eSAndroid Build Coastguard Worker 		}
588*49cdfc7eSAndroid Build Coastguard Worker 
589*49cdfc7eSAndroid Build Coastguard Worker 		if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 16, 0) < 0) {
590*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "fcntl on file failed, errno =%d",
591*49cdfc7eSAndroid Build Coastguard Worker 				 errno);
592*49cdfc7eSAndroid Build Coastguard Worker 			fail = 1;
593*49cdfc7eSAndroid Build Coastguard Worker 		}
594*49cdfc7eSAndroid Build Coastguard Worker 
595*49cdfc7eSAndroid Build Coastguard Worker 		/*
596*49cdfc7eSAndroid Build Coastguard Worker 		 * Test the write lock
597*49cdfc7eSAndroid Build Coastguard Worker 		 */
598*49cdfc7eSAndroid Build Coastguard Worker 		do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
599*49cdfc7eSAndroid Build Coastguard Worker 		compare_lock(&tl, (short)F_WRLCK, (short)0, 10, 5, parent_pid);
600*49cdfc7eSAndroid Build Coastguard Worker 
601*49cdfc7eSAndroid Build Coastguard Worker 		/*
602*49cdfc7eSAndroid Build Coastguard Worker 		 * Test to make sure the rest of the file is unlocked
603*49cdfc7eSAndroid Build Coastguard Worker 		 */
604*49cdfc7eSAndroid Build Coastguard Worker 		do_test(&tl, (short)F_WRLCK, (short)0, 16, 0);
605*49cdfc7eSAndroid Build Coastguard Worker 		compare_lock(&tl, (short)F_UNLCK, (short)0, 16, 0, (pid_t) 0);
606*49cdfc7eSAndroid Build Coastguard Worker 
607*49cdfc7eSAndroid Build Coastguard Worker 		/*
608*49cdfc7eSAndroid Build Coastguard Worker 		 * remove all the locks set above
609*49cdfc7eSAndroid Build Coastguard Worker 		 */
610*49cdfc7eSAndroid Build Coastguard Worker 		unlock_file();
611*49cdfc7eSAndroid Build Coastguard Worker 
612*49cdfc7eSAndroid Build Coastguard Worker 		if (fail) {
613*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Test block 7: FAILED");
614*49cdfc7eSAndroid Build Coastguard Worker 		} else {
615*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Test block 7: PASSED");
616*49cdfc7eSAndroid Build Coastguard Worker 		}
617*49cdfc7eSAndroid Build Coastguard Worker 
618*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Exit block 7");
619*49cdfc7eSAndroid Build Coastguard Worker 
620*49cdfc7eSAndroid Build Coastguard Worker 		stop_child();
621*49cdfc7eSAndroid Build Coastguard Worker 		close(fd);
622*49cdfc7eSAndroid Build Coastguard Worker 	}
623*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
624*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
625*49cdfc7eSAndroid Build Coastguard Worker }
626