From b6131942246f587188208dd9df6f5332839101d2 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Wed, 17 Mar 2010 15:00:29 +0100 Subject: [PATCH] Apply FD_CLOEXEC on listening socket Use fcntl on listening socket to ensure it gets closed in case any server ever execs. There is no need for a child to listen and keep the port occupied. --- configure.ac | 2 +- src/yaz-pdu-assoc.cpp | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index 30e9464..bce01e5 100644 --- a/configure.ac +++ b/configure.ac @@ -17,7 +17,7 @@ if test -z "$YAZLIB"; then AC_MSG_ERROR([YAZ development libraries missing]) fi YAZ_DOC -AC_CHECK_HEADERS([unistd.h sys/stat.h sys/time.h sys/types.h]) +AC_CHECK_HEADERS([unistd.h sys/stat.h sys/time.h sys/types.h fcntl.h]) AC_ARG_ENABLE(zoom,[ --disable-zoom disable ZOOM (for old C++ compilers)],[enable_zoom=$enableval],[enable_zoom=yes]) AM_CONDITIONAL(ZOOM, test $enable_zoom = "yes") diff --git a/src/yaz-pdu-assoc.cpp b/src/yaz-pdu-assoc.cpp index 8211bc1..8f0d383 100644 --- a/src/yaz-pdu-assoc.cpp +++ b/src/yaz-pdu-assoc.cpp @@ -10,6 +10,10 @@ #include +#if HAVE_FCNTL_H +#include +#endif + using namespace yazpp_1; void PDU_Assoc::init(ISocketObservable *socketObservable) @@ -425,11 +429,21 @@ int PDU_Assoc::listen(IPDU_Observer *observer, const char *addr) return -1; if (cs_bind(m_cs, ap, CS_SERVER) < 0) return -2; - m_socketObservable->addObserver(cs_fileno(m_cs), this); + + int fd = cs_fileno(m_cs); +#if HAVE_FCNTL_H + int oldflags = fcntl(fd, F_GETFD, 0); + if (oldflags >= 0) + { + oldflags |= FD_CLOEXEC; + fcntl(fd, F_SETFD, oldflags); + } +#endif + m_socketObservable->addObserver(fd, this); yaz_log(m_log, "maskObserver 9"); m_socketObservable->maskObserver(this, SOCKET_OBSERVE_READ| SOCKET_OBSERVE_EXCEPT); - yaz_log (m_log, "PDU_Assoc::listen ok fd=%d", cs_fileno(m_cs)); + yaz_log (m_log, "PDU_Assoc::listen ok fd=%d", fd); m_state = Listen; return 0; } -- 1.7.10.4