• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org>
4  *
5  * Description:
6  * This program opens the PID file descriptor of the child process created with
7  * fork(). It then uses poll to monitor the file descriptor for process exit, as
8  * indicated by an EPOLLIN event.
9  */
10 #include <poll.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 
14 #include "tst_test.h"
15 #include "lapi/pidfd_open.h"
16 
run(void)17 static void run(void)
18 {
19 	struct pollfd pollfd;
20 	int fd, ready;
21 	pid_t pid;
22 
23 	pid = SAFE_FORK();
24 
25 	if (!pid) {
26 		TST_CHECKPOINT_WAIT(0);
27 		exit(EXIT_SUCCESS);
28 	}
29 
30 	TEST(pidfd_open(pid, 0));
31 
32 	fd = TST_RET;
33 	if (fd == -1)
34 		tst_brk(TFAIL | TTERRNO, "pidfd_open() failed");
35 
36 	TST_CHECKPOINT_WAKE(0);
37 
38 	pollfd.fd = fd;
39 	pollfd.events = POLLIN;
40 
41 	ready = poll(&pollfd, 1, -1);
42 
43 	SAFE_CLOSE(fd);
44 	SAFE_WAITPID(pid, NULL, 0);
45 
46 	if (ready != 1)
47 		tst_res(TFAIL, "poll() returned %d", ready);
48 	else
49 		tst_res(TPASS, "pidfd_open() passed");
50 }
51 
52 static struct tst_test test = {
53 	.min_kver = "5.3",
54 	.test_all = run,
55 	.forks_child = 1,
56 	.needs_checkpoints = 1,
57 };
58