forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_unix.cpp
426 lines (377 loc) · 9.47 KB
/
stream_unix.cpp
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* Copyright 2014-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
#ifdef HAVE_UCRED_H
#include <ucred.h>
#endif
#ifdef HAVE_SYS_UCRED_H
#include <sys/ucred.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <folly/SocketAddress.h>
#include <folly/net/NetworkSocket.h>
#include <memory>
#include "FileDescriptor.h"
#include "Pipe.h"
using watchman::FileDescriptor;
using watchman::Pipe;
using namespace watchman;
static const int kWriteTimeout = 60000;
namespace {
// This trait allows w_poll_events to wait on either a PipeEvent or
// a descriptor contained in a UnixStream
class PollableEvent : public watchman_event {
public:
virtual FileDescriptor::system_handle_type getFd() const = 0;
};
// The event object, implemented as pipe
class PipeEvent : public PollableEvent {
public:
SocketPair pipe;
void notify() override {
ignore_result(pipe.write.write("a", 1).hasValue());
}
bool testAndClear() override {
char buf[64];
bool signalled = false;
while (true) {
auto res = pipe.read.read(buf, sizeof(buf));
if (res.hasError() || res.value() == 0) {
break;
}
signalled = true;
}
return signalled;
}
FileDescriptor::system_handle_type getFd() const override {
return pipe.read.system_handle();
}
FileDescriptor::system_handle_type system_handle() override {
return pipe.read.system_handle();
}
bool isSocket() override {
return true;
}
};
// Event object that UnixStream returns via getEvents.
// It cannot be poked by hand; it is just a helper to
// allow waiting on a socket using w_poll_events.
class FakeSocketEvent : public PollableEvent {
private:
FileDescriptor::system_handle_type socket;
public:
explicit FakeSocketEvent(FileDescriptor::system_handle_type fd)
: socket(fd) {}
void notify() override {}
bool testAndClear() override {
return false;
}
FileDescriptor::system_handle_type getFd() const override {
return socket;
}
FileDescriptor::system_handle_type system_handle() override {
return socket;
}
bool isSocket() override {
return true;
}
};
class UnixStream : public watchman_stream {
public:
FileDescriptor fd;
FakeSocketEvent evt;
#ifdef SO_PEERCRED
struct ucred cred;
#elif defined(LOCAL_PEERCRED)
struct xucred cred;
pid_t epid;
#elif defined(SO_RECVUCRED)
struct ucred_deleter {
void operator()(ucred_t* utp) {
ucred_free(utp);
}
};
std::unique_ptr<ucred_t, ucred_deleter> cred;
#endif
bool credvalid{false};
bool blocking_{false};
explicit UnixStream(FileDescriptor&& descriptor)
: fd(std::move(descriptor)), evt(fd.system_handle()) {
#ifdef SO_PEERCRED
socklen_t len = sizeof(cred);
credvalid = getsockopt(fd.fd(), SOL_SOCKET, SO_PEERCRED, &cred, &len) == 0;
#elif defined(LOCAL_PEERCRED)
socklen_t len = sizeof(cred);
#if defined(__FreeBSD__) || defined(__DragonFly__)
credvalid = getsockopt(fd.fd(), 0, LOCAL_PEERCRED, &cred, &len) == 0;
#else
credvalid =
getsockopt(fd.fd(), SOL_LOCAL, LOCAL_PEERCRED, &cred, &len) == 0;
#endif
if (credvalid) {
len = sizeof(epid);
credvalid =
getsockopt(fd.fd(), SOL_LOCAL, LOCAL_PEEREPID, &epid, &len) == 0;
}
#elif defined(SO_RECVUCRED)
ucred_t* peer_cred{nullptr};
credvalid = getpeerucred(fd.fd(), &peer_cred) == 0;
cred.reset(peer_cred);
#endif
}
const FileDescriptor& getFileDescriptor() const override {
return fd;
}
int read(void* buf, int size) override {
auto res = fd.read(buf, size);
if (res.hasError()) {
#ifdef _WIN32
errno = map_win32_err(res.error().value());
#else
errno = res.error().value();
#endif
return -1;
}
errno = 0;
return res.value();
}
int write(const void* buf, int size) override {
if (blocking_) {
int wrote = 0;
while (size > 0) {
struct pollfd pfd;
pfd.fd = fd.system_handle();
pfd.events = POLLOUT;
#ifdef _WIN32
if (WSAPoll(&pfd, 1, kWriteTimeout) == 0) {
errno = map_win32_err(WSAGetLastError());
break;
}
#else
if (poll(&pfd, 1, kWriteTimeout) == 0) {
break;
}
#endif
if (pfd.revents & (POLLERR | POLLHUP)) {
break;
}
auto x = fd.write(buf, size);
if (x.hasError()) {
#ifdef _WIN32
errno = map_win32_err(x.error().value());
#else
errno = x.error().value();
#endif
break;
}
if (x.value() == 0) {
errno = 0;
break;
}
wrote += x.value();
size -= x.value();
buf = reinterpret_cast<const void*>(
reinterpret_cast<const char*>(buf) + x.value());
}
return wrote == 0 ? -1 : wrote;
}
auto x = fd.write(buf, size);
if (x.hasError()) {
#ifdef _WIN32
errno = map_win32_err(x.error().value());
#else
errno = x.error().value();
#endif
return -1;
}
errno = 0;
return x.value();
}
w_evt_t getEvents() override {
return &evt;
}
void setNonBlock(bool nonb) override {
if (nonb) {
fd.setNonBlock();
} else {
fd.clearNonBlock();
}
blocking_ = !nonb;
}
bool rewind() override {
#ifndef _WIN32
return lseek(fd.fd(), 0, SEEK_SET) == 0;
#else
return false;
#endif
}
bool shutdown() override {
return ::shutdown(
fd.system_handle(),
#ifdef SHUT_RDWR
SHUT_RDWR
#else
SD_BOTH
#endif
);
}
// For these PEERCRED things, the uid reported is the effective uid of
// the process, which may have been altered due to setuid or similar
// mechanisms. We'll treat the other process as an owner if their
// effective UID matches ours, or if they are root.
bool peerIsOwner() override {
#ifdef _WIN32
return true;
#else
if (!credvalid) {
return false;
}
#ifdef SO_PEERCRED
if (cred.uid == getuid() || cred.uid == 0) {
return true;
}
#elif defined(LOCAL_PEERCRED)
if (cred.cr_uid == getuid() || cred.cr_uid == 0) {
return true;
}
#elif defined(SO_RECVUCRED)
uid_t ucreduid = ucred_getruid(cred.get());
if (ucreduid == getuid() || ucreduid == 0) {
return true;
}
#endif
return false;
#endif
}
pid_t getPeerProcessID() const override {
if (!credvalid) {
return 0;
}
#ifdef SO_PEERCRED
return cred.pid;
#elif defined(LOCAL_PEERCRED)
return epid;
#elif defined(SO_RECVUCRED)
pid_t ucredpid = ucred_getpid(cred.get());
if (ucredpid == (pid_t)-1) {
return 0;
}
return ucredpid;
#else
return 0;
#endif
}
};
} // namespace
std::unique_ptr<watchman_event> w_event_make_sockets() {
return std::make_unique<PipeEvent>();
}
#define MAX_POLL_EVENTS 63 // Must match MAXIMUM_WAIT_OBJECTS-1 on win
int w_poll_events_sockets(struct watchman_event_poll* p, int n, int timeoutms) {
struct pollfd pfds[MAX_POLL_EVENTS];
int i;
int res;
if (n > MAX_POLL_EVENTS) {
// Programmer error :-/
logf(FATAL, "{} > MAX_POLL_EVENTS ({})\n", n, MAX_POLL_EVENTS);
}
for (i = 0; i < n; i++) {
auto pe = dynamic_cast<PollableEvent*>(p[i].evt);
w_check(pe != nullptr, "PollableEvent!?");
pfds[i].fd = pe->getFd();
pfds[i].events = POLLIN;
pfds[i].revents = 0;
}
#ifdef _WIN32
res = WSAPoll(pfds, n, timeoutms);
auto win_err = WSAGetLastError();
errno = map_win32_err(win_err);
#else
res = poll(pfds, n, timeoutms);
#endif
for (i = 0; i < n; i++) {
p[i].ready = pfds[i].revents != 0;
}
return res;
}
std::unique_ptr<watchman_stream> w_stm_fdopen(FileDescriptor&& fd) {
if (!fd) {
return nullptr;
}
#ifdef _WIN32
if (fd.fdType() != FileDescriptor::FDType::Socket) {
return w_stm_fdopen_windows(std::move(fd));
}
#endif
return std::make_unique<UnixStream>(std::move(fd));
}
std::unique_ptr<watchman_stream> w_stm_connect_unix(
const char* path,
int timeoutms) {
struct sockaddr_un un {};
int max_attempts = timeoutms / 10;
int attempts = 0;
if (strlen(path) >= sizeof(un.sun_path) - 1) {
logf(ERR, "w_stm_connect_unix({}) path is too long\n", path);
errno = E2BIG;
return NULL;
}
FileDescriptor fd(
::socket(
PF_LOCAL,
#ifdef SOCK_CLOEXEC
SOCK_CLOEXEC |
#endif
SOCK_STREAM,
0),
FileDescriptor::FDType::Socket);
if (!fd) {
return nullptr;
}
fd.setCloExec();
un.sun_family = PF_LOCAL;
memcpy(un.sun_path, path, strlen(path) + 1);
retry_connect:
if (::connect(fd.system_handle(), (struct sockaddr*)&un, sizeof(un))) {
#ifdef _WIN32
int win_err = WSAGetLastError();
int err = map_win32_err(win_err);
#else
int err = errno;
#endif
if (err == ECONNREFUSED || err == ENOENT) {
if (attempts++ < max_attempts) {
/* sleep override */ std::this_thread::sleep_for(
std::chrono::microseconds(10000));
goto retry_connect;
}
}
errno = err;
return nullptr;
}
int bufsize = WATCHMAN_IO_BUF_SIZE;
::setsockopt(
fd.system_handle(),
SOL_SOCKET,
SO_RCVBUF,
reinterpret_cast<const char*>(&bufsize),
sizeof(bufsize));
return w_stm_fdopen(std::move(fd));
}
#ifndef _WIN32
std::unique_ptr<watchman_stream>
w_stm_open(const char* filename, int flags, ...) {
int mode = 0;
// If we're creating, pull out the mode flag
if (flags & O_CREAT) {
va_list ap;
va_start(ap, flags);
mode = va_arg(ap, int);
va_end(ap);
}
return w_stm_fdopen(FileDescriptor(
open(filename, flags, mode), FileDescriptor::FDType::Unknown));
}
#endif