/
linux_fsnotifier.cpp
411 lines (358 loc) · 14.1 KB
/
linux_fsnotifier.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
#ifdef __linux__
#include <codecvt>
#include <dlfcn.h>
#include <locale>
#include <string>
#include <sys/ioctl.h>
#include <unistd.h>
#include "linux_fsnotifier.h"
#define EVENT_BUFFER_SIZE (16 * 1024)
#define EVENT_MASK (IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_EXCL_UNLINK | IN_MODIFY | IN_MOVE_SELF | IN_MOVED_FROM | IN_MOVED_TO | IN_ONLYDIR)
InotifyInstanceLimitTooLowException::InotifyInstanceLimitTooLowException()
: InsufficientResourcesFileWatcherException("Inotify instance limit too low") {
}
InotifyWatchesLimitTooLowException::InotifyWatchesLimitTooLowException()
: InsufficientResourcesFileWatcherException("Inotify watches limit too low") {
}
WatchPoint::WatchPoint(const u16string& path, shared_ptr<Inotify> inotify, int watchDescriptor, ino_t inode)
: status(WatchPointStatus::LISTENING)
, watchDescriptor(watchDescriptor)
, inotify(inotify)
, path(path)
, inode(inode) {
}
CancelResult WatchPoint::cancel() {
if (status == WatchPointStatus::CANCELLED) {
return CancelResult::ALREADY_CANCELLED;
}
status = WatchPointStatus::CANCELLED;
if (inotify_rm_watch(inotify->fd, watchDescriptor) != 0) {
switch (errno) {
case EINVAL:
logToJava(LogLevel::INFO, "Couldn't stop watching %s (probably because the directory was removed)", utf16ToUtf8String(path).c_str());
return CancelResult::NOT_CANCELLED;
break;
default:
throw FileWatcherException("Couldn't stop watching", path, errno);
}
}
return CancelResult::CANCELLED;
}
Inotify::Inotify()
: fd(inotify_init1(IN_CLOEXEC | IN_NONBLOCK)) {
if (fd == -1) {
if (errno == EMFILE) {
throw InotifyInstanceLimitTooLowException();
}
throw FileWatcherException("Couldn't register inotify handle", errno);
}
}
Inotify::~Inotify() {
close(fd);
}
ShutdownEvent::ShutdownEvent()
: fd(eventfd(0, 0)) {
if (fd == -1) {
throw FileWatcherException("Couldn't register event source", errno);
}
}
ShutdownEvent::~ShutdownEvent() {
close(fd);
}
void ShutdownEvent::trigger() const {
const uint64_t increment = 1;
write(fd, &increment, sizeof(increment));
}
void ShutdownEvent::consume() const {
uint64_t counter;
ssize_t bytesRead = read(fd, &counter, sizeof(counter));
if (bytesRead == -1) {
throw FileWatcherException("Couldn't read from termination event notifier", errno);
}
}
Server::Server(JNIEnv* env, jobject watcherCallback)
: AbstractServer(env, watcherCallback)
, inotify(new Inotify()) {
buffer.reserve(EVENT_BUFFER_SIZE);
jclass listClass = env->FindClass("java/util/List");
this->listAddMethod = env->GetMethodID(listClass, "add", "(Ljava/lang/Object;)Z");
}
void Server::initializeRunLoop() {
}
void Server::shutdownRunLoop() {
shutdownEvent.trigger();
}
void Server::runLoop() {
int forever = numeric_limits<int>::max();
while (!shouldTerminate) {
processQueues(forever);
}
// No need to clean up watch points, they will be cancelled
// and closed when the Inotify destructs
}
void Server::processQueues(int timeout) {
struct pollfd fds[2];
fds[0].fd = shutdownEvent.fd;
fds[1].fd = inotify->fd;
fds[0].events = POLLIN;
fds[1].events = POLLIN;
int ret = poll(fds, 2, timeout);
if (ret == -1) {
throw FileWatcherException("Couldn't poll for events", errno);
}
if (IS_SET(fds[0].revents, POLLIN)) {
shutdownEvent.consume();
// Ignore counter, we only care about the notification itself
shouldTerminate = true;
return;
}
if (IS_SET(fds[1].revents, POLLIN)) {
try {
handleEvents();
} catch (const exception& ex) {
reportFailure(getThreadEnv(), ex);
}
}
}
void Server::handleEvents() {
unsigned int available;
ioctl(inotify->fd, FIONREAD, &available);
while (available > 0) {
ssize_t bytesRead = read(inotify->fd, &buffer[0], buffer.capacity());
switch (bytesRead) {
case -1:
if (errno == EAGAIN) {
// For a non-blocking read, we receive EAGAIN here if there is nothing to read.
// This may happen when the inotify is already closed.
return;
} else {
throw FileWatcherException("Couldn't read from inotify", errno);
}
break;
case 0:
throw FileWatcherException("EOF reading from inotify", errno);
break;
default:
// Handle events
unique_lock<recursive_mutex> lock(mutationMutex);
JNIEnv* env = getThreadEnv();
logToJava(LogLevel::FINE, "Processing %d bytes worth of events", bytesRead);
int index = 0;
int count = 0;
while (index < bytesRead) {
const struct inotify_event* event = (struct inotify_event*) &buffer[index];
handleEvent(env, event);
index += sizeof(struct inotify_event) + event->len;
count++;
}
logToJava(LogLevel::FINE, "Processed %d events", count);
break;
}
available -= bytesRead;
}
}
void Server::handleEvent(JNIEnv* env, const inotify_event* event) {
uint32_t mask = event->mask;
const char* eventName = (event->len == 0)
? ""
: event->name;
logToJava(LogLevel::FINE, "Event mask: 0x%x for %s (wd = %d, cookie = 0x%x, len = %d)", mask, eventName, event->wd, event->cookie, event->len);
if (IS_SET(mask, IN_UNMOUNT)) {
return;
}
// Overflow received, handle gracefully
if (IS_SET(mask, IN_Q_OVERFLOW)) {
for (auto it : watchPoints) {
auto path = it.first;
reportOverflow(env, path);
}
return;
}
auto iWatchRoot = watchRoots.find(event->wd);
if (iWatchRoot == watchRoots.end()) {
auto iRecentlyUnregisteredWatchPoint = recentlyUnregisteredWatchRoots.find(event->wd);
if (iRecentlyUnregisteredWatchPoint == recentlyUnregisteredWatchRoots.end()) {
logToJava(LogLevel::INFO, "Received event for unknown watch descriptor %d", event->wd);
} else {
// We've removed this via unregisterPath() not long ago
auto& path = iRecentlyUnregisteredWatchPoint->second;
if (IS_SET(mask, IN_IGNORED)) {
logToJava(LogLevel::FINE, "Finished watching recently unregistered watch point '%s' (wd = %d)",
utf16ToUtf8String(path).c_str(), event->wd);
recentlyUnregisteredWatchRoots.erase(iRecentlyUnregisteredWatchPoint);
} else {
logToJava(LogLevel::FINE, "Ignoring incoming events for recently removed watch descriptor for '%s' (wd = %d)",
utf16ToUtf8String(path).c_str(), event->wd);
}
}
return;
}
auto path = iWatchRoot->second;
auto& watchPoint = watchPoints.at(path);
if (IS_SET(mask, IN_IGNORED)) {
// Finished with watch point
logToJava(LogLevel::FINE, "Finished watching still registered '%s' (wd = %d)",
utf16ToUtf8String(path).c_str(), event->wd);
watchRoots.erase(event->wd);
watchPoints.erase(path);
return;
}
if (watchPoint.status != WatchPointStatus::LISTENING) {
logToJava(LogLevel::FINE, "Ignoring incoming events for %s as watch-point is not listening (status = %d)",
utf16ToUtf8String(path).c_str(), watchPoint.status);
return;
}
if (shouldTerminate) {
logToJava(LogLevel::FINE, "Ignoring incoming events for %s because server is terminating (status = %d)",
utf16ToUtf8String(path).c_str(), watchPoint.status);
return;
}
ChangeType type;
const u16string name = utf8ToUtf16String(eventName);
if (!name.empty()) {
path.append(u"/");
path.append(name);
}
if (IS_SET(mask, IN_CREATE | IN_MOVED_TO)) {
type = ChangeType::CREATED;
} else if (IS_SET(mask, IN_DELETE | IN_DELETE_SELF | IN_MOVED_FROM)) {
type = ChangeType::REMOVED;
} else if (IS_SET(mask, IN_MODIFY)) {
type = ChangeType::MODIFIED;
} else {
logToJava(LogLevel::WARNING, "Unknown event 0x%x for %s", mask, utf16ToUtf8String(path).c_str());
reportUnknownEvent(env, path);
return;
}
reportChangeEvent(env, type, path);
}
void Server::registerPaths(const vector<u16string>& paths) {
unique_lock<recursive_mutex> lock(mutationMutex);
for (auto& path : paths) {
registerPath(path);
}
}
bool Server::unregisterPaths(const vector<u16string>& paths) {
unique_lock<recursive_mutex> lock(mutationMutex);
bool success = true;
for (auto& path : paths) {
success &= unregisterPath(path);
}
return success;
}
void Server::registerPath(const u16string& path) {
auto it = watchPoints.find(path);
if (it != watchPoints.end()) {
throw FileWatcherException("Already watching path", path);
}
string pathNarrow = utf16ToUtf8String(path);
struct stat st;
if (lstat(pathNarrow.c_str(), &st) != 0) {
throw FileWatcherException("Couldn't add watch, stat failed", path, errno);
}
int watchDescriptor = inotify_add_watch(inotify->fd, pathNarrow.c_str(), EVENT_MASK);
if (watchDescriptor == -1) {
if (errno == ENOSPC) {
rethrowAsJavaException(getThreadEnv(), InotifyWatchesLimitTooLowException(), linuxJniConstants->inotifyWatchesLimitTooLowExceptionClass.get());
throw JavaExceptionThrownException();
}
throw FileWatcherException("Couldn't add watch, inotify_add_watch failed", path, errno);
}
if (watchRoots.find(watchDescriptor) != watchRoots.end()) {
throw FileWatcherException("Already watching path", path);
}
watchPoints.emplace(piecewise_construct,
forward_as_tuple(path),
forward_as_tuple(path, inotify, watchDescriptor, st.st_ino));
watchRoots[watchDescriptor] = path;
}
bool Server::unregisterPath(const u16string& path) {
auto it = watchPoints.find(path);
if (it == watchPoints.end()) {
logToJava(LogLevel::INFO, "Path is not watched: %s", utf16ToUtf8String(path).c_str());
return false;
}
auto& watchPoint = it->second;
int wd = watchPoint.watchDescriptor;
CancelResult ret = watchPoint.cancel();
if (ret == CancelResult::ALREADY_CANCELLED) {
return false;
}
recentlyUnregisteredWatchRoots.emplace(wd, path);
watchRoots.erase(wd);
// We use the path instead erase(it) here because on Alpine Linux we've seen crashes happen here
// when inside a Docker container a host-mapped directory is watched. There is no good theory as
// of this writing why the problem occurs, but not using the iterator here fixes it.
watchPoints.erase(path);
return ret == CancelResult::CANCELLED;
}
void Server::stopWatchingMovedPaths(jobjectArray absolutePathsToCheck, jobject droppedPaths) {
JNIEnv* env = getThreadEnv();
int count = env->GetArrayLength(absolutePathsToCheck);
for (int i = 0; i < count; i++) {
jstring jPathToCheck = reinterpret_cast<jstring>(env->GetObjectArrayElement(absolutePathsToCheck, i));
auto pathToCheck = javaToUtf16String(env, jPathToCheck);
auto it = watchPoints.find(pathToCheck);
if (it == watchPoints.end()) {
addToList(env, droppedPaths, jPathToCheck);
env->DeleteLocalRef(jPathToCheck);
continue;
}
auto& watchPoint = it->second;
if (watchPoint.status != WatchPointStatus::LISTENING) {
addToList(env, droppedPaths, jPathToCheck);
env->DeleteLocalRef(jPathToCheck);
continue;
}
string pathNarrow = utf16ToUtf8String(watchPoint.path);
struct stat st;
if (lstat(pathNarrow.c_str(), &st) == 0 && st.st_ino == watchPoint.inode) {
env->DeleteLocalRef(jPathToCheck);
continue;
}
addToList(env, droppedPaths, jPathToCheck);
env->DeleteLocalRef(jPathToCheck);
watchPoint.cancel();
}
}
void Server::addToList(JNIEnv* env, jobject jList, jstring jString) {
env->CallBooleanMethod(jList, listAddMethod, jString);
throwNativeExceptionWhenJavaExceptionOccurred(env);
}
JNIEXPORT jobject JNICALL
Java_net_rubygrapefruit_platform_internal_jni_LinuxFileEventFunctions_startWatcher0(JNIEnv* env, jclass, jobject javaCallback) {
try {
return wrapServer(env, new Server(env, javaCallback));
} catch (const InotifyInstanceLimitTooLowException& e) {
rethrowAsJavaException(env, e, linuxJniConstants->inotifyInstanceLimitTooLowExceptionClass.get());
return NULL;
}
}
JNIEXPORT jboolean JNICALL
Java_net_rubygrapefruit_platform_internal_jni_LinuxFileEventFunctions_isGlibc0(JNIEnv*, jclass) {
void* libcLibrary = dlopen("libc.so.6", RTLD_LAZY);
if (!libcLibrary) {
return false;
}
void* libcVerCheck = dlsym(libcLibrary, "gnu_get_libc_version");
jboolean isValid = libcVerCheck != NULL;
dlclose(libcLibrary);
return isValid;
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_LinuxFileEventFunctions_00024LinuxFileWatcher_stopWatchingMovedPaths0(JNIEnv* env, jobject, jobject javaServer, jobjectArray jAbsolutePathsToCheck, jobject jDroppedPaths) {
try {
Server* server = (Server*) getServer(env, javaServer);
server->stopWatchingMovedPaths(jAbsolutePathsToCheck, jDroppedPaths);
} catch (const JavaExceptionThrownException&) {
// Ignore, the Java exception has already been thrown.
} catch (const exception& e) {
rethrowAsJavaException(env, e);
}
}
LinuxJniConstants::LinuxJniConstants(JavaVM* jvm)
: JniSupport(jvm)
, inotifyWatchesLimitTooLowExceptionClass(getThreadEnv(), "net/rubygrapefruit/platform/internal/jni/InotifyWatchesLimitTooLowException")
, inotifyInstanceLimitTooLowExceptionClass(getThreadEnv(), "net/rubygrapefruit/platform/internal/jni/InotifyInstanceLimitTooLowException") {
}
#endif