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.
5 #include "base/files/file_path_watcher.h"
9 #include <sys/inotify.h>
10 #include <sys/ioctl.h>
11 #include <sys/select.h>
20 #include "base/bind.h"
21 #include "base/containers/hash_tables.h"
22 #include "base/files/file_enumerator.h"
23 #include "base/files/file_path.h"
24 #include "base/files/file_util.h"
25 #include "base/lazy_instance.h"
26 #include "base/location.h"
27 #include "base/logging.h"
28 #include "base/memory/scoped_ptr.h"
29 #include "base/posix/eintr_wrapper.h"
30 #include "base/single_thread_task_runner.h"
31 #include "base/synchronization/lock.h"
32 #include "base/thread_task_runner_handle.h"
33 #include "base/threading/thread.h"
34 #include "base/trace_event/trace_event.h"
40 class FilePathWatcherImpl
;
42 // Singleton to manage all inotify watches.
43 // TODO(tony): It would be nice if this wasn't a singleton.
44 // http://crbug.com/38174
47 typedef int Watch
; // Watch descriptor used by AddWatch and RemoveWatch.
48 static const Watch kInvalidWatch
= -1;
50 // Watch directory |path| for changes. |watcher| will be notified on each
51 // change. Returns kInvalidWatch on failure.
52 Watch
AddWatch(const FilePath
& path
, FilePathWatcherImpl
* watcher
);
54 // Remove |watch| if it's valid.
55 void RemoveWatch(Watch watch
, FilePathWatcherImpl
* watcher
);
57 // Callback for InotifyReaderTask.
58 void OnInotifyEvent(const inotify_event
* event
);
61 friend struct DefaultLazyInstanceTraits
<InotifyReader
>;
63 typedef std::set
<FilePathWatcherImpl
*> WatcherSet
;
68 // We keep track of which delegates want to be notified on which watches.
69 hash_map
<Watch
, WatcherSet
> watchers_
;
71 // Lock to protect watchers_.
74 // Separate thread on which we run blocking read for inotify events.
77 // File descriptor returned by inotify_init.
78 const int inotify_fd_
;
80 // Use self-pipe trick to unblock select during shutdown.
81 int shutdown_pipe_
[2];
83 // Flag set to true when startup was successful.
86 DISALLOW_COPY_AND_ASSIGN(InotifyReader
);
89 class FilePathWatcherImpl
: public FilePathWatcher::PlatformDelegate
,
90 public MessageLoop::DestructionObserver
{
92 FilePathWatcherImpl();
94 // Called for each event coming from the watch. |fired_watch| identifies the
95 // watch that fired, |child| indicates what has changed, and is relative to
96 // the currently watched path for |fired_watch|.
98 // |created| is true if the object appears.
99 // |deleted| is true if the object disappears.
100 // |is_dir| is true if the object is a directory.
101 void OnFilePathChanged(InotifyReader::Watch fired_watch
,
102 const FilePath::StringType
& child
,
108 ~FilePathWatcherImpl() override
{}
111 // Start watching |path| for changes and notify |delegate| on each change.
112 // Returns true if watch for |path| has been added successfully.
113 bool Watch(const FilePath
& path
,
115 const FilePathWatcher::Callback
& callback
) override
;
117 // Cancel the watch. This unregisters the instance with InotifyReader.
118 void Cancel() override
;
120 // Cleans up and stops observing the message_loop() thread.
121 void CancelOnMessageLoopThread() override
;
123 // Deletion of the FilePathWatcher will call Cancel() to dispose of this
124 // object in the right thread. This also observes destruction of the required
125 // cleanup thread, in case it quits before Cancel() is called.
126 void WillDestroyCurrentMessageLoop() override
;
128 // Inotify watches are installed for all directory components of |target_|.
129 // A WatchEntry instance holds:
130 // - |watch|: the watch descriptor for a component.
131 // - |subdir|: the subdirectory that identifies the next component.
132 // - For the last component, there is no next component, so it is empty.
133 // - |linkname|: the target of the symlink.
134 // - Only if the target being watched is a symbolic link.
136 explicit WatchEntry(const FilePath::StringType
& dirname
)
137 : watch(InotifyReader::kInvalidWatch
),
140 InotifyReader::Watch watch
;
141 FilePath::StringType subdir
;
142 FilePath::StringType linkname
;
144 typedef std::vector
<WatchEntry
> WatchVector
;
146 // Reconfigure to watch for the most specific parent directory of |target_|
147 // that exists. Also calls UpdateRecursiveWatches() below.
148 void UpdateWatches();
150 // Reconfigure to recursively watch |target_| and all its sub-directories.
151 // - This is a no-op if the watch is not recursive.
152 // - If |target_| does not exist, then clear all the recursive watches.
153 // - Assuming |target_| exists, passing kInvalidWatch as |fired_watch| forces
154 // addition of recursive watches for |target_|.
155 // - Otherwise, only the directory associated with |fired_watch| and its
156 // sub-directories will be reconfigured.
157 void UpdateRecursiveWatches(InotifyReader::Watch fired_watch
, bool is_dir
);
159 // Enumerate recursively through |path| and add / update watches.
160 void UpdateRecursiveWatchesForPath(const FilePath
& path
);
162 // Do internal bookkeeping to update mappings between |watch| and its
163 // associated full path |path|.
164 void TrackWatchForRecursion(InotifyReader::Watch watch
, const FilePath
& path
);
166 // Remove all the recursive watches.
167 void RemoveRecursiveWatches();
169 // |path| is a symlink to a non-existent target. Attempt to add a watch to
170 // the link target's parent directory. Update |watch_entry| on success.
171 void AddWatchForBrokenSymlink(const FilePath
& path
, WatchEntry
* watch_entry
);
173 bool HasValidWatchVector() const;
175 // Callback to notify upon changes.
176 FilePathWatcher::Callback callback_
;
178 // The file or directory we're supposed to watch.
183 // The vector of watches and next component names for all path components,
184 // starting at the root directory. The last entry corresponds to the watch for
185 // |target_| and always stores an empty next component name in |subdir|.
186 WatchVector watches_
;
188 hash_map
<InotifyReader::Watch
, FilePath
> recursive_paths_by_watch_
;
189 std::map
<FilePath
, InotifyReader::Watch
> recursive_watches_by_path_
;
191 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl
);
194 void InotifyReaderCallback(InotifyReader
* reader
, int inotify_fd
,
196 // Make sure the file descriptors are good for use with select().
197 CHECK_LE(0, inotify_fd
);
198 CHECK_GT(FD_SETSIZE
, inotify_fd
);
199 CHECK_LE(0, shutdown_fd
);
200 CHECK_GT(FD_SETSIZE
, shutdown_fd
);
202 trace_event::TraceLog::GetInstance()->SetCurrentThreadBlocksMessageLoop();
207 FD_SET(inotify_fd
, &rfds
);
208 FD_SET(shutdown_fd
, &rfds
);
210 // Wait until some inotify events are available.
212 HANDLE_EINTR(select(std::max(inotify_fd
, shutdown_fd
) + 1,
213 &rfds
, NULL
, NULL
, NULL
));
214 if (select_result
< 0) {
215 DPLOG(WARNING
) << "select failed";
219 if (FD_ISSET(shutdown_fd
, &rfds
))
222 // Adjust buffer size to current event queue size.
224 int ioctl_result
= HANDLE_EINTR(ioctl(inotify_fd
, FIONREAD
,
227 if (ioctl_result
!= 0) {
228 DPLOG(WARNING
) << "ioctl failed";
232 std::vector
<char> buffer(buffer_size
);
234 ssize_t bytes_read
= HANDLE_EINTR(read(inotify_fd
, &buffer
[0],
237 if (bytes_read
< 0) {
238 DPLOG(WARNING
) << "read from inotify fd failed";
243 while (i
< bytes_read
) {
244 inotify_event
* event
= reinterpret_cast<inotify_event
*>(&buffer
[i
]);
245 size_t event_size
= sizeof(inotify_event
) + event
->len
;
246 DCHECK(i
+ event_size
<= static_cast<size_t>(bytes_read
));
247 reader
->OnInotifyEvent(event
);
253 static LazyInstance
<InotifyReader
>::Leaky g_inotify_reader
=
254 LAZY_INSTANCE_INITIALIZER
;
256 InotifyReader::InotifyReader()
257 : thread_("inotify_reader"),
258 inotify_fd_(inotify_init()),
261 PLOG(ERROR
) << "inotify_init() failed";
263 shutdown_pipe_
[0] = -1;
264 shutdown_pipe_
[1] = -1;
265 if (inotify_fd_
>= 0 && pipe(shutdown_pipe_
) == 0 && thread_
.Start()) {
266 thread_
.task_runner()->PostTask(
268 Bind(&InotifyReaderCallback
, this, inotify_fd_
, shutdown_pipe_
[0]));
273 InotifyReader::~InotifyReader() {
275 // Write to the self-pipe so that the select call in InotifyReaderTask
277 ssize_t ret
= HANDLE_EINTR(write(shutdown_pipe_
[1], "", 1));
282 if (inotify_fd_
>= 0)
284 if (shutdown_pipe_
[0] >= 0)
285 close(shutdown_pipe_
[0]);
286 if (shutdown_pipe_
[1] >= 0)
287 close(shutdown_pipe_
[1]);
290 InotifyReader::Watch
InotifyReader::AddWatch(
291 const FilePath
& path
, FilePathWatcherImpl
* watcher
) {
293 return kInvalidWatch
;
295 AutoLock
auto_lock(lock_
);
297 Watch watch
= inotify_add_watch(inotify_fd_
, path
.value().c_str(),
298 IN_ATTRIB
| IN_CREATE
| IN_DELETE
|
299 IN_CLOSE_WRITE
| IN_MOVE
|
302 if (watch
== kInvalidWatch
)
303 return kInvalidWatch
;
305 watchers_
[watch
].insert(watcher
);
310 void InotifyReader::RemoveWatch(Watch watch
, FilePathWatcherImpl
* watcher
) {
311 if (!valid_
|| (watch
== kInvalidWatch
))
314 AutoLock
auto_lock(lock_
);
316 watchers_
[watch
].erase(watcher
);
318 if (watchers_
[watch
].empty()) {
319 watchers_
.erase(watch
);
320 inotify_rm_watch(inotify_fd_
, watch
);
324 void InotifyReader::OnInotifyEvent(const inotify_event
* event
) {
325 if (event
->mask
& IN_IGNORED
)
328 FilePath::StringType
child(event
->len
? event
->name
: FILE_PATH_LITERAL(""));
329 AutoLock
auto_lock(lock_
);
331 for (WatcherSet::iterator watcher
= watchers_
[event
->wd
].begin();
332 watcher
!= watchers_
[event
->wd
].end();
334 (*watcher
)->OnFilePathChanged(event
->wd
,
336 event
->mask
& (IN_CREATE
| IN_MOVED_TO
),
337 event
->mask
& (IN_DELETE
| IN_MOVED_FROM
),
338 event
->mask
& IN_ISDIR
);
342 FilePathWatcherImpl::FilePathWatcherImpl()
343 : recursive_(false) {
346 void FilePathWatcherImpl::OnFilePathChanged(InotifyReader::Watch fired_watch
,
347 const FilePath::StringType
& child
,
351 if (!task_runner()->BelongsToCurrentThread()) {
352 // Switch to task_runner() to access |watches_| safely.
353 task_runner()->PostTask(FROM_HERE
,
354 Bind(&FilePathWatcherImpl::OnFilePathChanged
, this,
355 fired_watch
, child
, created
, deleted
, is_dir
));
359 // Check to see if CancelOnMessageLoopThread() has already been called.
360 // May happen when code flow reaches here from the PostTask() above.
361 if (watches_
.empty()) {
362 DCHECK(target_
.empty());
366 DCHECK(MessageLoopForIO::current());
367 DCHECK(HasValidWatchVector());
369 // Used below to avoid multiple recursive updates.
370 bool did_update
= false;
372 // Find the entry in |watches_| that corresponds to |fired_watch|.
373 for (size_t i
= 0; i
< watches_
.size(); ++i
) {
374 const WatchEntry
& watch_entry
= watches_
[i
];
375 if (fired_watch
!= watch_entry
.watch
)
378 // Check whether a path component of |target_| changed.
379 bool change_on_target_path
=
381 (child
== watch_entry
.linkname
) ||
382 (child
== watch_entry
.subdir
);
384 // Check if the change references |target_| or a direct child of |target_|.
386 if (watch_entry
.subdir
.empty()) {
387 // The fired watch is for a WatchEntry without a subdir. Thus for a given
388 // |target_| = "/path/to/foo", this is for "foo". Here, check either:
389 // - the target has no symlink: it is the target and it changed.
390 // - the target has a symlink, and it matches |child|.
391 target_changed
= (watch_entry
.linkname
.empty() ||
392 child
== watch_entry
.linkname
);
394 // The fired watch is for a WatchEntry with a subdir. Thus for a given
395 // |target_| = "/path/to/foo", this is for {"/", "/path", "/path/to"}.
396 // So we can safely access the next WatchEntry since we have not reached
397 // the end yet. Check |watch_entry| is for "/path/to", i.e. the next
399 bool next_watch_may_be_for_target
= watches_
[i
+ 1].subdir
.empty();
400 if (next_watch_may_be_for_target
) {
401 // The current |watch_entry| is for "/path/to", so check if the |child|
402 // that changed is "foo".
403 target_changed
= watch_entry
.subdir
== child
;
405 // The current |watch_entry| is not for "/path/to", so the next entry
406 // cannot be "foo". Thus |target_| has not changed.
407 target_changed
= false;
411 // Update watches if a directory component of the |target_| path
412 // (dis)appears. Note that we don't add the additional restriction of
413 // checking the event mask to see if it is for a directory here as changes
414 // to symlinks on the target path will not have IN_ISDIR set in the event
415 // masks. As a result we may sometimes call UpdateWatches() unnecessarily.
416 if (change_on_target_path
&& (created
|| deleted
) && !did_update
) {
421 // Report the following events:
422 // - The target or a direct child of the target got changed (in case the
423 // watched path refers to a directory).
424 // - One of the parent directories got moved or deleted, since the target
425 // disappears in this case.
426 // - One of the parent directories appears. The event corresponding to
427 // the target appearing might have been missed in this case, so recheck.
428 if (target_changed
||
429 (change_on_target_path
&& deleted
) ||
430 (change_on_target_path
&& created
&& PathExists(target_
))) {
432 UpdateRecursiveWatches(fired_watch
, is_dir
);
435 callback_
.Run(target_
, false /* error */);
440 if (ContainsKey(recursive_paths_by_watch_
, fired_watch
)) {
442 UpdateRecursiveWatches(fired_watch
, is_dir
);
443 callback_
.Run(target_
, false /* error */);
447 bool FilePathWatcherImpl::Watch(const FilePath
& path
,
449 const FilePathWatcher::Callback
& callback
) {
450 DCHECK(target_
.empty());
451 DCHECK(MessageLoopForIO::current());
453 set_task_runner(ThreadTaskRunnerHandle::Get());
454 callback_
= callback
;
456 recursive_
= recursive
;
457 MessageLoop::current()->AddDestructionObserver(this);
459 std::vector
<FilePath::StringType
> comps
;
460 target_
.GetComponents(&comps
);
461 DCHECK(!comps
.empty());
462 for (size_t i
= 1; i
< comps
.size(); ++i
)
463 watches_
.push_back(WatchEntry(comps
[i
]));
464 watches_
.push_back(WatchEntry(FilePath::StringType()));
469 void FilePathWatcherImpl::Cancel() {
470 if (callback_
.is_null()) {
471 // Watch was never called, or the message_loop() thread is already gone.
476 // Switch to the message_loop() if necessary so we can access |watches_|.
477 if (!task_runner()->BelongsToCurrentThread()) {
478 task_runner()->PostTask(FROM_HERE
, Bind(&FilePathWatcher::CancelWatch
,
479 make_scoped_refptr(this)));
481 CancelOnMessageLoopThread();
485 void FilePathWatcherImpl::CancelOnMessageLoopThread() {
486 DCHECK(task_runner()->BelongsToCurrentThread());
489 if (!callback_
.is_null()) {
490 MessageLoop::current()->RemoveDestructionObserver(this);
494 for (size_t i
= 0; i
< watches_
.size(); ++i
)
495 g_inotify_reader
.Get().RemoveWatch(watches_
[i
].watch
, this);
500 RemoveRecursiveWatches();
503 void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() {
504 CancelOnMessageLoopThread();
507 void FilePathWatcherImpl::UpdateWatches() {
508 // Ensure this runs on the message_loop() exclusively in order to avoid
509 // concurrency issues.
510 DCHECK(task_runner()->BelongsToCurrentThread());
511 DCHECK(HasValidWatchVector());
513 // Walk the list of watches and update them as we go.
514 FilePath
path(FILE_PATH_LITERAL("/"));
515 for (size_t i
= 0; i
< watches_
.size(); ++i
) {
516 WatchEntry
& watch_entry
= watches_
[i
];
517 InotifyReader::Watch old_watch
= watch_entry
.watch
;
518 watch_entry
.watch
= InotifyReader::kInvalidWatch
;
519 watch_entry
.linkname
.clear();
520 watch_entry
.watch
= g_inotify_reader
.Get().AddWatch(path
, this);
521 if (watch_entry
.watch
== InotifyReader::kInvalidWatch
) {
522 // Ignore the error code (beyond symlink handling) to attempt to add
523 // watches on accessible children of unreadable directories. Note that
524 // this is a best-effort attempt; we may not catch events in this
527 AddWatchForBrokenSymlink(path
, &watch_entry
);
529 if (old_watch
!= watch_entry
.watch
)
530 g_inotify_reader
.Get().RemoveWatch(old_watch
, this);
531 path
= path
.Append(watch_entry
.subdir
);
534 UpdateRecursiveWatches(InotifyReader::kInvalidWatch
,
535 false /* is directory? */);
538 void FilePathWatcherImpl::UpdateRecursiveWatches(
539 InotifyReader::Watch fired_watch
,
544 if (!DirectoryExists(target_
)) {
545 RemoveRecursiveWatches();
549 // Check to see if this is a forced update or if some component of |target_|
550 // has changed. For these cases, redo the watches for |target_| and below.
551 if (!ContainsKey(recursive_paths_by_watch_
, fired_watch
)) {
552 UpdateRecursiveWatchesForPath(target_
);
556 // Underneath |target_|, only directory changes trigger watch updates.
560 const FilePath
& changed_dir
= recursive_paths_by_watch_
[fired_watch
];
562 std::map
<FilePath
, InotifyReader::Watch
>::iterator start_it
=
563 recursive_watches_by_path_
.lower_bound(changed_dir
);
564 std::map
<FilePath
, InotifyReader::Watch
>::iterator end_it
= start_it
;
565 for (; end_it
!= recursive_watches_by_path_
.end(); ++end_it
) {
566 const FilePath
& cur_path
= end_it
->first
;
567 if (!changed_dir
.IsParent(cur_path
))
569 if (!DirectoryExists(cur_path
))
570 g_inotify_reader
.Get().RemoveWatch(end_it
->second
, this);
572 recursive_watches_by_path_
.erase(start_it
, end_it
);
573 UpdateRecursiveWatchesForPath(changed_dir
);
576 void FilePathWatcherImpl::UpdateRecursiveWatchesForPath(const FilePath
& path
) {
578 DCHECK(!path
.empty());
579 DCHECK(DirectoryExists(path
));
581 // Note: SHOW_SYM_LINKS exposes symlinks as symlinks, so they are ignored
582 // rather than followed. Following symlinks can easily lead to the undesirable
583 // situation where the entire file system is being watched.
584 FileEnumerator
enumerator(
586 true /* recursive enumeration */,
587 FileEnumerator::DIRECTORIES
| FileEnumerator::SHOW_SYM_LINKS
);
588 for (FilePath current
= enumerator
.Next();
590 current
= enumerator
.Next()) {
591 DCHECK(enumerator
.GetInfo().IsDirectory());
593 if (!ContainsKey(recursive_watches_by_path_
, current
)) {
595 InotifyReader::Watch watch
=
596 g_inotify_reader
.Get().AddWatch(current
, this);
597 TrackWatchForRecursion(watch
, current
);
599 // Update existing watches.
600 InotifyReader::Watch old_watch
= recursive_watches_by_path_
[current
];
601 DCHECK_NE(InotifyReader::kInvalidWatch
, old_watch
);
602 InotifyReader::Watch watch
=
603 g_inotify_reader
.Get().AddWatch(current
, this);
604 if (watch
!= old_watch
) {
605 g_inotify_reader
.Get().RemoveWatch(old_watch
, this);
606 recursive_paths_by_watch_
.erase(old_watch
);
607 recursive_watches_by_path_
.erase(current
);
608 TrackWatchForRecursion(watch
, current
);
614 void FilePathWatcherImpl::TrackWatchForRecursion(InotifyReader::Watch watch
,
615 const FilePath
& path
) {
617 DCHECK(!path
.empty());
618 DCHECK(target_
.IsParent(path
));
620 if (watch
== InotifyReader::kInvalidWatch
)
623 DCHECK(!ContainsKey(recursive_paths_by_watch_
, watch
));
624 DCHECK(!ContainsKey(recursive_watches_by_path_
, path
));
625 recursive_paths_by_watch_
[watch
] = path
;
626 recursive_watches_by_path_
[path
] = watch
;
629 void FilePathWatcherImpl::RemoveRecursiveWatches() {
633 for (hash_map
<InotifyReader::Watch
, FilePath
>::const_iterator it
=
634 recursive_paths_by_watch_
.begin();
635 it
!= recursive_paths_by_watch_
.end();
637 g_inotify_reader
.Get().RemoveWatch(it
->first
, this);
639 recursive_paths_by_watch_
.clear();
640 recursive_watches_by_path_
.clear();
643 void FilePathWatcherImpl::AddWatchForBrokenSymlink(const FilePath
& path
,
644 WatchEntry
* watch_entry
) {
645 DCHECK_EQ(InotifyReader::kInvalidWatch
, watch_entry
->watch
);
647 if (!ReadSymbolicLink(path
, &link
))
650 if (!link
.IsAbsolute())
651 link
= path
.DirName().Append(link
);
653 // Try watching symlink target directory. If the link target is "/", then we
654 // shouldn't get here in normal situations and if we do, we'd watch "/" for
655 // changes to a component "/" which is harmless so no special treatment of
656 // this case is required.
657 InotifyReader::Watch watch
=
658 g_inotify_reader
.Get().AddWatch(link
.DirName(), this);
659 if (watch
== InotifyReader::kInvalidWatch
) {
660 // TODO(craig) Symlinks only work if the parent directory for the target
661 // exist. Ideally we should make sure we've watched all the components of
662 // the symlink path for changes. See crbug.com/91561 for details.
663 DPLOG(WARNING
) << "Watch failed for " << link
.DirName().value();
666 watch_entry
->watch
= watch
;
667 watch_entry
->linkname
= link
.BaseName().value();
670 bool FilePathWatcherImpl::HasValidWatchVector() const {
671 if (watches_
.empty())
673 for (size_t i
= 0; i
< watches_
.size() - 1; ++i
) {
674 if (watches_
[i
].subdir
.empty())
677 return watches_
[watches_
.size() - 1].subdir
.empty();
682 FilePathWatcher::FilePathWatcher() {
683 impl_
= new FilePathWatcherImpl();