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_READER_THREAD_H_ |
| 6 | #define DLP_FANOTIFY_READER_THREAD_H_ |
| 7 | |
| 8 | #include "base/files/scoped_file.h" |
| 9 | #include "base/memory/scoped_refptr.h" |
| 10 | #include "base/threading/platform_thread.h" |
| 11 | #include "base/threading/sequenced_task_runner_handle.h" |
| 12 | |
| 13 | namespace dlp { |
| 14 | |
| 15 | // Reads events from fanotify file descriptor and post them to the delegate. |
| 16 | class FanotifyReaderThread : public base::PlatformThread::Delegate { |
| 17 | public: |
| 18 | class Delegate { |
| 19 | public: |
| 20 | // Request to process the file |inode| open request from process |pid|. |
| 21 | // |fd| is the file descriptor to the file. |
| 22 | virtual void OnFileOpenRequested(ino_t inode, |
| 23 | int pid, |
| 24 | base::ScopedFD fd) = 0; |
| 25 | |
| 26 | protected: |
| 27 | virtual ~Delegate() = default; |
| 28 | }; |
| 29 | |
| 30 | FanotifyReaderThread( |
| 31 | scoped_refptr<base::SequencedTaskRunner> parent_task_runner, |
| 32 | Delegate* delegate); |
| 33 | ~FanotifyReaderThread() override; |
| 34 | |
| 35 | // Starts the thread to read events from |fanotify_fd|. |
| 36 | void StartThread(int fanotify_fd); |
| 37 | |
| 38 | private: |
| 39 | // base::PlatformThread::Delegate overrides: |
| 40 | void ThreadMain() override; |
| 41 | |
| 42 | void RunLoop(); |
| 43 | |
| 44 | // Task runner from which this thread is started and where the delegate is |
| 45 | // running. |
| 46 | scoped_refptr<base::SequencedTaskRunner> parent_task_runner_; |
| 47 | Delegate* const delegate_; |
| 48 | int fanotify_fd_ = -1; |
| 49 | base::PlatformThreadHandle handle_; |
| 50 | }; |
| 51 | |
| 52 | } // namespace dlp |
| 53 | |
| 54 | #endif // DLP_FANOTIFY_READER_THREAD_H_ |