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