tools/testing/selftests/perf_events/remove_on_exec.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/perf_events/remove_on_exec.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/perf_events/remove_on_exec.c
Extension
.c
Size
6556 bytes
Lines
261
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 (pids[i] == 0) {
			execl("/proc/self/exe", "exec_child", NULL);
			_exit((perror("exec failed"), 1));
		}

		/* Some forked with event disabled, rest with enabled. */
		if (i > 10)
			EXPECT_EQ(ioctl(self->fd, PERF_EVENT_IOC_ENABLE, 0), 0);
	}

	usleep(100000); /* ... give time for event to trigger (in case of bug). */

	for (i = 0; i < sizeof(pids) / sizeof(pids[0]); i++) {
		/* All children should still be running. */
		EXPECT_EQ(waitpid(pids[i], &tmp, WNOHANG), 0);
		EXPECT_EQ(kill(pids[i], SIGKILL), 0);
	}

	/* Verify event is still alive. */
	tmp = signal_count;
	while (signal_count == tmp);
}

/* For exec'd child. */
static void exec_child(void)
{
	struct sigaction action = {};
	const int val = 42;

	/* Set up sigtrap handler in case we erroneously receive a trap. */
	action.sa_flags = SA_SIGINFO | SA_NODEFER;
	action.sa_sigaction = sigtrap_handler;
	sigemptyset(&action.sa_mask);
	if (sigaction(SIGTRAP, &action, NULL))
		_exit((perror("sigaction failed"), 1));

	/* Signal parent that we're starting to spin. */
	if (write(STDOUT_FILENO, &val, sizeof(int)) == -1)
		_exit((perror("write failed"), 1));

	/* Should hang here until killed. */
	while (!signal_count);
}

#define main test_main
TEST_HARNESS_MAIN
#undef main
int main(int argc, char *argv[])
{
	if (!strcmp(argv[0], "exec_child")) {
		exec_child();
		return 1;
	}

	return test_main(argc, argv);
}

Annotation

Implementation Notes