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