Skip to contentSkip to conversation

poll: add basic POLLHUP semantics test

PATCH Superseded Jinseok Kim 4 participants 19 messages Feb 19 – Feb 25

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 poll: add basic POLLHUP semantics test

Add a basic poll() test to verify that POLLHUP is reported when the peer end of a pipe is closed.

The test creates a pipe, closes the write end, and polls the read end for POLLIN events. poll() is expected to return successfully and set POLLHUP in revents.

This covers basic poll() lifecycle semantics that were not previously tested.

---
 testcases/kernel/syscalls/poll/poll03.c | 60 +++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
 create mode 100644 testcases/kernel/syscalls/poll/poll03.c
testcases/kernel/syscalls/poll/poll03.c new file +60 -0
new file mode 100644
index 000000000..01189eb70
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <[email protected]>
+ */
+
+/*
+ * Check that poll() reports POLLHUP on a pipe read end
+ * after the write end has been closed.
+ */
+#include <unistd.h>
+#include <errno.h>
+#include <sys/poll.h>
+
+#include "tst_test.h"
+
+static int fds[2];
+
+void verify_pollhup(void)
+{
+	struct pollfd pfd = {
+		.fd = fds[0], .events = POLLIN,
+	};
+
+	SAFE_CLOSE(fds[1]);
+	fds[1] = -1;
+
+	TEST(poll(&pfd, 1, -1));
+
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "poll() failed");
+		return;
+	}
+
+	if (!(pfd.revents & POLLHUP)) {
+		tst_res(TFAIL, "poll() did not report POLLHUP");
+		return;
+	}
+
+	tst_res(TPASS, "poll() reported POLLHUP");
+}
+
+static void setup(void)
+{
+	SAFE_PIPE(fds);
+}
+
+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_pollhup,
+};

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH 2/2 poll: add test for POLLNVAL on invalid fd

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.

---
 testcases/kernel/syscalls/poll/poll04.c | 60 +++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
 create mode 100644 testcases/kernel/syscalls/poll/poll04.c
testcases/kernel/syscalls/poll/poll04.c new file +60 -0
new file mode 100644
index 000000000..2013de642
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <[email protected]>
+ */
+
+/*
+ * 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];
+
+void verify_pollnval(void)
+{
+	int invalid_fd = fds[0];
+	struct pollfd pfd = {
+		.fd = invalid_fd, .events = POLLIN,
+	};
+
+	SAFE_CLOSE(fds[0]);
+	fds[0] = -1;
+
+	TEST(poll(&pfd, 1, 0));
+
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "poll() failed");
+		return;
+	}
+
+	if (!(pfd.revents & POLLNVAL)) {
+		tst_res(TFAIL, "poll() did not report POLLNVAL");
+		return;
+	}
+
+	tst_res(TPASS, "poll() reported POLLNVAL");
+}
+
+static void setup(void)
+{
+	SAFE_PIPE(fds);
+}
+
+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,
+};
\ No newline at end of file

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

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

Hi!

Quoted 22 lines

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.

---
 testcases/kernel/syscalls/poll/poll04.c | 60 +++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
 create mode 100644 testcases/kernel/syscalls/poll/poll04.c
testcases/kernel/syscalls/poll/poll04.c new file +0 -0
new file mode 100644
index 000000000..2013de642

testcases/kernel/syscalls/poll/poll04.c line 6

Quoted 6 lines
testcases/kernel/syscalls/poll/poll04.c new file +6 -0
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <[email protected]>
+ */
+
+/*

Here as well /*\

Quoted 9 lines
testcases/kernel/syscalls/poll/poll04.c new file +9 -0
+ * 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];

testcases/kernel/syscalls/poll/poll04.c line 25

Quoted 10 lines
testcases/kernel/syscalls/poll/poll04.c new file +10 -0
+
+void verify_pollnval(void)
+{
+	int invalid_fd = fds[0];
+	struct pollfd pfd = {
+		.fd = invalid_fd, .events = POLLIN,
+	};
+
+	SAFE_CLOSE(fds[0]);
+	fds[0] = -1;

Here as well, fds[0] is already set in the SAFE_CLOSE() macro.

Ah, and also this code will fail the test if we run it with -i 2 command line parameter.

I guess that we need to do in test setup()

- copy the value of fds[0] to different variable
- close the fds[0]

Moreover it may make sense to write the test so that the fd is closed while one thread is sleeping in the poll().

For that we would need to start a second thread that would wait for the verify_pollnval() to sleep in the kernel and then close the fd. We do have functions for that in the test library:

https://linux-test-project.readthedocs.io/en/latest/developers/api_c_tests.html#macro-tst-thread-state-wait

testcases/kernel/syscalls/poll/poll04.c line 32

Quoted 6 lines
testcases/kernel/syscalls/poll/poll04.c new file +6 -0
+	TEST(poll(&pfd, 1, 0));
+
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "poll() failed");
+		return;
+	}

Here as well, we should check that the return value was correct.

testcases/kernel/syscalls/poll/poll04.c line 37

Quoted 4 lines
testcases/kernel/syscalls/poll/poll04.c new file +4 -0
+	if (!(pfd.revents & POLLNVAL)) {
+		tst_res(TFAIL, "poll() did not report POLLNVAL");
+		return;
+	}

Here as well, we should check that only the flag we expected was set.

Quoted 5 lines
testcases/kernel/syscalls/poll/poll04.c new file +5 -0
+	tst_res(TPASS, "poll() reported POLLNVAL");
+}
+
+static void setup(void)
+{

testcases/kernel/syscalls/poll/poll04.c line 53

Quoted 10 lines
testcases/kernel/syscalls/poll/poll04.c new file +10 -0
+	SAFE_PIPE(fds);
+}
+
+static void cleanup(void)
+{
+	if (fds[0] >= 0)
+		SAFE_CLOSE(fds[0]);
+
+	if (fds[1] >= 0)
+		SAFE_CLOSE(fds[1]);

Here as well, either initialize fds or change the checks to >.

Quoted 13 lines
testcases/kernel/syscalls/poll/poll04.c new file +7 -1
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_pollnval,
+};
\ No newline at end of file
--

2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH v2 1/2 poll: add basic POLLHUP semantics test

Hi,

Thanks for the review.

I've added the return value check as suggested. Since TST_RET is a long, I used %ld in the message to match the type and avoid format warnings.

The suggestion about closing the fd while poll() is sleeping in the kernel makes sense as well. I'll keep this in mind and consider adding such a scenario as a follow-up test using the thread helpers you mentioned.

Best regards,
Jinseok.

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

PATCH v2 1/2 poll: add basic POLLHUP semantics test

In reply to Cyril Hrubis

Add a basic poll() test to verify that POLLHUP is reported when the peer end of a pipe is closed.

The test creates a pipe, closes the write end, and polls the read end for POLLIN events. poll() is expected to return successfully and set POLLHUP in revents.

This covers basic poll() lifecycle semantics that were not previously tested.

---
 testcases/kernel/syscalls/poll/poll03.c | 62 +++++++++++++++++++++++++
 1 file changed, 62 insertions(+)
 create mode 100644 testcases/kernel/syscalls/poll/poll03.c
testcases/kernel/syscalls/poll/poll03.c new file +62 -0
new file mode 100644
index 000000000..da90b2945
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <[email protected]>
+ */
+
+/*\
+ * Check that poll() reports POLLHUP on a pipe read end
+ * after the write end has been closed.
+ */
+#include <unistd.h>
+#include <errno.h>
+#include <sys/poll.h>
+
+#include "tst_test.h"
+
+static int fds[2];
+
+void verify_pollhup(void)
+{
+	struct pollfd pfd = {
+		.fd = fds[0], .events = POLLIN,
+	};
+
+	SAFE_CLOSE(fds[1]);
+
+	TEST(poll(&pfd, 1, -1));
+
+	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 & POLLHUP);
+	TST_EXP_EXPR((pfd.revents & ~POLLHUP) == 0);
+
+	tst_res(TPASS, "poll() reported POLLHUP");
+}
+
+static void setup(void)
+{
+	SAFE_PIPE(fds);
+}
+
+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_pollhup,
+};

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

PATCH v2 2/2 poll: add test for POLLNVAL on invalid fd

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.

---
 testcases/kernel/syscalls/poll/poll04.c | 63 +++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100644 testcases/kernel/syscalls/poll/poll04.c
testcases/kernel/syscalls/poll/poll04.c new file +63 -0
new file mode 100644
index 000000000..7645459b5
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <[email protected]>
+ */
+
+/*\
+ * 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

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

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

Hi!
This is missing the runtest entry in runtest/syscalls but otherwise it's fine.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

PATCH v3 1/2 poll: add basic POLLHUP semantics test

Add a basic poll() test to verify that POLLHUP is reported when the peer end of a pipe is closed.

The test creates a pipe, closes the write end, and polls the read end for POLLIN events. poll() is expected to return successfully and set POLLHUP in revents.

This covers basic poll() lifecycle semantics that were not previously tested.

---
 runtest/syscalls                          |  1 +
 testcases/kernel/syscalls/poll/.gitignore |  1 +
 testcases/kernel/syscalls/poll/poll03.c   | 59 +++++++++++++++++++++++
 3 files changed, 61 insertions(+)
 create mode 100644 testcases/kernel/syscalls/poll/poll03.c
runtest/syscalls +1 -0
index 05bb3ceb1..15df7c414 100644
@@ -1092,6 +1092,7 @@ pivot_root01 pivot_root01
 
 poll01 poll01
 poll02 poll02
+poll03 poll03
 
 ppoll01 ppoll01
 
testcases/kernel/syscalls/poll/.gitignore +1 -0
index 83be0ddf8..e3f66f4e6 100644
@@ -1,2 +1,3 @@
 /poll01
 /poll02
+/poll03
testcases/kernel/syscalls/poll/poll03.c new file +59 -0

59 lines not on this page · raw message

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

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

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.

---
 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
runtest/syscalls +1 -0
index 15df7c414..f937dd4c8 100644
@@ -1093,6 +1093,7 @@ pivot_root01 pivot_root01
 poll01 poll01
 poll02 poll02
 poll03 poll03
+poll04 poll04
 
 ppoll01 ppoll01
 
testcases/kernel/syscalls/poll/.gitignore +1 -0
index e3f66f4e6..7ba7262ec 100644
@@ -1,3 +1,4 @@
 /poll01
 /poll02
 /poll03
+/poll04
testcases/kernel/syscalls/poll/poll04.c new file +63 -0

63 lines not on this page · raw message

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v3 1/2
#10 lore raw
In reply to Jinseok Kim

Hi!

testcases/kernel/syscalls/poll/poll03.c line 32

Quoted 9 lines
testcases/kernel/syscalls/poll/poll03.c new file +9 -0
+	TEST(poll(&pfd, 1, -1));
+
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "poll() failed");
+		return;
+	} else if (TST_RET != 1) {
+		tst_res(TFAIL, "Unexpected poll() return value %ld", TST_RET);
+		return;
+	}

No need for the else here. We do return in the previous if () block.

Otherwise:

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw

PATCH v4 1/2 poll: add basic POLLHUP semantics test

Add a basic poll() test to verify that POLLHUP is reported when the peer end of a pipe is closed.

The test creates a pipe, closes the write end, and polls the read end for POLLIN events. poll() is expected to return successfully and set POLLHUP in revents.

This covers basic poll() lifecycle semantics that were not previously tested.

---
 runtest/syscalls                          |  1 +
 testcases/kernel/syscalls/poll/.gitignore |  1 +
 testcases/kernel/syscalls/poll/poll03.c   | 61 +++++++++++++++++++++++
 3 files changed, 63 insertions(+)
 create mode 100644 testcases/kernel/syscalls/poll/poll03.c
runtest/syscalls +1 -0
index 05bb3ceb1..15df7c414 100644
@@ -1092,6 +1092,7 @@ pivot_root01 pivot_root01
 
 poll01 poll01
 poll02 poll02
+poll03 poll03
 
 ppoll01 ppoll01
 
testcases/kernel/syscalls/poll/.gitignore +1 -0
index 83be0ddf8..e3f66f4e6 100644
@@ -1,2 +1,3 @@
 /poll01
 /poll02
+/poll03
testcases/kernel/syscalls/poll/poll03.c new file +61 -0

61 lines not on this page · raw message

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#12 lore raw

PATCH v4 2/2 poll: add test for POLLNVAL on invalid fd

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.

---
 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
runtest/syscalls +1 -0
index 15df7c414..f937dd4c8 100644
@@ -1093,6 +1093,7 @@ pivot_root01 pivot_root01
 poll01 poll01
 poll02 poll02
 poll03 poll03
+poll04 poll04
 
 ppoll01 ppoll01
 
testcases/kernel/syscalls/poll/.gitignore +1 -0
index e3f66f4e6..7ba7262ec 100644
@@ -1,3 +1,4 @@
 /poll01
 /poll02
 /poll03
+/poll04
testcases/kernel/syscalls/poll/poll04.c new file +63 -0

63 lines not on this page · raw message

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#15 lore raw
In reply to Jinseok Kim

From: Andrea Cervesato <[email protected]>

Quoted 8 lines · Jinseok Kim

Add a basic poll() test to verify that POLLHUP is reported when the peer end of a pipe is closed.

The test creates a pipe, closes the write end, and polls the read end for POLLIN events. poll() is expected to return successfully and set POLLHUP in revents.

[...]

Applied, thanks!

[1/2] poll: add basic POLLHUP semantics test

      commit: de9426e858f1de8fbe0a8c86676a193907c6ddce
[2/2] poll: add test for POLLNVAL on invalid fd
      commit: c7a45fc28c75092dd86340c5cbf0f8124509522d

Best regards,

Signature · 4 lines
Andrea Cervesato <[email protected]>

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#16 lore raw
In reply to Jinseok Kim

Hi!

Quoted 7 lines

Add a basic poll() test to verify that POLLHUP is reported when the peer end of a pipe is closed.

The test creates a pipe, closes the write end, and polls the read end for POLLIN events. poll() is expected to return successfully and set POLLHUP in revents.

Quoted 6 lines

This covers basic poll() lifecycle semantics that were not previously tested.

---
 testcases/kernel/syscalls/poll/poll03.c | 62 +++++++++++++++++++++++++

This is missing runtest entry in runtest/syscalls file.

testcases/kernel/syscalls/poll/poll03.c line 24

Quoted 7 lines
testcases/kernel/syscalls/poll/poll03.c new file +7 -0
+void verify_pollhup(void)
+{
+	struct pollfd pfd = {
+		.fd = fds[0], .events = POLLIN,
+	};
+
+	SAFE_CLOSE(fds[1]);

Doesn't this fail the test on a second iteration? Have you tried to run the test with -i 2?

The fd should be closed only once in the setup.

Quoted 39 lines
testcases/kernel/syscalls/poll/poll03.c new file +37 -1
+	TEST(poll(&pfd, 1, -1));
+
+	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 & POLLHUP);
+	TST_EXP_EXPR((pfd.revents & ~POLLHUP) == 0);
+
+	tst_res(TPASS, "poll() reported POLLHUP");
+}
+
+static void setup(void)
+{
+	SAFE_PIPE(fds);
+}
+
+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_pollhup,
+};
--

2.43.0

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#17 lore raw

PATCH v2 1/2 poll: add basic POLLHUP semantics test

Hi,

Thanks for the review.

You're right, I missed that. Sorry for the extra work.

Jinseok.

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#18 lore raw
In reply to Jinseok Kim

Hi!

Quoted 12 lines
---
 testcases/kernel/syscalls/poll/poll03.c | 60 +++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
 create mode 100644 testcases/kernel/syscalls/poll/poll03.c
testcases/kernel/syscalls/poll/poll03.c new file +0 -0
new file mode 100644
index 000000000..01189eb70

testcases/kernel/syscalls/poll/poll03.c line 6

Quoted 6 lines
testcases/kernel/syscalls/poll/poll03.c new file +6 -0
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <[email protected]>
+ */
+
+/*

This should be documentation comment which starts with /*\

Quoted 9 lines
testcases/kernel/syscalls/poll/poll03.c new file +9 -0
+ * Check that poll() reports POLLHUP on a pipe read end
+ * after the write end has been closed.
+ */
+#include <unistd.h>
+#include <errno.h>
+#include <sys/poll.h>
+
+#include "tst_test.h"
+

testcases/kernel/syscalls/poll/poll03.c line 25

Quoted 10 lines
testcases/kernel/syscalls/poll/poll03.c new file +10 -0
+static int fds[2];
+
+void verify_pollhup(void)
+{
+	struct pollfd pfd = {
+		.fd = fds[0], .events = POLLIN,
+	};
+
+	SAFE_CLOSE(fds[1]);
+	fds[1] = -1;

This is not needed, the SAFE_CLOSE() already sets the fd to -1.

testcases/kernel/syscalls/poll/poll03.c line 32

Quoted 6 lines
testcases/kernel/syscalls/poll/poll03.c new file +6 -0
+	TEST(poll(&pfd, 1, -1));
+
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "poll() failed");
+		return;
+	}

Here we should also check that we got correct return value. We expect to get one event so:

	if (TST_RES != 1) {
		tst_res(TFAIL, "Unexpected poll() return value %i", TST_RET);
		return;
	}

testcases/kernel/syscalls/poll/poll03.c line 37

Quoted 4 lines
testcases/kernel/syscalls/poll/poll03.c new file +4 -0
+	if (!(pfd.revents & POLLHUP)) {
+		tst_res(TFAIL, "poll() did not report POLLHUP");
+		return;
+	}

Here as well, if we do not expect other bits set in the revents we should also check that they are not set, something as:

	TST_EXP_EXPR(pfd.revents & POLLHUP);
	TST_EXP_EXPR((pfd.revents & ~POLLHUP) == 0);
Quoted 5 lines
testcases/kernel/syscalls/poll/poll03.c new file +5 -0
+	tst_res(TPASS, "poll() reported POLLHUP");
+}
+
+static void setup(void)
+{

testcases/kernel/syscalls/poll/poll03.c line 53

Quoted 10 lines
testcases/kernel/syscalls/poll/poll03.c new file +10 -0
+	SAFE_PIPE(fds);
+}
+
+static void cleanup(void)
+{
+	if (fds[0] >= 0)
+		SAFE_CLOSE(fds[0]);
+
+	if (fds[1] >= 0)
+		SAFE_CLOSE(fds[1]);

These ifs does not work. You either need to initialize the fds to -1 or check just fds[0] > 0 here. As it is you will close stdin if SAFE_PIPE() fails.

Quoted 12 lines
testcases/kernel/syscalls/poll/poll03.c new file +7 -1
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_pollhup,
+};
--

2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp