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