tools/testing/selftests/pidfd/pidfd_poll_test.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/pidfd/pidfd_poll_test.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/pidfd/pidfd_poll_test.c
Extension
.c
Size
2496 bytes
Lines
117
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (child_pid < 0) {
			if (errno == EAGAIN) {
				iter--;
				continue;
			}
			ksft_exit_fail_msg(
				"%s - failed to fork a child process\n",
				strerror(errno));
		}

		if (child_pid == 0) {
			/* Child process just sleeps for a min and exits */
			sleep(60);
			exit(EXIT_SUCCESS);
		}

		/* Parent kills the child and waits for its death */
		pidfd = sys_pidfd_open(child_pid, 0);
		if (pidfd < 0)
			ksft_exit_fail_msg("%s - pidfd_open failed\n",
					strerror(errno));

		/* Setup 3 sec alarm - plenty of time */
		if (signal(SIGALRM, handle_alarm) == SIG_ERR)
			ksft_exit_fail_msg("%s - signal failed\n",
					strerror(errno));
		alarm(3);

		/* Send SIGKILL to the child */
		if (sys_pidfd_send_signal(pidfd, SIGKILL, NULL, 0))
			ksft_exit_fail_msg("%s - pidfd_send_signal failed\n",
					strerror(errno));

		/* Wait for the death notification */
		fds.fd = pidfd;
		nevents = poll(&fds, 1, -1);

		/* Check for error conditions */
		if (nevents < 0)
			ksft_exit_fail_msg("%s - poll failed\n",
					strerror(errno));

		if (nevents != 1)
			ksft_exit_fail_msg("unexpected poll result: %d\n",
					nevents);

		if (!(fds.revents & POLLIN))
			ksft_exit_fail_msg(
				"unexpected event type received: 0x%x\n",
				fds.revents);

		if (timeout)
			ksft_exit_fail_msg(
				"death notification wait timeout\n");

		close(pidfd);
		/* Wait for child to prevent zombies */
		if (waitpid(child_pid, NULL, 0) < 0)
			ksft_exit_fail_msg("%s - waitpid failed\n",
					strerror(errno));

	}

	ksft_test_result_pass("pidfd poll test: pass\n");
	ksft_exit_pass();
}

Annotation

Implementation Notes