xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fcntl/fcntl27.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) International Business Machines  Corp., 2004
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) Bull S.A.S 2005-2006
5*49cdfc7eSAndroid Build Coastguard Worker  * Author: Jacky Malcles
6*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2024 SUSE LLC Andrea Manzini <[email protected]>
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Basic test for fcntl(2) using F_SETLEASE and F_RDLCK argument,
13*49cdfc7eSAndroid Build Coastguard Worker  * testing O_RDWR and O_WRONLY.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #define TC(x, y) .oflags = x, .mode = y, .desc = #x " with mode " #y,
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker static struct test_case
22*49cdfc7eSAndroid Build Coastguard Worker {
23*49cdfc7eSAndroid Build Coastguard Worker 	int oflags;
24*49cdfc7eSAndroid Build Coastguard Worker 	mode_t mode;
25*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
26*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
27*49cdfc7eSAndroid Build Coastguard Worker 	{ TC(O_RDWR | O_CREAT, 0777) },
28*49cdfc7eSAndroid Build Coastguard Worker 	{ TC(O_WRONLY | O_CREAT, 0222) },
29*49cdfc7eSAndroid Build Coastguard Worker };
30*49cdfc7eSAndroid Build Coastguard Worker 
verify_fcntl(unsigned int nr)31*49cdfc7eSAndroid Build Coastguard Worker static void verify_fcntl(unsigned int nr)
32*49cdfc7eSAndroid Build Coastguard Worker {
33*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case *tc = &tcases[nr];
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Testing %s", tc->desc);
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	int fd = SAFE_OPEN("testfile", tc->oflags, tc->mode);
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(fcntl(fd, F_SETLEASE, F_RDLCK), EAGAIN);
40*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
44*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_fcntl,
45*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
46*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
47*49cdfc7eSAndroid Build Coastguard Worker };
48