[LTP] [PATCH 1/6] api/epoll: Add safe epoll functions

Richard Palethorpe rpalethorpe@suse.com
Tue Sep 27 18:14:03 CEST 2022


Probably safe to use it over (p)select/(p)poll now.

Signed-off-by: Richard Palethorpe <rpalethorpe@suse.com>
---
 include/tst_epoll.h | 36 ++++++++++++++++++++
 lib/tst_epoll.c     | 81 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 117 insertions(+)
 create mode 100644 include/tst_epoll.h
 create mode 100644 lib/tst_epoll.c

diff --git a/include/tst_epoll.h b/include/tst_epoll.h
new file mode 100644
index 000000000..c5ffc07e3
--- /dev/null
+++ b/include/tst_epoll.h
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2022 SUSE LLC <rpalethorpe@suse.com>
+ */
+
+#include <sys/epoll.h>
+
+#ifndef TST_EPOLL_H
+#define TST_EPOLL_H
+
+typedef int (*tst_on_epoll_fn)(void *, uint32_t);
+struct tst_epoll_event_data {
+	tst_on_epoll_fn on_epoll;
+	void *self;
+};
+
+int safe_epoll_create1(const char *const file, const int lineno,
+		       const int flags);
+
+#define SAFE_EPOLL_CREATE1(flags) \
+	safe_epoll_create1(__FILE__, __LINE__, (flags))
+
+int safe_epoll_ctl(const char *const file, const int lineno,
+		   int epfd, int op, int fd, struct epoll_event *ev);
+
+#define SAFE_EPOLL_CTL(epfd, op, fd, ev) \
+	safe_epoll_ctl(__FILE__, __LINE__, epfd, op, fd, ev)
+
+int safe_epoll_wait(const char *const file, const int lineno,
+		    int epfd, struct epoll_event *events,
+		    int maxevents, int timeout);
+
+#define SAFE_EPOLL_WAIT(epfd, events, maxevents, timeout)\
+	safe_epoll_wait(__FILE__, __LINE__, epfd, events, maxevents, timeout)
+
+#endif
diff --git a/lib/tst_epoll.c b/lib/tst_epoll.c
new file mode 100644
index 000000000..556b3bdab
--- /dev/null
+++ b/lib/tst_epoll.c
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2022 SUSE LLC <rpalethorpe@suse.com>
+ */
+#define _GNU_SOURCE
+#define TST_NO_DEFAULT_MAIN
+
+#include "tst_test.h"
+#include "tst_epoll.h"
+
+int safe_epoll_create1(const char *const file, const int lineno,
+		       const int flags)
+{
+	const char *flags_str;
+	int ret = epoll_create1(flags);
+
+	switch (flags) {
+	case EPOLL_CLOEXEC:
+		flags_str = "EPOLL_CLOEXEC";
+		break;
+	case 0:
+		flags_str = "";
+		break;
+	default:
+		flags_str = "???";
+	}
+
+	if (ret == -1) {
+		tst_brk_(file, lineno,
+			 TBROK | TERRNO, "epoll_create1(%s)", flags_str);
+	}
+
+	return ret;
+}
+
+int safe_epoll_ctl(const char *const file, const int lineno,
+		   int epfd, int op, int fd, struct epoll_event *ev)
+{
+	const char *op_str;
+	int ret;
+
+	switch (op) {
+	case EPOLL_CTL_ADD:
+		op_str = "EPOLL_CTL_ADD";
+		break;
+	case EPOLL_CTL_DEL:
+		op_str = "EPOLL_CTL_DEL";
+		break;
+	case EPOLL_CTL_MOD:
+		op_str = "EPOLL_CTL_MOD";
+		break;
+	default:
+		op_str = "???";
+	}
+
+	ret = epoll_ctl(epfd, op, fd, ev);
+
+	if (ret == -1) {
+		tst_brk_(file, lineno,
+			 TBROK | TERRNO,
+			 "epoll_ctl(%d, %s, %d, ...", epfd, op_str, fd);
+	}
+
+	return ret;
+}
+
+int safe_epoll_wait(const char *const file, const int lineno,
+		    int epfd, struct epoll_event *events,
+		    int maxevents, int timeout)
+{
+	int ret = epoll_wait(epfd, events, maxevents, timeout);
+
+	if (ret == -1) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			 "epoll_wait(%d, ..., %d, %d)",
+			 epfd, maxevents, timeout);
+	}
+
+	return ret;
+}
+
-- 
2.36.1



More information about the ltp mailing list