From dc0023293292d5247a5984e78fc10de9399da5f5 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 28 Jan 2014 13:33:23 +0100 Subject: src: Add signalfd() to handle stale fds. Reviewed-by: Stefan Metzmacher --- ConfigureChecks.cmake | 9 ++------- config.h.cmake | 3 +++ src/socket_wrapper.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake index 2d8e690..65f8b1a 100644 --- a/ConfigureChecks.cmake +++ b/ConfigureChecks.cmake @@ -48,18 +48,13 @@ endif(CMAKE_COMPILER_IS_GNUCC AND NOT MINGW AND NOT OS2) # HEADERS check_include_file(sys/filio.h HAVE_SYS_FILIO_H) +check_include_file(sys/signalfd.h HAVE_SYS_SIGNALFD_H) # FUNCTIONS check_function_exists(strncpy HAVE_STRNCPY) check_function_exists(vsnprintf HAVE_VSNPRINTF) check_function_exists(snprintf HAVE_SNPRINTF) - -if (WIN32) - check_function_exists(_vsnprintf_s HAVE__VSNPRINTF_S) - check_function_exists(_vsnprintf HAVE__VSNPRINTF) - check_function_exists(_snprintf HAVE__SNPRINTF) - check_function_exists(_snprintf_s HAVE__SNPRINTF_S) -endif (WIN32) +check_function_exists(signalfd HAVE_SIGNALFD) if (UNIX) if (NOT LINUX) diff --git a/config.h.cmake b/config.h.cmake index 63bbdb4..6b29d5b 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -15,6 +15,7 @@ /************************** HEADER FILES *************************/ #cmakedefine HAVE_SYS_FILIO_H 1 +#cmakedefine HAVE_SYS_SIGNALFD_H 1 /************************ STRUCT MEMBERS *************************/ @@ -25,6 +26,8 @@ /* Define to 1 if you have the `getaddrinfo' function. */ #cmakedefine HAVE_GETADDRINFO 1 +#cmakedefine HAVE_SIGNALFD 1 + #cmakedefine HAVE_ACCEPT_PSOCKLEN_T 1 #cmakedefine HAVE_IOCTL_INT 1 diff --git a/src/socket_wrapper.c b/src/socket_wrapper.c index 41b9923..f78f4a5 100644 --- a/src/socket_wrapper.c +++ b/src/socket_wrapper.c @@ -50,6 +50,9 @@ #ifdef HAVE_SYS_FILIO_H #include #endif +#ifdef HAVE_SYS_SIGNALFD_H +#include +#endif #include #include #include @@ -321,6 +324,9 @@ struct swrap_libc_fns { int optname, const void *optval, socklen_t optlen); +#ifdef HAVE_SIGNALFD + int (*libc_signalfd)(int fd, const sigset_t *mask, int flags); +#endif int (*libc_socket)(int domain, int type, int protocol); int (*libc_socketpair)(int domain, int type, int protocol, int sv[2]); ssize_t (*libc_writev)(int fd, const struct iovec *iov, int iovcnt); @@ -657,6 +663,15 @@ static int libc_setsockopt(int sockfd, return swrap.fns.libc_setsockopt(sockfd, level, optname, optval, optlen); } +#ifdef HAVE_SIGNALFD +static int libc_signalfd(int fd, const sigset_t *mask, int flags) +{ + swrap_load_lib_function(SWRAP_LIBSOCKET, signalfd); + + return swrap.fns.libc_signalfd(fd, mask, flags); +} +#endif + static int libc_socket(int domain, int type, int protocol) { swrap_load_lib_function(SWRAP_LIBSOCKET, socket); @@ -1965,6 +1980,29 @@ static void swrap_dump_packet(struct socket_info *si, free(packet); } +/**************************************************************************** + * SIGNALFD + ***************************************************************************/ + +#ifdef HAVE_SIGNALFD +static int swrap_signalfd(int fd, const sigset_t *mask, int flags) +{ + int rc; + + rc = libc_signalfd(fd, mask, flags); + if (rc != -1) { + swrap_remove_stale(fd); + } + + return rc; +} + +int signalfd(int fd, const sigset_t *mask, int flags) +{ + return swrap_signalfd(fd, mask, flags); +} +#endif + /**************************************************************************** * SOCKET ***************************************************************************/ -- cgit