summaryrefslogtreecommitdiff
path: root/src/ip.cpp
diff options
context:
space:
mode:
authorMartin Sustrik <sustrik@250bpm.com>2011-09-02 15:34:12 +0200
committerMartin Sustrik <sustrik@250bpm.com>2011-09-02 15:34:12 +0200
commit8b7ac4c2a9c3ede95d6f5f9717a1939a23788964 (patch)
tree9afa85cda119a35f2dcd18eb7ff77e92cb8bd78a /src/ip.cpp
parent2910a728dc777068e6ae7f67041da185b0865171 (diff)
Close file descriptors on exec (issue 218)
When exec is executed to start a different process image old 0MQ file descriptors could stay open, thus blocking TCP ports and alike. This patch should solve the problem. Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
Diffstat (limited to 'src/ip.cpp')
-rw-r--r--src/ip.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/ip.cpp b/src/ip.cpp
index e1f0e6b..b11a244 100644
--- a/src/ip.cpp
+++ b/src/ip.cpp
@@ -36,6 +36,29 @@
#include <ioctl.h>
#endif
+zmq::fd_t zmq::open_socket (int domain_, int type_, int protocol_)
+{
+ // Setting this option result in sane behaviour when exec() functions
+ // are used. Old sockets are closed and don't block TCP ports etc.
+#if defined SOCK_CLOEXEC
+ type_ |= SOCK_CLOEXEC;
+#endif
+
+ fd_t s = socket (domain_, type_, protocol_);
+ if (s == retired_fd)
+ return retired_fd;
+
+ // 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 SOCK_CLOEXEC && defined FD_CLOEXEC
+ int rc = fcntl (s, F_SETFD, FD_CLOEXEC);
+ errno_assert (rc != -1);
+#endif
+
+ return s;
+}
+
void zmq::tune_tcp_socket (fd_t s_)
{
// Disable Nagle's algorithm. We are doing data batching on 0MQ level,