ipc_listener.cpp 5.29 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    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/>.
*/

20 21 22 23
#include "ipc_listener.hpp"

#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS

24 25 26 27
#include <new>

#include <string.h>

28
#include "stream_engine.hpp"
29
#include "ipc_address.hpp"
30
#include "io_thread.hpp"
31
#include "session_base.hpp"
32 33
#include "config.hpp"
#include "err.hpp"
34
#include "ip.hpp"
35
#include "socket_base.hpp"
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

#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 ()
{
54
    zmq_assert (s == retired_fd);
55 56 57 58 59 60 61 62 63 64 65 66
}

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);
67
    close ();
68 69 70 71 72 73 74 75 76
    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.
77
    if (fd == retired_fd) {
78
        socket->event_accept_failed (endpoint, zmq_errno());
79
        return;
80
    }
81 82

    //  Create the engine object for this connection.
83
    stream_engine_t *engine = new (std::nothrow) stream_engine_t (fd, options, endpoint);
84 85 86 87 88 89 90 91
    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. 
92
    session_base_t *session = session_base_t::create (io_thread, false, socket,
93
        options, NULL);
94
    errno_assert (session);
95 96 97
    session->inc_seqnum ();
    launch_child (session);
    send_attach (session, engine, false);
98
    socket->event_accepted (endpoint, fd);
99 100
}

101
int zmq::ipc_listener_t::get_address (std::string &addr_)
102
{
103
    struct sockaddr_storage ss;
AJ Lewis's avatar
AJ Lewis committed
104 105 106
#ifdef ZMQ_HAVE_HPUX
    int sl = sizeof (ss);
#else
107
    socklen_t sl = sizeof (ss);
AJ Lewis's avatar
AJ Lewis committed
108
#endif
109
    int rc = getsockname (s, (sockaddr *) &ss, &sl);
110
    if (rc != 0) {
111
        addr_.clear ();
112
        return rc;
113
    }
Mikko Koppanen's avatar
Mikko Koppanen committed
114

115 116
    ipc_address_t addr ((struct sockaddr *) &ss, sl);
    return addr.to_string (addr_);
117 118
}

119
int zmq::ipc_listener_t::set_address (const char *addr_)
120
{
121
    // Allow wildcard file
122
    if (*addr_ == '*') {
123 124
        addr_ = tempnam(NULL, NULL);
    }
125

126 127 128
    //  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_);
129
    filename.clear ();
130

131 132 133
    //  Initialise the address structure.
    ipc_address_t address;
    int rc = address.resolve (addr_);
134
    if (rc != 0)
135
        return -1;
136 137

    //  Create a listening socket.
138
    s = open_socket (AF_UNIX, SOCK_STREAM, 0);
139
    if (s == -1)
140 141
        return -1;

142 143
    address.to_string (endpoint);

144
    //  Bind the socket to the file path.
145
    rc = bind (s, address.addr (), address.addrlen ());
146
    if (rc != 0)
147
        goto error;
148

149
    filename.assign(addr_);
150
    has_file = true;
151 152 153

    //  Listen for incomming connections.
    rc = listen (s, options.backlog);
154
    if (rc != 0)
155
        goto error;
156

157
    socket->event_listening (endpoint, s);
158
    return 0;
159 160 161 162 163 164

error:
    int err = errno;
    close ();
    errno = err;
    return -1;
165 166 167 168 169 170
}

int zmq::ipc_listener_t::close ()
{
    zmq_assert (s != retired_fd);
    int rc = ::close (s);
171
    errno_assert (rc == 0);
172

173 174
    s = retired_fd;

175 176
    //  If there's an underlying UNIX domain socket, get rid of the file it
    //  is associated with.
177 178
    if (has_file && !filename.empty ()) {
        rc = ::unlink(filename.c_str ());
179
        if (rc != 0) {
180
            socket->event_close_failed (endpoint, zmq_errno());
181
            return -1;
182
        }
183 184
    }

185
    socket->event_closed (endpoint, s);
186 187 188 189 190
    return 0;
}

zmq::fd_t zmq::ipc_listener_t::accept ()
{
191
    //  Accept one connection and deal with different failure modes.
192 193
    //  The situation where connection cannot be accepted due to insufficient
    //  resources is considered valid and treated by ignoring the connection.
194 195
    zmq_assert (s != retired_fd);
    fd_t sock = ::accept (s, NULL, NULL);
196 197
    if (sock == -1) {
        errno_assert (errno == EAGAIN || errno == EWOULDBLOCK ||
198
            errno == EINTR || errno == ECONNABORTED || errno == EPROTO ||
199
            errno == ENFILE);
200
        return retired_fd;
201
    }
202 203 204 205
    return sock;
}

#endif