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/stl_util.h"
32 #include "base/synchronization/lock.h"
33 #include "base/thread_task_runner_handle.h"
34 #include "base/threading/thread.h"
35 #include "base/trace_event/trace_event.h"
41 class FilePathWatcherImpl
;
43 // Singleton to manage all inotify watches.
44 // TODO(tony): It would be nice if this wasn't a singleton.
45 // http://crbug.com/38174
48 typedef int Watch
; // Watch descriptor used by AddWatch and RemoveWatch.
49 static const Watch kInvalidWatch
= -1;
51 // Watch directory |path| for changes. |watcher| will be notified on each
52 // change. Returns kInvalidWatch on failure.
53 Watch
AddWatch(const FilePath
& path
, FilePathWatcherImpl
* watcher
);
55 // Remove |watch| if it's valid.
56 void RemoveWatch(Watch watch
, FilePathWatcherImpl
* watcher
);
58 // Callback for InotifyReaderTask.
59 void OnInotifyEvent(const inotify_event
* event
);
62 friend struct DefaultLazyInstanceTraits
<InotifyReader
>;
64 typedef std::set
<FilePathWatcherImpl
*> WatcherSet
;
69 // We keep track of which delegates want to be notified on which watches.
70 hash_map
<Watch
, WatcherSet
> watchers_
;
72 // Lock to protect watchers_.
75 // Separate thread on which we run blocking read for inotify events.
78 // File descriptor returned by inotify_init.
79 const int inotify_fd_
;
81 // Use self-pipe trick to unblock select during shutdown.
82 int shutdown_pipe_
[2];
84 // Flag set to true when startup was successful.
87 DISALLOW_COPY_AND_ASSIGN(InotifyReader
);
90 class FilePathWatcherImpl
: public FilePathWatcher::PlatformDelegate
,
91 public MessageLoop::DestructionObserver
{
93 FilePathWatcherImpl();
95 // Called for each event coming from the watch. |fired_watch| identifies the
96 // watch that fired, |child| indicates what has changed, and is relative to
97 // the currently watched path for |fired_watch|.
99 // |created| is true if the object appears.
100 // |deleted| is true if the object disappears.
101 // |is_dir| is true if the object is a directory.
102 void OnFilePathChanged(InotifyReader::Watch fired_watch
,
103 const FilePath::StringType
& child
,
109 ~FilePathWatcherImpl() override
{}
112 // Start watching |path| for changes and notify |delegate| on each change.
113 // Returns true if watch for |path| has been added successfully.
114 bool Watch(const FilePath
& path
,
116 const FilePathWatcher::Callback
& callback
) override
;
118 // Cancel the watch. This unregisters the instance with InotifyReader.
119 void Cancel() override
;
121 // Cleans up and stops observing the message_loop() thread.
122 void CancelOnMessageLoopThread() override
;
124 // Deletion of the FilePathWatcher will call Cancel() to dispose of this
125 // object in the right thread. This also observes destruction of the required
126 // cleanup thread, in case it quits before Cancel() is called.
127 void WillDestroyCurrentMessageLoop() override
;
129 // Inotify watches are installed for all directory components of |target_|.
130 // A WatchEntry instance holds:
131 // - |watch|: the watch descriptor for a component.
132 // - |subdir|: the subdirectory that identifies the next component.
133 // - For the last component, there is no next component, so it is empty.
134 // - |linkname|: the target of the symlink.
135 // - Only if the target being watched is a symbolic link.
137 explicit WatchEntry(const FilePath::StringType
& dirname
)
138 : watch(InotifyReader::kInvalidWatch
),
141 InotifyReader::Watch watch
;
142 FilePath::StringType subdir
;
143 FilePath::StringType linkname
;
145 typedef std::vector
<WatchEntry
> WatchVector
;
147 // Reconfigure to watch for the most specific parent directory of |target_|
148 // that exists. Also calls UpdateRecursiveWatches() below.
149 void UpdateWatches();
151 // Reconfigure to recursively watch |target_| and all its sub-directories.
152 // - This is a no-op if the watch is not recursive.
153 // - If |target_| does not exist, then clear all the recursive watches.
154 // - Assuming |target_| exists, passing kInvalidWatch as |fired_watch| forces
155 // addition of recursive watches for |target_|.
156 // - Otherwise, only the directory associated with |fired_watch| and its
157 // sub-directories will be reconfigured.
158 void UpdateRecursiveWatches(InotifyReader::Watch fired_watch
, bool is_dir
);
160 // Enumerate recursively through |path| and add / update watches.
161 void UpdateRecursiveWatchesForPath(const FilePath
& path
);
163 // Do internal bookkeeping to update mappings between |watch| and its
164 // associated full path |path|.
165 void TrackWatchForRecursion(InotifyReader::Watch watch
, const FilePath
& path
);
167 // Remove all the recursive watches.
168 void RemoveRecursiveWatches();
170 // |path| is a symlink to a non-existent target. Attempt to add a watch to
171 // the link target's parent directory. Update |watch_entry| on success.
172 void AddWatchForBrokenSymlink(const FilePath
& path
, WatchEntry
* watch_entry
);
174 bool HasValidWatchVector() const;
176 // Callback to notify upon changes.
177 FilePathWatcher::Callback callback_
;
179 // The file or directory we're supposed to watch.
184 // The vector of watches and next component names for all path components,
185 // starting at the root directory. The last entry corresponds to the watch for
186 // |target_| and always stores an empty next component name in |subdir|.
187 WatchVector watches_
;
189 hash_map
<InotifyReader::Watch
, FilePath
> recursive_paths_by_watch_
;
190 std::map
<FilePath
, InotifyReader::Watch
> recursive_watches_by_path_
;
192 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl
);
195 void InotifyReaderCallback(InotifyReader
* reader
, int inotify_fd
,
197 // Make sure the file descriptors are good for use with select().
198 CHECK_LE(0, inotify_fd
);
199 CHECK_GT(FD_SETSIZE
, inotify_fd
);
200 CHECK_LE(0, shutdown_fd
);
201 CHECK_GT(FD_SETSIZE
, shutdown_fd
);
203 trace_event::TraceLog::GetInstance()->SetCurrentThreadBlocksMessageLoop();
208 FD_SET(inotify_fd
, &rfds
);
209 FD_SET(shutdown_fd
, &rfds
);
211 // Wait until some inotify events are available.
213 HANDLE_EINTR(select(std::max(inotify_fd
, shutdown_fd
) + 1,
214 &rfds
, NULL
, NULL
, NULL
));
215 if (select_result
< 0) {
216 DPLOG(WARNING
) << "select failed";
220 if (FD_ISSET(shutdown_fd
, &rfds
))
223 // Adjust buffer size to current event queue size.
225 int ioctl_result
= HANDLE_EINTR(ioctl(inotify_fd
, FIONREAD
,
228 if (ioctl_result
!= 0) {
229 DPLOG(WARNING
) << "ioctl failed";
233 std::vector
<char> buffer(buffer_size
);
235 ssize_t bytes_read
= HANDLE_EINTR(read(inotify_fd
, &buffer
[0],
238 if (bytes_read
< 0) {
239 DPLOG(WARNING
) << "read from inotify fd failed";
244 while (i
< bytes_read
) {
245 inotify_event
* event
= reinterpret_cast<inotify_event
*>(&buffer
[i
]);
246 size_t event_size
= sizeof(inotify_event
) + event
->len
;
247 DCHECK(i
+ event_size
<= static_cast<size_t>(bytes_read
));
248 reader
->OnInotifyEvent(event
);
254 static LazyInstance
<InotifyReader
>::Leaky g_inotify_reader
=
255 LAZY_INSTANCE_INITIALIZER
;
257 InotifyReader::InotifyReader()
258 : thread_("inotify_reader"),
259 inotify_fd_(inotify_init()),
262 PLOG(ERROR
) << "inotify_init() failed";
264 shutdown_pipe_
[0] = -1;
265 shutdown_pipe_
[1] = -1;
266 if (inotify_fd_
>= 0 && pipe(shutdown_pipe_
) == 0 && thread_
.Start()) {
267 thread_
.task_runner()->PostTask(
269 Bind(&InotifyReaderCallback
, this, inotify_fd_
, shutdown_pipe_
[0]));
274 InotifyReader::~InotifyReader() {
276 // Write to the self-pipe so that the select call in InotifyReaderTask
278 ssize_t ret
= HANDLE_EINTR(write(shutdown_pipe_
[1], "", 1));
283 if (inotify_fd_
>= 0)
285 if (shutdown_pipe_
[0] >= 0)
286 close(shutdown_pipe_
[0]);
287 if (shutdown_pipe_
[1] >= 0)
288 close(shutdown_pipe_
[1]);
291 InotifyReader::Watch
InotifyReader::AddWatch(
292 const FilePath
& path
, FilePathWatcherImpl
* watcher
) {
294 return kInvalidWatch
;
296 AutoLock
auto_lock(lock_
);
298 Watch watch
= inotify_add_watch(inotify_fd_
, path
.value().c_str(),
299 IN_ATTRIB
| IN_CREATE
| IN_DELETE
|
300 IN_CLOSE_WRITE
| IN_MOVE
|
303 if (watch
== kInvalidWatch
)
304 return kInvalidWatch
;
306 watchers_
[watch
].insert(watcher
);
311 void InotifyReader::RemoveWatch(Watch watch
, FilePathWatcherImpl
* watcher
) {
312 if (!valid_
|| (watch
== kInvalidWatch
))
315 AutoLock
auto_lock(lock_
);
317 watchers_
[watch
].erase(watcher
);
319 if (watchers_
[watch
].empty()) {
320 watchers_
.erase(watch
);
321 inotify_rm_watch(inotify_fd_
, watch
);
325 void InotifyReader::OnInotifyEvent(const inotify_event
* event
) {
326 if (event
->mask
& IN_IGNORED
)
329 FilePath::StringType
child(event
->len
? event
->name
: FILE_PATH_LITERAL(""));
330 AutoLock
auto_lock(lock_
);
332 for (WatcherSet::iterator watcher
= watchers_
[event
->wd
].begin();
333 watcher
!= watchers_
[event
->wd
].end();
335 (*watcher
)->OnFilePathChanged(event
->wd
,
337 event
->mask
& (IN_CREATE
| IN_MOVED_TO
),
338 event
->mask
& (IN_DELETE
| IN_MOVED_FROM
),
339 event
->mask
& IN_ISDIR
);
343 FilePathWatcherImpl::FilePathWatcherImpl()
344 : recursive_(false) {
347 void FilePathWatcherImpl::OnFilePathChanged(InotifyReader::Watch fired_watch
,
348 const FilePath::StringType
& child
,
352 if (!task_runner()->BelongsToCurrentThread()) {
353 // Switch to task_runner() to access |watches_| safely.
354 task_runner()->PostTask(FROM_HERE
,
355 Bind(&FilePathWatcherImpl::OnFilePathChanged
, this,
356 fired_watch
, child
, created
, deleted
, is_dir
));
360 // Check to see if CancelOnMessageLoopThread() has already been called.
361 // May happen when code flow reaches here from the PostTask() above.
362 if (watches_
.empty()) {
363 DCHECK(target_
.empty());
367 DCHECK(MessageLoopForIO::current());
368 DCHECK(HasValidWatchVector());
370 // Used below to avoid multiple recursive updates.
371 bool did_update
= false;
373 // Find the entry in |watches_| that corresponds to |fired_watch|.
374 for (size_t i
= 0; i
< watches_
.size(); ++i
) {
375 const WatchEntry
& watch_entry
= watches_
[i
];
376 if (fired_watch
!= watch_entry
.watch
)
379 // Check whether a path component of |target_| changed.
380 bool change_on_target_path
=
382 (child
== watch_entry
.linkname
) ||
383 (child
== watch_entry
.subdir
);
385 // Check if the change references |target_| or a direct child of |target_|.
387 if (watch_entry
.subdir
.empty()) {
388 // The fired watch is for a WatchEntry without a subdir. Thus for a given
389 // |target_| = "/path/to/foo", this is for "foo". Here, check either:
390 // - the target has no symlink: it is the target and it changed.
391 // - the target has a symlink, and it matches |child|.
392 target_changed
= (watch_entry
.linkname
.empty() ||
393 child
== watch_entry
.linkname
);
395 // The fired watch is for a WatchEntry with a subdir. Thus for a given
396 // |target_| = "/path/to/foo", this is for {"/", "/path", "/path/to"}.
397 // So we can safely access the next WatchEntry since we have not reached
398 // the end yet. Check |watch_entry| is for "/path/to", i.e. the next
400 bool next_watch_may_be_for_target
= watches_
[i
+ 1].subdir
.empty();
401 if (next_watch_may_be_for_target
) {
402 // The current |watch_entry| is for "/path/to", so check if the |child|
403 // that changed is "foo".
404 target_changed
= watch_entry
.subdir
== child
;
406 // The current |watch_entry| is not for "/path/to", so the next entry
407 // cannot be "foo". Thus |target_| has not changed.
408 target_changed
= false;
412 // Update watches if a directory component of the |target_| path
413 // (dis)appears. Note that we don't add the additional restriction of
414 // checking the event mask to see if it is for a directory here as changes
415 // to symlinks on the target path will not have IN_ISDIR set in the event
416 // masks. As a result we may sometimes call UpdateWatches() unnecessarily.
417 if (change_on_target_path
&& (created
|| deleted
) && !did_update
) {
422 // Report the following events:
423 // - The target or a direct child of the target got changed (in case the
424 // watched path refers to a directory).
425 // - One of the parent directories got moved or deleted, since the target
426 // disappears in this case.
427 // - One of the parent directories appears. The event corresponding to
428 // the target appearing might have been missed in this case, so recheck.
429 if (target_changed
||
430 (change_on_target_path
&& deleted
) ||
431 (change_on_target_path
&& created
&& PathExists(target_
))) {
433 UpdateRecursiveWatches(fired_watch
, is_dir
);
436 callback_
.Run(target_
, false /* error */);
441 if (ContainsKey(recursive_paths_by_watch_
, fired_watch
)) {
443 UpdateRecursiveWatches(fired_watch
, is_dir
);
444 callback_
.Run(target_
, false /* error */);
448 bool FilePathWatcherImpl::Watch(const FilePath
& path
,
450 const FilePathWatcher::Callback
& callback
) {
451 DCHECK(target_
.empty());
452 DCHECK(MessageLoopForIO::current());
454 set_task_runner(ThreadTaskRunnerHandle::Get());
455 callback_
= callback
;
457 recursive_
= recursive
;
458 MessageLoop::current()->AddDestructionObserver(this);
460 std::vector
<FilePath::StringType
> comps
;
461 target_
.GetComponents(&comps
);
462 DCHECK(!comps
.empty());
463 for (size_t i
= 1; i
< comps
.size(); ++i
)
464 watches_
.push_back(WatchEntry(comps
[i
]));
465 watches_
.push_back(WatchEntry(FilePath::StringType()));
470 void FilePathWatcherImpl::Cancel() {
471 if (callback_
.is_null()) {
472 // Watch was never called, or the message_loop() thread is already gone.
477 // Switch to the message_loop() if necessary so we can access |watches_|.
478 if (!task_runner()->BelongsToCurrentThread()) {
479 task_runner()->PostTask(FROM_HERE
, Bind(&FilePathWatcher::CancelWatch
,
480 make_scoped_refptr(this)));
482 CancelOnMessageLoopThread();
486 void FilePathWatcherImpl::CancelOnMessageLoopThread() {
487 DCHECK(task_runner()->BelongsToCurrentThread());
490 if (!callback_
.is_null()) {
491 MessageLoop::current()->RemoveDestructionObserver(this);
495 for (size_t i
= 0; i
< watches_
.size(); ++i
)
496 g_inotify_reader
.Get().RemoveWatch(watches_
[i
].watch
, this);
501 RemoveRecursiveWatches();
504 void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() {
505 CancelOnMessageLoopThread();
508 void FilePathWatcherImpl::UpdateWatches() {
509 // Ensure this runs on the message_loop() exclusively in order to avoid
510 // concurrency issues.
511 DCHECK(task_runner()->BelongsToCurrentThread());
512 DCHECK(HasValidWatchVector());
514 // Walk the list of watches and update them as we go.
515 FilePath
path(FILE_PATH_LITERAL("/"));
516 for (size_t i
= 0; i
< watches_
.size(); ++i
) {
517 WatchEntry
& watch_entry
= watches_
[i
];
518 InotifyReader::Watch old_watch
= watch_entry
.watch
;
519 watch_entry
.watch
= InotifyReader::kInvalidWatch
;
520 watch_entry
.linkname
.clear();
521 watch_entry
.watch
= g_inotify_reader
.Get().AddWatch(path
, this);
522 if (watch_entry
.watch
== InotifyReader::kInvalidWatch
) {
523 // Ignore the error code (beyond symlink handling) to attempt to add
524 // watches on accessible children of unreadable directories. Note that
525 // this is a best-effort attempt; we may not catch events in this
528 AddWatchForBrokenSymlink(path
, &watch_entry
);
530 if (old_watch
!= watch_entry
.watch
)
531 g_inotify_reader
.Get().RemoveWatch(old_watch
, this);
532 path
= path
.Append(watch_entry
.subdir
);
535 UpdateRecursiveWatches(InotifyReader::kInvalidWatch
,
536 false /* is directory? */);
539 void FilePathWatcherImpl::UpdateRecursiveWatches(
540 InotifyReader::Watch fired_watch
,
545 if (!DirectoryExists(target_
)) {
546 RemoveRecursiveWatches();
550 // Check to see if this is a forced update or if some component of |target_|
551 // has changed. For these cases, redo the watches for |target_| and below.
552 if (!ContainsKey(recursive_paths_by_watch_
, fired_watch
)) {
553 UpdateRecursiveWatchesForPath(target_
);
557 // Underneath |target_|, only directory changes trigger watch updates.
561 const FilePath
& changed_dir
= recursive_paths_by_watch_
[fired_watch
];
563 std::map
<FilePath
, InotifyReader::Watch
>::iterator start_it
=
564 recursive_watches_by_path_
.lower_bound(changed_dir
);
565 std::map
<FilePath
, InotifyReader::Watch
>::iterator end_it
= start_it
;
566 for (; end_it
!= recursive_watches_by_path_
.end(); ++end_it
) {
567 const FilePath
& cur_path
= end_it
->first
;
568 if (!changed_dir
.IsParent(cur_path
))
570 if (!DirectoryExists(cur_path
))
571 g_inotify_reader
.Get().RemoveWatch(end_it
->second
, this);
573 recursive_watches_by_path_
.erase(start_it
, end_it
);
574 UpdateRecursiveWatchesForPath(changed_dir
);
577 void FilePathWatcherImpl::UpdateRecursiveWatchesForPath(const FilePath
& path
) {
579 DCHECK(!path
.empty());
580 DCHECK(DirectoryExists(path
));
582 // Note: SHOW_SYM_LINKS exposes symlinks as symlinks, so they are ignored
583 // rather than followed. Following symlinks can easily lead to the undesirable
584 // situation where the entire file system is being watched.
585 FileEnumerator
enumerator(
587 true /* recursive enumeration */,
588 FileEnumerator::DIRECTORIES
| FileEnumerator::SHOW_SYM_LINKS
);
589 for (FilePath current
= enumerator
.Next();
591 current
= enumerator
.Next()) {
592 DCHECK(enumerator
.GetInfo().IsDirectory());
594 if (!ContainsKey(recursive_watches_by_path_
, current
)) {
596 InotifyReader::Watch watch
=
597 g_inotify_reader
.Get().AddWatch(current
, this);
598 TrackWatchForRecursion(watch
, current
);
600 // Update existing watches.
601 InotifyReader::Watch old_watch
= recursive_watches_by_path_
[current
];
602 DCHECK_NE(InotifyReader::kInvalidWatch
, old_watch
);
603 InotifyReader::Watch watch
=
604 g_inotify_reader
.Get().AddWatch(current
, this);
605 if (watch
!= old_watch
) {
606 g_inotify_reader
.Get().RemoveWatch(old_watch
, this);
607 recursive_paths_by_watch_
.erase(old_watch
);
608 recursive_watches_by_path_
.erase(current
);
609 TrackWatchForRecursion(watch
, current
);
615 void FilePathWatcherImpl::TrackWatchForRecursion(InotifyReader::Watch watch
,
616 const FilePath
& path
) {
618 DCHECK(!path
.empty());
619 DCHECK(target_
.IsParent(path
));
621 if (watch
== InotifyReader::kInvalidWatch
)
624 DCHECK(!ContainsKey(recursive_paths_by_watch_
, watch
));
625 DCHECK(!ContainsKey(recursive_watches_by_path_
, path
));
626 recursive_paths_by_watch_
[watch
] = path
;
627 recursive_watches_by_path_
[path
] = watch
;
630 void FilePathWatcherImpl::RemoveRecursiveWatches() {
634 for (hash_map
<InotifyReader::Watch
, FilePath
>::const_iterator it
=
635 recursive_paths_by_watch_
.begin();
636 it
!= recursive_paths_by_watch_
.end();
638 g_inotify_reader
.Get().RemoveWatch(it
->first
, this);
640 recursive_paths_by_watch_
.clear();
641 recursive_watches_by_path_
.clear();
644 void FilePathWatcherImpl::AddWatchForBrokenSymlink(const FilePath
& path
,
645 WatchEntry
* watch_entry
) {
646 DCHECK_EQ(InotifyReader::kInvalidWatch
, watch_entry
->watch
);
648 if (!ReadSymbolicLink(path
, &link
))
651 if (!link
.IsAbsolute())
652 link
= path
.DirName().Append(link
);
654 // Try watching symlink target directory. If the link target is "/", then we
655 // shouldn't get here in normal situations and if we do, we'd watch "/" for
656 // changes to a component "/" which is harmless so no special treatment of
657 // this case is required.
658 InotifyReader::Watch watch
=
659 g_inotify_reader
.Get().AddWatch(link
.DirName(), this);
660 if (watch
== InotifyReader::kInvalidWatch
) {
661 // TODO(craig) Symlinks only work if the parent directory for the target
662 // exist. Ideally we should make sure we've watched all the components of
663 // the symlink path for changes. See crbug.com/91561 for details.
664 DPLOG(WARNING
) << "Watch failed for " << link
.DirName().value();
667 watch_entry
->watch
= watch
;
668 watch_entry
->linkname
= link
.BaseName().value();
671 bool FilePathWatcherImpl::HasValidWatchVector() const {
672 if (watches_
.empty())
674 for (size_t i
= 0; i
< watches_
.size() - 1; ++i
) {
675 if (watches_
[i
].subdir
.empty())
678 return watches_
[watches_
.size() - 1].subdir
.empty();
683 FilePathWatcher::FilePathWatcher() {
684 impl_
= new FilePathWatcherImpl();