1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2021
4f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
5f08c3bdfSopenharmony_ci * 07/2001 Ported by Wayne Boyer
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Tests basic error handling of the fcntl syscall.
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * - EFAULT when lock is outside your accessible address space
14f08c3bdfSopenharmony_ci * - EINVAL when cmd argument is not recognized by this kernel
15f08c3bdfSopenharmony_ci * - EINVAL when cmd argument is F_SETLK and flock.l_whence is not equal to
16f08c3bdfSopenharmony_ci *   SEET_CUR,SEEK_SET,SEEK_END
17f08c3bdfSopenharmony_ci * - EBADF when fd refers to an invalid file descriptor
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#include <fcntl.h>
21f08c3bdfSopenharmony_ci#include "tst_test.h"
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#define F_BADCMD 999
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic struct flock flock;
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic struct tcase {
28f08c3bdfSopenharmony_ci	int fd;
29f08c3bdfSopenharmony_ci	int cmd;
30f08c3bdfSopenharmony_ci	struct flock *flock;
31f08c3bdfSopenharmony_ci	char *desc;
32f08c3bdfSopenharmony_ci	int exp_errno;
33f08c3bdfSopenharmony_ci} tcases[] = {
34f08c3bdfSopenharmony_ci	{1, F_SETLK, NULL, "F_SETLK", EFAULT},
35f08c3bdfSopenharmony_ci	{1, F_BADCMD, &flock, "F_BADCMD", EINVAL},
36f08c3bdfSopenharmony_ci	{1, F_SETLK, &flock,  "F_SETLK", EINVAL},
37f08c3bdfSopenharmony_ci	{-1, F_GETLK, &flock, "F_GETLK", EBADF}
38f08c3bdfSopenharmony_ci};
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_cistatic void verify_fcntl(unsigned int n)
41f08c3bdfSopenharmony_ci{
42f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	if (!tc->flock)
45f08c3bdfSopenharmony_ci		tc->flock = tst_get_bad_addr(NULL);
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	TST_EXP_FAIL2(fcntl(tc->fd, tc->cmd, tc->flock), tc->exp_errno,
48f08c3bdfSopenharmony_ci		"fcntl(%d, %s, flock)", tc->fd, tc->desc);
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cistatic void setup(void)
52f08c3bdfSopenharmony_ci{
53f08c3bdfSopenharmony_ci	flock.l_whence = -1;
54f08c3bdfSopenharmony_ci	flock.l_type = F_WRLCK;
55f08c3bdfSopenharmony_ci	flock.l_start = 0L;
56f08c3bdfSopenharmony_ci	flock.l_len = 0L;
57f08c3bdfSopenharmony_ci}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_cistatic struct tst_test test = {
60f08c3bdfSopenharmony_ci	.setup = setup,
61f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
62f08c3bdfSopenharmony_ci	.test = verify_fcntl,
63f08c3bdfSopenharmony_ci};
64