1865695bbc89088b9526ea9045410e5afb70a985cplars/*
2865695bbc89088b9526ea9045410e5afb70a985cplars *
3865695bbc89088b9526ea9045410e5afb70a985cplars *   Copyright (c) International Business Machines  Corp., 2001
4865695bbc89088b9526ea9045410e5afb70a985cplars *
5865695bbc89088b9526ea9045410e5afb70a985cplars *   This program is free software;  you can redistribute it and/or modify
6865695bbc89088b9526ea9045410e5afb70a985cplars *   it under the terms of the GNU General Public License as published by
7865695bbc89088b9526ea9045410e5afb70a985cplars *   the Free Software Foundation; either version 2 of the License, or
8865695bbc89088b9526ea9045410e5afb70a985cplars *   (at your option) any later version.
9865695bbc89088b9526ea9045410e5afb70a985cplars *
10865695bbc89088b9526ea9045410e5afb70a985cplars *   This program is distributed in the hope that it will be useful,
11865695bbc89088b9526ea9045410e5afb70a985cplars *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12865695bbc89088b9526ea9045410e5afb70a985cplars *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13865695bbc89088b9526ea9045410e5afb70a985cplars *   the GNU General Public License for more details.
14865695bbc89088b9526ea9045410e5afb70a985cplars *
15865695bbc89088b9526ea9045410e5afb70a985cplars *   You should have received a copy of the GNU General Public License
16865695bbc89088b9526ea9045410e5afb70a985cplars *   along with this program;  if not, write to the Free Software
174548c6cf9bcdd96d8303caa4130ab638b61f8a30Wanlong Gao *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18865695bbc89088b9526ea9045410e5afb70a985cplars */
19865695bbc89088b9526ea9045410e5afb70a985cplars
20865695bbc89088b9526ea9045410e5afb70a985cplars/*
21865695bbc89088b9526ea9045410e5afb70a985cplars * NAME
2256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *	fcntl14.c
23865695bbc89088b9526ea9045410e5afb70a985cplars *
24865695bbc89088b9526ea9045410e5afb70a985cplars * DESCRIPTION
2556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *	File locking test cases for fcntl. In Linux, S_ENFMT is not implemented
26865695bbc89088b9526ea9045410e5afb70a985cplars *	in the kernel. However all standard Unix kernels define S_ENFMT as
27865695bbc89088b9526ea9045410e5afb70a985cplars *	S_ISGID. So this test defines S_ENFMT as S_ISGID.
28865695bbc89088b9526ea9045410e5afb70a985cplars *
29865695bbc89088b9526ea9045410e5afb70a985cplars * ALGORITHM
3056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *	Various test cases are used to lock a file opened without mandatory
3156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *	locking, with mandatory locking and mandatory locking with NOBLOCK
32865695bbc89088b9526ea9045410e5afb70a985cplars *
33865695bbc89088b9526ea9045410e5afb70a985cplars * USAGE
34fa31d55d3486830313bd044f7333697ce6124d22nstraz *	fcntl14
35865695bbc89088b9526ea9045410e5afb70a985cplars *
36865695bbc89088b9526ea9045410e5afb70a985cplars * HISTORY
37865695bbc89088b9526ea9045410e5afb70a985cplars *	07/2001 Ported by Wayne Boyer
38865695bbc89088b9526ea9045410e5afb70a985cplars *
39865695bbc89088b9526ea9045410e5afb70a985cplars * RESTRICTIONS
4056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *	None
41865695bbc89088b9526ea9045410e5afb70a985cplars */
424cf809678474282d6e9cf95e11c1690e9affb89erobbiew#define _GNU_SOURCE 1
43865695bbc89088b9526ea9045410e5afb70a985cplars#include <fcntl.h>
44865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/types.h>
45865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/stat.h>
46865695bbc89088b9526ea9045410e5afb70a985cplars#include <signal.h>
47865695bbc89088b9526ea9045410e5afb70a985cplars#include <errno.h>
48db63921767012310af15f29035a334e50486292cmridge#include <sys/wait.h>
49923b23ff1fd1b77bd895949f9a6b4508c6485f33subrata_modak#include <inttypes.h>
500a643cbbb767d477008ea1d30e422aa78195813eGarrett Cooper#include "test.h"
51865695bbc89088b9526ea9045410e5afb70a985cplars
52db63921767012310af15f29035a334e50486292cmridge#define SKIP 0x0c00
53db63921767012310af15f29035a334e50486292cmridge#if SKIP == F_RDLCK || SKIP== F_WRLCK
54db63921767012310af15f29035a334e50486292cmridge#error invalid value for SKIP, must be distinct from F_RDLCK and F_WRLCK
55db63921767012310af15f29035a334e50486292cmridge#endif
56db63921767012310af15f29035a334e50486292cmridge#ifndef S_ENFMT
57865695bbc89088b9526ea9045410e5afb70a985cplars#define S_ENFMT S_ISGID
58db63921767012310af15f29035a334e50486292cmridge#endif
59865695bbc89088b9526ea9045410e5afb70a985cplars
60865695bbc89088b9526ea9045410e5afb70a985cplars/* NOBLOCK - immediate success */
61865695bbc89088b9526ea9045410e5afb70a985cplars#define NOBLOCK 2
62865695bbc89088b9526ea9045410e5afb70a985cplars
63865695bbc89088b9526ea9045410e5afb70a985cplars/* WILLBLOCK - blocks, then succeeds (parent must unlock records) */
64865695bbc89088b9526ea9045410e5afb70a985cplars#define WILLBLOCK 3
65865695bbc89088b9526ea9045410e5afb70a985cplars
66865695bbc89088b9526ea9045410e5afb70a985cplars#define TIME_OUT 60
67865695bbc89088b9526ea9045410e5afb70a985cplars
68865695bbc89088b9526ea9045410e5afb70a985cplarstypedef struct {
69865695bbc89088b9526ea9045410e5afb70a985cplars	short a_type;
70865695bbc89088b9526ea9045410e5afb70a985cplars	short a_whence;
71865695bbc89088b9526ea9045410e5afb70a985cplars	long a_start;
72865695bbc89088b9526ea9045410e5afb70a985cplars	long a_len;
7356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	short b_type;		/* SKIP means suppress fcntl call */
74865695bbc89088b9526ea9045410e5afb70a985cplars	short b_whence;
75865695bbc89088b9526ea9045410e5afb70a985cplars	long b_start;
76865695bbc89088b9526ea9045410e5afb70a985cplars	long b_len;
77865695bbc89088b9526ea9045410e5afb70a985cplars	short c_type;
78b370c3cc476bfe75f1999b04f432af308e5812f7subrata_modak	int c_whence;
79865695bbc89088b9526ea9045410e5afb70a985cplars	long c_start;
80865695bbc89088b9526ea9045410e5afb70a985cplars	long c_len;
81865695bbc89088b9526ea9045410e5afb70a985cplars	short c_flag;
8256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak} testcase;
83865695bbc89088b9526ea9045410e5afb70a985cplars
8456207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic testcase testcases[] = {
85865695bbc89088b9526ea9045410e5afb70a985cplars	/* Test cases: entire boundary */
86865695bbc89088b9526ea9045410e5afb70a985cplars	/* #1 Parent making a write lock on entire file */
8756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
8856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a read lock on entire file */
8956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 0L, 0L, WILLBLOCK},
9056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
91865695bbc89088b9526ea9045410e5afb70a985cplars	/* #2 Parent making a write lock on entire file */
9256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
9356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a write lock on entire file */
9456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 0L, 0L, WILLBLOCK},
95865695bbc89088b9526ea9045410e5afb70a985cplars
96865695bbc89088b9526ea9045410e5afb70a985cplars	/* #3 Parent making a read lock on entire file */
9756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
9856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a read lock on entire file */
9956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 0L, 0L, NOBLOCK},
100865695bbc89088b9526ea9045410e5afb70a985cplars
101865695bbc89088b9526ea9045410e5afb70a985cplars	/* #4 Parent making a read lock on entire file */
10256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
10356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a write lock on entire file */
10456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 0L, 0L, WILLBLOCK},
105865695bbc89088b9526ea9045410e5afb70a985cplars
106865695bbc89088b9526ea9045410e5afb70a985cplars	/* Test case: start boundary */
107865695bbc89088b9526ea9045410e5afb70a985cplars	/* #5 Parent making a write lock on entire file */
10856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
10956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /*
11056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * Child attempting a read lock from beginning of
11156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * file for 5 bytes
11256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  */
11356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 0L, 5L, WILLBLOCK},
114865695bbc89088b9526ea9045410e5afb70a985cplars
115865695bbc89088b9526ea9045410e5afb70a985cplars	/* #6 Parent making a write lock on entire file */
11656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
11756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /*
11856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * Child attempting a write lock from beginning of
11956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * file for 5 bytes
12056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  */
12156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 0L, 5L, WILLBLOCK},
122865695bbc89088b9526ea9045410e5afb70a985cplars
123865695bbc89088b9526ea9045410e5afb70a985cplars	/* #7 Parent making a read lock on entire file */
12456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
12556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /*
12656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * Child attempting a read lock from beginning of
12756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * file for 5 bytes
12856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  */
12956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 0L, 5L, NOBLOCK},
130865695bbc89088b9526ea9045410e5afb70a985cplars
131865695bbc89088b9526ea9045410e5afb70a985cplars	/* #8 Parent making a read lock on entire file */
13256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
13356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /*
13456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * Child attempting a write lock from beginning of
13556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * file for 5 bytes
13656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  */
13756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 0L, 5L, WILLBLOCK},
138865695bbc89088b9526ea9045410e5afb70a985cplars
139865695bbc89088b9526ea9045410e5afb70a985cplars	/* Test cases: end boundary */
140865695bbc89088b9526ea9045410e5afb70a985cplars	/* #9 Parent making a write lock on entire file */
14156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
14256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a read lock from byte 7 to end of file */
14356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 7L, 0L, WILLBLOCK},
144865695bbc89088b9526ea9045410e5afb70a985cplars
145865695bbc89088b9526ea9045410e5afb70a985cplars	/* #10 Parent making a write lock on entire file */
14656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
14756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a write lock from byte 7 to end of file */
14856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 7L, 0L, WILLBLOCK},
149865695bbc89088b9526ea9045410e5afb70a985cplars
150865695bbc89088b9526ea9045410e5afb70a985cplars	/* #11 Parent making a read lock on entire file */
15156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
15256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a read lock from byte 7 to end of file */
15356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 7L, 0L, NOBLOCK},
154865695bbc89088b9526ea9045410e5afb70a985cplars
155865695bbc89088b9526ea9045410e5afb70a985cplars	/* #12 Parent making a read lock on entire file */
15656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
15756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a write lock from byte 7 to end of file */
15856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 7L, 0L, WILLBLOCK},
159865695bbc89088b9526ea9045410e5afb70a985cplars
160865695bbc89088b9526ea9045410e5afb70a985cplars	/* Test cases: entire boundary ( less than entire file) */
1614bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak	/*
162865695bbc89088b9526ea9045410e5afb70a985cplars	 * #13 Parent making a write lock from beginning of
163865695bbc89088b9526ea9045410e5afb70a985cplars	 * file for 5 bytes
164865695bbc89088b9526ea9045410e5afb70a985cplars	 */
16556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
16656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /*
16756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * Child attempting a read lock from beginning of
16856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * file for 5 bytes
16956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  */
17056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 0L, 5L, WILLBLOCK},
171865695bbc89088b9526ea9045410e5afb70a985cplars
172865695bbc89088b9526ea9045410e5afb70a985cplars	/*
173865695bbc89088b9526ea9045410e5afb70a985cplars	 * #14 Parent making a write lock from beginning of file
174865695bbc89088b9526ea9045410e5afb70a985cplars	 * for 5 bytes
175865695bbc89088b9526ea9045410e5afb70a985cplars	 */
17656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
17756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /*
17856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * Child attempting a write lock from beginning of
17956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * file for 5 bytes
18056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  */
18156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 0L, 5L, WILLBLOCK},
182865695bbc89088b9526ea9045410e5afb70a985cplars
183865695bbc89088b9526ea9045410e5afb70a985cplars	/*
184865695bbc89088b9526ea9045410e5afb70a985cplars	 * #15 Parent making a read lock from beginning of
185865695bbc89088b9526ea9045410e5afb70a985cplars	 * file for 5 bytes
186865695bbc89088b9526ea9045410e5afb70a985cplars	 */
18756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
18856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /*
18956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * Child attempting a read lock from beginning of
19056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * file for 5 bytes
19156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  */
19256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 0L, 5L, NOBLOCK},
193865695bbc89088b9526ea9045410e5afb70a985cplars
194865695bbc89088b9526ea9045410e5afb70a985cplars	/*
195865695bbc89088b9526ea9045410e5afb70a985cplars	 * #16 Parent making a read lock from beginning of
196865695bbc89088b9526ea9045410e5afb70a985cplars	 * file for 5 bytes
197865695bbc89088b9526ea9045410e5afb70a985cplars	 */
19856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
19956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /*
20056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * Child attempting a write lock from beginning
20156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  * of file for 5 bytes
20256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	  */
20356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 0L, 5L, WILLBLOCK},
204865695bbc89088b9526ea9045410e5afb70a985cplars
205865695bbc89088b9526ea9045410e5afb70a985cplars	/* Test cases: inside boundary */
206865695bbc89088b9526ea9045410e5afb70a985cplars	/*
207865695bbc89088b9526ea9045410e5afb70a985cplars	 * #17 Parent making a write lock from beginning
208865695bbc89088b9526ea9045410e5afb70a985cplars	 * of file for 5 bytes
209865695bbc89088b9526ea9045410e5afb70a985cplars	 */
21056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
21156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a read lock from byte 2 to byte 4 */
21256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 1L, 3L, WILLBLOCK},
213865695bbc89088b9526ea9045410e5afb70a985cplars
214865695bbc89088b9526ea9045410e5afb70a985cplars	/*
215865695bbc89088b9526ea9045410e5afb70a985cplars	 * #18 Parent making a write lock from beginning of
216865695bbc89088b9526ea9045410e5afb70a985cplars	 * file for 5 bytes
217865695bbc89088b9526ea9045410e5afb70a985cplars	 */
21856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
21956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a write lock from byte 2 to byte 4 */
22056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 1L, 3L, WILLBLOCK},
221865695bbc89088b9526ea9045410e5afb70a985cplars
222865695bbc89088b9526ea9045410e5afb70a985cplars	/*
223865695bbc89088b9526ea9045410e5afb70a985cplars	 * #19 Parent making a read lock from beginning of
224865695bbc89088b9526ea9045410e5afb70a985cplars	 * file for 5 bytes
225865695bbc89088b9526ea9045410e5afb70a985cplars	 */
22656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
22756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a read lock from byte 2 to byte 4 */
22856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 1L, 3L, NOBLOCK},
229865695bbc89088b9526ea9045410e5afb70a985cplars
230865695bbc89088b9526ea9045410e5afb70a985cplars	/*
231865695bbc89088b9526ea9045410e5afb70a985cplars	 * #20 Parent making a read lock from beginning of
232865695bbc89088b9526ea9045410e5afb70a985cplars	 * file for 5 bytes
233865695bbc89088b9526ea9045410e5afb70a985cplars	 */
23456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
23556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a write lock from byte 2 to byte 4 */
23656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 1L, 3L, WILLBLOCK},
237865695bbc89088b9526ea9045410e5afb70a985cplars
238865695bbc89088b9526ea9045410e5afb70a985cplars	/* Test cases: cross boundary (inside to after) */
239865695bbc89088b9526ea9045410e5afb70a985cplars	/*
240865695bbc89088b9526ea9045410e5afb70a985cplars	 * #21 Parent making a write lock from beginning of
241865695bbc89088b9526ea9045410e5afb70a985cplars	 * file for 5 bytes
242865695bbc89088b9526ea9045410e5afb70a985cplars	 */
24356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
24456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a read lock from byte 3 to byte 7 */
24556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 2L, 5L, WILLBLOCK},
246865695bbc89088b9526ea9045410e5afb70a985cplars
2474bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak	/*
248865695bbc89088b9526ea9045410e5afb70a985cplars	 * #22 Parent making a write lock from beginning
249865695bbc89088b9526ea9045410e5afb70a985cplars	 * of file for 5 bytes
250865695bbc89088b9526ea9045410e5afb70a985cplars	 */
25156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
25256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a write lock from byte 3 to byte 7 */
25356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 2L, 5L, WILLBLOCK},
254865695bbc89088b9526ea9045410e5afb70a985cplars
255865695bbc89088b9526ea9045410e5afb70a985cplars	/*
256865695bbc89088b9526ea9045410e5afb70a985cplars	 * #23 Parent making a read lock from beginning of
257865695bbc89088b9526ea9045410e5afb70a985cplars	 * file for 5 bytes
258865695bbc89088b9526ea9045410e5afb70a985cplars	 */
25956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
26056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a read lock from byte 3 to byte 7 */
26156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 2L, 5L, NOBLOCK},
262865695bbc89088b9526ea9045410e5afb70a985cplars
263865695bbc89088b9526ea9045410e5afb70a985cplars	/*
264865695bbc89088b9526ea9045410e5afb70a985cplars	 * #24 Parent making a read lock from beginning of
265865695bbc89088b9526ea9045410e5afb70a985cplars	 * file for 5 bytes
266865695bbc89088b9526ea9045410e5afb70a985cplars	 */
26756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
26856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a write lock from byte 3 to byte 7 */
26956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 2L, 5L, WILLBLOCK},
270865695bbc89088b9526ea9045410e5afb70a985cplars
271865695bbc89088b9526ea9045410e5afb70a985cplars	/* Test cases: outside boundary (after) */
272865695bbc89088b9526ea9045410e5afb70a985cplars
273865695bbc89088b9526ea9045410e5afb70a985cplars	/*
274865695bbc89088b9526ea9045410e5afb70a985cplars	 * #25 Parent making a write lock from beginning of
275865695bbc89088b9526ea9045410e5afb70a985cplars	 * file for 5 bytes
276865695bbc89088b9526ea9045410e5afb70a985cplars	 */
27756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
27856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /*  Child attempting a read lock from byte 7 to end of file */
27956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 6L, 0L, NOBLOCK},
280865695bbc89088b9526ea9045410e5afb70a985cplars
281865695bbc89088b9526ea9045410e5afb70a985cplars	/*
282865695bbc89088b9526ea9045410e5afb70a985cplars	 * #26 Parent making a write lock from beginning of
283865695bbc89088b9526ea9045410e5afb70a985cplars	 * file for 5 bytes
284865695bbc89088b9526ea9045410e5afb70a985cplars	 */
28556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
28656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a write lock from byte 7 to end of file */
28756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 6L, 0L, NOBLOCK},
288865695bbc89088b9526ea9045410e5afb70a985cplars
289865695bbc89088b9526ea9045410e5afb70a985cplars	/*
290865695bbc89088b9526ea9045410e5afb70a985cplars	 * #27 Parent making a read lock from beginning of
291865695bbc89088b9526ea9045410e5afb70a985cplars	 * file for 5 bytes
292865695bbc89088b9526ea9045410e5afb70a985cplars	 */
29356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
29456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a read lock from byte 7 to end of file */
29556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 6L, 0L, NOBLOCK},
296865695bbc89088b9526ea9045410e5afb70a985cplars
297865695bbc89088b9526ea9045410e5afb70a985cplars	/*
298865695bbc89088b9526ea9045410e5afb70a985cplars	 * #28 Parent making a read lock from beginning of
299865695bbc89088b9526ea9045410e5afb70a985cplars	 * file for 5 bytes
300865695bbc89088b9526ea9045410e5afb70a985cplars	 */
30156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
30256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a write lock from byte 7 to end of file */
30356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 6L, 0L, NOBLOCK},
304865695bbc89088b9526ea9045410e5afb70a985cplars
305865695bbc89088b9526ea9045410e5afb70a985cplars	/* Test cases: outside boundary (before) */
306865695bbc89088b9526ea9045410e5afb70a985cplars
307865695bbc89088b9526ea9045410e5afb70a985cplars	/* #29 Parent making a write lock from byte 3 to byte 7 */
30856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 2L, 5L, SKIP, 0, 0L, 0L,
30956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a read lock from beginning of file to byte 2 */
31056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 0L, 2L, NOBLOCK},
311865695bbc89088b9526ea9045410e5afb70a985cplars
312865695bbc89088b9526ea9045410e5afb70a985cplars	/* #30 Parent making a write lock from byte 3 to byte 7 */
31356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 2L, 5L, SKIP, 0, 0L, 0L,
31456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a write lock from beginning of file to byte 2 */
31556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 0L, 2L, NOBLOCK},
316bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
317865695bbc89088b9526ea9045410e5afb70a985cplars	/* #31 Parent making a write lock from byte 3 to byte 7 */
31856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 2L, 5L, SKIP, 0, 0L, 0L,
31956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a read lock from beginning of file to byte 2 */
32056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 0L, 2L, NOBLOCK},
321865695bbc89088b9526ea9045410e5afb70a985cplars
322865695bbc89088b9526ea9045410e5afb70a985cplars	/* #32 Parent making a write lock from byte 3 to byte 7 */
32356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 2L, 5L, SKIP, 0, 0L, 0L,
32456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a write lock from beginning of file to byte 2 */
32556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 0L, 2L, NOBLOCK},
326865695bbc89088b9526ea9045410e5afb70a985cplars
327865695bbc89088b9526ea9045410e5afb70a985cplars	/* Test cases: cross boundary (before to inside) */
328865695bbc89088b9526ea9045410e5afb70a985cplars	/* #33 Parent making a write lock from byte 5 to end of file */
32956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 4L, 0L, SKIP, 0, 0L, 0L,
33056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a read lock from byte 3 to byte 7 */
33156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 2L, 5L, WILLBLOCK},
332865695bbc89088b9526ea9045410e5afb70a985cplars
333865695bbc89088b9526ea9045410e5afb70a985cplars	/* #34 Parent making a write lock from byte 5 to end of file */
33456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 4L, 0L, SKIP, 0, 0L, 0L,
33556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a write lock from byte 3 to byte 7 */
33656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 2L, 5L, WILLBLOCK},
337865695bbc89088b9526ea9045410e5afb70a985cplars
338865695bbc89088b9526ea9045410e5afb70a985cplars	/* #35 Parent making a read lock from byte 5 to end of file */
33956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 4L, 0L, SKIP, 0, 0L, 0L,
34056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a read lock from byte 3 to byte 7 */
34156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_RDLCK, 0, 2L, 5L, NOBLOCK},
342865695bbc89088b9526ea9045410e5afb70a985cplars
343865695bbc89088b9526ea9045410e5afb70a985cplars	/* #36 Parent making a read lock from byte 5 to end of file */
34456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_RDLCK, 0, 4L, 0L, SKIP, 0, 0L, 0L,
34556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting a write lock from byte 3 to byte 7 */
34656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 2L, 5L, WILLBLOCK},
347865695bbc89088b9526ea9045410e5afb70a985cplars
348865695bbc89088b9526ea9045410e5afb70a985cplars	/* Start of negative L_start and L_len test cases */
3494bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak	/*
350865695bbc89088b9526ea9045410e5afb70a985cplars	 * #37 Parent making write lock from byte 2 to byte 3
351865695bbc89088b9526ea9045410e5afb70a985cplars	 * with L_start = -3
352865695bbc89088b9526ea9045410e5afb70a985cplars	 */
35356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L,
35456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 1 */
35556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 1L, 1L, NOBLOCK},
356865695bbc89088b9526ea9045410e5afb70a985cplars
357865695bbc89088b9526ea9045410e5afb70a985cplars	/*
358865695bbc89088b9526ea9045410e5afb70a985cplars	 * #38 Parent making write lock from byte 2 to byte 3
359865695bbc89088b9526ea9045410e5afb70a985cplars	 * with L_start = -3
360865695bbc89088b9526ea9045410e5afb70a985cplars	 */
36156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L,
36256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 4 */
36356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 4L, 1L, NOBLOCK},
364865695bbc89088b9526ea9045410e5afb70a985cplars
365865695bbc89088b9526ea9045410e5afb70a985cplars	/*
366865695bbc89088b9526ea9045410e5afb70a985cplars	 * #39 Parent making write lock from byte 2 to byte 3
367865695bbc89088b9526ea9045410e5afb70a985cplars	 * with L_start = -3
368865695bbc89088b9526ea9045410e5afb70a985cplars	 */
36956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L,
37056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 2 */
37156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 2L, 1L, WILLBLOCK},
372865695bbc89088b9526ea9045410e5afb70a985cplars
373865695bbc89088b9526ea9045410e5afb70a985cplars	/*
374865695bbc89088b9526ea9045410e5afb70a985cplars	 * #40 Parent making write lock from byte 2 to byte 3
375865695bbc89088b9526ea9045410e5afb70a985cplars	 * with L_start = -3
376865695bbc89088b9526ea9045410e5afb70a985cplars	 */
37756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L,
37856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 3 */
37956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 3L, 1L, WILLBLOCK},
380865695bbc89088b9526ea9045410e5afb70a985cplars
381865695bbc89088b9526ea9045410e5afb70a985cplars	/*
382865695bbc89088b9526ea9045410e5afb70a985cplars	 * #41 Parent making write lock from byte 2 to byte 6
383865695bbc89088b9526ea9045410e5afb70a985cplars	 * with L_start = -3
384865695bbc89088b9526ea9045410e5afb70a985cplars	 */
38556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L,
38656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 1 */
38756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 1L, 1L, NOBLOCK},
388865695bbc89088b9526ea9045410e5afb70a985cplars
389865695bbc89088b9526ea9045410e5afb70a985cplars	/*
390865695bbc89088b9526ea9045410e5afb70a985cplars	 * #42 Parent making write lock from byte 2 to byte 6
391865695bbc89088b9526ea9045410e5afb70a985cplars	 * with L_start = -3
392865695bbc89088b9526ea9045410e5afb70a985cplars	 */
39356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L,
39456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 7 */
39556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 1L, 1L, NOBLOCK},
396865695bbc89088b9526ea9045410e5afb70a985cplars
397865695bbc89088b9526ea9045410e5afb70a985cplars	/*
398865695bbc89088b9526ea9045410e5afb70a985cplars	 * #43 Parent making write lock from byte 2 to byte 6
399865695bbc89088b9526ea9045410e5afb70a985cplars	 * with L_start = -3
400865695bbc89088b9526ea9045410e5afb70a985cplars	 */
40156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L,
40256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 2 */
40356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 2L, 1L, WILLBLOCK},
404865695bbc89088b9526ea9045410e5afb70a985cplars
405865695bbc89088b9526ea9045410e5afb70a985cplars	/*
406865695bbc89088b9526ea9045410e5afb70a985cplars	 * #44 Parent making write lock from byte 2 to byte 6
407865695bbc89088b9526ea9045410e5afb70a985cplars	 * with L_start = -3
408865695bbc89088b9526ea9045410e5afb70a985cplars	 */
40956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L,
41056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 5 */
41156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 5L, 1L, WILLBLOCK},
412865695bbc89088b9526ea9045410e5afb70a985cplars
413865695bbc89088b9526ea9045410e5afb70a985cplars	/*
414865695bbc89088b9526ea9045410e5afb70a985cplars	 * #45 Parent making write lock from byte 2 to byte 6
415865695bbc89088b9526ea9045410e5afb70a985cplars	 * with L_start = -3
416865695bbc89088b9526ea9045410e5afb70a985cplars	 */
41756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L,
41856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 6 */
41956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 6L, 1L, WILLBLOCK},
420865695bbc89088b9526ea9045410e5afb70a985cplars
421865695bbc89088b9526ea9045410e5afb70a985cplars	/*
422865695bbc89088b9526ea9045410e5afb70a985cplars	 * #46 Parent making write lock from byte 2 to byte 3 with
423865695bbc89088b9526ea9045410e5afb70a985cplars	 * L_start = -2 and L_len = -2
424865695bbc89088b9526ea9045410e5afb70a985cplars	 */
42556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L,
42656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 1 */
42756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 1L, 1L, NOBLOCK},
428865695bbc89088b9526ea9045410e5afb70a985cplars
429865695bbc89088b9526ea9045410e5afb70a985cplars	/*
430865695bbc89088b9526ea9045410e5afb70a985cplars	 * #47 Parent making write lock from byte 2 to byte 3 with
431865695bbc89088b9526ea9045410e5afb70a985cplars	 * L_start = -2 and L_len = -2
432865695bbc89088b9526ea9045410e5afb70a985cplars	 */
43356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, -2L, -2L, SKIP, 0, 0L, 0L,
43456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 4 */
43556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 4L, 1L, NOBLOCK},
436865695bbc89088b9526ea9045410e5afb70a985cplars
437865695bbc89088b9526ea9045410e5afb70a985cplars	/*
438865695bbc89088b9526ea9045410e5afb70a985cplars	 * #48 Parent making write lock from byte 2 to byte 3 with
439865695bbc89088b9526ea9045410e5afb70a985cplars	 * L_start = -2 and L_len = -2
440865695bbc89088b9526ea9045410e5afb70a985cplars	 */
44156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, -2L, -2L, SKIP, 0, 0L, 0L,
44256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 2 */
44356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 2L, 1L, WILLBLOCK},
444865695bbc89088b9526ea9045410e5afb70a985cplars
445865695bbc89088b9526ea9045410e5afb70a985cplars	/*
446865695bbc89088b9526ea9045410e5afb70a985cplars	 * #49 Parent making write lock from byte 2 to byte 3 with
447865695bbc89088b9526ea9045410e5afb70a985cplars	 * L_start = -2 and L_len = -2
448865695bbc89088b9526ea9045410e5afb70a985cplars	 */
44956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, -2L, -2L, SKIP, 0, 0L, 0L,
45056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 3 */
45156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 3L, 1L, WILLBLOCK},
452865695bbc89088b9526ea9045410e5afb70a985cplars
453865695bbc89088b9526ea9045410e5afb70a985cplars	/*
454865695bbc89088b9526ea9045410e5afb70a985cplars	 * #50 Parent making write lock from byte 6 to byte 7 with
455865695bbc89088b9526ea9045410e5afb70a985cplars	 * L_start = 2 and L_len = -2
456865695bbc89088b9526ea9045410e5afb70a985cplars	 */
45756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L,
45856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 5 */
45956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 5L, 1L, NOBLOCK},
460865695bbc89088b9526ea9045410e5afb70a985cplars
461865695bbc89088b9526ea9045410e5afb70a985cplars	/*
462865695bbc89088b9526ea9045410e5afb70a985cplars	 * #51 Parent making write lock from byte 6 to byte 7 with
463865695bbc89088b9526ea9045410e5afb70a985cplars	 * L_start = 2 and L_len = -2
464865695bbc89088b9526ea9045410e5afb70a985cplars	 */
46556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L,
46656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 8 */
46756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 8L, 1L, NOBLOCK},
468865695bbc89088b9526ea9045410e5afb70a985cplars
469865695bbc89088b9526ea9045410e5afb70a985cplars	/*
470865695bbc89088b9526ea9045410e5afb70a985cplars	 * #52 Parent making write lock from byte 6 to byte 7 with
471865695bbc89088b9526ea9045410e5afb70a985cplars	 * L_start = 2 and L_len = -2
472865695bbc89088b9526ea9045410e5afb70a985cplars	 */
47356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L,
47456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 6 */
47556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 6L, 1L, WILLBLOCK},
476865695bbc89088b9526ea9045410e5afb70a985cplars
477865695bbc89088b9526ea9045410e5afb70a985cplars	/*
478865695bbc89088b9526ea9045410e5afb70a985cplars	 * #53 Parent making write lock from byte 6 to byte 7 with
479865695bbc89088b9526ea9045410e5afb70a985cplars	 * L_start = 2 and L_len = -2
480865695bbc89088b9526ea9045410e5afb70a985cplars	 */
48156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L,
48256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 7 */
48356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 7L, 1L, WILLBLOCK},
484865695bbc89088b9526ea9045410e5afb70a985cplars
485865695bbc89088b9526ea9045410e5afb70a985cplars	/*
486865695bbc89088b9526ea9045410e5afb70a985cplars	 * #54 Parent making write lock from byte 3 to byte 7 with
487865695bbc89088b9526ea9045410e5afb70a985cplars	 * L_start = 2 and L_len = -5
488865695bbc89088b9526ea9045410e5afb70a985cplars	 */
48956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L,
49056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 2 */
49156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 2L, 1L, NOBLOCK},
492865695bbc89088b9526ea9045410e5afb70a985cplars
493865695bbc89088b9526ea9045410e5afb70a985cplars	/*
494865695bbc89088b9526ea9045410e5afb70a985cplars	 * #55 Parent making write lock from byte 3 to byte 7 with
495865695bbc89088b9526ea9045410e5afb70a985cplars	 * L_start = 2 and L_len = -5
496865695bbc89088b9526ea9045410e5afb70a985cplars	 */
49756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L,
49856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 8 */
49956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 8L, 1L, NOBLOCK},
500865695bbc89088b9526ea9045410e5afb70a985cplars
501865695bbc89088b9526ea9045410e5afb70a985cplars	/*
502865695bbc89088b9526ea9045410e5afb70a985cplars	 * #56 Parent making write lock from byte 3 to byte 7 with
503865695bbc89088b9526ea9045410e5afb70a985cplars	 * L_start = 2 and L_len = -5
504865695bbc89088b9526ea9045410e5afb70a985cplars	 */
50556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L,
50656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 3 */
50756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 3L, 1L, WILLBLOCK},
508865695bbc89088b9526ea9045410e5afb70a985cplars
509865695bbc89088b9526ea9045410e5afb70a985cplars	/*
510865695bbc89088b9526ea9045410e5afb70a985cplars	 * #57 Parent making write lock from byte 3 to byte 7 with
511865695bbc89088b9526ea9045410e5afb70a985cplars	 * L_start = 2 and L_len = -5
512865695bbc89088b9526ea9045410e5afb70a985cplars	 */
51356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L,
51456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 5 */
51556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 5L, 1L, WILLBLOCK},
516865695bbc89088b9526ea9045410e5afb70a985cplars
517865695bbc89088b9526ea9045410e5afb70a985cplars	/*
518865695bbc89088b9526ea9045410e5afb70a985cplars	 * #58 Parent making write lock from byte 3 to byte 7 with
519865695bbc89088b9526ea9045410e5afb70a985cplars	 * L_start = 2 and L_len = -5
520865695bbc89088b9526ea9045410e5afb70a985cplars	 */
52156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L,
52256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 7 */
52356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 7L, 1L, WILLBLOCK},
524865695bbc89088b9526ea9045410e5afb70a985cplars
525865695bbc89088b9526ea9045410e5afb70a985cplars	/* Test case for block 4 */
526865695bbc89088b9526ea9045410e5afb70a985cplars	/* #59 Parent making write lock on entire file */
52756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
52856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 /* Child attempting write lock on byte 15 to end of file */
52956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 F_WRLCK, 0, 15L, 0L, WILLBLOCK},
530865695bbc89088b9526ea9045410e5afb70a985cplars};
531865695bbc89088b9526ea9045410e5afb70a985cplars
53256207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic testcase *thiscase;
53356207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic struct flock flock;
53456207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic int parent, child, status, fail = 0;
53556207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic int got1 = 0;
53656207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic int fd;
53756207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic int test;
53856207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic char tmpname[40];
539865695bbc89088b9526ea9045410e5afb70a985cplars
540865695bbc89088b9526ea9045410e5afb70a985cplars#define FILEDATA	"ten bytes!"
541865695bbc89088b9526ea9045410e5afb70a985cplars
542b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubisvoid catch1(int sig);
543e61ddbaf9b02e1f4f94f4708551ecc825c1ac1d7Mike Frysingervoid catch_alarm(int sig);
544865695bbc89088b9526ea9045410e5afb70a985cplars
545fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubischar *TCID = "fcntl14";
546fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubisint TST_TOTAL = 1;
54711930d756e01af0a971eb57952208c6795a61b14Xiong Zhouint NO_NFS = 1;
548865695bbc89088b9526ea9045410e5afb70a985cplars
549d34d581c6a320e356a6cda923c7aa399479e812crobbiew#ifdef UCLINUX
550b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubisstatic char *argv0;
551d34d581c6a320e356a6cda923c7aa399479e812crobbiew#endif
552865695bbc89088b9526ea9045410e5afb70a985cplars
55356207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid cleanup(void)
5544cf809678474282d6e9cf95e11c1690e9affb89erobbiew{
5554cf809678474282d6e9cf95e11c1690e9affb89erobbiew	tst_rmdir();
556865695bbc89088b9526ea9045410e5afb70a985cplars}
557865695bbc89088b9526ea9045410e5afb70a985cplars
55856207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid setup(void)
559865695bbc89088b9526ea9045410e5afb70a985cplars{
5601e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	struct sigaction act;
5611e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew
5622c28215423293e443469a07ae7011135d058b671Garrett Cooper	tst_sig(FORK, DEF_HANDLER, cleanup);
563b370c3cc476bfe75f1999b04f432af308e5812f7subrata_modak	signal(SIGHUP, SIG_IGN);
564865695bbc89088b9526ea9045410e5afb70a985cplars	umask(0);
5652c28215423293e443469a07ae7011135d058b671Garrett Cooper	TEST_PAUSE;
566b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis	tst_tmpdir();
567865695bbc89088b9526ea9045410e5afb70a985cplars	parent = getpid();
568865695bbc89088b9526ea9045410e5afb70a985cplars
569865695bbc89088b9526ea9045410e5afb70a985cplars	sprintf(tmpname, "fcntl2.%d", parent);
570865695bbc89088b9526ea9045410e5afb70a985cplars
571865695bbc89088b9526ea9045410e5afb70a985cplars	/* setup signal handler for signal from child */
5721e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	memset(&act, 0, sizeof(act));
5731e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	act.sa_handler = catch1;
5741e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	sigemptyset(&act.sa_mask);
5751e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	sigaddset(&act.sa_mask, SIGUSR1);
5761e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	if ((sigaction(SIGUSR1, &act, NULL)) < 0) {
577865695bbc89088b9526ea9045410e5afb70a985cplars		tst_resm(TFAIL, "SIGUSR1 signal setup failed, errno = %d",
578865695bbc89088b9526ea9045410e5afb70a985cplars			 errno);
579865695bbc89088b9526ea9045410e5afb70a985cplars		cleanup();
580354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	}
5811e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew
5821e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	memset(&act, 0, sizeof(act));
5831e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	act.sa_handler = catch_alarm;
5841e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	sigemptyset(&act.sa_mask);
5851e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	sigaddset(&act.sa_mask, SIGALRM);
5861e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	if ((sigaction(SIGALRM, &act, NULL)) < 0) {
587865695bbc89088b9526ea9045410e5afb70a985cplars		tst_resm(TFAIL, "SIGALRM signal setup failed");
588865695bbc89088b9526ea9045410e5afb70a985cplars		cleanup();
589354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	}
590865695bbc89088b9526ea9045410e5afb70a985cplars}
591865695bbc89088b9526ea9045410e5afb70a985cplars
59256207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid wake_parent(void)
593db63921767012310af15f29035a334e50486292cmridge{
59456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	if ((kill(parent, SIGUSR1)) < 0) {
59556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		tst_resm(TFAIL, "Attempt to send signal to parent " "failed");
59656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		tst_resm(TFAIL, "Test case %d, errno = %d", test + 1, errno);
59756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		fail = 1;
59856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	}
599db63921767012310af15f29035a334e50486292cmridge}
600db63921767012310af15f29035a334e50486292cmridge
601c57fba5535abf457e33dd7a986b6c512d95cdef6Mike Frysingervoid do_usleep_child(void)
60256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak{
60356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	usleep(100000);		/* XXX how long is long enough? */
604d34d581c6a320e356a6cda923c7aa399479e812crobbiew	wake_parent();
605d34d581c6a320e356a6cda923c7aa399479e812crobbiew	exit(0);
606d34d581c6a320e356a6cda923c7aa399479e812crobbiew}
607d34d581c6a320e356a6cda923c7aa399479e812crobbiew
608c57fba5535abf457e33dd7a986b6c512d95cdef6Mike Frysingervoid dochild(void)
609b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis{
61056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	int rc;
61156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	pid_t pid;
612db63921767012310af15f29035a334e50486292cmridge
6134cf809678474282d6e9cf95e11c1690e9affb89erobbiew	flock.l_type = thiscase->c_type;
6144cf809678474282d6e9cf95e11c1690e9affb89erobbiew	flock.l_whence = thiscase->c_whence;
6154cf809678474282d6e9cf95e11c1690e9affb89erobbiew	flock.l_start = thiscase->c_start;
6164cf809678474282d6e9cf95e11c1690e9affb89erobbiew	flock.l_len = thiscase->c_len;
6174cf809678474282d6e9cf95e11c1690e9affb89erobbiew	flock.l_pid = 0;
6184cf809678474282d6e9cf95e11c1690e9affb89erobbiew	fail = 0;
6194cf809678474282d6e9cf95e11c1690e9affb89erobbiew
6204cf809678474282d6e9cf95e11c1690e9affb89erobbiew	/*
6214cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 * Check to see if child lock will succeed. If it will, FLOCK
6224cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 * structure will return with l_type changed to F_UNLCK. If it will
6234cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 * not, the parent pid will be returned in l_pid and the type of
6244cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 * lock that will block it in l_type.
6254cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 */
626db63921767012310af15f29035a334e50486292cmridge	if ((rc = fcntl(fd, F_GETLK, &flock)) < 0) {
6274cf809678474282d6e9cf95e11c1690e9affb89erobbiew		tst_resm(TFAIL, "Attempt to check lock status failed");
62856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		tst_resm(TFAIL, "Test case %d, errno = %d", test + 1, errno);
6294cf809678474282d6e9cf95e11c1690e9affb89erobbiew		fail = 1;
630db63921767012310af15f29035a334e50486292cmridge	} else {
6314cf809678474282d6e9cf95e11c1690e9affb89erobbiew
63256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		if ((thiscase->c_flag) == NOBLOCK) {
63356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			if (flock.l_type != F_UNLCK) {
63456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TFAIL,
63556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 "Test case %d, GETLK: type = %d, "
63656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 "%d was expected", test + 1,
63756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 flock.l_type, F_UNLCK);
63856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				fail = 1;
63956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			}
6404cf809678474282d6e9cf95e11c1690e9affb89erobbiew
64156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			if (flock.l_whence != thiscase->c_whence) {
64256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TFAIL,
64356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 "Test case %d, GETLK: whence = %d, "
64456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 "should have remained %d", test + 1,
64556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 flock.l_whence, thiscase->c_whence);
64656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				fail = 1;
64756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			}
6484cf809678474282d6e9cf95e11c1690e9affb89erobbiew
64956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			if (flock.l_start != thiscase->c_start) {
65056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TFAIL,
651354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 "Test case %d, GETLK: start = %" PRId64
652354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 ", " "should have remained %" PRId64,
653354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 test + 1, (int64_t) flock.l_start,
654354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 (int64_t) thiscase->c_start);
65556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				fail = 1;
65656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			}
6574cf809678474282d6e9cf95e11c1690e9affb89erobbiew
65856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			if (flock.l_len != thiscase->c_len) {
65956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TFAIL,
660354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 "Test case %d, GETLK: len = %" PRId64
661354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 ", " "should have remained %" PRId64,
662354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 test + 1, (int64_t) flock.l_len,
663354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 (int64_t) thiscase->c_len);
66456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				fail = 1;
66556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			}
6664cf809678474282d6e9cf95e11c1690e9affb89erobbiew
66756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			if (flock.l_pid != 0) {
66856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TFAIL,
66956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 "Test case %d, GETLK: pid = %d, "
67056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 "should have remained 0", test + 1,
67156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 flock.l_pid);
67256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				fail = 1;
67356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			}
67456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		} else {
67556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			if (flock.l_pid != parent) {
67656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TFAIL,
67756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 "Test case %d, GETLK: pid = %d, "
67856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 "should be parent's id of %d",
67956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 test + 1, flock.l_pid, parent);
68056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				fail = 1;
68156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			}
6824cf809678474282d6e9cf95e11c1690e9affb89erobbiew
68356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			if (flock.l_type != thiscase->a_type) {
68456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TFAIL,
68556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 "Test case %d, GETLK: type = %d, "
68656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 "should be parent's first lock type of %d",
68756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 test + 1, flock.l_type,
68856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 thiscase->a_type);
68956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				fail = 1;
69056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			}
6914cf809678474282d6e9cf95e11c1690e9affb89erobbiew		}
6924cf809678474282d6e9cf95e11c1690e9affb89erobbiew	}
6934cf809678474282d6e9cf95e11c1690e9affb89erobbiew
6944cf809678474282d6e9cf95e11c1690e9affb89erobbiew	/*
6954cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 * now try to set the lock, nonblocking
6964cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 * This will succeed for NOBLOCK,
6974cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 * fail for WILLBLOCK
6984cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 */
6994cf809678474282d6e9cf95e11c1690e9affb89erobbiew	flock.l_type = thiscase->c_type;
7004cf809678474282d6e9cf95e11c1690e9affb89erobbiew	flock.l_whence = thiscase->c_whence;
7014cf809678474282d6e9cf95e11c1690e9affb89erobbiew	flock.l_start = thiscase->c_start;
7024cf809678474282d6e9cf95e11c1690e9affb89erobbiew	flock.l_len = thiscase->c_len;
7034cf809678474282d6e9cf95e11c1690e9affb89erobbiew	flock.l_pid = 0;
7044cf809678474282d6e9cf95e11c1690e9affb89erobbiew
705db63921767012310af15f29035a334e50486292cmridge	if ((rc = fcntl(fd, F_SETLK, &flock)) < 0) {
7064cf809678474282d6e9cf95e11c1690e9affb89erobbiew		if ((thiscase->c_flag) == NOBLOCK) {
7074cf809678474282d6e9cf95e11c1690e9affb89erobbiew			tst_resm(TFAIL, "Attempt to set child NONBLOCKING "
7084cf809678474282d6e9cf95e11c1690e9affb89erobbiew				 "lock failed");
7094cf809678474282d6e9cf95e11c1690e9affb89erobbiew			tst_resm(TFAIL, "Test case %d, errno = %d",
7104cf809678474282d6e9cf95e11c1690e9affb89erobbiew				 test + 1, errno);
7114cf809678474282d6e9cf95e11c1690e9affb89erobbiew			fail = 1;
7124cf809678474282d6e9cf95e11c1690e9affb89erobbiew		}
7134cf809678474282d6e9cf95e11c1690e9affb89erobbiew	}
7144cf809678474282d6e9cf95e11c1690e9affb89erobbiew
7154cf809678474282d6e9cf95e11c1690e9affb89erobbiew	if ((thiscase->c_flag) == WILLBLOCK) {
716db63921767012310af15f29035a334e50486292cmridge		if (rc != -1 || (errno != EACCES && errno != EAGAIN)) {
717db63921767012310af15f29035a334e50486292cmridge			tst_resm(TFAIL,
71856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				 "SETLK: rc = %d, errno = %d, -1/EAGAIN or EACCES "
719db63921767012310af15f29035a334e50486292cmridge				 "was expected", rc, errno);
7204cf809678474282d6e9cf95e11c1690e9affb89erobbiew			fail = 1;
7214cf809678474282d6e9cf95e11c1690e9affb89erobbiew		}
72256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		if (rc == 0) {
72356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			/* accidentally got the lock */
72456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			/* XXX how to clean up? */
72556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			(void)fcntl(fd, F_UNLCK, &flock);
72656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		}
7274cf809678474282d6e9cf95e11c1690e9affb89erobbiew		/*
7284cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 * Lock should succeed after blocking and parent releases
729db63921767012310af15f29035a334e50486292cmridge		 * lock, tell the parent to release the locks.
73056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * Do the lock in this process, send the signal in a child
73156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * process, so that the SETLKW actually uses the blocking
73256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * mechanism in the kernel.
73356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 *
73456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * XXX inherent race: we want to wait until the
73556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * F_SETLKW has started, but we don't have a way to
73656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * check that reliably in the child.  (We'd
73756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * need some way to have fcntl() atomically unblock a
73856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * signal and wait for the lock.)
7394cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 */
74056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		pid = FORK_OR_VFORK();
74156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		switch (pid) {
74256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		case -1:
74356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			tst_resm(TFAIL, "Fork failed");
744dd9ddae284b4e49ab9185f3c6025674d27f7a284Han Pingtian			fail = 1;
74556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			break;
746b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		case 0:
747d34d581c6a320e356a6cda923c7aa399479e812crobbiew#ifdef UCLINUX
74864bcf4a03c48557fc26887f501d44343dbc4df3fGarrett Cooper			if (self_exec(argv0, "nd", 1, parent) < 0) {
74956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TFAIL, "self_exec failed");
75056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				break;
75156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			}
752d34d581c6a320e356a6cda923c7aa399479e812crobbiew#else
75356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			do_usleep_child();
754d34d581c6a320e356a6cda923c7aa399479e812crobbiew#endif
75556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			break;
756db63921767012310af15f29035a334e50486292cmridge
75756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		default:
75856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			if ((rc = fcntl(fd, F_SETLKW, &flock)) < 0) {
75956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TFAIL, "Attempt to set child BLOCKING "
76056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 "lock failed");
76156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TFAIL, "Test case %d, errno = %d",
76256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 test + 1, errno);
76356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				fail = 1;
76456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			}
76556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			waitpid(pid, &status, 0);
76656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			break;
76756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		}
7684cf809678474282d6e9cf95e11c1690e9affb89erobbiew	}
7694cf809678474282d6e9cf95e11c1690e9affb89erobbiew	if (fail) {
7704cf809678474282d6e9cf95e11c1690e9affb89erobbiew		exit(1);
7714cf809678474282d6e9cf95e11c1690e9affb89erobbiew	} else {
7724cf809678474282d6e9cf95e11c1690e9affb89erobbiew		exit(0);
7734cf809678474282d6e9cf95e11c1690e9affb89erobbiew	}
7744cf809678474282d6e9cf95e11c1690e9affb89erobbiew}
7754cf809678474282d6e9cf95e11c1690e9affb89erobbiew
7764cf809678474282d6e9cf95e11c1690e9affb89erobbiewvoid run_test(int file_flag, int file_mode, int seek, int start, int end)
7774cf809678474282d6e9cf95e11c1690e9affb89erobbiew{
77856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	fail = 0;
77949058422e9a874f36094095c9df17dbab5281ba8robbiew
780865695bbc89088b9526ea9045410e5afb70a985cplars	for (test = start; test < end; test++) {
781865695bbc89088b9526ea9045410e5afb70a985cplars		fd = open(tmpname, file_flag, file_mode);
782b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		if (fd < 0)
783865695bbc89088b9526ea9045410e5afb70a985cplars			tst_brkm(TBROK, cleanup, "open() failed");
784865695bbc89088b9526ea9045410e5afb70a985cplars
785b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		if (write(fd, FILEDATA, 10) < 0)
786865695bbc89088b9526ea9045410e5afb70a985cplars			tst_brkm(TBROK, cleanup, "write() failed");
787865695bbc89088b9526ea9045410e5afb70a985cplars
788865695bbc89088b9526ea9045410e5afb70a985cplars		if (seek) {
789b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis			if (lseek(fd, seek, 0) < 0)
790865695bbc89088b9526ea9045410e5afb70a985cplars				tst_brkm(TBROK, cleanup, "lseek() failed");
791865695bbc89088b9526ea9045410e5afb70a985cplars		}
792865695bbc89088b9526ea9045410e5afb70a985cplars
793865695bbc89088b9526ea9045410e5afb70a985cplars		thiscase = &testcases[test];
794865695bbc89088b9526ea9045410e5afb70a985cplars		flock.l_type = thiscase->a_type;
795865695bbc89088b9526ea9045410e5afb70a985cplars		flock.l_whence = thiscase->a_whence;
796865695bbc89088b9526ea9045410e5afb70a985cplars		flock.l_start = thiscase->a_start;
797865695bbc89088b9526ea9045410e5afb70a985cplars		flock.l_len = thiscase->a_len;
798865695bbc89088b9526ea9045410e5afb70a985cplars
799865695bbc89088b9526ea9045410e5afb70a985cplars		/* set the initial parent lock on the file */
80056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		if ((fcntl(fd, F_SETLK, &flock)) < 0) {
801865695bbc89088b9526ea9045410e5afb70a985cplars			tst_resm(TFAIL, "First parent lock failed");
802865695bbc89088b9526ea9045410e5afb70a985cplars			tst_resm(TFAIL, "Test case %d, errno = %d",
803865695bbc89088b9526ea9045410e5afb70a985cplars				 test + 1, errno);
804865695bbc89088b9526ea9045410e5afb70a985cplars			fail = 1;
805865695bbc89088b9526ea9045410e5afb70a985cplars		}
806865695bbc89088b9526ea9045410e5afb70a985cplars
807865695bbc89088b9526ea9045410e5afb70a985cplars		if ((thiscase->b_type) != SKIP) {
808865695bbc89088b9526ea9045410e5afb70a985cplars			flock.l_type = thiscase->b_type;
809865695bbc89088b9526ea9045410e5afb70a985cplars			flock.l_whence = thiscase->b_whence;
810865695bbc89088b9526ea9045410e5afb70a985cplars			flock.l_start = thiscase->b_start;
811865695bbc89088b9526ea9045410e5afb70a985cplars			flock.l_len = thiscase->b_len;
812865695bbc89088b9526ea9045410e5afb70a985cplars
813865695bbc89088b9526ea9045410e5afb70a985cplars			/* set the second parent lock */
814865695bbc89088b9526ea9045410e5afb70a985cplars			if ((fcntl(fd, F_SETLK, &flock)) < 0) {
815865695bbc89088b9526ea9045410e5afb70a985cplars				tst_resm(TFAIL, "Second parent lock failed");
816865695bbc89088b9526ea9045410e5afb70a985cplars				tst_resm(TFAIL, "Test case %d, errno = %d",
817865695bbc89088b9526ea9045410e5afb70a985cplars					 test + 1, errno);
818865695bbc89088b9526ea9045410e5afb70a985cplars				fail = 1;
819865695bbc89088b9526ea9045410e5afb70a985cplars			}
820865695bbc89088b9526ea9045410e5afb70a985cplars		}
821865695bbc89088b9526ea9045410e5afb70a985cplars		if ((thiscase->c_type) == SKIP) {
822865695bbc89088b9526ea9045410e5afb70a985cplars			close(fd);
823865695bbc89088b9526ea9045410e5afb70a985cplars			tst_resm(TINFO, "skipping test %d", test + 1);
824b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis			continue;
825865695bbc89088b9526ea9045410e5afb70a985cplars		}
826b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis
827b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		/* Mask SIG_USR1 before forking child, to avoid race */
828865695bbc89088b9526ea9045410e5afb70a985cplars		(void)sighold(SIGUSR1);
829865695bbc89088b9526ea9045410e5afb70a985cplars
83049058422e9a874f36094095c9df17dbab5281ba8robbiew		/* flush the stdout to avoid garbled output */
83149058422e9a874f36094095c9df17dbab5281ba8robbiew		fflush(stdout);
83249058422e9a874f36094095c9df17dbab5281ba8robbiew
833d34d581c6a320e356a6cda923c7aa399479e812crobbiew		if ((child = FORK_OR_VFORK()) == 0) {
834d34d581c6a320e356a6cda923c7aa399479e812crobbiew#ifdef UCLINUX
835d34d581c6a320e356a6cda923c7aa399479e812crobbiew			if (self_exec(argv0, "nddddddddd", 2, thiscase->c_type,
836d34d581c6a320e356a6cda923c7aa399479e812crobbiew				      thiscase->c_whence, thiscase->c_start,
837d34d581c6a320e356a6cda923c7aa399479e812crobbiew				      thiscase->c_len, thiscase->c_flag,
838d34d581c6a320e356a6cda923c7aa399479e812crobbiew				      thiscase->a_type, fd, test, parent) < 0) {
839d34d581c6a320e356a6cda923c7aa399479e812crobbiew				tst_resm(TFAIL, "self_exec failed");
840d34d581c6a320e356a6cda923c7aa399479e812crobbiew				cleanup();
841354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			}
842d34d581c6a320e356a6cda923c7aa399479e812crobbiew#else
843865695bbc89088b9526ea9045410e5afb70a985cplars			dochild();
844d34d581c6a320e356a6cda923c7aa399479e812crobbiew#endif
845865695bbc89088b9526ea9045410e5afb70a985cplars		}
846b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		if (child < 0)
847dd9ddae284b4e49ab9185f3c6025674d27f7a284Han Pingtian			tst_brkm(TBROK|TERRNO, cleanup, "Fork failed");
848b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis
849865695bbc89088b9526ea9045410e5afb70a985cplars		if ((thiscase->c_flag) == WILLBLOCK) {
850865695bbc89088b9526ea9045410e5afb70a985cplars			/*
851865695bbc89088b9526ea9045410e5afb70a985cplars			 * Wait for a signal from the child then remove
852865695bbc89088b9526ea9045410e5afb70a985cplars			 * blocking lock. Set a 60 second alarm to break the
853865695bbc89088b9526ea9045410e5afb70a985cplars			 * pause just in case the child never signals us.
854865695bbc89088b9526ea9045410e5afb70a985cplars			 */
855865695bbc89088b9526ea9045410e5afb70a985cplars			alarm(TIME_OUT);
856865695bbc89088b9526ea9045410e5afb70a985cplars			sigpause(SIGUSR1);
857865695bbc89088b9526ea9045410e5afb70a985cplars
858865695bbc89088b9526ea9045410e5afb70a985cplars			/* turn off the alarm timer */
859865695bbc89088b9526ea9045410e5afb70a985cplars			alarm((unsigned)0);
860865695bbc89088b9526ea9045410e5afb70a985cplars			if (got1 != 1)
861865695bbc89088b9526ea9045410e5afb70a985cplars				tst_resm(TINFO, "Pause terminated without "
862865695bbc89088b9526ea9045410e5afb70a985cplars					 "signal SIGUSR1 from child");
863b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis			got1 = 0;
864865695bbc89088b9526ea9045410e5afb70a985cplars
865865695bbc89088b9526ea9045410e5afb70a985cplars			/*
866865695bbc89088b9526ea9045410e5afb70a985cplars			 * setup lock structure for parent to delete
867865695bbc89088b9526ea9045410e5afb70a985cplars			 * blocking lock then wait for child to exit
868865695bbc89088b9526ea9045410e5afb70a985cplars			 */
869865695bbc89088b9526ea9045410e5afb70a985cplars			flock.l_type = F_UNLCK;
870865695bbc89088b9526ea9045410e5afb70a985cplars			flock.l_whence = 0;
871865695bbc89088b9526ea9045410e5afb70a985cplars			flock.l_start = 0L;
872865695bbc89088b9526ea9045410e5afb70a985cplars			flock.l_len = 0L;
873865695bbc89088b9526ea9045410e5afb70a985cplars			if ((fcntl(fd, F_SETLK, &flock)) < 0) {
874865695bbc89088b9526ea9045410e5afb70a985cplars				tst_resm(TFAIL, "Attempt to release parent "
875865695bbc89088b9526ea9045410e5afb70a985cplars					 "lock failed");
876865695bbc89088b9526ea9045410e5afb70a985cplars				tst_resm(TFAIL, "Test case %d, errno = %d",
877865695bbc89088b9526ea9045410e5afb70a985cplars					 test + 1, errno);
878865695bbc89088b9526ea9045410e5afb70a985cplars				fail = 1;
879865695bbc89088b9526ea9045410e5afb70a985cplars			}
880865695bbc89088b9526ea9045410e5afb70a985cplars		}
881865695bbc89088b9526ea9045410e5afb70a985cplars		/*
882865695bbc89088b9526ea9045410e5afb70a985cplars		 * set a 60 second alarm to break the wait just in case the
883865695bbc89088b9526ea9045410e5afb70a985cplars		 * child doesn't terminate on its own accord
884865695bbc89088b9526ea9045410e5afb70a985cplars		 */
885865695bbc89088b9526ea9045410e5afb70a985cplars		alarm(TIME_OUT);
886865695bbc89088b9526ea9045410e5afb70a985cplars
887865695bbc89088b9526ea9045410e5afb70a985cplars		/* wait for the child to terminate and close the file */
888db63921767012310af15f29035a334e50486292cmridge		waitpid(child, &status, 0);
889865695bbc89088b9526ea9045410e5afb70a985cplars		/* turn off the alarm clock */
890865695bbc89088b9526ea9045410e5afb70a985cplars		alarm((unsigned)0);
891865695bbc89088b9526ea9045410e5afb70a985cplars		if (status != 0) {
892865695bbc89088b9526ea9045410e5afb70a985cplars			tst_resm(TFAIL, "tchild returned status 0x%x", status);
893865695bbc89088b9526ea9045410e5afb70a985cplars			fail = 1;
894865695bbc89088b9526ea9045410e5afb70a985cplars		}
895865695bbc89088b9526ea9045410e5afb70a985cplars		close(fd);
896b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		if (fail)
897865695bbc89088b9526ea9045410e5afb70a985cplars			tst_resm(TFAIL, "testcase:%d FAILED", test + 1);
898b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		else
899865695bbc89088b9526ea9045410e5afb70a985cplars			tst_resm(TPASS, "testcase:%d PASSED", test + 1);
900865695bbc89088b9526ea9045410e5afb70a985cplars	}
901865695bbc89088b9526ea9045410e5afb70a985cplars	unlink(tmpname);
902865695bbc89088b9526ea9045410e5afb70a985cplars}
903865695bbc89088b9526ea9045410e5afb70a985cplars
904e61ddbaf9b02e1f4f94f4708551ecc825c1ac1d7Mike Frysingervoid catch_alarm(int sig)
9054cf809678474282d6e9cf95e11c1690e9affb89erobbiew{
9064cf809678474282d6e9cf95e11c1690e9affb89erobbiew	/*
9074cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 * Timer has runout and child has not signaled, need
9084cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 * to kill off the child as it appears it will not
9094cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 * on its own accord. Check that it is still around
9104cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 * as it may have terminated abnormally while parent
9114cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 * was waiting for SIGUSR1 signal from it.
9124cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 */
9134cf809678474282d6e9cf95e11c1690e9affb89erobbiew	if (kill(child, 0) == 0) {
9144cf809678474282d6e9cf95e11c1690e9affb89erobbiew		kill(child, SIGKILL);
9154cf809678474282d6e9cf95e11c1690e9affb89erobbiew		perror("The child didnot terminate on its own accord");
9164cf809678474282d6e9cf95e11c1690e9affb89erobbiew	}
9174cf809678474282d6e9cf95e11c1690e9affb89erobbiew}
918865695bbc89088b9526ea9045410e5afb70a985cplars
919e61ddbaf9b02e1f4f94f4708551ecc825c1ac1d7Mike Frysingervoid catch1(int sig)
920b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis{
9211e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	struct sigaction act;
9221e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew
923865695bbc89088b9526ea9045410e5afb70a985cplars	/*
9244cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 * Set flag to let parent know that child is ready to have lock
9254cf809678474282d6e9cf95e11c1690e9affb89erobbiew	 * removed
926865695bbc89088b9526ea9045410e5afb70a985cplars	 */
9271e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	memset(&act, 0, sizeof(act));
9281e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	act.sa_handler = catch1;
9291e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	sigemptyset(&act.sa_mask);
9301e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	sigaddset(&act.sa_mask, SIGUSR1);
9311e4cf0c6ea46b11ed5d6fac9bc9646f1784645b7robbiew	sigaction(SIGUSR1, &act, NULL);
9324cf809678474282d6e9cf95e11c1690e9affb89erobbiew	got1++;
9334cf809678474282d6e9cf95e11c1690e9affb89erobbiew}
9344cf809678474282d6e9cf95e11c1690e9affb89erobbiew
93582180501c61d78b9c1a5d6ca5241047985eed8faXiong Zhoustatic void testcheck_end(int check_fail, char *msg)
93682180501c61d78b9c1a5d6ca5241047985eed8faXiong Zhou{
937b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis	if (check_fail)
93882180501c61d78b9c1a5d6ca5241047985eed8faXiong Zhou		tst_resm(TFAIL, "%s FAILED", msg);
939b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis	else
94082180501c61d78b9c1a5d6ca5241047985eed8faXiong Zhou		tst_resm(TPASS, "%s PASSED", msg);
94182180501c61d78b9c1a5d6ca5241047985eed8faXiong Zhou}
94282180501c61d78b9c1a5d6ca5241047985eed8faXiong Zhou
9434cf809678474282d6e9cf95e11c1690e9affb89erobbiewint main(int ac, char **av)
9444cf809678474282d6e9cf95e11c1690e9affb89erobbiew{
94589af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis	int lc;
9464cf809678474282d6e9cf95e11c1690e9affb89erobbiew
947d6d11d08678aac1ed2c370ea8e42e5f45aea07beCyril Hrubis	tst_parse_opts(ac, av, NULL, NULL);
948d34d581c6a320e356a6cda923c7aa399479e812crobbiew#ifdef UCLINUX
949d34d581c6a320e356a6cda923c7aa399479e812crobbiew	argv0 = av[0];
950d34d581c6a320e356a6cda923c7aa399479e812crobbiew
95164bcf4a03c48557fc26887f501d44343dbc4df3fGarrett Cooper	maybe_run_child(&do_usleep_child, "nd", 1, &parent);
952d34d581c6a320e356a6cda923c7aa399479e812crobbiew	thiscase = malloc(sizeof(testcase));
953d34d581c6a320e356a6cda923c7aa399479e812crobbiew
954d34d581c6a320e356a6cda923c7aa399479e812crobbiew	maybe_run_child(&dochild, "nddddddddd", 2, &thiscase->c_type,
955d34d581c6a320e356a6cda923c7aa399479e812crobbiew			&thiscase->c_whence, &thiscase->c_start,
956d34d581c6a320e356a6cda923c7aa399479e812crobbiew			&thiscase->c_len, &thiscase->c_flag, &thiscase->a_type,
957d34d581c6a320e356a6cda923c7aa399479e812crobbiew			&fd, &test, &parent);
958d34d581c6a320e356a6cda923c7aa399479e812crobbiew#endif
959d34d581c6a320e356a6cda923c7aa399479e812crobbiew
960b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis	setup();
9614cf809678474282d6e9cf95e11c1690e9affb89erobbiew
96211930d756e01af0a971eb57952208c6795a61b14Xiong Zhou	if (tst_fs_type(cleanup, ".") == TST_NFS_MAGIC)
96311930d756e01af0a971eb57952208c6795a61b14Xiong Zhou		NO_NFS = 0;
9645387ba03ce8eaddc7ec330ed03e579eed92596ccsubrata_modak
9654cf809678474282d6e9cf95e11c1690e9affb89erobbiew	for (lc = 0; TEST_LOOPING(lc); lc++) {
966d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang		tst_count = 0;
9674cf809678474282d6e9cf95e11c1690e9affb89erobbiew
968db63921767012310af15f29035a334e50486292cmridge/* //block1: */
9694cf809678474282d6e9cf95e11c1690e9affb89erobbiew		tst_resm(TINFO, "Enter block 1: without mandatory locking");
9704cf809678474282d6e9cf95e11c1690e9affb89erobbiew		fail = 0;
9714bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak		/*
97256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * try various file locks on an ordinary file without
9734cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 * mandatory locking
9744cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 */
9754cf809678474282d6e9cf95e11c1690e9affb89erobbiew		(void)run_test(O_CREAT | O_RDWR | O_TRUNC, 0777, 0, 0, 36);
97682180501c61d78b9c1a5d6ca5241047985eed8faXiong Zhou		testcheck_end(fail, "Block 1, test 1");
977865695bbc89088b9526ea9045410e5afb70a985cplars
9784cf809678474282d6e9cf95e11c1690e9affb89erobbiew		/* Now try with negative values for L_start and L_len */
9794cf809678474282d6e9cf95e11c1690e9affb89erobbiew		(void)run_test(O_CREAT | O_RDWR | O_TRUNC, 0777, 5, 36, 45);
98082180501c61d78b9c1a5d6ca5241047985eed8faXiong Zhou		testcheck_end(fail, "Block 1, test 2");
981865695bbc89088b9526ea9045410e5afb70a985cplars
9824cf809678474282d6e9cf95e11c1690e9affb89erobbiew		tst_resm(TINFO, "Exit block 1");
9834cf809678474282d6e9cf95e11c1690e9affb89erobbiew
984db63921767012310af15f29035a334e50486292cmridge/* //block2: */
9854bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak		/*
98611930d756e01af0a971eb57952208c6795a61b14Xiong Zhou		 * Skip block2 if test on NFS, since NFS does not support
98711930d756e01af0a971eb57952208c6795a61b14Xiong Zhou		 * mandatory locking
9884cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 */
98911930d756e01af0a971eb57952208c6795a61b14Xiong Zhou		tst_resm(TINFO, "Enter block 2: with mandatory locking");
99011930d756e01af0a971eb57952208c6795a61b14Xiong Zhou		if (NO_NFS) {
99111930d756e01af0a971eb57952208c6795a61b14Xiong Zhou			fail = 0;
99211930d756e01af0a971eb57952208c6795a61b14Xiong Zhou			/*
99311930d756e01af0a971eb57952208c6795a61b14Xiong Zhou			 * Try various locks on a file with mandatory
99411930d756e01af0a971eb57952208c6795a61b14Xiong Zhou			 * record locking this should behave the same
99511930d756e01af0a971eb57952208c6795a61b14Xiong Zhou			 * as an ordinary file
99611930d756e01af0a971eb57952208c6795a61b14Xiong Zhou			 */
99711930d756e01af0a971eb57952208c6795a61b14Xiong Zhou			(void)run_test(O_CREAT | O_RDWR | O_TRUNC,
99811930d756e01af0a971eb57952208c6795a61b14Xiong Zhou				S_ENFMT | S_IRUSR | S_IWUSR, 0, 0, 36);
99911930d756e01af0a971eb57952208c6795a61b14Xiong Zhou			testcheck_end(fail, "Block 2, test 1");
100011930d756e01af0a971eb57952208c6795a61b14Xiong Zhou
100111930d756e01af0a971eb57952208c6795a61b14Xiong Zhou			/* Now try negative values for L_start and L_len */
100211930d756e01af0a971eb57952208c6795a61b14Xiong Zhou			(void)run_test(O_CREAT | O_RDWR | O_TRUNC,
100311930d756e01af0a971eb57952208c6795a61b14Xiong Zhou				S_ENFMT | S_IRUSR | S_IWUSR, 5, 36, 45);
100411930d756e01af0a971eb57952208c6795a61b14Xiong Zhou			testcheck_end(fail, "Block 2, test 2");
1005b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		} else {
100611930d756e01af0a971eb57952208c6795a61b14Xiong Zhou			tst_resm(TCONF, "Skip block 2 as NFS does not"
100711930d756e01af0a971eb57952208c6795a61b14Xiong Zhou				" support mandatory locking");
1008b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		}
1009b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis
10104cf809678474282d6e9cf95e11c1690e9affb89erobbiew		tst_resm(TINFO, "Exit block 2");
10114cf809678474282d6e9cf95e11c1690e9affb89erobbiew
1012db63921767012310af15f29035a334e50486292cmridge/* //block3: */
10134cf809678474282d6e9cf95e11c1690e9affb89erobbiew		tst_resm(TINFO, "Enter block 3");
10144cf809678474282d6e9cf95e11c1690e9affb89erobbiew		fail = 0;
10154cf809678474282d6e9cf95e11c1690e9affb89erobbiew		/*
10164cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 * Check that proper error status is returned when invalid
10174cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 * argument used for WHENCE (negative value)
10184cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 */
10194cf809678474282d6e9cf95e11c1690e9affb89erobbiew
10204cf809678474282d6e9cf95e11c1690e9affb89erobbiew		fd = open(tmpname, O_CREAT | O_RDWR | O_TRUNC, 0777);
1021b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		if (fd < 0)
10224cf809678474282d6e9cf95e11c1690e9affb89erobbiew			tst_brkm(TBROK, cleanup, "open failed");
10234cf809678474282d6e9cf95e11c1690e9affb89erobbiew
1024b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		if (write(fd, FILEDATA, 10) < 0)
10254cf809678474282d6e9cf95e11c1690e9affb89erobbiew			tst_brkm(TBROK, cleanup, "write failed");
1026865695bbc89088b9526ea9045410e5afb70a985cplars
10274cf809678474282d6e9cf95e11c1690e9affb89erobbiew		flock.l_type = F_WRLCK;
10284cf809678474282d6e9cf95e11c1690e9affb89erobbiew		flock.l_whence = -1;
10294cf809678474282d6e9cf95e11c1690e9affb89erobbiew		flock.l_start = 0L;
10304cf809678474282d6e9cf95e11c1690e9affb89erobbiew		flock.l_len = 0L;
10314cf809678474282d6e9cf95e11c1690e9affb89erobbiew
10324cf809678474282d6e9cf95e11c1690e9affb89erobbiew		if ((fcntl(fd, F_SETLK, &flock)) < 0) {
10334cf809678474282d6e9cf95e11c1690e9affb89erobbiew			if (errno != EINVAL) {
10344cf809678474282d6e9cf95e11c1690e9affb89erobbiew				tst_resm(TFAIL, "Expected %d got %d",
10354cf809678474282d6e9cf95e11c1690e9affb89erobbiew					 EINVAL, errno);
10364cf809678474282d6e9cf95e11c1690e9affb89erobbiew				fail = 1;
10374cf809678474282d6e9cf95e11c1690e9affb89erobbiew			}
10384cf809678474282d6e9cf95e11c1690e9affb89erobbiew		} else {
10394cf809678474282d6e9cf95e11c1690e9affb89erobbiew			tst_resm(TFAIL, "Lock succeeded when it should have "
10404cf809678474282d6e9cf95e11c1690e9affb89erobbiew				 "failed");
1041865695bbc89088b9526ea9045410e5afb70a985cplars			fail = 1;
1042865695bbc89088b9526ea9045410e5afb70a985cplars		}
1043865695bbc89088b9526ea9045410e5afb70a985cplars
10444cf809678474282d6e9cf95e11c1690e9affb89erobbiew		close(fd);
10454cf809678474282d6e9cf95e11c1690e9affb89erobbiew		unlink(tmpname);
1046865695bbc89088b9526ea9045410e5afb70a985cplars
104782180501c61d78b9c1a5d6ca5241047985eed8faXiong Zhou		testcheck_end(fail, "Test with negative whence locking");
10484cf809678474282d6e9cf95e11c1690e9affb89erobbiew		tst_resm(TINFO, "Exit block 3");
1049865695bbc89088b9526ea9045410e5afb70a985cplars
1050db63921767012310af15f29035a334e50486292cmridge/* //block4: */
10514cf809678474282d6e9cf95e11c1690e9affb89erobbiew		tst_resm(TINFO, "Enter block 4");
10524cf809678474282d6e9cf95e11c1690e9affb89erobbiew		fail = 0;
10534cf809678474282d6e9cf95e11c1690e9affb89erobbiew		/*
10544cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 * Check that a lock on end of file is still valid when
10554cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 * additional data is appended to end of file and a new
10564cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 * process attempts to lock new data
10574cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 */
10584cf809678474282d6e9cf95e11c1690e9affb89erobbiew		fd = open(tmpname, O_CREAT | O_RDWR | O_TRUNC, 0777);
1059b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		if (fd < 0)
10604cf809678474282d6e9cf95e11c1690e9affb89erobbiew			tst_brkm(TBROK, cleanup, "open failed");
10614cf809678474282d6e9cf95e11c1690e9affb89erobbiew
1062b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		if (write(fd, FILEDATA, 10) < 0)
10634cf809678474282d6e9cf95e11c1690e9affb89erobbiew			tst_brkm(TBROK, cleanup, "write failed");
10644cf809678474282d6e9cf95e11c1690e9affb89erobbiew
10654cf809678474282d6e9cf95e11c1690e9affb89erobbiew		thiscase = &testcases[58];
10664cf809678474282d6e9cf95e11c1690e9affb89erobbiew		flock.l_type = thiscase->a_type;
10674cf809678474282d6e9cf95e11c1690e9affb89erobbiew		flock.l_whence = thiscase->a_whence;
10684cf809678474282d6e9cf95e11c1690e9affb89erobbiew		flock.l_start = thiscase->a_start;
10694cf809678474282d6e9cf95e11c1690e9affb89erobbiew		flock.l_len = thiscase->a_len;
10704cf809678474282d6e9cf95e11c1690e9affb89erobbiew
10714cf809678474282d6e9cf95e11c1690e9affb89erobbiew		/* Set the initial parent lock on the file */
10724cf809678474282d6e9cf95e11c1690e9affb89erobbiew		if ((fcntl(fd, F_SETLK, &flock)) < 0) {
10734cf809678474282d6e9cf95e11c1690e9affb89erobbiew			tst_resm(TFAIL, "First parent lock failed");
10744cf809678474282d6e9cf95e11c1690e9affb89erobbiew			tst_resm(TFAIL, "Test case %d, errno = %d", 58, errno);
1075865695bbc89088b9526ea9045410e5afb70a985cplars			fail = 1;
1076865695bbc89088b9526ea9045410e5afb70a985cplars		}
1077865695bbc89088b9526ea9045410e5afb70a985cplars
10784cf809678474282d6e9cf95e11c1690e9affb89erobbiew		/* Write some additional data to end of file */
1079b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		if (write(fd, FILEDATA, 10) < 0)
10804cf809678474282d6e9cf95e11c1690e9affb89erobbiew			tst_brkm(TBROK, cleanup, "write failed");
10814cf809678474282d6e9cf95e11c1690e9affb89erobbiew
10824cf809678474282d6e9cf95e11c1690e9affb89erobbiew		/* Mask signal to avoid race */
1083b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		if (sighold(SIGUSR1) < 0)
10844cf809678474282d6e9cf95e11c1690e9affb89erobbiew			tst_brkm(TBROK, cleanup, "sighold failed");
10854cf809678474282d6e9cf95e11c1690e9affb89erobbiew
1086b370c3cc476bfe75f1999b04f432af308e5812f7subrata_modak		if ((child = FORK_OR_VFORK()) == 0) {
1087d34d581c6a320e356a6cda923c7aa399479e812crobbiew#ifdef UCLINUX
1088d34d581c6a320e356a6cda923c7aa399479e812crobbiew			if (self_exec(argv0, "nddddddddd", 2, thiscase->c_type,
1089d34d581c6a320e356a6cda923c7aa399479e812crobbiew				      thiscase->c_whence, thiscase->c_start,
1090d34d581c6a320e356a6cda923c7aa399479e812crobbiew				      thiscase->c_len, thiscase->c_flag,
1091d34d581c6a320e356a6cda923c7aa399479e812crobbiew				      thiscase->a_type, fd, test, parent) < 0) {
1092d34d581c6a320e356a6cda923c7aa399479e812crobbiew				tst_resm(TFAIL, "self_exec failed");
1093d34d581c6a320e356a6cda923c7aa399479e812crobbiew				cleanup();
1094354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			}
1095d34d581c6a320e356a6cda923c7aa399479e812crobbiew#else
10964cf809678474282d6e9cf95e11c1690e9affb89erobbiew			dochild();
1097d34d581c6a320e356a6cda923c7aa399479e812crobbiew#endif
10984cf809678474282d6e9cf95e11c1690e9affb89erobbiew		}
1099b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		if (child < 0)
1100dd9ddae284b4e49ab9185f3c6025674d27f7a284Han Pingtian			tst_brkm(TBROK|TERRNO, cleanup, "Fork failed");
11014cf809678474282d6e9cf95e11c1690e9affb89erobbiew
1102865695bbc89088b9526ea9045410e5afb70a985cplars		/*
11034cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 * Wait for a signal from the child then remove blocking lock.
11044cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 * Set a 60 sec alarm to break the pause just in case the
11054cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 * child doesn't terminate on its own accord
1106865695bbc89088b9526ea9045410e5afb70a985cplars		 */
11074cf809678474282d6e9cf95e11c1690e9affb89erobbiew		(void)alarm(TIME_OUT);
11084cf809678474282d6e9cf95e11c1690e9affb89erobbiew
11094cf809678474282d6e9cf95e11c1690e9affb89erobbiew		(void)sigpause(SIGUSR1);
11104cf809678474282d6e9cf95e11c1690e9affb89erobbiew
11114cf809678474282d6e9cf95e11c1690e9affb89erobbiew		/* turn off the alarm timer */
11124cf809678474282d6e9cf95e11c1690e9affb89erobbiew		(void)alarm((unsigned)0);
11134cf809678474282d6e9cf95e11c1690e9affb89erobbiew		if (got1 != 1) {
11144cf809678474282d6e9cf95e11c1690e9affb89erobbiew			tst_resm(TINFO, "Pause terminated without signal "
11154cf809678474282d6e9cf95e11c1690e9affb89erobbiew				 "SIGUSR1 from child");
1116865695bbc89088b9526ea9045410e5afb70a985cplars		}
1117b2be8191028f0d2461c2e0f88f77fd36e2b72484Cyril Hrubis		got1 = 0;
11184cf809678474282d6e9cf95e11c1690e9affb89erobbiew
11194cf809678474282d6e9cf95e11c1690e9affb89erobbiew		/*
11204cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 * Set up lock structure for parent to delete
11214cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 * blocking lock then wait for child to exit
11224cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 */
11234cf809678474282d6e9cf95e11c1690e9affb89erobbiew		flock.l_type = F_UNLCK;
11244cf809678474282d6e9cf95e11c1690e9affb89erobbiew		flock.l_whence = 0;
11254cf809678474282d6e9cf95e11c1690e9affb89erobbiew		flock.l_start = 0L;
11264cf809678474282d6e9cf95e11c1690e9affb89erobbiew		flock.l_len = 0L;
11274cf809678474282d6e9cf95e11c1690e9affb89erobbiew		if ((fcntl(fd, F_SETLK, &flock)) < 0) {
11284cf809678474282d6e9cf95e11c1690e9affb89erobbiew			tst_resm(TFAIL, "Attempt to release parent lock "
11294cf809678474282d6e9cf95e11c1690e9affb89erobbiew				 "failed");
1130865695bbc89088b9526ea9045410e5afb70a985cplars			tst_resm(TFAIL, "Test case %d, errno = %d", test + 1,
1131865695bbc89088b9526ea9045410e5afb70a985cplars				 errno);
1132865695bbc89088b9526ea9045410e5afb70a985cplars			fail = 1;
1133865695bbc89088b9526ea9045410e5afb70a985cplars		}
1134865695bbc89088b9526ea9045410e5afb70a985cplars
11354cf809678474282d6e9cf95e11c1690e9affb89erobbiew		/*
11364cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 * set a 60 sec alarm to break the wait just in case the
11374cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 * child doesn't terminate on its own accord
11384cf809678474282d6e9cf95e11c1690e9affb89erobbiew		 */
11394cf809678474282d6e9cf95e11c1690e9affb89erobbiew		(void)alarm(TIME_OUT);
1140865695bbc89088b9526ea9045410e5afb70a985cplars
11414cf809678474282d6e9cf95e11c1690e9affb89erobbiew		waitpid(child, &status, 0);
11424cf809678474282d6e9cf95e11c1690e9affb89erobbiew		if (WEXITSTATUS(status) != 0) {
11434cf809678474282d6e9cf95e11c1690e9affb89erobbiew			fail = 1;
11444cf809678474282d6e9cf95e11c1690e9affb89erobbiew			tst_resm(TFAIL, "child returned bad exit status");
11454cf809678474282d6e9cf95e11c1690e9affb89erobbiew		}
1146865695bbc89088b9526ea9045410e5afb70a985cplars
11474cf809678474282d6e9cf95e11c1690e9affb89erobbiew		/* turn off the alarm clock */
11484cf809678474282d6e9cf95e11c1690e9affb89erobbiew		(void)alarm((unsigned)0);
11494cf809678474282d6e9cf95e11c1690e9affb89erobbiew		if (status != 0) {
11504cf809678474282d6e9cf95e11c1690e9affb89erobbiew			tst_resm(TFAIL, "child returned status 0x%x", status);
11514cf809678474282d6e9cf95e11c1690e9affb89erobbiew			fail = 1;
11524cf809678474282d6e9cf95e11c1690e9affb89erobbiew		}
11534cf809678474282d6e9cf95e11c1690e9affb89erobbiew		close(fd);
11544cf809678474282d6e9cf95e11c1690e9affb89erobbiew		unlink(tmpname);
1155865695bbc89088b9526ea9045410e5afb70a985cplars
115682180501c61d78b9c1a5d6ca5241047985eed8faXiong Zhou		testcheck_end(fail, "Test of locks on file");
11574cf809678474282d6e9cf95e11c1690e9affb89erobbiew		tst_resm(TINFO, "Exit block 4");
11584cf809678474282d6e9cf95e11c1690e9affb89erobbiew	}
11594cf809678474282d6e9cf95e11c1690e9affb89erobbiew	cleanup();
11602c28215423293e443469a07ae7011135d058b671Garrett Cooper	tst_exit();
116164bcf4a03c48557fc26887f501d44343dbc4df3fGarrett Cooper}
1162