blob: ec0f92456a4d4fed1f1c6b982f712dd34f73ebfe [file] [log] [blame]
Sergey Poromov04887322021-03-17 17:30:54 +01001// 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 Poromov9232e3f2021-04-26 19:03:18 +02008#include "base/callback.h"
Sergey Poromov04887322021-03-17 17:30:54 +01009#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 Poromov9232e3f2021-04-26 19:03:18 +020013#include "dlp/fanotify_reader_thread.h"
Sergey Poromov04887322021-03-17 17:30:54 +010014
15namespace dlp {
16
17// Interacts with fanotify API to process file access events.
18class FanotifyWatcher : public FanotifyReaderThread::Delegate {
19 public:
20 class Delegate {
21 public:
Sergey Poromov9232e3f2021-04-26 19:03:18 +020022 virtual void ProcessFileOpenRequest(
23 ino_t inode, int pid, base::OnceCallback<void(bool)> callback) = 0;
Sergey Poromov04887322021-03-17 17:30:54 +010024 };
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 Poromov9232e3f2021-04-26 19:03:18 +020037 void OnRequestProcessed(base::ScopedFD fd, bool allowed);
38
Sergey Poromov04887322021-03-17 17:30:54 +010039 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_