xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/flock/flock06.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /* Copyright (c) Matthew Wilcox for Hewlett Packard 2003
3*49cdfc7eSAndroid Build Coastguard Worker  * Author: Matthew Wilcox
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
6*49cdfc7eSAndroid Build Coastguard Worker  *  This test verifies that flock locks held on one fd conflict with flock
7*49cdfc7eSAndroid Build Coastguard Worker  *  locks held on a different fd.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Test Steps:
10*49cdfc7eSAndroid Build Coastguard Worker  *  The process opens two file descriptors on the same file.  It acquires
11*49cdfc7eSAndroid Build Coastguard Worker  *  an exclusive flock on the first descriptor, checks that attempting to
12*49cdfc7eSAndroid Build Coastguard Worker  *  acquire an flock on the second descriptor fails.  Then it removes the
13*49cdfc7eSAndroid Build Coastguard Worker  *  first descriptor's lock and attempts to acquire an exclusive lock on
14*49cdfc7eSAndroid Build Coastguard Worker  *  the second descriptor.
15*49cdfc7eSAndroid Build Coastguard Worker  */
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/file.h>
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker 
verify_flock(void)22*49cdfc7eSAndroid Build Coastguard Worker static void verify_flock(void)
23*49cdfc7eSAndroid Build Coastguard Worker {
24*49cdfc7eSAndroid Build Coastguard Worker 	int fd1, fd2;
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker 	fd1 = SAFE_OPEN("testfile", O_RDWR);
27*49cdfc7eSAndroid Build Coastguard Worker 	TEST(flock(fd1, LOCK_EX | LOCK_NB));
28*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != 0)
29*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "First attempt to flock() failed");
30*49cdfc7eSAndroid Build Coastguard Worker 	else
31*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "First attempt to flock() passed");
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	fd2 = SAFE_OPEN("testfile", O_RDWR);
34*49cdfc7eSAndroid Build Coastguard Worker 	TEST(flock(fd2, LOCK_EX | LOCK_NB));
35*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1)
36*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "Second attempt to flock() denied");
37*49cdfc7eSAndroid Build Coastguard Worker 	else
38*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Second attempt to flock() succeeded!");
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	TEST(flock(fd1, LOCK_UN));
41*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != 0)
42*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "Failed to unlock fd1");
43*49cdfc7eSAndroid Build Coastguard Worker 	else
44*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Unlocked fd1");
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	TEST(flock(fd2, LOCK_EX | LOCK_NB));
47*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != 0)
48*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "Third attempt to flock() denied!");
49*49cdfc7eSAndroid Build Coastguard Worker 	else
50*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Third attempt to flock() succeeded");
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd1);
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd2);
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)56*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
57*49cdfc7eSAndroid Build Coastguard Worker {
58*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0666);
61*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
62*49cdfc7eSAndroid Build Coastguard Worker }
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
65*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_flock,
66*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
67*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
68*49cdfc7eSAndroid Build Coastguard Worker };
69