blob: 13cf991ca7f5f9f5325ea477f43a020576ba3ad2 [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 Cherryfe062052017-04-24 16:59:05 -070033#include "ueventd.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070034#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070035
Tom Cherrye7656b72017-05-01 17:10:09 -070036#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 Cherry81f5d3e2017-06-22 12:53:17 -070040using android::base::Basename;
41using android::base::Dirname;
42using android::base::Readlink;
43using android::base::Realpath;
44using android::base::StartsWith;
45using android::base::StringPrintf;
46
47namespace android {
48namespace init {
49
Andrew Boiea885d042013-09-13 17:41:20 -070050/* 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 Cherryed506f72017-05-25 15:58:59 -070053static bool FindPciDevicePrefix(const std::string& path, std::string* result) {
Tom Cherry2e344f92017-04-04 17:53:45 -070054 result->clear();
Andrew Boiea885d042013-09-13 17:41:20 -070055
Tom Cherry81f5d3e2017-06-22 12:53:17 -070056 if (!StartsWith(path, "/devices/pci")) return false;
Andrew Boiea885d042013-09-13 17:41:20 -070057
58 /* Beginning of the prefix is the initial "pci" after "/devices/" */
Tom Cherry2e344f92017-04-04 17:53:45 -070059 std::string::size_type start = 9;
Andrew Boiea885d042013-09-13 17:41:20 -070060
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 Cherry2e344f92017-04-04 17:53:45 -070063 auto end = path.find('/', start);
64 if (end == std::string::npos) return false;
Andrew Boiea885d042013-09-13 17:41:20 -070065
Tom Cherry2e344f92017-04-04 17:53:45 -070066 end = path.find('/', end + 1);
67 if (end == std::string::npos) return false;
Andrew Boiea885d042013-09-13 17:41:20 -070068
Tom Cherry2e344f92017-04-04 17:53:45 -070069 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 Boiea885d042013-09-13 17:41:20 -070078}
79
Jeremy Compostella937309d2017-03-03 16:27:29 +010080/* 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 Cherryed506f72017-05-25 15:58:59 -070084static bool FindVbdDevicePrefix(const std::string& path, std::string* result) {
Tom Cherry2e344f92017-04-04 17:53:45 -070085 result->clear();
86
Tom Cherry81f5d3e2017-06-22 12:53:17 -070087 if (!StartsWith(path, "/devices/vbd-")) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +010088
89 /* Beginning of the prefix is the initial "vbd-" after "/devices/" */
Tom Cherry2e344f92017-04-04 17:53:45 -070090 std::string::size_type start = 13;
Jeremy Compostella937309d2017-03-03 16:27:29 +010091
92 /* End of the prefix is one path '/' later, capturing the
93 virtual block device ID. Example: 768 */
Tom Cherry2e344f92017-04-04 17:53:45 -070094 auto end = path.find('/', start);
95 if (end == std::string::npos) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +010096
Tom Cherry2e344f92017-04-04 17:53:45 -070097 auto length = end - start;
98 if (length == 0) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +010099
Tom Cherry2e344f92017-04-04 17:53:45 -0700100 *result = path.substr(start, length);
101 return true;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100102}
103
Tom Cherryed506f72017-05-25 15:58:59 -0700104Permissions::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 Project4f6e8d72008-10-21 07:00:00 -0700124 }
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800125 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700126}
127
Tom Cherryed506f72017-05-25 15:58:59 -0700128bool Permissions::Match(const std::string& path) const {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700129 if (prefix_) return StartsWith(path, name_.c_str());
Tom Cherryed506f72017-05-25 15:58:59 -0700130 if (wildcard_) return fnmatch(name_.c_str(), path.c_str(), FNM_PATHNAME) == 0;
131 return path == name_;
132}
133
134bool SysfsPermissions::MatchWithSubsystem(const std::string& path,
135 const std::string& subsystem) const {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700136 std::string path_basename = Basename(path);
Tom Cherryed506f72017-05-25 15:58:59 -0700137 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
144void 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 Patilcd2ba0d2017-06-21 12:46:41 -0700160// 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
163bool 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 Cherry81f5d3e2017-06-22 12:53:17 -0700169 std::string directory = Dirname(path);
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700170
171 while (directory != "/" && directory != ".") {
172 std::string subsystem_link_path;
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700173 if (Realpath(directory + "/subsystem", &subsystem_link_path) &&
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700174 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 Cherryed506f72017-05-25 15:58:59 -0700178 return true;
179 }
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700180
181 auto last_slash = path.rfind('/');
182 if (last_slash == std::string::npos) return false;
183
184 path.erase(last_slash);
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700185 directory = Dirname(path);
Tom Cherryed506f72017-05-25 15:58:59 -0700186 }
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700187
Tom Cherryed506f72017-05-25 15:58:59 -0700188 return false;
189}
190
191void 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 Cherryc5833052017-05-16 15:35:41 -0700201 if (!skip_restorecon_ && access(path.c_str(), F_OK) == 0) {
Tom Cherryed506f72017-05-25 15:58:59 -0700202 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
209std::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 Cherryb4dd8812017-06-23 12:43:48 -0700222void DeviceHandler::MakeDevice(const std::string& path, bool block, int major, int minor,
Tom Cherryed506f72017-05-25 15:58:59 -0700223 const std::vector<std::string>& links) const {
224 auto[mode, uid, gid] = GetDevicePermissions(path, links);
225 mode |= (block ? S_IFBLK : S_IFCHR);
226
227 char* secontext = nullptr;
228 if (sehandle_) {
229 std::vector<const char*> c_links;
230 for (const auto& link : links) {
231 c_links.emplace_back(link.c_str());
232 }
233 c_links.emplace_back(nullptr);
234 if (selabel_lookup_best_match(sehandle_, &secontext, path.c_str(), &c_links[0], mode)) {
235 PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
236 return;
237 }
238 setfscreatecon(secontext);
239 }
240
241 dev_t dev = makedev(major, minor);
242 /* Temporarily change egid to avoid race condition setting the gid of the
243 * device node. Unforunately changing the euid would prevent creation of
244 * some device nodes, so the uid has to be set with chown() and is still
245 * racy. Fixing the gid race at least fixed the issue with system_server
246 * opening dynamic input devices under the AID_INPUT gid. */
247 if (setegid(gid)) {
248 PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed";
249 goto out;
250 }
251 /* If the node already exists update its SELinux label to handle cases when
252 * it was created with the wrong context during coldboot procedure. */
253 if (mknod(path.c_str(), mode, dev) && (errno == EEXIST) && secontext) {
254 char* fcon = nullptr;
255 int rc = lgetfilecon(path.c_str(), &fcon);
256 if (rc < 0) {
257 PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
258 goto out;
259 }
260
261 bool different = strcmp(fcon, secontext) != 0;
262 freecon(fcon);
263
264 if (different && lsetfilecon(path.c_str(), secontext)) {
265 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path
266 << "' device";
267 }
268 }
269
270out:
271 chown(path.c_str(), uid, -1);
272 if (setegid(AID_ROOT)) {
273 PLOG(FATAL) << "setegid(AID_ROOT) failed";
274 }
275
276 if (secontext) {
277 freecon(secontext);
278 setfscreatecon(nullptr);
279 }
280}
281
Tom Cherryc44f6a42017-04-05 15:58:31 -0700282// replaces any unacceptable characters with '_', the
283// length of the resulting string is equal to the input string
Tom Cherryed506f72017-05-25 15:58:59 -0700284void SanitizePartitionName(std::string* string) {
Tom Cherryc44f6a42017-04-05 15:58:31 -0700285 const char* accept =
286 "abcdefghijklmnopqrstuvwxyz"
287 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
288 "0123456789"
289 "_-.";
290
Tom Cherry2e344f92017-04-04 17:53:45 -0700291 if (!string) return;
Tom Cherryc44f6a42017-04-05 15:58:31 -0700292
Tom Cherry2e344f92017-04-04 17:53:45 -0700293 std::string::size_type pos = 0;
294 while ((pos = string->find_first_not_of(accept, pos)) != std::string::npos) {
295 (*string)[pos] = '_';
Tom Cherryc44f6a42017-04-05 15:58:31 -0700296 }
297}
298
Tom Cherryed506f72017-05-25 15:58:59 -0700299std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uevent) const {
Tom Cherry2e344f92017-04-04 17:53:45 -0700300 std::string device;
Tom Cherry2e344f92017-04-04 17:53:45 -0700301 std::string type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700302
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700303 if (FindPlatformDevice(uevent.path, &device)) {
Tom Cherry1ab8f552017-04-06 14:41:30 -0700304 // Skip /devices/platform or /devices/ if present
305 static const std::string devices_platform_prefix = "/devices/platform/";
306 static const std::string devices_prefix = "/devices/";
307
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700308 if (StartsWith(device, devices_platform_prefix.c_str())) {
Tom Cherry1ab8f552017-04-06 14:41:30 -0700309 device = device.substr(devices_platform_prefix.length());
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700310 } else if (StartsWith(device, devices_prefix.c_str())) {
Tom Cherry1ab8f552017-04-06 14:41:30 -0700311 device = device.substr(devices_prefix.length());
312 }
313
Andrew Boiea885d042013-09-13 17:41:20 -0700314 type = "platform";
Tom Cherryed506f72017-05-25 15:58:59 -0700315 } else if (FindPciDevicePrefix(uevent.path, &device)) {
Andrew Boiea885d042013-09-13 17:41:20 -0700316 type = "pci";
Tom Cherryed506f72017-05-25 15:58:59 -0700317 } else if (FindVbdDevicePrefix(uevent.path, &device)) {
Jeremy Compostella937309d2017-03-03 16:27:29 +0100318 type = "vbd";
Andrew Boiea885d042013-09-13 17:41:20 -0700319 } else {
Tom Cherry2e344f92017-04-04 17:53:45 -0700320 return {};
Andrew Boiea885d042013-09-13 17:41:20 -0700321 }
Dima Zavinf395c922013-03-06 16:23:57 -0800322
Tom Cherry2e344f92017-04-04 17:53:45 -0700323 std::vector<std::string> links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700324
Sandeep Patil35403eb2017-02-08 20:27:12 -0800325 LOG(VERBOSE) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700326
Tom Cherry2e344f92017-04-04 17:53:45 -0700327 auto link_path = "/dev/block/" + type + "/" + device;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700328
Tom Cherryed506f72017-05-25 15:58:59 -0700329 if (!uevent.partition_name.empty()) {
330 std::string partition_name_sanitized(uevent.partition_name);
331 SanitizePartitionName(&partition_name_sanitized);
332 if (partition_name_sanitized != uevent.partition_name) {
333 LOG(VERBOSE) << "Linking partition '" << uevent.partition_name << "' as '"
Tom Cherry2e344f92017-04-04 17:53:45 -0700334 << partition_name_sanitized << "'";
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700335 }
Tom Cherry2e344f92017-04-04 17:53:45 -0700336 links.emplace_back(link_path + "/by-name/" + partition_name_sanitized);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700337 }
338
Tom Cherryed506f72017-05-25 15:58:59 -0700339 if (uevent.partition_num >= 0) {
340 links.emplace_back(link_path + "/by-num/p" + std::to_string(uevent.partition_num));
Colin Crossb0ab94b2010-04-08 16:16:20 -0700341 }
342
Tom Cherryed506f72017-05-25 15:58:59 -0700343 auto last_slash = uevent.path.rfind('/');
344 links.emplace_back(link_path + "/" + uevent.path.substr(last_slash + 1));
Colin Crossb0ab94b2010-04-08 16:16:20 -0700345
346 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700347}
348
Tom Cherryb4dd8812017-06-23 12:43:48 -0700349void DeviceHandler::HandleDevice(const std::string& action, const std::string& devpath, bool block,
Tom Cherryed506f72017-05-25 15:58:59 -0700350 int major, int minor, const std::vector<std::string>& links) const {
Tom Cherrye3e48212017-04-11 13:53:37 -0700351 if (action == "add") {
Tom Cherryed506f72017-05-25 15:58:59 -0700352 MakeDevice(devpath, block, major, minor, links);
Tom Cherry2e344f92017-04-04 17:53:45 -0700353 for (const auto& link : links) {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700354 if (mkdir_recursive(Dirname(link), 0755, sehandle_)) {
355 PLOG(ERROR) << "Failed to create directory " << Dirname(link);
Tom Cherryed506f72017-05-25 15:58:59 -0700356 }
357
358 if (symlink(devpath.c_str(), link.c_str()) && errno != EEXIST) {
359 PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link;
360 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700361 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700362 }
363
Tom Cherrye3e48212017-04-11 13:53:37 -0700364 if (action == "remove") {
Tom Cherry2e344f92017-04-04 17:53:45 -0700365 for (const auto& link : links) {
Tom Cherryed506f72017-05-25 15:58:59 -0700366 std::string link_path;
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700367 if (Readlink(link, &link_path) && link_path == devpath) {
Tom Cherryed506f72017-05-25 15:58:59 -0700368 unlink(link.c_str());
369 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700370 }
Tom Cherrye3e48212017-04-11 13:53:37 -0700371 unlink(devpath.c_str());
Colin Crossb0ab94b2010-04-08 16:16:20 -0700372 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700373}
374
Tom Cherryb4dd8812017-06-23 12:43:48 -0700375void DeviceHandler::HandleDeviceEvent(const Uevent& uevent) {
376 if (uevent.action == "add" || uevent.action == "change" || uevent.action == "online") {
377 FixupSysPermissions(uevent.path, uevent.subsystem);
Tom Cherrye3e48212017-04-11 13:53:37 -0700378 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700379
Tom Cherry3fa46732017-04-11 14:19:50 -0700380 // if it's not a /dev device, nothing to do
Tom Cherryed506f72017-05-25 15:58:59 -0700381 if (uevent.major < 0 || uevent.minor < 0) return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800382
Tom Cherry3fa46732017-04-11 14:19:50 -0700383 std::string devpath;
Tom Cherryb4dd8812017-06-23 12:43:48 -0700384 std::vector<std::string> links;
385 bool block = false;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800386
Tom Cherryb4dd8812017-06-23 12:43:48 -0700387 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 }
394 } else if (StartsWith(uevent.subsystem, "usb")) {
Tom Cherryed506f72017-05-25 15:58:59 -0700395 if (uevent.subsystem == "usb") {
396 if (!uevent.device_name.empty()) {
397 devpath = "/dev/" + uevent.device_name;
Tom Cherry3fa46732017-04-11 14:19:50 -0700398 } else {
399 // This imitates the file system that would be created
400 // if we were using devfs instead.
401 // Minors are broken up into groups of 128, starting at "001"
Tom Cherryed506f72017-05-25 15:58:59 -0700402 int bus_id = uevent.minor / 128 + 1;
403 int device_id = uevent.minor % 128 + 1;
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700404 devpath = StringPrintf("/dev/bus/usb/%03d/%03d", bus_id, device_id);
Tom Cherry3fa46732017-04-11 14:19:50 -0700405 }
Tom Cherry3fa46732017-04-11 14:19:50 -0700406 } else {
407 // ignore other USB events
408 return;
409 }
Tom Cherryed506f72017-05-25 15:58:59 -0700410 } else if (const auto subsystem =
411 std::find(subsystems_.cbegin(), subsystems_.cend(), uevent.subsystem);
412 subsystem != subsystems_.cend()) {
Tom Cherryfe062052017-04-24 16:59:05 -0700413 devpath = subsystem->ParseDevPath(uevent);
Tom Cherry780a71e2017-04-04 16:30:40 -0700414 } else {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700415 devpath = "/dev/" + Basename(uevent.path);
Tom Cherry780a71e2017-04-04 16:30:40 -0700416 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700417
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700418 mkdir_recursive(Dirname(devpath), 0755, sehandle_);
Tom Cherryfe062052017-04-24 16:59:05 -0700419
Tom Cherryb4dd8812017-06-23 12:43:48 -0700420 HandleDevice(uevent.action, devpath, block, uevent.major, uevent.minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700421}
422
Tom Cherryed506f72017-05-25 15:58:59 -0700423DeviceHandler::DeviceHandler(std::vector<Permissions> dev_permissions,
424 std::vector<SysfsPermissions> sysfs_permissions,
Tom Cherryc5833052017-05-16 15:35:41 -0700425 std::vector<Subsystem> subsystems, 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)),
Tom Cherryc5833052017-05-16 15:35:41 -0700429 sehandle_(selinux_android_file_context_handle()),
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>{},
Tom Cherryc5833052017-05-16 15:35:41 -0700435 std::vector<Subsystem>{}, false) {}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700436
437} // namespace init
438} // namespace android