summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Sustrik <sustrik@250bpm.com>2012-04-07 18:50:12 +0200
committerMartin Sustrik <sustrik@250bpm.com>2012-04-08 07:21:20 +0200
commitf3d6779cb7ab76eab9b5558100ec57bbed9dc497 (patch)
tree20cf70e7def3fb6402b9232934745eb98bc35ac7
parent123c0f5387ecef287dd11f4dc790fb76ee1c0f67 (diff)
Set CLOEXEC flag on fds used by signaler
While TCP and IPC socket duplicates are correctly closed on fork+exec, file descriptors used for internal communication within libxs are not. This patch fixes the problem. Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
-rw-r--r--src/signaler.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/signaler.cpp b/src/signaler.cpp
index d9a308e..4d983a8 100644
--- a/src/signaler.cpp
+++ b/src/signaler.cpp
@@ -76,6 +76,11 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
+#include <fcntl.h>
+#endif
+
+#if defined XS_HAVE_OPENVMS
+#include <ioctl.h>
#endif
xs::signaler_t::signaler_t ()
@@ -226,8 +231,17 @@ int xs::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
#if defined XS_HAVE_EVENTFD
// Create eventfd object.
+#if defined EFD_CLOEXEC
+ fd_t fd = eventfd (0, EFD_CLOEXEC);
+ errno_assert (fd != -1);
+#else
fd_t fd = eventfd (0, 0);
errno_assert (fd != -1);
+#if defined FD_CLOEXEC
+ int rc = fcntl (fd, F_SETFD, FD_CLOEXEC);
+ errno_assert (rc != -1);
+#endif
+#endif
*w_ = fd;
*r_ = fd;
return 0;
@@ -384,8 +398,19 @@ int xs::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
#else // All other implementations support socketpair()
int sv [2];
+#if defined XS_HAVE_SOCK_CLOEXEC
+ int rc = socketpair (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv);
+ errno_assert (rc == 0);
+#else
int rc = socketpair (AF_UNIX, SOCK_STREAM, 0, sv);
errno_assert (rc == 0);
+#if defined FD_CLOEXEC
+ rc = fcntl (sv [0], F_SETFD, FD_CLOEXEC);
+ errno_assert (rc != -1);
+ rc = fcntl (sv [1], F_SETFD, FD_CLOEXEC);
+ errno_assert (rc != -1);
+#endif
+#endif
*w_ = sv [0];
*r_ = sv [1];
return 0;