1 //===- DirectoryWatcher-linux.cpp - Linux-platform directory watching -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "DirectoryScanner.h"
10 #include "clang/DirectoryWatcher/DirectoryWatcher.h"
11 
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/ScopeExit.h"
14 #include "llvm/Support/AlignOf.h"
15 #include "llvm/Support/Errno.h"
16 #include "llvm/Support/Mutex.h"
17 #include "llvm/Support/Path.h"
18 #include <atomic>
19 #include <condition_variable>
20 #include <mutex>
21 #include <queue>
22 #include <string>
23 #include <thread>
24 #include <vector>
25 
26 #include <fcntl.h>
27 #include <linux/version.h>
28 #include <sys/epoll.h>
29 #include <sys/inotify.h>
30 #include <unistd.h>
31 
32 namespace {
33 
34 using namespace llvm;
35 using namespace clang;
36 
37 /// Pipe for inter-thread synchronization - for epoll-ing on multiple
38 /// conditions. It is meant for uni-directional 1:1 signalling - specifically:
39 /// no multiple consumers, no data passing. Thread waiting for signal should
40 /// poll the FDRead. Signalling thread should call signal() which writes single
41 /// character to FDRead.
42 struct SemaphorePipe {
43   // Expects two file-descriptors opened as a pipe in the canonical POSIX
44   // order: pipefd[0] refers to the read end of the pipe. pipefd[1] refers to
45   // the write end of the pipe.
46   SemaphorePipe(int pipefd[2])
47       : FDRead(pipefd[0]), FDWrite(pipefd[1]), OwnsFDs(true) {}
48   SemaphorePipe(const SemaphorePipe &) = delete;
49   void operator=(const SemaphorePipe &) = delete;
50   SemaphorePipe(SemaphorePipe &&other)
51       : FDRead(other.FDRead), FDWrite(other.FDWrite),
52         OwnsFDs(other.OwnsFDs) // Someone could have moved from the other
53                                // instance before.
54   {
55     other.OwnsFDs = false;
56   };
57 
58   void signal() {
59 #ifndef NDEBUG
60     ssize_t Result =
61 #endif
62     llvm::sys::RetryAfterSignal(-1, write, FDWrite, "A", 1);
63     assert(Result != -1);
64   }
65   ~SemaphorePipe() {
66     if (OwnsFDs) {
67       close(FDWrite);
68       close(FDRead);
69     }
70   }
71   const int FDRead;
72   const int FDWrite;
73   bool OwnsFDs;
74 
75   static llvm::Optional<SemaphorePipe> create() {
76     int InotifyPollingStopperFDs[2];
77     if (pipe2(InotifyPollingStopperFDs, O_CLOEXEC) == -1)
78       return llvm::None;
79     return SemaphorePipe(InotifyPollingStopperFDs);
80   }
81 };
82 
83 /// Mutex-protected queue of Events.
84 class EventQueue {
85   std::mutex Mtx;
86   std::condition_variable NonEmpty;
87   std::queue<DirectoryWatcher::Event> Events;
88 
89 public:
90   void push_back(const DirectoryWatcher::Event::EventKind K,
91                  StringRef Filename) {
92     {
93       std::unique_lock<std::mutex> L(Mtx);
94       Events.emplace(K, Filename);
95     }
96     NonEmpty.notify_one();
97   }
98 
99   // Blocks on caller thread and uses codition_variable to wait until there's an
100   // event to return.
101   DirectoryWatcher::Event pop_front_blocking() {
102     std::unique_lock<std::mutex> L(Mtx);
103     while (true) {
104       // Since we might have missed all the prior notifications on NonEmpty we
105       // have to check the queue first (under lock).
106       if (!Events.empty()) {
107         DirectoryWatcher::Event Front = Events.front();
108         Events.pop();
109         return Front;
110       }
111       NonEmpty.wait(L, [this]() { return !Events.empty(); });
112     }
113   }
114 };
115 
116 class DirectoryWatcherLinux : public clang::DirectoryWatcher {
117 public:
118   DirectoryWatcherLinux(
119       llvm::StringRef WatchedDirPath,
120       std::function<void(llvm::ArrayRef<Event>, bool)> Receiver,
121       bool WaitForInitialSync, int InotifyFD, int InotifyWD,
122       SemaphorePipe &&InotifyPollingStopSignal);
123 
124   ~DirectoryWatcherLinux() override {
125     StopWork();
126     InotifyPollingThread.join();
127     EventsReceivingThread.join();
128     inotify_rm_watch(InotifyFD, InotifyWD);
129     llvm::sys::RetryAfterSignal(-1, close, InotifyFD);
130   }
131 
132 private:
133   const std::string WatchedDirPath;
134   // inotify file descriptor
135   int InotifyFD = -1;
136   // inotify watch descriptor
137   int InotifyWD = -1;
138 
139   EventQueue Queue;
140 
141   // Make sure lifetime of Receiver fully contains lifetime of
142   // EventsReceivingThread.
143   std::function<void(llvm::ArrayRef<Event>, bool)> Receiver;
144 
145   // Consumes inotify events and pushes directory watcher events to the Queue.
146   void InotifyPollingLoop();
147   std::thread InotifyPollingThread;
148   // Using pipe so we can epoll two file descriptors at once - inotify and
149   // stopping condition.
150   SemaphorePipe InotifyPollingStopSignal;
151 
152   // Does the initial scan of the directory - directly calling Receiver,
153   // bypassing the Queue. Both InitialScan and EventReceivingLoop use Receiver
154   // which isn't necessarily thread-safe.
155   void InitialScan();
156 
157   // Processing events from the Queue.
158   // In case client doesn't want to do the initial scan synchronously
159   // (WaitForInitialSync=false in ctor) we do the initial scan at the beginning
160   // of this thread.
161   std::thread EventsReceivingThread;
162   // Push event of WatcherGotInvalidated kind to the Queue to stop the loop.
163   // Both InitialScan and EventReceivingLoop use Receiver which isn't
164   // necessarily thread-safe.
165   void EventReceivingLoop();
166 
167   // Stops all the async work. Reentrant.
168   void StopWork() {
169     Queue.push_back(DirectoryWatcher::Event::EventKind::WatcherGotInvalidated,
170                     "");
171     InotifyPollingStopSignal.signal();
172   }
173 };
174 
175 void DirectoryWatcherLinux::InotifyPollingLoop() {
176   // We want to be able to read ~30 events at once even in the worst case
177   // (obscenely long filenames).
178   constexpr size_t EventBufferLength =
179       30 * (sizeof(struct inotify_event) + NAME_MAX + 1);
180   // http://man7.org/linux/man-pages/man7/inotify.7.html
181   // Some systems cannot read integer variables if they are not
182   // properly aligned. On other systems, incorrect alignment may
183   // decrease performance. Hence, the buffer used for reading from
184   // the inotify file descriptor should have the same alignment as
185   // struct inotify_event.
186 
187   auto ManagedBuffer =
188       llvm::make_unique<llvm::AlignedCharArray<alignof(struct inotify_event),
189                                                EventBufferLength>>();
190   char *const Buf = ManagedBuffer->buffer;
191 
192   const int EpollFD = epoll_create1(EPOLL_CLOEXEC);
193   if (EpollFD == -1) {
194     StopWork();
195     return;
196   }
197   auto EpollFDGuard = llvm::make_scope_exit([EpollFD]() { close(EpollFD); });
198 
199   struct epoll_event EventSpec;
200   EventSpec.events = EPOLLIN;
201   EventSpec.data.fd = InotifyFD;
202   if (epoll_ctl(EpollFD, EPOLL_CTL_ADD, InotifyFD, &EventSpec) == -1) {
203     StopWork();
204     return;
205   }
206 
207   EventSpec.data.fd = InotifyPollingStopSignal.FDRead;
208   if (epoll_ctl(EpollFD, EPOLL_CTL_ADD, InotifyPollingStopSignal.FDRead,
209                 &EventSpec) == -1) {
210     StopWork();
211     return;
212   }
213 
214   std::array<struct epoll_event, 2> EpollEventBuffer;
215 
216   while (true) {
217     const int EpollWaitResult = llvm::sys::RetryAfterSignal(
218         -1, epoll_wait, EpollFD, EpollEventBuffer.data(),
219         EpollEventBuffer.size(), /*timeout=*/-1 /*== infinity*/);
220     if (EpollWaitResult == -1) {
221       StopWork();
222       return;
223     }
224 
225     // Multiple epoll_events can be received for a single file descriptor per
226     // epoll_wait call.
227     for (int i = 0; i < EpollWaitResult; ++i) {
228       if (EpollEventBuffer[i].data.fd == InotifyPollingStopSignal.FDRead) {
229         StopWork();
230         return;
231       }
232     }
233 
234     // epoll_wait() always return either error or >0 events. Since there was no
235     // event for stopping, it must be an inotify event ready for reading.
236     ssize_t NumRead = llvm::sys::RetryAfterSignal(-1, read, InotifyFD, Buf,
237                                                   EventBufferLength);
238     for (char *P = Buf; P < Buf + NumRead;) {
239       if (P + sizeof(struct inotify_event) > Buf + NumRead) {
240         StopWork();
241         llvm_unreachable("an incomplete inotify_event was read");
242         return;
243       }
244 
245       struct inotify_event *Event = reinterpret_cast<struct inotify_event *>(P);
246       P += sizeof(struct inotify_event) + Event->len;
247 
248       if (Event->mask & (IN_CREATE | IN_MODIFY | IN_MOVED_TO | IN_DELETE) &&
249           Event->len <= 0) {
250         StopWork();
251         llvm_unreachable("expected a filename from inotify");
252         return;
253       }
254 
255       if (Event->mask & (IN_CREATE | IN_MOVED_TO | IN_MODIFY)) {
256         Queue.push_back(DirectoryWatcher::Event::EventKind::Modified,
257                         Event->name);
258       } else if (Event->mask & (IN_DELETE | IN_MOVED_FROM)) {
259         Queue.push_back(DirectoryWatcher::Event::EventKind::Removed,
260                         Event->name);
261       } else if (Event->mask & (IN_DELETE_SELF | IN_MOVE_SELF)) {
262         Queue.push_back(DirectoryWatcher::Event::EventKind::WatchedDirRemoved,
263                         "");
264         StopWork();
265         return;
266       } else if (Event->mask & IN_IGNORED) {
267         StopWork();
268         return;
269       } else {
270         StopWork();
271         llvm_unreachable("Unknown event type.");
272         return;
273       }
274     }
275   }
276 }
277 
278 void DirectoryWatcherLinux::InitialScan() {
279   this->Receiver(getAsFileEvents(scanDirectory(WatchedDirPath)),
280                  /*IsInitial=*/true);
281 }
282 
283 void DirectoryWatcherLinux::EventReceivingLoop() {
284   while (true) {
285     DirectoryWatcher::Event Event = this->Queue.pop_front_blocking();
286     this->Receiver(Event, false);
287     if (Event.Kind ==
288         DirectoryWatcher::Event::EventKind::WatcherGotInvalidated) {
289       StopWork();
290       return;
291     }
292   }
293 }
294 
295 DirectoryWatcherLinux::DirectoryWatcherLinux(
296     StringRef WatchedDirPath,
297     std::function<void(llvm::ArrayRef<Event>, bool)> Receiver,
298     bool WaitForInitialSync, int InotifyFD, int InotifyWD,
299     SemaphorePipe &&InotifyPollingStopSignal)
300     : WatchedDirPath(WatchedDirPath), InotifyFD(InotifyFD),
301       InotifyWD(InotifyWD), Receiver(Receiver),
302       InotifyPollingStopSignal(std::move(InotifyPollingStopSignal)) {
303 
304   InotifyPollingThread = std::thread([this]() { InotifyPollingLoop(); });
305   // We have no guarantees about thread safety of the Receiver which is being
306   // used in both InitialScan and EventReceivingLoop. We shouldn't run these
307   // only synchronously.
308   if (WaitForInitialSync) {
309     InitialScan();
310     EventsReceivingThread = std::thread([this]() { EventReceivingLoop(); });
311   } else {
312     EventsReceivingThread = std::thread([this]() {
313       // FIXME: We might want to terminate an async initial scan early in case
314       // of a failure in EventsReceivingThread.
315       InitialScan();
316       EventReceivingLoop();
317     });
318   }
319 }
320 
321 } // namespace
322 
323 std::unique_ptr<DirectoryWatcher> clang::DirectoryWatcher::create(
324     StringRef Path,
325     std::function<void(llvm::ArrayRef<DirectoryWatcher::Event>, bool)> Receiver,
326     bool WaitForInitialSync) {
327   if (Path.empty())
328     return nullptr;
329 
330   const int InotifyFD = inotify_init1(IN_CLOEXEC);
331   if (InotifyFD == -1)
332     return nullptr;
333 
334   const int InotifyWD = inotify_add_watch(
335       InotifyFD, Path.str().c_str(),
336       IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MODIFY |
337       IN_MOVED_FROM | IN_MOVE_SELF | IN_MOVED_TO | IN_ONLYDIR | IN_IGNORED
338 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
339       | IN_EXCL_UNLINK
340 #endif
341       );
342   if (InotifyWD == -1)
343     return nullptr;
344 
345   auto InotifyPollingStopper = SemaphorePipe::create();
346 
347   if (!InotifyPollingStopper)
348     return nullptr;
349 
350   return llvm::make_unique<DirectoryWatcherLinux>(
351       Path, Receiver, WaitForInitialSync, InotifyFD, InotifyWD,
352       std::move(*InotifyPollingStopper));
353 }