Change in libosmocore[master]: select.c: Introduce support for signalfd

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Fri Apr 17 19:41:18 UTC 2020


laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/17853 )


Change subject: select.c: Introduce support for signalfd
......................................................................

select.c: Introduce support for signalfd

The signalfd(2) mechanism of Linux allows signals to be delivered
and processed via normal file descriptor I/O.  This avoids any of the
usual problems about re-entrancy of signal processing, as signals can
be processed from the osmocom select() loop abstraction just like any
other event.

Change-Id: If8d89dd1f6989e1cd9b9367fad954d65f91ada30
---
M configure.ac
M include/osmocom/core/select.h
M src/select.c
3 files changed, 76 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/53/17853/1

diff --git a/configure.ac b/configure.ac
index 92457f1..352648b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -62,7 +62,7 @@
 
 dnl checks for header files
 AC_HEADER_STDC
-AC_CHECK_HEADERS(execinfo.h sys/select.h sys/socket.h sys/timerfd.h syslog.h ctype.h netinet/tcp.h netinet/in.h)
+AC_CHECK_HEADERS(execinfo.h sys/select.h sys/socket.h sys/signalfd.h sys/timerfd.h syslog.h ctype.h netinet/tcp.h netinet/in.h)
 # for src/conv.c
 AC_FUNC_ALLOCA
 AC_SEARCH_LIBS([dlopen], [dl dld], [LIBRARY_DLOPEN="$LIBS";LIBS=""])
diff --git a/include/osmocom/core/select.h b/include/osmocom/core/select.h
index 92904e2..bc60198 100644
--- a/include/osmocom/core/select.h
+++ b/include/osmocom/core/select.h
@@ -7,6 +7,7 @@
 #include <osmocom/core/linuxlist.h>
 #include <stdbool.h>
 #include <time.h>
+#include <signal.h>
 
 /*! \defgroup select Select loop abstraction
  *  @{
@@ -68,4 +69,21 @@
 			  const struct timespec *interval);
 int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned int), void *data);
 
+/* signalfd integration */
+struct osmo_signalfd;
+struct signalfd_siginfo;
+
+typedef void osmo_signalfd_cb(struct osmo_signalfd *osfd, const struct signalfd_siginfo *fdsi);
+
+struct osmo_signalfd {
+	struct osmo_fd ofd;
+	sigset_t sigset;
+	osmo_signalfd_cb *cb;
+	void *data;
+};
+
+struct osmo_signalfd *
+osmo_signalfd_setup(void *ctx, sigset_t set, osmo_signalfd_cb *cb, void *data);
+
+
 /*! @} */
diff --git a/src/select.c b/src/select.c
index b997122..5c10a40 100644
--- a/src/select.c
+++ b/src/select.c
@@ -392,6 +392,63 @@
 
 #endif /* HAVE_SYS_TIMERFD_H */
 
+#ifdef HAVE_SYS_SIGNALFD_H
+#include <sys/signalfd.h>
+
+static int signalfd_callback(struct osmo_fd *ofd, unsigned int what)
+{
+	struct osmo_signalfd *osfd = ofd->data;
+	struct signalfd_siginfo fdsi;
+	int rc;
+
+	rc = read(ofd->fd, &fdsi, sizeof(fdsi));
+	if (rc < 0) {
+		close(ofd->fd);
+		ofd->fd = -1;
+		return rc;
+	}
+
+	osfd->cb(osfd, &fdsi);
+
+	return 0;
+};
+
+/*! create a signalfd and register it with osmocom select loop.
+ *  \param[in] ctx talloc context from which osmo_signalfd is to be allocated
+ *  \param[in] set of signals to be accept via this file descriptor
+ *  \param[in] cb call-back function to be called for each arriving signal
+ *  \param[in] data opaque user-provided data to pass to callback
+ *  \returns pointer to newly-allocated + registered osmo_signalfd; NULL on error */
+struct osmo_signalfd *
+osmo_signalfd_setup(void *ctx, sigset_t set, osmo_signalfd_cb *cb, void *data)
+{
+	struct osmo_signalfd *osfd = talloc_size(ctx, sizeof(*osfd));
+	int fd, rc;
+
+	if (!osfd)
+		return NULL;
+
+	osfd->data = data;
+	osfd->sigset = set;
+	osfd->cb = cb;
+
+	fd = signalfd(-1, &osfd->sigset, O_NONBLOCK);
+	if (fd < 0) {
+		talloc_free(osfd);
+		return NULL;
+	}
+
+	osmo_fd_setup(&osfd->ofd, fd, OSMO_FD_READ, signalfd_callback, osfd, 0);
+	rc = osmo_fd_register(&osfd->ofd);
+	if (rc < 0) {
+		close(fd);
+		return NULL;
+	}
+
+	return osfd;
+}
+
+#endif /* HAVE_SYS_SIGNALFD_H */
 
 /*! @} */
 

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/17853
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: If8d89dd1f6989e1cd9b9367fad954d65f91ada30
Gerrit-Change-Number: 17853
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200417/48fdbd63/attachment.htm>


More information about the gerrit-log mailing list