xref: /llvm-project/clang/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp (revision ba13fa2a5d57581bff1a7e9322234af30f4882f6)
177dd8a79SJan Korous //===- DirectoryWatcher-linux.cpp - Linux-platform directory watching -----===//
277dd8a79SJan Korous //
377dd8a79SJan Korous // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
477dd8a79SJan Korous // See https://llvm.org/LICENSE.txt for license information.
577dd8a79SJan Korous // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
677dd8a79SJan Korous //
777dd8a79SJan Korous //===----------------------------------------------------------------------===//
877dd8a79SJan Korous 
977dd8a79SJan Korous #include "DirectoryScanner.h"
1077dd8a79SJan Korous #include "clang/DirectoryWatcher/DirectoryWatcher.h"
1177dd8a79SJan Korous 
1277dd8a79SJan Korous #include "llvm/ADT/STLExtras.h"
1377dd8a79SJan Korous #include "llvm/ADT/ScopeExit.h"
14d2ed9d6bSReid Kleckner #include "llvm/Support/AlignOf.h"
1577dd8a79SJan Korous #include "llvm/Support/Errno.h"
16ef74924fSPuyan Lotfi #include "llvm/Support/Error.h"
1777dd8a79SJan Korous #include "llvm/Support/Path.h"
1877dd8a79SJan Korous #include <atomic>
1977dd8a79SJan Korous #include <condition_variable>
2077dd8a79SJan Korous #include <mutex>
2177dd8a79SJan Korous #include <queue>
2277dd8a79SJan Korous #include <string>
2377dd8a79SJan Korous #include <thread>
2477dd8a79SJan Korous #include <vector>
2577dd8a79SJan Korous 
2677dd8a79SJan Korous #include <fcntl.h>
2765a18412SKazu Hirata #include <limits.h>
28e8aee7efSKazu Hirata #include <optional>
2977dd8a79SJan Korous #include <sys/epoll.h>
3077dd8a79SJan Korous #include <sys/inotify.h>
3177dd8a79SJan Korous #include <unistd.h>
3277dd8a79SJan Korous 
3377dd8a79SJan Korous namespace {
3477dd8a79SJan Korous 
3577dd8a79SJan Korous using namespace llvm;
3677dd8a79SJan Korous using namespace clang;
3777dd8a79SJan Korous 
3877dd8a79SJan Korous /// Pipe for inter-thread synchronization - for epoll-ing on multiple
3977dd8a79SJan Korous /// conditions. It is meant for uni-directional 1:1 signalling - specifically:
4077dd8a79SJan Korous /// no multiple consumers, no data passing. Thread waiting for signal should
4177dd8a79SJan Korous /// poll the FDRead. Signalling thread should call signal() which writes single
4277dd8a79SJan Korous /// character to FDRead.
4377dd8a79SJan Korous struct SemaphorePipe {
4477dd8a79SJan Korous   // Expects two file-descriptors opened as a pipe in the canonical POSIX
4577dd8a79SJan Korous   // order: pipefd[0] refers to the read end of the pipe. pipefd[1] refers to
4677dd8a79SJan Korous   // the write end of the pipe.
SemaphorePipe__anone8a8889e0111::SemaphorePipe4777dd8a79SJan Korous   SemaphorePipe(int pipefd[2])
4877dd8a79SJan Korous       : FDRead(pipefd[0]), FDWrite(pipefd[1]), OwnsFDs(true) {}
4977dd8a79SJan Korous   SemaphorePipe(const SemaphorePipe &) = delete;
5077dd8a79SJan Korous   void operator=(const SemaphorePipe &) = delete;
SemaphorePipe__anone8a8889e0111::SemaphorePipe5177dd8a79SJan Korous   SemaphorePipe(SemaphorePipe &&other)
5277dd8a79SJan Korous       : FDRead(other.FDRead), FDWrite(other.FDWrite),
5377dd8a79SJan Korous         OwnsFDs(other.OwnsFDs) // Someone could have moved from the other
5477dd8a79SJan Korous                                // instance before.
5577dd8a79SJan Korous   {
5677dd8a79SJan Korous     other.OwnsFDs = false;
5777dd8a79SJan Korous   };
5877dd8a79SJan Korous 
signal__anone8a8889e0111::SemaphorePipe5977dd8a79SJan Korous   void signal() {
60000ba715SJan Korous #ifndef NDEBUG
61000ba715SJan Korous     ssize_t Result =
62000ba715SJan Korous #endif
63000ba715SJan Korous     llvm::sys::RetryAfterSignal(-1, write, FDWrite, "A", 1);
6477dd8a79SJan Korous     assert(Result != -1);
6577dd8a79SJan Korous   }
~SemaphorePipe__anone8a8889e0111::SemaphorePipe6677dd8a79SJan Korous   ~SemaphorePipe() {
6777dd8a79SJan Korous     if (OwnsFDs) {
6877dd8a79SJan Korous       close(FDWrite);
6977dd8a79SJan Korous       close(FDRead);
7077dd8a79SJan Korous     }
7177dd8a79SJan Korous   }
7277dd8a79SJan Korous   const int FDRead;
7377dd8a79SJan Korous   const int FDWrite;
7477dd8a79SJan Korous   bool OwnsFDs;
7577dd8a79SJan Korous 
create__anone8a8889e0111::SemaphorePipe76e8aee7efSKazu Hirata   static std::optional<SemaphorePipe> create() {
7777dd8a79SJan Korous     int InotifyPollingStopperFDs[2];
7877dd8a79SJan Korous     if (pipe2(InotifyPollingStopperFDs, O_CLOEXEC) == -1)
795891420eSKazu Hirata       return std::nullopt;
8077dd8a79SJan Korous     return SemaphorePipe(InotifyPollingStopperFDs);
8177dd8a79SJan Korous   }
8277dd8a79SJan Korous };
8377dd8a79SJan Korous 
8477dd8a79SJan Korous /// Mutex-protected queue of Events.
8577dd8a79SJan Korous class EventQueue {
8677dd8a79SJan Korous   std::mutex Mtx;
8777dd8a79SJan Korous   std::condition_variable NonEmpty;
8877dd8a79SJan Korous   std::queue<DirectoryWatcher::Event> Events;
8977dd8a79SJan Korous 
9077dd8a79SJan Korous public:
push_back(const DirectoryWatcher::Event::EventKind K,StringRef Filename)9177dd8a79SJan Korous   void push_back(const DirectoryWatcher::Event::EventKind K,
9277dd8a79SJan Korous                  StringRef Filename) {
9377dd8a79SJan Korous     {
9477dd8a79SJan Korous       std::unique_lock<std::mutex> L(Mtx);
9577dd8a79SJan Korous       Events.emplace(K, Filename);
9677dd8a79SJan Korous     }
9777dd8a79SJan Korous     NonEmpty.notify_one();
9877dd8a79SJan Korous   }
9977dd8a79SJan Korous 
10077dd8a79SJan Korous   // Blocks on caller thread and uses codition_variable to wait until there's an
10177dd8a79SJan Korous   // event to return.
pop_front_blocking()10277dd8a79SJan Korous   DirectoryWatcher::Event pop_front_blocking() {
10377dd8a79SJan Korous     std::unique_lock<std::mutex> L(Mtx);
10477dd8a79SJan Korous     while (true) {
10577dd8a79SJan Korous       // Since we might have missed all the prior notifications on NonEmpty we
10677dd8a79SJan Korous       // have to check the queue first (under lock).
10777dd8a79SJan Korous       if (!Events.empty()) {
10877dd8a79SJan Korous         DirectoryWatcher::Event Front = Events.front();
10977dd8a79SJan Korous         Events.pop();
11077dd8a79SJan Korous         return Front;
11177dd8a79SJan Korous       }
11277dd8a79SJan Korous       NonEmpty.wait(L, [this]() { return !Events.empty(); });
11377dd8a79SJan Korous     }
11477dd8a79SJan Korous   }
11577dd8a79SJan Korous };
11677dd8a79SJan Korous 
11777dd8a79SJan Korous class DirectoryWatcherLinux : public clang::DirectoryWatcher {
11877dd8a79SJan Korous public:
11977dd8a79SJan Korous   DirectoryWatcherLinux(
12077dd8a79SJan Korous       llvm::StringRef WatchedDirPath,
12177dd8a79SJan Korous       std::function<void(llvm::ArrayRef<Event>, bool)> Receiver,
12277dd8a79SJan Korous       bool WaitForInitialSync, int InotifyFD, int InotifyWD,
12377dd8a79SJan Korous       SemaphorePipe &&InotifyPollingStopSignal);
12477dd8a79SJan Korous 
~DirectoryWatcherLinux()12577dd8a79SJan Korous   ~DirectoryWatcherLinux() override {
12677dd8a79SJan Korous     StopWork();
12777dd8a79SJan Korous     InotifyPollingThread.join();
12877dd8a79SJan Korous     EventsReceivingThread.join();
12977dd8a79SJan Korous     inotify_rm_watch(InotifyFD, InotifyWD);
13077dd8a79SJan Korous     llvm::sys::RetryAfterSignal(-1, close, InotifyFD);
13177dd8a79SJan Korous   }
13277dd8a79SJan Korous 
13377dd8a79SJan Korous private:
13477dd8a79SJan Korous   const std::string WatchedDirPath;
13577dd8a79SJan Korous   // inotify file descriptor
13677dd8a79SJan Korous   int InotifyFD = -1;
13777dd8a79SJan Korous   // inotify watch descriptor
13877dd8a79SJan Korous   int InotifyWD = -1;
13977dd8a79SJan Korous 
14077dd8a79SJan Korous   EventQueue Queue;
14177dd8a79SJan Korous 
14277dd8a79SJan Korous   // Make sure lifetime of Receiver fully contains lifetime of
14377dd8a79SJan Korous   // EventsReceivingThread.
14477dd8a79SJan Korous   std::function<void(llvm::ArrayRef<Event>, bool)> Receiver;
14577dd8a79SJan Korous 
14677dd8a79SJan Korous   // Consumes inotify events and pushes directory watcher events to the Queue.
14777dd8a79SJan Korous   void InotifyPollingLoop();
14877dd8a79SJan Korous   std::thread InotifyPollingThread;
14977dd8a79SJan Korous   // Using pipe so we can epoll two file descriptors at once - inotify and
15077dd8a79SJan Korous   // stopping condition.
15177dd8a79SJan Korous   SemaphorePipe InotifyPollingStopSignal;
15277dd8a79SJan Korous 
15377dd8a79SJan Korous   // Does the initial scan of the directory - directly calling Receiver,
15477dd8a79SJan Korous   // bypassing the Queue. Both InitialScan and EventReceivingLoop use Receiver
15577dd8a79SJan Korous   // which isn't necessarily thread-safe.
15677dd8a79SJan Korous   void InitialScan();
15777dd8a79SJan Korous 
15877dd8a79SJan Korous   // Processing events from the Queue.
15977dd8a79SJan Korous   // In case client doesn't want to do the initial scan synchronously
16077dd8a79SJan Korous   // (WaitForInitialSync=false in ctor) we do the initial scan at the beginning
16177dd8a79SJan Korous   // of this thread.
16277dd8a79SJan Korous   std::thread EventsReceivingThread;
16377dd8a79SJan Korous   // Push event of WatcherGotInvalidated kind to the Queue to stop the loop.
16477dd8a79SJan Korous   // Both InitialScan and EventReceivingLoop use Receiver which isn't
16577dd8a79SJan Korous   // necessarily thread-safe.
16677dd8a79SJan Korous   void EventReceivingLoop();
16777dd8a79SJan Korous 
16877dd8a79SJan Korous   // Stops all the async work. Reentrant.
StopWork()16977dd8a79SJan Korous   void StopWork() {
17077dd8a79SJan Korous     Queue.push_back(DirectoryWatcher::Event::EventKind::WatcherGotInvalidated,
17177dd8a79SJan Korous                     "");
17277dd8a79SJan Korous     InotifyPollingStopSignal.signal();
17377dd8a79SJan Korous   }
17477dd8a79SJan Korous };
17577dd8a79SJan Korous 
InotifyPollingLoop()17677dd8a79SJan Korous void DirectoryWatcherLinux::InotifyPollingLoop() {
17777dd8a79SJan Korous   // We want to be able to read ~30 events at once even in the worst case
17877dd8a79SJan Korous   // (obscenely long filenames).
17977dd8a79SJan Korous   constexpr size_t EventBufferLength =
18077dd8a79SJan Korous       30 * (sizeof(struct inotify_event) + NAME_MAX + 1);
18177dd8a79SJan Korous   // http://man7.org/linux/man-pages/man7/inotify.7.html
18277dd8a79SJan Korous   // Some systems cannot read integer variables if they are not
18377dd8a79SJan Korous   // properly aligned. On other systems, incorrect alignment may
18477dd8a79SJan Korous   // decrease performance. Hence, the buffer used for reading from
18577dd8a79SJan Korous   // the inotify file descriptor should have the same alignment as
18677dd8a79SJan Korous   // struct inotify_event.
18777dd8a79SJan Korous 
188ac868620SJF Bastien   struct Buffer {
189ac868620SJF Bastien     alignas(struct inotify_event) char buffer[EventBufferLength];
190ac868620SJF Bastien   };
1912b3d49b6SJonas Devlieghere   auto ManagedBuffer = std::make_unique<Buffer>();
192d9e55fa5SJF Bastien   char *const Buf = ManagedBuffer->buffer;
19377dd8a79SJan Korous 
19477dd8a79SJan Korous   const int EpollFD = epoll_create1(EPOLL_CLOEXEC);
19577dd8a79SJan Korous   if (EpollFD == -1) {
19677dd8a79SJan Korous     StopWork();
19777dd8a79SJan Korous     return;
19877dd8a79SJan Korous   }
19977dd8a79SJan Korous   auto EpollFDGuard = llvm::make_scope_exit([EpollFD]() { close(EpollFD); });
20077dd8a79SJan Korous 
20177dd8a79SJan Korous   struct epoll_event EventSpec;
20277dd8a79SJan Korous   EventSpec.events = EPOLLIN;
20377dd8a79SJan Korous   EventSpec.data.fd = InotifyFD;
20477dd8a79SJan Korous   if (epoll_ctl(EpollFD, EPOLL_CTL_ADD, InotifyFD, &EventSpec) == -1) {
20577dd8a79SJan Korous     StopWork();
20677dd8a79SJan Korous     return;
20777dd8a79SJan Korous   }
20877dd8a79SJan Korous 
20977dd8a79SJan Korous   EventSpec.data.fd = InotifyPollingStopSignal.FDRead;
21077dd8a79SJan Korous   if (epoll_ctl(EpollFD, EPOLL_CTL_ADD, InotifyPollingStopSignal.FDRead,
21177dd8a79SJan Korous                 &EventSpec) == -1) {
21277dd8a79SJan Korous     StopWork();
21377dd8a79SJan Korous     return;
21477dd8a79SJan Korous   }
21577dd8a79SJan Korous 
21677dd8a79SJan Korous   std::array<struct epoll_event, 2> EpollEventBuffer;
21777dd8a79SJan Korous 
21877dd8a79SJan Korous   while (true) {
21977dd8a79SJan Korous     const int EpollWaitResult = llvm::sys::RetryAfterSignal(
22077dd8a79SJan Korous         -1, epoll_wait, EpollFD, EpollEventBuffer.data(),
22177dd8a79SJan Korous         EpollEventBuffer.size(), /*timeout=*/-1 /*== infinity*/);
22277dd8a79SJan Korous     if (EpollWaitResult == -1) {
22377dd8a79SJan Korous       StopWork();
22477dd8a79SJan Korous       return;
22577dd8a79SJan Korous     }
22677dd8a79SJan Korous 
22777dd8a79SJan Korous     // Multiple epoll_events can be received for a single file descriptor per
22877dd8a79SJan Korous     // epoll_wait call.
229ec2abbafSJan Korous     for (int i = 0; i < EpollWaitResult; ++i) {
230ec2abbafSJan Korous       if (EpollEventBuffer[i].data.fd == InotifyPollingStopSignal.FDRead) {
23177dd8a79SJan Korous         StopWork();
23277dd8a79SJan Korous         return;
23377dd8a79SJan Korous       }
23477dd8a79SJan Korous     }
23577dd8a79SJan Korous 
23677dd8a79SJan Korous     // epoll_wait() always return either error or >0 events. Since there was no
23777dd8a79SJan Korous     // event for stopping, it must be an inotify event ready for reading.
23877dd8a79SJan Korous     ssize_t NumRead = llvm::sys::RetryAfterSignal(-1, read, InotifyFD, Buf,
23977dd8a79SJan Korous                                                   EventBufferLength);
24077dd8a79SJan Korous     for (char *P = Buf; P < Buf + NumRead;) {
24177dd8a79SJan Korous       if (P + sizeof(struct inotify_event) > Buf + NumRead) {
24277dd8a79SJan Korous         StopWork();
24377dd8a79SJan Korous         llvm_unreachable("an incomplete inotify_event was read");
24477dd8a79SJan Korous         return;
24577dd8a79SJan Korous       }
24677dd8a79SJan Korous 
24777dd8a79SJan Korous       struct inotify_event *Event = reinterpret_cast<struct inotify_event *>(P);
24877dd8a79SJan Korous       P += sizeof(struct inotify_event) + Event->len;
24977dd8a79SJan Korous 
25077dd8a79SJan Korous       if (Event->mask & (IN_CREATE | IN_MODIFY | IN_MOVED_TO | IN_DELETE) &&
25177dd8a79SJan Korous           Event->len <= 0) {
25277dd8a79SJan Korous         StopWork();
25377dd8a79SJan Korous         llvm_unreachable("expected a filename from inotify");
25477dd8a79SJan Korous         return;
25577dd8a79SJan Korous       }
25677dd8a79SJan Korous 
25777dd8a79SJan Korous       if (Event->mask & (IN_CREATE | IN_MOVED_TO | IN_MODIFY)) {
25877dd8a79SJan Korous         Queue.push_back(DirectoryWatcher::Event::EventKind::Modified,
25977dd8a79SJan Korous                         Event->name);
26077dd8a79SJan Korous       } else if (Event->mask & (IN_DELETE | IN_MOVED_FROM)) {
26177dd8a79SJan Korous         Queue.push_back(DirectoryWatcher::Event::EventKind::Removed,
26277dd8a79SJan Korous                         Event->name);
26377dd8a79SJan Korous       } else if (Event->mask & (IN_DELETE_SELF | IN_MOVE_SELF)) {
26477dd8a79SJan Korous         Queue.push_back(DirectoryWatcher::Event::EventKind::WatchedDirRemoved,
26577dd8a79SJan Korous                         "");
26677dd8a79SJan Korous         StopWork();
26777dd8a79SJan Korous         return;
26877dd8a79SJan Korous       } else if (Event->mask & IN_IGNORED) {
26977dd8a79SJan Korous         StopWork();
27077dd8a79SJan Korous         return;
27177dd8a79SJan Korous       } else {
27277dd8a79SJan Korous         StopWork();
27377dd8a79SJan Korous         llvm_unreachable("Unknown event type.");
27477dd8a79SJan Korous         return;
27577dd8a79SJan Korous       }
27677dd8a79SJan Korous     }
27777dd8a79SJan Korous   }
27877dd8a79SJan Korous }
27977dd8a79SJan Korous 
InitialScan()28077dd8a79SJan Korous void DirectoryWatcherLinux::InitialScan() {
28177dd8a79SJan Korous   this->Receiver(getAsFileEvents(scanDirectory(WatchedDirPath)),
28277dd8a79SJan Korous                  /*IsInitial=*/true);
28377dd8a79SJan Korous }
28477dd8a79SJan Korous 
EventReceivingLoop()28577dd8a79SJan Korous void DirectoryWatcherLinux::EventReceivingLoop() {
28677dd8a79SJan Korous   while (true) {
28777dd8a79SJan Korous     DirectoryWatcher::Event Event = this->Queue.pop_front_blocking();
28877dd8a79SJan Korous     this->Receiver(Event, false);
28977dd8a79SJan Korous     if (Event.Kind ==
29077dd8a79SJan Korous         DirectoryWatcher::Event::EventKind::WatcherGotInvalidated) {
29177dd8a79SJan Korous       StopWork();
29277dd8a79SJan Korous       return;
29377dd8a79SJan Korous     }
29477dd8a79SJan Korous   }
29577dd8a79SJan Korous }
29677dd8a79SJan Korous 
DirectoryWatcherLinux(StringRef WatchedDirPath,std::function<void (llvm::ArrayRef<Event>,bool)> Receiver,bool WaitForInitialSync,int InotifyFD,int InotifyWD,SemaphorePipe && InotifyPollingStopSignal)29777dd8a79SJan Korous DirectoryWatcherLinux::DirectoryWatcherLinux(
29877dd8a79SJan Korous     StringRef WatchedDirPath,
29977dd8a79SJan Korous     std::function<void(llvm::ArrayRef<Event>, bool)> Receiver,
30077dd8a79SJan Korous     bool WaitForInitialSync, int InotifyFD, int InotifyWD,
30177dd8a79SJan Korous     SemaphorePipe &&InotifyPollingStopSignal)
30277dd8a79SJan Korous     : WatchedDirPath(WatchedDirPath), InotifyFD(InotifyFD),
30377dd8a79SJan Korous       InotifyWD(InotifyWD), Receiver(Receiver),
30477dd8a79SJan Korous       InotifyPollingStopSignal(std::move(InotifyPollingStopSignal)) {
30577dd8a79SJan Korous 
30677dd8a79SJan Korous   InotifyPollingThread = std::thread([this]() { InotifyPollingLoop(); });
30777dd8a79SJan Korous   // We have no guarantees about thread safety of the Receiver which is being
30877dd8a79SJan Korous   // used in both InitialScan and EventReceivingLoop. We shouldn't run these
30977dd8a79SJan Korous   // only synchronously.
31077dd8a79SJan Korous   if (WaitForInitialSync) {
31177dd8a79SJan Korous     InitialScan();
31277dd8a79SJan Korous     EventsReceivingThread = std::thread([this]() { EventReceivingLoop(); });
31377dd8a79SJan Korous   } else {
31477dd8a79SJan Korous     EventsReceivingThread = std::thread([this]() {
31577dd8a79SJan Korous       // FIXME: We might want to terminate an async initial scan early in case
31677dd8a79SJan Korous       // of a failure in EventsReceivingThread.
31777dd8a79SJan Korous       InitialScan();
31877dd8a79SJan Korous       EventReceivingLoop();
31977dd8a79SJan Korous     });
32077dd8a79SJan Korous   }
32177dd8a79SJan Korous }
32277dd8a79SJan Korous 
32377dd8a79SJan Korous } // namespace
32477dd8a79SJan Korous 
create(StringRef Path,std::function<void (llvm::ArrayRef<DirectoryWatcher::Event>,bool)> Receiver,bool WaitForInitialSync)325ef74924fSPuyan Lotfi llvm::Expected<std::unique_ptr<DirectoryWatcher>> clang::DirectoryWatcher::create(
32677dd8a79SJan Korous     StringRef Path,
32777dd8a79SJan Korous     std::function<void(llvm::ArrayRef<DirectoryWatcher::Event>, bool)> Receiver,
32877dd8a79SJan Korous     bool WaitForInitialSync) {
3291dcf216fSPuyan Lotfi   if (Path.empty())
3301dcf216fSPuyan Lotfi     llvm::report_fatal_error(
3311dcf216fSPuyan Lotfi         "DirectoryWatcher::create can not accept an empty Path.");
33277dd8a79SJan Korous 
33377dd8a79SJan Korous   const int InotifyFD = inotify_init1(IN_CLOEXEC);
33477dd8a79SJan Korous   if (InotifyFD == -1)
335ef74924fSPuyan Lotfi     return llvm::make_error<llvm::StringError>(
336*ba13fa2aSMichael Spencer         llvm::errnoAsErrorCode(), std::string(": inotify_init1()"));
33777dd8a79SJan Korous 
33877dd8a79SJan Korous   const int InotifyWD = inotify_add_watch(
33977dd8a79SJan Korous       InotifyFD, Path.str().c_str(),
34060a0d49eSJan Korous       IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MODIFY |
34160a0d49eSJan Korous       IN_MOVED_FROM | IN_MOVE_SELF | IN_MOVED_TO | IN_ONLYDIR | IN_IGNORED
34257f4bacfSJan Korous #ifdef IN_EXCL_UNLINK
34360a0d49eSJan Korous       | IN_EXCL_UNLINK
34460a0d49eSJan Korous #endif
34560a0d49eSJan Korous       );
34677dd8a79SJan Korous   if (InotifyWD == -1)
347ef74924fSPuyan Lotfi     return llvm::make_error<llvm::StringError>(
348*ba13fa2aSMichael Spencer         llvm::errnoAsErrorCode(), std::string(": inotify_add_watch()"));
34977dd8a79SJan Korous 
35077dd8a79SJan Korous   auto InotifyPollingStopper = SemaphorePipe::create();
35177dd8a79SJan Korous 
35277dd8a79SJan Korous   if (!InotifyPollingStopper)
353ef74924fSPuyan Lotfi     return llvm::make_error<llvm::StringError>(
354*ba13fa2aSMichael Spencer         llvm::errnoAsErrorCode(), std::string(": SemaphorePipe::create()"));
35577dd8a79SJan Korous 
3562b3d49b6SJonas Devlieghere   return std::make_unique<DirectoryWatcherLinux>(
35777dd8a79SJan Korous       Path, Receiver, WaitForInitialSync, InotifyFD, InotifyWD,
35877dd8a79SJan Korous       std::move(*InotifyPollingStopper));
35977dd8a79SJan Korous }
360