blob: ada1e2870ee9fb07a8ff385091974dec9fd7c94b [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
Tom Cherryed506f72017-05-25 15:58:59 -07002 * Copyright (C) 2007 The Android Open Source Project
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07003 *
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 Cherrycc054c92017-04-05 17:55:46 -070017#include "devices.h"
18
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070019#include <errno.h>
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070020#include <fnmatch.h>
Elliott Hughes51056c42017-05-18 09:13:15 -070021#include <sys/sysmacros.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070022#include <unistd.h>
23
James Hawkins588a2ca2016-02-18 14:52:46 -080024#include <memory>
25
Tom Cherry3f5eaae52017-04-06 16:30:22 -070026#include <android-base/logging.h>
Rob Herring6de783a2016-05-06 10:06:59 -050027#include <android-base/stringprintf.h>
Tom Cherry2e344f92017-04-04 17:53:45 -070028#include <android-base/strings.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070029#include <private/android_filesystem_config.h>
30#include <selinux/android.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070031#include <selinux/selinux.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100032
Tom Cherry0c8d6d22017-08-10 12:22:44 -070033#include "selinux.h"
Tom Cherryfe062052017-04-24 16:59:05 -070034#include "ueventd.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070035#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070036
Tom Cherrye7656b72017-05-01 17:10:09 -070037#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 Cherry81f5d3e2017-06-22 12:53:17 -070041using android::base::Basename;
42using android::base::Dirname;
43using android::base::Readlink;
44using android::base::Realpath;
45using android::base::StartsWith;
46using android::base::StringPrintf;
47
48namespace android {
49namespace init {
50
Andrew Boiea885d042013-09-13 17:41:20 -070051/* 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 Cherryed506f72017-05-25 15:58:59 -070054static bool FindPciDevicePrefix(const std::string& path, std::string* result) {
Tom Cherry2e344f92017-04-04 17:53:45 -070055 result->clear();
Andrew Boiea885d042013-09-13 17:41:20 -070056
Tom Cherry81f5d3e2017-06-22 12:53:17 -070057 if (!StartsWith(path, "/devices/pci")) return false;
Andrew Boiea885d042013-09-13 17:41:20 -070058
59 /* Beginning of the prefix is the initial "pci" after "/devices/" */
Tom Cherry2e344f92017-04-04 17:53:45 -070060 std::string::size_type start = 9;
Andrew Boiea885d042013-09-13 17:41:20 -070061
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 Cherry2e344f92017-04-04 17:53:45 -070064 auto end = path.find('/', start);
65 if (end == std::string::npos) return false;
Andrew Boiea885d042013-09-13 17:41:20 -070066
Tom Cherry2e344f92017-04-04 17:53:45 -070067 end = path.find('/', end + 1);
68 if (end == std::string::npos) return false;
Andrew Boiea885d042013-09-13 17:41:20 -070069
Tom Cherry2e344f92017-04-04 17:53:45 -070070 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 Boiea885d042013-09-13 17:41:20 -070079}
80
Jeremy Compostella937309d2017-03-03 16:27:29 +010081/* 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 Cherryed506f72017-05-25 15:58:59 -070085static bool FindVbdDevicePrefix(const std::string& path, std::string* result) {
Tom Cherry2e344f92017-04-04 17:53:45 -070086 result->clear();
87
Tom Cherry81f5d3e2017-06-22 12:53:17 -070088 if (!StartsWith(path, "/devices/vbd-")) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +010089
90 /* Beginning of the prefix is the initial "vbd-" after "/devices/" */
Tom Cherry2e344f92017-04-04 17:53:45 -070091 std::string::size_type start = 13;
Jeremy Compostella937309d2017-03-03 16:27:29 +010092
93 /* End of the prefix is one path '/' later, capturing the
94 virtual block device ID. Example: 768 */
Tom Cherry2e344f92017-04-04 17:53:45 -070095 auto end = path.find('/', start);
96 if (end == std::string::npos) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +010097
Tom Cherry2e344f92017-04-04 17:53:45 -070098 auto length = end - start;
99 if (length == 0) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100100
Tom Cherry2e344f92017-04-04 17:53:45 -0700101 *result = path.substr(start, length);
102 return true;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100103}
104
Tom Cherryed506f72017-05-25 15:58:59 -0700105Permissions::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 Project4f6e8d72008-10-21 07:00:00 -0700125 }
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800126 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700127}
128
Tom Cherryed506f72017-05-25 15:58:59 -0700129bool Permissions::Match(const std::string& path) const {
Elliott Hughes579e6822017-12-20 09:41:00 -0800130 if (prefix_) return StartsWith(path, name_);
Tom Cherryed506f72017-05-25 15:58:59 -0700131 if (wildcard_) return fnmatch(name_.c_str(), path.c_str(), FNM_PATHNAME) == 0;
132 return path == name_;
133}
134
135bool SysfsPermissions::MatchWithSubsystem(const std::string& path,
136 const std::string& subsystem) const {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700137 std::string path_basename = Basename(path);
Tom Cherryed506f72017-05-25 15:58:59 -0700138 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
145void 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 Patilcd2ba0d2017-06-21 12:46:41 -0700161// 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
164bool 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 Cherry81f5d3e2017-06-22 12:53:17 -0700170 std::string directory = Dirname(path);
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700171
172 while (directory != "/" && directory != ".") {
173 std::string subsystem_link_path;
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700174 if (Realpath(directory + "/subsystem", &subsystem_link_path) &&
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700175 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 Cherryed506f72017-05-25 15:58:59 -0700179 return true;
180 }
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700181
182 auto last_slash = path.rfind('/');
183 if (last_slash == std::string::npos) return false;
184
185 path.erase(last_slash);
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700186 directory = Dirname(path);
Tom Cherryed506f72017-05-25 15:58:59 -0700187 }
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700188
Tom Cherryed506f72017-05-25 15:58:59 -0700189 return false;
190}
191
192void 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 Cherryc5833052017-05-16 15:35:41 -0700202 if (!skip_restorecon_ && access(path.c_str(), F_OK) == 0) {
Tom Cherryed506f72017-05-25 15:58:59 -0700203 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
210std::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 Cherryb4dd8812017-06-23 12:43:48 -0700223void DeviceHandler::MakeDevice(const std::string& path, bool block, int major, int minor,
Tom Cherryed506f72017-05-25 15:58:59 -0700224 const std::vector<std::string>& links) const {
225 auto[mode, uid, gid] = GetDevicePermissions(path, links);
226 mode |= (block ? S_IFBLK : S_IFCHR);
227
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700228 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 Cherryed506f72017-05-25 15:58:59 -0700235 }
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 Cherry0c8d6d22017-08-10 12:22:44 -0700249 if (mknod(path.c_str(), mode, dev) && (errno == EEXIST) && !secontext.empty()) {
Tom Cherryed506f72017-05-25 15:58:59 -0700250 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 Cherry0c8d6d22017-08-10 12:22:44 -0700257 bool different = fcon != secontext;
Tom Cherryed506f72017-05-25 15:58:59 -0700258 freecon(fcon);
259
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700260 if (different && lsetfilecon(path.c_str(), secontext.c_str())) {
Tom Cherryed506f72017-05-25 15:58:59 -0700261 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path
262 << "' device";
263 }
264 }
265
266out:
267 chown(path.c_str(), uid, -1);
268 if (setegid(AID_ROOT)) {
269 PLOG(FATAL) << "setegid(AID_ROOT) failed";
270 }
271
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700272 if (!secontext.empty()) {
Tom Cherryed506f72017-05-25 15:58:59 -0700273 setfscreatecon(nullptr);
274 }
275}
276
Tom Cherryc44f6a42017-04-05 15:58:31 -0700277// replaces any unacceptable characters with '_', the
278// length of the resulting string is equal to the input string
Tom Cherryed506f72017-05-25 15:58:59 -0700279void SanitizePartitionName(std::string* string) {
Tom Cherryc44f6a42017-04-05 15:58:31 -0700280 const char* accept =
281 "abcdefghijklmnopqrstuvwxyz"
282 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
283 "0123456789"
284 "_-.";
285
Tom Cherry2e344f92017-04-04 17:53:45 -0700286 if (!string) return;
Tom Cherryc44f6a42017-04-05 15:58:31 -0700287
Tom Cherry2e344f92017-04-04 17:53:45 -0700288 std::string::size_type pos = 0;
289 while ((pos = string->find_first_not_of(accept, pos)) != std::string::npos) {
290 (*string)[pos] = '_';
Tom Cherryc44f6a42017-04-05 15:58:31 -0700291 }
292}
293
Tom Cherryed506f72017-05-25 15:58:59 -0700294std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uevent) const {
Tom Cherry2e344f92017-04-04 17:53:45 -0700295 std::string device;
Tom Cherry2e344f92017-04-04 17:53:45 -0700296 std::string type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700297
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700298 if (FindPlatformDevice(uevent.path, &device)) {
Tom Cherry1ab8f552017-04-06 14:41:30 -0700299 // 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 Hughes579e6822017-12-20 09:41:00 -0800303 if (StartsWith(device, devices_platform_prefix)) {
Tom Cherry1ab8f552017-04-06 14:41:30 -0700304 device = device.substr(devices_platform_prefix.length());
Elliott Hughes579e6822017-12-20 09:41:00 -0800305 } else if (StartsWith(device, devices_prefix)) {
Tom Cherry1ab8f552017-04-06 14:41:30 -0700306 device = device.substr(devices_prefix.length());
307 }
308
Andrew Boiea885d042013-09-13 17:41:20 -0700309 type = "platform";
Tom Cherryed506f72017-05-25 15:58:59 -0700310 } else if (FindPciDevicePrefix(uevent.path, &device)) {
Andrew Boiea885d042013-09-13 17:41:20 -0700311 type = "pci";
Tom Cherryed506f72017-05-25 15:58:59 -0700312 } else if (FindVbdDevicePrefix(uevent.path, &device)) {
Jeremy Compostella937309d2017-03-03 16:27:29 +0100313 type = "vbd";
Andrew Boiea885d042013-09-13 17:41:20 -0700314 } else {
Tom Cherry2e344f92017-04-04 17:53:45 -0700315 return {};
Andrew Boiea885d042013-09-13 17:41:20 -0700316 }
Dima Zavinf395c922013-03-06 16:23:57 -0800317
Tom Cherry2e344f92017-04-04 17:53:45 -0700318 std::vector<std::string> links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700319
Sandeep Patil35403eb2017-02-08 20:27:12 -0800320 LOG(VERBOSE) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700321
Tom Cherry2e344f92017-04-04 17:53:45 -0700322 auto link_path = "/dev/block/" + type + "/" + device;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700323
Tom Cherryed506f72017-05-25 15:58:59 -0700324 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 Cherry2e344f92017-04-04 17:53:45 -0700329 << partition_name_sanitized << "'";
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700330 }
Tom Cherry2e344f92017-04-04 17:53:45 -0700331 links.emplace_back(link_path + "/by-name/" + partition_name_sanitized);
Bowgo Tsai5ee7dae2018-05-16 18:33:44 +0800332 // 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 Crossb0ab94b2010-04-08 16:16:20 -0700336 }
337
Tom Cherryed506f72017-05-25 15:58:59 -0700338 auto last_slash = uevent.path.rfind('/');
339 links.emplace_back(link_path + "/" + uevent.path.substr(last_slash + 1));
Colin Crossb0ab94b2010-04-08 16:16:20 -0700340
341 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700342}
343
Tom Cherryb4dd8812017-06-23 12:43:48 -0700344void DeviceHandler::HandleDevice(const std::string& action, const std::string& devpath, bool block,
Tom Cherryed506f72017-05-25 15:58:59 -0700345 int major, int minor, const std::vector<std::string>& links) const {
Tom Cherrye3e48212017-04-11 13:53:37 -0700346 if (action == "add") {
Tom Cherryed506f72017-05-25 15:58:59 -0700347 MakeDevice(devpath, block, major, minor, links);
Tom Cherry2e344f92017-04-04 17:53:45 -0700348 for (const auto& link : links) {
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700349 if (!mkdir_recursive(Dirname(link), 0755)) {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700350 PLOG(ERROR) << "Failed to create directory " << Dirname(link);
Tom Cherryed506f72017-05-25 15:58:59 -0700351 }
352
Bowgo Tsai5ee7dae2018-05-16 18:33:44 +0800353 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 Cherryed506f72017-05-25 15:58:59 -0700361 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700362 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700363 }
364
Tom Cherrye3e48212017-04-11 13:53:37 -0700365 if (action == "remove") {
Tom Cherry2e344f92017-04-04 17:53:45 -0700366 for (const auto& link : links) {
Tom Cherryed506f72017-05-25 15:58:59 -0700367 std::string link_path;
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700368 if (Readlink(link, &link_path) && link_path == devpath) {
Tom Cherryed506f72017-05-25 15:58:59 -0700369 unlink(link.c_str());
370 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700371 }
Tom Cherrye3e48212017-04-11 13:53:37 -0700372 unlink(devpath.c_str());
Colin Crossb0ab94b2010-04-08 16:16:20 -0700373 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700374}
375
Tom Cherryb4dd8812017-06-23 12:43:48 -0700376void DeviceHandler::HandleDeviceEvent(const Uevent& uevent) {
377 if (uevent.action == "add" || uevent.action == "change" || uevent.action == "online") {
378 FixupSysPermissions(uevent.path, uevent.subsystem);
Tom Cherrye3e48212017-04-11 13:53:37 -0700379 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700380
Tom Cherry3fa46732017-04-11 14:19:50 -0700381 // if it's not a /dev device, nothing to do
Tom Cherryed506f72017-05-25 15:58:59 -0700382 if (uevent.major < 0 || uevent.minor < 0) return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800383
Tom Cherry3fa46732017-04-11 14:19:50 -0700384 std::string devpath;
Tom Cherryb4dd8812017-06-23 12:43:48 -0700385 std::vector<std::string> links;
386 bool block = false;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800387
Tom Cherryb4dd8812017-06-23 12:43:48 -0700388 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 Cherryed506f72017-05-25 15:58:59 -0700395 } else if (const auto subsystem =
396 std::find(subsystems_.cbegin(), subsystems_.cend(), uevent.subsystem);
397 subsystem != subsystems_.cend()) {
Tom Cherryfe062052017-04-24 16:59:05 -0700398 devpath = subsystem->ParseDevPath(uevent);
Tom Cherry9c8d6dd2017-08-17 09:38:01 -0700399 } 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 Cherry780a71e2017-04-04 16:30:40 -0700413 } else {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700414 devpath = "/dev/" + Basename(uevent.path);
Tom Cherry780a71e2017-04-04 16:30:40 -0700415 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700416
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700417 mkdir_recursive(Dirname(devpath), 0755);
Tom Cherryfe062052017-04-24 16:59:05 -0700418
Tom Cherryb4dd8812017-06-23 12:43:48 -0700419 HandleDevice(uevent.action, devpath, block, uevent.major, uevent.minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700420}
421
Tom Cherryed506f72017-05-25 15:58:59 -0700422DeviceHandler::DeviceHandler(std::vector<Permissions> dev_permissions,
423 std::vector<SysfsPermissions> sysfs_permissions,
Bowgo Tsai5ee7dae2018-05-16 18:33:44 +0800424 std::vector<Subsystem> subsystems, std::set<std::string> boot_devices,
425 bool skip_restorecon)
Tom Cherryed506f72017-05-25 15:58:59 -0700426 : dev_permissions_(std::move(dev_permissions)),
427 sysfs_permissions_(std::move(sysfs_permissions)),
428 subsystems_(std::move(subsystems)),
Bowgo Tsai5ee7dae2018-05-16 18:33:44 +0800429 boot_devices_(std::move(boot_devices)),
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700430 skip_restorecon_(skip_restorecon),
431 sysfs_mount_point_("/sys") {}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700432
Tom Cherryed506f72017-05-25 15:58:59 -0700433DeviceHandler::DeviceHandler()
434 : DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{},
Bowgo Tsai5ee7dae2018-05-16 18:33:44 +0800435 std::vector<Subsystem>{}, std::set<std::string>{}, false) {}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700436
437} // namespace init
438} // namespace android