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/>.
|
|
|
|
*/
|
|
|
|
|
2016-12-02 00:38:16 +01:00
|
|
|
#include "precompiled.hpp"
|
|
|
|
#include "poller.hpp"
|
2018-06-27 19:01:02 +02:00
|
|
|
#include "polling_util.hpp"
|
2016-12-02 00:38:16 +01:00
|
|
|
|
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>
|
2018-03-10 12:03:02 +01:00
|
|
|
#elif defined ZMQ_HAVE_VXWORKS
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sockLib.h>
|
|
|
|
#include <strings.h>
|
2011-07-03 13:33:45 +02:00
|
|
|
#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"
|
2018-02-05 17:09:39 +01:00
|
|
|
#include "tcp.hpp"
|
2011-07-03 13:33:45 +02:00
|
|
|
|
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
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
#if !defined(ZMQ_HAVE_WINDOWS)
|
2014-07-23 03:45:42 +02:00
|
|
|
// 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;
|
2018-03-10 12:03:02 +01:00
|
|
|
#elif defined ZMQ_HAVE_VXWORKS
|
|
|
|
struct timespec ns_;
|
|
|
|
ns_.tv_sec = ms_ / 1000;
|
|
|
|
ns_.tv_nsec = ms_ % 1000 * 1000000;
|
|
|
|
return nanosleep (&ns_, 0);
|
2014-07-23 03:45:42 +02:00
|
|
|
#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;
|
2018-05-28 10:04:23 +02:00
|
|
|
const unsigned int min_step_ms = 1;
|
|
|
|
const unsigned int max_step_ms = 100;
|
|
|
|
const unsigned int step_ms =
|
|
|
|
std::min (std::max (min_step_ms, max_ms_ / 10), max_step_ms);
|
2014-07-23 03:45:42 +02:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
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.
|
2018-05-27 11:10:39 +02:00
|
|
|
if (make_fdpair (&_r, &_w) == 0) {
|
|
|
|
unblock_socket (_w);
|
|
|
|
unblock_socket (_r);
|
2015-09-03 10:56:26 +02:00
|
|
|
}
|
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
|
2018-05-27 11:10:39 +02:00
|
|
|
// 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
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_r == retired_fd)
|
2018-02-01 11:46:09 +01:00
|
|
|
return;
|
2018-05-27 11:10:39 +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
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_w != retired_fd) {
|
2018-02-01 11:46:09 +01:00
|
|
|
const struct linger so_linger = {1, 0};
|
2018-05-27 11:10:39 +02:00
|
|
|
int rc = setsockopt (_w, SOL_SOCKET, SO_LINGER,
|
2018-05-18 15:54:00 +02:00
|
|
|
reinterpret_cast<const char *> (&so_linger),
|
|
|
|
sizeof so_linger);
|
2015-07-22 20:18:48 +02:00
|
|
|
// Only check shutdown if WSASTARTUP was previously done
|
|
|
|
if (rc == 0 || WSAGetLastError () != WSANOTINITIALISED) {
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
2018-05-27 11:10:39 +02:00
|
|
|
rc = closesocket (_w);
|
2015-07-22 20:18:48 +02:00
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_r == retired_fd)
|
2018-02-01 11:46:09 +01:00
|
|
|
return;
|
2018-05-27 11:10:39 +02:00
|
|
|
rc = closesocket (_r);
|
2015-07-22 20:18:48 +02:00
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
}
|
2015-07-23 09:49:03 +02:00
|
|
|
}
|
2011-07-03 13:33:45 +02:00
|
|
|
#else
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_w != retired_fd) {
|
|
|
|
int rc = close_wait_ms (_w);
|
2015-07-22 20:18:48 +02:00
|
|
|
errno_assert (rc == 0);
|
|
|
|
}
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_r != retired_fd) {
|
|
|
|
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
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
return _r;
|
2011-07-03 13:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-05-27 11:10:39 +02:00
|
|
|
ssize_t sz = write (_w, &inc, sizeof (inc));
|
2011-07-03 15:13:57 +02:00
|
|
|
errno_assert (sz == sizeof (inc));
|
|
|
|
#elif defined ZMQ_HAVE_WINDOWS
|
2011-07-03 13:33:45 +02:00
|
|
|
unsigned char dummy = 0;
|
2018-08-07 16:11:52 +02:00
|
|
|
const int nbytes =
|
|
|
|
::send (_w, reinterpret_cast<char *> (&dummy), sizeof (dummy), 0);
|
|
|
|
wsa_assert (nbytes != SOCKET_ERROR);
|
|
|
|
zmq_assert (nbytes == sizeof (dummy));
|
2018-03-10 12:03:02 +01:00
|
|
|
#elif defined ZMQ_HAVE_VXWORKS
|
|
|
|
unsigned char dummy = 0;
|
|
|
|
while (true) {
|
2018-05-27 11:10:39 +02:00
|
|
|
ssize_t nbytes = ::send (_w, (char *) &dummy, sizeof (dummy), 0);
|
2018-03-10 12:03:02 +01:00
|
|
|
if (unlikely (nbytes == -1 && errno == EINTR))
|
|
|
|
continue;
|
|
|
|
#if defined(HAVE_FORK)
|
|
|
|
if (unlikely (pid != getpid ())) {
|
|
|
|
//printf("Child process %d signaler_t::send returning without sending #2\n", getpid());
|
|
|
|
errno = EINTR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
zmq_assert (nbytes == sizeof dummy);
|
|
|
|
break;
|
|
|
|
}
|
2011-07-03 13:33:45 +02:00
|
|
|
#else
|
|
|
|
unsigned char dummy = 0;
|
|
|
|
while (true) {
|
2018-05-27 11:10:39 +02:00
|
|
|
ssize_t nbytes = ::send (_w, &dummy, sizeof (dummy), 0);
|
2011-07-03 13:33:45 +02:00
|
|
|
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;
|
2018-05-27 11:10:39 +02:00
|
|
|
pfd.fd = _r;
|
2011-07-03 13:33:45 +02:00
|
|
|
pfd.events = POLLIN;
|
2018-08-09 17:30:17 +02:00
|
|
|
const int rc = poll (&pfd, 1, timeout_);
|
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;
|
2018-05-30 22:34:42 +02:00
|
|
|
}
|
|
|
|
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
|
2018-05-30 22:34:42 +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 #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
|
|
|
|
2018-06-27 19:01:02 +02:00
|
|
|
optimized_fd_set_t fds (FD_SETSIZE);
|
|
|
|
FD_ZERO (fds.get ());
|
|
|
|
FD_SET (_r, fds.get ());
|
2011-07-03 13:33:45 +02:00
|
|
|
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
|
2018-06-27 19:01:02 +02:00
|
|
|
int rc =
|
|
|
|
select (0, fds.get (), NULL, NULL, timeout_ >= 0 ? &timeout : NULL);
|
2011-07-03 13:33:45 +02:00
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
#else
|
2018-06-27 19:01:02 +02:00
|
|
|
int rc =
|
|
|
|
select (_r + 1, fds.get (), 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
|
|
|
{
|
2018-02-01 11:46:09 +01:00
|
|
|
// Attempt to read a signal.
|
2015-09-29 03:14:02 +02:00
|
|
|
#if defined ZMQ_HAVE_EVENTFD
|
|
|
|
uint64_t dummy;
|
2018-05-27 11:10:39 +02:00
|
|
|
ssize_t sz = read (_r, &dummy, sizeof (dummy));
|
2015-09-29 03:14:02 +02:00
|
|
|
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;
|
2018-05-27 11:10:39 +02:00
|
|
|
ssize_t sz2 = write (_w, &inc, sizeof (inc));
|
2015-09-29 03:14:02 +02:00
|
|
|
errno_assert (sz2 == sizeof (inc));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq_assert (dummy == 1);
|
|
|
|
#else
|
|
|
|
unsigned char dummy;
|
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
2018-08-09 17:30:17 +02:00
|
|
|
const int nbytes =
|
2018-05-27 11:10:39 +02:00
|
|
|
::recv (_r, reinterpret_cast<char *> (&dummy), sizeof (dummy), 0);
|
2015-09-29 03:14:02 +02:00
|
|
|
wsa_assert (nbytes != SOCKET_ERROR);
|
2018-03-10 12:03:02 +01:00
|
|
|
#elif defined ZMQ_HAVE_VXWORKS
|
2018-05-27 11:10:39 +02:00
|
|
|
ssize_t nbytes = ::recv (_r, (char *) &dummy, sizeof (dummy), 0);
|
2018-03-10 12:03:02 +01:00
|
|
|
errno_assert (nbytes >= 0);
|
2015-09-29 03:14:02 +02:00
|
|
|
#else
|
2018-05-27 11:10:39 +02:00
|
|
|
ssize_t nbytes = ::recv (_r, &dummy, sizeof (dummy), 0);
|
2015-09-29 03:14:02 +02:00
|
|
|
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
|
|
|
{
|
2018-02-01 11:46:09 +01:00
|
|
|
// Attempt to read a signal.
|
2011-07-03 15:13:57 +02:00
|
|
|
#if defined ZMQ_HAVE_EVENTFD
|
|
|
|
uint64_t dummy;
|
2018-05-27 11:10:39 +02:00
|
|
|
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;
|
2018-08-09 17:30:17 +02:00
|
|
|
}
|
|
|
|
errno_assert (sz == sizeof (dummy));
|
2011-07-03 15:30:31 +02:00
|
|
|
|
2018-08-09 17:30:17 +02:00
|
|
|
// 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 0;
|
2015-09-18 16:48:08 +02:00
|
|
|
}
|
2018-08-09 17:30:17 +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
|
2018-08-09 17:30:17 +02:00
|
|
|
const int nbytes =
|
2018-05-27 11:10:39 +02:00
|
|
|
::recv (_r, reinterpret_cast<char *> (&dummy), sizeof (dummy), 0);
|
2015-09-29 03:14:02 +02:00
|
|
|
if (nbytes == SOCKET_ERROR) {
|
2018-02-01 11:46:09 +01:00
|
|
|
const int last_error = WSAGetLastError ();
|
2016-09-17 08:44:00 +02:00
|
|
|
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);
|
|
|
|
}
|
2018-03-10 12:03:02 +01:00
|
|
|
#elif defined ZMQ_HAVE_VXWORKS
|
2018-05-27 11:10:39 +02:00
|
|
|
ssize_t nbytes = ::recv (_r, (char *) &dummy, sizeof (dummy), 0);
|
2018-03-10 12:03:02 +01: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
|
|
|
#else
|
2018-05-27 11:10:39 +02:00
|
|
|
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;
|
|
|
|
}
|
2018-02-01 11:46:09 +01:00
|
|
|
errno_assert (errno == EAGAIN || errno == EWOULDBLOCK
|
|
|
|
|| errno == EINTR);
|
2015-09-29 03:14:02 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-01-31 17:03:29 +01:00
|
|
|
bool zmq::signaler_t::valid () const
|
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
return _w != retired_fd;
|
2018-01-31 17:03:29 +01: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
|
2018-05-27 11:10:39 +02:00
|
|
|
close (_r);
|
|
|
|
close (_w);
|
|
|
|
make_fdpair (&_r, &_w);
|
2013-08-31 13:17:11 +02:00
|
|
|
}
|
|
|
|
#endif
|