xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pidfd_open/pidfd_open03.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  * This program opens the PID file descriptor of the child process created with
10  * fork(). It then uses poll to monitor the file descriptor for process exit, as
11  * indicated by an EPOLLIN event.
12  */
13 #include <poll.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 
17 #include "tst_test.h"
18 #include "lapi/pidfd.h"
19 
run(void)20 static void run(void)
21 {
22 	struct pollfd pollfd;
23 	int fd, ready;
24 	pid_t pid;
25 
26 	pid = SAFE_FORK();
27 
28 	if (!pid) {
29 		TST_CHECKPOINT_WAIT(0);
30 		exit(EXIT_SUCCESS);
31 	}
32 
33 	TST_EXP_FD_SILENT(pidfd_open(pid, 0), "pidfd_open(%d, 0)", pid);
34 
35 	fd = TST_RET;
36 
37 	TST_CHECKPOINT_WAKE(0);
38 
39 	pollfd.fd = fd;
40 	pollfd.events = POLLIN;
41 
42 	ready = poll(&pollfd, 1, -1);
43 
44 	SAFE_CLOSE(fd);
45 	SAFE_WAITPID(pid, NULL, 0);
46 
47 	if (ready != 1)
48 		tst_res(TFAIL, "poll() returned %d", ready);
49 	else
50 		tst_res(TPASS, "pidfd_open() passed");
51 }
52 
53 static struct tst_test test = {
54 	.setup = pidfd_open_supported,
55 	.test_all = run,
56 	.forks_child = 1,
57 	.needs_checkpoints = 1,
58 };
59