1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
4f08c3bdfSopenharmony_ci * Author: Vatsal Avasthi
5f08c3bdfSopenharmony_ci *
6f08c3bdfSopenharmony_ci * Test Description:
7f08c3bdfSopenharmony_ci *  1) flock() returns -1 and sets error number to EBADF if the file descriptor
8f08c3bdfSopenharmony_ci *     is invalid.
9f08c3bdfSopenharmony_ci *  2) flock() returns -1 and sets error number to EINVAL if the argument
10f08c3bdfSopenharmony_ci *     operation does not include LOCK_SH,LOCK_EX,LOCK_UN.
11f08c3bdfSopenharmony_ci *  3) flock() returns -1 and sets error number to EINVAL if an invalid
12f08c3bdfSopenharmony_ci *     combination of locking modes is used i.e LOCK_SH with LOCK_EX
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include <errno.h>
16f08c3bdfSopenharmony_ci#include <sys/file.h>
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#include "tst_test.h"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_cistatic int badfd = -1;
21f08c3bdfSopenharmony_cistatic int fd;
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic struct tcase {
24f08c3bdfSopenharmony_ci	int *fd;
25f08c3bdfSopenharmony_ci	int operation;
26f08c3bdfSopenharmony_ci	int exp_err;
27f08c3bdfSopenharmony_ci} tcases[] = {
28f08c3bdfSopenharmony_ci	{&badfd, LOCK_SH, EBADF},
29f08c3bdfSopenharmony_ci	{&fd, LOCK_NB, EINVAL},
30f08c3bdfSopenharmony_ci	{&fd, LOCK_SH | LOCK_EX, EINVAL},
31f08c3bdfSopenharmony_ci};
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic void verify_flock(unsigned n)
34f08c3bdfSopenharmony_ci{
35f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	fd = SAFE_OPEN("testfile", O_RDWR);
38f08c3bdfSopenharmony_ci	TEST(flock(*tc->fd, tc->operation));
39f08c3bdfSopenharmony_ci	if (TST_RET == 0) {
40f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "flock() succeeded unexpectedly");
41f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
42f08c3bdfSopenharmony_ci		return;
43f08c3bdfSopenharmony_ci	}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	if (tc->exp_err == TST_ERR) {
46f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO, "flock() failed expectedly");
47f08c3bdfSopenharmony_ci	} else {
48f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "flock() failed unexpectedly, "
49f08c3bdfSopenharmony_ci			"expected %s", tst_strerrno(tc->exp_err));
50f08c3bdfSopenharmony_ci	}
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
53f08c3bdfSopenharmony_ci}
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_cistatic void setup(void)
56f08c3bdfSopenharmony_ci{
57f08c3bdfSopenharmony_ci	int fd1;
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	fd1 = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0666);
60f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd1);
61f08c3bdfSopenharmony_ci}
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_cistatic struct tst_test test = {
64f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
65f08c3bdfSopenharmony_ci	.test = verify_flock,
66f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
67f08c3bdfSopenharmony_ci	.setup = setup,
68f08c3bdfSopenharmony_ci};
69