1 /*
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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * NAME
22 * fcntl14.c
23 *
24 * DESCRIPTION
25 * File locking test cases for fcntl. In Linux, S_ENFMT is not implemented
26 * 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
30 * Various test cases are used to lock a file opened without mandatory
31 * locking, with mandatory locking and mandatory locking with NOBLOCK
32 *
33 * USAGE
34 * fcntl14
35 *
36 * HISTORY
37 * 07/2001 Ported by Wayne Boyer
38 *
39 * RESTRICTIONS
40 * None
41 */
42 #define _GNU_SOURCE 1
43 #include <fcntl.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <signal.h>
47 #include <errno.h>
48 #include <sys/wait.h>
49 #include <inttypes.h>
50 #include "test.h"
51 #include "safe_macros.h"
52
53 #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
58 #define S_ENFMT S_ISGID
59 #endif
60
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
69 typedef struct {
70 short a_type;
71 short a_whence;
72 long a_start;
73 long a_len;
74 short b_type; /* SKIP means suppress fcntl call */
75 short b_whence;
76 long b_start;
77 long b_len;
78 short c_type;
79 int c_whence;
80 long c_start;
81 long c_len;
82 short c_flag;
83 } testcase;
84
85 static testcase testcases[] = {
86 /* Test cases: entire boundary */
87 /* #1 Parent making a write lock on entire file */
88 {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
92 /* #2 Parent making a write lock on entire file */
93 {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},
96
97 /* #3 Parent making a read lock on entire file */
98 {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},
101
102 /* #4 Parent making a read lock on entire file */
103 {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},
106
107 /* Test case: start boundary */
108 /* #5 Parent making a write lock on entire file */
109 {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},
115
116 /* #6 Parent making a write lock on entire file */
117 {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},
123
124 /* #7 Parent making a read lock on entire file */
125 {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},
131
132 /* #8 Parent making a read lock on entire file */
133 {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},
139
140 /* Test cases: end boundary */
141 /* #9 Parent making a write lock on entire file */
142 {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},
145
146 /* #10 Parent making a write lock on entire file */
147 {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},
150
151 /* #11 Parent making a read lock on entire file */
152 {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},
155
156 /* #12 Parent making a read lock on entire file */
157 {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},
160
161 /* Test cases: entire boundary ( less than entire file) */
162 /*
163 * #13 Parent making a write lock from beginning of
164 * file for 5 bytes
165 */
166 {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},
172
173 /*
174 * #14 Parent making a write lock from beginning of file
175 * for 5 bytes
176 */
177 {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},
183
184 /*
185 * #15 Parent making a read lock from beginning of
186 * file for 5 bytes
187 */
188 {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},
194
195 /*
196 * #16 Parent making a read lock from beginning of
197 * file for 5 bytes
198 */
199 {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},
205
206 /* Test cases: inside boundary */
207 /*
208 * #17 Parent making a write lock from beginning
209 * of file for 5 bytes
210 */
211 {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},
214
215 /*
216 * #18 Parent making a write lock from beginning of
217 * file for 5 bytes
218 */
219 {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},
222
223 /*
224 * #19 Parent making a read lock from beginning of
225 * file for 5 bytes
226 */
227 {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},
230
231 /*
232 * #20 Parent making a read lock from beginning of
233 * file for 5 bytes
234 */
235 {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},
238
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 */
244 {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},
247
248 /*
249 * #22 Parent making a write lock from beginning
250 * of file for 5 bytes
251 */
252 {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},
255
256 /*
257 * #23 Parent making a read lock from beginning of
258 * file for 5 bytes
259 */
260 {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},
263
264 /*
265 * #24 Parent making a read lock from beginning of
266 * file for 5 bytes
267 */
268 {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},
271
272 /* Test cases: outside boundary (after) */
273
274 /*
275 * #25 Parent making a write lock from beginning of
276 * file for 5 bytes
277 */
278 {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},
281
282 /*
283 * #26 Parent making a write lock from beginning of
284 * file for 5 bytes
285 */
286 {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},
289
290 /*
291 * #27 Parent making a read lock from beginning of
292 * file for 5 bytes
293 */
294 {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},
297
298 /*
299 * #28 Parent making a read lock from beginning of
300 * file for 5 bytes
301 */
302 {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},
305
306 /* Test cases: outside boundary (before) */
307
308 /* #29 Parent making a write lock from byte 3 to byte 7 */
309 {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},
312
313 /* #30 Parent making a write lock from byte 3 to byte 7 */
314 {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},
317
318 /* #31 Parent making a write lock from byte 3 to byte 7 */
319 {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},
322
323 /* #32 Parent making a write lock from byte 3 to byte 7 */
324 {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},
327
328 /* Test cases: cross boundary (before to inside) */
329 /* #33 Parent making a write lock from byte 5 to end of file */
330 {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},
333
334 /* #34 Parent making a write lock from byte 5 to end of file */
335 {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},
338
339 /* #35 Parent making a read lock from byte 5 to end of file */
340 {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},
343
344 /* #36 Parent making a read lock from byte 5 to end of file */
345 {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},
348
349 /* Start of negative L_start and L_len test cases */
350 /*
351 * #37 Parent making write lock from byte 2 to byte 3
352 * with L_start = -3
353 */
354 {F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L,
355 /* Child attempting write lock on byte 1 */
356 F_WRLCK, 0, 1L, 1L, NOBLOCK},
357
358 /*
359 * #38 Parent making write lock from byte 2 to byte 3
360 * with L_start = -3
361 */
362 {F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L,
363 /* Child attempting write lock on byte 4 */
364 F_WRLCK, 0, 4L, 1L, NOBLOCK},
365
366 /*
367 * #39 Parent making write lock from byte 2 to byte 3
368 * with L_start = -3
369 */
370 {F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L,
371 /* Child attempting write lock on byte 2 */
372 F_WRLCK, 0, 2L, 1L, WILLBLOCK},
373
374 /*
375 * #40 Parent making write lock from byte 2 to byte 3
376 * with L_start = -3
377 */
378 {F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L,
379 /* Child attempting write lock on byte 3 */
380 F_WRLCK, 0, 3L, 1L, WILLBLOCK},
381
382 /*
383 * #41 Parent making write lock from byte 2 to byte 6
384 * with L_start = -3
385 */
386 {F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L,
387 /* Child attempting write lock on byte 1 */
388 F_WRLCK, 0, 1L, 1L, NOBLOCK},
389
390 /*
391 * #42 Parent making write lock from byte 2 to byte 6
392 * with L_start = -3
393 */
394 {F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L,
395 /* Child attempting write lock on byte 7 */
396 F_WRLCK, 0, 1L, 1L, NOBLOCK},
397
398 /*
399 * #43 Parent making write lock from byte 2 to byte 6
400 * with L_start = -3
401 */
402 {F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L,
403 /* Child attempting write lock on byte 2 */
404 F_WRLCK, 0, 2L, 1L, WILLBLOCK},
405
406 /*
407 * #44 Parent making write lock from byte 2 to byte 6
408 * with L_start = -3
409 */
410 {F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L,
411 /* Child attempting write lock on byte 5 */
412 F_WRLCK, 0, 5L, 1L, WILLBLOCK},
413
414 /*
415 * #45 Parent making write lock from byte 2 to byte 6
416 * with L_start = -3
417 */
418 {F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L,
419 /* Child attempting write lock on byte 6 */
420 F_WRLCK, 0, 6L, 1L, WILLBLOCK},
421
422 /*
423 * #46 Parent making write lock from byte 2 to byte 3 with
424 * L_start = -2 and L_len = -2
425 */
426 {F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L,
427 /* Child attempting write lock on byte 1 */
428 F_WRLCK, 0, 1L, 1L, NOBLOCK},
429
430 /*
431 * #47 Parent making write lock from byte 2 to byte 3 with
432 * L_start = -2 and L_len = -2
433 */
434 {F_WRLCK, 1, -2L, -2L, SKIP, 0, 0L, 0L,
435 /* Child attempting write lock on byte 4 */
436 F_WRLCK, 0, 4L, 1L, NOBLOCK},
437
438 /*
439 * #48 Parent making write lock from byte 2 to byte 3 with
440 * L_start = -2 and L_len = -2
441 */
442 {F_WRLCK, 1, -2L, -2L, SKIP, 0, 0L, 0L,
443 /* Child attempting write lock on byte 2 */
444 F_WRLCK, 0, 2L, 1L, WILLBLOCK},
445
446 /*
447 * #49 Parent making write lock from byte 2 to byte 3 with
448 * L_start = -2 and L_len = -2
449 */
450 {F_WRLCK, 1, -2L, -2L, SKIP, 0, 0L, 0L,
451 /* Child attempting write lock on byte 3 */
452 F_WRLCK, 0, 3L, 1L, WILLBLOCK},
453
454 /*
455 * #50 Parent making write lock from byte 6 to byte 7 with
456 * L_start = 2 and L_len = -2
457 */
458 {F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L,
459 /* Child attempting write lock on byte 5 */
460 F_WRLCK, 0, 5L, 1L, NOBLOCK},
461
462 /*
463 * #51 Parent making write lock from byte 6 to byte 7 with
464 * L_start = 2 and L_len = -2
465 */
466 {F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L,
467 /* Child attempting write lock on byte 8 */
468 F_WRLCK, 0, 8L, 1L, NOBLOCK},
469
470 /*
471 * #52 Parent making write lock from byte 6 to byte 7 with
472 * L_start = 2 and L_len = -2
473 */
474 {F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L,
475 /* Child attempting write lock on byte 6 */
476 F_WRLCK, 0, 6L, 1L, WILLBLOCK},
477
478 /*
479 * #53 Parent making write lock from byte 6 to byte 7 with
480 * L_start = 2 and L_len = -2
481 */
482 {F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L,
483 /* Child attempting write lock on byte 7 */
484 F_WRLCK, 0, 7L, 1L, WILLBLOCK},
485
486 /*
487 * #54 Parent making write lock from byte 3 to byte 7 with
488 * L_start = 2 and L_len = -5
489 */
490 {F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L,
491 /* Child attempting write lock on byte 2 */
492 F_WRLCK, 0, 2L, 1L, NOBLOCK},
493
494 /*
495 * #55 Parent making write lock from byte 3 to byte 7 with
496 * L_start = 2 and L_len = -5
497 */
498 {F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L,
499 /* Child attempting write lock on byte 8 */
500 F_WRLCK, 0, 8L, 1L, NOBLOCK},
501
502 /*
503 * #56 Parent making write lock from byte 3 to byte 7 with
504 * L_start = 2 and L_len = -5
505 */
506 {F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L,
507 /* Child attempting write lock on byte 3 */
508 F_WRLCK, 0, 3L, 1L, WILLBLOCK},
509
510 /*
511 * #57 Parent making write lock from byte 3 to byte 7 with
512 * L_start = 2 and L_len = -5
513 */
514 {F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L,
515 /* Child attempting write lock on byte 5 */
516 F_WRLCK, 0, 5L, 1L, WILLBLOCK},
517
518 /*
519 * #58 Parent making write lock from byte 3 to byte 7 with
520 * L_start = 2 and L_len = -5
521 */
522 {F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L,
523 /* Child attempting write lock on byte 7 */
524 F_WRLCK, 0, 7L, 1L, WILLBLOCK},
525
526 /* Test case for block 4 */
527 /* #59 Parent making write lock on entire file */
528 {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},
531 };
532
533 static testcase *thiscase;
534 static struct flock flock;
535 static int parent, child, status, fail = 0;
536 static int got1 = 0;
537 static int fd;
538 static int test;
539 static char tmpname[40];
540
541 #define FILEDATA "ten bytes!"
542
543 void catch1(int sig);
544 void catch_alarm(int sig);
545
546 char *TCID = "fcntl14";
547 int TST_TOTAL = 1;
548 int NO_NFS = 1;
549
cleanup(void)550 void cleanup(void)
551 {
552 tst_rmdir();
553 }
554
setup(void)555 void setup(void)
556 {
557 struct sigaction act;
558
559 tst_sig(FORK, DEF_HANDLER, cleanup);
560 signal(SIGHUP, SIG_IGN);
561 umask(0);
562 TEST_PAUSE;
563 tst_tmpdir();
564 parent = getpid();
565
566 sprintf(tmpname, "fcntl2.%d", parent);
567
568 /* setup signal handler for signal from child */
569 memset(&act, 0, sizeof(act));
570 act.sa_handler = catch1;
571 sigemptyset(&act.sa_mask);
572 sigaddset(&act.sa_mask, SIGUSR1);
573 if ((sigaction(SIGUSR1, &act, NULL)) < 0) {
574 tst_resm(TFAIL, "SIGUSR1 signal setup failed, errno = %d",
575 errno);
576 cleanup();
577 }
578
579 memset(&act, 0, sizeof(act));
580 act.sa_handler = catch_alarm;
581 sigemptyset(&act.sa_mask);
582 sigaddset(&act.sa_mask, SIGALRM);
583 if ((sigaction(SIGALRM, &act, NULL)) < 0) {
584 tst_resm(TFAIL, "SIGALRM signal setup failed");
585 cleanup();
586 }
587 }
588
wake_parent(void)589 void wake_parent(void)
590 {
591 if ((kill(parent, SIGUSR1)) < 0) {
592 tst_resm(TFAIL, "Attempt to send signal to parent " "failed");
593 tst_resm(TFAIL, "Test case %d, errno = %d", test + 1, errno);
594 fail = 1;
595 }
596 }
597
do_usleep_child(void)598 void do_usleep_child(void)
599 {
600 usleep(100000); /* XXX how long is long enough? */
601 wake_parent();
602 exit(0);
603 }
604
dochild(void)605 void dochild(void)
606 {
607 int rc;
608 pid_t pid;
609
610 flock.l_type = thiscase->c_type;
611 flock.l_whence = thiscase->c_whence;
612 flock.l_start = thiscase->c_start;
613 flock.l_len = thiscase->c_len;
614 flock.l_pid = 0;
615 fail = 0;
616
617 /*
618 * Check to see if child lock will succeed. If it will, FLOCK
619 * structure will return with l_type changed to F_UNLCK. If it will
620 * not, the parent pid will be returned in l_pid and the type of
621 * lock that will block it in l_type.
622 */
623 if ((rc = fcntl(fd, F_GETLK, &flock)) < 0) {
624 tst_resm(TFAIL, "Attempt to check lock status failed");
625 tst_resm(TFAIL, "Test case %d, errno = %d", test + 1, errno);
626 fail = 1;
627 } else {
628
629 if ((thiscase->c_flag) == NOBLOCK) {
630 if (flock.l_type != F_UNLCK) {
631 tst_resm(TFAIL,
632 "Test case %d, GETLK: type = %d, "
633 "%d was expected", test + 1,
634 flock.l_type, F_UNLCK);
635 fail = 1;
636 }
637
638 if (flock.l_whence != thiscase->c_whence) {
639 tst_resm(TFAIL,
640 "Test case %d, GETLK: whence = %d, "
641 "should have remained %d", test + 1,
642 flock.l_whence, thiscase->c_whence);
643 fail = 1;
644 }
645
646 if (flock.l_start != thiscase->c_start) {
647 tst_resm(TFAIL,
648 "Test case %d, GETLK: start = %" PRId64
649 ", " "should have remained %" PRId64,
650 test + 1, (int64_t) flock.l_start,
651 (int64_t) thiscase->c_start);
652 fail = 1;
653 }
654
655 if (flock.l_len != thiscase->c_len) {
656 tst_resm(TFAIL,
657 "Test case %d, GETLK: len = %" PRId64
658 ", " "should have remained %" PRId64,
659 test + 1, (int64_t) flock.l_len,
660 (int64_t) thiscase->c_len);
661 fail = 1;
662 }
663
664 if (flock.l_pid != 0) {
665 tst_resm(TFAIL,
666 "Test case %d, GETLK: pid = %d, "
667 "should have remained 0", test + 1,
668 flock.l_pid);
669 fail = 1;
670 }
671 } else {
672 if (flock.l_pid != parent) {
673 tst_resm(TFAIL,
674 "Test case %d, GETLK: pid = %d, "
675 "should be parent's id of %d",
676 test + 1, flock.l_pid, parent);
677 fail = 1;
678 }
679
680 if (flock.l_type != thiscase->a_type) {
681 tst_resm(TFAIL,
682 "Test case %d, GETLK: type = %d, "
683 "should be parent's first lock type of %d",
684 test + 1, flock.l_type,
685 thiscase->a_type);
686 fail = 1;
687 }
688 }
689 }
690
691 /*
692 * now try to set the lock, nonblocking
693 * This will succeed for NOBLOCK,
694 * fail for WILLBLOCK
695 */
696 flock.l_type = thiscase->c_type;
697 flock.l_whence = thiscase->c_whence;
698 flock.l_start = thiscase->c_start;
699 flock.l_len = thiscase->c_len;
700 flock.l_pid = 0;
701
702 if ((rc = fcntl(fd, F_SETLK, &flock)) < 0) {
703 if ((thiscase->c_flag) == NOBLOCK) {
704 tst_resm(TFAIL, "Attempt to set child NONBLOCKING "
705 "lock failed");
706 tst_resm(TFAIL, "Test case %d, errno = %d",
707 test + 1, errno);
708 fail = 1;
709 }
710 }
711
712 if ((thiscase->c_flag) == WILLBLOCK) {
713 if (rc != -1 || (errno != EACCES && errno != EAGAIN)) {
714 tst_resm(TFAIL,
715 "SETLK: rc = %d, errno = %d, -1/EAGAIN or EACCES "
716 "was expected", rc, errno);
717 fail = 1;
718 }
719 if (rc == 0) {
720 /* accidentally got the lock */
721 /* XXX how to clean up? */
722 (void)fcntl(fd, F_UNLCK, &flock);
723 }
724 /*
725 * Lock should succeed after blocking and parent releases
726 * lock, tell the parent to release the locks.
727 * Do the lock in this process, send the signal in a child
728 * process, so that the SETLKW actually uses the blocking
729 * mechanism in the kernel.
730 *
731 * XXX inherent race: we want to wait until the
732 * F_SETLKW has started, but we don't have a way to
733 * check that reliably in the child. (We'd
734 * need some way to have fcntl() atomically unblock a
735 * signal and wait for the lock.)
736 */
737 pid = tst_fork();
738 switch (pid) {
739 case -1:
740 tst_resm(TFAIL, "Fork failed");
741 fail = 1;
742 break;
743 case 0:
744 do_usleep_child();
745 break;
746
747 default:
748 if ((rc = fcntl(fd, F_SETLKW, &flock)) < 0) {
749 tst_resm(TFAIL, "Attempt to set child BLOCKING "
750 "lock failed");
751 tst_resm(TFAIL, "Test case %d, errno = %d",
752 test + 1, errno);
753 fail = 1;
754 }
755 waitpid(pid, &status, 0);
756 break;
757 }
758 }
759 if (fail) {
760 exit(1);
761 } else {
762 exit(0);
763 }
764 }
765
run_test(int file_flag,int file_mode,int seek,int start,int end)766 void run_test(int file_flag, int file_mode, int seek, int start, int end)
767 {
768 fail = 0;
769
770 for (test = start; test < end; test++) {
771 fd = SAFE_OPEN(cleanup, tmpname, file_flag, file_mode);
772
773 if (write(fd, FILEDATA, 10) < 0)
774 tst_brkm(TBROK, cleanup, "write() failed");
775
776 if (seek) {
777 SAFE_LSEEK(cleanup, fd, seek, 0);
778 }
779
780 thiscase = &testcases[test];
781 flock.l_type = thiscase->a_type;
782 flock.l_whence = thiscase->a_whence;
783 flock.l_start = thiscase->a_start;
784 flock.l_len = thiscase->a_len;
785
786 /* set the initial parent lock on the file */
787 if ((fcntl(fd, F_SETLK, &flock)) < 0) {
788 tst_resm(TFAIL, "First parent lock failed");
789 tst_resm(TFAIL, "Test case %d, errno = %d",
790 test + 1, errno);
791 fail = 1;
792 }
793
794 if ((thiscase->b_type) != SKIP) {
795 flock.l_type = thiscase->b_type;
796 flock.l_whence = thiscase->b_whence;
797 flock.l_start = thiscase->b_start;
798 flock.l_len = thiscase->b_len;
799
800 /* set the second parent lock */
801 if ((fcntl(fd, F_SETLK, &flock)) < 0) {
802 tst_resm(TFAIL, "Second parent lock failed");
803 tst_resm(TFAIL, "Test case %d, errno = %d",
804 test + 1, errno);
805 fail = 1;
806 }
807 }
808 if ((thiscase->c_type) == SKIP) {
809 close(fd);
810 tst_resm(TINFO, "skipping test %d", test + 1);
811 continue;
812 }
813
814 /* Mask SIG_USR1 before forking child, to avoid race */
815 (void)sighold(SIGUSR1);
816
817 /* flush the stdout to avoid garbled output */
818 fflush(stdout);
819
820 if ((child = tst_fork()) == 0)
821 dochild();
822
823 if (child < 0)
824 tst_brkm(TBROK|TERRNO, cleanup, "Fork failed");
825
826 if ((thiscase->c_flag) == WILLBLOCK) {
827 /*
828 * Wait for a signal from the child then remove
829 * blocking lock. Set a 60 second alarm to break the
830 * pause just in case the child never signals us.
831 */
832 alarm(TIME_OUT);
833 sigpause(SIGUSR1);
834
835 /* turn off the alarm timer */
836 alarm((unsigned)0);
837 if (got1 != 1)
838 tst_resm(TINFO, "Pause terminated without "
839 "signal SIGUSR1 from child");
840 got1 = 0;
841
842 /*
843 * setup lock structure for parent to delete
844 * blocking lock then wait for child to exit
845 */
846 flock.l_type = F_UNLCK;
847 flock.l_whence = 0;
848 flock.l_start = 0L;
849 flock.l_len = 0L;
850 if ((fcntl(fd, F_SETLK, &flock)) < 0) {
851 tst_resm(TFAIL, "Attempt to release parent "
852 "lock failed");
853 tst_resm(TFAIL, "Test case %d, errno = %d",
854 test + 1, errno);
855 fail = 1;
856 }
857 }
858 /*
859 * set a 60 second alarm to break the wait just in case the
860 * child doesn't terminate on its own accord
861 */
862 alarm(TIME_OUT);
863
864 /* wait for the child to terminate and close the file */
865 waitpid(child, &status, 0);
866 /* turn off the alarm clock */
867 alarm((unsigned)0);
868 if (status != 0) {
869 tst_resm(TFAIL, "tchild returned status 0x%x", status);
870 fail = 1;
871 }
872 close(fd);
873 if (fail)
874 tst_resm(TFAIL, "testcase:%d FAILED", test + 1);
875 else
876 tst_resm(TPASS, "testcase:%d PASSED", test + 1);
877 }
878 unlink(tmpname);
879 }
880
catch_alarm(int sig)881 void catch_alarm(int sig)
882 {
883 /*
884 * Timer has runout and child has not signaled, need
885 * to kill off the child as it appears it will not
886 * on its own accord. Check that it is still around
887 * as it may have terminated abnormally while parent
888 * was waiting for SIGUSR1 signal from it.
889 */
890 if (kill(child, 0) == 0) {
891 kill(child, SIGKILL);
892 perror("The child didnot terminate on its own accord");
893 }
894 }
895
catch1(int sig)896 void catch1(int sig)
897 {
898 struct sigaction act;
899
900 /*
901 * Set flag to let parent know that child is ready to have lock
902 * removed
903 */
904 memset(&act, 0, sizeof(act));
905 act.sa_handler = catch1;
906 sigemptyset(&act.sa_mask);
907 sigaddset(&act.sa_mask, SIGUSR1);
908 sigaction(SIGUSR1, &act, NULL);
909 got1++;
910 }
911
testcheck_end(int check_fail,char * msg)912 static void testcheck_end(int check_fail, char *msg)
913 {
914 if (check_fail)
915 tst_resm(TFAIL, "%s FAILED", msg);
916 else
917 tst_resm(TPASS, "%s PASSED", msg);
918 }
919
main(int ac,char ** av)920 int main(int ac, char **av)
921 {
922 int lc;
923
924 tst_parse_opts(ac, av, NULL, NULL);
925
926 setup();
927
928 if (tst_fs_type(cleanup, ".") == TST_NFS_MAGIC)
929 NO_NFS = 0;
930
931 for (lc = 0; TEST_LOOPING(lc); lc++) {
932 tst_count = 0;
933
934 /* //block1: */
935 tst_resm(TINFO, "Enter block 1: without mandatory locking");
936 fail = 0;
937 /*
938 * try various file locks on an ordinary file without
939 * mandatory locking
940 */
941 (void)run_test(O_CREAT | O_RDWR | O_TRUNC, 0777, 0, 0, 36);
942 testcheck_end(fail, "Block 1, test 1");
943
944 /* Now try with negative values for L_start and L_len */
945 (void)run_test(O_CREAT | O_RDWR | O_TRUNC, 0777, 5, 36, 45);
946 testcheck_end(fail, "Block 1, test 2");
947
948 tst_resm(TINFO, "Exit block 1");
949
950 /* //block2: */
951 /*
952 * Skip block2 if test on NFS, since NFS does not support
953 * mandatory locking
954 */
955 tst_resm(TINFO, "Enter block 2: with mandatory locking");
956 if (NO_NFS) {
957 fail = 0;
958 /*
959 * Try various locks on a file with mandatory
960 * record locking this should behave the same
961 * as an ordinary file
962 */
963 (void)run_test(O_CREAT | O_RDWR | O_TRUNC,
964 S_ENFMT | S_IRUSR | S_IWUSR, 0, 0, 36);
965 testcheck_end(fail, "Block 2, test 1");
966
967 /* Now try negative values for L_start and L_len */
968 (void)run_test(O_CREAT | O_RDWR | O_TRUNC,
969 S_ENFMT | S_IRUSR | S_IWUSR, 5, 36, 45);
970 testcheck_end(fail, "Block 2, test 2");
971 } else {
972 tst_resm(TCONF, "Skip block 2 as NFS does not"
973 " support mandatory locking");
974 }
975
976 tst_resm(TINFO, "Exit block 2");
977
978 /* //block3: */
979 tst_resm(TINFO, "Enter block 3");
980 fail = 0;
981 /*
982 * Check that proper error status is returned when invalid
983 * argument used for WHENCE (negative value)
984 */
985
986 fd = SAFE_OPEN(cleanup, tmpname, O_CREAT | O_RDWR | O_TRUNC,
987 0777);
988
989 if (write(fd, FILEDATA, 10) < 0)
990 tst_brkm(TBROK, cleanup, "write failed");
991
992 flock.l_type = F_WRLCK;
993 flock.l_whence = -1;
994 flock.l_start = 0L;
995 flock.l_len = 0L;
996
997 if ((fcntl(fd, F_SETLK, &flock)) < 0) {
998 if (errno != EINVAL) {
999 tst_resm(TFAIL, "Expected %d got %d",
1000 EINVAL, errno);
1001 fail = 1;
1002 }
1003 } else {
1004 tst_resm(TFAIL, "Lock succeeded when it should have "
1005 "failed");
1006 fail = 1;
1007 }
1008
1009 close(fd);
1010 unlink(tmpname);
1011
1012 testcheck_end(fail, "Test with negative whence locking");
1013 tst_resm(TINFO, "Exit block 3");
1014
1015 /* //block4: */
1016 tst_resm(TINFO, "Enter block 4");
1017 fail = 0;
1018 /*
1019 * Check that a lock on end of file is still valid when
1020 * additional data is appended to end of file and a new
1021 * process attempts to lock new data
1022 */
1023 fd = SAFE_OPEN(cleanup, tmpname, O_CREAT | O_RDWR | O_TRUNC,
1024 0777);
1025
1026 if (write(fd, FILEDATA, 10) < 0)
1027 tst_brkm(TBROK, cleanup, "write failed");
1028
1029 thiscase = &testcases[58];
1030 flock.l_type = thiscase->a_type;
1031 flock.l_whence = thiscase->a_whence;
1032 flock.l_start = thiscase->a_start;
1033 flock.l_len = thiscase->a_len;
1034
1035 /* Set the initial parent lock on the file */
1036 if ((fcntl(fd, F_SETLK, &flock)) < 0) {
1037 tst_resm(TFAIL, "First parent lock failed");
1038 tst_resm(TFAIL, "Test case %d, errno = %d", 58, errno);
1039 fail = 1;
1040 }
1041
1042 /* Write some additional data to end of file */
1043 if (write(fd, FILEDATA, 10) < 0)
1044 tst_brkm(TBROK, cleanup, "write failed");
1045
1046 /* Mask signal to avoid race */
1047 if (sighold(SIGUSR1) < 0)
1048 tst_brkm(TBROK, cleanup, "sighold failed");
1049
1050 if ((child = tst_fork()) == 0)
1051 dochild();
1052
1053 if (child < 0)
1054 tst_brkm(TBROK|TERRNO, cleanup, "Fork failed");
1055
1056 /*
1057 * Wait for a signal from the child then remove blocking lock.
1058 * Set a 60 sec alarm to break the pause just in case the
1059 * child doesn't terminate on its own accord
1060 */
1061 (void)alarm(TIME_OUT);
1062
1063 (void)sigpause(SIGUSR1);
1064
1065 /* turn off the alarm timer */
1066 (void)alarm((unsigned)0);
1067 if (got1 != 1) {
1068 tst_resm(TINFO, "Pause terminated without signal "
1069 "SIGUSR1 from child");
1070 }
1071 got1 = 0;
1072
1073 /*
1074 * Set up lock structure for parent to delete
1075 * blocking lock then wait for child to exit
1076 */
1077 flock.l_type = F_UNLCK;
1078 flock.l_whence = 0;
1079 flock.l_start = 0L;
1080 flock.l_len = 0L;
1081 if ((fcntl(fd, F_SETLK, &flock)) < 0) {
1082 tst_resm(TFAIL, "Attempt to release parent lock "
1083 "failed");
1084 tst_resm(TFAIL, "Test case %d, errno = %d", test + 1,
1085 errno);
1086 fail = 1;
1087 }
1088
1089 /*
1090 * set a 60 sec alarm to break the wait just in case the
1091 * child doesn't terminate on its own accord
1092 */
1093 (void)alarm(TIME_OUT);
1094
1095 waitpid(child, &status, 0);
1096 if (WEXITSTATUS(status) != 0) {
1097 fail = 1;
1098 tst_resm(TFAIL, "child returned bad exit status");
1099 }
1100
1101 /* turn off the alarm clock */
1102 (void)alarm((unsigned)0);
1103 if (status != 0) {
1104 tst_resm(TFAIL, "child returned status 0x%x", status);
1105 fail = 1;
1106 }
1107 close(fd);
1108 unlink(tmpname);
1109
1110 testcheck_end(fail, "Test of locks on file");
1111 tst_resm(TINFO, "Exit block 4");
1112 }
1113 cleanup();
1114 tst_exit();
1115 }
1116