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