Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 1 | // 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 Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 8 | #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 Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 24 | namespace brillo { |
| 25 | class Udev; |
| 26 | class UdevDevice; |
| 27 | class UdevMonitor; |
| 28 | } // namespace brillo |
| 29 | |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 30 | namespace 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. |
| 38 | class DiskMonitor : public DeviceEventSourceInterface { |
| 39 | public: |
| 40 | DiskMonitor(); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame^] | 41 | DiskMonitor(const DiskMonitor&) = delete; |
| 42 | DiskMonitor& operator=(const DiskMonitor&) = delete; |
| 43 | |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 44 | ~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 Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 57 | // A file descriptor that can be select()ed or poll()ed for system changes. |
Anand K Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 58 | int udev_monitor_fd() const; |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 59 | |
| 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 Datsenko | ad8b504 | 2020-09-21 22:39:04 +1000 | [diff] [blame] | 65 | // 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 Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 71 | private: |
François Degros | 16ad1ae | 2019-07-17 16:02:39 +1000 | [diff] [blame] | 72 | // An EnumerateBlockDevices callback that emulates an 'add' action on |
| 73 | // |device|. Always returns true to continue enumeration in |
| 74 | // EnumerateBlockDevices. |
Anand K Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 75 | bool EmulateAddBlockDeviceEvent(std::unique_ptr<brillo::UdevDevice> device); |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 76 | |
| 77 | // Enumerates the block devices on the system and invokes |callback| for each |
Anand K Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 78 | // device found during the enumeration. The enumeration stops if |callback| |
| 79 | // returns false. |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 80 | void EnumerateBlockDevices( |
Anand K Mistry | c0ece3d | 2020-01-28 15:55:29 +1100 | [diff] [blame] | 81 | base::RepeatingCallback<bool(std::unique_ptr<brillo::UdevDevice> dev)> |
Anand K Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 82 | callback) const; |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 83 | |
| 84 | // Determines one or more device/disk events from a udev block device change. |
Anand K Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 85 | void ProcessBlockDeviceEvents(std::unique_ptr<brillo::UdevDevice> device, |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 86 | 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 Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 91 | void ProcessMmcOrScsiDeviceEvents(std::unique_ptr<brillo::UdevDevice> device, |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 92 | const char* action, |
| 93 | DeviceEventList* events); |
| 94 | |
| 95 | // The root udev object. |
Anand K Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 96 | std::unique_ptr<brillo::Udev> udev_; |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 97 | |
| 98 | // Provides access to udev changes as they occur. |
Anand K Mistry | 5757f63 | 2019-09-11 13:29:41 +1000 | [diff] [blame] | 99 | std::unique_ptr<brillo::UdevMonitor> udev_monitor_; |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 100 | |
| 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 Datsenko | ad8b504 | 2020-09-21 22:39:04 +1000 | [diff] [blame] | 108 | std::set<std::string> allowlist_; |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } // namespace cros_disks |
| 112 | |
Ben Chan | a9181d6 | 2019-04-10 16:29:10 -0700 | [diff] [blame] | 113 | #endif // CROS_DISKS_DISK_MONITOR_H_ |