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 | |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 24 | #include <memory> |
| 25 | |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 26 | #include <android-base/logging.h> |
Rob Herring | 6de783a | 2016-05-06 10:06:59 -0500 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 28 | #include <android-base/strings.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 29 | #include <private/android_filesystem_config.h> |
| 30 | #include <selinux/android.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 31 | #include <selinux/selinux.h> |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 32 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 33 | #include "selinux.h" |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 34 | #include "util.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | |
Tom Cherry | e7656b7 | 2017-05-01 17:10:09 -0700 | [diff] [blame] | 36 | #ifdef _INIT_INIT_H |
| 37 | #error "Do not include init.h in files used by ueventd or watchdogd; it will expose init's globals" |
| 38 | #endif |
| 39 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 40 | using android::base::Basename; |
| 41 | using android::base::Dirname; |
| 42 | using android::base::Readlink; |
| 43 | using android::base::Realpath; |
| 44 | using android::base::StartsWith; |
| 45 | using android::base::StringPrintf; |
| 46 | |
| 47 | namespace android { |
| 48 | namespace init { |
| 49 | |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 50 | /* Given a path that may start with a PCI device, populate the supplied buffer |
| 51 | * with the PCI domain/bus number and the peripheral ID and return 0. |
| 52 | * 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] | 53 | static bool FindPciDevicePrefix(const std::string& path, std::string* result) { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 54 | result->clear(); |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 55 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 56 | if (!StartsWith(path, "/devices/pci")) return false; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 57 | |
| 58 | /* Beginning of the prefix is the initial "pci" after "/devices/" */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 59 | std::string::size_type start = 9; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 60 | |
| 61 | /* End of the prefix is two path '/' later, capturing the domain/bus number |
| 62 | * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 63 | auto end = path.find('/', start); |
| 64 | if (end == std::string::npos) return false; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 65 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 66 | end = path.find('/', end + 1); |
| 67 | if (end == std::string::npos) return false; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 68 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 69 | auto length = end - start; |
| 70 | if (length <= 4) { |
| 71 | // The minimum string that will get to this check is 'pci/', which is malformed, |
| 72 | // so return false |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | *result = path.substr(start, length); |
| 77 | return true; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 80 | /* Given a path that may start with a virtual block device, populate |
| 81 | * the supplied buffer with the virtual block device ID and return 0. |
| 82 | * If it doesn't start with a virtual block device, or there is some |
| 83 | * error, return -1 */ |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 84 | static bool FindVbdDevicePrefix(const std::string& path, std::string* result) { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 85 | result->clear(); |
| 86 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 87 | if (!StartsWith(path, "/devices/vbd-")) return false; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 88 | |
| 89 | /* Beginning of the prefix is the initial "vbd-" after "/devices/" */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 90 | std::string::size_type start = 13; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 91 | |
| 92 | /* End of the prefix is one path '/' later, capturing the |
| 93 | virtual block device ID. Example: 768 */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 94 | auto end = path.find('/', start); |
| 95 | if (end == std::string::npos) return false; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 96 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 97 | auto length = end - start; |
| 98 | if (length == 0) return false; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 99 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 100 | *result = path.substr(start, length); |
| 101 | return true; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 104 | Permissions::Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid) |
| 105 | : name_(name), perm_(perm), uid_(uid), gid_(gid), prefix_(false), wildcard_(false) { |
| 106 | // Set 'prefix_' or 'wildcard_' based on the below cases: |
| 107 | // |
| 108 | // 1) No '*' in 'name' -> Neither are set and Match() checks a given path for strict |
| 109 | // equality with 'name' |
| 110 | // |
| 111 | // 2) '*' only appears as the last character in 'name' -> 'prefix'_ is set to true and |
| 112 | // Match() checks if 'name' is a prefix of a given path. |
| 113 | // |
| 114 | // 3) '*' appears elsewhere -> 'wildcard_' is set to true and Match() uses fnmatch() |
| 115 | // with FNM_PATHNAME to compare 'name' to a given path. |
| 116 | |
| 117 | auto wildcard_position = name_.find('*'); |
| 118 | if (wildcard_position != std::string::npos) { |
| 119 | if (wildcard_position == name_.length() - 1) { |
| 120 | prefix_ = true; |
| 121 | name_.pop_back(); |
| 122 | } else { |
| 123 | wildcard_ = true; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 124 | } |
Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 125 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 128 | bool Permissions::Match(const std::string& path) const { |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 129 | if (prefix_) return StartsWith(path, name_); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 130 | if (wildcard_) return fnmatch(name_.c_str(), path.c_str(), FNM_PATHNAME) == 0; |
| 131 | return path == name_; |
| 132 | } |
| 133 | |
| 134 | bool SysfsPermissions::MatchWithSubsystem(const std::string& path, |
| 135 | const std::string& subsystem) const { |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 136 | std::string path_basename = Basename(path); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 137 | if (name().find(subsystem) != std::string::npos) { |
| 138 | if (Match("/sys/class/" + subsystem + "/" + path_basename)) return true; |
| 139 | if (Match("/sys/bus/" + subsystem + "/devices/" + path_basename)) return true; |
| 140 | } |
| 141 | return Match(path); |
| 142 | } |
| 143 | |
| 144 | void SysfsPermissions::SetPermissions(const std::string& path) const { |
| 145 | std::string attribute_file = path + "/" + attribute_; |
| 146 | LOG(VERBOSE) << "fixup " << attribute_file << " " << uid() << " " << gid() << " " << std::oct |
| 147 | << perm(); |
| 148 | |
| 149 | if (access(attribute_file.c_str(), F_OK) == 0) { |
| 150 | if (chown(attribute_file.c_str(), uid(), gid()) != 0) { |
| 151 | PLOG(ERROR) << "chown(" << attribute_file << ", " << uid() << ", " << gid() |
| 152 | << ") failed"; |
| 153 | } |
| 154 | if (chmod(attribute_file.c_str(), perm()) != 0) { |
| 155 | PLOG(ERROR) << "chmod(" << attribute_file << ", " << perm() << ") failed"; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 160 | // Given a path that may start with a platform device, find the parent platform device by finding a |
| 161 | // parent directory with a 'subsystem' symlink that points to the platform bus. |
| 162 | // If it doesn't start with a platform device, return false |
| 163 | bool DeviceHandler::FindPlatformDevice(std::string path, std::string* platform_device_path) const { |
| 164 | platform_device_path->clear(); |
| 165 | |
| 166 | // Uevents don't contain the mount point, so we need to add it here. |
| 167 | path.insert(0, sysfs_mount_point_); |
| 168 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 169 | std::string directory = Dirname(path); |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 170 | |
| 171 | while (directory != "/" && directory != ".") { |
| 172 | std::string subsystem_link_path; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 173 | if (Realpath(directory + "/subsystem", &subsystem_link_path) && |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 174 | subsystem_link_path == sysfs_mount_point_ + "/bus/platform") { |
| 175 | // We need to remove the mount point that we added above before returning. |
| 176 | directory.erase(0, sysfs_mount_point_.size()); |
| 177 | *platform_device_path = directory; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 178 | return true; |
| 179 | } |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 180 | |
| 181 | auto last_slash = path.rfind('/'); |
| 182 | if (last_slash == std::string::npos) return false; |
| 183 | |
| 184 | path.erase(last_slash); |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 185 | directory = Dirname(path); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 186 | } |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 187 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 188 | return false; |
| 189 | } |
| 190 | |
| 191 | void DeviceHandler::FixupSysPermissions(const std::string& upath, |
| 192 | const std::string& subsystem) const { |
| 193 | // upaths omit the "/sys" that paths in this list |
| 194 | // contain, so we prepend it... |
| 195 | std::string path = "/sys" + upath; |
| 196 | |
| 197 | for (const auto& s : sysfs_permissions_) { |
| 198 | if (s.MatchWithSubsystem(path, subsystem)) s.SetPermissions(path); |
| 199 | } |
| 200 | |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 201 | if (!skip_restorecon_ && access(path.c_str(), F_OK) == 0) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 202 | LOG(VERBOSE) << "restorecon_recursive: " << path; |
| 203 | if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { |
| 204 | PLOG(ERROR) << "selinux_android_restorecon(" << path << ") failed"; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | std::tuple<mode_t, uid_t, gid_t> DeviceHandler::GetDevicePermissions( |
| 210 | const std::string& path, const std::vector<std::string>& links) const { |
| 211 | // Search the perms list in reverse so that ueventd.$hardware can override ueventd.rc. |
| 212 | for (auto it = dev_permissions_.crbegin(); it != dev_permissions_.crend(); ++it) { |
| 213 | if (it->Match(path) || std::any_of(links.cbegin(), links.cend(), |
| 214 | [it](const auto& link) { return it->Match(link); })) { |
| 215 | return {it->perm(), it->uid(), it->gid()}; |
| 216 | } |
| 217 | } |
| 218 | /* Default if nothing found. */ |
| 219 | return {0600, 0, 0}; |
| 220 | } |
| 221 | |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 222 | 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] | 223 | const std::vector<std::string>& links) const { |
| 224 | auto[mode, uid, gid] = GetDevicePermissions(path, links); |
| 225 | mode |= (block ? S_IFBLK : S_IFCHR); |
| 226 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 227 | std::string secontext; |
| 228 | if (!SelabelLookupFileContextBestMatch(path, links, mode, &secontext)) { |
| 229 | PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label"; |
| 230 | return; |
| 231 | } |
| 232 | if (!secontext.empty()) { |
| 233 | setfscreatecon(secontext.c_str()); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | dev_t dev = makedev(major, minor); |
| 237 | /* Temporarily change egid to avoid race condition setting the gid of the |
| 238 | * device node. Unforunately changing the euid would prevent creation of |
| 239 | * some device nodes, so the uid has to be set with chown() and is still |
| 240 | * racy. Fixing the gid race at least fixed the issue with system_server |
| 241 | * opening dynamic input devices under the AID_INPUT gid. */ |
| 242 | if (setegid(gid)) { |
| 243 | PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed"; |
| 244 | goto out; |
| 245 | } |
| 246 | /* If the node already exists update its SELinux label to handle cases when |
| 247 | * it was created with the wrong context during coldboot procedure. */ |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 248 | if (mknod(path.c_str(), mode, dev) && (errno == EEXIST) && !secontext.empty()) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 249 | char* fcon = nullptr; |
| 250 | int rc = lgetfilecon(path.c_str(), &fcon); |
| 251 | if (rc < 0) { |
| 252 | PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device"; |
| 253 | goto out; |
| 254 | } |
| 255 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 256 | bool different = fcon != secontext; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 257 | freecon(fcon); |
| 258 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 259 | if (different && lsetfilecon(path.c_str(), secontext.c_str())) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 260 | PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path |
| 261 | << "' device"; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | out: |
| 266 | chown(path.c_str(), uid, -1); |
| 267 | if (setegid(AID_ROOT)) { |
| 268 | PLOG(FATAL) << "setegid(AID_ROOT) failed"; |
| 269 | } |
| 270 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 271 | if (!secontext.empty()) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 272 | setfscreatecon(nullptr); |
| 273 | } |
| 274 | } |
| 275 | |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 276 | // replaces any unacceptable characters with '_', the |
| 277 | // length of the resulting string is equal to the input string |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 278 | void SanitizePartitionName(std::string* string) { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 279 | const char* accept = |
| 280 | "abcdefghijklmnopqrstuvwxyz" |
| 281 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 282 | "0123456789" |
| 283 | "_-."; |
| 284 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 285 | if (!string) return; |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 286 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 287 | std::string::size_type pos = 0; |
| 288 | while ((pos = string->find_first_not_of(accept, pos)) != std::string::npos) { |
| 289 | (*string)[pos] = '_'; |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 293 | std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uevent) const { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 294 | std::string device; |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 295 | std::string type; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 296 | |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 297 | if (FindPlatformDevice(uevent.path, &device)) { |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame] | 298 | // Skip /devices/platform or /devices/ if present |
| 299 | static const std::string devices_platform_prefix = "/devices/platform/"; |
| 300 | static const std::string devices_prefix = "/devices/"; |
| 301 | |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 302 | if (StartsWith(device, devices_platform_prefix)) { |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame] | 303 | device = device.substr(devices_platform_prefix.length()); |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 304 | } else if (StartsWith(device, devices_prefix)) { |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame] | 305 | device = device.substr(devices_prefix.length()); |
| 306 | } |
| 307 | |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 308 | type = "platform"; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 309 | } else if (FindPciDevicePrefix(uevent.path, &device)) { |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 310 | type = "pci"; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 311 | } else if (FindVbdDevicePrefix(uevent.path, &device)) { |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 312 | type = "vbd"; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 313 | } else { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 314 | return {}; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 315 | } |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 316 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 317 | std::vector<std::string> links; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 318 | |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 319 | LOG(VERBOSE) << "found " << type << " device " << device; |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 320 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 321 | auto link_path = "/dev/block/" + type + "/" + device; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 322 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 323 | if (!uevent.partition_name.empty()) { |
| 324 | std::string partition_name_sanitized(uevent.partition_name); |
| 325 | SanitizePartitionName(&partition_name_sanitized); |
| 326 | if (partition_name_sanitized != uevent.partition_name) { |
| 327 | LOG(VERBOSE) << "Linking partition '" << uevent.partition_name << "' as '" |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 328 | << partition_name_sanitized << "'"; |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 329 | } |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 330 | links.emplace_back(link_path + "/by-name/" + partition_name_sanitized); |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 331 | // Adds symlink: /dev/block/by-name/<partition_name>. |
| 332 | if (boot_devices_.find(device) != boot_devices_.end()) { |
| 333 | links.emplace_back("/dev/block/by-name/" + partition_name_sanitized); |
| 334 | } |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 337 | auto last_slash = uevent.path.rfind('/'); |
| 338 | links.emplace_back(link_path + "/" + uevent.path.substr(last_slash + 1)); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 339 | |
| 340 | return links; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 343 | 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] | 344 | int major, int minor, const std::vector<std::string>& links) const { |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 345 | if (action == "add") { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 346 | MakeDevice(devpath, block, major, minor, links); |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 347 | for (const auto& link : links) { |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 348 | if (!mkdir_recursive(Dirname(link), 0755)) { |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 349 | PLOG(ERROR) << "Failed to create directory " << Dirname(link); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 352 | if (symlink(devpath.c_str(), link.c_str())) { |
| 353 | if (errno != EEXIST) { |
| 354 | PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link; |
| 355 | } else if (std::string link_path; |
| 356 | Readlink(link, &link_path) && link_path != devpath) { |
| 357 | PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link |
| 358 | << ", which already links to: " << link_path; |
| 359 | } |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 360 | } |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 361 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 364 | if (action == "remove") { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 365 | for (const auto& link : links) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 366 | std::string link_path; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 367 | if (Readlink(link, &link_path) && link_path == devpath) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 368 | unlink(link.c_str()); |
| 369 | } |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 370 | } |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 371 | unlink(devpath.c_str()); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 372 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame^] | 375 | void DeviceHandler::HandleUevent(const Uevent& uevent) { |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 376 | if (uevent.action == "add" || uevent.action == "change" || uevent.action == "online") { |
| 377 | FixupSysPermissions(uevent.path, uevent.subsystem); |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 378 | } |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 379 | |
Tom Cherry | 3fa4673 | 2017-04-11 14:19:50 -0700 | [diff] [blame] | 380 | // if it's not a /dev device, nothing to do |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 381 | if (uevent.major < 0 || uevent.minor < 0) return; |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 382 | |
Tom Cherry | 3fa4673 | 2017-04-11 14:19:50 -0700 | [diff] [blame] | 383 | std::string devpath; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 384 | std::vector<std::string> links; |
| 385 | bool block = false; |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 386 | |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 387 | if (uevent.subsystem == "block") { |
| 388 | block = true; |
| 389 | devpath = "/dev/block/" + Basename(uevent.path); |
| 390 | |
| 391 | if (StartsWith(uevent.path, "/devices")) { |
| 392 | links = GetBlockDeviceSymlinks(uevent); |
| 393 | } |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 394 | } else if (const auto subsystem = |
| 395 | std::find(subsystems_.cbegin(), subsystems_.cend(), uevent.subsystem); |
| 396 | subsystem != subsystems_.cend()) { |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 397 | devpath = subsystem->ParseDevPath(uevent); |
Tom Cherry | 9c8d6dd | 2017-08-17 09:38:01 -0700 | [diff] [blame] | 398 | } else if (uevent.subsystem == "usb") { |
| 399 | if (!uevent.device_name.empty()) { |
| 400 | devpath = "/dev/" + uevent.device_name; |
| 401 | } else { |
| 402 | // This imitates the file system that would be created |
| 403 | // if we were using devfs instead. |
| 404 | // Minors are broken up into groups of 128, starting at "001" |
| 405 | int bus_id = uevent.minor / 128 + 1; |
| 406 | int device_id = uevent.minor % 128 + 1; |
| 407 | devpath = StringPrintf("/dev/bus/usb/%03d/%03d", bus_id, device_id); |
| 408 | } |
| 409 | } else if (StartsWith(uevent.subsystem, "usb")) { |
| 410 | // ignore other USB events |
| 411 | return; |
Tom Cherry | 780a71e | 2017-04-04 16:30:40 -0700 | [diff] [blame] | 412 | } else { |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 413 | devpath = "/dev/" + Basename(uevent.path); |
Tom Cherry | 780a71e | 2017-04-04 16:30:40 -0700 | [diff] [blame] | 414 | } |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 415 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 416 | mkdir_recursive(Dirname(devpath), 0755); |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 417 | |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 418 | HandleDevice(uevent.action, devpath, block, uevent.major, uevent.minor, links); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame^] | 421 | void DeviceHandler::ColdbootDone() { |
| 422 | skip_restorecon_ = true; |
| 423 | } |
| 424 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 425 | DeviceHandler::DeviceHandler(std::vector<Permissions> dev_permissions, |
| 426 | std::vector<SysfsPermissions> sysfs_permissions, |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 427 | std::vector<Subsystem> subsystems, std::set<std::string> boot_devices, |
| 428 | bool skip_restorecon) |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 429 | : dev_permissions_(std::move(dev_permissions)), |
| 430 | sysfs_permissions_(std::move(sysfs_permissions)), |
| 431 | subsystems_(std::move(subsystems)), |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 432 | boot_devices_(std::move(boot_devices)), |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 433 | skip_restorecon_(skip_restorecon), |
| 434 | sysfs_mount_point_("/sys") {} |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 435 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 436 | DeviceHandler::DeviceHandler() |
| 437 | : DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{}, |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 438 | std::vector<Subsystem>{}, std::set<std::string>{}, false) {} |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 439 | |
| 440 | } // namespace init |
| 441 | } // namespace android |