| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 2 | * Copyright (C) 2007 The Android Open Source Project | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 |  | 
| Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 17 | #include "devices.h" | 
|  | 18 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | #include <errno.h> | 
| Daniel Leung | c0c1ffe | 2012-07-02 11:32:30 -0700 | [diff] [blame] | 20 | #include <fnmatch.h> | 
| Elliott Hughes | 51056c4 | 2017-05-18 09:13:15 -0700 | [diff] [blame] | 21 | #include <sys/sysmacros.h> | 
| Elliott Hughes | f39f7f1 | 2016-08-31 14:41:51 -0700 | [diff] [blame] | 22 | #include <unistd.h> | 
|  | 23 |  | 
| Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 24 | #include <chrono> | 
| David Anderson | 515a5bd | 2020-10-19 19:54:18 -0700 | [diff] [blame] | 25 | #include <filesystem> | 
| James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 26 | #include <memory> | 
| Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 27 | #include <string> | 
|  | 28 | #include <thread> | 
| James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 29 |  | 
| Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 30 | #include <android-base/chrono_utils.h> | 
|  | 31 | #include <android-base/file.h> | 
| Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 32 | #include <android-base/logging.h> | 
| Rob Herring | 6de783a | 2016-05-06 10:06:59 -0500 | [diff] [blame] | 33 | #include <android-base/stringprintf.h> | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 34 | #include <android-base/strings.h> | 
| Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 35 | #include <private/android_filesystem_config.h> | 
|  | 36 | #include <selinux/android.h> | 
| Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 37 | #include <selinux/selinux.h> | 
| Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 38 |  | 
| Vic Yang | 92c236e | 2019-05-28 15:58:35 -0700 | [diff] [blame] | 39 | #include "selabel.h" | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 40 | #include "util.h" | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 41 |  | 
| Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 42 | using namespace std::chrono_literals; | 
|  | 43 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 44 | using android::base::Basename; | 
|  | 45 | using android::base::Dirname; | 
| Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 46 | using android::base::ReadFileToString; | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 47 | using android::base::Readlink; | 
|  | 48 | using android::base::Realpath; | 
| Tom Cherry | 96e5f9b | 2021-07-30 09:54:36 -0700 | [diff] [blame] | 49 | using android::base::Split; | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 50 | using android::base::StartsWith; | 
|  | 51 | using android::base::StringPrintf; | 
| Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 52 | using android::base::Trim; | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 53 |  | 
|  | 54 | namespace android { | 
|  | 55 | namespace init { | 
|  | 56 |  | 
| Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 57 | /* Given a path that may start with a PCI device, populate the supplied buffer | 
|  | 58 | * with the PCI domain/bus number and the peripheral ID and return 0. | 
|  | 59 | * If it doesn't start with a PCI device, or there is some error, return -1 */ | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 60 | static bool FindPciDevicePrefix(const std::string& path, std::string* result) { | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 61 | result->clear(); | 
| Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 62 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 63 | if (!StartsWith(path, "/devices/pci")) return false; | 
| Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 64 |  | 
|  | 65 | /* Beginning of the prefix is the initial "pci" after "/devices/" */ | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 66 | std::string::size_type start = 9; | 
| Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 67 |  | 
|  | 68 | /* End of the prefix is two path '/' later, capturing the domain/bus number | 
|  | 69 | * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */ | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 70 | auto end = path.find('/', start); | 
|  | 71 | if (end == std::string::npos) return false; | 
| Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 72 |  | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 73 | end = path.find('/', end + 1); | 
|  | 74 | if (end == std::string::npos) return false; | 
| Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 75 |  | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 76 | auto length = end - start; | 
|  | 77 | if (length <= 4) { | 
|  | 78 | // The minimum string that will get to this check is 'pci/', which is malformed, | 
|  | 79 | // so return false | 
|  | 80 | return false; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | *result = path.substr(start, length); | 
|  | 84 | return true; | 
| Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
| Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 87 | /* Given a path that may start with a virtual block device, populate | 
|  | 88 | * the supplied buffer with the virtual block device ID and return 0. | 
|  | 89 | * If it doesn't start with a virtual block device, or there is some | 
|  | 90 | * error, return -1 */ | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 91 | static bool FindVbdDevicePrefix(const std::string& path, std::string* result) { | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 92 | result->clear(); | 
|  | 93 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 94 | if (!StartsWith(path, "/devices/vbd-")) return false; | 
| Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 95 |  | 
|  | 96 | /* Beginning of the prefix is the initial "vbd-" after "/devices/" */ | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 97 | std::string::size_type start = 13; | 
| Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 98 |  | 
|  | 99 | /* End of the prefix is one path '/' later, capturing the | 
|  | 100 | virtual block device ID. Example: 768 */ | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 101 | auto end = path.find('/', start); | 
|  | 102 | if (end == std::string::npos) return false; | 
| Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 103 |  | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 104 | auto length = end - start; | 
|  | 105 | if (length == 0) return false; | 
| Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 106 |  | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 107 | *result = path.substr(start, length); | 
|  | 108 | return true; | 
| Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 109 | } | 
|  | 110 |  | 
| Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 111 | // Given a path that may start with a virtual dm block device, populate | 
|  | 112 | // the supplied buffer with the dm module's instantiated name. | 
|  | 113 | // If it doesn't start with a virtual block device, or there is some | 
|  | 114 | // error, return false. | 
| David Anderson | 924858c | 2019-06-26 17:00:00 -0700 | [diff] [blame] | 115 | static bool FindDmDevice(const std::string& path, std::string* name, std::string* uuid) { | 
| Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 116 | if (!StartsWith(path, "/devices/virtual/block/dm-")) return false; | 
| Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 117 |  | 
| David Anderson | 924858c | 2019-06-26 17:00:00 -0700 | [diff] [blame] | 118 | if (!ReadFileToString("/sys" + path + "/dm/name", name)) { | 
|  | 119 | return false; | 
| Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 120 | } | 
| David Anderson | 924858c | 2019-06-26 17:00:00 -0700 | [diff] [blame] | 121 | ReadFileToString("/sys" + path + "/dm/uuid", uuid); | 
|  | 122 |  | 
|  | 123 | *name = android::base::Trim(*name); | 
|  | 124 | *uuid = android::base::Trim(*uuid); | 
| Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 125 | return true; | 
|  | 126 | } | 
|  | 127 |  | 
| Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 128 | Permissions::Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid, | 
|  | 129 | bool no_fnm_pathname) | 
|  | 130 | : name_(name), | 
|  | 131 | perm_(perm), | 
|  | 132 | uid_(uid), | 
|  | 133 | gid_(gid), | 
|  | 134 | prefix_(false), | 
|  | 135 | wildcard_(false), | 
|  | 136 | no_fnm_pathname_(no_fnm_pathname) { | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 137 | // Set 'prefix_' or 'wildcard_' based on the below cases: | 
|  | 138 | // | 
|  | 139 | // 1) No '*' in 'name' -> Neither are set and Match() checks a given path for strict | 
|  | 140 | //    equality with 'name' | 
|  | 141 | // | 
|  | 142 | // 2) '*' only appears as the last character in 'name' -> 'prefix'_ is set to true and | 
|  | 143 | //    Match() checks if 'name' is a prefix of a given path. | 
|  | 144 | // | 
|  | 145 | // 3) '*' appears elsewhere -> 'wildcard_' is set to true and Match() uses fnmatch() | 
|  | 146 | //    with FNM_PATHNAME to compare 'name' to a given path. | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 147 | auto wildcard_position = name_.find('*'); | 
|  | 148 | if (wildcard_position != std::string::npos) { | 
|  | 149 | if (wildcard_position == name_.length() - 1) { | 
|  | 150 | prefix_ = true; | 
|  | 151 | name_.pop_back(); | 
|  | 152 | } else { | 
|  | 153 | wildcard_ = true; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 154 | } | 
| Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 155 | } | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 158 | bool Permissions::Match(const std::string& path) const { | 
| Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 159 | if (prefix_) return StartsWith(path, name_); | 
| Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 160 | if (wildcard_) | 
|  | 161 | return fnmatch(name_.c_str(), path.c_str(), no_fnm_pathname_ ? 0 : FNM_PATHNAME) == 0; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 162 | return path == name_; | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | bool SysfsPermissions::MatchWithSubsystem(const std::string& path, | 
|  | 166 | const std::string& subsystem) const { | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 167 | std::string path_basename = Basename(path); | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 168 | if (name().find(subsystem) != std::string::npos) { | 
|  | 169 | if (Match("/sys/class/" + subsystem + "/" + path_basename)) return true; | 
|  | 170 | if (Match("/sys/bus/" + subsystem + "/devices/" + path_basename)) return true; | 
|  | 171 | } | 
|  | 172 | return Match(path); | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | void SysfsPermissions::SetPermissions(const std::string& path) const { | 
|  | 176 | std::string attribute_file = path + "/" + attribute_; | 
|  | 177 | LOG(VERBOSE) << "fixup " << attribute_file << " " << uid() << " " << gid() << " " << std::oct | 
|  | 178 | << perm(); | 
|  | 179 |  | 
|  | 180 | if (access(attribute_file.c_str(), F_OK) == 0) { | 
|  | 181 | if (chown(attribute_file.c_str(), uid(), gid()) != 0) { | 
|  | 182 | PLOG(ERROR) << "chown(" << attribute_file << ", " << uid() << ", " << gid() | 
|  | 183 | << ") failed"; | 
|  | 184 | } | 
|  | 185 | if (chmod(attribute_file.c_str(), perm()) != 0) { | 
|  | 186 | PLOG(ERROR) << "chmod(" << attribute_file << ", " << perm() << ") failed"; | 
|  | 187 | } | 
|  | 188 | } | 
|  | 189 | } | 
|  | 190 |  | 
| Tom Cherry | 96e5f9b | 2021-07-30 09:54:36 -0700 | [diff] [blame] | 191 | std::string DeviceHandler::GetPartitionNameForDevice(const std::string& query_device) { | 
|  | 192 | static const auto partition_map = [] { | 
|  | 193 | std::vector<std::pair<std::string, std::string>> partition_map; | 
|  | 194 | auto parser = [&partition_map](const std::string& key, const std::string& value) { | 
|  | 195 | if (key != "androidboot.partition_map") { | 
|  | 196 | return; | 
|  | 197 | } | 
|  | 198 | for (const auto& map : Split(value, ";")) { | 
|  | 199 | auto map_pieces = Split(map, ","); | 
|  | 200 | if (map_pieces.size() != 2) { | 
|  | 201 | LOG(ERROR) << "Expected a comma separated device,partition mapping, but found '" | 
|  | 202 | << map << "'"; | 
|  | 203 | continue; | 
|  | 204 | } | 
|  | 205 | partition_map.emplace_back(map_pieces[0], map_pieces[1]); | 
|  | 206 | } | 
|  | 207 | }; | 
|  | 208 | ImportKernelCmdline(parser); | 
|  | 209 | ImportBootconfig(parser); | 
|  | 210 | return partition_map; | 
|  | 211 | }(); | 
|  | 212 |  | 
|  | 213 | for (const auto& [device, partition] : partition_map) { | 
|  | 214 | if (query_device == device) { | 
|  | 215 | return partition; | 
|  | 216 | } | 
|  | 217 | } | 
|  | 218 | return {}; | 
|  | 219 | } | 
|  | 220 |  | 
| Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 221 | // Given a path that may start with a platform device, find the parent platform device by finding a | 
|  | 222 | // parent directory with a 'subsystem' symlink that points to the platform bus. | 
|  | 223 | // If it doesn't start with a platform device, return false | 
|  | 224 | bool DeviceHandler::FindPlatformDevice(std::string path, std::string* platform_device_path) const { | 
|  | 225 | platform_device_path->clear(); | 
|  | 226 |  | 
|  | 227 | // Uevents don't contain the mount point, so we need to add it here. | 
|  | 228 | path.insert(0, sysfs_mount_point_); | 
|  | 229 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 230 | std::string directory = Dirname(path); | 
| Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 231 |  | 
|  | 232 | while (directory != "/" && directory != ".") { | 
|  | 233 | std::string subsystem_link_path; | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 234 | if (Realpath(directory + "/subsystem", &subsystem_link_path) && | 
| Usama Arif | d86300c | 2020-06-12 13:45:11 +0100 | [diff] [blame] | 235 | (subsystem_link_path == sysfs_mount_point_ + "/bus/platform" || | 
|  | 236 | subsystem_link_path == sysfs_mount_point_ + "/bus/amba")) { | 
| Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 237 | // We need to remove the mount point that we added above before returning. | 
|  | 238 | directory.erase(0, sysfs_mount_point_.size()); | 
|  | 239 | *platform_device_path = directory; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 240 | return true; | 
|  | 241 | } | 
| Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 242 |  | 
|  | 243 | auto last_slash = path.rfind('/'); | 
|  | 244 | if (last_slash == std::string::npos) return false; | 
|  | 245 |  | 
|  | 246 | path.erase(last_slash); | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 247 | directory = Dirname(path); | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 248 | } | 
| Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 249 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 250 | return false; | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | void DeviceHandler::FixupSysPermissions(const std::string& upath, | 
|  | 254 | const std::string& subsystem) const { | 
|  | 255 | // upaths omit the "/sys" that paths in this list | 
|  | 256 | // contain, so we prepend it... | 
|  | 257 | std::string path = "/sys" + upath; | 
|  | 258 |  | 
|  | 259 | for (const auto& s : sysfs_permissions_) { | 
|  | 260 | if (s.MatchWithSubsystem(path, subsystem)) s.SetPermissions(path); | 
|  | 261 | } | 
|  | 262 |  | 
| Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 263 | if (!skip_restorecon_ && access(path.c_str(), F_OK) == 0) { | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 264 | LOG(VERBOSE) << "restorecon_recursive: " << path; | 
|  | 265 | if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { | 
|  | 266 | PLOG(ERROR) << "selinux_android_restorecon(" << path << ") failed"; | 
|  | 267 | } | 
|  | 268 | } | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | std::tuple<mode_t, uid_t, gid_t> DeviceHandler::GetDevicePermissions( | 
|  | 272 | const std::string& path, const std::vector<std::string>& links) const { | 
|  | 273 | // Search the perms list in reverse so that ueventd.$hardware can override ueventd.rc. | 
|  | 274 | for (auto it = dev_permissions_.crbegin(); it != dev_permissions_.crend(); ++it) { | 
|  | 275 | if (it->Match(path) || std::any_of(links.cbegin(), links.cend(), | 
|  | 276 | [it](const auto& link) { return it->Match(link); })) { | 
|  | 277 | return {it->perm(), it->uid(), it->gid()}; | 
|  | 278 | } | 
|  | 279 | } | 
|  | 280 | /* Default if nothing found. */ | 
|  | 281 | return {0600, 0, 0}; | 
|  | 282 | } | 
|  | 283 |  | 
| Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 284 | void DeviceHandler::MakeDevice(const std::string& path, bool block, int major, int minor, | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 285 | const std::vector<std::string>& links) const { | 
|  | 286 | auto[mode, uid, gid] = GetDevicePermissions(path, links); | 
|  | 287 | mode |= (block ? S_IFBLK : S_IFCHR); | 
|  | 288 |  | 
| Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 289 | std::string secontext; | 
|  | 290 | if (!SelabelLookupFileContextBestMatch(path, links, mode, &secontext)) { | 
|  | 291 | PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label"; | 
|  | 292 | return; | 
|  | 293 | } | 
|  | 294 | if (!secontext.empty()) { | 
|  | 295 | setfscreatecon(secontext.c_str()); | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 296 | } | 
|  | 297 |  | 
| David Anderson | f8825fa | 2021-07-02 20:47:27 -0700 | [diff] [blame] | 298 | gid_t new_group = -1; | 
|  | 299 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 300 | dev_t dev = makedev(major, minor); | 
|  | 301 | /* Temporarily change egid to avoid race condition setting the gid of the | 
|  | 302 | * device node. Unforunately changing the euid would prevent creation of | 
|  | 303 | * some device nodes, so the uid has to be set with chown() and is still | 
|  | 304 | * racy. Fixing the gid race at least fixed the issue with system_server | 
|  | 305 | * opening dynamic input devices under the AID_INPUT gid. */ | 
|  | 306 | if (setegid(gid)) { | 
|  | 307 | PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed"; | 
|  | 308 | goto out; | 
|  | 309 | } | 
| zexin.hou | 76cba8a | 2022-04-14 16:08:17 +0800 | [diff] [blame] | 310 | /* If the node already exists update its SELinux label and the file mode to handle cases when | 
|  | 311 | * it was created with the wrong context and file mode during coldboot procedure. */ | 
| Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 312 | if (mknod(path.c_str(), mode, dev) && (errno == EEXIST) && !secontext.empty()) { | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 313 | char* fcon = nullptr; | 
|  | 314 | int rc = lgetfilecon(path.c_str(), &fcon); | 
|  | 315 | if (rc < 0) { | 
|  | 316 | PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device"; | 
|  | 317 | goto out; | 
|  | 318 | } | 
|  | 319 |  | 
| Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 320 | bool different = fcon != secontext; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 321 | freecon(fcon); | 
|  | 322 |  | 
| Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 323 | if (different && lsetfilecon(path.c_str(), secontext.c_str())) { | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 324 | PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path | 
|  | 325 | << "' device"; | 
|  | 326 | } | 
| David Anderson | f8825fa | 2021-07-02 20:47:27 -0700 | [diff] [blame] | 327 |  | 
|  | 328 | struct stat s; | 
|  | 329 | if (stat(path.c_str(), &s) == 0) { | 
|  | 330 | if (gid != s.st_gid) { | 
|  | 331 | new_group = gid; | 
|  | 332 | } | 
| zexin.hou | 76cba8a | 2022-04-14 16:08:17 +0800 | [diff] [blame] | 333 | if (mode != s.st_mode) { | 
|  | 334 | if (chmod(path.c_str(), mode) != 0) { | 
|  | 335 | PLOG(ERROR) << "Cannot chmod " << path << " to " << mode; | 
|  | 336 | } | 
|  | 337 | } | 
| David Anderson | f8825fa | 2021-07-02 20:47:27 -0700 | [diff] [blame] | 338 | } else { | 
|  | 339 | PLOG(ERROR) << "Cannot stat " << path; | 
|  | 340 | } | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 341 | } | 
|  | 342 |  | 
|  | 343 | out: | 
| David Anderson | f8825fa | 2021-07-02 20:47:27 -0700 | [diff] [blame] | 344 | if (chown(path.c_str(), uid, new_group) < 0) { | 
|  | 345 | PLOG(ERROR) << "Cannot chown " << path << " " << uid << " " << new_group; | 
|  | 346 | } | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 347 | if (setegid(AID_ROOT)) { | 
|  | 348 | PLOG(FATAL) << "setegid(AID_ROOT) failed"; | 
|  | 349 | } | 
|  | 350 |  | 
| Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 351 | if (!secontext.empty()) { | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 352 | setfscreatecon(nullptr); | 
|  | 353 | } | 
|  | 354 | } | 
|  | 355 |  | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 356 | // replaces any unacceptable characters with '_', the | 
|  | 357 | // length of the resulting string is equal to the input string | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 358 | void SanitizePartitionName(std::string* string) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 359 | const char* accept = | 
|  | 360 | "abcdefghijklmnopqrstuvwxyz" | 
|  | 361 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 
|  | 362 | "0123456789" | 
|  | 363 | "_-."; | 
|  | 364 |  | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 365 | if (!string) return; | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 366 |  | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 367 | std::string::size_type pos = 0; | 
|  | 368 | while ((pos = string->find_first_not_of(accept, pos)) != std::string::npos) { | 
|  | 369 | (*string)[pos] = '_'; | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 370 | } | 
|  | 371 | } | 
|  | 372 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 373 | std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uevent) const { | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 374 | std::string device; | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 375 | std::string type; | 
| Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 376 | std::string partition; | 
| David Anderson | 924858c | 2019-06-26 17:00:00 -0700 | [diff] [blame] | 377 | std::string uuid; | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 378 |  | 
| Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 379 | if (FindPlatformDevice(uevent.path, &device)) { | 
| Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame] | 380 | // Skip /devices/platform or /devices/ if present | 
|  | 381 | static const std::string devices_platform_prefix = "/devices/platform/"; | 
|  | 382 | static const std::string devices_prefix = "/devices/"; | 
|  | 383 |  | 
| Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 384 | if (StartsWith(device, devices_platform_prefix)) { | 
| Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame] | 385 | device = device.substr(devices_platform_prefix.length()); | 
| Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 386 | } else if (StartsWith(device, devices_prefix)) { | 
| Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame] | 387 | device = device.substr(devices_prefix.length()); | 
|  | 388 | } | 
|  | 389 |  | 
| Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 390 | type = "platform"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 391 | } else if (FindPciDevicePrefix(uevent.path, &device)) { | 
| Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 392 | type = "pci"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 393 | } else if (FindVbdDevicePrefix(uevent.path, &device)) { | 
| Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 394 | type = "vbd"; | 
| David Anderson | 924858c | 2019-06-26 17:00:00 -0700 | [diff] [blame] | 395 | } else if (FindDmDevice(uevent.path, &partition, &uuid)) { | 
|  | 396 | std::vector<std::string> symlinks = {"/dev/block/mapper/" + partition}; | 
|  | 397 | if (!uuid.empty()) { | 
|  | 398 | symlinks.emplace_back("/dev/block/mapper/by-uuid/" + uuid); | 
|  | 399 | } | 
|  | 400 | return symlinks; | 
| Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 401 | } else { | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 402 | return {}; | 
| Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 403 | } | 
| Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 404 |  | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 405 | std::vector<std::string> links; | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 406 |  | 
| Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 407 | LOG(VERBOSE) << "found " << type << " device " << device; | 
| Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 408 |  | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 409 | auto link_path = "/dev/block/" + type + "/" + device; | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 410 |  | 
| Tom Cherry | 2c56d7c | 2018-12-20 14:36:49 -0800 | [diff] [blame] | 411 | bool is_boot_device = boot_devices_.find(device) != boot_devices_.end(); | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 412 | if (!uevent.partition_name.empty()) { | 
|  | 413 | std::string partition_name_sanitized(uevent.partition_name); | 
|  | 414 | SanitizePartitionName(&partition_name_sanitized); | 
|  | 415 | if (partition_name_sanitized != uevent.partition_name) { | 
|  | 416 | LOG(VERBOSE) << "Linking partition '" << uevent.partition_name << "' as '" | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 417 | << partition_name_sanitized << "'"; | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 418 | } | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 419 | links.emplace_back(link_path + "/by-name/" + partition_name_sanitized); | 
| Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 420 | // Adds symlink: /dev/block/by-name/<partition_name>. | 
| Tom Cherry | 2c56d7c | 2018-12-20 14:36:49 -0800 | [diff] [blame] | 421 | if (is_boot_device) { | 
| Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 422 | links.emplace_back("/dev/block/by-name/" + partition_name_sanitized); | 
|  | 423 | } | 
| Tom Cherry | 2c56d7c | 2018-12-20 14:36:49 -0800 | [diff] [blame] | 424 | } else if (is_boot_device) { | 
|  | 425 | // If we don't have a partition name but we are a partition on a boot device, create a | 
|  | 426 | // symlink of /dev/block/by-name/<device_name> for symmetry. | 
|  | 427 | links.emplace_back("/dev/block/by-name/" + uevent.device_name); | 
| Tom Cherry | 96e5f9b | 2021-07-30 09:54:36 -0700 | [diff] [blame] | 428 | auto partition_name = GetPartitionNameForDevice(uevent.device_name); | 
|  | 429 | if (!partition_name.empty()) { | 
|  | 430 | links.emplace_back("/dev/block/by-name/" + partition_name); | 
|  | 431 | } | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 432 | } | 
|  | 433 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 434 | auto last_slash = uevent.path.rfind('/'); | 
|  | 435 | links.emplace_back(link_path + "/" + uevent.path.substr(last_slash + 1)); | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 436 |  | 
|  | 437 | return links; | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 438 | } | 
|  | 439 |  | 
| David Anderson | 924858c | 2019-06-26 17:00:00 -0700 | [diff] [blame] | 440 | static void RemoveDeviceMapperLinks(const std::string& devpath) { | 
|  | 441 | std::vector<std::string> dirs = { | 
|  | 442 | "/dev/block/mapper", | 
|  | 443 | "/dev/block/mapper/by-uuid", | 
|  | 444 | }; | 
|  | 445 | for (const auto& dir : dirs) { | 
|  | 446 | if (access(dir.c_str(), F_OK) != 0) continue; | 
|  | 447 |  | 
|  | 448 | std::unique_ptr<DIR, decltype(&closedir)> dh(opendir(dir.c_str()), closedir); | 
|  | 449 | if (!dh) { | 
|  | 450 | PLOG(ERROR) << "Failed to open directory " << dir; | 
|  | 451 | continue; | 
|  | 452 | } | 
|  | 453 |  | 
|  | 454 | struct dirent* dp; | 
|  | 455 | std::string link_path; | 
|  | 456 | while ((dp = readdir(dh.get())) != nullptr) { | 
|  | 457 | if (dp->d_type != DT_LNK) continue; | 
|  | 458 |  | 
|  | 459 | auto path = dir + "/" + dp->d_name; | 
|  | 460 | if (Readlink(path, &link_path) && link_path == devpath) { | 
|  | 461 | unlink(path.c_str()); | 
|  | 462 | } | 
|  | 463 | } | 
|  | 464 | } | 
|  | 465 | } | 
|  | 466 |  | 
| Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 467 | void DeviceHandler::HandleDevice(const std::string& action, const std::string& devpath, bool block, | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 468 | int major, int minor, const std::vector<std::string>& links) const { | 
| Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 469 | if (action == "add") { | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 470 | MakeDevice(devpath, block, major, minor, links); | 
| David Anderson | 924858c | 2019-06-26 17:00:00 -0700 | [diff] [blame] | 471 | } | 
|  | 472 |  | 
|  | 473 | // We don't have full device-mapper information until a change event is fired. | 
|  | 474 | if (action == "add" || (action == "change" && StartsWith(devpath, "/dev/block/dm-"))) { | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 475 | for (const auto& link : links) { | 
| Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 476 | if (!mkdir_recursive(Dirname(link), 0755)) { | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 477 | PLOG(ERROR) << "Failed to create directory " << Dirname(link); | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 478 | } | 
|  | 479 |  | 
| Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 480 | if (symlink(devpath.c_str(), link.c_str())) { | 
|  | 481 | if (errno != EEXIST) { | 
|  | 482 | PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link; | 
|  | 483 | } else if (std::string link_path; | 
|  | 484 | Readlink(link, &link_path) && link_path != devpath) { | 
|  | 485 | PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link | 
|  | 486 | << ", which already links to: " << link_path; | 
|  | 487 | } | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 488 | } | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 489 | } | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 490 | } | 
|  | 491 |  | 
| Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 492 | if (action == "remove") { | 
| David Anderson | 924858c | 2019-06-26 17:00:00 -0700 | [diff] [blame] | 493 | if (StartsWith(devpath, "/dev/block/dm-")) { | 
|  | 494 | RemoveDeviceMapperLinks(devpath); | 
|  | 495 | } | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 496 | for (const auto& link : links) { | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 497 | std::string link_path; | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 498 | if (Readlink(link, &link_path) && link_path == devpath) { | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 499 | unlink(link.c_str()); | 
|  | 500 | } | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 501 | } | 
| Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 502 | unlink(devpath.c_str()); | 
| Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 503 | } | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 504 | } | 
|  | 505 |  | 
| Tri Vo | ff89b8d | 2019-09-24 13:00:43 -0700 | [diff] [blame] | 506 | void DeviceHandler::HandleAshmemUevent(const Uevent& uevent) { | 
|  | 507 | if (uevent.device_name == "ashmem") { | 
|  | 508 | static const std::string boot_id_path = "/proc/sys/kernel/random/boot_id"; | 
|  | 509 | std::string boot_id; | 
|  | 510 | if (!ReadFileToString(boot_id_path, &boot_id)) { | 
|  | 511 | PLOG(ERROR) << "Cannot duplicate ashmem device node. Failed to read " << boot_id_path; | 
|  | 512 | return; | 
|  | 513 | }; | 
|  | 514 | boot_id = Trim(boot_id); | 
|  | 515 |  | 
|  | 516 | Uevent dup_ashmem_uevent = uevent; | 
|  | 517 | dup_ashmem_uevent.device_name += boot_id; | 
|  | 518 | dup_ashmem_uevent.path += boot_id; | 
|  | 519 | HandleUevent(dup_ashmem_uevent); | 
|  | 520 | } | 
|  | 521 | } | 
|  | 522 |  | 
| Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 523 | void DeviceHandler::HandleUevent(const Uevent& uevent) { | 
| Sriharsha Allenki | e609478 | 2020-11-10 11:44:59 +0530 | [diff] [blame] | 524 | if (uevent.action == "add" || uevent.action == "change" || | 
|  | 525 | uevent.action == "bind" || uevent.action == "online") { | 
|  | 526 | FixupSysPermissions(uevent.path, uevent.subsystem); | 
|  | 527 | } | 
| Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 528 |  | 
| Tom Cherry | 3fa4673 | 2017-04-11 14:19:50 -0700 | [diff] [blame] | 529 | // if it's not a /dev device, nothing to do | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 530 | if (uevent.major < 0 || uevent.minor < 0) return; | 
| Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 531 |  | 
| Tom Cherry | 3fa4673 | 2017-04-11 14:19:50 -0700 | [diff] [blame] | 532 | std::string devpath; | 
| Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 533 | std::vector<std::string> links; | 
|  | 534 | bool block = false; | 
| Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 535 |  | 
| Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 536 | if (uevent.subsystem == "block") { | 
|  | 537 | block = true; | 
|  | 538 | devpath = "/dev/block/" + Basename(uevent.path); | 
|  | 539 |  | 
|  | 540 | if (StartsWith(uevent.path, "/devices")) { | 
|  | 541 | links = GetBlockDeviceSymlinks(uevent); | 
|  | 542 | } | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 543 | } else if (const auto subsystem = | 
|  | 544 | std::find(subsystems_.cbegin(), subsystems_.cend(), uevent.subsystem); | 
|  | 545 | subsystem != subsystems_.cend()) { | 
| Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 546 | devpath = subsystem->ParseDevPath(uevent); | 
| Tom Cherry | 9c8d6dd | 2017-08-17 09:38:01 -0700 | [diff] [blame] | 547 | } else if (uevent.subsystem == "usb") { | 
|  | 548 | if (!uevent.device_name.empty()) { | 
|  | 549 | devpath = "/dev/" + uevent.device_name; | 
|  | 550 | } else { | 
|  | 551 | // This imitates the file system that would be created | 
|  | 552 | // if we were using devfs instead. | 
|  | 553 | // Minors are broken up into groups of 128, starting at "001" | 
|  | 554 | int bus_id = uevent.minor / 128 + 1; | 
|  | 555 | int device_id = uevent.minor % 128 + 1; | 
|  | 556 | devpath = StringPrintf("/dev/bus/usb/%03d/%03d", bus_id, device_id); | 
|  | 557 | } | 
|  | 558 | } else if (StartsWith(uevent.subsystem, "usb")) { | 
|  | 559 | // ignore other USB events | 
|  | 560 | return; | 
| David Anderson | 515a5bd | 2020-10-19 19:54:18 -0700 | [diff] [blame] | 561 | } else if (uevent.subsystem == "misc" && StartsWith(uevent.device_name, "dm-user/")) { | 
|  | 562 | devpath = "/dev/dm-user/" + uevent.device_name.substr(8); | 
| Tom Cherry | 780a71e | 2017-04-04 16:30:40 -0700 | [diff] [blame] | 563 | } else { | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 564 | devpath = "/dev/" + Basename(uevent.path); | 
| Tom Cherry | 780a71e | 2017-04-04 16:30:40 -0700 | [diff] [blame] | 565 | } | 
| Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 566 |  | 
| Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 567 | mkdir_recursive(Dirname(devpath), 0755); | 
| Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 568 |  | 
| Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 569 | HandleDevice(uevent.action, devpath, block, uevent.major, uevent.minor, links); | 
| Tri Vo | ff89b8d | 2019-09-24 13:00:43 -0700 | [diff] [blame] | 570 |  | 
|  | 571 | // Duplicate /dev/ashmem device and name it /dev/ashmem<boot_id>. | 
|  | 572 | // TODO(b/111903542): remove once all users of /dev/ashmem are migrated to libcutils API. | 
|  | 573 | HandleAshmemUevent(uevent); | 
| Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 574 | } | 
|  | 575 |  | 
| Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 576 | void DeviceHandler::ColdbootDone() { | 
| Oleksiy Avramchenko | dd5802a | 2018-11-02 10:06:47 +0100 | [diff] [blame] | 577 | skip_restorecon_ = false; | 
| Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 578 | } | 
|  | 579 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 580 | DeviceHandler::DeviceHandler(std::vector<Permissions> dev_permissions, | 
|  | 581 | std::vector<SysfsPermissions> sysfs_permissions, | 
| Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 582 | std::vector<Subsystem> subsystems, std::set<std::string> boot_devices, | 
|  | 583 | bool skip_restorecon) | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 584 | : dev_permissions_(std::move(dev_permissions)), | 
|  | 585 | sysfs_permissions_(std::move(sysfs_permissions)), | 
|  | 586 | subsystems_(std::move(subsystems)), | 
| Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 587 | boot_devices_(std::move(boot_devices)), | 
| Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 588 | skip_restorecon_(skip_restorecon), | 
|  | 589 | sysfs_mount_point_("/sys") {} | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 590 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 591 | DeviceHandler::DeviceHandler() | 
|  | 592 | : DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{}, | 
| Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 593 | std::vector<Subsystem>{}, std::set<std::string>{}, false) {} | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 594 |  | 
|  | 595 | }  // namespace init | 
|  | 596 | }  // namespace android |