blob: 90b34eab0a34808de477d178034a2b10bf0783c4 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
subrata_modak56207ce2009-03-23 13:35:39 +000022 * fcntl14.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
subrata_modak56207ce2009-03-23 13:35:39 +000025 * File locking test cases for fcntl. In Linux, S_ENFMT is not implemented
plars865695b2001-08-27 22:15:12 +000026 * in the kernel. However all standard Unix kernels define S_ENFMT as
27 * S_ISGID. So this test defines S_ENFMT as S_ISGID.
28 *
29 * ALGORITHM
subrata_modak56207ce2009-03-23 13:35:39 +000030 * Various test cases are used to lock a file opened without mandatory
31 * locking, with mandatory locking and mandatory locking with NOBLOCK
plars865695b2001-08-27 22:15:12 +000032 *
33 * USAGE
nstrazfa31d552002-05-14 16:50:06 +000034 * fcntl14
plars865695b2001-08-27 22:15:12 +000035 *
36 * HISTORY
37 * 07/2001 Ported by Wayne Boyer
38 *
39 * RESTRICTIONS
subrata_modak56207ce2009-03-23 13:35:39 +000040 * None
plars865695b2001-08-27 22:15:12 +000041 */
robbiew4cf80962003-03-25 21:53:41 +000042#define _GNU_SOURCE 1
plars865695b2001-08-27 22:15:12 +000043#include <fcntl.h>
44#include <sys/types.h>
45#include <sys/stat.h>
46#include <signal.h>
47#include <errno.h>
mridgedb639212005-01-04 21:04:11 +000048#include <sys/wait.h>
subrata_modak923b23f2009-11-02 13:57:16 +000049#include <inttypes.h>
Garrett Cooper0a643cb2010-12-21 11:21:19 -080050#include "test.h"
Cyril Hrubis0f0e3482014-02-27 16:08:04 +010051#include "tst_fs_type.h"
plars865695b2001-08-27 22:15:12 +000052
mridgedb639212005-01-04 21:04:11 +000053#define SKIP 0x0c00
54#if SKIP == F_RDLCK || SKIP== F_WRLCK
55#error invalid value for SKIP, must be distinct from F_RDLCK and F_WRLCK
56#endif
57#ifndef S_ENFMT
plars865695b2001-08-27 22:15:12 +000058#define S_ENFMT S_ISGID
mridgedb639212005-01-04 21:04:11 +000059#endif
plars865695b2001-08-27 22:15:12 +000060
61/* NOBLOCK - immediate success */
62#define NOBLOCK 2
63
64/* WILLBLOCK - blocks, then succeeds (parent must unlock records) */
65#define WILLBLOCK 3
66
67#define TIME_OUT 60
68
69typedef struct {
70 short a_type;
71 short a_whence;
72 long a_start;
73 long a_len;
subrata_modak56207ce2009-03-23 13:35:39 +000074 short b_type; /* SKIP means suppress fcntl call */
plars865695b2001-08-27 22:15:12 +000075 short b_whence;
76 long b_start;
77 long b_len;
78 short c_type;
subrata_modakb370c3c2007-11-25 16:37:35 +000079 int c_whence;
plars865695b2001-08-27 22:15:12 +000080 long c_start;
81 long c_len;
82 short c_flag;
subrata_modak56207ce2009-03-23 13:35:39 +000083} testcase;
plars865695b2001-08-27 22:15:12 +000084
subrata_modak56207ce2009-03-23 13:35:39 +000085static testcase testcases[] = {
plars865695b2001-08-27 22:15:12 +000086 /* Test cases: entire boundary */
87 /* #1 Parent making a write lock on entire file */
subrata_modak56207ce2009-03-23 13:35:39 +000088 {F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
89 /* Child attempting a read lock on entire file */
90 F_RDLCK, 0, 0L, 0L, WILLBLOCK},
91
plars865695b2001-08-27 22:15:12 +000092 /* #2 Parent making a write lock on entire file */
subrata_modak56207ce2009-03-23 13:35:39 +000093 {F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
94 /* Child attempting a write lock on entire file */
95 F_WRLCK, 0, 0L, 0L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +000096
97 /* #3 Parent making a read lock on entire file */
subrata_modak56207ce2009-03-23 13:35:39 +000098 {F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
99 /* Child attempting a read lock on entire file */
100 F_RDLCK, 0, 0L, 0L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000101
102 /* #4 Parent making a read lock on entire file */
subrata_modak56207ce2009-03-23 13:35:39 +0000103 {F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
104 /* Child attempting a write lock on entire file */
105 F_WRLCK, 0, 0L, 0L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000106
107 /* Test case: start boundary */
108 /* #5 Parent making a write lock on entire file */
subrata_modak56207ce2009-03-23 13:35:39 +0000109 {F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
110 /*
111 * Child attempting a read lock from beginning of
112 * file for 5 bytes
113 */
114 F_RDLCK, 0, 0L, 5L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000115
116 /* #6 Parent making a write lock on entire file */
subrata_modak56207ce2009-03-23 13:35:39 +0000117 {F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
118 /*
119 * Child attempting a write lock from beginning of
120 * file for 5 bytes
121 */
122 F_WRLCK, 0, 0L, 5L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000123
124 /* #7 Parent making a read lock on entire file */
subrata_modak56207ce2009-03-23 13:35:39 +0000125 {F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
126 /*
127 * Child attempting a read lock from beginning of
128 * file for 5 bytes
129 */
130 F_RDLCK, 0, 0L, 5L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000131
132 /* #8 Parent making a read lock on entire file */
subrata_modak56207ce2009-03-23 13:35:39 +0000133 {F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
134 /*
135 * Child attempting a write lock from beginning of
136 * file for 5 bytes
137 */
138 F_WRLCK, 0, 0L, 5L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000139
140 /* Test cases: end boundary */
141 /* #9 Parent making a write lock on entire file */
subrata_modak56207ce2009-03-23 13:35:39 +0000142 {F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
143 /* Child attempting a read lock from byte 7 to end of file */
144 F_RDLCK, 0, 7L, 0L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000145
146 /* #10 Parent making a write lock on entire file */
subrata_modak56207ce2009-03-23 13:35:39 +0000147 {F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
148 /* Child attempting a write lock from byte 7 to end of file */
149 F_WRLCK, 0, 7L, 0L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000150
151 /* #11 Parent making a read lock on entire file */
subrata_modak56207ce2009-03-23 13:35:39 +0000152 {F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
153 /* Child attempting a read lock from byte 7 to end of file */
154 F_RDLCK, 0, 7L, 0L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000155
156 /* #12 Parent making a read lock on entire file */
subrata_modak56207ce2009-03-23 13:35:39 +0000157 {F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
158 /* Child attempting a write lock from byte 7 to end of file */
159 F_WRLCK, 0, 7L, 0L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000160
161 /* Test cases: entire boundary ( less than entire file) */
subrata_modak4bb656a2009-02-26 12:02:09 +0000162 /*
plars865695b2001-08-27 22:15:12 +0000163 * #13 Parent making a write lock from beginning of
164 * file for 5 bytes
165 */
subrata_modak56207ce2009-03-23 13:35:39 +0000166 {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
167 /*
168 * Child attempting a read lock from beginning of
169 * file for 5 bytes
170 */
171 F_RDLCK, 0, 0L, 5L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000172
173 /*
174 * #14 Parent making a write lock from beginning of file
175 * for 5 bytes
176 */
subrata_modak56207ce2009-03-23 13:35:39 +0000177 {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
178 /*
179 * Child attempting a write lock from beginning of
180 * file for 5 bytes
181 */
182 F_WRLCK, 0, 0L, 5L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000183
184 /*
185 * #15 Parent making a read lock from beginning of
186 * file for 5 bytes
187 */
subrata_modak56207ce2009-03-23 13:35:39 +0000188 {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
189 /*
190 * Child attempting a read lock from beginning of
191 * file for 5 bytes
192 */
193 F_RDLCK, 0, 0L, 5L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000194
195 /*
196 * #16 Parent making a read lock from beginning of
197 * file for 5 bytes
198 */
subrata_modak56207ce2009-03-23 13:35:39 +0000199 {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
200 /*
201 * Child attempting a write lock from beginning
202 * of file for 5 bytes
203 */
204 F_WRLCK, 0, 0L, 5L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000205
206 /* Test cases: inside boundary */
207 /*
208 * #17 Parent making a write lock from beginning
209 * of file for 5 bytes
210 */
subrata_modak56207ce2009-03-23 13:35:39 +0000211 {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
212 /* Child attempting a read lock from byte 2 to byte 4 */
213 F_RDLCK, 0, 1L, 3L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000214
215 /*
216 * #18 Parent making a write lock from beginning of
217 * file for 5 bytes
218 */
subrata_modak56207ce2009-03-23 13:35:39 +0000219 {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
220 /* Child attempting a write lock from byte 2 to byte 4 */
221 F_WRLCK, 0, 1L, 3L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000222
223 /*
224 * #19 Parent making a read lock from beginning of
225 * file for 5 bytes
226 */
subrata_modak56207ce2009-03-23 13:35:39 +0000227 {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
228 /* Child attempting a read lock from byte 2 to byte 4 */
229 F_RDLCK, 0, 1L, 3L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000230
231 /*
232 * #20 Parent making a read lock from beginning of
233 * file for 5 bytes
234 */
subrata_modak56207ce2009-03-23 13:35:39 +0000235 {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
236 /* Child attempting a write lock from byte 2 to byte 4 */
237 F_WRLCK, 0, 1L, 3L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000238
239 /* Test cases: cross boundary (inside to after) */
240 /*
241 * #21 Parent making a write lock from beginning of
242 * file for 5 bytes
243 */
subrata_modak56207ce2009-03-23 13:35:39 +0000244 {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
245 /* Child attempting a read lock from byte 3 to byte 7 */
246 F_RDLCK, 0, 2L, 5L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000247
subrata_modak4bb656a2009-02-26 12:02:09 +0000248 /*
plars865695b2001-08-27 22:15:12 +0000249 * #22 Parent making a write lock from beginning
250 * of file for 5 bytes
251 */
subrata_modak56207ce2009-03-23 13:35:39 +0000252 {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
253 /* Child attempting a write lock from byte 3 to byte 7 */
254 F_WRLCK, 0, 2L, 5L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000255
256 /*
257 * #23 Parent making a read lock from beginning of
258 * file for 5 bytes
259 */
subrata_modak56207ce2009-03-23 13:35:39 +0000260 {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
261 /* Child attempting a read lock from byte 3 to byte 7 */
262 F_RDLCK, 0, 2L, 5L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000263
264 /*
265 * #24 Parent making a read lock from beginning of
266 * file for 5 bytes
267 */
subrata_modak56207ce2009-03-23 13:35:39 +0000268 {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
269 /* Child attempting a write lock from byte 3 to byte 7 */
270 F_WRLCK, 0, 2L, 5L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000271
272 /* Test cases: outside boundary (after) */
273
274 /*
275 * #25 Parent making a write lock from beginning of
276 * file for 5 bytes
277 */
subrata_modak56207ce2009-03-23 13:35:39 +0000278 {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
279 /* Child attempting a read lock from byte 7 to end of file */
280 F_RDLCK, 0, 6L, 0L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000281
282 /*
283 * #26 Parent making a write lock from beginning of
284 * file for 5 bytes
285 */
subrata_modak56207ce2009-03-23 13:35:39 +0000286 {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
287 /* Child attempting a write lock from byte 7 to end of file */
288 F_WRLCK, 0, 6L, 0L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000289
290 /*
291 * #27 Parent making a read lock from beginning of
292 * file for 5 bytes
293 */
subrata_modak56207ce2009-03-23 13:35:39 +0000294 {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
295 /* Child attempting a read lock from byte 7 to end of file */
296 F_RDLCK, 0, 6L, 0L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000297
298 /*
299 * #28 Parent making a read lock from beginning of
300 * file for 5 bytes
301 */
subrata_modak56207ce2009-03-23 13:35:39 +0000302 {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L,
303 /* Child attempting a write lock from byte 7 to end of file */
304 F_WRLCK, 0, 6L, 0L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000305
306 /* Test cases: outside boundary (before) */
307
308 /* #29 Parent making a write lock from byte 3 to byte 7 */
subrata_modak56207ce2009-03-23 13:35:39 +0000309 {F_WRLCK, 0, 2L, 5L, SKIP, 0, 0L, 0L,
310 /* Child attempting a read lock from beginning of file to byte 2 */
311 F_RDLCK, 0, 0L, 2L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000312
313 /* #30 Parent making a write lock from byte 3 to byte 7 */
subrata_modak56207ce2009-03-23 13:35:39 +0000314 {F_WRLCK, 0, 2L, 5L, SKIP, 0, 0L, 0L,
315 /* Child attempting a write lock from beginning of file to byte 2 */
316 F_WRLCK, 0, 0L, 2L, NOBLOCK},
subrata_modakbdbaec52009-02-26 12:14:51 +0000317
plars865695b2001-08-27 22:15:12 +0000318 /* #31 Parent making a write lock from byte 3 to byte 7 */
subrata_modak56207ce2009-03-23 13:35:39 +0000319 {F_RDLCK, 0, 2L, 5L, SKIP, 0, 0L, 0L,
320 /* Child attempting a read lock from beginning of file to byte 2 */
321 F_RDLCK, 0, 0L, 2L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000322
323 /* #32 Parent making a write lock from byte 3 to byte 7 */
subrata_modak56207ce2009-03-23 13:35:39 +0000324 {F_RDLCK, 0, 2L, 5L, SKIP, 0, 0L, 0L,
325 /* Child attempting a write lock from beginning of file to byte 2 */
326 F_WRLCK, 0, 0L, 2L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000327
328 /* Test cases: cross boundary (before to inside) */
329 /* #33 Parent making a write lock from byte 5 to end of file */
subrata_modak56207ce2009-03-23 13:35:39 +0000330 {F_WRLCK, 0, 4L, 0L, SKIP, 0, 0L, 0L,
331 /* Child attempting a read lock from byte 3 to byte 7 */
332 F_RDLCK, 0, 2L, 5L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000333
334 /* #34 Parent making a write lock from byte 5 to end of file */
subrata_modak56207ce2009-03-23 13:35:39 +0000335 {F_WRLCK, 0, 4L, 0L, SKIP, 0, 0L, 0L,
336 /* Child attempting a write lock from byte 3 to byte 7 */
337 F_WRLCK, 0, 2L, 5L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000338
339 /* #35 Parent making a read lock from byte 5 to end of file */
subrata_modak56207ce2009-03-23 13:35:39 +0000340 {F_RDLCK, 0, 4L, 0L, SKIP, 0, 0L, 0L,
341 /* Child attempting a read lock from byte 3 to byte 7 */
342 F_RDLCK, 0, 2L, 5L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000343
344 /* #36 Parent making a read lock from byte 5 to end of file */
subrata_modak56207ce2009-03-23 13:35:39 +0000345 {F_RDLCK, 0, 4L, 0L, SKIP, 0, 0L, 0L,
346 /* Child attempting a write lock from byte 3 to byte 7 */
347 F_WRLCK, 0, 2L, 5L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000348
349 /* Start of negative L_start and L_len test cases */
subrata_modak4bb656a2009-02-26 12:02:09 +0000350 /*
plars865695b2001-08-27 22:15:12 +0000351 * #37 Parent making write lock from byte 2 to byte 3
352 * with L_start = -3
353 */
subrata_modak56207ce2009-03-23 13:35:39 +0000354 {F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L,
355 /* Child attempting write lock on byte 1 */
356 F_WRLCK, 0, 1L, 1L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000357
358 /*
359 * #38 Parent making write lock from byte 2 to byte 3
360 * with L_start = -3
361 */
subrata_modak56207ce2009-03-23 13:35:39 +0000362 {F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L,
363 /* Child attempting write lock on byte 4 */
364 F_WRLCK, 0, 4L, 1L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000365
366 /*
367 * #39 Parent making write lock from byte 2 to byte 3
368 * with L_start = -3
369 */
subrata_modak56207ce2009-03-23 13:35:39 +0000370 {F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L,
371 /* Child attempting write lock on byte 2 */
372 F_WRLCK, 0, 2L, 1L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000373
374 /*
375 * #40 Parent making write lock from byte 2 to byte 3
376 * with L_start = -3
377 */
subrata_modak56207ce2009-03-23 13:35:39 +0000378 {F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L,
379 /* Child attempting write lock on byte 3 */
380 F_WRLCK, 0, 3L, 1L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000381
382 /*
383 * #41 Parent making write lock from byte 2 to byte 6
384 * with L_start = -3
385 */
subrata_modak56207ce2009-03-23 13:35:39 +0000386 {F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L,
387 /* Child attempting write lock on byte 1 */
388 F_WRLCK, 0, 1L, 1L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000389
390 /*
391 * #42 Parent making write lock from byte 2 to byte 6
392 * with L_start = -3
393 */
subrata_modak56207ce2009-03-23 13:35:39 +0000394 {F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L,
395 /* Child attempting write lock on byte 7 */
396 F_WRLCK, 0, 1L, 1L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000397
398 /*
399 * #43 Parent making write lock from byte 2 to byte 6
400 * with L_start = -3
401 */
subrata_modak56207ce2009-03-23 13:35:39 +0000402 {F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L,
403 /* Child attempting write lock on byte 2 */
404 F_WRLCK, 0, 2L, 1L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000405
406 /*
407 * #44 Parent making write lock from byte 2 to byte 6
408 * with L_start = -3
409 */
subrata_modak56207ce2009-03-23 13:35:39 +0000410 {F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L,
411 /* Child attempting write lock on byte 5 */
412 F_WRLCK, 0, 5L, 1L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000413
414 /*
415 * #45 Parent making write lock from byte 2 to byte 6
416 * with L_start = -3
417 */
subrata_modak56207ce2009-03-23 13:35:39 +0000418 {F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L,
419 /* Child attempting write lock on byte 6 */
420 F_WRLCK, 0, 6L, 1L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000421
422 /*
423 * #46 Parent making write lock from byte 2 to byte 3 with
424 * L_start = -2 and L_len = -2
425 */
subrata_modak56207ce2009-03-23 13:35:39 +0000426 {F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L,
427 /* Child attempting write lock on byte 1 */
428 F_WRLCK, 0, 1L, 1L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000429
430 /*
431 * #47 Parent making write lock from byte 2 to byte 3 with
432 * L_start = -2 and L_len = -2
433 */
subrata_modak56207ce2009-03-23 13:35:39 +0000434 {F_WRLCK, 1, -2L, -2L, SKIP, 0, 0L, 0L,
435 /* Child attempting write lock on byte 4 */
436 F_WRLCK, 0, 4L, 1L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000437
438 /*
439 * #48 Parent making write lock from byte 2 to byte 3 with
440 * L_start = -2 and L_len = -2
441 */
subrata_modak56207ce2009-03-23 13:35:39 +0000442 {F_WRLCK, 1, -2L, -2L, SKIP, 0, 0L, 0L,
443 /* Child attempting write lock on byte 2 */
444 F_WRLCK, 0, 2L, 1L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000445
446 /*
447 * #49 Parent making write lock from byte 2 to byte 3 with
448 * L_start = -2 and L_len = -2
449 */
subrata_modak56207ce2009-03-23 13:35:39 +0000450 {F_WRLCK, 1, -2L, -2L, SKIP, 0, 0L, 0L,
451 /* Child attempting write lock on byte 3 */
452 F_WRLCK, 0, 3L, 1L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000453
454 /*
455 * #50 Parent making write lock from byte 6 to byte 7 with
456 * L_start = 2 and L_len = -2
457 */
subrata_modak56207ce2009-03-23 13:35:39 +0000458 {F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L,
459 /* Child attempting write lock on byte 5 */
460 F_WRLCK, 0, 5L, 1L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000461
462 /*
463 * #51 Parent making write lock from byte 6 to byte 7 with
464 * L_start = 2 and L_len = -2
465 */
subrata_modak56207ce2009-03-23 13:35:39 +0000466 {F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L,
467 /* Child attempting write lock on byte 8 */
468 F_WRLCK, 0, 8L, 1L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000469
470 /*
471 * #52 Parent making write lock from byte 6 to byte 7 with
472 * L_start = 2 and L_len = -2
473 */
subrata_modak56207ce2009-03-23 13:35:39 +0000474 {F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L,
475 /* Child attempting write lock on byte 6 */
476 F_WRLCK, 0, 6L, 1L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000477
478 /*
479 * #53 Parent making write lock from byte 6 to byte 7 with
480 * L_start = 2 and L_len = -2
481 */
subrata_modak56207ce2009-03-23 13:35:39 +0000482 {F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L,
483 /* Child attempting write lock on byte 7 */
484 F_WRLCK, 0, 7L, 1L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000485
486 /*
487 * #54 Parent making write lock from byte 3 to byte 7 with
488 * L_start = 2 and L_len = -5
489 */
subrata_modak56207ce2009-03-23 13:35:39 +0000490 {F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L,
491 /* Child attempting write lock on byte 2 */
492 F_WRLCK, 0, 2L, 1L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000493
494 /*
495 * #55 Parent making write lock from byte 3 to byte 7 with
496 * L_start = 2 and L_len = -5
497 */
subrata_modak56207ce2009-03-23 13:35:39 +0000498 {F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L,
499 /* Child attempting write lock on byte 8 */
500 F_WRLCK, 0, 8L, 1L, NOBLOCK},
plars865695b2001-08-27 22:15:12 +0000501
502 /*
503 * #56 Parent making write lock from byte 3 to byte 7 with
504 * L_start = 2 and L_len = -5
505 */
subrata_modak56207ce2009-03-23 13:35:39 +0000506 {F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L,
507 /* Child attempting write lock on byte 3 */
508 F_WRLCK, 0, 3L, 1L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000509
510 /*
511 * #57 Parent making write lock from byte 3 to byte 7 with
512 * L_start = 2 and L_len = -5
513 */
subrata_modak56207ce2009-03-23 13:35:39 +0000514 {F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L,
515 /* Child attempting write lock on byte 5 */
516 F_WRLCK, 0, 5L, 1L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000517
518 /*
519 * #58 Parent making write lock from byte 3 to byte 7 with
520 * L_start = 2 and L_len = -5
521 */
subrata_modak56207ce2009-03-23 13:35:39 +0000522 {F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L,
523 /* Child attempting write lock on byte 7 */
524 F_WRLCK, 0, 7L, 1L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000525
526 /* Test case for block 4 */
527 /* #59 Parent making write lock on entire file */
subrata_modak56207ce2009-03-23 13:35:39 +0000528 {F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L,
529 /* Child attempting write lock on byte 15 to end of file */
530 F_WRLCK, 0, 15L, 0L, WILLBLOCK},
plars865695b2001-08-27 22:15:12 +0000531};
532
subrata_modak56207ce2009-03-23 13:35:39 +0000533static testcase *thiscase;
534static struct flock flock;
535static int parent, child, status, fail = 0;
536static int got1 = 0;
537static int fd;
538static int test;
539static char tmpname[40];
plars865695b2001-08-27 22:15:12 +0000540
541#define FILEDATA "ten bytes!"
542
Mike Frysingere61ddba2014-04-09 23:24:32 -0400543void catch1(int sig); /* signal catching subroutine */
544void catch_alarm(int sig);
plars865695b2001-08-27 22:15:12 +0000545
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200546char *TCID = "fcntl14";
547int TST_TOTAL = 1;
Xiong Zhou11930d72014-09-25 03:21:39 -0400548int NO_NFS = 1;
plars865695b2001-08-27 22:15:12 +0000549
robbiewd34d5812005-07-11 22:28:09 +0000550#ifdef UCLINUX
551static char *argv0; /* Set by main(), passed to self_exec() */
552#endif
robbiew4cf80962003-03-25 21:53:41 +0000553
554/*
555 * cleanup()
subrata_modak56207ce2009-03-23 13:35:39 +0000556 * performs all the ONE TIME cleanup for this test at completion or
557 * premature exit
robbiew4cf80962003-03-25 21:53:41 +0000558 */
subrata_modak56207ce2009-03-23 13:35:39 +0000559void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000560{
plars865695b2001-08-27 22:15:12 +0000561
robbiew4cf80962003-03-25 21:53:41 +0000562 tst_rmdir();
plars865695b2001-08-27 22:15:12 +0000563
plars865695b2001-08-27 22:15:12 +0000564}
565
566/*
567 * setup
subrata_modak56207ce2009-03-23 13:35:39 +0000568 * performs all ONE TIME setup for this test
plars865695b2001-08-27 22:15:12 +0000569 */
subrata_modak56207ce2009-03-23 13:35:39 +0000570void setup(void)
plars865695b2001-08-27 22:15:12 +0000571{
robbiew1e4cf0c2005-02-24 17:03:42 +0000572 struct sigaction act;
573
Garrett Cooper2c282152010-12-16 00:55:50 -0800574 tst_sig(FORK, DEF_HANDLER, cleanup);
subrata_modakb370c3c2007-11-25 16:37:35 +0000575 signal(SIGHUP, SIG_IGN);
plars865695b2001-08-27 22:15:12 +0000576 umask(0);
Garrett Cooper2c282152010-12-16 00:55:50 -0800577 TEST_PAUSE;
subrata_modak56207ce2009-03-23 13:35:39 +0000578 tst_tmpdir(); /* make temp dir and cd to it */
plars865695b2001-08-27 22:15:12 +0000579 parent = getpid();
580
581 /* setup temporary file name */
582 sprintf(tmpname, "fcntl2.%d", parent);
583
584 /* setup signal handler for signal from child */
robbiew1e4cf0c2005-02-24 17:03:42 +0000585 memset(&act, 0, sizeof(act));
586 act.sa_handler = catch1;
587 sigemptyset(&act.sa_mask);
588 sigaddset(&act.sa_mask, SIGUSR1);
589 if ((sigaction(SIGUSR1, &act, NULL)) < 0) {
plars865695b2001-08-27 22:15:12 +0000590 tst_resm(TFAIL, "SIGUSR1 signal setup failed, errno = %d",
591 errno);
592 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800593 }
robbiew1e4cf0c2005-02-24 17:03:42 +0000594
595 memset(&act, 0, sizeof(act));
596 act.sa_handler = catch_alarm;
597 sigemptyset(&act.sa_mask);
598 sigaddset(&act.sa_mask, SIGALRM);
599 if ((sigaction(SIGALRM, &act, NULL)) < 0) {
plars865695b2001-08-27 22:15:12 +0000600 tst_resm(TFAIL, "SIGALRM signal setup failed");
601 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800602 }
subrata_modak56207ce2009-03-23 13:35:39 +0000603}
604
605void wake_parent(void)
606{
607 if ((kill(parent, SIGUSR1)) < 0) {
608 tst_resm(TFAIL, "Attempt to send signal to parent " "failed");
609 tst_resm(TFAIL, "Test case %d, errno = %d", test + 1, errno);
610 fail = 1;
plars865695b2001-08-27 22:15:12 +0000611 }
612}
613
Mike Frysingerc57fba52014-04-09 18:56:30 -0400614void do_usleep_child(void)
mridgedb639212005-01-04 21:04:11 +0000615{
subrata_modak56207ce2009-03-23 13:35:39 +0000616 usleep(100000); /* XXX how long is long enough? */
robbiewd34d5812005-07-11 22:28:09 +0000617 wake_parent();
618 exit(0);
619}
620
Mike Frysingerc57fba52014-04-09 18:56:30 -0400621void dochild(void)
subrata_modak56207ce2009-03-23 13:35:39 +0000622{ /* child process */
623 int rc;
624 pid_t pid;
mridgedb639212005-01-04 21:04:11 +0000625
robbiew4cf80962003-03-25 21:53:41 +0000626 /* Initialize the child lock structure */
627 flock.l_type = thiscase->c_type;
628 flock.l_whence = thiscase->c_whence;
629 flock.l_start = thiscase->c_start;
630 flock.l_len = thiscase->c_len;
631 flock.l_pid = 0;
632 fail = 0;
633
634 /*
635 * Check to see if child lock will succeed. If it will, FLOCK
636 * structure will return with l_type changed to F_UNLCK. If it will
637 * not, the parent pid will be returned in l_pid and the type of
638 * lock that will block it in l_type.
639 */
mridgedb639212005-01-04 21:04:11 +0000640 if ((rc = fcntl(fd, F_GETLK, &flock)) < 0) {
robbiew4cf80962003-03-25 21:53:41 +0000641 tst_resm(TFAIL, "Attempt to check lock status failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000642 tst_resm(TFAIL, "Test case %d, errno = %d", test + 1, errno);
robbiew4cf80962003-03-25 21:53:41 +0000643 fail = 1;
mridgedb639212005-01-04 21:04:11 +0000644 } else {
robbiew4cf80962003-03-25 21:53:41 +0000645
subrata_modak56207ce2009-03-23 13:35:39 +0000646 if ((thiscase->c_flag) == NOBLOCK) {
647 if (flock.l_type != F_UNLCK) {
648 tst_resm(TFAIL,
649 "Test case %d, GETLK: type = %d, "
650 "%d was expected", test + 1,
651 flock.l_type, F_UNLCK);
652 fail = 1;
653 }
robbiew4cf80962003-03-25 21:53:41 +0000654
subrata_modak56207ce2009-03-23 13:35:39 +0000655 if (flock.l_whence != thiscase->c_whence) {
656 tst_resm(TFAIL,
657 "Test case %d, GETLK: whence = %d, "
658 "should have remained %d", test + 1,
659 flock.l_whence, thiscase->c_whence);
660 fail = 1;
661 }
robbiew4cf80962003-03-25 21:53:41 +0000662
subrata_modak56207ce2009-03-23 13:35:39 +0000663 if (flock.l_start != thiscase->c_start) {
664 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800665 "Test case %d, GETLK: start = %" PRId64
666 ", " "should have remained %" PRId64,
667 test + 1, (int64_t) flock.l_start,
668 (int64_t) thiscase->c_start);
subrata_modak56207ce2009-03-23 13:35:39 +0000669 fail = 1;
670 }
robbiew4cf80962003-03-25 21:53:41 +0000671
subrata_modak56207ce2009-03-23 13:35:39 +0000672 if (flock.l_len != thiscase->c_len) {
673 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800674 "Test case %d, GETLK: len = %" PRId64
675 ", " "should have remained %" PRId64,
676 test + 1, (int64_t) flock.l_len,
677 (int64_t) thiscase->c_len);
subrata_modak56207ce2009-03-23 13:35:39 +0000678 fail = 1;
679 }
robbiew4cf80962003-03-25 21:53:41 +0000680
subrata_modak56207ce2009-03-23 13:35:39 +0000681 if (flock.l_pid != 0) {
682 tst_resm(TFAIL,
683 "Test case %d, GETLK: pid = %d, "
684 "should have remained 0", test + 1,
685 flock.l_pid);
686 fail = 1;
687 }
688 } else {
689 if (flock.l_pid != parent) {
690 tst_resm(TFAIL,
691 "Test case %d, GETLK: pid = %d, "
692 "should be parent's id of %d",
693 test + 1, flock.l_pid, parent);
694 fail = 1;
695 }
robbiew4cf80962003-03-25 21:53:41 +0000696
subrata_modak56207ce2009-03-23 13:35:39 +0000697 if (flock.l_type != thiscase->a_type) {
698 tst_resm(TFAIL,
699 "Test case %d, GETLK: type = %d, "
700 "should be parent's first lock type of %d",
701 test + 1, flock.l_type,
702 thiscase->a_type);
703 fail = 1;
704 }
robbiew4cf80962003-03-25 21:53:41 +0000705 }
706 }
707
708 /*
709 * now try to set the lock, nonblocking
710 * This will succeed for NOBLOCK,
711 * fail for WILLBLOCK
712 */
713 flock.l_type = thiscase->c_type;
714 flock.l_whence = thiscase->c_whence;
715 flock.l_start = thiscase->c_start;
716 flock.l_len = thiscase->c_len;
717 flock.l_pid = 0;
718
mridgedb639212005-01-04 21:04:11 +0000719 if ((rc = fcntl(fd, F_SETLK, &flock)) < 0) {
robbiew4cf80962003-03-25 21:53:41 +0000720 if ((thiscase->c_flag) == NOBLOCK) {
721 tst_resm(TFAIL, "Attempt to set child NONBLOCKING "
722 "lock failed");
723 tst_resm(TFAIL, "Test case %d, errno = %d",
724 test + 1, errno);
725 fail = 1;
726 }
727 }
728
729 if ((thiscase->c_flag) == WILLBLOCK) {
730 /* Check for proper errno condition */
mridgedb639212005-01-04 21:04:11 +0000731 if (rc != -1 || (errno != EACCES && errno != EAGAIN)) {
732 tst_resm(TFAIL,
subrata_modak56207ce2009-03-23 13:35:39 +0000733 "SETLK: rc = %d, errno = %d, -1/EAGAIN or EACCES "
mridgedb639212005-01-04 21:04:11 +0000734 "was expected", rc, errno);
robbiew4cf80962003-03-25 21:53:41 +0000735 fail = 1;
736 }
subrata_modak56207ce2009-03-23 13:35:39 +0000737 if (rc == 0) {
738 /* accidentally got the lock */
739 /* XXX how to clean up? */
740 (void)fcntl(fd, F_UNLCK, &flock);
741 }
robbiew4cf80962003-03-25 21:53:41 +0000742 /*
743 * Lock should succeed after blocking and parent releases
mridgedb639212005-01-04 21:04:11 +0000744 * lock, tell the parent to release the locks.
subrata_modak56207ce2009-03-23 13:35:39 +0000745 * Do the lock in this process, send the signal in a child
746 * process, so that the SETLKW actually uses the blocking
747 * mechanism in the kernel.
748 *
749 * XXX inherent race: we want to wait until the
750 * F_SETLKW has started, but we don't have a way to
751 * check that reliably in the child. (We'd
752 * need some way to have fcntl() atomically unblock a
753 * signal and wait for the lock.)
robbiew4cf80962003-03-25 21:53:41 +0000754 */
subrata_modak56207ce2009-03-23 13:35:39 +0000755 pid = FORK_OR_VFORK();
756 switch (pid) {
757 case -1:
758 tst_resm(TFAIL, "Fork failed");
759 break;
760 case 0: /* child */
robbiewd34d5812005-07-11 22:28:09 +0000761#ifdef UCLINUX
Garrett Cooper64bcf4a2010-12-21 12:00:28 -0800762 if (self_exec(argv0, "nd", 1, parent) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000763 tst_resm(TFAIL, "self_exec failed");
764 break;
765 }
robbiewd34d5812005-07-11 22:28:09 +0000766#else
subrata_modak56207ce2009-03-23 13:35:39 +0000767 do_usleep_child();
robbiewd34d5812005-07-11 22:28:09 +0000768#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000769 break;
mridgedb639212005-01-04 21:04:11 +0000770
subrata_modak56207ce2009-03-23 13:35:39 +0000771 default:
772 if ((rc = fcntl(fd, F_SETLKW, &flock)) < 0) {
773 tst_resm(TFAIL, "Attempt to set child BLOCKING "
774 "lock failed");
775 tst_resm(TFAIL, "Test case %d, errno = %d",
776 test + 1, errno);
777 fail = 1;
778 }
779 waitpid(pid, &status, 0);
780 break;
781 }
robbiew4cf80962003-03-25 21:53:41 +0000782 }
783 if (fail) {
784 exit(1);
785 } else {
786 exit(0);
787 }
788}
789
robbiew4cf80962003-03-25 21:53:41 +0000790void run_test(int file_flag, int file_mode, int seek, int start, int end)
plars865695b2001-08-27 22:15:12 +0000791{
plars865695b2001-08-27 22:15:12 +0000792 extern long time();
793
subrata_modak56207ce2009-03-23 13:35:39 +0000794 /* reset fail to 0 for each run_test call */
795 fail = 0;
robbiew49058422003-07-25 16:10:11 +0000796
plars865695b2001-08-27 22:15:12 +0000797 /* loop thru all test cases */
798 for (test = start; test < end; test++) {
799 /* open a temp file to lock */
800 fd = open(tmpname, file_flag, file_mode);
801 if (fd < 0) {
802 tst_brkm(TBROK, cleanup, "open() failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800803 }
plars865695b2001-08-27 22:15:12 +0000804
805 /* write some dummy data to the file */
subrata_modak56207ce2009-03-23 13:35:39 +0000806 if (write(fd, FILEDATA, 10) < 0) {
plars865695b2001-08-27 22:15:12 +0000807 tst_brkm(TBROK, cleanup, "write() failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800808 }
plars865695b2001-08-27 22:15:12 +0000809
810 /* seek into file if indicated */
811 if (seek) {
812 if (lseek(fd, seek, 0) < 0) {
813 tst_brkm(TBROK, cleanup, "lseek() failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800814 }
plars865695b2001-08-27 22:15:12 +0000815 }
816
817 /* Initialize first parent lock structure */
818 thiscase = &testcases[test];
819 flock.l_type = thiscase->a_type;
820 flock.l_whence = thiscase->a_whence;
821 flock.l_start = thiscase->a_start;
822 flock.l_len = thiscase->a_len;
823
824 /* set the initial parent lock on the file */
subrata_modak56207ce2009-03-23 13:35:39 +0000825 if ((fcntl(fd, F_SETLK, &flock)) < 0) {
plars865695b2001-08-27 22:15:12 +0000826 tst_resm(TFAIL, "First parent lock failed");
827 tst_resm(TFAIL, "Test case %d, errno = %d",
828 test + 1, errno);
829 fail = 1;
830 }
831
832 if ((thiscase->b_type) != SKIP) {
833 /* Initialize second parent lock structure */
834 flock.l_type = thiscase->b_type;
835 flock.l_whence = thiscase->b_whence;
836 flock.l_start = thiscase->b_start;
837 flock.l_len = thiscase->b_len;
838
839 /* set the second parent lock */
840 if ((fcntl(fd, F_SETLK, &flock)) < 0) {
841 tst_resm(TFAIL, "Second parent lock failed");
842 tst_resm(TFAIL, "Test case %d, errno = %d",
843 test + 1, errno);
844 fail = 1;
845 }
846 }
847 if ((thiscase->c_type) == SKIP) {
848 /* close the temp file and move to next test case */
849 close(fd);
850 tst_resm(TINFO, "skipping test %d", test + 1);
851 continue; /* continue to the next case */
852 }
853 /*
854 * Mask SIG_USR1 before forking child, to avoid race
855 */
856 (void)sighold(SIGUSR1);
857
robbiew49058422003-07-25 16:10:11 +0000858 /* flush the stdout to avoid garbled output */
859 fflush(stdout);
860
plars865695b2001-08-27 22:15:12 +0000861 /* spawn a child process */
robbiewd34d5812005-07-11 22:28:09 +0000862 if ((child = FORK_OR_VFORK()) == 0) {
863#ifdef UCLINUX
864 if (self_exec(argv0, "nddddddddd", 2, thiscase->c_type,
865 thiscase->c_whence, thiscase->c_start,
866 thiscase->c_len, thiscase->c_flag,
867 thiscase->a_type, fd, test, parent) < 0) {
868 tst_resm(TFAIL, "self_exec failed");
869 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800870 }
robbiewd34d5812005-07-11 22:28:09 +0000871#else
plars865695b2001-08-27 22:15:12 +0000872 dochild();
robbiewd34d5812005-07-11 22:28:09 +0000873#endif
plars865695b2001-08-27 22:15:12 +0000874 }
875 if (child < 0) {
876 tst_resm(TFAIL, "Fork failed");
877 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800878 }
plars865695b2001-08-27 22:15:12 +0000879 /* parent process */
880 if ((thiscase->c_flag) == WILLBLOCK) {
881 /*
882 * Wait for a signal from the child then remove
883 * blocking lock. Set a 60 second alarm to break the
884 * pause just in case the child never signals us.
885 */
886 alarm(TIME_OUT);
887 sigpause(SIGUSR1);
888
889 /* turn off the alarm timer */
890 alarm((unsigned)0);
891 if (got1 != 1)
892 tst_resm(TINFO, "Pause terminated without "
893 "signal SIGUSR1 from child");
894 got1 = 0; /* reset the flag */
895
896 /*
897 * setup lock structure for parent to delete
898 * blocking lock then wait for child to exit
899 */
900 flock.l_type = F_UNLCK;
901 flock.l_whence = 0;
902 flock.l_start = 0L;
903 flock.l_len = 0L;
904 if ((fcntl(fd, F_SETLK, &flock)) < 0) {
905 tst_resm(TFAIL, "Attempt to release parent "
906 "lock failed");
907 tst_resm(TFAIL, "Test case %d, errno = %d",
908 test + 1, errno);
909 fail = 1;
910 }
911 }
912 /*
913 * set a 60 second alarm to break the wait just in case the
914 * child doesn't terminate on its own accord
915 */
916 alarm(TIME_OUT);
917
918 /* wait for the child to terminate and close the file */
mridgedb639212005-01-04 21:04:11 +0000919 waitpid(child, &status, 0);
plars865695b2001-08-27 22:15:12 +0000920 /* turn off the alarm clock */
921 alarm((unsigned)0);
922 if (status != 0) {
923 tst_resm(TFAIL, "tchild returned status 0x%x", status);
924 fail = 1;
925 }
926 close(fd);
927 if (fail) {
928 tst_resm(TFAIL, "testcase:%d FAILED", test + 1);
929 } else {
930 tst_resm(TPASS, "testcase:%d PASSED", test + 1);
931 }
932 }
933 unlink(tmpname);
934}
935
Mike Frysingere61ddba2014-04-09 23:24:32 -0400936void catch_alarm(int sig)
plars865695b2001-08-27 22:15:12 +0000937{
938 /*
939 * Timer has runout and child has not signaled, need
940 * to kill off the child as it appears it will not
941 * on its own accord. Check that it is still around
942 * as it may have terminated abnormally while parent
943 * was waiting for SIGUSR1 signal from it.
944 */
945 if (kill(child, 0) == 0) {
946 kill(child, SIGKILL);
947 perror("The child didnot terminate on its own accord");
948 }
949}
950
Mike Frysingere61ddba2014-04-09 23:24:32 -0400951void catch1(int sig)
subrata_modak56207ce2009-03-23 13:35:39 +0000952{ /* invoked on catching SIGUSR1 */
robbiew1e4cf0c2005-02-24 17:03:42 +0000953 struct sigaction act;
954
plars865695b2001-08-27 22:15:12 +0000955 /*
956 * Set flag to let parent know that child is ready to have lock
957 * removed
958 */
robbiew1e4cf0c2005-02-24 17:03:42 +0000959 memset(&act, 0, sizeof(act));
960 act.sa_handler = catch1;
961 sigemptyset(&act.sa_mask);
962 sigaddset(&act.sa_mask, SIGUSR1);
963 sigaction(SIGUSR1, &act, NULL);
plars865695b2001-08-27 22:15:12 +0000964 got1++;
965}
966
Xiong Zhou82180502014-09-25 03:17:57 -0400967static void testcheck_end(int check_fail, char *msg)
968{
969 if (check_fail) {
970 tst_resm(TFAIL, "%s FAILED", msg);
971 } else {
972 tst_resm(TPASS, "%s PASSED", msg);
973 }
974}
975
robbiew4cf80962003-03-25 21:53:41 +0000976int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000977{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200978 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200979 const char *msg;
plars865695b2001-08-27 22:15:12 +0000980
Garrett Cooper45e285d2010-11-22 12:19:25 -0800981 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800982 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew4cf80962003-03-25 21:53:41 +0000983 }
robbiewd34d5812005-07-11 22:28:09 +0000984#ifdef UCLINUX
985 argv0 = av[0];
986
Garrett Cooper64bcf4a2010-12-21 12:00:28 -0800987 maybe_run_child(&do_usleep_child, "nd", 1, &parent);
robbiewd34d5812005-07-11 22:28:09 +0000988 thiscase = malloc(sizeof(testcase));
989
990 maybe_run_child(&dochild, "nddddddddd", 2, &thiscase->c_type,
991 &thiscase->c_whence, &thiscase->c_start,
992 &thiscase->c_len, &thiscase->c_flag, &thiscase->a_type,
993 &fd, &test, &parent);
994#endif
995
subrata_modak56207ce2009-03-23 13:35:39 +0000996 setup(); /* global setup */
robbiew4cf80962003-03-25 21:53:41 +0000997
Xiong Zhou11930d72014-09-25 03:21:39 -0400998 /* Check if test on NFS or not*/
999 if (tst_fs_type(cleanup, ".") == TST_NFS_MAGIC)
1000 NO_NFS = 0;
subrata_modak5387ba02008-05-05 08:49:34 +00001001
robbiew4cf80962003-03-25 21:53:41 +00001002 /* Check for looping state if -i option is given */
1003 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +08001004 /* reset tst_count in case we are looping */
1005 tst_count = 0;
robbiew4cf80962003-03-25 21:53:41 +00001006
mridgedb639212005-01-04 21:04:11 +00001007/* //block1: */
robbiew4cf80962003-03-25 21:53:41 +00001008 tst_resm(TINFO, "Enter block 1: without mandatory locking");
1009 fail = 0;
subrata_modak4bb656a2009-02-26 12:02:09 +00001010 /*
subrata_modak56207ce2009-03-23 13:35:39 +00001011 * try various file locks on an ordinary file without
robbiew4cf80962003-03-25 21:53:41 +00001012 * mandatory locking
1013 */
1014 (void)run_test(O_CREAT | O_RDWR | O_TRUNC, 0777, 0, 0, 36);
Xiong Zhou82180502014-09-25 03:17:57 -04001015 testcheck_end(fail, "Block 1, test 1");
robbiew4cf80962003-03-25 21:53:41 +00001016
1017 /* Now try with negative values for L_start and L_len */
1018 (void)run_test(O_CREAT | O_RDWR | O_TRUNC, 0777, 5, 36, 45);
Xiong Zhou82180502014-09-25 03:17:57 -04001019 testcheck_end(fail, "Block 1, test 2");
robbiew4cf80962003-03-25 21:53:41 +00001020
robbiew4cf80962003-03-25 21:53:41 +00001021 tst_resm(TINFO, "Exit block 1");
1022
mridgedb639212005-01-04 21:04:11 +00001023/* //block2: */
subrata_modak4bb656a2009-02-26 12:02:09 +00001024 /*
Xiong Zhou11930d72014-09-25 03:21:39 -04001025 * Skip block2 if test on NFS, since NFS does not support
1026 * mandatory locking
robbiew4cf80962003-03-25 21:53:41 +00001027 */
Xiong Zhou11930d72014-09-25 03:21:39 -04001028 tst_resm(TINFO, "Enter block 2: with mandatory locking");
1029 if (NO_NFS) {
1030 fail = 0;
1031 /*
1032 * Try various locks on a file with mandatory
1033 * record locking this should behave the same
1034 * as an ordinary file
1035 */
1036 (void)run_test(O_CREAT | O_RDWR | O_TRUNC,
1037 S_ENFMT | S_IRUSR | S_IWUSR, 0, 0, 36);
1038 testcheck_end(fail, "Block 2, test 1");
robbiew4cf80962003-03-25 21:53:41 +00001039
Xiong Zhou11930d72014-09-25 03:21:39 -04001040 /* Now try negative values for L_start and L_len */
1041 (void)run_test(O_CREAT | O_RDWR | O_TRUNC,
1042 S_ENFMT | S_IRUSR | S_IWUSR, 5, 36, 45);
1043 testcheck_end(fail, "Block 2, test 2");
1044 } else
1045 tst_resm(TCONF, "Skip block 2 as NFS does not"
1046 " support mandatory locking");
robbiew4cf80962003-03-25 21:53:41 +00001047 tst_resm(TINFO, "Exit block 2");
1048
mridgedb639212005-01-04 21:04:11 +00001049/* //block3: */
robbiew4cf80962003-03-25 21:53:41 +00001050 tst_resm(TINFO, "Enter block 3");
1051 fail = 0;
1052 /*
1053 * Check that proper error status is returned when invalid
1054 * argument used for WHENCE (negative value)
1055 */
1056
1057 /* open a temporary file to lock */
1058 fd = open(tmpname, O_CREAT | O_RDWR | O_TRUNC, 0777);
1059 if (fd < 0) {
1060 tst_brkm(TBROK, cleanup, "open failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +08001061 }
robbiew4cf80962003-03-25 21:53:41 +00001062
1063 /* Write some dummy data to the file */
1064 if (write(fd, FILEDATA, 10) < 0) {
1065 tst_brkm(TBROK, cleanup, "write failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +08001066 }
robbiew4cf80962003-03-25 21:53:41 +00001067
1068 /* Initialize lock structure */
1069 flock.l_type = F_WRLCK;
1070 flock.l_whence = -1;
1071 flock.l_start = 0L;
1072 flock.l_len = 0L;
1073
1074 /* Set the lock on the file */
1075 if ((fcntl(fd, F_SETLK, &flock)) < 0) {
1076 if (errno != EINVAL) {
1077 tst_resm(TFAIL, "Expected %d got %d",
1078 EINVAL, errno);
1079 fail = 1;
1080 }
1081 } else {
1082 tst_resm(TFAIL, "Lock succeeded when it should have "
1083 "failed");
1084 fail = 1;
1085 }
1086
1087 /* Close and remove temp file */
1088 close(fd);
1089 unlink(tmpname);
1090
Xiong Zhou82180502014-09-25 03:17:57 -04001091 testcheck_end(fail, "Test with negative whence locking");
robbiew4cf80962003-03-25 21:53:41 +00001092 tst_resm(TINFO, "Exit block 3");
1093
mridgedb639212005-01-04 21:04:11 +00001094/* //block4: */
robbiew4cf80962003-03-25 21:53:41 +00001095 tst_resm(TINFO, "Enter block 4");
1096 fail = 0;
1097 /*
1098 * Check that a lock on end of file is still valid when
1099 * additional data is appended to end of file and a new
1100 * process attempts to lock new data
1101 */
1102 /* open a temp file to lock */
1103 fd = open(tmpname, O_CREAT | O_RDWR | O_TRUNC, 0777);
1104 if (fd < 0) {
1105 tst_brkm(TBROK, cleanup, "open failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +08001106 }
robbiew4cf80962003-03-25 21:53:41 +00001107
1108 /* Write some dummy data to the file */
1109 if (write(fd, FILEDATA, 10) < 0) {
1110 tst_brkm(TBROK, cleanup, "write failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +08001111 }
robbiew4cf80962003-03-25 21:53:41 +00001112
1113 /* Initialize first parent lock structure */
1114 thiscase = &testcases[58];
1115 flock.l_type = thiscase->a_type;
1116 flock.l_whence = thiscase->a_whence;
1117 flock.l_start = thiscase->a_start;
1118 flock.l_len = thiscase->a_len;
1119
1120 /* Set the initial parent lock on the file */
1121 if ((fcntl(fd, F_SETLK, &flock)) < 0) {
1122 tst_resm(TFAIL, "First parent lock failed");
1123 tst_resm(TFAIL, "Test case %d, errno = %d", 58, errno);
1124 fail = 1;
1125 }
1126
1127 /* Write some additional data to end of file */
1128 if (write(fd, FILEDATA, 10) < 0) {
1129 tst_brkm(TBROK, cleanup, "write failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +08001130 }
robbiew4cf80962003-03-25 21:53:41 +00001131
1132 /* Mask signal to avoid race */
1133 if (sighold(SIGUSR1) < 0) {
1134 tst_brkm(TBROK, cleanup, "sighold failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +08001135 }
robbiew4cf80962003-03-25 21:53:41 +00001136
1137 /* spawn a child process */
subrata_modakb370c3c2007-11-25 16:37:35 +00001138 if ((child = FORK_OR_VFORK()) == 0) {
robbiewd34d5812005-07-11 22:28:09 +00001139#ifdef UCLINUX
1140 if (self_exec(argv0, "nddddddddd", 2, thiscase->c_type,
1141 thiscase->c_whence, thiscase->c_start,
1142 thiscase->c_len, thiscase->c_flag,
1143 thiscase->a_type, fd, test, parent) < 0) {
1144 tst_resm(TFAIL, "self_exec failed");
1145 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +08001146 }
robbiewd34d5812005-07-11 22:28:09 +00001147#else
robbiew4cf80962003-03-25 21:53:41 +00001148 dochild();
robbiewd34d5812005-07-11 22:28:09 +00001149#endif
robbiew4cf80962003-03-25 21:53:41 +00001150 }
1151 if (child < 0) {
1152 tst_resm(TFAIL, "Fork failed");
1153 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +08001154 }
robbiew4cf80962003-03-25 21:53:41 +00001155
1156 /* parent process */
1157
1158 /*
1159 * Wait for a signal from the child then remove blocking lock.
1160 * Set a 60 sec alarm to break the pause just in case the
1161 * child doesn't terminate on its own accord
1162 */
1163 (void)alarm(TIME_OUT);
1164
1165 /* pause for the SIGUSR1 signal from child */
1166 (void)sigpause(SIGUSR1);
1167
1168 /* turn off the alarm timer */
1169 (void)alarm((unsigned)0);
1170 if (got1 != 1) {
1171 tst_resm(TINFO, "Pause terminated without signal "
1172 "SIGUSR1 from child");
1173 }
subrata_modak56207ce2009-03-23 13:35:39 +00001174 got1 = 0; /* reset flag */
robbiew4cf80962003-03-25 21:53:41 +00001175
1176 /*
1177 * Set up lock structure for parent to delete
1178 * blocking lock then wait for child to exit
1179 */
1180 flock.l_type = F_UNLCK;
1181 flock.l_whence = 0;
1182 flock.l_start = 0L;
1183 flock.l_len = 0L;
1184 if ((fcntl(fd, F_SETLK, &flock)) < 0) {
1185 tst_resm(TFAIL, "Attempt to release parent lock "
1186 "failed");
1187 tst_resm(TFAIL, "Test case %d, errno = %d", test + 1,
1188 errno);
1189 fail = 1;
1190 }
1191
1192 /*
1193 * set a 60 sec alarm to break the wait just in case the
1194 * child doesn't terminate on its own accord
1195 */
1196 (void)alarm(TIME_OUT);
1197
1198 /* wait for the child to terminate and close the file */
1199 waitpid(child, &status, 0);
1200 if (WEXITSTATUS(status) != 0) {
1201 fail = 1;
1202 tst_resm(TFAIL, "child returned bad exit status");
1203 }
1204
1205 /* turn off the alarm clock */
1206 (void)alarm((unsigned)0);
1207 if (status != 0) {
1208 tst_resm(TFAIL, "child returned status 0x%x", status);
1209 fail = 1;
1210 }
1211 close(fd);
1212 unlink(tmpname);
1213
Xiong Zhou82180502014-09-25 03:17:57 -04001214 testcheck_end(fail, "Test of locks on file");
robbiew4cf80962003-03-25 21:53:41 +00001215 tst_resm(TINFO, "Exit block 4");
1216 }
1217 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -08001218 tst_exit();
Garrett Cooper64bcf4a2010-12-21 12:00:28 -08001219}