mirror of https://gerrit.osmocom.org/libosmocore
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
This commit is contained in:
parent
f3cc731d40
commit
a70ac85f5b
|
@ -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=""])
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
||||
/*! @} */
|
||||
|
|
59
src/select.c
59
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 */
|
||||
|
||||
/*! @} */
|
||||
|
||||
|
|
Loading…
Reference in New Issue