tools/testing/selftests/bpf/prog_tests/send_signal.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/send_signal.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/send_signal.c
Extension
.c
Size
7859 bytes
Lines
293
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 (remote) {
			sa.sa_sigaction = sigusr1_siginfo_handler;
			sa.sa_flags = SA_RESTART | SA_SIGINFO;
			ASSERT_NEQ(sigaction(SIGUSR1, &sa, NULL), -1, "sigaction");
		} else {
			ASSERT_NEQ(signal(SIGUSR1, sigusr1_handler), SIG_ERR, "signal");
		}

		close(pipe_c2p[0]); /* close read */
		close(pipe_p2c[1]); /* close write */

		/* boost with a high priority so we got a higher chance
		 * that if an interrupt happens, the underlying task
		 * is this process.
		 */
		if (!remote) {
			errno = 0;
			old_prio = getpriority(PRIO_PROCESS, 0);
			ASSERT_OK(errno, "getpriority");
			ASSERT_OK(setpriority(PRIO_PROCESS, 0, -20), "setpriority");
		}

		/* notify parent signal handler is installed */
		ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write");

		/* make sure parent enabled bpf program to send_signal */
		ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");

		/* wait a little for signal handler */
		for (int i = 0; i < 1000000000 && !sigusr1_received; i++) {
			j /= i + j + 1;
			if (remote)
				sleep(1);
			else
				if (!attr)
					/* trigger the nanosleep tracepoint program. */
					usleep(1);
		}

		buf[0] = sigusr1_received;

		ASSERT_EQ(sigusr1_received, 8, "sigusr1_received");
		ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write");

		/* wait for parent notification and exit */
		ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");

		/* restore the old priority */
		if (!remote)
			ASSERT_OK(setpriority(PRIO_PROCESS, 0, old_prio), "setpriority");

		close(pipe_c2p[1]);
		close(pipe_p2c[0]);
		exit(0);
	}

	close(pipe_c2p[1]); /* close write */
	close(pipe_p2c[0]); /* close read */

	skel = test_send_signal_kern__open_and_load();
	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
		goto skel_open_load_failure;

	/* boost with a high priority so we got a higher chance
	 * that if an interrupt happens, the underlying task
	 * is this process.
	 */
	if (remote) {
		errno = 0;
		old_prio = getpriority(PRIO_PROCESS, 0);
		ASSERT_OK(errno, "getpriority");
		ASSERT_OK(setpriority(PRIO_PROCESS, 0, -20), "setpriority");
	}

	if (!attr) {
		err = test_send_signal_kern__attach(skel);
		if (!ASSERT_OK(err, "skel_attach")) {
			err = -1;
			goto destroy_skel;
		}
	} else {
		if (!remote)
			pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1 /* cpu */,
					 -1 /* group id */, 0 /* flags */);
		else
			pmu_fd = syscall(__NR_perf_event_open, attr, getpid(), -1 /* cpu */,
					 -1 /* group id */, 0 /* flags */);
		if (!ASSERT_GE(pmu_fd, 0, "perf_event_open")) {
			err = -1;
			goto destroy_skel;

Annotation

Implementation Notes