aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--configure.ac2
-rw-r--r--include/osmocom/core/select.h18
-rw-r--r--src/select.c59
3 files changed, 78 insertions, 1 deletions
diff --git a/configure.ac b/configure.ac
index 92457f16..352648b0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -62,7 +62,7 @@ AC_SUBST(LTLDFLAGS_OSMOCTRL)
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 92904e2f..bc601982 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 @@ int osmo_timerfd_schedule(struct osmo_fd *ofd, const struct timespec *first,
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 b997122e..f0c814b7 100644
--- a/src/select.c
+++ b/src/select.c
@@ -392,6 +392,65 @@ int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned
#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) {
+ osmo_fd_unregister(ofd);
+ 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, SFD_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);
+ talloc_free(osfd);
+ return NULL;
+ }
+
+ return osfd;
+}
+
+#endif /* HAVE_SYS_SIGNALFD_H */
/*! @} */