1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org>
4 *
5 * Description:
6 * Basic pidfd_open() test:
7 * 1) Fetch the PID of the current process and try to get its file descriptor.
8 * 2) Check that the close-on-exec flag is set on the file descriptor.
9 */
10
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include "tst_test.h"
14 #include "lapi/pidfd_open.h"
15
run(void)16 static void run(void)
17 {
18 int flag;
19
20 TEST(pidfd_open(getpid(), 0));
21
22 if (TST_RET == -1)
23 tst_brk(TFAIL | TTERRNO, "pidfd_open(getpid(), 0) failed");
24
25 flag = fcntl(TST_RET, F_GETFD);
26
27 SAFE_CLOSE(TST_RET);
28
29 if (flag == -1)
30 tst_brk(TFAIL | TERRNO, "fcntl(F_GETFD) failed");
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
38 static struct tst_test test = {
39 .min_kver = "5.3",
40 .test_all = run,
41 };
42