xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pidfd_open/pidfd_open01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2020 Viresh Kumar <[email protected]>
4  */
5 
6 /*\
7  * [Description]
8  *
9  * Basic pidfd_open() test:
10  *
11  * - Fetch the PID of the current process and try to get its file descriptor.
12  * - Check that the close-on-exec flag is set on the file descriptor.
13  */
14 
15 #include <unistd.h>
16 #include "tst_test.h"
17 #include "lapi/pidfd.h"
18 
19 static int pidfd = -1;
20 
run(void)21 static void run(void)
22 {
23 	int flag;
24 
25 	TST_EXP_FD_SILENT(pidfd_open(getpid(), 0), "pidfd_open(getpid(), 0)");
26 
27 	pidfd = TST_RET;
28 	flag = SAFE_FCNTL(pidfd, F_GETFD);
29 
30 	SAFE_CLOSE(pidfd);
31 
32 	if (!(flag & FD_CLOEXEC))
33 		tst_brk(TFAIL, "pidfd_open(getpid(), 0) didn't set close-on-exec flag");
34 
35 	tst_res(TPASS, "pidfd_open(getpid(), 0) passed");
36 }
37 
cleanup(void)38 static void cleanup(void)
39 {
40 	if (pidfd > -1)
41 		SAFE_CLOSE(pidfd);
42 }
43 
44 static struct tst_test test = {
45 	.setup = pidfd_open_supported,
46 	.cleanup = cleanup,
47 	.test_all = run,
48 };
49