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