file_path_watcher_linux.cc revision f2477e01787aa58f445919b809d89e252beef54f
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
5#include "base/files/file_path_watcher.h"
6
7#include <errno.h>
8#include <string.h>
9#include <sys/inotify.h>
10#include <sys/ioctl.h>
11#include <sys/select.h>
12#include <unistd.h>
13
14#include <algorithm>
15#include <set>
16#include <utility>
17#include <vector>
18
19#include "base/bind.h"
20#include "base/containers/hash_tables.h"
21#include "base/debug/trace_event.h"
22#include "base/file_util.h"
23#include "base/files/file_path.h"
24#include "base/lazy_instance.h"
25#include "base/location.h"
26#include "base/logging.h"
27#include "base/memory/scoped_ptr.h"
28#include "base/message_loop/message_loop.h"
29#include "base/message_loop/message_loop_proxy.h"
30#include "base/posix/eintr_wrapper.h"
31#include "base/synchronization/lock.h"
32#include "base/threading/thread.h"
33
34namespace base {
35
36namespace {
37
38class FilePathWatcherImpl;
39
40// Singleton to manage all inotify watches.
41// TODO(tony): It would be nice if this wasn't a singleton.
42// http://crbug.com/38174
43class InotifyReader {
44 public:
45  typedef int Watch;  // Watch descriptor used by AddWatch and RemoveWatch.
46  static const Watch kInvalidWatch = -1;
47
48  // Watch directory |path| for changes. |watcher| will be notified on each
49  // change. Returns kInvalidWatch on failure.
50  Watch AddWatch(const FilePath& path, FilePathWatcherImpl* watcher);
51
52  // Remove |watch|. Returns true on success.
53  bool RemoveWatch(Watch watch, FilePathWatcherImpl* watcher);
54
55  // Callback for InotifyReaderTask.
56  void OnInotifyEvent(const inotify_event* event);
57
58 private:
59  friend struct DefaultLazyInstanceTraits<InotifyReader>;
60
61  typedef std::set<FilePathWatcherImpl*> WatcherSet;
62
63  InotifyReader();
64  ~InotifyReader();
65
66  // We keep track of which delegates want to be notified on which watches.
67  hash_map<Watch, WatcherSet> watchers_;
68
69  // Lock to protect watchers_.
70  Lock lock_;
71
72  // Separate thread on which we run blocking read for inotify events.
73  Thread thread_;
74
75  // File descriptor returned by inotify_init.
76  const int inotify_fd_;
77
78  // Use self-pipe trick to unblock select during shutdown.
79  int shutdown_pipe_[2];
80
81  // Flag set to true when startup was successful.
82  bool valid_;
83
84  DISALLOW_COPY_AND_ASSIGN(InotifyReader);
85};
86
87class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate,
88                            public MessageLoop::DestructionObserver {
89 public:
90  FilePathWatcherImpl();
91
92  // Called for each event coming from the watch. |fired_watch| identifies the
93  // watch that fired, |child| indicates what has changed, and is relative to
94  // the currently watched path for |fired_watch|. The flag |created| is true if
95  // the object appears.
96  void OnFilePathChanged(InotifyReader::Watch fired_watch,
97                         const FilePath::StringType& child,
98                         bool created);
99
100  // Start watching |path| for changes and notify |delegate| on each change.
101  // Returns true if watch for |path| has been added successfully.
102  virtual bool Watch(const FilePath& path,
103                     bool recursive,
104                     const FilePathWatcher::Callback& callback) OVERRIDE;
105
106  // Cancel the watch. This unregisters the instance with InotifyReader.
107  virtual void Cancel() OVERRIDE;
108
109  // Deletion of the FilePathWatcher will call Cancel() to dispose of this
110  // object in the right thread. This also observes destruction of the required
111  // cleanup thread, in case it quits before Cancel() is called.
112  virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
113
114 protected:
115  virtual ~FilePathWatcherImpl() {}
116
117 private:
118  // Cleans up and stops observing the |message_loop_| thread.
119  virtual void CancelOnMessageLoopThread() OVERRIDE;
120
121  // Inotify watches are installed for all directory components of |target_|. A
122  // WatchEntry instance holds the watch descriptor for a component and the
123  // subdirectory for that identifies the next component. If a symbolic link
124  // is being watched, the target of the link is also kept.
125  struct WatchEntry {
126    WatchEntry(InotifyReader::Watch watch, const FilePath::StringType& subdir)
127        : watch_(watch),
128          subdir_(subdir) {}
129
130    InotifyReader::Watch watch_;
131    FilePath::StringType subdir_;
132    FilePath::StringType linkname_;
133  };
134  typedef std::vector<WatchEntry> WatchVector;
135
136  // Reconfigure to watch for the most specific parent directory of |target_|
137  // that exists. Updates |watched_path_|. Returns true on success.
138  bool UpdateWatches() WARN_UNUSED_RESULT;
139
140  // Callback to notify upon changes.
141  FilePathWatcher::Callback callback_;
142
143  // The file or directory we're supposed to watch.
144  FilePath target_;
145
146  // The vector of watches and next component names for all path components,
147  // starting at the root directory. The last entry corresponds to the watch for
148  // |target_| and always stores an empty next component name in |subdir_|.
149  WatchVector watches_;
150
151  DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl);
152};
153
154void InotifyReaderCallback(InotifyReader* reader, int inotify_fd,
155                           int shutdown_fd) {
156  // Make sure the file descriptors are good for use with select().
157  CHECK_LE(0, inotify_fd);
158  CHECK_GT(FD_SETSIZE, inotify_fd);
159  CHECK_LE(0, shutdown_fd);
160  CHECK_GT(FD_SETSIZE, shutdown_fd);
161
162  debug::TraceLog::GetInstance()->SetCurrentThreadBlocksMessageLoop();
163
164  while (true) {
165    fd_set rfds;
166    FD_ZERO(&rfds);
167    FD_SET(inotify_fd, &rfds);
168    FD_SET(shutdown_fd, &rfds);
169
170    // Wait until some inotify events are available.
171    int select_result =
172      HANDLE_EINTR(select(std::max(inotify_fd, shutdown_fd) + 1,
173                          &rfds, NULL, NULL, NULL));
174    if (select_result < 0) {
175      DPLOG(WARNING) << "select failed";
176      return;
177    }
178
179    if (FD_ISSET(shutdown_fd, &rfds))
180      return;
181
182    // Adjust buffer size to current event queue size.
183    int buffer_size;
184    int ioctl_result = HANDLE_EINTR(ioctl(inotify_fd, FIONREAD,
185                                          &buffer_size));
186
187    if (ioctl_result != 0) {
188      DPLOG(WARNING) << "ioctl failed";
189      return;
190    }
191
192    std::vector<char> buffer(buffer_size);
193
194    ssize_t bytes_read = HANDLE_EINTR(read(inotify_fd, &buffer[0],
195                                           buffer_size));
196
197    if (bytes_read < 0) {
198      DPLOG(WARNING) << "read from inotify fd failed";
199      return;
200    }
201
202    ssize_t i = 0;
203    while (i < bytes_read) {
204      inotify_event* event = reinterpret_cast<inotify_event*>(&buffer[i]);
205      size_t event_size = sizeof(inotify_event) + event->len;
206      DCHECK(i + event_size <= static_cast<size_t>(bytes_read));
207      reader->OnInotifyEvent(event);
208      i += event_size;
209    }
210  }
211}
212
213static LazyInstance<InotifyReader>::Leaky g_inotify_reader =
214    LAZY_INSTANCE_INITIALIZER;
215
216InotifyReader::InotifyReader()
217    : thread_("inotify_reader"),
218      inotify_fd_(inotify_init()),
219      valid_(false) {
220  if (inotify_fd_ < 0)
221    PLOG(ERROR) << "inotify_init() failed";
222
223  shutdown_pipe_[0] = -1;
224  shutdown_pipe_[1] = -1;
225  if (inotify_fd_ >= 0 && pipe(shutdown_pipe_) == 0 && thread_.Start()) {
226    thread_.message_loop()->PostTask(
227        FROM_HERE, Bind(&InotifyReaderCallback, this, inotify_fd_,
228                              shutdown_pipe_[0]));
229    valid_ = true;
230  }
231}
232
233InotifyReader::~InotifyReader() {
234  if (valid_) {
235    // Write to the self-pipe so that the select call in InotifyReaderTask
236    // returns.
237    ssize_t ret = HANDLE_EINTR(write(shutdown_pipe_[1], "", 1));
238    DPCHECK(ret > 0);
239    DCHECK_EQ(ret, 1);
240    thread_.Stop();
241  }
242  if (inotify_fd_ >= 0)
243    close(inotify_fd_);
244  if (shutdown_pipe_[0] >= 0)
245    close(shutdown_pipe_[0]);
246  if (shutdown_pipe_[1] >= 0)
247    close(shutdown_pipe_[1]);
248}
249
250InotifyReader::Watch InotifyReader::AddWatch(
251    const FilePath& path, FilePathWatcherImpl* watcher) {
252  if (!valid_)
253    return kInvalidWatch;
254
255  AutoLock auto_lock(lock_);
256
257  Watch watch = inotify_add_watch(inotify_fd_, path.value().c_str(),
258                                  IN_CREATE | IN_DELETE |
259                                  IN_CLOSE_WRITE | IN_MOVE |
260                                  IN_ONLYDIR);
261
262  if (watch == kInvalidWatch)
263    return kInvalidWatch;
264
265  watchers_[watch].insert(watcher);
266
267  return watch;
268}
269
270bool InotifyReader::RemoveWatch(Watch watch,
271                                FilePathWatcherImpl* watcher) {
272  if (!valid_)
273    return false;
274
275  AutoLock auto_lock(lock_);
276
277  watchers_[watch].erase(watcher);
278
279  if (watchers_[watch].empty()) {
280    watchers_.erase(watch);
281    return (inotify_rm_watch(inotify_fd_, watch) == 0);
282  }
283
284  return true;
285}
286
287void InotifyReader::OnInotifyEvent(const inotify_event* event) {
288  if (event->mask & IN_IGNORED)
289    return;
290
291  FilePath::StringType child(event->len ? event->name : FILE_PATH_LITERAL(""));
292  AutoLock auto_lock(lock_);
293
294  for (WatcherSet::iterator watcher = watchers_[event->wd].begin();
295       watcher != watchers_[event->wd].end();
296       ++watcher) {
297    (*watcher)->OnFilePathChanged(event->wd,
298                                  child,
299                                  event->mask & (IN_CREATE | IN_MOVED_TO));
300  }
301}
302
303FilePathWatcherImpl::FilePathWatcherImpl() {
304}
305
306void FilePathWatcherImpl::OnFilePathChanged(InotifyReader::Watch fired_watch,
307                                            const FilePath::StringType& child,
308                                            bool created) {
309  if (!message_loop()->BelongsToCurrentThread()) {
310    // Switch to message_loop_ to access watches_ safely.
311    message_loop()->PostTask(FROM_HERE,
312        Bind(&FilePathWatcherImpl::OnFilePathChanged,
313                   this,
314                   fired_watch,
315                   child,
316                   created));
317    return;
318  }
319
320  DCHECK(MessageLoopForIO::current());
321
322  // Find the entry in |watches_| that corresponds to |fired_watch|.
323  WatchVector::const_iterator watch_entry(watches_.begin());
324  for ( ; watch_entry != watches_.end(); ++watch_entry) {
325    if (fired_watch == watch_entry->watch_) {
326      // Check whether a path component of |target_| changed.
327      bool change_on_target_path = child.empty() ||
328          ((child == watch_entry->subdir_) && watch_entry->linkname_.empty()) ||
329          (child == watch_entry->linkname_);
330
331      // Check whether the change references |target_| or a direct child.
332      DCHECK(watch_entry->subdir_.empty() ||
333          (watch_entry + 1) != watches_.end());
334      bool target_changed =
335          (watch_entry->subdir_.empty() && (child == watch_entry->linkname_)) ||
336          (watch_entry->subdir_.empty() && watch_entry->linkname_.empty()) ||
337          (watch_entry->subdir_ == child && (watch_entry + 1)->subdir_.empty());
338
339      // Update watches if a directory component of the |target_| path
340      // (dis)appears. Note that we don't add the additional restriction
341      // of checking the event mask to see if it is for a directory here
342      // as changes to symlinks on the target path will not have
343      // IN_ISDIR set in the event masks. As a result we may sometimes
344      // call UpdateWatches() unnecessarily.
345      if (change_on_target_path && !UpdateWatches()) {
346        callback_.Run(target_, true /* error */);
347        return;
348      }
349
350      // Report the following events:
351      //  - The target or a direct child of the target got changed (in case the
352      //    watched path refers to a directory).
353      //  - One of the parent directories got moved or deleted, since the target
354      //    disappears in this case.
355      //  - One of the parent directories appears. The event corresponding to
356      //    the target appearing might have been missed in this case, so
357      //    recheck.
358      if (target_changed ||
359          (change_on_target_path && !created) ||
360          (change_on_target_path && PathExists(target_))) {
361        callback_.Run(target_, false);
362        return;
363      }
364    }
365  }
366}
367
368bool FilePathWatcherImpl::Watch(const FilePath& path,
369                                bool recursive,
370                                const FilePathWatcher::Callback& callback) {
371  DCHECK(target_.empty());
372  DCHECK(MessageLoopForIO::current());
373  if (recursive) {
374    // Recursive watch is not supported on this platform.
375    NOTIMPLEMENTED();
376    return false;
377  }
378
379  set_message_loop(MessageLoopProxy::current().get());
380  callback_ = callback;
381  target_ = path;
382  MessageLoop::current()->AddDestructionObserver(this);
383
384  std::vector<FilePath::StringType> comps;
385  target_.GetComponents(&comps);
386  DCHECK(!comps.empty());
387  std::vector<FilePath::StringType>::const_iterator comp = comps.begin();
388  for (++comp; comp != comps.end(); ++comp)
389    watches_.push_back(WatchEntry(InotifyReader::kInvalidWatch, *comp));
390
391  watches_.push_back(WatchEntry(InotifyReader::kInvalidWatch,
392                                FilePath::StringType()));
393  return UpdateWatches();
394}
395
396void FilePathWatcherImpl::Cancel() {
397  if (callback_.is_null()) {
398    // Watch was never called, or the |message_loop_| thread is already gone.
399    set_cancelled();
400    return;
401  }
402
403  // Switch to the message_loop_ if necessary so we can access |watches_|.
404  if (!message_loop()->BelongsToCurrentThread()) {
405    message_loop()->PostTask(FROM_HERE,
406                             Bind(&FilePathWatcher::CancelWatch,
407                                        make_scoped_refptr(this)));
408  } else {
409    CancelOnMessageLoopThread();
410  }
411}
412
413void FilePathWatcherImpl::CancelOnMessageLoopThread() {
414  if (!is_cancelled())
415    set_cancelled();
416
417  if (!callback_.is_null()) {
418    MessageLoop::current()->RemoveDestructionObserver(this);
419    callback_.Reset();
420  }
421
422  for (WatchVector::iterator watch_entry(watches_.begin());
423       watch_entry != watches_.end(); ++watch_entry) {
424    if (watch_entry->watch_ != InotifyReader::kInvalidWatch)
425      g_inotify_reader.Get().RemoveWatch(watch_entry->watch_, this);
426  }
427  watches_.clear();
428  target_.clear();
429}
430
431void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() {
432  CancelOnMessageLoopThread();
433}
434
435bool FilePathWatcherImpl::UpdateWatches() {
436  // Ensure this runs on the |message_loop_| exclusively in order to avoid
437  // concurrency issues.
438  DCHECK(message_loop()->BelongsToCurrentThread());
439
440  // Walk the list of watches and update them as we go.
441  FilePath path(FILE_PATH_LITERAL("/"));
442  bool path_valid = true;
443  for (WatchVector::iterator watch_entry(watches_.begin());
444       watch_entry != watches_.end(); ++watch_entry) {
445    InotifyReader::Watch old_watch = watch_entry->watch_;
446    if (path_valid) {
447      watch_entry->watch_ = g_inotify_reader.Get().AddWatch(path, this);
448      if ((watch_entry->watch_ == InotifyReader::kInvalidWatch) &&
449          file_util::IsLink(path)) {
450        FilePath link;
451        if (ReadSymbolicLink(path, &link)) {
452          if (!link.IsAbsolute())
453            link = path.DirName().Append(link);
454          // Try watching symlink target directory. If the link target is "/",
455          // then we shouldn't get here in normal situations and if we do, we'd
456          // watch "/" for changes to a component "/" which is harmless so no
457          // special treatment of this case is required.
458          watch_entry->watch_ =
459              g_inotify_reader.Get().AddWatch(link.DirName(), this);
460          if (watch_entry->watch_ != InotifyReader::kInvalidWatch) {
461            watch_entry->linkname_ = link.BaseName().value();
462          } else {
463            DPLOG(WARNING) << "Watch failed for "  << link.DirName().value();
464            // TODO(craig) Symlinks only work if the parent directory
465            // for the target exist. Ideally we should make sure we've
466            // watched all the components of the symlink path for
467            // changes. See crbug.com/91561 for details.
468          }
469        }
470      }
471      if (watch_entry->watch_ == InotifyReader::kInvalidWatch) {
472        path_valid = false;
473      }
474    } else {
475      watch_entry->watch_ = InotifyReader::kInvalidWatch;
476    }
477    if (old_watch != InotifyReader::kInvalidWatch &&
478        old_watch != watch_entry->watch_) {
479      g_inotify_reader.Get().RemoveWatch(old_watch, this);
480    }
481    path = path.Append(watch_entry->subdir_);
482  }
483
484  return true;
485}
486
487}  // namespace
488
489FilePathWatcher::FilePathWatcher() {
490  impl_ = new FilePathWatcherImpl();
491}
492
493}  // namespace base
494