Coverage Report

Created: 2026-10-08 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzmq/src/ipc_listener.cpp
Line
Count
Source
1
/* SPDX-License-Identifier: MPL-2.0 */
2
3
#include "precompiled.hpp"
4
#include "ipc_listener.hpp"
5
6
#if defined ZMQ_HAVE_IPC
7
8
#include <new>
9
10
#include <string.h>
11
12
#include "ipc_address.hpp"
13
#include "io_thread.hpp"
14
#include "config.hpp"
15
#include "err.hpp"
16
#include "ip.hpp"
17
#include "socket_base.hpp"
18
#include "address.hpp"
19
20
#ifdef ZMQ_HAVE_WINDOWS
21
#ifdef ZMQ_IOTHREAD_POLLER_USE_SELECT
22
#error On Windows, IPC does not work with POLLER=select, use POLLER=epoll instead, or disable IPC transport
23
#endif
24
25
#include <afunix.h>
26
#include <direct.h>
27
28
#define rmdir rmdir_utf8
29
#define unlink unlink_utf8
30
31
#else
32
#include <unistd.h>
33
#include <sys/socket.h>
34
#include <fcntl.h>
35
#include <sys/un.h>
36
#endif
37
38
#ifdef ZMQ_HAVE_LOCAL_PEERCRED
39
#include <sys/types.h>
40
#include <sys/ucred.h>
41
#endif
42
#ifdef ZMQ_HAVE_SO_PEERCRED
43
#include <sys/types.h>
44
#include <pwd.h>
45
#include <grp.h>
46
#if defined ZMQ_HAVE_OPENBSD
47
#define ucred sockpeercred
48
#endif
49
#endif
50
51
zmq::ipc_listener_t::ipc_listener_t (io_thread_t *io_thread_,
52
                                     socket_base_t *socket_,
53
                                     const options_t &options_) :
54
0
    stream_listener_base_t (io_thread_, socket_, options_), _has_file (false)
55
0
{
56
0
}
57
58
void zmq::ipc_listener_t::in_event ()
59
0
{
60
0
    const fd_t fd = accept ();
61
62
    //  If connection was reset by the peer in the meantime, just ignore it.
63
    //  TODO: Handle specific errors like ENFILE/EMFILE etc.
64
0
    if (fd == retired_fd) {
65
0
        _socket->event_accept_failed (
66
0
          make_unconnected_bind_endpoint_pair (_endpoint), zmq_errno ());
67
0
        return;
68
0
    }
69
70
    //  Create the engine object for this connection.
71
0
    create_engine (fd);
72
0
}
73
74
std::string
75
zmq::ipc_listener_t::get_socket_name (zmq::fd_t fd_,
76
                                      socket_end_t socket_end_) const
77
0
{
78
0
    return zmq::get_socket_name<ipc_address_t> (fd_, socket_end_);
79
0
}
80
81
int zmq::ipc_listener_t::set_local_address (const char *addr_)
82
0
{
83
    //  Create addr on stack for auto-cleanup
84
0
    std::string addr (addr_);
85
86
    //  Allow wildcard file
87
0
    if (options.use_fd == -1 && addr[0] == '*') {
88
0
        if (create_ipc_wildcard_address (_tmp_socket_dirname, addr) < 0) {
89
0
            return -1;
90
0
        }
91
0
    }
92
93
    //  Get rid of the file associated with the UNIX domain socket that
94
    //  may have been left behind by the previous run of the application.
95
    //  MUST NOT unlink if the FD is managed by the user, or it will stop
96
    //  working after the first client connects. The user will take care of
97
    //  cleaning up the file after the service is stopped.
98
0
    if (options.use_fd == -1) {
99
0
        ::unlink (addr.c_str ());
100
0
    }
101
0
    _filename.clear ();
102
103
    //  Initialise the address structure.
104
0
    ipc_address_t address;
105
0
    int rc = address.resolve (addr.c_str ());
106
0
    if (rc != 0) {
107
0
        if (!_tmp_socket_dirname.empty ()) {
108
            // We need to preserve errno to return to the user
109
0
            const int tmp_errno = errno;
110
0
            ::rmdir (_tmp_socket_dirname.c_str ());
111
0
            _tmp_socket_dirname.clear ();
112
0
            errno = tmp_errno;
113
0
        }
114
0
        return -1;
115
0
    }
116
117
0
    address.to_string (_endpoint);
118
119
0
    if (options.use_fd != -1) {
120
0
        _s = options.use_fd;
121
0
    } else {
122
        //  Create a listening socket.
123
0
        _s = open_socket (AF_UNIX, SOCK_STREAM, 0);
124
0
        if (_s == retired_fd) {
125
0
            if (!_tmp_socket_dirname.empty ()) {
126
                // We need to preserve errno to return to the user
127
0
                const int tmp_errno = errno;
128
0
                ::rmdir (_tmp_socket_dirname.c_str ());
129
0
                _tmp_socket_dirname.clear ();
130
0
                errno = tmp_errno;
131
0
            }
132
0
            return -1;
133
0
        }
134
135
        //  Bind the socket to the file path.
136
0
        rc = bind (_s, const_cast<sockaddr *> (address.addr ()),
137
0
                   address.addrlen ());
138
0
        if (rc != 0)
139
0
            goto error;
140
141
        //  Listen for incoming connections.
142
0
        rc = listen (_s, options.backlog);
143
0
        if (rc != 0)
144
0
            goto error;
145
0
    }
146
147
0
    _filename = ZMQ_MOVE (addr);
148
0
    _has_file = true;
149
150
0
    _socket->event_listening (make_unconnected_bind_endpoint_pair (_endpoint),
151
0
                              _s);
152
0
    return 0;
153
154
0
error:
155
0
    const int err = errno;
156
0
    close ();
157
0
    errno = err;
158
0
    return -1;
159
0
}
160
161
int zmq::ipc_listener_t::close ()
162
0
{
163
0
    zmq_assert (_s != retired_fd);
164
0
    const fd_t fd_for_event = _s;
165
#ifdef ZMQ_HAVE_WINDOWS
166
    int rc = closesocket (_s);
167
    wsa_assert (rc != SOCKET_ERROR);
168
#else
169
0
    int rc = ::close (_s);
170
0
    errno_assert (rc == 0);
171
0
#endif
172
173
0
    _s = retired_fd;
174
175
0
    if (_has_file && options.use_fd == -1) {
176
0
        if (!_tmp_socket_dirname.empty ()) {
177
            //  TODO review this behaviour, it is inconsistent with the use of
178
            //  unlink in open since 656cdb959a7482c45db979c1d08ede585d12e315;
179
            //  however, we must at least remove the file before removing the
180
            //  directory, otherwise it will always fail
181
0
            rc = ::unlink (_filename.c_str ());
182
183
0
            if (rc == 0) {
184
0
                rc = ::rmdir (_tmp_socket_dirname.c_str ());
185
0
                _tmp_socket_dirname.clear ();
186
0
            }
187
0
        }
188
189
0
        if (rc != 0) {
190
0
            _socket->event_close_failed (
191
0
              make_unconnected_bind_endpoint_pair (_endpoint), zmq_errno ());
192
0
            return -1;
193
0
        }
194
0
    }
195
196
0
    _socket->event_closed (make_unconnected_bind_endpoint_pair (_endpoint),
197
0
                           fd_for_event);
198
0
    return 0;
199
0
}
200
201
#if defined ZMQ_HAVE_SO_PEERCRED
202
203
bool zmq::ipc_listener_t::filter (fd_t sock_)
204
0
{
205
0
    if (options.ipc_uid_accept_filters.empty ()
206
0
        && options.ipc_pid_accept_filters.empty ()
207
0
        && options.ipc_gid_accept_filters.empty ())
208
0
        return true;
209
210
0
    struct ucred cred;
211
0
    socklen_t size = sizeof (cred);
212
213
0
    if (getsockopt (sock_, SOL_SOCKET, SO_PEERCRED, &cred, &size))
214
0
        return false;
215
0
    if (options.ipc_uid_accept_filters.find (cred.uid)
216
0
          != options.ipc_uid_accept_filters.end ()
217
0
        || options.ipc_gid_accept_filters.find (cred.gid)
218
0
             != options.ipc_gid_accept_filters.end ()
219
0
        || options.ipc_pid_accept_filters.find (cred.pid)
220
0
             != options.ipc_pid_accept_filters.end ())
221
0
        return true;
222
223
0
    const struct passwd *pw;
224
0
    const struct group *gr;
225
226
0
    if (!(pw = getpwuid (cred.uid)))
227
0
        return false;
228
0
    for (options_t::ipc_gid_accept_filters_t::const_iterator
229
0
           it = options.ipc_gid_accept_filters.begin (),
230
0
           end = options.ipc_gid_accept_filters.end ();
231
0
         it != end; it++) {
232
0
        if (!(gr = getgrgid (*it)))
233
0
            continue;
234
0
        for (char **mem = gr->gr_mem; *mem; mem++) {
235
0
            if (!strcmp (*mem, pw->pw_name))
236
0
                return true;
237
0
        }
238
0
    }
239
0
    return false;
240
0
}
241
242
#elif defined ZMQ_HAVE_LOCAL_PEERCRED
243
244
bool zmq::ipc_listener_t::filter (fd_t sock_)
245
{
246
    if (options.ipc_uid_accept_filters.empty ()
247
        && options.ipc_gid_accept_filters.empty ())
248
        return true;
249
250
    struct xucred cred;
251
    socklen_t size = sizeof (cred);
252
253
    if (getsockopt (sock_, 0, LOCAL_PEERCRED, &cred, &size))
254
        return false;
255
    if (cred.cr_version != XUCRED_VERSION)
256
        return false;
257
    if (options.ipc_uid_accept_filters.find (cred.cr_uid)
258
        != options.ipc_uid_accept_filters.end ())
259
        return true;
260
    for (int i = 0; i < cred.cr_ngroups; i++) {
261
        if (options.ipc_gid_accept_filters.find (cred.cr_groups[i])
262
            != options.ipc_gid_accept_filters.end ())
263
            return true;
264
    }
265
266
    return false;
267
}
268
269
#endif
270
271
zmq::fd_t zmq::ipc_listener_t::accept ()
272
0
{
273
    //  Accept one connection and deal with different failure modes.
274
    //  The situation where connection cannot be accepted due to insufficient
275
    //  resources is considered valid and treated by ignoring the connection.
276
0
    zmq_assert (_s != retired_fd);
277
0
#if defined ZMQ_HAVE_SOCK_CLOEXEC && defined HAVE_ACCEPT4
278
0
    fd_t sock = ::accept4 (_s, NULL, NULL, SOCK_CLOEXEC);
279
#else
280
    struct sockaddr_storage ss;
281
    memset (&ss, 0, sizeof (ss));
282
#if defined ZMQ_HAVE_HPUX || defined ZMQ_HAVE_VXWORKS
283
    int ss_len = sizeof (ss);
284
#else
285
    socklen_t ss_len = sizeof (ss);
286
#endif
287
288
    const fd_t sock =
289
      ::accept (_s, reinterpret_cast<struct sockaddr *> (&ss), &ss_len);
290
#endif
291
0
    if (sock == retired_fd) {
292
#if defined ZMQ_HAVE_WINDOWS
293
        const int last_error = WSAGetLastError ();
294
        wsa_assert (last_error == WSAEWOULDBLOCK || last_error == WSAECONNRESET
295
                    || last_error == WSAEMFILE || last_error == WSAENOBUFS);
296
#else
297
0
        errno_assert (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR
298
0
                      || errno == ECONNABORTED || errno == EPROTO
299
0
                      || errno == ENFILE);
300
0
#endif
301
0
        return retired_fd;
302
0
    }
303
304
0
    make_socket_noninheritable (sock);
305
306
    // IPC accept() filters
307
0
#if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
308
0
    if (!filter (sock)) {
309
0
        int rc = ::close (sock);
310
0
        errno_assert (rc == 0);
311
0
        return retired_fd;
312
0
    }
313
0
#endif
314
315
0
    if (zmq::set_nosigpipe (sock)) {
316
#ifdef ZMQ_HAVE_WINDOWS
317
        const int rc = closesocket (sock);
318
        wsa_assert (rc != SOCKET_ERROR);
319
#else
320
0
        int rc = ::close (sock);
321
0
        errno_assert (rc == 0);
322
0
#endif
323
0
        return retired_fd;
324
0
    }
325
326
0
    return sock;
327
0
}
328
329
#endif