Commit 6e708ce1 authored by Luca Boccassi's avatar Luca Boccassi

Problem: socketpair leaks file on fork+exec

Solution: if available, call socketpair with SOCK_CLOEXEC flag to
make the process close the socket on fork+exec
parent f287c7a2
...@@ -644,13 +644,29 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_) ...@@ -644,13 +644,29 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
#else #else
// All other implementations support socketpair() // All other implementations support socketpair()
int sv [2]; int sv [2];
int rc = socketpair (AF_UNIX, SOCK_STREAM, 0, sv); int type = SOCK_STREAM;
// Setting this option result in sane behaviour when exec() functions
// are used. Old sockets are closed and don't block TCP ports, avoid
// leaks, etc.
#if defined ZMQ_HAVE_SOCK_CLOEXEC
type |= SOCK_CLOEXEC;
#endif
int rc = socketpair (AF_UNIX, type, 0, sv);
if (rc == -1) { if (rc == -1) {
errno_assert (errno == ENFILE || errno == EMFILE); errno_assert (errno == ENFILE || errno == EMFILE);
*w_ = *r_ = -1; *w_ = *r_ = -1;
return -1; return -1;
} }
else { else {
// If there's no SOCK_CLOEXEC, let's try the second best option. Note that
// race condition can cause socket not to be closed (if fork happens
// between socket creation and this point).
#if !defined ZMQ_HAVE_SOCK_CLOEXEC && 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
*w_ = sv [0]; *w_ = sv [0];
*r_ = sv [1]; *r_ = sv [1];
return 0; return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment