Sergey Poromov | 0488732 | 2021-03-17 17:30:54 +0100 | [diff] [blame] | 1 | // Copyright 2021 The Chromium OS 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 | #ifndef DLP_FANOTIFY_WATCHER_H_ |
| 6 | #define DLP_FANOTIFY_WATCHER_H_ |
| 7 | |
Sergey Poromov | 9232e3f | 2021-04-26 19:03:18 +0200 | [diff] [blame] | 8 | #include "base/callback.h" |
Sergey Poromov | 0488732 | 2021-03-17 17:30:54 +0100 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
| 10 | #include "base/files/scoped_file.h" |
| 11 | #include "base/memory/scoped_refptr.h" |
| 12 | #include "base/threading/sequenced_task_runner_handle.h" |
Sergey Poromov | 9232e3f | 2021-04-26 19:03:18 +0200 | [diff] [blame] | 13 | #include "dlp/fanotify_reader_thread.h" |
Sergey Poromov | 0488732 | 2021-03-17 17:30:54 +0100 | [diff] [blame] | 14 | |
| 15 | namespace dlp { |
| 16 | |
| 17 | // Interacts with fanotify API to process file access events. |
| 18 | class FanotifyWatcher : public FanotifyReaderThread::Delegate { |
| 19 | public: |
| 20 | class Delegate { |
| 21 | public: |
Sergey Poromov | 9232e3f | 2021-04-26 19:03:18 +0200 | [diff] [blame] | 22 | virtual void ProcessFileOpenRequest( |
| 23 | ino_t inode, int pid, base::OnceCallback<void(bool)> callback) = 0; |
Sergey Poromov | 0488732 | 2021-03-17 17:30:54 +0100 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | explicit FanotifyWatcher(Delegate* delegate); |
| 27 | ~FanotifyWatcher(); |
| 28 | FanotifyWatcher(const FanotifyWatcher&) = delete; |
| 29 | FanotifyWatcher& operator=(const FanotifyWatcher&) = delete; |
| 30 | |
| 31 | // Start to listen to event for the mount point with |path|. |
| 32 | void AddWatch(const base::FilePath& path); |
| 33 | |
| 34 | private: |
| 35 | void OnFileOpenRequested(ino_t inode, int pid, base::ScopedFD fd) override; |
| 36 | |
Sergey Poromov | 9232e3f | 2021-04-26 19:03:18 +0200 | [diff] [blame] | 37 | void OnRequestProcessed(base::ScopedFD fd, bool allowed); |
| 38 | |
Sergey Poromov | 0488732 | 2021-03-17 17:30:54 +0100 | [diff] [blame] | 39 | scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 40 | // fanotify file descriptor should be destructed before the reader thread so |
| 41 | // that the read loop there will exit on closed file descriptor. |
| 42 | FanotifyReaderThread thread_; |
| 43 | base::ScopedFD fanotify_fd_; |
| 44 | Delegate* delegate_; |
| 45 | }; |
| 46 | |
| 47 | } // namespace dlp |
| 48 | |
| 49 | #endif // DLP_FANOTIFY_WATCHER_H_ |