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