[LTP] [PATCH 3/5] fanotify05: Test reporting overflow event with FAN_REPORT_FD_ERROR

Amir Goldstein amir73il@gmail.com
Wed Jan 22 18:24:38 CET 2025


Expecting to get -EBADF instead of FAN_NOFD.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
---
 include/lapi/fanotify.h                        |  4 ++++
 testcases/kernel/syscalls/fanotify/fanotify.h  |  1 +
 .../kernel/syscalls/fanotify/fanotify05.c      | 18 ++++++++++++++++--
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/include/lapi/fanotify.h b/include/lapi/fanotify.h
index 424514625..40ea7ead7 100644
--- a/include/lapi/fanotify.h
+++ b/include/lapi/fanotify.h
@@ -32,6 +32,10 @@
 #define FAN_REPORT_DFID_NAME_TARGET (FAN_REPORT_DFID_NAME | \
 				     FAN_REPORT_FID | FAN_REPORT_TARGET_FID)
 #endif
+#ifndef FAN_REPORT_FD_ERROR
+#define FAN_REPORT_FD_ERROR	0x00002000
+#endif
+
 
 /* Non-uapi convenience macros */
 #ifndef FAN_REPORT_DFID_NAME_FID
diff --git a/testcases/kernel/syscalls/fanotify/fanotify.h b/testcases/kernel/syscalls/fanotify/fanotify.h
index 554940a7e..48a44cc7e 100644
--- a/testcases/kernel/syscalls/fanotify/fanotify.h
+++ b/testcases/kernel/syscalls/fanotify/fanotify.h
@@ -213,6 +213,7 @@ static inline int fanotify_mark_supported_on_fs(uint64_t flag, const char *fname
 
 #define TST_FANOTIFY_INIT_KNOWN_FLAGS                                      \
 	(FAN_REPORT_DFID_NAME_TARGET | FAN_REPORT_TID | FAN_REPORT_PIDFD | \
+	 FAN_REPORT_FD_ERROR | \
 	FAN_CLASS_NOTIF | FAN_CLASS_CONTENT | FAN_CLASS_PRE_CONTENT)
 
 /*
diff --git a/testcases/kernel/syscalls/fanotify/fanotify05.c b/testcases/kernel/syscalls/fanotify/fanotify05.c
index 12c240881..f1a132cbf 100644
--- a/testcases/kernel/syscalls/fanotify/fanotify05.c
+++ b/testcases/kernel/syscalls/fanotify/fanotify05.c
@@ -51,6 +51,10 @@ static struct tcase {
 		"Limited queue",
 		FAN_CLASS_NOTIF,
 	},
+	{
+		"Limited queue (FAN_REPORT_FD_ERROR)",
+		FAN_CLASS_NOTIF | FAN_REPORT_FD_ERROR,
+	},
 	{
 		"Unlimited queue",
 		FAN_CLASS_NOTIF | FAN_UNLIMITED_QUEUE,
@@ -63,6 +67,8 @@ static char symlnk[BUF_SIZE];
 static char fdpath[BUF_SIZE];
 static int fd, fd_notify;
 
+static int fd_error_unsupported;
+
 static struct fanotify_event_metadata event;
 
 static void event_res(struct fanotify_event_metadata *event, int i)
@@ -110,9 +116,15 @@ static void test_fanotify(unsigned int n)
 	int len, nevents = 0, got_overflow = 0;
 	int num_files = max_events + 1;
 	int expect_overflow = !(tc->init_flags & FAN_UNLIMITED_QUEUE);
+	int nofd_err = tc->init_flags & FAN_REPORT_FD_ERROR ? -EBADF : FAN_NOFD;
 
 	tst_res(TINFO, "Test #%d: %s", n, tc->tname);
 
+	if (fd_error_unsupported && (tc->init_flags & FAN_REPORT_FD_ERROR)) {
+		FANOTIFY_INIT_FLAGS_ERR_MSG(FAN_REPORT_FD_ERROR, fd_error_unsupported);
+		return;
+	}
+
 	fd_notify = SAFE_FANOTIFY_INIT(tc->init_flags | FAN_NONBLOCK, O_RDONLY);
 
 	SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_MOUNT | FAN_MARK_ADD, FAN_OPEN,
@@ -142,7 +154,7 @@ static void test_fanotify(unsigned int n)
 				tst_res(expect_overflow ? TFAIL : TPASS, "Overflow event not generated!\n");
 			break;
 		}
-		if (event.fd != FAN_NOFD) {
+		if (event.fd >= 0) {
 			/*
 			 * Verify that events generated on unique files
 			 * are received by the same order they were generated.
@@ -166,7 +178,7 @@ static void test_fanotify(unsigned int n)
 			break;
 		}
 		if (event.mask == FAN_Q_OVERFLOW) {
-			if (got_overflow || event.fd != FAN_NOFD) {
+			if (got_overflow || event.fd != nofd_err) {
 				tst_res(TFAIL,
 					"%s overflow event: mask=%llx pid=%u fd=%d",
 					got_overflow ? "unexpected" : "invalid",
@@ -193,6 +205,8 @@ static void setup(void)
 	fd = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY);
 	SAFE_CLOSE(fd);
 
+	fd_error_unsupported = fanotify_init_flags_supported_on_fs(FAN_REPORT_FD_ERROR, ".");
+
 	/* In older kernels this limit is fixed in kernel */
 	if (access(SYSFS_MAX_EVENTS, F_OK) && errno == ENOENT)
 		max_events = DEFAULT_MAX_EVENTS;
-- 
2.34.1



More information about the ltp mailing list