[LTP] [PATCH 2/2] Add test for fanotify monitoring of tracing filesystem

Petr Vorel pvorel@suse.cz
Tue Feb 24 14:40:23 CET 2026


Hi Martin,

LGTM
Reviewed-by: Petr Vorel <pvorel@suse.cz>
...
> --- /dev/null
> +++ b/testcases/kernel/syscalls/fanotify/fanotify25.c
...
> +#define MNTPOINT "/sys/kernel/tracing"
> +#define EVENTS_SYSFILE MNTPOINT "/kprobe_events"
> +
> +static const struct traceconfig {
> +	const char *filename;
> +	const char *wdata;
> +} trace_cmds[] = {
> +	{EVENTS_SYSFILE, "p:ltp_load_module_0 load_module"},
> +	{MNTPOINT "/events/kprobes/ltp_load_module_0/enable", "1"},
> +	{MNTPOINT "/events/kprobes/ltp_load_module_0/enable", "0"},
> +	{EVENTS_SYSFILE, "-:ltp_load_module_0"},
> +	{}
> +};
> +
> +static int fan_fd = -1;
> +static int unmount_needed;
> +
> +static void setup(void)
> +{
> +	if (tst_fs_type(MNTPOINT) != TST_TRACEFS_MAGIC) {
> +		SAFE_MOUNT("tracefs", MNTPOINT, "tracefs",
> +			MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL);

I see possible mounting is the reason...

> +		unmount_needed = 1;
> +	}
> +
> +	if (access(EVENTS_SYSFILE, F_OK))
> +		tst_brk(TCONF, "Kprobe events not supported by kernel");

... why we cannot check /sys/kernel/tracing/kprobe_events presence via
.save_restore.

> +
> +	fan_fd = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF | FAN_NONBLOCK, O_RDONLY);
> +	SAFE_FANOTIFY_MARK(fan_fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_MODIFY,
> +		-1, MNTPOINT);
> +}
> +
> +static void do_child(void)
> +{
> +	int i, fd, events, ret;
> +	pid_t pid = getpid();
> +	struct fanotify_event_metadata buf;
> +
> +	for (i = 0, events = 0; trace_cmds[i].filename; i++) {
> +		fd = SAFE_OPEN(trace_cmds[i].filename, O_WRONLY, 0644);
> +		SAFE_WRITE(1, fd, trace_cmds[i].wdata,
> +			strlen(trace_cmds[i].wdata));
> +		SAFE_CLOSE(fd);
> +
> +		while ((ret = read(fan_fd, &buf, sizeof(buf))) > 0) {
> +			if (buf.pid != pid)
> +				continue;
> +
> +			if (!(buf.mask & FAN_MODIFY)) {
> +				tst_res(TFAIL, "Unexpected event %llx",
> +					buf.mask);
> +				continue;
> +			}
> +
> +			events++;
> +		}
> +
> +		if (ret < 0 && errno != EAGAIN)
> +			tst_res(TFAIL | TERRNO, "fanotify read() failed");
> +	}
> +
> +	if (events == i)
> +		tst_res(TPASS, "Received %d events", events);
> +	else
> +		tst_res(TFAIL, "Received %d events, expected %d", events, i);
> +}
OT: I guess we miss optional string arg for TST_EXP_EQ_*() macros to be usable.

> +
> +static void run(void)
> +{
> +	/*
> +	 * Fork a child to do the actual trace writes, otherwise tracefs
> +	 * would be busy until the current process exits and it would become
> +	 * impossible to unmount in cleanup().
> +	 */
+1

Kind regards,
Petr

> +	if (!SAFE_FORK()) {
> +		do_child();
> +		SAFE_CLOSE(fan_fd);
> +		exit(0);
> +	}
> +}
...


More information about the ltp mailing list