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