From 6df2ede5f5e2b3db8c2647327274e83cc485600b Mon Sep 17 00:00:00 2001 From: Niels Provos Date: Tue, 10 Aug 2004 18:29:37 +0000 Subject: [PATCH] close file descriptors on exec(); suggested by aaron at die.net svn:r119 --- configure.in | 14 +++++++++++++- epoll.c | 14 ++++++++++++++ signal.c | 15 +++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/configure.in b/configure.in index 69737297..347aad3f 100644 --- a/configure.in +++ b/configure.in @@ -33,7 +33,7 @@ AC_CHECK_LIB(socket, socket) dnl Checks for header files. AC_HEADER_STDC -AC_CHECK_HEADERS(stdarg.h inttypes.h stdint.h poll.h signal.h unistd.h sys/epoll.h sys/time.h sys/queue.h sys/event.h sys/ioctl.h sys/devpoll.h) +AC_CHECK_HEADERS(fcntl.h stdarg.h inttypes.h stdint.h poll.h signal.h unistd.h sys/epoll.h sys/time.h sys/queue.h sys/event.h sys/ioctl.h sys/devpoll.h) if test "x$ac_cv_header_sys_queue_h" = "xyes"; then AC_MSG_CHECKING(for TAILQ_FOREACH in sys/queue.h) AC_EGREP_CPP(yes, @@ -71,6 +71,18 @@ AC_HEADER_TIME dnl Checks for library functions. AC_CHECK_FUNCS(gettimeofday vasprintf fcntl) +AC_MSG_CHECKING(for F_SETFD in fcntl.h) +AC_EGREP_CPP(yes, +[ +#define _GNU_SOURCE +#include +#ifdef F_SETFD +yes +#endif +], [ AC_DEFINE(HAVE_SETFD, 1, + [Define if F_SETFD is defined in ]) + AC_MSG_RESULT(yes) ], AC_MSG_RESULT(no)) + needsignal=no haveselect=no AC_CHECK_FUNCS(select, [haveselect=yes], ) diff --git a/epoll.c b/epoll.c index a3a35f33..be7e2d41 100644 --- a/epoll.c +++ b/epoll.c @@ -45,6 +45,9 @@ #include #include #include +#ifdef HAVE_FCNTL_H +#include +#endif #ifdef USE_LOG #include "log.h" @@ -92,6 +95,15 @@ struct eventop epollops = { epoll_dispatch }; +#ifdef HAVE_SETFD +#define FD_CLOSEONEXEC(x) do { \ + if (fcntl(x, F_SETFD, 1) == -1) \ + warn("fcntl(%d, F_SETFD)", x); \ +} while (0) +#else +#define FD_CLOSEONEXEC(x) +#endif + #define NEVENT 32000 void * @@ -117,6 +129,8 @@ epoll_init(void) return (NULL); } + FD_CLOSEONEXEC(epfd); + epollop.epfd = epfd; /* Initalize fields */ diff --git a/signal.c b/signal.c index 07345002..a57943e8 100644 --- a/signal.c +++ b/signal.c @@ -43,6 +43,9 @@ #include #include #include +#ifdef HAVE_FCNTL_H +#include +#endif #ifdef USE_LOG #include "log.h" @@ -81,6 +84,15 @@ static void evsignal_cb(int fd, short what, void *arg) event_add(ev, NULL); } +#ifdef HAVE_SETFD +#define FD_CLOSEONEXEC(x) do { \ + if (fcntl(x, F_SETFD, 1) == -1) \ + warn("fcntl(%d, F_SETFD)", x); \ +} while (0) +#else +#define FD_CLOSEONEXEC(x) +#endif + void evsignal_init(sigset_t *evsigmask) { @@ -94,6 +106,9 @@ evsignal_init(sigset_t *evsigmask) if (socketpair(AF_UNIX, SOCK_STREAM, 0, ev_signal_pair) == -1) err(1, "%s: socketpair", __func__); + FD_CLOSEONEXEC(ev_signal_pair[0]); + FD_CLOSEONEXEC(ev_signal_pair[1]); + event_set(&ev_signal, ev_signal_pair[1], EV_READ, evsignal_cb, &ev_signal); ev_signal.ev_flags |= EVLIST_INTERNAL; -- 2.40.0