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