2011-07-03 13:33:45 +02:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2011-07-03 13:33:45 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2011-07-03 13:33:45 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
libzmq is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or
|
2011-07-03 13:33:45 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
As a special exception, the Contributors give you permission to link
|
|
|
|
this library with independent modules to produce an executable,
|
|
|
|
regardless of the license terms of these independent modules, and to
|
|
|
|
copy and distribute the resulting executable under terms of your choice,
|
|
|
|
provided that you also meet, for each linked independent module, the
|
|
|
|
terms and conditions of the license of that module. An independent
|
|
|
|
module is a module which is not derived from or based on this library.
|
|
|
|
If you modify this library, you must extend this exception to your
|
|
|
|
version of the library.
|
|
|
|
|
|
|
|
libzmq 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.
|
2011-07-03 13:33:45 +02:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// 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).
|
2016-12-02 00:38:16 +01:00
|
|
|
// zmq.h must be included *after* poll.h for AIX to build properly.
|
|
|
|
// precompiled.hpp includes include/zmq.h
|
|
|
|
#if defined ZMQ_POLL_BASED_ON_POLL && defined ZMQ_HAVE_AIX
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "precompiled.hpp"
|
|
|
|
#include "poller.hpp"
|
|
|
|
|
2014-02-17 14:08:11 +01:00
|
|
|
#if defined ZMQ_POLL_BASED_ON_POLL
|
2016-12-02 00:38:16 +01:00
|
|
|
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_AIX
|
2011-07-03 13:33:45 +02:00
|
|
|
#include <poll.h>
|
2016-06-11 19:14:25 +02:00
|
|
|
#endif
|
2014-02-17 14:08:11 +01:00
|
|
|
#elif defined ZMQ_POLL_BASED_ON_SELECT
|
2011-07-03 13:33:45 +02:00
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
|
|
|
#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
|
|
|
|
|
2016-05-14 05:41:26 +02:00
|
|
|
#if !defined ZMQ_HAVE_WINDOWS
|
2011-07-03 13:33:45 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
2014-07-23 03:45:42 +02:00
|
|
|
#if !defined (ZMQ_HAVE_WINDOWS)
|
|
|
|
// Helper to sleep for specific number of milliseconds (or until signal)
|
|
|
|
//
|
|
|
|
static int sleep_ms (unsigned int ms_)
|
|
|
|
{
|
|
|
|
if (ms_ == 0)
|
|
|
|
return 0;
|
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
|
|
|
Sleep (ms_ > 0 ? ms_ : INFINITE);
|
|
|
|
return 0;
|
|
|
|
#elif defined ZMQ_HAVE_ANDROID
|
|
|
|
usleep (ms_ * 1000);
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
return usleep (ms_ * 1000);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper to wait on close(), for non-blocking sockets, until it completes
|
|
|
|
// If EAGAIN is received, will sleep briefly (1-100ms) then try again, until
|
|
|
|
// the overall timeout is reached.
|
|
|
|
//
|
|
|
|
static int close_wait_ms (int fd_, unsigned int max_ms_ = 2000)
|
|
|
|
{
|
|
|
|
unsigned int ms_so_far = 0;
|
|
|
|
unsigned int step_ms = max_ms_ / 10;
|
|
|
|
if (step_ms < 1)
|
|
|
|
step_ms = 1;
|
|
|
|
if (step_ms > 100)
|
|
|
|
step_ms = 100;
|
|
|
|
|
|
|
|
int rc = 0; // do not sleep on first attempt
|
2015-10-30 10:19:46 +01:00
|
|
|
do {
|
|
|
|
if (rc == -1 && errno == EAGAIN) {
|
2014-07-23 03:45:42 +02:00
|
|
|
sleep_ms (step_ms);
|
|
|
|
ms_so_far += step_ms;
|
|
|
|
}
|
|
|
|
rc = close (fd_);
|
|
|
|
} while (ms_so_far < max_ms_ && rc == -1 && errno == EAGAIN);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-03 13:33:45 +02:00
|
|
|
zmq::signaler_t::signaler_t ()
|
|
|
|
{
|
|
|
|
// Create the socketpair for signaling.
|
2015-09-03 10:56:26 +02:00
|
|
|
if (make_fdpair (&r, &w) == 0) {
|
|
|
|
unblock_socket (w);
|
|
|
|
unblock_socket (r);
|
|
|
|
}
|
2013-08-31 13:17:11 +02:00
|
|
|
#ifdef HAVE_FORK
|
2014-07-09 13:49:40 +02:00
|
|
|
pid = getpid ();
|
2013-08-31 13:17:11 +02:00
|
|
|
#endif
|
2011-07-03 13:33:45 +02:00
|
|
|
}
|
|
|
|
|
2015-07-22 20:18:48 +02:00
|
|
|
// This might get run after some part of construction failed, leaving one or
|
|
|
|
// both of r and w retired_fd.
|
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
|
2015-07-22 20:18:48 +02:00
|
|
|
if (r == retired_fd) return;
|
2014-07-23 03:45:42 +02:00
|
|
|
int rc = close_wait_ms (r);
|
2011-07-03 15:13:57 +02:00
|
|
|
errno_assert (rc == 0);
|
|
|
|
#elif defined ZMQ_HAVE_WINDOWS
|
2015-07-22 20:18:48 +02:00
|
|
|
if (w != retired_fd) {
|
|
|
|
const struct linger so_linger = { 1, 0 };
|
|
|
|
int rc = setsockopt (w, SOL_SOCKET, SO_LINGER,
|
|
|
|
(const char *) &so_linger, sizeof so_linger);
|
|
|
|
// Only check shutdown if WSASTARTUP was previously done
|
|
|
|
if (rc == 0 || WSAGetLastError () != WSANOTINITIALISED) {
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
rc = closesocket (w);
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
if (r == retired_fd) return;
|
|
|
|
rc = closesocket (r);
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
}
|
2015-07-23 09:49:03 +02:00
|
|
|
}
|
2011-07-03 13:33:45 +02:00
|
|
|
#else
|
2015-07-22 20:18:48 +02:00
|
|
|
if (w != retired_fd) {
|
|
|
|
int rc = close_wait_ms (w);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
}
|
|
|
|
if (r != retired_fd) {
|
2015-07-22 21:31:48 +02:00
|
|
|
int rc = close_wait_ms (r);
|
2015-07-22 20:18:48 +02:00
|
|
|
errno_assert (rc == 0);
|
|
|
|
}
|
2011-07-03 13:33:45 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-07-09 13:49:40 +02:00
|
|
|
zmq::fd_t zmq::signaler_t::get_fd () const
|
2011-07-03 13:33:45 +02:00
|
|
|
{
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::signaler_t::send ()
|
|
|
|
{
|
2014-07-09 13:49:40 +02:00
|
|
|
#if defined HAVE_FORK
|
|
|
|
if (unlikely (pid != getpid ())) {
|
2013-08-31 13:17:11 +02:00
|
|
|
//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;
|
2015-10-30 10:19:46 +01: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;
|
2014-03-18 21:04:52 +01:00
|
|
|
#if defined(HAVE_FORK)
|
2014-07-09 13:49:40 +02:00
|
|
|
if (unlikely (pid != getpid ())) {
|
2013-08-31 13:17:11 +02:00
|
|
|
//printf("Child process %d signaler_t::send returning without sending #2\n", getpid());
|
|
|
|
errno = EINTR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2014-07-09 13:49:40 +02:00
|
|
|
zmq_assert (nbytes == sizeof dummy);
|
2011-07-03 13:33:45 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::signaler_t::wait (int timeout_)
|
|
|
|
{
|
2013-08-31 13:17:11 +02:00
|
|
|
#ifdef HAVE_FORK
|
2014-07-09 13:49:40 +02:00
|
|
|
if (unlikely (pid != getpid ())) {
|
2015-09-06 18:46:32 +02:00
|
|
|
// we have forked and the file descriptor is closed. Emulate an interrupt
|
2013-08-31 13:17:11 +02:00
|
|
|
// response.
|
|
|
|
//printf("Child process %d signaler_t::wait returning simulating interrupt #1\n", getpid());
|
|
|
|
errno = EINTR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-17 14:08:11 +01:00
|
|
|
#ifdef ZMQ_POLL_BASED_ON_POLL
|
2011-07-03 13:33:45 +02:00
|
|
|
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
|
2014-07-09 13:49:40 +02:00
|
|
|
else
|
|
|
|
if (unlikely (pid != getpid ())) {
|
2015-09-06 18:46:32 +02:00
|
|
|
// we have forked and the file descriptor is closed. Emulate an interrupt
|
2013-08-31 13:17:11 +02:00
|
|
|
// 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;
|
|
|
|
|
2014-02-17 14:08:11 +01:00
|
|
|
#elif defined ZMQ_POLL_BASED_ON_SELECT
|
2011-07-03 13:33:45 +02:00
|
|
|
|
|
|
|
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 ()
|
2015-09-29 03:14:02 +02:00
|
|
|
{
|
|
|
|
// Attempt to read a signal.
|
|
|
|
#if defined ZMQ_HAVE_EVENTFD
|
|
|
|
uint64_t dummy;
|
|
|
|
ssize_t sz = read (r, &dummy, sizeof (dummy));
|
|
|
|
errno_assert (sz == sizeof (dummy));
|
|
|
|
|
|
|
|
// If we accidentally grabbed the next signal(s) along with the current
|
|
|
|
// one, return it back to the eventfd object.
|
|
|
|
if (unlikely (dummy > 1)) {
|
|
|
|
const uint64_t inc = dummy - 1;
|
|
|
|
ssize_t sz2 = write (w, &inc, sizeof (inc));
|
|
|
|
errno_assert (sz2 == sizeof (inc));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq_assert (dummy == 1);
|
|
|
|
#else
|
|
|
|
unsigned char dummy;
|
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
2015-10-30 10:19:46 +01:00
|
|
|
int nbytes = ::recv (r, (char *) &dummy, sizeof (dummy), 0);
|
2015-09-29 03:14:02 +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);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::signaler_t::recv_failable ()
|
2011-07-03 13:33:45 +02:00
|
|
|
{
|
|
|
|
// 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));
|
2015-09-18 16:48:08 +02:00
|
|
|
if (sz == -1) {
|
|
|
|
errno_assert (errno == EAGAIN);
|
2015-09-29 03:14:02 +02:00
|
|
|
return -1;
|
2011-07-03 15:30:31 +02:00
|
|
|
}
|
2015-09-18 16:48:08 +02:00
|
|
|
else {
|
|
|
|
errno_assert (sz == sizeof (dummy));
|
|
|
|
|
|
|
|
// If we accidentally grabbed the next signal(s) along with the current
|
|
|
|
// one, return it back to the eventfd object.
|
|
|
|
if (unlikely (dummy > 1)) {
|
|
|
|
const uint64_t inc = dummy - 1;
|
|
|
|
ssize_t sz2 = write (w, &inc, sizeof (inc));
|
|
|
|
errno_assert (sz2 == sizeof (inc));
|
2015-09-29 03:14:02 +02:00
|
|
|
return 0;
|
2015-09-18 16:48:08 +02:00
|
|
|
}
|
2011-07-03 15:30:31 +02:00
|
|
|
|
2015-09-18 16:48:08 +02:00
|
|
|
zmq_assert (dummy == 1);
|
|
|
|
}
|
2011-07-03 15:13:57 +02:00
|
|
|
#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
|
2015-10-30 10:19:46 +01:00
|
|
|
int nbytes = ::recv (r, (char *) &dummy, sizeof (dummy), 0);
|
2015-09-29 03:14:02 +02:00
|
|
|
if (nbytes == SOCKET_ERROR) {
|
2016-09-17 08:44:00 +02:00
|
|
|
const int last_error = WSAGetLastError();
|
|
|
|
if (last_error == WSAEWOULDBLOCK) {
|
|
|
|
errno = EAGAIN;
|
2015-09-29 03:14:02 +02:00
|
|
|
return -1;
|
2016-09-17 08:44:00 +02:00
|
|
|
}
|
2015-09-29 03:14:02 +02:00
|
|
|
wsa_assert (last_error == WSAEWOULDBLOCK);
|
|
|
|
}
|
2011-07-03 13:33:45 +02:00
|
|
|
#else
|
|
|
|
ssize_t nbytes = ::recv (r, &dummy, sizeof (dummy), 0);
|
2015-09-29 03:14:02 +02:00
|
|
|
if (nbytes == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
|
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
errno_assert (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR);
|
|
|
|
}
|
2011-07-03 13:33:45 +02:00
|
|
|
#endif
|
|
|
|
zmq_assert (nbytes == sizeof (dummy));
|
|
|
|
zmq_assert (dummy == 0);
|
2011-07-03 15:13:57 +02:00
|
|
|
#endif
|
2015-09-29 03:14:02 +02:00
|
|
|
return 0;
|
2011-07-03 13:33:45 +02:00
|
|
|
}
|
|
|
|
|
2013-08-31 13:17:11 +02:00
|
|
|
#ifdef HAVE_FORK
|
2014-07-09 13:49:40 +02:00
|
|
|
void zmq::signaler_t::forked ()
|
2013-08-31 13:17:11 +02:00
|
|
|
{
|
2013-11-07 15:30:25 +01:00
|
|
|
// Close file descriptors created in the parent and create new pair
|
|
|
|
close (r);
|
|
|
|
close (w);
|
2015-09-03 10:56:26 +02:00
|
|
|
make_fdpair (&r, &w);
|
2013-08-31 13:17:11 +02:00
|
|
|
}
|
|
|
|
#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
|
2016-12-26 18:18:00 +01:00
|
|
|
int flags = 0;
|
|
|
|
#if defined ZMQ_HAVE_EVENTFD_CLOEXEC
|
|
|
|
// 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.
|
|
|
|
flags |= EFD_CLOEXEC;
|
|
|
|
#endif
|
|
|
|
fd_t fd = eventfd (0, flags);
|
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
|
2016-09-17 08:44:00 +02:00
|
|
|
// Windows CE does not manage security attributes
|
2013-01-01 23:16:50 +01:00
|
|
|
SECURITY_DESCRIPTOR sd;
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
2014-07-09 13:49:40 +02:00
|
|
|
memset (&sd, 0, sizeof sd);
|
|
|
|
memset (&sa, 0, sizeof sa);
|
2012-07-26 19:52:38 +02:00
|
|
|
|
2014-07-09 13:49:40 +02:00
|
|
|
InitializeSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION);
|
|
|
|
SetSecurityDescriptorDacl (&sd, TRUE, 0, FALSE);
|
2012-07-26 19:52:38 +02:00
|
|
|
|
2014-07-09 13:49:40 +02:00
|
|
|
sa.nLength = sizeof (SECURITY_ATTRIBUTES);
|
2012-07-26 19:52:38 +02:00
|
|
|
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-28 11:25:28 +01:00
|
|
|
HANDLE sync = NULL;
|
|
|
|
|
|
|
|
// Create critical section only if using fixed signaler port
|
2013-12-11 17:39:01 +01:00
|
|
|
// Use problematic Event implementation for compatibility if using old port 5905.
|
|
|
|
// Otherwise use Mutex implementation.
|
|
|
|
int event_signaler_port = 5905;
|
|
|
|
|
|
|
|
if (signaler_port == event_signaler_port) {
|
2013-11-28 11:25:28 +01:00
|
|
|
# if !defined _WIN32_WCE
|
2014-04-25 09:43:47 +02:00
|
|
|
sync = CreateEventW (&sa, FALSE, TRUE, L"Global\\zmq-signaler-port-sync");
|
2013-11-28 11:25:28 +01:00
|
|
|
# else
|
2014-04-25 09:43:47 +02:00
|
|
|
sync = CreateEventW (NULL, FALSE, TRUE, L"Global\\zmq-signaler-port-sync");
|
2013-11-28 11:25:28 +01:00
|
|
|
# endif
|
|
|
|
if (sync == NULL && GetLastError () == ERROR_ACCESS_DENIED)
|
2014-04-27 14:29:20 +02:00
|
|
|
sync = OpenEventW (SYNCHRONIZE | EVENT_MODIFY_STATE,
|
2014-04-25 09:43:47 +02:00
|
|
|
FALSE, L"Global\\zmq-signaler-port-sync");
|
2013-11-28 11:25:28 +01:00
|
|
|
|
|
|
|
win_assert (sync != NULL);
|
|
|
|
}
|
2014-07-09 13:49:40 +02:00
|
|
|
else
|
|
|
|
if (signaler_port != 0) {
|
|
|
|
wchar_t mutex_name [MAX_PATH];
|
2014-09-24 15:46:43 +02:00
|
|
|
# ifdef __MINGW32__
|
|
|
|
_snwprintf (mutex_name, MAX_PATH, L"Global\\zmq-signaler-port-%d", signaler_port);
|
|
|
|
# else
|
2014-07-09 13:49:40 +02:00
|
|
|
swprintf (mutex_name, MAX_PATH, L"Global\\zmq-signaler-port-%d", signaler_port);
|
2014-09-24 15:46:43 +02:00
|
|
|
# endif
|
2013-12-11 17:39:01 +01:00
|
|
|
|
|
|
|
# if !defined _WIN32_WCE
|
2014-04-25 09:43:47 +02:00
|
|
|
sync = CreateMutexW (&sa, FALSE, mutex_name);
|
2013-12-11 17:39:01 +01:00
|
|
|
# else
|
2014-04-25 09:43:47 +02:00
|
|
|
sync = CreateMutexW (NULL, FALSE, mutex_name);
|
2013-12-11 17:39:01 +01:00
|
|
|
# endif
|
|
|
|
if (sync == NULL && GetLastError () == ERROR_ACCESS_DENIED)
|
2014-04-25 09:43:47 +02:00
|
|
|
sync = OpenMutexW (SYNCHRONIZE, FALSE, mutex_name);
|
2013-12-11 17:39:01 +01:00
|
|
|
|
|
|
|
win_assert (sync != NULL);
|
|
|
|
}
|
2011-09-29 14:47:41 +02:00
|
|
|
|
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,
|
2015-10-30 10:19:46 +01:00
|
|
|
(char *) &so_reuseaddr, sizeof so_reuseaddr);
|
2011-07-03 13:33:45 +02:00
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
BOOL tcp_nodelay = 1;
|
|
|
|
rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELAY,
|
2015-10-30 10:19:46 +01:00
|
|
|
(char *) &tcp_nodelay, sizeof tcp_nodelay);
|
2011-07-03 13:33:45 +02:00
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
|
2013-11-11 18:12:24 +01:00
|
|
|
// Init sockaddr to signaler port.
|
2011-07-03 13:33:45 +02:00
|
|
|
struct sockaddr_in addr;
|
2014-07-09 13:49:40 +02:00
|
|
|
memset (&addr, 0, sizeof addr);
|
2011-07-03 13:33:45 +02:00
|
|
|
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
|
|
|
|
|
|
|
// Create the writer socket.
|
2013-11-11 18:12:24 +01:00
|
|
|
*w_ = open_socket (AF_INET, SOCK_STREAM, 0);
|
2011-07-03 13:33:45 +02:00
|
|
|
wsa_assert (*w_ != INVALID_SOCKET);
|
|
|
|
|
|
|
|
// Set TCP_NODELAY on writer socket.
|
|
|
|
rc = setsockopt (*w_, IPPROTO_TCP, TCP_NODELAY,
|
2014-07-09 13:49:40 +02:00
|
|
|
(char *) &tcp_nodelay, sizeof tcp_nodelay);
|
2011-07-03 13:33:45 +02:00
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
|
2013-11-28 11:25:28 +01:00
|
|
|
if (sync != NULL) {
|
|
|
|
// Enter the critical section.
|
|
|
|
DWORD dwrc = WaitForSingleObject (sync, INFINITE);
|
2013-12-11 17:39:01 +01:00
|
|
|
zmq_assert (dwrc == WAIT_OBJECT_0 || dwrc == WAIT_ABANDONED);
|
2013-11-28 11:25:28 +01:00
|
|
|
}
|
2013-11-11 18:12:24 +01:00
|
|
|
|
|
|
|
// Bind listening socket to signaler port.
|
2015-10-30 10:19:46 +01:00
|
|
|
rc = bind (listener, (const struct sockaddr *) &addr, sizeof addr);
|
2013-11-11 18:12:24 +01:00
|
|
|
|
2013-11-28 11:25:28 +01:00
|
|
|
if (rc != SOCKET_ERROR && signaler_port == 0) {
|
|
|
|
// Retrieve ephemeral port number
|
2014-07-09 13:49:40 +02:00
|
|
|
int addrlen = sizeof addr;
|
2015-10-30 10:19:46 +01:00
|
|
|
rc = getsockname (listener, (struct sockaddr *) &addr, &addrlen);
|
2013-11-28 11:25:28 +01:00
|
|
|
}
|
|
|
|
|
2013-11-11 18:12:24 +01:00
|
|
|
// Listen for incoming connections.
|
|
|
|
if (rc != SOCKET_ERROR)
|
|
|
|
rc = listen (listener, 1);
|
|
|
|
|
2011-07-03 13:33:45 +02:00
|
|
|
// Connect writer to the listener.
|
2013-11-11 18:12:24 +01:00
|
|
|
if (rc != SOCKET_ERROR)
|
2015-10-30 10:19:46 +01:00
|
|
|
rc = connect (*w_, (struct sockaddr *) &addr, sizeof addr);
|
2011-07-03 13:33:45 +02:00
|
|
|
|
2013-11-11 18:12:24 +01:00
|
|
|
// Accept connection from writer.
|
|
|
|
if (rc != SOCKET_ERROR)
|
2013-02-15 03:45:43 +01:00
|
|
|
*r_ = accept (listener, NULL, NULL);
|
2013-11-11 18:12:24 +01:00
|
|
|
|
2015-11-01 12:57:32 +01:00
|
|
|
// Send/receive large chunk to work around TCP slow start
|
|
|
|
// This code is a workaround for #1608
|
|
|
|
if (*r_ != INVALID_SOCKET) {
|
|
|
|
size_t dummy_size = 1024 * 1024; // 1M to overload default receive buffer
|
|
|
|
unsigned char *dummy = (unsigned char *) malloc (dummy_size);
|
|
|
|
int still_to_send = (int) dummy_size;
|
|
|
|
int still_to_recv = (int) dummy_size;
|
|
|
|
while (still_to_send || still_to_recv) {
|
|
|
|
int nbytes;
|
|
|
|
if (still_to_send > 0) {
|
|
|
|
nbytes = ::send (*w_, (char *) (dummy + dummy_size - still_to_send), still_to_send, 0);
|
|
|
|
wsa_assert (nbytes != SOCKET_ERROR);
|
|
|
|
still_to_send -= nbytes;
|
|
|
|
}
|
|
|
|
nbytes = ::recv (*r_, (char *) (dummy + dummy_size - still_to_recv), still_to_recv, 0);
|
|
|
|
wsa_assert (nbytes != SOCKET_ERROR);
|
|
|
|
still_to_recv -= nbytes;
|
|
|
|
}
|
|
|
|
free (dummy);
|
|
|
|
}
|
|
|
|
|
2013-11-11 18:12:24 +01:00
|
|
|
// Save errno if error occurred in bind/listen/connect/accept.
|
|
|
|
int saved_errno = 0;
|
|
|
|
if (*r_ == INVALID_SOCKET)
|
|
|
|
saved_errno = WSAGetLastError ();
|
|
|
|
|
2011-07-03 13:33:45 +02:00
|
|
|
// We don't need the listening socket anymore. Close it.
|
2016-02-21 22:49:47 +01:00
|
|
|
rc = closesocket (listener);
|
2016-02-22 00:16:44 +01:00
|
|
|
wsa_assert(rc != SOCKET_ERROR);
|
2011-07-03 13:33:45 +02:00
|
|
|
|
2013-11-28 11:25:28 +01:00
|
|
|
if (sync != NULL) {
|
|
|
|
// Exit the critical section.
|
2013-12-11 17:39:01 +01:00
|
|
|
BOOL brc;
|
|
|
|
if (signaler_port == event_signaler_port)
|
|
|
|
brc = SetEvent (sync);
|
|
|
|
else
|
|
|
|
brc = ReleaseMutex (sync);
|
2013-11-28 11:25:28 +01:00
|
|
|
win_assert (brc != 0);
|
2011-09-29 14:47:41 +02:00
|
|
|
|
2013-11-28 11:25:28 +01:00
|
|
|
// Release the kernel object
|
|
|
|
brc = CloseHandle (sync);
|
|
|
|
win_assert (brc != 0);
|
|
|
|
}
|
2012-12-27 14:31:12 +01:00
|
|
|
|
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.
|
2013-11-28 11:25:28 +01:00
|
|
|
BOOL brc = SetHandleInformation ((HANDLE) *r_, HANDLE_FLAG_INHERIT, 0);
|
2013-02-15 03:45:43 +01:00
|
|
|
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
|
2013-11-11 18:12:24 +01:00
|
|
|
if (*w_ != INVALID_SOCKET) {
|
|
|
|
rc = closesocket (*w_);
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
*w_ = INVALID_SOCKET;
|
|
|
|
}
|
2013-02-15 03:45:43 +01:00
|
|
|
// Set errno from saved value
|
2013-11-11 18:12:24 +01:00
|
|
|
errno = wsa_error_to_errno (saved_errno);
|
2013-02-15 03:45:43 +01:00
|
|
|
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;
|
2014-07-09 13:49:40 +02:00
|
|
|
memset (&lcladdr, 0, sizeof lcladdr);
|
2011-07-03 13:33:45 +02:00
|
|
|
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;
|
2014-07-09 13:49:40 +02:00
|
|
|
int rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
|
2011-07-03 13:33:45 +02:00
|
|
|
errno_assert (rc != -1);
|
|
|
|
|
2014-07-09 13:49:40 +02:00
|
|
|
rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELACK, &on, sizeof on);
|
2011-07-03 13:33:45 +02:00
|
|
|
errno_assert (rc != -1);
|
|
|
|
|
2015-10-30 10:19:46 +01:00
|
|
|
rc = bind (listener, (struct sockaddr *) &lcladdr, sizeof lcladdr);
|
2011-07-03 13:33:45 +02:00
|
|
|
errno_assert (rc != -1);
|
|
|
|
|
2014-07-09 13:49:40 +02:00
|
|
|
socklen_t lcladdr_len = sizeof lcladdr;
|
2011-07-03 13:33:45 +02:00
|
|
|
|
2015-10-30 10:19:46 +01:00
|
|
|
rc = getsockname (listener, (struct sockaddr *) &lcladdr, &lcladdr_len);
|
2011-07-03 13:33:45 +02:00
|
|
|
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);
|
|
|
|
|
2014-07-09 13:49:40 +02:00
|
|
|
rc = setsockopt (*w_, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
|
2011-07-03 13:33:45 +02:00
|
|
|
errno_assert (rc != -1);
|
|
|
|
|
2014-07-09 13:49:40 +02:00
|
|
|
rc = setsockopt (*w_, IPPROTO_TCP, TCP_NODELACK, &on, sizeof on);
|
2011-07-03 13:33:45 +02:00
|
|
|
errno_assert (rc != -1);
|
|
|
|
|
2015-10-30 10:19:46 +01:00
|
|
|
rc = connect (*w_, (struct sockaddr *) &lcladdr, sizeof lcladdr);
|
2011-07-03 13:33:45 +02:00
|
|
|
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];
|
2016-12-26 18:28:18 +01:00
|
|
|
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);
|
2013-11-07 14:59:53 +01:00
|
|
|
if (rc == -1) {
|
|
|
|
errno_assert (errno == ENFILE || errno == EMFILE);
|
2013-11-10 11:19:19 +01:00
|
|
|
*w_ = *r_ = -1;
|
2013-11-07 14:59:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
2016-12-26 18:28:18 +01:00
|
|
|
// 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
|
2013-11-07 14:59:53 +01:00
|
|
|
*w_ = sv [0];
|
|
|
|
*r_ = sv [1];
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-03 13:33:45 +02:00
|
|
|
#endif
|
|
|
|
}
|