2011-07-28 13:19:55 +02:00
|
|
|
/*
|
2011-11-01 18:06:11 +01:00
|
|
|
Copyright (c) 2011 250bpm s.r.o.
|
|
|
|
Copyright (c) 2011 Other contributors as noted in the AUTHORS file
|
2011-07-28 13:19:55 +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/>.
|
|
|
|
*/
|
|
|
|
|
2011-07-28 13:46:16 +02:00
|
|
|
#include "ipc_listener.hpp"
|
|
|
|
|
|
|
|
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
|
|
|
|
|
2011-07-28 13:19:55 +02:00
|
|
|
#include <new>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2011-08-16 12:44:34 +02:00
|
|
|
#include "stream_engine.hpp"
|
2011-08-18 11:08:22 +02:00
|
|
|
#include "ipc_address.hpp"
|
2011-07-28 13:19:55 +02:00
|
|
|
#include "io_thread.hpp"
|
2011-09-15 10:00:23 +02:00
|
|
|
#include "session_base.hpp"
|
2011-07-28 13:19:55 +02:00
|
|
|
#include "config.hpp"
|
|
|
|
#include "err.hpp"
|
2011-09-02 15:34:12 +02:00
|
|
|
#include "ip.hpp"
|
2012-05-04 03:32:46 +02:00
|
|
|
#include "socket_base.hpp"
|
2011-07-28 13:19:55 +02:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
zmq::ipc_listener_t::ipc_listener_t (io_thread_t *io_thread_,
|
|
|
|
socket_base_t *socket_, const options_t &options_) :
|
|
|
|
own_t (io_thread_, options_),
|
|
|
|
io_object_t (io_thread_),
|
|
|
|
has_file (false),
|
|
|
|
s (retired_fd),
|
|
|
|
socket (socket_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq::ipc_listener_t::~ipc_listener_t ()
|
|
|
|
{
|
2012-06-12 15:12:03 +02:00
|
|
|
if (s != retired_fd)
|
|
|
|
close ();
|
2011-07-28 13:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::ipc_listener_t::process_plug ()
|
|
|
|
{
|
|
|
|
// Start polling for incoming connections.
|
|
|
|
handle = add_fd (s);
|
|
|
|
set_pollin (handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::ipc_listener_t::process_term (int linger_)
|
|
|
|
{
|
|
|
|
rm_fd (handle);
|
2012-04-21 16:56:10 +02:00
|
|
|
close ();
|
2011-07-28 13:19:55 +02:00
|
|
|
own_t::process_term (linger_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::ipc_listener_t::in_event ()
|
|
|
|
{
|
|
|
|
fd_t fd = accept ();
|
|
|
|
|
|
|
|
// If connection was reset by the peer in the meantime, just ignore it.
|
|
|
|
// TODO: Handle specific errors like ENFILE/EMFILE etc.
|
2012-05-04 03:32:46 +02:00
|
|
|
if (fd == retired_fd) {
|
|
|
|
socket->monitor_event (ZMQ_EVENT_ACCEPT_FAILED, endpoint.c_str(), zmq_errno());
|
2011-07-28 13:19:55 +02:00
|
|
|
return;
|
2012-05-04 03:32:46 +02:00
|
|
|
}
|
2011-07-28 13:19:55 +02:00
|
|
|
|
|
|
|
// Create the engine object for this connection.
|
2011-08-16 12:44:34 +02:00
|
|
|
stream_engine_t *engine = new (std::nothrow) stream_engine_t (fd, options);
|
2011-07-28 13:19:55 +02:00
|
|
|
alloc_assert (engine);
|
|
|
|
|
|
|
|
// Choose I/O thread to run connecter in. Given that we are already
|
|
|
|
// running in an I/O thread, there must be at least one available.
|
|
|
|
io_thread_t *io_thread = choose_io_thread (options.affinity);
|
|
|
|
zmq_assert (io_thread);
|
|
|
|
|
|
|
|
// Create and launch a session object.
|
2011-09-15 10:00:23 +02:00
|
|
|
session_base_t *session = session_base_t::create (io_thread, false, socket,
|
2012-02-02 14:56:51 +01:00
|
|
|
options, NULL);
|
2011-09-15 10:00:23 +02:00
|
|
|
errno_assert (session);
|
2011-07-28 13:19:55 +02:00
|
|
|
session->inc_seqnum ();
|
|
|
|
launch_child (session);
|
|
|
|
send_attach (session, engine, false);
|
2012-05-04 03:32:46 +02:00
|
|
|
socket->monitor_event (ZMQ_EVENT_ACCEPTED, endpoint.c_str(), fd);
|
2011-07-28 13:19:55 +02:00
|
|
|
}
|
|
|
|
|
2012-02-18 21:44:41 +01:00
|
|
|
int zmq::ipc_listener_t::get_address (std::string &addr_)
|
2012-02-08 23:06:46 +01:00
|
|
|
{
|
2012-02-18 21:44:41 +01:00
|
|
|
struct sockaddr_storage ss;
|
2012-06-05 16:44:02 +02:00
|
|
|
#ifdef ZMQ_HAVE_HPUX
|
|
|
|
int sl = sizeof (ss);
|
|
|
|
#else
|
2012-02-18 21:44:41 +01:00
|
|
|
socklen_t sl = sizeof (ss);
|
2012-06-05 16:44:02 +02:00
|
|
|
#endif
|
2012-04-18 21:42:11 +02:00
|
|
|
int rc = getsockname (s, (sockaddr *) &ss, &sl);
|
2012-02-11 16:09:03 +01:00
|
|
|
if (rc != 0) {
|
2012-04-18 21:42:11 +02:00
|
|
|
addr_.clear ();
|
2012-02-11 16:09:03 +01:00
|
|
|
return rc;
|
2012-02-08 23:06:46 +01:00
|
|
|
}
|
2012-02-17 22:45:17 +01:00
|
|
|
|
2012-04-18 21:42:11 +02:00
|
|
|
ipc_address_t addr ((struct sockaddr *) &ss, sl);
|
|
|
|
return addr.to_string (addr_);
|
2012-02-08 23:06:46 +01:00
|
|
|
}
|
|
|
|
|
2011-07-28 13:46:16 +02:00
|
|
|
int zmq::ipc_listener_t::set_address (const char *addr_)
|
2011-07-28 13:19:55 +02:00
|
|
|
{
|
2012-02-08 23:06:46 +01:00
|
|
|
// Allow wildcard file
|
2012-02-18 21:44:41 +01:00
|
|
|
if (*addr_ == '*') {
|
2012-02-08 23:06:46 +01:00
|
|
|
addr_ = tempnam(NULL, NULL);
|
|
|
|
}
|
2012-02-18 21:44:41 +01:00
|
|
|
|
2011-07-28 13:46:16 +02:00
|
|
|
// Get rid of the file associated with the UNIX domain socket that
|
|
|
|
// may have been left behind by the previous run of the application.
|
|
|
|
::unlink (addr_);
|
2011-08-18 11:08:22 +02:00
|
|
|
filename.clear ();
|
2011-07-28 13:19:55 +02:00
|
|
|
|
2011-08-18 11:08:22 +02:00
|
|
|
// Initialise the address structure.
|
|
|
|
ipc_address_t address;
|
|
|
|
int rc = address.resolve (addr_);
|
2011-07-28 13:19:55 +02:00
|
|
|
if (rc != 0)
|
2011-07-28 13:46:16 +02:00
|
|
|
return -1;
|
2011-07-28 13:19:55 +02:00
|
|
|
|
|
|
|
// Create a listening socket.
|
2011-09-02 15:34:12 +02:00
|
|
|
s = open_socket (AF_UNIX, SOCK_STREAM, 0);
|
2011-07-28 13:46:16 +02:00
|
|
|
if (s == -1)
|
2011-07-28 13:19:55 +02:00
|
|
|
return -1;
|
|
|
|
|
2012-05-04 03:32:46 +02:00
|
|
|
address.to_string (endpoint);
|
|
|
|
|
2011-07-28 13:46:16 +02:00
|
|
|
// Bind the socket to the file path.
|
2011-08-18 11:08:22 +02:00
|
|
|
rc = bind (s, address.addr (), address.addrlen ());
|
2011-07-28 16:32:08 +02:00
|
|
|
if (rc != 0)
|
2011-07-28 13:19:55 +02:00
|
|
|
return -1;
|
2011-07-28 16:32:08 +02:00
|
|
|
|
2012-03-01 22:49:46 +01:00
|
|
|
filename.assign(addr_);
|
2011-07-28 13:46:16 +02:00
|
|
|
has_file = true;
|
2011-07-28 13:19:55 +02:00
|
|
|
|
|
|
|
// Listen for incomming connections.
|
|
|
|
rc = listen (s, options.backlog);
|
2011-07-28 16:32:08 +02:00
|
|
|
if (rc != 0)
|
2011-07-28 13:46:16 +02:00
|
|
|
return -1;
|
2012-02-18 21:44:41 +01:00
|
|
|
|
2012-05-04 03:32:46 +02:00
|
|
|
socket->monitor_event (ZMQ_EVENT_LISTENING, addr_, s);
|
2012-02-18 21:44:41 +01:00
|
|
|
return 0;
|
2011-07-28 13:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::ipc_listener_t::close ()
|
|
|
|
{
|
|
|
|
zmq_assert (s != retired_fd);
|
|
|
|
int rc = ::close (s);
|
2012-05-04 03:32:46 +02:00
|
|
|
if (rc != 0) {
|
|
|
|
socket->monitor_event (ZMQ_EVENT_CLOSE_FAILED, endpoint.c_str(), zmq_errno());
|
2011-07-28 13:19:55 +02:00
|
|
|
return -1;
|
2012-05-04 03:32:46 +02:00
|
|
|
}
|
2011-07-28 13:19:55 +02:00
|
|
|
|
|
|
|
// If there's an underlying UNIX domain socket, get rid of the file it
|
|
|
|
// is associated with.
|
2011-08-18 11:08:22 +02:00
|
|
|
if (has_file && !filename.empty ()) {
|
|
|
|
rc = ::unlink(filename.c_str ());
|
2012-05-04 03:32:46 +02:00
|
|
|
if (rc != 0) {
|
|
|
|
socket->monitor_event (ZMQ_EVENT_CLOSE_FAILED, endpoint.c_str(), zmq_errno());
|
2011-07-28 13:19:55 +02:00
|
|
|
return -1;
|
2012-05-04 03:32:46 +02:00
|
|
|
}
|
2011-07-28 13:19:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-04 03:32:46 +02:00
|
|
|
socket->monitor_event (ZMQ_EVENT_CLOSED, endpoint.c_str(), s);
|
|
|
|
s = retired_fd;
|
2011-07-28 13:19:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq::fd_t zmq::ipc_listener_t::accept ()
|
|
|
|
{
|
2011-07-28 16:32:08 +02:00
|
|
|
// Accept one connection and deal with different failure modes.
|
2011-07-28 13:19:55 +02:00
|
|
|
zmq_assert (s != retired_fd);
|
|
|
|
fd_t sock = ::accept (s, NULL, NULL);
|
2011-07-28 16:32:08 +02:00
|
|
|
if (sock == -1) {
|
|
|
|
errno_assert (errno == EAGAIN || errno == EWOULDBLOCK ||
|
|
|
|
errno == EINTR || errno == ECONNABORTED || errno == EPROTO ||
|
|
|
|
errno == ENOBUFS);
|
2011-07-28 13:19:55 +02:00
|
|
|
return retired_fd;
|
2011-07-28 16:32:08 +02:00
|
|
|
}
|
2011-07-28 13:19:55 +02:00
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|