1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
#include "core/nng_impl.h"
#ifdef PLATFORM_POSIX_IPC
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/un.h>
#include <fcntl.h>
#include <unistd.h>
#include <netdb.h>
// Solaris/SunOS systems define this, which collides with our symbol
// names. Just undefine it now.
#ifdef sun
#undef sun
#endif
#ifdef SOCK_CLOEXEC
#define NNI_IPC_SOCKTYPE (SOCK_STREAM | SOCK_CLOEXEC)
#else
#define NNI_IPC_SOCKTYPE SOCK_STREAM
#endif
static int
nni_plat_ipc_path_to_sockaddr(struct sockaddr_un *sun, const char *path)
{
memset(sun, 0, sizeof (*sun));
sun->sun_family = PF_UNIX;
// Technically on some platforms we could support path names larger
// than the path, and on others we could skip null termination. We
// take a conservative approach, which is that the path must fit in
// the supplied character array, and *must* be NULL terminated.
// TODO: abstract sockets, including autobind sockets.
if (strlen(path) >= sizeof (sun->sun_path)) {
return (NNG_EADDRINVAL);
}
if (strlen(path) == 0) {
return (-1);
}
snprintf(sun->sun_path, sizeof (sun->sun_path), "%s", path);
return (sizeof (*sun));
}
int
nni_plat_ipc_send(nni_plat_ipcsock *s, nni_iov *iovs, int cnt)
{
struct iovec iov[4]; // We never have more than 3 at present
int i;
int offset;
int resid = 0;
int rv;
if (cnt > 4) {
return (NNG_EINVAL);
}
for (i = 0; i < cnt; i++) {
iov[i].iov_base = iovs[i].iov_buf;
iov[i].iov_len = iovs[i].iov_len;
resid += iov[i].iov_len;
}
i = 0;
while (resid) {
rv = writev(s->fd, &iov[i], cnt);
if (rv < 0) {
if (rv == EINTR) {
continue;
}
return (nni_plat_errno(errno));
}
if (rv > resid) {
nni_panic("writev says it wrote too much!");
}
resid -= rv;
while (rv) {
if (iov[i].iov_len <= rv) {
rv -= iov[i].iov_len;
i++;
cnt--;
} else {
iov[i].iov_len -= rv;
iov[i].iov_base += rv;
rv = 0;
}
}
}
return (0);
}
int
nni_plat_ipc_recv(nni_plat_ipcsock *s, nni_iov *iovs, int cnt)
{
struct iovec iov[4]; // We never have more than 3 at present
int i;
int offset;
int resid = 0;
int rv;
if (cnt > 4) {
return (NNG_EINVAL);
}
for (i = 0; i < cnt; i++) {
iov[i].iov_base = iovs[i].iov_buf;
iov[i].iov_len = iovs[i].iov_len;
resid += iov[i].iov_len;
}
i = 0;
while (resid) {
rv = readv(s->fd, &iov[i], cnt);
if (rv < 0) {
if (errno == EINTR) {
continue;
}
return (nni_plat_errno(errno));
}
if (rv == 0) {
return (NNG_ECLOSED);
}
if (rv > resid) {
nni_panic("readv says it read too much!");
}
resid -= rv;
while (rv) {
if (iov[i].iov_len <= rv) {
rv -= iov[i].iov_len;
i++;
cnt--;
} else {
iov[i].iov_len -= rv;
iov[i].iov_base += rv;
rv = 0;
}
}
}
return (0);
}
static void
nni_plat_ipc_setopts(int fd)
{
int one;
// Try to ensure that both CLOEXEC is set, and that we don't
// generate SIGPIPE. (Note that SIGPIPE suppression in this way
// only works on BSD systems. Linux wants us to use sendmsg().)
(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
#if defined(F_SETNOSIGPIPE)
(void) fcntl(fd, F_SETNOSIGPIPE, 1);
#elif defined(SO_NOSIGPIPE)
one = 1;
(void) setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof (one));
#endif
}
int
nni_plat_ipc_init(nni_plat_ipcsock *s)
{
s->fd = -1;
return (0);
}
void
nni_plat_ipc_fini(nni_plat_ipcsock *s)
{
if (s->fd != -1) {
(void) close(s->fd);
s->fd = -1;
}
if (s->unlink != NULL) {
(void) unlink(s->unlink);
nni_free(s->unlink, strlen(s->unlink) + 1);
}
}
void
nni_plat_ipc_shutdown(nni_plat_ipcsock *s)
{
if (s->fd != -1) {
(void) shutdown(s->fd, SHUT_RDWR);
// This causes the equivalent of a close. Hopefully waking
// up anything that didn't get the hint with the shutdown.
// (macOS does not see the shtudown).
(void) dup2(nni_plat_devnull, s->fd);
}
}
// nni_plat_ipc_listen creates a file descriptor bound to the given address.
// This basically does the equivalent of socket, bind, and listen. We have
// chosen a default value for the listen backlog of 128, which should be
// plenty. (If it isn't, then the accept thread can't get enough resources
// to keep up, and your clients are going to experience bad things. Normally
// the actual backlog should hover near 0 anyway.)
int
nni_plat_ipc_listen(nni_plat_ipcsock *s, const char *path)
{
int fd, checkfd;
struct sockaddr_un sun;
int rv;
if (nni_plat_ipc_path_to_sockaddr(&sun, path) < 0) {
return (NNG_EADDRINVAL);
}
if ((fd = socket(AF_UNIX, NNI_IPC_SOCKTYPE, 0)) < 0) {
return (nni_plat_errno(errno));
}
// We are going to check to see if there was a name already there.
// If there was, and nothing is listening (ECONNREFUSED), then we
// will just try to cleanup the old socket. Note that this is not
// perfect in all scenarios, so use this with caution.
if ((checkfd = socket(AF_UNIX, NNI_IPC_SOCKTYPE, 0)) < 0) {
(void) close(fd);
return (nni_plat_errno(errno));
}
// Nonblocking because we don't want to wait for any remote server.
(void) fcntl(checkfd, F_SETFL, O_NONBLOCK);
if (connect(checkfd, (struct sockaddr *) &sun, sizeof (sun)) < 0) {
if (errno == ECONNREFUSED) {
(void) unlink(path);
}
}
(void) close(checkfd);
nni_plat_ipc_setopts(fd);
if ((s->unlink = nni_alloc(strlen(path) + 1)) == NULL) {
return (NNG_ENOMEM);
}
strcpy(s->unlink, path);
if (bind(fd, (struct sockaddr *) &sun, sizeof (sun)) < 0) {
rv = nni_plat_errno(errno);
nni_free(s->unlink, strlen(path) + 1);
s->unlink = NULL;
(void) close(fd);
return (rv);
}
// Listen -- 128 depth is probably sufficient. If it isn't, other
// bad things are going to happen.
if (listen(fd, 128) < 0) {
rv = nni_plat_errno(errno);
(void) close(fd);
return (rv);
}
s->fd = fd;
return (0);
}
int
nni_plat_ipc_connect(nni_plat_ipcsock *s, const char *path)
{
int fd;
int len;
struct sockaddr_un sun;
int rv;
if (nni_plat_ipc_path_to_sockaddr(&sun, path) < 0) {
return (NNG_EADDRINVAL);
}
if ((fd = socket(AF_UNIX, NNI_IPC_SOCKTYPE, 0)) < 0) {
return (nni_plat_errno(errno));
}
nni_plat_ipc_setopts(fd);
if (connect(fd, (struct sockaddr *) &sun, sizeof (sun)) != 0) {
rv = nni_plat_errno(errno);
(void) close(fd);
if (rv == NNG_ENOENT) {
// In this case we want to treat this the same as
// ECONNREFUSED, since they mean the same to us.
rv = NNG_ECONNREFUSED;
}
return (rv);
}
s->fd = fd;
return (0);
}
int
nni_plat_ipc_accept(nni_plat_ipcsock *s, nni_plat_ipcsock *server)
{
int fd;
for (;;) {
#ifdef NNG_USE_ACCEPT4
fd = accept4(server->fd, NULL, NULL, SOCK_CLOEXEC);
if ((fd < 0) && ((errno == ENOSYS) || (errno == ENOTSUP))) {
fd = accept(server->fd, NULL, NULL);
}
#else
fd = accept(server->fd, NULL, NULL);
#endif
if (fd < 0) {
if ((errno == EINTR) || (errno == ECONNABORTED)) {
// These are not fatal errors, keep trying
continue;
}
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
continue;
}
return (nni_plat_errno(errno));
} else {
break;
}
}
nni_plat_ipc_setopts(fd);
s->fd = fd;
return (0);
}
#else
// Suppress empty symbols warnings in ranlib.
int nni_posix_ipc_not_used = 0;
#endif // PLATFORM_POSIX_IPC
|