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() succeeds with all kind of locks.
7f08c3bdfSopenharmony_ci */
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci#include <errno.h>
10f08c3bdfSopenharmony_ci#include <sys/file.h>
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#include "tst_test.h"
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_cistatic int fd = -1;
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_cistatic struct tcase {
17f08c3bdfSopenharmony_ci	int operation;
18f08c3bdfSopenharmony_ci	char *opt;
19f08c3bdfSopenharmony_ci} tcases[] = {
20f08c3bdfSopenharmony_ci	{LOCK_SH, "Shared Lock" },
21f08c3bdfSopenharmony_ci	{LOCK_UN, "Unlock"},
22f08c3bdfSopenharmony_ci	{LOCK_EX, "Exclusive Lock"},
23f08c3bdfSopenharmony_ci};
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic void verify_flock(unsigned n)
26f08c3bdfSopenharmony_ci{
27f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci	TEST(flock(fd, tc->operation));
30f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
31f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO,
32f08c3bdfSopenharmony_ci			"flock() failed to get %s", tc->opt);
33f08c3bdfSopenharmony_ci	} else {
34f08c3bdfSopenharmony_ci		tst_res(TPASS,
35f08c3bdfSopenharmony_ci			"flock() succeeded with %s", tc->opt);
36f08c3bdfSopenharmony_ci	}
37f08c3bdfSopenharmony_ci}
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic void setup(void)
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	fd = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0644);
42f08c3bdfSopenharmony_ci}
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_cistatic void cleanup(void)
45f08c3bdfSopenharmony_ci{
46f08c3bdfSopenharmony_ci	if (fd >= 0)
47f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
48f08c3bdfSopenharmony_ci}
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cistatic struct tst_test test = {
51f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
52f08c3bdfSopenharmony_ci	.test = verify_flock,
53f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
54f08c3bdfSopenharmony_ci	.setup = setup,
55f08c3bdfSopenharmony_ci	.cleanup = cleanup,
56f08c3bdfSopenharmony_ci};
57