xref: /freebsd-src/contrib/llvm-project/clang/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- DirectoryWatcher-linux.cpp - Linux-platform directory watching -----===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "DirectoryScanner.h"
100b57cec5SDimitry Andric #include "clang/DirectoryWatcher/DirectoryWatcher.h"
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
130b57cec5SDimitry Andric #include "llvm/ADT/ScopeExit.h"
140b57cec5SDimitry Andric #include "llvm/Support/AlignOf.h"
150b57cec5SDimitry Andric #include "llvm/Support/Errno.h"
16a7dea167SDimitry Andric #include "llvm/Support/Error.h"
170b57cec5SDimitry Andric #include "llvm/Support/Path.h"
180b57cec5SDimitry Andric #include <atomic>
190b57cec5SDimitry Andric #include <condition_variable>
200b57cec5SDimitry Andric #include <mutex>
210b57cec5SDimitry Andric #include <queue>
220b57cec5SDimitry Andric #include <string>
230b57cec5SDimitry Andric #include <thread>
240b57cec5SDimitry Andric #include <vector>
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric #include <fcntl.h>
275f757f3fSDimitry Andric #include <limits.h>
28bdd1243dSDimitry Andric #include <optional>
290b57cec5SDimitry Andric #include <sys/epoll.h>
300b57cec5SDimitry Andric #include <sys/inotify.h>
310b57cec5SDimitry Andric #include <unistd.h>
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric namespace {
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric using namespace llvm;
360b57cec5SDimitry Andric using namespace clang;
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric /// Pipe for inter-thread synchronization - for epoll-ing on multiple
390b57cec5SDimitry Andric /// conditions. It is meant for uni-directional 1:1 signalling - specifically:
400b57cec5SDimitry Andric /// no multiple consumers, no data passing. Thread waiting for signal should
410b57cec5SDimitry Andric /// poll the FDRead. Signalling thread should call signal() which writes single
420b57cec5SDimitry Andric /// character to FDRead.
430b57cec5SDimitry Andric struct SemaphorePipe {
440b57cec5SDimitry Andric   // Expects two file-descriptors opened as a pipe in the canonical POSIX
450b57cec5SDimitry Andric   // order: pipefd[0] refers to the read end of the pipe. pipefd[1] refers to
460b57cec5SDimitry Andric   // the write end of the pipe.
470b57cec5SDimitry Andric   SemaphorePipe(int pipefd[2])
480b57cec5SDimitry Andric       : FDRead(pipefd[0]), FDWrite(pipefd[1]), OwnsFDs(true) {}
490b57cec5SDimitry Andric   SemaphorePipe(const SemaphorePipe &) = delete;
500b57cec5SDimitry Andric   void operator=(const SemaphorePipe &) = delete;
510b57cec5SDimitry Andric   SemaphorePipe(SemaphorePipe &&other)
520b57cec5SDimitry Andric       : FDRead(other.FDRead), FDWrite(other.FDWrite),
530b57cec5SDimitry Andric         OwnsFDs(other.OwnsFDs) // Someone could have moved from the other
540b57cec5SDimitry Andric                                // instance before.
550b57cec5SDimitry Andric   {
560b57cec5SDimitry Andric     other.OwnsFDs = false;
570b57cec5SDimitry Andric   };
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   void signal() {
600b57cec5SDimitry Andric #ifndef NDEBUG
610b57cec5SDimitry Andric     ssize_t Result =
620b57cec5SDimitry Andric #endif
630b57cec5SDimitry Andric     llvm::sys::RetryAfterSignal(-1, write, FDWrite, "A", 1);
640b57cec5SDimitry Andric     assert(Result != -1);
650b57cec5SDimitry Andric   }
660b57cec5SDimitry Andric   ~SemaphorePipe() {
670b57cec5SDimitry Andric     if (OwnsFDs) {
680b57cec5SDimitry Andric       close(FDWrite);
690b57cec5SDimitry Andric       close(FDRead);
700b57cec5SDimitry Andric     }
710b57cec5SDimitry Andric   }
720b57cec5SDimitry Andric   const int FDRead;
730b57cec5SDimitry Andric   const int FDWrite;
740b57cec5SDimitry Andric   bool OwnsFDs;
750b57cec5SDimitry Andric 
76bdd1243dSDimitry Andric   static std::optional<SemaphorePipe> create() {
770b57cec5SDimitry Andric     int InotifyPollingStopperFDs[2];
780b57cec5SDimitry Andric     if (pipe2(InotifyPollingStopperFDs, O_CLOEXEC) == -1)
79bdd1243dSDimitry Andric       return std::nullopt;
800b57cec5SDimitry Andric     return SemaphorePipe(InotifyPollingStopperFDs);
810b57cec5SDimitry Andric   }
820b57cec5SDimitry Andric };
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric /// Mutex-protected queue of Events.
850b57cec5SDimitry Andric class EventQueue {
860b57cec5SDimitry Andric   std::mutex Mtx;
870b57cec5SDimitry Andric   std::condition_variable NonEmpty;
880b57cec5SDimitry Andric   std::queue<DirectoryWatcher::Event> Events;
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric public:
910b57cec5SDimitry Andric   void push_back(const DirectoryWatcher::Event::EventKind K,
920b57cec5SDimitry Andric                  StringRef Filename) {
930b57cec5SDimitry Andric     {
940b57cec5SDimitry Andric       std::unique_lock<std::mutex> L(Mtx);
950b57cec5SDimitry Andric       Events.emplace(K, Filename);
960b57cec5SDimitry Andric     }
970b57cec5SDimitry Andric     NonEmpty.notify_one();
980b57cec5SDimitry Andric   }
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   // Blocks on caller thread and uses codition_variable to wait until there's an
1010b57cec5SDimitry Andric   // event to return.
1020b57cec5SDimitry Andric   DirectoryWatcher::Event pop_front_blocking() {
1030b57cec5SDimitry Andric     std::unique_lock<std::mutex> L(Mtx);
1040b57cec5SDimitry Andric     while (true) {
1050b57cec5SDimitry Andric       // Since we might have missed all the prior notifications on NonEmpty we
1060b57cec5SDimitry Andric       // have to check the queue first (under lock).
1070b57cec5SDimitry Andric       if (!Events.empty()) {
1080b57cec5SDimitry Andric         DirectoryWatcher::Event Front = Events.front();
1090b57cec5SDimitry Andric         Events.pop();
1100b57cec5SDimitry Andric         return Front;
1110b57cec5SDimitry Andric       }
1120b57cec5SDimitry Andric       NonEmpty.wait(L, [this]() { return !Events.empty(); });
1130b57cec5SDimitry Andric     }
1140b57cec5SDimitry Andric   }
1150b57cec5SDimitry Andric };
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric class DirectoryWatcherLinux : public clang::DirectoryWatcher {
1180b57cec5SDimitry Andric public:
1190b57cec5SDimitry Andric   DirectoryWatcherLinux(
1200b57cec5SDimitry Andric       llvm::StringRef WatchedDirPath,
1210b57cec5SDimitry Andric       std::function<void(llvm::ArrayRef<Event>, bool)> Receiver,
1220b57cec5SDimitry Andric       bool WaitForInitialSync, int InotifyFD, int InotifyWD,
1230b57cec5SDimitry Andric       SemaphorePipe &&InotifyPollingStopSignal);
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric   ~DirectoryWatcherLinux() override {
1260b57cec5SDimitry Andric     StopWork();
1270b57cec5SDimitry Andric     InotifyPollingThread.join();
1280b57cec5SDimitry Andric     EventsReceivingThread.join();
1290b57cec5SDimitry Andric     inotify_rm_watch(InotifyFD, InotifyWD);
1300b57cec5SDimitry Andric     llvm::sys::RetryAfterSignal(-1, close, InotifyFD);
1310b57cec5SDimitry Andric   }
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric private:
1340b57cec5SDimitry Andric   const std::string WatchedDirPath;
1350b57cec5SDimitry Andric   // inotify file descriptor
1360b57cec5SDimitry Andric   int InotifyFD = -1;
1370b57cec5SDimitry Andric   // inotify watch descriptor
1380b57cec5SDimitry Andric   int InotifyWD = -1;
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   EventQueue Queue;
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   // Make sure lifetime of Receiver fully contains lifetime of
1430b57cec5SDimitry Andric   // EventsReceivingThread.
1440b57cec5SDimitry Andric   std::function<void(llvm::ArrayRef<Event>, bool)> Receiver;
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   // Consumes inotify events and pushes directory watcher events to the Queue.
1470b57cec5SDimitry Andric   void InotifyPollingLoop();
1480b57cec5SDimitry Andric   std::thread InotifyPollingThread;
1490b57cec5SDimitry Andric   // Using pipe so we can epoll two file descriptors at once - inotify and
1500b57cec5SDimitry Andric   // stopping condition.
1510b57cec5SDimitry Andric   SemaphorePipe InotifyPollingStopSignal;
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric   // Does the initial scan of the directory - directly calling Receiver,
1540b57cec5SDimitry Andric   // bypassing the Queue. Both InitialScan and EventReceivingLoop use Receiver
1550b57cec5SDimitry Andric   // which isn't necessarily thread-safe.
1560b57cec5SDimitry Andric   void InitialScan();
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric   // Processing events from the Queue.
1590b57cec5SDimitry Andric   // In case client doesn't want to do the initial scan synchronously
1600b57cec5SDimitry Andric   // (WaitForInitialSync=false in ctor) we do the initial scan at the beginning
1610b57cec5SDimitry Andric   // of this thread.
1620b57cec5SDimitry Andric   std::thread EventsReceivingThread;
1630b57cec5SDimitry Andric   // Push event of WatcherGotInvalidated kind to the Queue to stop the loop.
1640b57cec5SDimitry Andric   // Both InitialScan and EventReceivingLoop use Receiver which isn't
1650b57cec5SDimitry Andric   // necessarily thread-safe.
1660b57cec5SDimitry Andric   void EventReceivingLoop();
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric   // Stops all the async work. Reentrant.
1690b57cec5SDimitry Andric   void StopWork() {
1700b57cec5SDimitry Andric     Queue.push_back(DirectoryWatcher::Event::EventKind::WatcherGotInvalidated,
1710b57cec5SDimitry Andric                     "");
1720b57cec5SDimitry Andric     InotifyPollingStopSignal.signal();
1730b57cec5SDimitry Andric   }
1740b57cec5SDimitry Andric };
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric void DirectoryWatcherLinux::InotifyPollingLoop() {
1770b57cec5SDimitry Andric   // We want to be able to read ~30 events at once even in the worst case
1780b57cec5SDimitry Andric   // (obscenely long filenames).
1790b57cec5SDimitry Andric   constexpr size_t EventBufferLength =
1800b57cec5SDimitry Andric       30 * (sizeof(struct inotify_event) + NAME_MAX + 1);
1810b57cec5SDimitry Andric   // http://man7.org/linux/man-pages/man7/inotify.7.html
1820b57cec5SDimitry Andric   // Some systems cannot read integer variables if they are not
1830b57cec5SDimitry Andric   // properly aligned. On other systems, incorrect alignment may
1840b57cec5SDimitry Andric   // decrease performance. Hence, the buffer used for reading from
1850b57cec5SDimitry Andric   // the inotify file descriptor should have the same alignment as
1860b57cec5SDimitry Andric   // struct inotify_event.
1870b57cec5SDimitry Andric 
188a7dea167SDimitry Andric   struct Buffer {
189a7dea167SDimitry Andric     alignas(struct inotify_event) char buffer[EventBufferLength];
190a7dea167SDimitry Andric   };
191a7dea167SDimitry Andric   auto ManagedBuffer = std::make_unique<Buffer>();
1920b57cec5SDimitry Andric   char *const Buf = ManagedBuffer->buffer;
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric   const int EpollFD = epoll_create1(EPOLL_CLOEXEC);
1950b57cec5SDimitry Andric   if (EpollFD == -1) {
1960b57cec5SDimitry Andric     StopWork();
1970b57cec5SDimitry Andric     return;
1980b57cec5SDimitry Andric   }
1990b57cec5SDimitry Andric   auto EpollFDGuard = llvm::make_scope_exit([EpollFD]() { close(EpollFD); });
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric   struct epoll_event EventSpec;
2020b57cec5SDimitry Andric   EventSpec.events = EPOLLIN;
2030b57cec5SDimitry Andric   EventSpec.data.fd = InotifyFD;
2040b57cec5SDimitry Andric   if (epoll_ctl(EpollFD, EPOLL_CTL_ADD, InotifyFD, &EventSpec) == -1) {
2050b57cec5SDimitry Andric     StopWork();
2060b57cec5SDimitry Andric     return;
2070b57cec5SDimitry Andric   }
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric   EventSpec.data.fd = InotifyPollingStopSignal.FDRead;
2100b57cec5SDimitry Andric   if (epoll_ctl(EpollFD, EPOLL_CTL_ADD, InotifyPollingStopSignal.FDRead,
2110b57cec5SDimitry Andric                 &EventSpec) == -1) {
2120b57cec5SDimitry Andric     StopWork();
2130b57cec5SDimitry Andric     return;
2140b57cec5SDimitry Andric   }
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric   std::array<struct epoll_event, 2> EpollEventBuffer;
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric   while (true) {
2190b57cec5SDimitry Andric     const int EpollWaitResult = llvm::sys::RetryAfterSignal(
2200b57cec5SDimitry Andric         -1, epoll_wait, EpollFD, EpollEventBuffer.data(),
2210b57cec5SDimitry Andric         EpollEventBuffer.size(), /*timeout=*/-1 /*== infinity*/);
2220b57cec5SDimitry Andric     if (EpollWaitResult == -1) {
2230b57cec5SDimitry Andric       StopWork();
2240b57cec5SDimitry Andric       return;
2250b57cec5SDimitry Andric     }
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric     // Multiple epoll_events can be received for a single file descriptor per
2280b57cec5SDimitry Andric     // epoll_wait call.
2290b57cec5SDimitry Andric     for (int i = 0; i < EpollWaitResult; ++i) {
2300b57cec5SDimitry Andric       if (EpollEventBuffer[i].data.fd == InotifyPollingStopSignal.FDRead) {
2310b57cec5SDimitry Andric         StopWork();
2320b57cec5SDimitry Andric         return;
2330b57cec5SDimitry Andric       }
2340b57cec5SDimitry Andric     }
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric     // epoll_wait() always return either error or >0 events. Since there was no
2370b57cec5SDimitry Andric     // event for stopping, it must be an inotify event ready for reading.
2380b57cec5SDimitry Andric     ssize_t NumRead = llvm::sys::RetryAfterSignal(-1, read, InotifyFD, Buf,
2390b57cec5SDimitry Andric                                                   EventBufferLength);
2400b57cec5SDimitry Andric     for (char *P = Buf; P < Buf + NumRead;) {
2410b57cec5SDimitry Andric       if (P + sizeof(struct inotify_event) > Buf + NumRead) {
2420b57cec5SDimitry Andric         StopWork();
2430b57cec5SDimitry Andric         llvm_unreachable("an incomplete inotify_event was read");
2440b57cec5SDimitry Andric         return;
2450b57cec5SDimitry Andric       }
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric       struct inotify_event *Event = reinterpret_cast<struct inotify_event *>(P);
2480b57cec5SDimitry Andric       P += sizeof(struct inotify_event) + Event->len;
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric       if (Event->mask & (IN_CREATE | IN_MODIFY | IN_MOVED_TO | IN_DELETE) &&
2510b57cec5SDimitry Andric           Event->len <= 0) {
2520b57cec5SDimitry Andric         StopWork();
2530b57cec5SDimitry Andric         llvm_unreachable("expected a filename from inotify");
2540b57cec5SDimitry Andric         return;
2550b57cec5SDimitry Andric       }
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric       if (Event->mask & (IN_CREATE | IN_MOVED_TO | IN_MODIFY)) {
2580b57cec5SDimitry Andric         Queue.push_back(DirectoryWatcher::Event::EventKind::Modified,
2590b57cec5SDimitry Andric                         Event->name);
2600b57cec5SDimitry Andric       } else if (Event->mask & (IN_DELETE | IN_MOVED_FROM)) {
2610b57cec5SDimitry Andric         Queue.push_back(DirectoryWatcher::Event::EventKind::Removed,
2620b57cec5SDimitry Andric                         Event->name);
2630b57cec5SDimitry Andric       } else if (Event->mask & (IN_DELETE_SELF | IN_MOVE_SELF)) {
2640b57cec5SDimitry Andric         Queue.push_back(DirectoryWatcher::Event::EventKind::WatchedDirRemoved,
2650b57cec5SDimitry Andric                         "");
2660b57cec5SDimitry Andric         StopWork();
2670b57cec5SDimitry Andric         return;
2680b57cec5SDimitry Andric       } else if (Event->mask & IN_IGNORED) {
2690b57cec5SDimitry Andric         StopWork();
2700b57cec5SDimitry Andric         return;
2710b57cec5SDimitry Andric       } else {
2720b57cec5SDimitry Andric         StopWork();
2730b57cec5SDimitry Andric         llvm_unreachable("Unknown event type.");
2740b57cec5SDimitry Andric         return;
2750b57cec5SDimitry Andric       }
2760b57cec5SDimitry Andric     }
2770b57cec5SDimitry Andric   }
2780b57cec5SDimitry Andric }
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric void DirectoryWatcherLinux::InitialScan() {
2810b57cec5SDimitry Andric   this->Receiver(getAsFileEvents(scanDirectory(WatchedDirPath)),
2820b57cec5SDimitry Andric                  /*IsInitial=*/true);
2830b57cec5SDimitry Andric }
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric void DirectoryWatcherLinux::EventReceivingLoop() {
2860b57cec5SDimitry Andric   while (true) {
2870b57cec5SDimitry Andric     DirectoryWatcher::Event Event = this->Queue.pop_front_blocking();
2880b57cec5SDimitry Andric     this->Receiver(Event, false);
2890b57cec5SDimitry Andric     if (Event.Kind ==
2900b57cec5SDimitry Andric         DirectoryWatcher::Event::EventKind::WatcherGotInvalidated) {
2910b57cec5SDimitry Andric       StopWork();
2920b57cec5SDimitry Andric       return;
2930b57cec5SDimitry Andric     }
2940b57cec5SDimitry Andric   }
2950b57cec5SDimitry Andric }
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric DirectoryWatcherLinux::DirectoryWatcherLinux(
2980b57cec5SDimitry Andric     StringRef WatchedDirPath,
2990b57cec5SDimitry Andric     std::function<void(llvm::ArrayRef<Event>, bool)> Receiver,
3000b57cec5SDimitry Andric     bool WaitForInitialSync, int InotifyFD, int InotifyWD,
3010b57cec5SDimitry Andric     SemaphorePipe &&InotifyPollingStopSignal)
3020b57cec5SDimitry Andric     : WatchedDirPath(WatchedDirPath), InotifyFD(InotifyFD),
3030b57cec5SDimitry Andric       InotifyWD(InotifyWD), Receiver(Receiver),
3040b57cec5SDimitry Andric       InotifyPollingStopSignal(std::move(InotifyPollingStopSignal)) {
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric   InotifyPollingThread = std::thread([this]() { InotifyPollingLoop(); });
3070b57cec5SDimitry Andric   // We have no guarantees about thread safety of the Receiver which is being
3080b57cec5SDimitry Andric   // used in both InitialScan and EventReceivingLoop. We shouldn't run these
3090b57cec5SDimitry Andric   // only synchronously.
3100b57cec5SDimitry Andric   if (WaitForInitialSync) {
3110b57cec5SDimitry Andric     InitialScan();
3120b57cec5SDimitry Andric     EventsReceivingThread = std::thread([this]() { EventReceivingLoop(); });
3130b57cec5SDimitry Andric   } else {
3140b57cec5SDimitry Andric     EventsReceivingThread = std::thread([this]() {
3150b57cec5SDimitry Andric       // FIXME: We might want to terminate an async initial scan early in case
3160b57cec5SDimitry Andric       // of a failure in EventsReceivingThread.
3170b57cec5SDimitry Andric       InitialScan();
3180b57cec5SDimitry Andric       EventReceivingLoop();
3190b57cec5SDimitry Andric     });
3200b57cec5SDimitry Andric   }
3210b57cec5SDimitry Andric }
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric } // namespace
3240b57cec5SDimitry Andric 
325a7dea167SDimitry Andric llvm::Expected<std::unique_ptr<DirectoryWatcher>> clang::DirectoryWatcher::create(
3260b57cec5SDimitry Andric     StringRef Path,
3270b57cec5SDimitry Andric     std::function<void(llvm::ArrayRef<DirectoryWatcher::Event>, bool)> Receiver,
3280b57cec5SDimitry Andric     bool WaitForInitialSync) {
3290b57cec5SDimitry Andric   if (Path.empty())
330a7dea167SDimitry Andric     llvm::report_fatal_error(
331a7dea167SDimitry Andric         "DirectoryWatcher::create can not accept an empty Path.");
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric   const int InotifyFD = inotify_init1(IN_CLOEXEC);
3340b57cec5SDimitry Andric   if (InotifyFD == -1)
335a7dea167SDimitry Andric     return llvm::make_error<llvm::StringError>(
336*0fca6ea1SDimitry Andric         llvm::errnoAsErrorCode(), std::string(": inotify_init1()"));
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric   const int InotifyWD = inotify_add_watch(
3390b57cec5SDimitry Andric       InotifyFD, Path.str().c_str(),
3400b57cec5SDimitry Andric       IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MODIFY |
3410b57cec5SDimitry Andric       IN_MOVED_FROM | IN_MOVE_SELF | IN_MOVED_TO | IN_ONLYDIR | IN_IGNORED
3420b57cec5SDimitry Andric #ifdef IN_EXCL_UNLINK
3430b57cec5SDimitry Andric       | IN_EXCL_UNLINK
3440b57cec5SDimitry Andric #endif
3450b57cec5SDimitry Andric       );
3460b57cec5SDimitry Andric   if (InotifyWD == -1)
347a7dea167SDimitry Andric     return llvm::make_error<llvm::StringError>(
348*0fca6ea1SDimitry Andric         llvm::errnoAsErrorCode(), std::string(": inotify_add_watch()"));
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric   auto InotifyPollingStopper = SemaphorePipe::create();
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric   if (!InotifyPollingStopper)
353a7dea167SDimitry Andric     return llvm::make_error<llvm::StringError>(
354*0fca6ea1SDimitry Andric         llvm::errnoAsErrorCode(), std::string(": SemaphorePipe::create()"));
3550b57cec5SDimitry Andric 
356a7dea167SDimitry Andric   return std::make_unique<DirectoryWatcherLinux>(
3570b57cec5SDimitry Andric       Path, Receiver, WaitForInitialSync, InotifyFD, InotifyWD,
3580b57cec5SDimitry Andric       std::move(*InotifyPollingStopper));
3590b57cec5SDimitry Andric }
360