xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fcntl/fcntl29.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2014 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Xiaoguang Wang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2024 SUSE LLC Andrea Manzini <[email protected]>
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  * Basic test for fcntl(2) using F_DUPFD_CLOEXEC and getting FD_CLOEXEC.
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
15*49cdfc7eSAndroid Build Coastguard Worker #include <tst_test.h>
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker static int fd;
18*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)19*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
20*49cdfc7eSAndroid Build Coastguard Worker {
21*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_CREAT("testfile", 0644);
22*49cdfc7eSAndroid Build Coastguard Worker }
23*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)24*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
25*49cdfc7eSAndroid Build Coastguard Worker {
26*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
27*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
28*49cdfc7eSAndroid Build Coastguard Worker }
29*49cdfc7eSAndroid Build Coastguard Worker 
run(void)30*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
31*49cdfc7eSAndroid Build Coastguard Worker {
32*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FD(fcntl(fd, F_DUPFD_CLOEXEC, 0));
33*49cdfc7eSAndroid Build Coastguard Worker 	int dup_fd = TST_RET;
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_POSITIVE(fcntl(dup_fd, F_GETFD), "fcntl test F_GETFD");
36*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET & FD_CLOEXEC)
37*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "fcntl() set FD_CLOEXEC");
38*49cdfc7eSAndroid Build Coastguard Worker 	else
39*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "fcntl() did not set FD_CLOEXEC");
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(dup_fd);
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
45*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
46*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
47*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
48*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
49*49cdfc7eSAndroid Build Coastguard Worker };
50