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
This commit is contained in:
Luca Boccassi 2016-12-26 18:28:18 +01:00
parent f287c7a2aa
commit 6e708ce144

View File

@ -644,13 +644,29 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
#else
// All other implementations support socketpair()
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) {
errno_assert (errno == ENFILE || errno == EMFILE);
*w_ = *r_ = -1;
return -1;
}
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];
*r_ = sv [1];
return 0;