[LTP] [PATCH v3 2/2] poll: add test for POLLNVAL on invalid fd

Jinseok Kim always.starving0@gmail.com
Tue Feb 24 18:15:39 CET 2026


Add a poll() test to verify that POLLNVAL is reported for invalid file
descriptors.

The test closes one end of a pipe and passes the closed descriptor to
poll(). poll() is expected to return successfully and set POLLNVAL in
revents.

This verifies poll() handling of invalid file descriptors without
triggering an error return.

Signed-off-by: Jinseok Kim <always.starving0@gmail.com>
---
 runtest/syscalls                          |  1 +
 testcases/kernel/syscalls/poll/.gitignore |  1 +
 testcases/kernel/syscalls/poll/poll04.c   | 63 +++++++++++++++++++++++
 3 files changed, 65 insertions(+)
 create mode 100644 testcases/kernel/syscalls/poll/poll04.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 15df7c414..f937dd4c8 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1093,6 +1093,7 @@ pivot_root01 pivot_root01
 poll01 poll01
 poll02 poll02
 poll03 poll03
+poll04 poll04

 ppoll01 ppoll01

diff --git a/testcases/kernel/syscalls/poll/.gitignore b/testcases/kernel/syscalls/poll/.gitignore
index e3f66f4e6..7ba7262ec 100644
--- a/testcases/kernel/syscalls/poll/.gitignore
+++ b/testcases/kernel/syscalls/poll/.gitignore
@@ -1,3 +1,4 @@
 /poll01
 /poll02
 /poll03
+/poll04
diff --git a/testcases/kernel/syscalls/poll/poll04.c b/testcases/kernel/syscalls/poll/poll04.c
new file mode 100644
index 000000000..7645459b5
--- /dev/null
+++ b/testcases/kernel/syscalls/poll/poll04.c
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <always.starving0@gmail.com>
+ */
+
+/*\
+ * Check that poll() reports POLLNVAL for invalid file descriptors.
+ */
+#include <unistd.h>
+#include <errno.h>
+#include <sys/poll.h>
+
+#include "tst_test.h"
+
+static int fds[2];
+static int invalid_fd;
+
+void verify_pollnval(void)
+{
+	struct pollfd pfd = {
+		.fd = invalid_fd, .events = POLLIN,
+	};
+
+	TEST(poll(&pfd, 1, 0));
+
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "poll() failed");
+		return;
+	}
+
+	if (TST_RET != 1) {
+		tst_res(TFAIL, "Unexpected poll() return value %ld", TST_RET);
+		return;
+	}
+
+	TST_EXP_EXPR(pfd.revents & POLLNVAL);
+	TST_EXP_EXPR((pfd.revents & ~POLLNVAL) == 0);
+
+	tst_res(TPASS, "poll() reported POLLNVAL");
+}
+
+static void setup(void)
+{
+	SAFE_PIPE(fds);
+
+	invalid_fd = fds[0];
+	SAFE_CLOSE(fds[0]);
+}
+
+static void cleanup(void)
+{
+	if (fds[0] > 0)
+		SAFE_CLOSE(fds[0]);
+
+	if (fds[1] > 0)
+		SAFE_CLOSE(fds[1]);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_pollnval,
+};
--
2.43.0


More information about the ltp mailing list