tools/testing/selftests/pidfd/pidfd_autoreap_test.c

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/pidfd/pidfd_autoreap_test.c
Extension
.c
Size
19970 bytes
Lines
901
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

struct cap_header {
	__u32 version;
	int pid;
};

struct cap_data {
	__u32 effective;
	__u32 permitted;
	__u32 inheritable;
};

static int drop_all_caps(void)
{
	struct cap_header hdr = { .version = _LINUX_CAPABILITY_VERSION_3 };
	struct cap_data data[2] = {};

	return syscall(__NR_capset, &hdr, data);
}

static pid_t create_autoreap_child(int *pidfd)
{
	struct __clone_args args = {
		.flags		= CLONE_PIDFD | CLONE_AUTOREAP,
		.exit_signal	= 0,
		.pidfd		= ptr_to_u64(pidfd),
	};

	return sys_clone3(&args, sizeof(args));
}

/*
 * Test that CLONE_AUTOREAP works without CLONE_PIDFD (fire-and-forget).
 */
TEST(autoreap_without_pidfd)
{
	struct __clone_args args = {
		.flags		= CLONE_AUTOREAP,
		.exit_signal	= 0,
	};
	pid_t pid;
	int ret;

	pid = sys_clone3(&args, sizeof(args));
	if (pid < 0 && errno == EINVAL)
		SKIP(return, "CLONE_AUTOREAP not supported");
	ASSERT_GE(pid, 0);

	if (pid == 0)
		_exit(0);

	/*
	 * Give the child a moment to exit and be autoreaped.
	 * Then verify no zombie remains.
	 */
	usleep(200000);
	ret = waitpid(pid, NULL, WNOHANG);
	ASSERT_EQ(ret, -1);
	ASSERT_EQ(errno, ECHILD);
}

/*
 * Test that CLONE_AUTOREAP with a non-zero exit_signal fails.
 */
TEST(autoreap_rejects_exit_signal)
{
	struct __clone_args args = {
		.flags		= CLONE_AUTOREAP,
		.exit_signal	= SIGCHLD,
	};
	pid_t pid;

	pid = sys_clone3(&args, sizeof(args));
	ASSERT_EQ(pid, -1);
	ASSERT_EQ(errno, EINVAL);
}

/*
 * Test that CLONE_AUTOREAP with CLONE_PARENT fails.
 */
TEST(autoreap_rejects_parent)
{
	struct __clone_args args = {
		.flags		= CLONE_AUTOREAP | CLONE_PARENT,
		.exit_signal	= 0,
	};
	pid_t pid;

	pid = sys_clone3(&args, sizeof(args));
	ASSERT_EQ(pid, -1);
	ASSERT_EQ(errno, EINVAL);

Annotation

Implementation Notes