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 * fcntl21.c
23*49cdfc7eSAndroid Build Coastguard Worker *
24*49cdfc7eSAndroid Build Coastguard Worker * DESCRIPTION
25*49cdfc7eSAndroid Build Coastguard Worker * Check locking of regions of a file
26*49cdfc7eSAndroid Build Coastguard Worker *
27*49cdfc7eSAndroid Build Coastguard Worker * ALGORITHM
28*49cdfc7eSAndroid Build Coastguard Worker * Test changing lock sections around a read lock
29*49cdfc7eSAndroid Build Coastguard Worker *
30*49cdfc7eSAndroid Build Coastguard Worker * USAGE
31*49cdfc7eSAndroid Build Coastguard Worker * fcntl21
32*49cdfc7eSAndroid Build Coastguard Worker *
33*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
34*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
35*49cdfc7eSAndroid Build Coastguard Worker *
36*49cdfc7eSAndroid Build Coastguard Worker * RESTRICTIONS
37*49cdfc7eSAndroid Build Coastguard Worker * None
38*49cdfc7eSAndroid Build Coastguard Worker */
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <inttypes.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker #define STRINGSIZE 27
50*49cdfc7eSAndroid Build Coastguard Worker #define STRING "abcdefghijklmnopqrstuvwxyz\n"
51*49cdfc7eSAndroid Build Coastguard Worker #define STOP 0xFFF0
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker int parent_pipe[2];
54*49cdfc7eSAndroid Build Coastguard Worker int child_pipe[2];
55*49cdfc7eSAndroid Build Coastguard Worker int fd;
56*49cdfc7eSAndroid Build Coastguard Worker pid_t parent_pid, child_pid;
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker void parent_put();
59*49cdfc7eSAndroid Build Coastguard Worker void parent_get();
60*49cdfc7eSAndroid Build Coastguard Worker void child_put();
61*49cdfc7eSAndroid Build Coastguard Worker void child_get();
62*49cdfc7eSAndroid Build Coastguard Worker void stop_child();
63*49cdfc7eSAndroid Build Coastguard Worker void compare_lock(struct flock *, short, short, int, int, pid_t);
64*49cdfc7eSAndroid Build Coastguard Worker void unlock_file();
65*49cdfc7eSAndroid Build Coastguard Worker void do_test(struct flock *, short, short, int, int);
66*49cdfc7eSAndroid Build Coastguard Worker void catch_child();
67*49cdfc7eSAndroid Build Coastguard Worker char *str_type();
68*49cdfc7eSAndroid Build Coastguard Worker int do_lock(int, short, short, int, int);
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "fcntl21";
71*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker void setup(void);
74*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void);
75*49cdfc7eSAndroid Build Coastguard Worker int fail;
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker /*
78*49cdfc7eSAndroid Build Coastguard Worker * setup
79*49cdfc7eSAndroid Build Coastguard Worker * performs all ONE TIME setup for this test
80*49cdfc7eSAndroid Build Coastguard Worker */
setup(void)81*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
82*49cdfc7eSAndroid Build Coastguard Worker {
83*49cdfc7eSAndroid Build Coastguard Worker char *buf = STRING;
84*49cdfc7eSAndroid Build Coastguard Worker char template[PATH_MAX];
85*49cdfc7eSAndroid Build Coastguard Worker struct sigaction act;
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker tst_sig(FORK, DEF_HANDLER, cleanup);
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker umask(0);
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
92*49cdfc7eSAndroid Build Coastguard Worker
93*49cdfc7eSAndroid Build Coastguard Worker pipe(parent_pipe);
94*49cdfc7eSAndroid Build Coastguard Worker pipe(child_pipe);
95*49cdfc7eSAndroid Build Coastguard Worker parent_pid = getpid();
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Worker tst_tmpdir();
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker snprintf(template, PATH_MAX, "fcntl21XXXXXX");
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker if ((fd = mkstemp(template)) < 0) {
102*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "Couldn't open temp file! errno = %d", errno);
103*49cdfc7eSAndroid Build Coastguard Worker }
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker if (write(fd, buf, STRINGSIZE) < 0) {
106*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "Couldn't write to temp file! errno = %d",
107*49cdfc7eSAndroid Build Coastguard Worker errno);
108*49cdfc7eSAndroid Build Coastguard Worker }
109*49cdfc7eSAndroid Build Coastguard Worker
110*49cdfc7eSAndroid Build Coastguard Worker memset(&act, 0, sizeof(act));
111*49cdfc7eSAndroid Build Coastguard Worker act.sa_handler = catch_child;
112*49cdfc7eSAndroid Build Coastguard Worker sigemptyset(&act.sa_mask);
113*49cdfc7eSAndroid Build Coastguard Worker sigaddset(&act.sa_mask, SIGCHLD);
114*49cdfc7eSAndroid Build Coastguard Worker if ((sigaction(SIGCHLD, &act, NULL)) < 0) {
115*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "SIGCHLD signal setup failed, errno: %d", errno);
116*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
117*49cdfc7eSAndroid Build Coastguard Worker }
118*49cdfc7eSAndroid Build Coastguard Worker }
119*49cdfc7eSAndroid Build Coastguard Worker
120*49cdfc7eSAndroid Build Coastguard Worker /*
121*49cdfc7eSAndroid Build Coastguard Worker * cleanup()
122*49cdfc7eSAndroid Build Coastguard Worker * performs all ONE TIME cleanup for this test at completion or
123*49cdfc7eSAndroid Build Coastguard Worker * premature exit
124*49cdfc7eSAndroid Build Coastguard Worker */
cleanup(void)125*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
126*49cdfc7eSAndroid Build Coastguard Worker {
127*49cdfc7eSAndroid Build Coastguard Worker
128*49cdfc7eSAndroid Build Coastguard Worker tst_rmdir();
129*49cdfc7eSAndroid Build Coastguard Worker
130*49cdfc7eSAndroid Build Coastguard Worker }
131*49cdfc7eSAndroid Build Coastguard Worker
do_child(void)132*49cdfc7eSAndroid Build Coastguard Worker void do_child(void)
133*49cdfc7eSAndroid Build Coastguard Worker {
134*49cdfc7eSAndroid Build Coastguard Worker struct flock fl;
135*49cdfc7eSAndroid Build Coastguard Worker
136*49cdfc7eSAndroid Build Coastguard Worker close(parent_pipe[1]);
137*49cdfc7eSAndroid Build Coastguard Worker close(child_pipe[0]);
138*49cdfc7eSAndroid Build Coastguard Worker while (1) {
139*49cdfc7eSAndroid Build Coastguard Worker child_get(&fl);
140*49cdfc7eSAndroid Build Coastguard Worker if (fcntl(fd, F_GETLK, &fl) < 0) {
141*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
142*49cdfc7eSAndroid Build Coastguard Worker errno);
143*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
144*49cdfc7eSAndroid Build Coastguard Worker }
145*49cdfc7eSAndroid Build Coastguard Worker child_put(&fl);
146*49cdfc7eSAndroid Build Coastguard Worker }
147*49cdfc7eSAndroid Build Coastguard Worker }
148*49cdfc7eSAndroid Build Coastguard Worker
do_lock(int cmd,short type,short whence,int start,int len)149*49cdfc7eSAndroid Build Coastguard Worker int do_lock(int cmd, short type, short whence, int start, int len)
150*49cdfc7eSAndroid Build Coastguard Worker {
151*49cdfc7eSAndroid Build Coastguard Worker struct flock fl;
152*49cdfc7eSAndroid Build Coastguard Worker
153*49cdfc7eSAndroid Build Coastguard Worker fl.l_type = type;
154*49cdfc7eSAndroid Build Coastguard Worker fl.l_whence = whence;
155*49cdfc7eSAndroid Build Coastguard Worker fl.l_start = start;
156*49cdfc7eSAndroid Build Coastguard Worker fl.l_len = len;
157*49cdfc7eSAndroid Build Coastguard Worker return (fcntl(fd, cmd, &fl));
158*49cdfc7eSAndroid Build Coastguard Worker }
159*49cdfc7eSAndroid Build Coastguard Worker
do_test(struct flock * fl,short type,short whence,int start,int len)160*49cdfc7eSAndroid Build Coastguard Worker void do_test(struct flock *fl, short type, short whence, int start, int len)
161*49cdfc7eSAndroid Build Coastguard Worker {
162*49cdfc7eSAndroid Build Coastguard Worker fl->l_type = type;
163*49cdfc7eSAndroid Build Coastguard Worker fl->l_whence = whence;
164*49cdfc7eSAndroid Build Coastguard Worker fl->l_start = start;
165*49cdfc7eSAndroid Build Coastguard Worker fl->l_len = len;
166*49cdfc7eSAndroid Build Coastguard Worker fl->l_pid = (short)0;
167*49cdfc7eSAndroid Build Coastguard Worker
168*49cdfc7eSAndroid Build Coastguard Worker parent_put(fl);
169*49cdfc7eSAndroid Build Coastguard Worker parent_get(fl);
170*49cdfc7eSAndroid Build Coastguard Worker }
171*49cdfc7eSAndroid Build Coastguard Worker
172*49cdfc7eSAndroid Build Coastguard Worker void
compare_lock(struct flock * fl,short type,short whence,int start,int len,pid_t pid)173*49cdfc7eSAndroid Build Coastguard Worker compare_lock(struct flock *fl, short type, short whence, int start, int len,
174*49cdfc7eSAndroid Build Coastguard Worker pid_t pid)
175*49cdfc7eSAndroid Build Coastguard Worker {
176*49cdfc7eSAndroid Build Coastguard Worker if (fl->l_type != type) {
177*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "lock type is wrong should be %s is %s",
178*49cdfc7eSAndroid Build Coastguard Worker str_type(type), str_type(fl->l_type));
179*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
180*49cdfc7eSAndroid Build Coastguard Worker }
181*49cdfc7eSAndroid Build Coastguard Worker
182*49cdfc7eSAndroid Build Coastguard Worker if (fl->l_whence != whence) {
183*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "lock whence is wrong should be %d is %d",
184*49cdfc7eSAndroid Build Coastguard Worker whence, fl->l_whence);
185*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
186*49cdfc7eSAndroid Build Coastguard Worker }
187*49cdfc7eSAndroid Build Coastguard Worker
188*49cdfc7eSAndroid Build Coastguard Worker if (fl->l_start != start) {
189*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "region starts in wrong place, should be"
190*49cdfc7eSAndroid Build Coastguard Worker "%d is %" PRId64, start, (int64_t) fl->l_start);
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_len != len) {
195*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL,
196*49cdfc7eSAndroid Build Coastguard Worker "region length is wrong, should be %d is %" PRId64,
197*49cdfc7eSAndroid Build Coastguard Worker len, (int64_t) fl->l_len);
198*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
199*49cdfc7eSAndroid Build Coastguard Worker }
200*49cdfc7eSAndroid Build Coastguard Worker
201*49cdfc7eSAndroid Build Coastguard Worker if (fl->l_pid != pid) {
202*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "locking pid is wrong, should be %d is %d",
203*49cdfc7eSAndroid Build Coastguard Worker pid, fl->l_pid);
204*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
205*49cdfc7eSAndroid Build Coastguard Worker }
206*49cdfc7eSAndroid Build Coastguard Worker }
207*49cdfc7eSAndroid Build Coastguard Worker
unlock_file(void)208*49cdfc7eSAndroid Build Coastguard Worker void unlock_file(void)
209*49cdfc7eSAndroid Build Coastguard Worker {
210*49cdfc7eSAndroid Build Coastguard Worker struct flock fl;
211*49cdfc7eSAndroid Build Coastguard Worker
212*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 0, 0) < 0) {
213*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d", errno);
214*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
215*49cdfc7eSAndroid Build Coastguard Worker }
216*49cdfc7eSAndroid Build Coastguard Worker do_test(&fl, F_WRLCK, 0, 0, 0);
217*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&fl, (short)F_UNLCK, (short)0, 0, 0, (pid_t) 0);
218*49cdfc7eSAndroid Build Coastguard Worker }
219*49cdfc7eSAndroid Build Coastguard Worker
str_type(int type)220*49cdfc7eSAndroid Build Coastguard Worker char *str_type(int type)
221*49cdfc7eSAndroid Build Coastguard Worker {
222*49cdfc7eSAndroid Build Coastguard Worker static char buf[20];
223*49cdfc7eSAndroid Build Coastguard Worker
224*49cdfc7eSAndroid Build Coastguard Worker switch (type) {
225*49cdfc7eSAndroid Build Coastguard Worker case F_RDLCK:
226*49cdfc7eSAndroid Build Coastguard Worker return ("F_RDLCK");
227*49cdfc7eSAndroid Build Coastguard Worker case F_WRLCK:
228*49cdfc7eSAndroid Build Coastguard Worker return ("F_WRLCK");
229*49cdfc7eSAndroid Build Coastguard Worker case F_UNLCK:
230*49cdfc7eSAndroid Build Coastguard Worker return ("F_UNLCK");
231*49cdfc7eSAndroid Build Coastguard Worker default:
232*49cdfc7eSAndroid Build Coastguard Worker sprintf(buf, "BAD VALUE: %d", type);
233*49cdfc7eSAndroid Build Coastguard Worker return (buf);
234*49cdfc7eSAndroid Build Coastguard Worker }
235*49cdfc7eSAndroid Build Coastguard Worker }
236*49cdfc7eSAndroid Build Coastguard Worker
parent_put(struct flock * l)237*49cdfc7eSAndroid Build Coastguard Worker void parent_put(struct flock *l)
238*49cdfc7eSAndroid Build Coastguard Worker {
239*49cdfc7eSAndroid Build Coastguard Worker if (write(parent_pipe[1], l, sizeof(*l)) != sizeof(*l)) {
240*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "couldn't send message to child");
241*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
242*49cdfc7eSAndroid Build Coastguard Worker }
243*49cdfc7eSAndroid Build Coastguard Worker }
244*49cdfc7eSAndroid Build Coastguard Worker
parent_get(struct flock * l)245*49cdfc7eSAndroid Build Coastguard Worker void parent_get(struct flock *l)
246*49cdfc7eSAndroid Build Coastguard Worker {
247*49cdfc7eSAndroid Build Coastguard Worker if (read(child_pipe[0], l, sizeof(*l)) != sizeof(*l)) {
248*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "couldn't get message from child");
249*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
250*49cdfc7eSAndroid Build Coastguard Worker }
251*49cdfc7eSAndroid Build Coastguard Worker }
252*49cdfc7eSAndroid Build Coastguard Worker
child_put(struct flock * l)253*49cdfc7eSAndroid Build Coastguard Worker void child_put(struct flock *l)
254*49cdfc7eSAndroid Build Coastguard Worker {
255*49cdfc7eSAndroid Build Coastguard Worker if (write(child_pipe[1], l, sizeof(*l)) != sizeof(*l)) {
256*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "couldn't send message to parent");
257*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
258*49cdfc7eSAndroid Build Coastguard Worker }
259*49cdfc7eSAndroid Build Coastguard Worker }
260*49cdfc7eSAndroid Build Coastguard Worker
child_get(struct flock * l)261*49cdfc7eSAndroid Build Coastguard Worker void child_get(struct flock *l)
262*49cdfc7eSAndroid Build Coastguard Worker {
263*49cdfc7eSAndroid Build Coastguard Worker if (read(parent_pipe[0], l, sizeof(*l)) != sizeof(*l)) {
264*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "couldn't get message from parent");
265*49cdfc7eSAndroid Build Coastguard Worker cleanup();
266*49cdfc7eSAndroid Build Coastguard Worker } else if (l->l_type == (short)STOP) {
267*49cdfc7eSAndroid Build Coastguard Worker exit(0);
268*49cdfc7eSAndroid Build Coastguard Worker }
269*49cdfc7eSAndroid Build Coastguard Worker }
270*49cdfc7eSAndroid Build Coastguard Worker
stop_child(void)271*49cdfc7eSAndroid Build Coastguard Worker void stop_child(void)
272*49cdfc7eSAndroid Build Coastguard Worker {
273*49cdfc7eSAndroid Build Coastguard Worker struct flock fl;
274*49cdfc7eSAndroid Build Coastguard Worker
275*49cdfc7eSAndroid Build Coastguard Worker signal(SIGCHLD, SIG_DFL);
276*49cdfc7eSAndroid Build Coastguard Worker fl.l_type = STOP;
277*49cdfc7eSAndroid Build Coastguard Worker parent_put(&fl);
278*49cdfc7eSAndroid Build Coastguard Worker wait(0);
279*49cdfc7eSAndroid Build Coastguard Worker }
280*49cdfc7eSAndroid Build Coastguard Worker
catch_child(void)281*49cdfc7eSAndroid Build Coastguard Worker void catch_child(void)
282*49cdfc7eSAndroid Build Coastguard Worker {
283*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "Unexpected death of child process");
284*49cdfc7eSAndroid Build Coastguard Worker cleanup();
285*49cdfc7eSAndroid Build Coastguard Worker }
286*49cdfc7eSAndroid Build Coastguard Worker
main(int ac,char ** av)287*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
288*49cdfc7eSAndroid Build Coastguard Worker {
289*49cdfc7eSAndroid Build Coastguard Worker struct flock tl;
290*49cdfc7eSAndroid Build Coastguard Worker
291*49cdfc7eSAndroid Build Coastguard Worker int lc;
292*49cdfc7eSAndroid Build Coastguard Worker
293*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(ac, av, NULL, NULL);
294*49cdfc7eSAndroid Build Coastguard Worker setup(); /* global setup */
295*49cdfc7eSAndroid Build Coastguard Worker
296*49cdfc7eSAndroid Build Coastguard Worker /* Check for looping state if -i option is given */
297*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); lc++) {
298*49cdfc7eSAndroid Build Coastguard Worker /* reset tst_count in case we are looping */
299*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
300*49cdfc7eSAndroid Build Coastguard Worker
301*49cdfc7eSAndroid Build Coastguard Worker if ((child_pid = tst_fork()) == 0)
302*49cdfc7eSAndroid Build Coastguard Worker do_child();
303*49cdfc7eSAndroid Build Coastguard Worker
304*49cdfc7eSAndroid Build Coastguard Worker if (child_pid < 0) {
305*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "Fork failed");
306*49cdfc7eSAndroid Build Coastguard Worker cleanup();
307*49cdfc7eSAndroid Build Coastguard Worker }
308*49cdfc7eSAndroid Build Coastguard Worker
309*49cdfc7eSAndroid Build Coastguard Worker (void)close(parent_pipe[0]);
310*49cdfc7eSAndroid Build Coastguard Worker (void)close(child_pipe[1]);
311*49cdfc7eSAndroid Build Coastguard Worker
312*49cdfc7eSAndroid Build Coastguard Worker /* //block1: */
313*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Enter block 1");
314*49cdfc7eSAndroid Build Coastguard Worker fail = 0;
315*49cdfc7eSAndroid Build Coastguard Worker /*
316*49cdfc7eSAndroid Build Coastguard Worker * Set a read lock on the whole file
317*49cdfc7eSAndroid Build Coastguard Worker */
318*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 0, 0) < 0) {
319*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
320*49cdfc7eSAndroid Build Coastguard Worker errno);
321*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
322*49cdfc7eSAndroid Build Coastguard Worker }
323*49cdfc7eSAndroid Build Coastguard Worker
324*49cdfc7eSAndroid Build Coastguard Worker /*
325*49cdfc7eSAndroid Build Coastguard Worker * Test to make sure it's there.
326*49cdfc7eSAndroid Build Coastguard Worker */
327*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
328*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_RDLCK, (short)0, 0, 0, parent_pid);
329*49cdfc7eSAndroid Build Coastguard Worker
330*49cdfc7eSAndroid Build Coastguard Worker /*
331*49cdfc7eSAndroid Build Coastguard Worker * remove the lock set above
332*49cdfc7eSAndroid Build Coastguard Worker */
333*49cdfc7eSAndroid Build Coastguard Worker unlock_file();
334*49cdfc7eSAndroid Build Coastguard Worker
335*49cdfc7eSAndroid Build Coastguard Worker if (fail) {
336*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 1: FAILED");
337*49cdfc7eSAndroid Build Coastguard Worker } else {
338*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 1: PASSED");
339*49cdfc7eSAndroid Build Coastguard Worker }
340*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Exit block 1");
341*49cdfc7eSAndroid Build Coastguard Worker
342*49cdfc7eSAndroid Build Coastguard Worker /* //block2: */
343*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Enter block 2");
344*49cdfc7eSAndroid Build Coastguard Worker fail = 0;
345*49cdfc7eSAndroid Build Coastguard Worker
346*49cdfc7eSAndroid Build Coastguard Worker /*
347*49cdfc7eSAndroid Build Coastguard Worker * Set a write lock on the whole file
348*49cdfc7eSAndroid Build Coastguard Worker */
349*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 0, 0) < 0) {
350*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
351*49cdfc7eSAndroid Build Coastguard Worker errno);
352*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
353*49cdfc7eSAndroid Build Coastguard Worker }
354*49cdfc7eSAndroid Build Coastguard Worker
355*49cdfc7eSAndroid Build Coastguard Worker /*
356*49cdfc7eSAndroid Build Coastguard Worker * Test to make sure its there
357*49cdfc7eSAndroid Build Coastguard Worker */
358*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
359*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_WRLCK, (short)0, 0, 0, parent_pid);
360*49cdfc7eSAndroid Build Coastguard Worker
361*49cdfc7eSAndroid Build Coastguard Worker /*
362*49cdfc7eSAndroid Build Coastguard Worker * remove the lock set above
363*49cdfc7eSAndroid Build Coastguard Worker */
364*49cdfc7eSAndroid Build Coastguard Worker unlock_file();
365*49cdfc7eSAndroid Build Coastguard Worker
366*49cdfc7eSAndroid Build Coastguard Worker if (fail) {
367*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 2: FAILED");
368*49cdfc7eSAndroid Build Coastguard Worker } else {
369*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 2: PASSED");
370*49cdfc7eSAndroid Build Coastguard Worker }
371*49cdfc7eSAndroid Build Coastguard Worker
372*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Exit block 2");
373*49cdfc7eSAndroid Build Coastguard Worker
374*49cdfc7eSAndroid Build Coastguard Worker /* //block3: */
375*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Enter block 3");
376*49cdfc7eSAndroid Build Coastguard Worker fail = 0;
377*49cdfc7eSAndroid Build Coastguard Worker
378*49cdfc7eSAndroid Build Coastguard Worker /*
379*49cdfc7eSAndroid Build Coastguard Worker * Add a read lock to the middle of the file and a write
380*49cdfc7eSAndroid Build Coastguard Worker * at the begining
381*49cdfc7eSAndroid Build Coastguard Worker */
382*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
383*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
384*49cdfc7eSAndroid Build Coastguard Worker errno);
385*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
386*49cdfc7eSAndroid Build Coastguard Worker }
387*49cdfc7eSAndroid Build Coastguard Worker
388*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 1, 5) < 0) {
389*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
390*49cdfc7eSAndroid Build Coastguard Worker errno);
391*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
392*49cdfc7eSAndroid Build Coastguard Worker }
393*49cdfc7eSAndroid Build Coastguard Worker
394*49cdfc7eSAndroid Build Coastguard Worker /*
395*49cdfc7eSAndroid Build Coastguard Worker * Test write lock
396*49cdfc7eSAndroid Build Coastguard Worker */
397*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, F_WRLCK, 0, 0, 0);
398*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_WRLCK, (short)0, 1, 5, parent_pid);
399*49cdfc7eSAndroid Build Coastguard Worker
400*49cdfc7eSAndroid Build Coastguard Worker /*
401*49cdfc7eSAndroid Build Coastguard Worker * Test read lock
402*49cdfc7eSAndroid Build Coastguard Worker */
403*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, F_WRLCK, 0, 6, 0);
404*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
405*49cdfc7eSAndroid Build Coastguard Worker
406*49cdfc7eSAndroid Build Coastguard Worker /*
407*49cdfc7eSAndroid Build Coastguard Worker * Test that the rest of the file is unlocked
408*49cdfc7eSAndroid Build Coastguard Worker */
409*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, F_WRLCK, 0, 15, 0);
410*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
411*49cdfc7eSAndroid Build Coastguard Worker
412*49cdfc7eSAndroid Build Coastguard Worker /*
413*49cdfc7eSAndroid Build Coastguard Worker * remove all the locks set above
414*49cdfc7eSAndroid Build Coastguard Worker */
415*49cdfc7eSAndroid Build Coastguard Worker unlock_file();
416*49cdfc7eSAndroid Build Coastguard Worker
417*49cdfc7eSAndroid Build Coastguard Worker if (fail) {
418*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 3: FAILED");
419*49cdfc7eSAndroid Build Coastguard Worker } else {
420*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 3 : PASSED");
421*49cdfc7eSAndroid Build Coastguard Worker }
422*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Exit block 3");
423*49cdfc7eSAndroid Build Coastguard Worker
424*49cdfc7eSAndroid Build Coastguard Worker /* //block4: */
425*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Enter block 4");
426*49cdfc7eSAndroid Build Coastguard Worker fail = 0;
427*49cdfc7eSAndroid Build Coastguard Worker
428*49cdfc7eSAndroid Build Coastguard Worker /*
429*49cdfc7eSAndroid Build Coastguard Worker * Set a read lock at the middle of the file and a
430*49cdfc7eSAndroid Build Coastguard Worker * write lock just before
431*49cdfc7eSAndroid Build Coastguard Worker */
432*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
433*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
434*49cdfc7eSAndroid Build Coastguard Worker errno);
435*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
436*49cdfc7eSAndroid Build Coastguard Worker }
437*49cdfc7eSAndroid Build Coastguard Worker
438*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 5, 5) < 0) {
439*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
440*49cdfc7eSAndroid Build Coastguard Worker errno);
441*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
442*49cdfc7eSAndroid Build Coastguard Worker }
443*49cdfc7eSAndroid Build Coastguard Worker
444*49cdfc7eSAndroid Build Coastguard Worker /*
445*49cdfc7eSAndroid Build Coastguard Worker * Test the write lock
446*49cdfc7eSAndroid Build Coastguard Worker */
447*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
448*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_WRLCK, (short)0, 5, 5, parent_pid);
449*49cdfc7eSAndroid Build Coastguard Worker
450*49cdfc7eSAndroid Build Coastguard Worker /*
451*49cdfc7eSAndroid Build Coastguard Worker * Test the read lock.
452*49cdfc7eSAndroid Build Coastguard Worker */
453*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 10, 0);
454*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
455*49cdfc7eSAndroid Build Coastguard Worker
456*49cdfc7eSAndroid Build Coastguard Worker /*
457*49cdfc7eSAndroid Build Coastguard Worker * Test to make sure the rest of the file is unlocked.
458*49cdfc7eSAndroid Build Coastguard Worker */
459*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
460*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
461*49cdfc7eSAndroid Build Coastguard Worker
462*49cdfc7eSAndroid Build Coastguard Worker /*
463*49cdfc7eSAndroid Build Coastguard Worker * remove all the locks set above
464*49cdfc7eSAndroid Build Coastguard Worker */
465*49cdfc7eSAndroid Build Coastguard Worker unlock_file();
466*49cdfc7eSAndroid Build Coastguard Worker
467*49cdfc7eSAndroid Build Coastguard Worker if (fail) {
468*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 4: FAILED");
469*49cdfc7eSAndroid Build Coastguard Worker } else {
470*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 4: PASSED");
471*49cdfc7eSAndroid Build Coastguard Worker }
472*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Exit block 4");
473*49cdfc7eSAndroid Build Coastguard Worker
474*49cdfc7eSAndroid Build Coastguard Worker /* //block5: */
475*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Enter block 5");
476*49cdfc7eSAndroid Build Coastguard Worker fail = 0;
477*49cdfc7eSAndroid Build Coastguard Worker
478*49cdfc7eSAndroid Build Coastguard Worker /*
479*49cdfc7eSAndroid Build Coastguard Worker * Set a read lock in the middle and a write lock that
480*49cdfc7eSAndroid Build Coastguard Worker * ends at the first byte of the read lock
481*49cdfc7eSAndroid Build Coastguard Worker */
482*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
483*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
484*49cdfc7eSAndroid Build Coastguard Worker errno);
485*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
486*49cdfc7eSAndroid Build Coastguard Worker }
487*49cdfc7eSAndroid Build Coastguard Worker
488*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 5, 6) < 0) {
489*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
490*49cdfc7eSAndroid Build Coastguard Worker errno);
491*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
492*49cdfc7eSAndroid Build Coastguard Worker }
493*49cdfc7eSAndroid Build Coastguard Worker
494*49cdfc7eSAndroid Build Coastguard Worker /*
495*49cdfc7eSAndroid Build Coastguard Worker * Test write lock
496*49cdfc7eSAndroid Build Coastguard Worker */
497*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
498*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_WRLCK, (short)0, 5, 6, parent_pid);
499*49cdfc7eSAndroid Build Coastguard Worker
500*49cdfc7eSAndroid Build Coastguard Worker /*
501*49cdfc7eSAndroid Build Coastguard Worker * Test read lock
502*49cdfc7eSAndroid Build Coastguard Worker */
503*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 11, 0);
504*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_RDLCK, (short)0, 11, 4, parent_pid);
505*49cdfc7eSAndroid Build Coastguard Worker
506*49cdfc7eSAndroid Build Coastguard Worker /*
507*49cdfc7eSAndroid Build Coastguard Worker * Test to make sure the rest of the file is unlocked.
508*49cdfc7eSAndroid Build Coastguard Worker */
509*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
510*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0);
511*49cdfc7eSAndroid Build Coastguard Worker
512*49cdfc7eSAndroid Build Coastguard Worker /*
513*49cdfc7eSAndroid Build Coastguard Worker * remove all the locks set above
514*49cdfc7eSAndroid Build Coastguard Worker */
515*49cdfc7eSAndroid Build Coastguard Worker unlock_file();
516*49cdfc7eSAndroid Build Coastguard Worker
517*49cdfc7eSAndroid Build Coastguard Worker if (fail) {
518*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 5: FAILED");
519*49cdfc7eSAndroid Build Coastguard Worker } else {
520*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 5: PASSED");
521*49cdfc7eSAndroid Build Coastguard Worker }
522*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Exit block 5");
523*49cdfc7eSAndroid Build Coastguard Worker
524*49cdfc7eSAndroid Build Coastguard Worker /* //block6: */
525*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Enter block 6");
526*49cdfc7eSAndroid Build Coastguard Worker fail = 0;
527*49cdfc7eSAndroid Build Coastguard Worker
528*49cdfc7eSAndroid Build Coastguard Worker /*
529*49cdfc7eSAndroid Build Coastguard Worker * Set a read lock on the middle of the file and a write
530*49cdfc7eSAndroid Build Coastguard Worker * lock that overlaps the front of the read.
531*49cdfc7eSAndroid Build Coastguard Worker */
532*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
533*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
534*49cdfc7eSAndroid Build Coastguard Worker errno);
535*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
536*49cdfc7eSAndroid Build Coastguard Worker }
537*49cdfc7eSAndroid Build Coastguard Worker
538*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 5, 8) < 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 /*
545*49cdfc7eSAndroid Build Coastguard Worker * Test the write lock
546*49cdfc7eSAndroid Build Coastguard Worker */
547*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 5, 0);
548*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_WRLCK, (short)0, 5, 8, parent_pid);
549*49cdfc7eSAndroid Build Coastguard Worker
550*49cdfc7eSAndroid Build Coastguard Worker /*
551*49cdfc7eSAndroid Build Coastguard Worker * Test the read lock
552*49cdfc7eSAndroid Build Coastguard Worker */
553*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
554*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_RDLCK, (short)0, 13, 2, parent_pid);
555*49cdfc7eSAndroid Build Coastguard Worker
556*49cdfc7eSAndroid Build Coastguard Worker /*
557*49cdfc7eSAndroid Build Coastguard Worker * Test to make sure the rest of the file is unlocked.
558*49cdfc7eSAndroid Build Coastguard Worker */
559*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
560*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 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 read lock in the middle of a file and a write
580*49cdfc7eSAndroid Build Coastguard Worker * lock in the middle of it
581*49cdfc7eSAndroid Build Coastguard Worker */
582*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 10) < 0) {
583*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
584*49cdfc7eSAndroid Build Coastguard Worker errno);
585*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
586*49cdfc7eSAndroid Build Coastguard Worker }
587*49cdfc7eSAndroid Build Coastguard Worker
588*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 13, 5) < 0) {
589*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
590*49cdfc7eSAndroid Build Coastguard Worker errno);
591*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
592*49cdfc7eSAndroid Build Coastguard Worker }
593*49cdfc7eSAndroid Build Coastguard Worker
594*49cdfc7eSAndroid Build Coastguard Worker /*
595*49cdfc7eSAndroid Build Coastguard Worker * Test the first read lock
596*49cdfc7eSAndroid Build Coastguard Worker */
597*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
598*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 3, parent_pid);
599*49cdfc7eSAndroid Build Coastguard Worker
600*49cdfc7eSAndroid Build Coastguard Worker /*
601*49cdfc7eSAndroid Build Coastguard Worker * Test the write lock
602*49cdfc7eSAndroid Build Coastguard Worker */
603*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
604*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_WRLCK, (short)0, 13, 5, parent_pid);
605*49cdfc7eSAndroid Build Coastguard Worker
606*49cdfc7eSAndroid Build Coastguard Worker /*
607*49cdfc7eSAndroid Build Coastguard Worker * Test the second read lock
608*49cdfc7eSAndroid Build Coastguard Worker */
609*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 18, 0);
610*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_RDLCK, (short)0, 18, 2, parent_pid);
611*49cdfc7eSAndroid Build Coastguard Worker
612*49cdfc7eSAndroid Build Coastguard Worker /*
613*49cdfc7eSAndroid Build Coastguard Worker * Test to make sure the rest of the file is unlocked
614*49cdfc7eSAndroid Build Coastguard Worker */
615*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 20, 0);
616*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, 0);
617*49cdfc7eSAndroid Build Coastguard Worker
618*49cdfc7eSAndroid Build Coastguard Worker /*
619*49cdfc7eSAndroid Build Coastguard Worker * remove all the locks set above.
620*49cdfc7eSAndroid Build Coastguard Worker */
621*49cdfc7eSAndroid Build Coastguard Worker unlock_file();
622*49cdfc7eSAndroid Build Coastguard Worker if (fail) {
623*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 7: FAILED");
624*49cdfc7eSAndroid Build Coastguard Worker } else {
625*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 7: PASSED");
626*49cdfc7eSAndroid Build Coastguard Worker }
627*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Exit block 7");
628*49cdfc7eSAndroid Build Coastguard Worker
629*49cdfc7eSAndroid Build Coastguard Worker /* //block8: */
630*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Enter block 8");
631*49cdfc7eSAndroid Build Coastguard Worker fail = 0;
632*49cdfc7eSAndroid Build Coastguard Worker /*
633*49cdfc7eSAndroid Build Coastguard Worker * Set a read lock in the middle of the file and a write
634*49cdfc7eSAndroid Build Coastguard Worker * lock that overlaps the end
635*49cdfc7eSAndroid Build Coastguard Worker */
636*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
637*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
638*49cdfc7eSAndroid Build Coastguard Worker errno);
639*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
640*49cdfc7eSAndroid Build Coastguard Worker }
641*49cdfc7eSAndroid Build Coastguard Worker
642*49cdfc7eSAndroid Build Coastguard Worker /*
643*49cdfc7eSAndroid Build Coastguard Worker * Set a write lock on the whole file
644*49cdfc7eSAndroid Build Coastguard Worker */
645*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 13, 5) < 0) {
646*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
647*49cdfc7eSAndroid Build Coastguard Worker errno);
648*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
649*49cdfc7eSAndroid Build Coastguard Worker }
650*49cdfc7eSAndroid Build Coastguard Worker
651*49cdfc7eSAndroid Build Coastguard Worker /*
652*49cdfc7eSAndroid Build Coastguard Worker * Test the read lock
653*49cdfc7eSAndroid Build Coastguard Worker */
654*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
655*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 3, parent_pid);
656*49cdfc7eSAndroid Build Coastguard Worker
657*49cdfc7eSAndroid Build Coastguard Worker /*
658*49cdfc7eSAndroid Build Coastguard Worker * Test the write lock
659*49cdfc7eSAndroid Build Coastguard Worker */
660*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 13, 0);
661*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_WRLCK, (short)0, 13, 5, parent_pid);
662*49cdfc7eSAndroid Build Coastguard Worker
663*49cdfc7eSAndroid Build Coastguard Worker /*
664*49cdfc7eSAndroid Build Coastguard Worker * Test to make sure the rest of the file is unlocked
665*49cdfc7eSAndroid Build Coastguard Worker */
666*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 18, 0);
667*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_UNLCK, (short)0, 18, 0, 0);
668*49cdfc7eSAndroid Build Coastguard Worker
669*49cdfc7eSAndroid Build Coastguard Worker /*
670*49cdfc7eSAndroid Build Coastguard Worker * remove all the locks set above
671*49cdfc7eSAndroid Build Coastguard Worker */
672*49cdfc7eSAndroid Build Coastguard Worker unlock_file();
673*49cdfc7eSAndroid Build Coastguard Worker
674*49cdfc7eSAndroid Build Coastguard Worker if (fail) {
675*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 8: FAILED");
676*49cdfc7eSAndroid Build Coastguard Worker } else {
677*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 8: PASSED");
678*49cdfc7eSAndroid Build Coastguard Worker }
679*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Exit block 8");
680*49cdfc7eSAndroid Build Coastguard Worker
681*49cdfc7eSAndroid Build Coastguard Worker /* //block9: */
682*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Enter block 9");
683*49cdfc7eSAndroid Build Coastguard Worker fail = 0;
684*49cdfc7eSAndroid Build Coastguard Worker
685*49cdfc7eSAndroid Build Coastguard Worker /*
686*49cdfc7eSAndroid Build Coastguard Worker * Set a read lock in the middle of the file and a write
687*49cdfc7eSAndroid Build Coastguard Worker * lock starting at the last byte of the read lock
688*49cdfc7eSAndroid Build Coastguard Worker */
689*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
690*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
691*49cdfc7eSAndroid Build Coastguard Worker errno);
692*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
693*49cdfc7eSAndroid Build Coastguard Worker }
694*49cdfc7eSAndroid Build Coastguard Worker
695*49cdfc7eSAndroid Build Coastguard Worker /*
696*49cdfc7eSAndroid Build Coastguard Worker * Set a write lock on the whole file.
697*49cdfc7eSAndroid Build Coastguard Worker */
698*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 14, 5) < 0) {
699*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
700*49cdfc7eSAndroid Build Coastguard Worker errno);
701*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
702*49cdfc7eSAndroid Build Coastguard Worker }
703*49cdfc7eSAndroid Build Coastguard Worker
704*49cdfc7eSAndroid Build Coastguard Worker /*
705*49cdfc7eSAndroid Build Coastguard Worker * Test read lock
706*49cdfc7eSAndroid Build Coastguard Worker */
707*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
708*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 4, parent_pid);
709*49cdfc7eSAndroid Build Coastguard Worker
710*49cdfc7eSAndroid Build Coastguard Worker /*
711*49cdfc7eSAndroid Build Coastguard Worker * Test the write lock
712*49cdfc7eSAndroid Build Coastguard Worker */
713*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 14, 0);
714*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_WRLCK, (short)0, 14, 5, parent_pid);
715*49cdfc7eSAndroid Build Coastguard Worker
716*49cdfc7eSAndroid Build Coastguard Worker /*
717*49cdfc7eSAndroid Build Coastguard Worker * Test to make sure the end of the file is unlocked
718*49cdfc7eSAndroid Build Coastguard Worker */
719*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 19, 0);
720*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_UNLCK, (short)0, 19, 0, 0);
721*49cdfc7eSAndroid Build Coastguard Worker
722*49cdfc7eSAndroid Build Coastguard Worker /*
723*49cdfc7eSAndroid Build Coastguard Worker * remove all the locks set above
724*49cdfc7eSAndroid Build Coastguard Worker */
725*49cdfc7eSAndroid Build Coastguard Worker unlock_file();
726*49cdfc7eSAndroid Build Coastguard Worker
727*49cdfc7eSAndroid Build Coastguard Worker if (fail) {
728*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 9: FAILED");
729*49cdfc7eSAndroid Build Coastguard Worker } else {
730*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 9: PASSED");
731*49cdfc7eSAndroid Build Coastguard Worker }
732*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Exit block 9");
733*49cdfc7eSAndroid Build Coastguard Worker
734*49cdfc7eSAndroid Build Coastguard Worker /* //block10: */
735*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Enter block 10");
736*49cdfc7eSAndroid Build Coastguard Worker fail = 0;
737*49cdfc7eSAndroid Build Coastguard Worker
738*49cdfc7eSAndroid Build Coastguard Worker /*
739*49cdfc7eSAndroid Build Coastguard Worker * Set a read lock in the middle of the file and a write
740*49cdfc7eSAndroid Build Coastguard Worker * lock that starts just after the last byte of the
741*49cdfc7eSAndroid Build Coastguard Worker * read lock.
742*49cdfc7eSAndroid Build Coastguard Worker */
743*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
744*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
745*49cdfc7eSAndroid Build Coastguard Worker errno);
746*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
747*49cdfc7eSAndroid Build Coastguard Worker }
748*49cdfc7eSAndroid Build Coastguard Worker
749*49cdfc7eSAndroid Build Coastguard Worker /*
750*49cdfc7eSAndroid Build Coastguard Worker * Set a write lock on the whole file
751*49cdfc7eSAndroid Build Coastguard Worker */
752*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 15, 5) < 0) {
753*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
754*49cdfc7eSAndroid Build Coastguard Worker errno);
755*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
756*49cdfc7eSAndroid Build Coastguard Worker }
757*49cdfc7eSAndroid Build Coastguard Worker
758*49cdfc7eSAndroid Build Coastguard Worker /*
759*49cdfc7eSAndroid Build Coastguard Worker * Test the read lock
760*49cdfc7eSAndroid Build Coastguard Worker */
761*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
762*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
763*49cdfc7eSAndroid Build Coastguard Worker
764*49cdfc7eSAndroid Build Coastguard Worker /*
765*49cdfc7eSAndroid Build Coastguard Worker * Test the write lock
766*49cdfc7eSAndroid Build Coastguard Worker */
767*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 15, 0);
768*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_WRLCK, (short)0, 15, 5, parent_pid);
769*49cdfc7eSAndroid Build Coastguard Worker
770*49cdfc7eSAndroid Build Coastguard Worker /*
771*49cdfc7eSAndroid Build Coastguard Worker * Test to make sure the rest of the file is unlocked
772*49cdfc7eSAndroid Build Coastguard Worker */
773*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 20, 0);
774*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, 0);
775*49cdfc7eSAndroid Build Coastguard Worker
776*49cdfc7eSAndroid Build Coastguard Worker /*
777*49cdfc7eSAndroid Build Coastguard Worker * remove all the locks set above
778*49cdfc7eSAndroid Build Coastguard Worker */
779*49cdfc7eSAndroid Build Coastguard Worker unlock_file();
780*49cdfc7eSAndroid Build Coastguard Worker
781*49cdfc7eSAndroid Build Coastguard Worker if (fail) {
782*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 10: FAILED");
783*49cdfc7eSAndroid Build Coastguard Worker } else {
784*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 10: PASSED");
785*49cdfc7eSAndroid Build Coastguard Worker }
786*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Exit block 10");
787*49cdfc7eSAndroid Build Coastguard Worker
788*49cdfc7eSAndroid Build Coastguard Worker /* //block11: */
789*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Enter block 11");
790*49cdfc7eSAndroid Build Coastguard Worker fail = 0;
791*49cdfc7eSAndroid Build Coastguard Worker
792*49cdfc7eSAndroid Build Coastguard Worker /*
793*49cdfc7eSAndroid Build Coastguard Worker * Set a read lock at the middle of the file and a write
794*49cdfc7eSAndroid Build Coastguard Worker * lock that starts past the end of the read lock.
795*49cdfc7eSAndroid Build Coastguard Worker */
796*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) {
797*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
798*49cdfc7eSAndroid Build Coastguard Worker errno);
799*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
800*49cdfc7eSAndroid Build Coastguard Worker }
801*49cdfc7eSAndroid Build Coastguard Worker
802*49cdfc7eSAndroid Build Coastguard Worker if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 16, 5) < 0) {
803*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "fcntl on file failed, errno =%d",
804*49cdfc7eSAndroid Build Coastguard Worker errno);
805*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
806*49cdfc7eSAndroid Build Coastguard Worker }
807*49cdfc7eSAndroid Build Coastguard Worker
808*49cdfc7eSAndroid Build Coastguard Worker /*
809*49cdfc7eSAndroid Build Coastguard Worker * Test the read lock
810*49cdfc7eSAndroid Build Coastguard Worker */
811*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 0, 0);
812*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid);
813*49cdfc7eSAndroid Build Coastguard Worker
814*49cdfc7eSAndroid Build Coastguard Worker /*
815*49cdfc7eSAndroid Build Coastguard Worker * Test that byte in between is unlocked
816*49cdfc7eSAndroid Build Coastguard Worker */
817*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 15, 1);
818*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 1, 0);
819*49cdfc7eSAndroid Build Coastguard Worker
820*49cdfc7eSAndroid Build Coastguard Worker /*
821*49cdfc7eSAndroid Build Coastguard Worker * Test the write lock
822*49cdfc7eSAndroid Build Coastguard Worker */
823*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 16, 0);
824*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_WRLCK, (short)0, 16, 5, parent_pid);
825*49cdfc7eSAndroid Build Coastguard Worker
826*49cdfc7eSAndroid Build Coastguard Worker /*
827*49cdfc7eSAndroid Build Coastguard Worker * Test to make sure the rest of the file is unlocked
828*49cdfc7eSAndroid Build Coastguard Worker */
829*49cdfc7eSAndroid Build Coastguard Worker do_test(&tl, (short)F_WRLCK, (short)0, 21, 0);
830*49cdfc7eSAndroid Build Coastguard Worker compare_lock(&tl, (short)F_UNLCK, (short)0, 21, 0, 0);
831*49cdfc7eSAndroid Build Coastguard Worker
832*49cdfc7eSAndroid Build Coastguard Worker /*
833*49cdfc7eSAndroid Build Coastguard Worker * remove all the locks set above
834*49cdfc7eSAndroid Build Coastguard Worker */
835*49cdfc7eSAndroid Build Coastguard Worker unlock_file();
836*49cdfc7eSAndroid Build Coastguard Worker
837*49cdfc7eSAndroid Build Coastguard Worker if (fail) {
838*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 11: FAILED");
839*49cdfc7eSAndroid Build Coastguard Worker } else {
840*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Test block 11: PASSED");
841*49cdfc7eSAndroid Build Coastguard Worker }
842*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Exit block 11");
843*49cdfc7eSAndroid Build Coastguard Worker
844*49cdfc7eSAndroid Build Coastguard Worker stop_child();
845*49cdfc7eSAndroid Build Coastguard Worker close(fd);
846*49cdfc7eSAndroid Build Coastguard Worker }
847*49cdfc7eSAndroid Build Coastguard Worker cleanup();
848*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
849*49cdfc7eSAndroid Build Coastguard Worker }
850