diff options
-rw-r--r-- | src/ip.cpp | 23 | ||||
-rw-r--r-- | src/ip.hpp | 3 | ||||
-rw-r--r-- | src/ipc_connecter.cpp | 2 | ||||
-rw-r--r-- | src/ipc_listener.cpp | 3 | ||||
-rw-r--r-- | src/signaler.cpp | 6 | ||||
-rw-r--r-- | src/tcp_address.cpp | 5 | ||||
-rw-r--r-- | src/tcp_connecter.cpp | 2 | ||||
-rw-r--r-- | src/tcp_listener.cpp | 2 |
8 files changed, 37 insertions, 9 deletions
@@ -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, @@ -26,6 +26,9 @@ namespace zmq { + // Same as socket(2), but allows for transparent tweaking the options. + fd_t open_socket (int domain_, int type_, int protocol_); + // Tunes the supplied TCP socket for the best latency. void tune_tcp_socket (fd_t s_); diff --git a/src/ipc_connecter.cpp b/src/ipc_connecter.cpp index 54def6e..9b8520d 100644 --- a/src/ipc_connecter.cpp +++ b/src/ipc_connecter.cpp @@ -174,7 +174,7 @@ int zmq::ipc_connecter_t::open () zmq_assert (s == retired_fd); // Create the socket. - s = socket (AF_UNIX, SOCK_STREAM, 0); + s = open_socket (AF_UNIX, SOCK_STREAM, 0); if (s == -1) return -1; diff --git a/src/ipc_listener.cpp b/src/ipc_listener.cpp index 1457349..cad58ba 100644 --- a/src/ipc_listener.cpp +++ b/src/ipc_listener.cpp @@ -32,6 +32,7 @@ #include "session.hpp" #include "config.hpp" #include "err.hpp" +#include "ip.hpp" #include <unistd.h> #include <sys/socket.h> @@ -108,7 +109,7 @@ int zmq::ipc_listener_t::set_address (const char *addr_) return -1; // Create a listening socket. - s = ::socket (AF_UNIX, SOCK_STREAM, 0); + s = open_socket (AF_UNIX, SOCK_STREAM, 0); if (s == -1) return -1; diff --git a/src/signaler.cpp b/src/signaler.cpp index 1c1c5b6..aac3e7c 100644 --- a/src/signaler.cpp +++ b/src/signaler.cpp @@ -240,7 +240,7 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_) // Create listening socket. SOCKET listener; - listener = socket (AF_INET, SOCK_STREAM, 0); + listener = open_socket (AF_INET, SOCK_STREAM, 0); wsa_assert (listener != INVALID_SOCKET); // Set SO_REUSEADDR and TCP_NODELAY on listening socket. @@ -308,7 +308,7 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_) lcladdr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); lcladdr.sin_port = 0; - int listener = socket (AF_INET, SOCK_STREAM, 0); + int listener = open_socket (AF_INET, SOCK_STREAM, 0); errno_assert (listener != -1); int on = 1; @@ -329,7 +329,7 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_) rc = listen (listener, 1); errno_assert (rc != -1); - *w_ = socket (AF_INET, SOCK_STREAM, 0); + *w_ = open_socket (AF_INET, SOCK_STREAM, 0); errno_assert (*w_ != -1); rc = setsockopt (*w_, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)); diff --git a/src/tcp_address.cpp b/src/tcp_address.cpp index 999c015..b6370fa 100644 --- a/src/tcp_address.cpp +++ b/src/tcp_address.cpp @@ -25,6 +25,7 @@ #include "platform.hpp" #include "stdint.hpp" #include "err.hpp" +#include "ip.hpp" #ifdef ZMQ_HAVE_WINDOWS #include "windows.hpp" @@ -56,7 +57,7 @@ int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv4only_) (void) ipv4only_; // Create a socket. - int fd = socket (AF_INET, SOCK_DGRAM, 0); + int fd = open_socket (AF_INET, SOCK_DGRAM, 0); zmq_assert (fd != -1); // Retrieve number of interfaces. @@ -121,7 +122,7 @@ int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv4only_) (void) ipv4only_; // Create a socket. - int sd = socket (AF_INET, SOCK_DGRAM, 0); + int sd = open_socket (AF_INET, SOCK_DGRAM, 0); zmq_assert (sd != -1); struct ifreq ifr; diff --git a/src/tcp_connecter.cpp b/src/tcp_connecter.cpp index ac27181..00eb5f6 100644 --- a/src/tcp_connecter.cpp +++ b/src/tcp_connecter.cpp @@ -184,7 +184,7 @@ int zmq::tcp_connecter_t::open () zmq_assert (s == retired_fd); // Create the socket. - s = socket (address.family (), SOCK_STREAM, IPPROTO_TCP); + s = open_socket (address.family (), SOCK_STREAM, IPPROTO_TCP); #ifdef ZMQ_HAVE_WINDOWS if (s == INVALID_SOCKET) { wsa_error_to_errno (); diff --git a/src/tcp_listener.cpp b/src/tcp_listener.cpp index e78a4a3..da476a4 100644 --- a/src/tcp_listener.cpp +++ b/src/tcp_listener.cpp @@ -126,7 +126,7 @@ int zmq::tcp_listener_t::set_address (const char *addr_) return -1; // Create a listening socket. - s = ::socket (address.family (), SOCK_STREAM, IPPROTO_TCP); + s = open_socket (address.family (), SOCK_STREAM, IPPROTO_TCP); #ifdef ZMQ_HAVE_WINDOWS if (s == INVALID_SOCKET) wsa_error_to_errno (); |