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