1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/* Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3f08c3bdfSopenharmony_ci * Author: Vatsal Avasthi
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * Test Description:
6f08c3bdfSopenharmony_ci *  This test verifies that flock() behavior with different locking
7f08c3bdfSopenharmony_ci *  combinations along with LOCK_SH and LOCK_EX:
8f08c3bdfSopenharmony_ci *	1) flock() succeeded in acquiring shared lock on shared lock file.
9f08c3bdfSopenharmony_ci *	2) flock() failed to acquire exclusive lock on shared lock file.
10f08c3bdfSopenharmony_ci *	3) flock() failed to acquire shared lock on exclusive lock file.
11f08c3bdfSopenharmony_ci *	4) flock() failed to acquire exclusive lock on exclusive lock file.
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include <errno.h>
15f08c3bdfSopenharmony_ci#include <sys/file.h>
16f08c3bdfSopenharmony_ci#include <stdlib.h>
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#include "tst_test.h"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_cistatic struct tcase {
21f08c3bdfSopenharmony_ci	int operation;
22f08c3bdfSopenharmony_ci	char *f_lock;
23f08c3bdfSopenharmony_ci} tcases[] = {
24f08c3bdfSopenharmony_ci	{LOCK_SH, "shared lock"},
25f08c3bdfSopenharmony_ci	{LOCK_EX, "exclusive lock"},
26f08c3bdfSopenharmony_ci};
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_cistatic void child(int opt, int should_pass, char *lock)
29f08c3bdfSopenharmony_ci{
30f08c3bdfSopenharmony_ci	int retval, fd1;
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci	fd1 = SAFE_OPEN("testfile", O_RDWR);
33f08c3bdfSopenharmony_ci	retval = flock(fd1, opt);
34f08c3bdfSopenharmony_ci	if (should_pass) {
35f08c3bdfSopenharmony_ci		tst_res(retval == -1 ? TFAIL : TPASS,
36f08c3bdfSopenharmony_ci			" Child acquiring %s got %d", lock, retval);
37f08c3bdfSopenharmony_ci	} else {
38f08c3bdfSopenharmony_ci		tst_res(retval == -1 ? TPASS : TFAIL,
39f08c3bdfSopenharmony_ci			" Child acquiring %s got %d", lock, retval);
40f08c3bdfSopenharmony_ci	}
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd1);
43f08c3bdfSopenharmony_ci	exit(0);
44f08c3bdfSopenharmony_ci}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic void verify_flock(unsigned n)
47f08c3bdfSopenharmony_ci{
48f08c3bdfSopenharmony_ci	int fd2;
49f08c3bdfSopenharmony_ci	pid_t pid;
50f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	fd2 = SAFE_OPEN("testfile", O_RDWR);
53f08c3bdfSopenharmony_ci	TEST(flock(fd2, tc->operation));
54f08c3bdfSopenharmony_ci	if (TST_RET != 0) {
55f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "flock() failed to acquire %s",
56f08c3bdfSopenharmony_ci			tc->f_lock);
57f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd2);
58f08c3bdfSopenharmony_ci		return;
59f08c3bdfSopenharmony_ci	}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	tst_res(TPASS, "Parent had %s", tc->f_lock);
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	pid = SAFE_FORK();
64f08c3bdfSopenharmony_ci	if (pid == 0)
65f08c3bdfSopenharmony_ci		child(LOCK_SH | LOCK_NB, tc->operation & LOCK_SH, "shared lock");
66f08c3bdfSopenharmony_ci	else
67f08c3bdfSopenharmony_ci		tst_reap_children();
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	pid = SAFE_FORK();
70f08c3bdfSopenharmony_ci	if (pid == 0)
71f08c3bdfSopenharmony_ci		child(LOCK_EX | LOCK_NB, 0, "exclusive lock");
72f08c3bdfSopenharmony_ci	else
73f08c3bdfSopenharmony_ci		tst_reap_children();
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd2);
76f08c3bdfSopenharmony_ci}
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_cistatic void setup(void)
79f08c3bdfSopenharmony_ci{
80f08c3bdfSopenharmony_ci	int fd;
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	fd = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0644);
83f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
84f08c3bdfSopenharmony_ci}
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_cistatic struct tst_test test = {
87f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
88f08c3bdfSopenharmony_ci	.test = verify_flock,
89f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
90f08c3bdfSopenharmony_ci	.setup = setup,
91f08c3bdfSopenharmony_ci	.forks_child = 1,
92f08c3bdfSopenharmony_ci};
93