The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _INIT_DEVICES_H |
| 18 | #define _INIT_DEVICES_H |
| 19 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 20 | #include <sys/stat.h> |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 22 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 23 | #include <algorithm> |
Eric Caruso | d17d5c5 | 2024-10-28 15:30:11 -0400 | [diff] [blame] | 24 | #include <map> |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 25 | #include <set> |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 26 | #include <string> |
| 27 | #include <vector> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 28 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 29 | #include <android-base/file.h> |
| 30 | #include <selinux/label.h> |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 31 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 32 | #include "uevent.h" |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 33 | #include "uevent_handler.h" |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 34 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace init { |
| 37 | |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 38 | class Permissions { |
| 39 | public: |
Tom Cherry | 5f0198b | 2018-07-17 15:28:16 -0700 | [diff] [blame] | 40 | friend void TestPermissions(const Permissions& expected, const Permissions& test); |
| 41 | |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 42 | Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid, bool no_fnm_pathname); |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 43 | |
| 44 | bool Match(const std::string& path) const; |
| 45 | |
| 46 | mode_t perm() const { return perm_; } |
| 47 | uid_t uid() const { return uid_; } |
| 48 | gid_t gid() const { return gid_; } |
| 49 | |
| 50 | protected: |
| 51 | const std::string& name() const { return name_; } |
| 52 | |
| 53 | private: |
| 54 | std::string name_; |
| 55 | mode_t perm_; |
| 56 | uid_t uid_; |
| 57 | gid_t gid_; |
| 58 | bool prefix_; |
| 59 | bool wildcard_; |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 60 | bool no_fnm_pathname_; |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | class SysfsPermissions : public Permissions { |
| 64 | public: |
Tom Cherry | 5f0198b | 2018-07-17 15:28:16 -0700 | [diff] [blame] | 65 | friend void TestSysfsPermissions(const SysfsPermissions& expected, const SysfsPermissions& test); |
| 66 | |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 67 | SysfsPermissions(const std::string& name, const std::string& attribute, mode_t perm, uid_t uid, |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 68 | gid_t gid, bool no_fnm_pathname) |
| 69 | : Permissions(name, perm, uid, gid, no_fnm_pathname), attribute_(attribute) {} |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 70 | |
| 71 | bool MatchWithSubsystem(const std::string& path, const std::string& subsystem) const; |
| 72 | void SetPermissions(const std::string& path) const; |
| 73 | |
| 74 | private: |
| 75 | const std::string attribute_; |
| 76 | }; |
| 77 | |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 78 | class Subsystem { |
| 79 | public: |
| 80 | friend class SubsystemParser; |
Tom Cherry | 5f0198b | 2018-07-17 15:28:16 -0700 | [diff] [blame] | 81 | friend void TestSubsystems(const Subsystem& expected, const Subsystem& test); |
| 82 | |
| 83 | enum DevnameSource { |
| 84 | DEVNAME_UEVENT_DEVNAME, |
| 85 | DEVNAME_UEVENT_DEVPATH, |
A. Cody Schuffelen | b479666 | 2024-04-23 18:59:00 -0700 | [diff] [blame] | 86 | DEVNAME_SYS_NAME, |
Tom Cherry | 5f0198b | 2018-07-17 15:28:16 -0700 | [diff] [blame] | 87 | }; |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 88 | |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 89 | Subsystem() {} |
Tom Cherry | 7c1d87e | 2019-07-10 11:18:24 -0700 | [diff] [blame] | 90 | Subsystem(std::string name) : name_(std::move(name)) {} |
| 91 | Subsystem(std::string name, DevnameSource source, std::string dir_name) |
| 92 | : name_(std::move(name)), devname_source_(source), dir_name_(std::move(dir_name)) {} |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 93 | |
| 94 | // Returns the full path for a uevent of a device that is a member of this subsystem, |
| 95 | // according to the rules parsed from ueventd.rc |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 96 | std::string ParseDevPath(const Uevent& uevent) const { |
A. Cody Schuffelen | b479666 | 2024-04-23 18:59:00 -0700 | [diff] [blame] | 97 | std::string devname; |
| 98 | if (devname_source_ == DEVNAME_UEVENT_DEVNAME) { |
| 99 | devname = uevent.device_name; |
| 100 | } else if (devname_source_ == DEVNAME_UEVENT_DEVPATH) { |
| 101 | devname = android::base::Basename(uevent.path); |
| 102 | } else if (devname_source_ == DEVNAME_SYS_NAME) { |
| 103 | if (android::base::ReadFileToString("/sys/" + uevent.path + "/name", &devname)) { |
| 104 | devname.pop_back(); // Remove terminating newline |
| 105 | } else { |
| 106 | devname = uevent.device_name; |
| 107 | } |
| 108 | } |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 109 | return dir_name_ + "/" + devname; |
| 110 | } |
| 111 | |
| 112 | bool operator==(const std::string& string_name) const { return name_ == string_name; } |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 113 | |
| 114 | private: |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 115 | std::string name_; |
Tom Cherry | 5f0198b | 2018-07-17 15:28:16 -0700 | [diff] [blame] | 116 | DevnameSource devname_source_ = DEVNAME_UEVENT_DEVNAME; |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 117 | std::string dir_name_ = "/dev"; |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
Douglas Anderson | 9481f97 | 2024-11-06 09:28:07 -0800 | [diff] [blame] | 120 | struct BlockDeviceInfo { |
| 121 | std::string str; |
| 122 | std::string type; |
| 123 | bool is_boot_device; |
| 124 | }; |
| 125 | |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 126 | class DeviceHandler : public UeventHandler { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 127 | public: |
| 128 | friend class DeviceHandlerTester; |
| 129 | |
| 130 | DeviceHandler(); |
| 131 | DeviceHandler(std::vector<Permissions> dev_permissions, |
Eric Caruso | d17d5c5 | 2024-10-28 15:30:11 -0400 | [diff] [blame] | 132 | std::vector<SysfsPermissions> sysfs_permissions, std::vector<Subsystem> drivers, |
Douglas Anderson | e9de310 | 2024-10-22 14:34:30 -0700 | [diff] [blame] | 133 | std::vector<Subsystem> subsystems, std::set<std::string> boot_devices, |
| 134 | std::string boot_part_uuid, bool skip_restorecon); |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 135 | virtual ~DeviceHandler() = default; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 136 | |
Douglas Anderson | e9de310 | 2024-10-22 14:34:30 -0700 | [diff] [blame] | 137 | bool CheckUeventForBootPartUuid(const Uevent& uevent); |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 138 | void HandleUevent(const Uevent& uevent) override; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 139 | |
Tom Cherry | 96e5f9b | 2021-07-30 09:54:36 -0700 | [diff] [blame] | 140 | // `androidboot.partition_map` allows associating a partition name for a raw block device |
| 141 | // through a comma separated and semicolon deliminated list. For example, |
| 142 | // `androidboot.partition_map=vdb,metadata;vdc,userdata` maps `vdb` to `metadata` and `vdc` to |
| 143 | // `userdata`. |
| 144 | static std::string GetPartitionNameForDevice(const std::string& device); |
Douglas Anderson | eb3d280 | 2024-11-05 09:15:25 -0800 | [diff] [blame] | 145 | bool IsBootDeviceStrict() const; |
| 146 | bool IsBootDevice(const Uevent& uevent) const; |
Tom Cherry | 96e5f9b | 2021-07-30 09:54:36 -0700 | [diff] [blame] | 147 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 148 | private: |
Eric Caruso | d17d5c5 | 2024-10-28 15:30:11 -0400 | [diff] [blame] | 149 | struct TrackedUevent { |
| 150 | Uevent uevent; |
| 151 | std::string canonical_device_path; |
| 152 | }; |
| 153 | |
Bart Van Assche | 254436b | 2024-05-23 11:14:50 -0700 | [diff] [blame] | 154 | void ColdbootDone() override; |
Douglas Anderson | 9481f97 | 2024-11-06 09:28:07 -0800 | [diff] [blame] | 155 | BlockDeviceInfo GetBlockDeviceInfo(const std::string& uevent_path) const; |
Douglas Anderson | 6519e6d | 2024-10-22 14:09:26 -0700 | [diff] [blame] | 156 | bool FindSubsystemDevice(std::string path, std::string* device_path, |
| 157 | const std::set<std::string>& subsystem_paths) const; |
Douglas Anderson | 46afe22 | 2024-11-08 14:38:41 -0800 | [diff] [blame] | 158 | bool FindPlatformDevice(const std::string& path, std::string* platform_device_path) const; |
| 159 | bool FindMmcDevice(const std::string& path, std::string* mmc_device_path) const; |
Douglas Anderson | dd8edea | 2024-11-14 13:53:54 -0800 | [diff] [blame] | 160 | bool FindNvmeDevice(const std::string& path, std::string* nvme_device_path) const; |
Douglas Anderson | 46afe22 | 2024-11-08 14:38:41 -0800 | [diff] [blame] | 161 | bool FindScsiDevice(const std::string& path, std::string* scsi_device_path) const; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 162 | std::tuple<mode_t, uid_t, gid_t> GetDevicePermissions( |
| 163 | const std::string& path, const std::vector<std::string>& links) const; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 164 | void MakeDevice(const std::string& path, bool block, int major, int minor, |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 165 | const std::vector<std::string>& links) const; |
Bart Van Assche | 254436b | 2024-05-23 11:14:50 -0700 | [diff] [blame] | 166 | std::vector<std::string> GetBlockDeviceSymlinks(const Uevent& uevent) const; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 167 | void HandleDevice(const std::string& action, const std::string& devpath, bool block, int major, |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 168 | int minor, const std::vector<std::string>& links) const; |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 169 | void FixupSysPermissions(const std::string& upath, const std::string& subsystem) const; |
Tri Vo | ff89b8d | 2019-09-24 13:00:43 -0700 | [diff] [blame] | 170 | void HandleAshmemUevent(const Uevent& uevent); |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 171 | |
Eric Caruso | d17d5c5 | 2024-10-28 15:30:11 -0400 | [diff] [blame] | 172 | void TrackDeviceUevent(const Uevent& uevent); |
| 173 | void HandleBindInternal(std::string driver_name, std::string action, const Uevent& uevent); |
| 174 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 175 | std::vector<Permissions> dev_permissions_; |
| 176 | std::vector<SysfsPermissions> sysfs_permissions_; |
Eric Caruso | d17d5c5 | 2024-10-28 15:30:11 -0400 | [diff] [blame] | 177 | std::vector<Subsystem> drivers_; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 178 | std::vector<Subsystem> subsystems_; |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 179 | std::set<std::string> boot_devices_; |
Douglas Anderson | e9de310 | 2024-10-22 14:34:30 -0700 | [diff] [blame] | 180 | std::string boot_part_uuid_; |
| 181 | bool found_boot_part_uuid_; |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 182 | bool skip_restorecon_; |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 183 | std::string sysfs_mount_point_; |
Eric Caruso | d17d5c5 | 2024-10-28 15:30:11 -0400 | [diff] [blame] | 184 | |
| 185 | std::vector<TrackedUevent> tracked_uevents_; |
| 186 | std::map<std::string, std::string> bound_drivers_; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 187 | }; |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 188 | |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 189 | // Exposed for testing |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 190 | void SanitizePartitionName(std::string* string); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 191 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 192 | } // namespace init |
| 193 | } // namespace android |
| 194 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 195 | #endif |