xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/flock/flock03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2002
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Cyril Hrubis
5*49cdfc7eSAndroid Build Coastguard Worker  *
6*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
7*49cdfc7eSAndroid Build Coastguard Worker  *  This test verifies that flock() cannot unlock a file locked by another
8*49cdfc7eSAndroid Build Coastguard Worker  *  task.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Test Steps:
11*49cdfc7eSAndroid Build Coastguard Worker  *  Fork a child processes The parent flocks a file with LOCK_EX Child waits
12*49cdfc7eSAndroid Build Coastguard Worker  *  for that to happen, then checks to make sure it is locked.  Child then
13*49cdfc7eSAndroid Build Coastguard Worker  *  tries to unlock the file. If the unlock succeeds, the child attempts to
14*49cdfc7eSAndroid Build Coastguard Worker  *  lock the file with LOCK_EX. The test passes if the child is able to lock
15*49cdfc7eSAndroid Build Coastguard Worker  *  the file.
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/file.h>
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
childfunc(int fd)24*49cdfc7eSAndroid Build Coastguard Worker static void childfunc(int fd)
25*49cdfc7eSAndroid Build Coastguard Worker {
26*49cdfc7eSAndroid Build Coastguard Worker 	int fd2;
27*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAIT(0);
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	fd2 = SAFE_OPEN("testfile", O_RDWR);
30*49cdfc7eSAndroid Build Coastguard Worker 	if (flock(fd2, LOCK_EX | LOCK_NB) != -1)
31*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "CHILD: The file was not already locked");
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	TEST(flock(fd, LOCK_UN));
34*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
35*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "CHILD: Unable to unlock file locked by "
36*49cdfc7eSAndroid Build Coastguard Worker 			"parent: %s", tst_strerrno(TST_ERR));
37*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
38*49cdfc7eSAndroid Build Coastguard Worker 	} else {
39*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "CHILD: File locked by parent unlocked");
40*49cdfc7eSAndroid Build Coastguard Worker 	}
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	TEST(flock(fd2, LOCK_EX | LOCK_NB));
43*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
44*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "CHILD: Unable to unlock file after "
45*49cdfc7eSAndroid Build Coastguard Worker 			"unlocking: %s", tst_strerrno(TST_ERR));
46*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
47*49cdfc7eSAndroid Build Coastguard Worker 	} else {
48*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Locking after unlock passed");
49*49cdfc7eSAndroid Build Coastguard Worker 	}
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
52*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd2);
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	exit(0);
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker 
verify_flock(void)57*49cdfc7eSAndroid Build Coastguard Worker static void verify_flock(void)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker 	int fd1;
60*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	fd1 = SAFE_OPEN("testfile", O_RDWR);
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 	pid = SAFE_FORK();
65*49cdfc7eSAndroid Build Coastguard Worker 	if (pid == 0)
66*49cdfc7eSAndroid Build Coastguard Worker 		childfunc(fd1);
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	TEST(flock(fd1, LOCK_EX | LOCK_NB));
69*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != 0) {
70*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO,
71*49cdfc7eSAndroid Build Coastguard Worker 			"Parent: Initial attempt to flock() failed");
72*49cdfc7eSAndroid Build Coastguard Worker 	} else {
73*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS,
74*49cdfc7eSAndroid Build Coastguard Worker 			"Parent: Initial attempt to flock() passed");
75*49cdfc7eSAndroid Build Coastguard Worker 	}
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE(0);
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	tst_reap_children();
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd1);
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)84*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
85*49cdfc7eSAndroid Build Coastguard Worker {
86*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0666);
89*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
90*49cdfc7eSAndroid Build Coastguard Worker }
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
93*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_flock,
94*49cdfc7eSAndroid Build Coastguard Worker 	.needs_checkpoints = 1,
95*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
96*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
97*49cdfc7eSAndroid Build Coastguard Worker };
98