2011-07-03 13:33:45 +02:00
|
|
|
/*
|
2013-03-12 13:17:00 +01:00
|
|
|
Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
|
2011-07-03 13:33:45 +02:00
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
0MQ is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "platform.hpp"
|
|
|
|
|
|
|
|
#if defined ZMQ_FORCE_SELECT
|
|
|
|
#define ZMQ_SIGNALER_WAIT_BASED_ON_SELECT
|
|
|
|
#elif defined ZMQ_FORCE_POLL
|
|
|
|
#define ZMQ_SIGNALER_WAIT_BASED_ON_POLL
|
|
|
|
#elif defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_FREEBSD ||\
|
|
|
|
defined ZMQ_HAVE_OPENBSD || defined ZMQ_HAVE_SOLARIS ||\
|
|
|
|
defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_QNXNTO ||\
|
|
|
|
defined ZMQ_HAVE_HPUX || defined ZMQ_HAVE_AIX ||\
|
|
|
|
defined ZMQ_HAVE_NETBSD
|
|
|
|
#define ZMQ_SIGNALER_WAIT_BASED_ON_POLL
|
2011-07-18 10:54:53 +02:00
|
|
|
#elif defined ZMQ_HAVE_WINDOWS || defined ZMQ_HAVE_OPENVMS ||\
|
|
|
|
defined ZMQ_HAVE_CYGWIN
|
2011-07-03 13:33:45 +02:00
|
|
|
#define ZMQ_SIGNALER_WAIT_BASED_ON_SELECT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// On AIX, poll.h has to be included before zmq.h to get consistent
|
|
|
|
// definition of pollfd structure (AIX uses 'reqevents' and 'retnevents'
|
|
|
|
// instead of 'events' and 'revents' and defines macros to map from POSIX-y
|
|
|
|
// names to AIX-specific names).
|
|
|
|
#if defined ZMQ_SIGNALER_WAIT_BASED_ON_POLL
|
|
|
|
#include <poll.h>
|
|
|
|
#elif defined ZMQ_SIGNALER_WAIT_BASED_ON_SELECT
|
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
|
|
|
#include "windows.hpp"
|
|
|
|
#elif defined ZMQ_HAVE_HPUX
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#elif defined ZMQ_HAVE_OPENVMS
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#else
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "signaler.hpp"
|
|
|
|
#include "likely.hpp"
|
2011-07-03 15:13:57 +02:00
|
|
|
#include "stdint.hpp"
|
2011-09-29 14:47:41 +02:00
|
|
|
#include "config.hpp"
|
2011-07-03 13:33:45 +02:00
|
|
|
#include "err.hpp"
|
|
|
|
#include "fd.hpp"
|
|
|
|
#include "ip.hpp"
|
|
|
|
|
2011-07-03 15:13:57 +02:00
|
|
|
#if defined ZMQ_HAVE_EVENTFD
|
|
|
|
#include <sys/eventfd.h>
|
|
|
|
#endif
|
|
|
|
|
2011-07-03 13:33:45 +02:00
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
|
|
|
#include "windows.hpp"
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
zmq::signaler_t::signaler_t ()
|
|
|
|
{
|
|
|
|
// Create the socketpair for signaling.
|
2013-11-07 14:59:53 +01:00
|
|
|
if (make_fdpair (&r, &w) == 0) {
|
|
|
|
unblock_socket (w);
|
|
|
|
unblock_socket (r);
|
|
|
|
}
|
2013-08-31 13:17:11 +02:00
|
|
|
#ifdef HAVE_FORK
|
|
|
|
pid = getpid();
|
|
|
|
#endif
|
2011-07-03 13:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zmq::signaler_t::~signaler_t ()
|
|
|
|
{
|
2011-07-03 15:13:57 +02:00
|
|
|
#if defined ZMQ_HAVE_EVENTFD
|
|
|
|
int rc = close (r);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
#elif defined ZMQ_HAVE_WINDOWS
|
2012-12-29 11:05:15 +01:00
|
|
|
struct linger so_linger = { 1, 0 };
|
|
|
|
int rc = setsockopt (w, SOL_SOCKET, SO_LINGER,
|
|
|
|
(char *)&so_linger, sizeof (so_linger));
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
rc = closesocket (w);
|
2011-07-03 13:33:45 +02:00
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
rc = closesocket (r);
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
#else
|
2011-07-03 15:13:57 +02:00
|
|
|
int rc = close (w);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
rc = close (r);
|
|
|
|
errno_assert (rc == 0);
|
2011-07-03 13:33:45 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq::fd_t zmq::signaler_t::get_fd ()
|
|
|
|
{
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::signaler_t::send ()
|
|
|
|
{
|
2013-08-31 13:17:11 +02:00
|
|
|
#if HAVE_FORK
|
|
|
|
if (unlikely(pid != getpid())) {
|
|
|
|
//printf("Child process %d signaler_t::send returning without sending #1\n", getpid());
|
|
|
|
return; // do not send anything in forked child context
|
|
|
|
}
|
|
|
|
#endif
|
2011-07-03 15:13:57 +02:00
|
|
|
#if defined ZMQ_HAVE_EVENTFD
|
|
|
|
const uint64_t inc = 1;
|
|
|
|
ssize_t sz = write (w, &inc, sizeof (inc));
|
|
|
|
errno_assert (sz == sizeof (inc));
|
|
|
|
#elif defined ZMQ_HAVE_WINDOWS
|
2011-07-03 13:33:45 +02:00
|
|
|
unsigned char dummy = 0;
|
2011-07-03 14:11:33 +02:00
|
|
|
int nbytes = ::send (w, (char*) &dummy, sizeof (dummy), 0);
|
2011-07-03 13:33:45 +02:00
|
|
|
wsa_assert (nbytes != SOCKET_ERROR);
|
|
|
|
zmq_assert (nbytes == sizeof (dummy));
|
|
|
|
#else
|
|
|
|
unsigned char dummy = 0;
|
|
|
|
while (true) {
|
|
|
|
ssize_t nbytes = ::send (w, &dummy, sizeof (dummy), 0);
|
|
|
|
if (unlikely (nbytes == -1 && errno == EINTR))
|
|
|
|
continue;
|
2013-08-31 13:17:11 +02:00
|
|
|
#if HAVE_FORK
|
|
|
|
if (unlikely(pid != getpid())) {
|
|
|
|
//printf("Child process %d signaler_t::send returning without sending #2\n", getpid());
|
|
|
|
errno = EINTR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2011-07-03 13:33:45 +02:00
|
|
|
zmq_assert (nbytes == sizeof (dummy));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::signaler_t::wait (int timeout_)
|
|
|
|
{
|
2013-08-31 13:17:11 +02:00
|
|
|
#ifdef HAVE_FORK
|
|
|
|
if (unlikely(pid != getpid()))
|
|
|
|
{
|
|
|
|
// we have forked and the file descriptor is closed. Emulate an interupt
|
|
|
|
// response.
|
|
|
|
//printf("Child process %d signaler_t::wait returning simulating interrupt #1\n", getpid());
|
|
|
|
errno = EINTR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-03 13:33:45 +02:00
|
|
|
#ifdef ZMQ_SIGNALER_WAIT_BASED_ON_POLL
|
|
|
|
|
|
|
|
struct pollfd pfd;
|
|
|
|
pfd.fd = r;
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
int rc = poll (&pfd, 1, timeout_);
|
|
|
|
if (unlikely (rc < 0)) {
|
2012-05-28 23:13:09 +02:00
|
|
|
errno_assert (errno == EINTR);
|
2011-07-03 13:33:45 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2012-10-24 02:18:52 +02:00
|
|
|
else
|
|
|
|
if (unlikely (rc == 0)) {
|
2011-07-03 13:33:45 +02:00
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-08-31 13:17:11 +02:00
|
|
|
#ifdef HAVE_FORK
|
|
|
|
if (unlikely(pid != getpid()))
|
|
|
|
{
|
|
|
|
// we have forked and the file descriptor is closed. Emulate an interupt
|
|
|
|
// response.
|
|
|
|
//printf("Child process %d signaler_t::wait returning simulating interrupt #2\n", getpid());
|
|
|
|
errno = EINTR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2011-07-03 13:33:45 +02:00
|
|
|
zmq_assert (rc == 1);
|
|
|
|
zmq_assert (pfd.revents & POLLIN);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#elif defined ZMQ_SIGNALER_WAIT_BASED_ON_SELECT
|
|
|
|
|
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO (&fds);
|
|
|
|
FD_SET (r, &fds);
|
|
|
|
struct timeval timeout;
|
2011-07-18 09:41:26 +02:00
|
|
|
if (timeout_ >= 0) {
|
|
|
|
timeout.tv_sec = timeout_ / 1000;
|
|
|
|
timeout.tv_usec = timeout_ % 1000 * 1000;
|
|
|
|
}
|
2011-07-03 13:33:45 +02:00
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
2011-07-18 09:41:26 +02:00
|
|
|
int rc = select (0, &fds, NULL, NULL,
|
|
|
|
timeout_ >= 0 ? &timeout : NULL);
|
2011-07-03 13:33:45 +02:00
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
#else
|
2011-07-18 09:41:26 +02:00
|
|
|
int rc = select (r + 1, &fds, NULL, NULL,
|
|
|
|
timeout_ >= 0 ? &timeout : NULL);
|
2011-07-03 13:33:45 +02:00
|
|
|
if (unlikely (rc < 0)) {
|
2012-05-28 23:13:09 +02:00
|
|
|
errno_assert (errno == EINTR);
|
2011-07-03 13:33:45 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (unlikely (rc == 0)) {
|
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
zmq_assert (rc == 1);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::signaler_t::recv ()
|
|
|
|
{
|
|
|
|
// Attempt to read a signal.
|
2011-07-03 15:13:57 +02:00
|
|
|
#if defined ZMQ_HAVE_EVENTFD
|
|
|
|
uint64_t dummy;
|
|
|
|
ssize_t sz = read (r, &dummy, sizeof (dummy));
|
|
|
|
errno_assert (sz == sizeof (dummy));
|
2011-07-03 15:30:31 +02:00
|
|
|
|
|
|
|
// If we accidentally grabbed the next signal along with the current
|
|
|
|
// one, return it back to the eventfd object.
|
|
|
|
if (unlikely (dummy == 2)) {
|
|
|
|
const uint64_t inc = 1;
|
2013-01-01 11:42:46 +01:00
|
|
|
ssize_t sz2 = write (w, &inc, sizeof (inc));
|
|
|
|
errno_assert (sz2 == sizeof (inc));
|
2011-07-03 15:30:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-03 15:13:57 +02:00
|
|
|
zmq_assert (dummy == 1);
|
|
|
|
#else
|
2011-07-03 13:33:45 +02:00
|
|
|
unsigned char dummy;
|
2011-07-03 14:11:33 +02:00
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
|
|
|
int nbytes = ::recv (r, (char*) &dummy, sizeof (dummy), 0);
|
2011-07-03 13:33:45 +02:00
|
|
|
wsa_assert (nbytes != SOCKET_ERROR);
|
|
|
|
#else
|
|
|
|
ssize_t nbytes = ::recv (r, &dummy, sizeof (dummy), 0);
|
|
|
|
errno_assert (nbytes >= 0);
|
|
|
|
#endif
|
|
|
|
zmq_assert (nbytes == sizeof (dummy));
|
|
|
|
zmq_assert (dummy == 0);
|
2011-07-03 15:13:57 +02:00
|
|
|
#endif
|
2011-07-03 13:33:45 +02:00
|
|
|
}
|
|
|
|
|
2013-08-31 13:17:11 +02:00
|
|
|
#ifdef HAVE_FORK
|
|
|
|
void zmq::signaler_t::forked()
|
|
|
|
{
|
|
|
|
int oldr = r;
|
2013-09-01 22:49:30 +02:00
|
|
|
#if !defined ZMQ_HAVE_EVENTFD
|
2013-08-31 13:17:11 +02:00
|
|
|
int oldw = w;
|
2013-09-01 22:49:30 +02:00
|
|
|
#endif
|
2013-08-31 13:17:11 +02:00
|
|
|
|
|
|
|
// replace the file descriptors created in the parent with new
|
|
|
|
// ones, and close the inherited ones
|
2013-11-07 14:59:53 +01:00
|
|
|
make_fdpair (&r, &w);
|
2013-08-31 13:17:11 +02:00
|
|
|
#if defined ZMQ_HAVE_EVENTFD
|
|
|
|
int rc = close (oldr);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
#else
|
|
|
|
int rc = close (oldw);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
rc = close (oldr);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-11-07 14:59:53 +01:00
|
|
|
// Returns -1 if we could not make the socket pair successfully
|
2011-07-03 13:33:45 +02:00
|
|
|
int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
|
|
|
|
{
|
2011-07-03 15:13:57 +02:00
|
|
|
#if defined ZMQ_HAVE_EVENTFD
|
|
|
|
fd_t fd = eventfd (0, 0);
|
2013-11-07 14:59:53 +01:00
|
|
|
if (fd == -1) {
|
|
|
|
errno_assert (errno == ENFILE || errno == EMFILE);
|
|
|
|
*w_ = *r_ = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*w_ = *r_ = fd;
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-03 15:13:57 +02:00
|
|
|
|
|
|
|
#elif defined ZMQ_HAVE_WINDOWS
|
2013-11-07 14:59:53 +01:00
|
|
|
# if !defined _WIN32_WCE
|
2013-02-19 18:57:12 +01:00
|
|
|
// Windows CE does not manage security attributes
|
2013-01-01 23:16:50 +01:00
|
|
|
SECURITY_DESCRIPTOR sd;
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
memset (&sd, 0, sizeof (sd));
|
|
|
|
memset (&sa, 0, sizeof (sa));
|
2012-07-26 19:52:38 +02:00
|
|
|
|
|
|
|
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
|
|
|
|
SetSecurityDescriptorDacl(&sd, TRUE, 0, FALSE);
|
|
|
|
|
|
|
|
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
sa.lpSecurityDescriptor = &sd;
|
2013-11-07 14:59:53 +01:00
|
|
|
# endif
|
2011-07-03 13:33:45 +02:00
|
|
|
|
2011-09-29 14:47:41 +02:00
|
|
|
// This function has to be in a system-wide critical section so that
|
|
|
|
// two instances of the library don't accidentally create signaler
|
|
|
|
// crossing the process boundary.
|
|
|
|
// We'll use named event object to implement the critical section.
|
2012-04-11 10:54:05 +02:00
|
|
|
// Note that if the event object already exists, the CreateEvent requests
|
|
|
|
// EVENT_ALL_ACCESS access right. If this fails, we try to open
|
|
|
|
// the event object asking for SYNCHRONIZE access only.
|
2013-11-07 14:59:53 +01:00
|
|
|
# if !defined _WIN32_WCE
|
2012-07-26 19:55:27 +02:00
|
|
|
HANDLE sync = CreateEvent (&sa, FALSE, TRUE, TEXT ("Global\\zmq-signaler-port-sync"));
|
2013-11-07 14:59:53 +01:00
|
|
|
# else
|
2013-02-19 18:57:12 +01:00
|
|
|
HANDLE sync = CreateEvent (NULL, FALSE, TRUE, TEXT ("Global\\zmq-signaler-port-sync"));
|
2013-11-07 14:59:53 +01:00
|
|
|
# endif
|
2012-04-11 10:54:05 +02:00
|
|
|
if (sync == NULL && GetLastError () == ERROR_ACCESS_DENIED)
|
2013-11-07 14:59:53 +01:00
|
|
|
sync = OpenEvent (SYNCHRONIZE | EVENT_MODIFY_STATE,
|
|
|
|
FALSE, TEXT ("Global\\zmq-signaler-port-sync"));
|
2012-04-11 10:54:05 +02:00
|
|
|
|
2011-09-29 14:47:41 +02:00
|
|
|
win_assert (sync != NULL);
|
|
|
|
|
|
|
|
// Enter the critical section.
|
|
|
|
DWORD dwrc = WaitForSingleObject (sync, INFINITE);
|
|
|
|
zmq_assert (dwrc == WAIT_OBJECT_0);
|
|
|
|
|
2011-07-03 13:33:45 +02:00
|
|
|
// Windows has no 'socketpair' function. CreatePipe is no good as pipe
|
|
|
|
// handles cannot be polled on. Here we create the socketpair by hand.
|
|
|
|
*w_ = INVALID_SOCKET;
|
|
|
|
*r_ = INVALID_SOCKET;
|
|
|
|
|
|
|
|
// Create listening socket.
|
|
|
|
SOCKET listener;
|
2011-09-02 15:34:12 +02:00
|
|
|
listener = open_socket (AF_INET, SOCK_STREAM, 0);
|
2011-07-03 13:33:45 +02:00
|
|
|
wsa_assert (listener != INVALID_SOCKET);
|
|
|
|
|
|
|
|
// Set SO_REUSEADDR and TCP_NODELAY on listening socket.
|
|
|
|
BOOL so_reuseaddr = 1;
|
|
|
|
int rc = setsockopt (listener, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(char *)&so_reuseaddr, sizeof (so_reuseaddr));
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
BOOL tcp_nodelay = 1;
|
|
|
|
rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELAY,
|
|
|
|
(char *)&tcp_nodelay, sizeof (tcp_nodelay));
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
|
2013-02-15 03:45:43 +01:00
|
|
|
// Bind listening socket to signaler port.
|
2011-07-03 13:33:45 +02:00
|
|
|
struct sockaddr_in addr;
|
|
|
|
memset (&addr, 0, sizeof (addr));
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
|
2011-09-29 14:47:41 +02:00
|
|
|
addr.sin_port = htons (signaler_port);
|
2011-07-03 13:33:45 +02:00
|
|
|
rc = bind (listener, (const struct sockaddr*) &addr, sizeof (addr));
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
|
|
|
|
// Listen for incomming connections.
|
|
|
|
rc = listen (listener, 1);
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
|
|
|
|
// Create the writer socket.
|
|
|
|
*w_ = WSASocket (AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
|
|
|
|
wsa_assert (*w_ != INVALID_SOCKET);
|
|
|
|
|
2013-11-07 14:59:53 +01:00
|
|
|
# if !defined _WIN32_WCE
|
2012-05-07 15:46:55 +02:00
|
|
|
// On Windows, preventing sockets to be inherited by child processes.
|
|
|
|
BOOL brc = SetHandleInformation ((HANDLE) *w_, HANDLE_FLAG_INHERIT, 0);
|
|
|
|
win_assert (brc);
|
2013-11-07 14:59:53 +01:00
|
|
|
# else
|
2013-02-19 18:57:12 +01:00
|
|
|
BOOL brc;
|
2013-11-07 14:59:53 +01:00
|
|
|
# endif
|
2012-05-07 15:46:55 +02:00
|
|
|
|
2011-07-03 13:33:45 +02:00
|
|
|
// Set TCP_NODELAY on writer socket.
|
|
|
|
rc = setsockopt (*w_, IPPROTO_TCP, TCP_NODELAY,
|
|
|
|
(char *)&tcp_nodelay, sizeof (tcp_nodelay));
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
|
|
|
|
// Connect writer to the listener.
|
2012-06-12 15:43:18 +02:00
|
|
|
rc = connect (*w_, (struct sockaddr*) &addr, sizeof (addr));
|
2011-07-03 13:33:45 +02:00
|
|
|
|
2013-02-15 03:45:43 +01:00
|
|
|
// Save errno if connection fails
|
|
|
|
int conn_errno = 0;
|
2013-11-07 14:59:53 +01:00
|
|
|
if (rc == SOCKET_ERROR)
|
2013-02-15 03:45:43 +01:00
|
|
|
conn_errno = WSAGetLastError ();
|
2013-11-07 14:59:53 +01:00
|
|
|
else {
|
2013-02-15 03:45:43 +01:00
|
|
|
// Accept connection from writer.
|
|
|
|
*r_ = accept (listener, NULL, NULL);
|
2013-11-07 14:59:53 +01:00
|
|
|
if (*r_ == INVALID_SOCKET)
|
2013-02-15 03:45:43 +01:00
|
|
|
conn_errno = WSAGetLastError ();
|
|
|
|
}
|
2011-07-03 13:33:45 +02:00
|
|
|
// We don't need the listening socket anymore. Close it.
|
|
|
|
rc = closesocket (listener);
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
|
2011-09-29 14:47:41 +02:00
|
|
|
// Exit the critical section.
|
2012-05-07 15:46:55 +02:00
|
|
|
brc = SetEvent (sync);
|
2011-09-29 14:47:41 +02:00
|
|
|
win_assert (brc != 0);
|
|
|
|
|
2013-02-15 03:45:43 +01:00
|
|
|
// Release the kernel object
|
2012-12-27 14:31:12 +01:00
|
|
|
brc = CloseHandle (sync);
|
|
|
|
win_assert (brc != 0);
|
|
|
|
|
2013-02-15 03:45:43 +01:00
|
|
|
if (*r_ != INVALID_SOCKET) {
|
2013-11-07 14:59:53 +01:00
|
|
|
# if !defined _WIN32_WCE
|
2013-02-15 03:45:43 +01:00
|
|
|
// On Windows, preventing sockets to be inherited by child processes.
|
|
|
|
brc = SetHandleInformation ((HANDLE) *r_, HANDLE_FLAG_INHERIT, 0);
|
|
|
|
win_assert (brc);
|
2013-11-07 14:59:53 +01:00
|
|
|
# endif
|
2013-02-15 03:45:43 +01:00
|
|
|
return 0;
|
2013-11-07 14:59:53 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-02-15 03:45:43 +01:00
|
|
|
// Cleanup writer if connection failed
|
|
|
|
rc = closesocket (*w_);
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
*w_ = INVALID_SOCKET;
|
|
|
|
// Set errno from saved value
|
|
|
|
errno = wsa_error_to_errno (conn_errno);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-07-03 13:33:45 +02:00
|
|
|
|
|
|
|
#elif defined ZMQ_HAVE_OPENVMS
|
|
|
|
|
|
|
|
// Whilst OpenVMS supports socketpair - it maps to AF_INET only. Further,
|
|
|
|
// it does not set the socket options TCP_NODELAY and TCP_NODELACK which
|
|
|
|
// can lead to performance problems.
|
|
|
|
//
|
|
|
|
// The bug will be fixed in V5.6 ECO4 and beyond. In the meantime, we'll
|
|
|
|
// create the socket pair manually.
|
2012-06-12 15:43:18 +02:00
|
|
|
struct sockaddr_in lcladdr;
|
2011-07-03 13:33:45 +02:00
|
|
|
memset (&lcladdr, 0, sizeof (lcladdr));
|
|
|
|
lcladdr.sin_family = AF_INET;
|
|
|
|
lcladdr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
|
|
|
|
lcladdr.sin_port = 0;
|
|
|
|
|
2011-09-02 15:34:12 +02:00
|
|
|
int listener = open_socket (AF_INET, SOCK_STREAM, 0);
|
2011-07-03 13:33:45 +02:00
|
|
|
errno_assert (listener != -1);
|
|
|
|
|
|
|
|
int on = 1;
|
|
|
|
int rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on));
|
|
|
|
errno_assert (rc != -1);
|
|
|
|
|
|
|
|
rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELACK, &on, sizeof (on));
|
|
|
|
errno_assert (rc != -1);
|
|
|
|
|
2013-11-07 14:59:53 +01:00
|
|
|
rc = bind (listener, (struct sockaddr*) &lcladdr, sizeof (lcladdr));
|
2011-07-03 13:33:45 +02:00
|
|
|
errno_assert (rc != -1);
|
|
|
|
|
|
|
|
socklen_t lcladdr_len = sizeof (lcladdr);
|
|
|
|
|
|
|
|
rc = getsockname (listener, (struct sockaddr*) &lcladdr, &lcladdr_len);
|
|
|
|
errno_assert (rc != -1);
|
|
|
|
|
|
|
|
rc = listen (listener, 1);
|
|
|
|
errno_assert (rc != -1);
|
|
|
|
|
2011-09-02 15:34:12 +02:00
|
|
|
*w_ = open_socket (AF_INET, SOCK_STREAM, 0);
|
2011-07-03 13:33:45 +02:00
|
|
|
errno_assert (*w_ != -1);
|
|
|
|
|
|
|
|
rc = setsockopt (*w_, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on));
|
|
|
|
errno_assert (rc != -1);
|
|
|
|
|
|
|
|
rc = setsockopt (*w_, IPPROTO_TCP, TCP_NODELACK, &on, sizeof (on));
|
|
|
|
errno_assert (rc != -1);
|
|
|
|
|
|
|
|
rc = connect (*w_, (struct sockaddr*) &lcladdr, sizeof (lcladdr));
|
|
|
|
errno_assert (rc != -1);
|
|
|
|
|
|
|
|
*r_ = accept (listener, NULL, NULL);
|
|
|
|
errno_assert (*r_ != -1);
|
|
|
|
|
|
|
|
close (listener);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2013-11-07 14:59:53 +01:00
|
|
|
#else
|
|
|
|
// All other implementations support socketpair()
|
2011-07-03 13:33:45 +02:00
|
|
|
int sv [2];
|
|
|
|
int rc = socketpair (AF_UNIX, SOCK_STREAM, 0, sv);
|
2013-11-07 14:59:53 +01:00
|
|
|
if (rc == -1) {
|
|
|
|
errno_assert (errno == ENFILE || errno == EMFILE);
|
|
|
|
sv [0] = sv [1] = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*w_ = sv [0];
|
|
|
|
*r_ = sv [1];
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-03 13:33:45 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined ZMQ_SIGNALER_WAIT_BASED_ON_SELECT
|
|
|
|
#undef ZMQ_SIGNALER_WAIT_BASED_ON_SELECT
|
|
|
|
#endif
|
|
|
|
#if defined ZMQ_SIGNALER_WAIT_BASED_ON_POLL
|
|
|
|
#undef ZMQ_SIGNALER_WAIT_BASED_ON_POLL
|
|
|
|
#endif
|
|
|
|
|