file_path_watcher_kqueue.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/files/file_path_watcher_kqueue.h"
  5. #include <fcntl.h>
  6. #include <stddef.h>
  7. #include <sys/param.h>
  8. #include <algorithm>
  9. #include <string>
  10. #include <vector>
  11. #include "base/bind.h"
  12. #include "base/file_descriptor_posix.h"
  13. #include "base/files/file_util.h"
  14. #include "base/logging.h"
  15. #include "base/strings/stringprintf.h"
  16. #include "base/threading/scoped_blocking_call.h"
  17. #include "base/threading/sequenced_task_runner_handle.h"
  18. // On some platforms these are not defined.
  19. #if !defined(EV_RECEIPT)
  20. #define EV_RECEIPT 0
  21. #endif
  22. #if !defined(O_EVTONLY)
  23. #define O_EVTONLY O_RDONLY
  24. #endif
  25. namespace base {
  26. FilePathWatcherKQueue::FilePathWatcherKQueue() : kqueue_(-1) {}
  27. FilePathWatcherKQueue::~FilePathWatcherKQueue() {
  28. DCHECK(!task_runner() || task_runner()->RunsTasksInCurrentSequence());
  29. }
  30. void FilePathWatcherKQueue::ReleaseEvent(struct kevent& event) {
  31. CloseFileDescriptor(&event.ident);
  32. EventData* entry = EventDataForKevent(event);
  33. delete entry;
  34. event.udata = NULL;
  35. }
  36. size_t FilePathWatcherKQueue::EventsForPath(FilePath path,
  37. EventVector* events) {
  38. // Make sure that we are working with a clean slate.
  39. DCHECK(events->empty());
  40. std::vector<FilePath::StringType> components = path.GetComponents();
  41. if (components.empty()) {
  42. return 0;
  43. }
  44. size_t last_existing_entry = 0;
  45. FilePath built_path;
  46. bool path_still_exists = true;
  47. for (std::vector<FilePath::StringType>::iterator i = components.begin();
  48. i != components.end(); ++i) {
  49. if (i == components.begin()) {
  50. built_path = FilePath(*i);
  51. } else {
  52. built_path = built_path.Append(*i);
  53. }
  54. uintptr_t fd = kNoFileDescriptor;
  55. if (path_still_exists) {
  56. fd = FileDescriptorForPath(built_path);
  57. if (fd == kNoFileDescriptor) {
  58. path_still_exists = false;
  59. } else {
  60. ++last_existing_entry;
  61. }
  62. }
  63. FilePath::StringType subdir = (i != (components.end() - 1)) ? *(i + 1) : "";
  64. EventData* data = new EventData(built_path, subdir);
  65. struct kevent event;
  66. EV_SET(&event, fd, EVFILT_VNODE, (EV_ADD | EV_CLEAR | EV_RECEIPT),
  67. (NOTE_DELETE | NOTE_WRITE | NOTE_ATTRIB |
  68. NOTE_RENAME | NOTE_REVOKE | NOTE_EXTEND), 0, data);
  69. events->push_back(event);
  70. }
  71. return last_existing_entry;
  72. }
  73. // static
  74. size_t FilePathWatcherKQueue::EventForItem(const FilePath& path,
  75. EventVector* events) {
  76. // Make sure that we are working with a clean slate.
  77. DCHECK(events->empty());
  78. events->resize(1);
  79. auto& event = events->front();
  80. EV_SET(&event, FileDescriptorForPath(path), EVFILT_VNODE,
  81. (EV_ADD | EV_CLEAR | EV_RECEIPT),
  82. (NOTE_DELETE | NOTE_WRITE | NOTE_ATTRIB | NOTE_RENAME | NOTE_REVOKE |
  83. NOTE_EXTEND),
  84. 0, new EventData(path, /*subdir=*/FilePath::StringType()));
  85. return event.ident != kNoFileDescriptor ? 1 : 0;
  86. }
  87. uintptr_t FilePathWatcherKQueue::FileDescriptorForPath(const FilePath& path) {
  88. ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
  89. int fd = HANDLE_EINTR(open(path.value().c_str(), O_EVTONLY));
  90. if (fd < 0)
  91. return kNoFileDescriptor;
  92. return static_cast<uintptr_t>(fd);
  93. }
  94. void FilePathWatcherKQueue::CloseFileDescriptor(uintptr_t* fd) {
  95. if (*fd == kNoFileDescriptor) {
  96. return;
  97. }
  98. if (IGNORE_EINTR(close(checked_cast<int>(*fd))) != 0) {
  99. DPLOG(ERROR) << "close";
  100. }
  101. *fd = kNoFileDescriptor;
  102. }
  103. bool FilePathWatcherKQueue::AreKeventValuesValid(struct kevent* kevents,
  104. int count) {
  105. if (count < 0) {
  106. DPLOG(ERROR) << "kevent";
  107. return false;
  108. }
  109. bool valid = true;
  110. for (int i = 0; i < count; ++i) {
  111. if (kevents[i].flags & EV_ERROR && kevents[i].data) {
  112. // Find the kevent in |events_| that matches the kevent with the error.
  113. EventVector::iterator event = events_.begin();
  114. for (; event != events_.end(); ++event) {
  115. if (event->ident == kevents[i].ident) {
  116. break;
  117. }
  118. }
  119. std::string path_name;
  120. if (event != events_.end()) {
  121. EventData* event_data = EventDataForKevent(*event);
  122. if (event_data != NULL) {
  123. path_name = event_data->path_.value();
  124. }
  125. }
  126. if (path_name.empty()) {
  127. path_name = base::StringPrintf(
  128. "fd %ld", reinterpret_cast<long>(&kevents[i].ident));
  129. }
  130. DLOG(ERROR) << "Error: " << kevents[i].data << " for " << path_name;
  131. valid = false;
  132. }
  133. }
  134. return valid;
  135. }
  136. void FilePathWatcherKQueue::HandleAttributesChange(
  137. const EventVector::iterator& event,
  138. bool* target_file_affected,
  139. bool* update_watches) {
  140. EventVector::iterator next_event = event + 1;
  141. EventData* next_event_data = EventDataForKevent(*next_event);
  142. // Check to see if the next item in path is still accessible.
  143. uintptr_t have_access = FileDescriptorForPath(next_event_data->path_);
  144. if (have_access == kNoFileDescriptor) {
  145. *target_file_affected = true;
  146. *update_watches = true;
  147. EventVector::iterator local_event(event);
  148. for (; local_event != events_.end(); ++local_event) {
  149. // Close all nodes from the event down. This has the side effect of
  150. // potentially rendering other events in |updates| invalid.
  151. // There is no need to remove the events from |kqueue_| because this
  152. // happens as a side effect of closing the file descriptor.
  153. CloseFileDescriptor(&local_event->ident);
  154. }
  155. } else {
  156. CloseFileDescriptor(&have_access);
  157. }
  158. }
  159. void FilePathWatcherKQueue::HandleDeleteOrMoveChange(
  160. const EventVector::iterator& event,
  161. bool* target_file_affected,
  162. bool* update_watches) {
  163. *target_file_affected = true;
  164. *update_watches = true;
  165. EventVector::iterator local_event(event);
  166. for (; local_event != events_.end(); ++local_event) {
  167. // Close all nodes from the event down. This has the side effect of
  168. // potentially rendering other events in |updates| invalid.
  169. // There is no need to remove the events from |kqueue_| because this
  170. // happens as a side effect of closing the file descriptor.
  171. CloseFileDescriptor(&local_event->ident);
  172. }
  173. }
  174. void FilePathWatcherKQueue::HandleCreateItemChange(
  175. const EventVector::iterator& event,
  176. bool* target_file_affected,
  177. bool* update_watches) {
  178. // Get the next item in the path.
  179. EventVector::iterator next_event = event + 1;
  180. // Check to see if it already has a valid file descriptor.
  181. if (!IsKeventFileDescriptorOpen(*next_event)) {
  182. EventData* next_event_data = EventDataForKevent(*next_event);
  183. // If not, attempt to open a file descriptor for it.
  184. next_event->ident = FileDescriptorForPath(next_event_data->path_);
  185. if (IsKeventFileDescriptorOpen(*next_event)) {
  186. *update_watches = true;
  187. if (next_event_data->subdir_.empty()) {
  188. *target_file_affected = true;
  189. }
  190. }
  191. }
  192. }
  193. bool FilePathWatcherKQueue::UpdateWatches(bool* target_file_affected) {
  194. // Iterate over events adding kevents for items that exist to the kqueue.
  195. // Then check to see if new components in the path have been created.
  196. // Repeat until no new components in the path are detected.
  197. // This is to get around races in directory creation in a watched path.
  198. bool update_watches = true;
  199. while (update_watches) {
  200. size_t valid;
  201. for (valid = 0; valid < events_.size(); ++valid) {
  202. if (!IsKeventFileDescriptorOpen(events_[valid])) {
  203. break;
  204. }
  205. }
  206. if (valid == 0) {
  207. // The root of the file path is inaccessible?
  208. return false;
  209. }
  210. EventVector updates(valid);
  211. ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
  212. const int valid_int = checked_cast<int>(valid);
  213. int count = HANDLE_EINTR(
  214. kevent(kqueue_, &events_[0], valid_int, &updates[0], valid_int, NULL));
  215. if (!AreKeventValuesValid(&updates[0], count)) {
  216. return false;
  217. }
  218. update_watches = false;
  219. for (; valid < events_.size(); ++valid) {
  220. EventData* event_data = EventDataForKevent(events_[valid]);
  221. events_[valid].ident = FileDescriptorForPath(event_data->path_);
  222. if (IsKeventFileDescriptorOpen(events_[valid])) {
  223. update_watches = true;
  224. if (event_data->subdir_.empty()) {
  225. *target_file_affected = true;
  226. }
  227. } else {
  228. break;
  229. }
  230. }
  231. }
  232. return true;
  233. }
  234. bool FilePathWatcherKQueue::Watch(const FilePath& path,
  235. Type type,
  236. const FilePathWatcher::Callback& callback) {
  237. DCHECK(target_.value().empty()); // Can only watch one path.
  238. DCHECK(!callback.is_null());
  239. DCHECK_EQ(kqueue_, -1);
  240. // Recursive watch is not supported using kqueue.
  241. DCHECK_NE(type, Type::kRecursive);
  242. callback_ = callback;
  243. target_ = path;
  244. set_task_runner(SequencedTaskRunnerHandle::Get());
  245. kqueue_ = kqueue();
  246. if (kqueue_ == -1) {
  247. DPLOG(ERROR) << "kqueue";
  248. return false;
  249. }
  250. size_t last_entry = type == Type::kNonRecursive
  251. ? EventsForPath(target_, &events_)
  252. : EventForItem(target_, &events_);
  253. if (!last_entry) {
  254. // No notifications can possibly come in, so fail fast.
  255. Cancel();
  256. return false;
  257. }
  258. EventVector responses(last_entry);
  259. ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
  260. const int last_entry_int = checked_cast<int>(last_entry);
  261. int count = HANDLE_EINTR(kevent(kqueue_, &events_[0], last_entry_int,
  262. &responses[0], last_entry_int, NULL));
  263. if (!AreKeventValuesValid(&responses[0], count)) {
  264. // Calling Cancel() here to close any file descriptors that were opened.
  265. // This would happen in the destructor anyways, but FilePathWatchers tend to
  266. // be long lived, and if an error has occurred, there is no reason to waste
  267. // the file descriptors.
  268. Cancel();
  269. return false;
  270. }
  271. // It's safe to use Unretained() because the watch is cancelled and the
  272. // callback cannot be invoked after |kqueue_watch_controller_| (which is a
  273. // member of |this|) has been deleted.
  274. kqueue_watch_controller_ = FileDescriptorWatcher::WatchReadable(
  275. kqueue_, BindRepeating(&FilePathWatcherKQueue::OnKQueueReadable,
  276. Unretained(this)));
  277. return true;
  278. }
  279. void FilePathWatcherKQueue::Cancel() {
  280. if (!task_runner()) {
  281. set_cancelled();
  282. return;
  283. }
  284. DCHECK(task_runner()->RunsTasksInCurrentSequence());
  285. if (!is_cancelled()) {
  286. set_cancelled();
  287. kqueue_watch_controller_.reset();
  288. if (IGNORE_EINTR(close(kqueue_)) != 0) {
  289. DPLOG(ERROR) << "close kqueue";
  290. }
  291. kqueue_ = -1;
  292. std::for_each(events_.begin(), events_.end(), ReleaseEvent);
  293. events_.clear();
  294. callback_.Reset();
  295. }
  296. }
  297. void FilePathWatcherKQueue::OnKQueueReadable() {
  298. DCHECK(task_runner()->RunsTasksInCurrentSequence());
  299. DCHECK(events_.size());
  300. // Request the file system update notifications that have occurred and return
  301. // them in |updates|. |count| will contain the number of updates that have
  302. // occurred.
  303. EventVector updates(events_.size());
  304. struct timespec timeout = {0, 0};
  305. int count = HANDLE_EINTR(kevent(kqueue_, NULL, 0, &updates[0],
  306. checked_cast<int>(updates.size()), &timeout));
  307. // Error values are stored within updates, so check to make sure that no
  308. // errors occurred.
  309. if (!AreKeventValuesValid(&updates[0], count)) {
  310. callback_.Run(target_, true /* error */);
  311. Cancel();
  312. return;
  313. }
  314. bool update_watches = false;
  315. bool send_notification = false;
  316. // Iterate through each of the updates and react to them.
  317. // AreKeventValuesValid() guarantees `count` is non-negative.
  318. for (size_t i = 0; i < static_cast<size_t>(count); ++i) {
  319. // Find our kevent record that matches the update notification.
  320. EventVector::iterator event = events_.begin();
  321. for (; event != events_.end(); ++event) {
  322. if (!IsKeventFileDescriptorOpen(*event) ||
  323. event->ident == updates[i].ident) {
  324. break;
  325. }
  326. }
  327. if (event == events_.end() || !IsKeventFileDescriptorOpen(*event)) {
  328. // The event may no longer exist in |events_| because another event
  329. // modified |events_| in such a way to make it invalid. For example if
  330. // the path is /foo/bar/bam and foo is deleted, NOTE_DELETE events for
  331. // foo, bar and bam will be sent. If foo is processed first, then
  332. // the file descriptors for bar and bam will already be closed and set
  333. // to -1 before they get a chance to be processed.
  334. continue;
  335. }
  336. EventData* event_data = EventDataForKevent(*event);
  337. // If the subdir is empty, this is the last item on the path and is the
  338. // target file.
  339. bool target_file_affected = event_data->subdir_.empty();
  340. if ((updates[i].fflags & NOTE_ATTRIB) && !target_file_affected) {
  341. HandleAttributesChange(event, &target_file_affected, &update_watches);
  342. }
  343. if (updates[i].fflags & (NOTE_DELETE | NOTE_REVOKE | NOTE_RENAME)) {
  344. HandleDeleteOrMoveChange(event, &target_file_affected, &update_watches);
  345. }
  346. if ((updates[i].fflags & NOTE_WRITE) && !target_file_affected) {
  347. HandleCreateItemChange(event, &target_file_affected, &update_watches);
  348. }
  349. send_notification |= target_file_affected;
  350. }
  351. if (update_watches) {
  352. if (!UpdateWatches(&send_notification)) {
  353. callback_.Run(target_, true /* error */);
  354. Cancel();
  355. return;
  356. }
  357. }
  358. if (send_notification) {
  359. callback_.Run(target_, false);
  360. }
  361. }
  362. } // namespace base