blob: 92cd1375cf638cc14e11d7c526840d1e692ecccf [file] [log] [blame]
Sergei Datsenko16821892019-04-05 11:26:38 +11001// Copyright 2019 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 CROS_DISKS_DISK_MONITOR_H_
6#define CROS_DISKS_DISK_MONITOR_H_
7
Sergei Datsenko16821892019-04-05 11:26:38 +11008#include <map>
9#include <memory>
10#include <set>
11#include <string>
12#include <vector>
13
14#include <base/callback.h>
15#include <base/files/file_path.h>
16#include <base/macros.h>
17#include <gtest/gtest_prod.h>
18
19#include "cros-disks/device_event.h"
20#include "cros-disks/device_event_source_interface.h"
21#include "cros-disks/disk.h"
22#include "cros-disks/mount_manager.h"
23
Anand K Mistry5757f632019-09-11 13:29:41 +100024namespace brillo {
25class Udev;
26class UdevDevice;
27class UdevMonitor;
28} // namespace brillo
29
Sergei Datsenko16821892019-04-05 11:26:38 +110030namespace cros_disks {
31
32// The DiskMonitor is responsible for reading device state from udev.
33// Said changes could be the result of a udev notification or a synchronous
34// call to enumerate the relevant storage devices attached to the system.
35//
36// This class is designed to run within a single-threaded GMainLoop application
37// and should not be considered thread safe.
38class DiskMonitor : public DeviceEventSourceInterface {
39 public:
40 DiskMonitor();
Qijiang Fan6bc59e12020-11-11 02:51:06 +090041 DiskMonitor(const DiskMonitor&) = delete;
42 DiskMonitor& operator=(const DiskMonitor&) = delete;
43
Sergei Datsenko16821892019-04-05 11:26:38 +110044 ~DiskMonitor() override;
45
46 // Initializes the disk monitor.
47 // Returns true on success.
48 virtual bool Initialize();
49
50 // Lists the current block devices attached to the system.
51 virtual std::vector<Disk> EnumerateDisks() const;
52
53 // Gets a Disk object that corresponds to a given device file.
54 virtual bool GetDiskByDevicePath(const base::FilePath& device_path,
55 Disk* disk) const;
56
Sergei Datsenko16821892019-04-05 11:26:38 +110057 // A file descriptor that can be select()ed or poll()ed for system changes.
Anand K Mistry5757f632019-09-11 13:29:41 +100058 int udev_monitor_fd() const;
Sergei Datsenko16821892019-04-05 11:26:38 +110059
60 // Implements the DeviceEventSourceInterface interface to read the changes
61 // from udev and converts the changes into device events. Returns false on
62 // error or if not device event is available. Must be called to clear the fd.
63 bool GetDeviceEvents(DeviceEventList* events) override;
64
Sergei Datsenkoad8b5042020-09-21 22:39:04 +100065 // Adds the specified device to a list allowing it to be used despite
66 // not having certain properties. Used by tests to perform operations on
67 // loopback devices.
68 void AddDeviceToAllowlist(const base::FilePath& device);
69 void RemoveDeviceFromAllowlist(const base::FilePath& device);
70
Sergei Datsenko16821892019-04-05 11:26:38 +110071 private:
François Degros16ad1ae2019-07-17 16:02:39 +100072 // An EnumerateBlockDevices callback that emulates an 'add' action on
73 // |device|. Always returns true to continue enumeration in
74 // EnumerateBlockDevices.
Anand K Mistry5757f632019-09-11 13:29:41 +100075 bool EmulateAddBlockDeviceEvent(std::unique_ptr<brillo::UdevDevice> device);
Sergei Datsenko16821892019-04-05 11:26:38 +110076
77 // Enumerates the block devices on the system and invokes |callback| for each
Anand K Mistry5757f632019-09-11 13:29:41 +100078 // device found during the enumeration. The enumeration stops if |callback|
79 // returns false.
Sergei Datsenko16821892019-04-05 11:26:38 +110080 void EnumerateBlockDevices(
Anand K Mistryc0ece3d2020-01-28 15:55:29 +110081 base::RepeatingCallback<bool(std::unique_ptr<brillo::UdevDevice> dev)>
Anand K Mistry5757f632019-09-11 13:29:41 +100082 callback) const;
Sergei Datsenko16821892019-04-05 11:26:38 +110083
84 // Determines one or more device/disk events from a udev block device change.
Anand K Mistry5757f632019-09-11 13:29:41 +100085 void ProcessBlockDeviceEvents(std::unique_ptr<brillo::UdevDevice> device,
Sergei Datsenko16821892019-04-05 11:26:38 +110086 const char* action,
87 DeviceEventList* events);
88
89 // Determines one or more device/disk events from a udev MMC or SCSI device
90 // change.
Anand K Mistry5757f632019-09-11 13:29:41 +100091 void ProcessMmcOrScsiDeviceEvents(std::unique_ptr<brillo::UdevDevice> device,
Sergei Datsenko16821892019-04-05 11:26:38 +110092 const char* action,
93 DeviceEventList* events);
94
95 // The root udev object.
Anand K Mistry5757f632019-09-11 13:29:41 +100096 std::unique_ptr<brillo::Udev> udev_;
Sergei Datsenko16821892019-04-05 11:26:38 +110097
98 // Provides access to udev changes as they occur.
Anand K Mistry5757f632019-09-11 13:29:41 +100099 std::unique_ptr<brillo::UdevMonitor> udev_monitor_;
Sergei Datsenko16821892019-04-05 11:26:38 +1100100
101 // A set of device sysfs paths detected by the udev monitor.
102 std::set<std::string> devices_detected_;
103
104 // A mapping from a sysfs path of a disk, detected by the udev monitor,
105 // to a set of sysfs paths of the immediate children of the disk.
106 std::map<std::string, std::set<std::string>> disks_detected_;
107
Sergei Datsenkoad8b5042020-09-21 22:39:04 +1000108 std::set<std::string> allowlist_;
Sergei Datsenko16821892019-04-05 11:26:38 +1100109};
110
111} // namespace cros_disks
112
Ben Chana9181d62019-04-10 16:29:10 -0700113#endif // CROS_DISKS_DISK_MONITOR_H_