xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/flock/flock01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /* Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3*49cdfc7eSAndroid Build Coastguard Worker  * Author: Vatsal Avasthi
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
6*49cdfc7eSAndroid Build Coastguard Worker  *  This test verifies that flock() succeeds with all kind of locks.
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include <sys/file.h>
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker static int fd = -1;
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
17*49cdfc7eSAndroid Build Coastguard Worker 	int operation;
18*49cdfc7eSAndroid Build Coastguard Worker 	char *opt;
19*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
20*49cdfc7eSAndroid Build Coastguard Worker 	{LOCK_SH, "Shared Lock" },
21*49cdfc7eSAndroid Build Coastguard Worker 	{LOCK_UN, "Unlock"},
22*49cdfc7eSAndroid Build Coastguard Worker 	{LOCK_EX, "Exclusive Lock"},
23*49cdfc7eSAndroid Build Coastguard Worker };
24*49cdfc7eSAndroid Build Coastguard Worker 
verify_flock(unsigned n)25*49cdfc7eSAndroid Build Coastguard Worker static void verify_flock(unsigned n)
26*49cdfc7eSAndroid Build Coastguard Worker {
27*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	TEST(flock(fd, tc->operation));
30*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
31*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO,
32*49cdfc7eSAndroid Build Coastguard Worker 			"flock() failed to get %s", tc->opt);
33*49cdfc7eSAndroid Build Coastguard Worker 	} else {
34*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS,
35*49cdfc7eSAndroid Build Coastguard Worker 			"flock() succeeded with %s", tc->opt);
36*49cdfc7eSAndroid Build Coastguard Worker 	}
37*49cdfc7eSAndroid Build Coastguard Worker }
38*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)39*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0644);
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)44*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker 	if (fd >= 0)
47*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
51*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
52*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_flock,
53*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
54*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
55*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
56*49cdfc7eSAndroid Build Coastguard Worker };
57