[LTP] [PATCH v5] Add epoll_wait06 test

Andrea Cervesato andrea.cervesato@suse.de
Thu Aug 24 16:44:49 CEST 2023


From: Andrea Cervesato <andrea.cervesato@suse.com>

This test verifies that epoll EPOLLET feature works for both EPOLLIN and
EPOLLOUT.

Implements: https://github.com/linux-test-project/ltp/issues/860
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 runtest/syscalls                              |   1 +
 .../kernel/syscalls/epoll_wait/.gitignore     |   1 +
 .../kernel/syscalls/epoll_wait/epoll_wait06.c | 107 ++++++++++++++++++
 3 files changed, 109 insertions(+)
 create mode 100644 testcases/kernel/syscalls/epoll_wait/epoll_wait06.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 7af2842f3..9c13c7094 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -175,6 +175,7 @@ epoll_wait01 epoll_wait01
 epoll_wait02 epoll_wait02
 epoll_wait03 epoll_wait03
 epoll_wait04 epoll_wait04
+epoll_wait06 epoll_wait06
 epoll_pwait01 epoll_pwait01
 epoll_pwait02 epoll_pwait02
 epoll_pwait03 epoll_pwait03
diff --git a/testcases/kernel/syscalls/epoll_wait/.gitignore b/testcases/kernel/syscalls/epoll_wait/.gitignore
index 222955dd2..204949e85 100644
--- a/testcases/kernel/syscalls/epoll_wait/.gitignore
+++ b/testcases/kernel/syscalls/epoll_wait/.gitignore
@@ -2,3 +2,4 @@ epoll_wait01
 epoll_wait02
 epoll_wait03
 epoll_wait04
+epoll_wait06
diff --git a/testcases/kernel/syscalls/epoll_wait/epoll_wait06.c b/testcases/kernel/syscalls/epoll_wait/epoll_wait06.c
new file mode 100644
index 000000000..fa0bf650c
--- /dev/null
+++ b/testcases/kernel/syscalls/epoll_wait/epoll_wait06.c
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that edge triggering is correctly handled by epoll, for both EPOLLIN
+ * and EPOLLOUT.
+ *
+ * [Algorithm]
+ *
+ * 1. The file descriptor that represents the non-blocking read side of a pipe
+ *    (rfd) is registered on the epoll instance.
+ * 2. A pipe writer writes data on the write side of the pipe.
+ * 3. A call to epoll_wait() is done that will return a EPOLLIN event.
+ * 4. The pipe reader reads half of data from rfd.
+ * 5. A call to epoll_wait() should hang because there's data left to read.
+ * 6. The pipe reader reads remaining data from rfd.
+ * 7. A call to epoll_wait() should return a EPOLLOUT event.
+ */
+
+#define _GNU_SOURCE
+
+#include <fcntl.h>
+#include "tst_test.h"
+#include "tst_epoll.h"
+
+static size_t write_size;
+static size_t read_size;
+static int fds[2];
+static int epfd;
+
+static void setup(void)
+{
+	write_size = getpagesize();
+	read_size = write_size / 2;
+
+	SAFE_PIPE2(fds, O_NONBLOCK);
+
+	/* EPOLLOUT will be raised when buffer became empty after becoming full */
+	SAFE_FCNTL(fds[1], F_SETPIPE_SZ, write_size);
+}
+
+static void cleanup(void)
+{
+	if (epfd > 0)
+		SAFE_CLOSE(epfd);
+
+	if (fds[0] > 0)
+		SAFE_CLOSE(fds[0]);
+
+	if (fds[1] > 0)
+		SAFE_CLOSE(fds[1]);
+}
+
+static void run(void)
+{
+	char buff[write_size];
+	struct epoll_event evt_receive;
+
+	tst_res(TINFO, "Polling on channel with EPOLLET");
+
+	epfd = SAFE_EPOLL_CREATE1(0);
+
+	SAFE_EPOLL_CTL(epfd, EPOLL_CTL_ADD, fds[0], &((struct epoll_event) {
+		.events = EPOLLIN | EPOLLET,
+		.data.fd = fds[0],
+	}));
+	SAFE_EPOLL_CTL(epfd, EPOLL_CTL_ADD, fds[1], &((struct epoll_event) {
+		.events = EPOLLOUT | EPOLLET,
+		.data.fd = fds[1],
+	}));
+
+	tst_res(TINFO, "Write bytes on channel: %zu bytes", write_size);
+
+	memset(buff, 'a', write_size);
+	SAFE_WRITE(SAFE_WRITE_ANY, fds[1], buff, write_size);
+	TST_EXP_FAIL(write(fds[1], buff, write_size), EAGAIN, "write() failed");
+
+	TST_EXP_EQ_LI(SAFE_EPOLL_WAIT(epfd, &evt_receive, 1, 0), 1);
+	TST_EXP_EQ_LI(evt_receive.data.fd, fds[0]);
+	TST_EXP_EQ_LI(evt_receive.events & EPOLLIN, EPOLLIN);
+
+	tst_res(TINFO, "Read half bytes from channel: %zu bytes", read_size);
+
+	memset(buff, 0, write_size);
+	SAFE_READ(1, fds[0], buff, read_size);
+
+	TST_EXP_EQ_LI(SAFE_EPOLL_WAIT(epfd, &evt_receive, 1, 0), 0);
+
+	tst_res(TINFO, "Read remaining bytes from channel: %zu bytes", read_size);
+
+	SAFE_READ(1, fds[0], buff + read_size, read_size);
+	TST_EXP_FAIL(read(fds[0], buff, read_size), EAGAIN, "read() failed");
+
+	TST_EXP_EQ_LI(SAFE_EPOLL_WAIT(epfd, &evt_receive, 1, 0), 1);
+	TST_EXP_EQ_LI(evt_receive.data.fd, fds[1]);
+	TST_EXP_EQ_LI(evt_receive.events & EPOLLOUT, EPOLLOUT);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run,
+};
-- 
2.35.3



More information about the ltp mailing list