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