blob: f6e453aa6cd7a615092458063988eaacf3d82eed [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
Mark Salyzyne419a792019-03-06 15:18:46 -080024#include <chrono>
James Hawkins588a2ca2016-02-18 14:52:46 -080025#include <memory>
Mark Salyzyne419a792019-03-06 15:18:46 -080026#include <string>
27#include <thread>
James Hawkins588a2ca2016-02-18 14:52:46 -080028
Mark Salyzyne419a792019-03-06 15:18:46 -080029#include <android-base/chrono_utils.h>
30#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070031#include <android-base/logging.h>
Rob Herring6de783a2016-05-06 10:06:59 -050032#include <android-base/stringprintf.h>
Tom Cherry2e344f92017-04-04 17:53:45 -070033#include <android-base/strings.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070034#include <private/android_filesystem_config.h>
35#include <selinux/android.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070036#include <selinux/selinux.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100037
Vic Yang92c236e2019-05-28 15:58:35 -070038#include "selabel.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070039#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070040
Mark Salyzyne419a792019-03-06 15:18:46 -080041using namespace std::chrono_literals;
42
Tom Cherry81f5d3e2017-06-22 12:53:17 -070043using android::base::Basename;
44using android::base::Dirname;
Mark Salyzyne419a792019-03-06 15:18:46 -080045using android::base::ReadFileToString;
Tom Cherry81f5d3e2017-06-22 12:53:17 -070046using android::base::Readlink;
47using android::base::Realpath;
48using android::base::StartsWith;
49using android::base::StringPrintf;
Mark Salyzyne419a792019-03-06 15:18:46 -080050using android::base::Trim;
Tom Cherry81f5d3e2017-06-22 12:53:17 -070051
52namespace android {
53namespace init {
54
Andrew Boiea885d042013-09-13 17:41:20 -070055/* Given a path that may start with a PCI device, populate the supplied buffer
56 * with the PCI domain/bus number and the peripheral ID and return 0.
57 * If it doesn't start with a PCI device, or there is some error, return -1 */
Tom Cherryed506f72017-05-25 15:58:59 -070058static bool FindPciDevicePrefix(const std::string& path, std::string* result) {
Tom Cherry2e344f92017-04-04 17:53:45 -070059 result->clear();
Andrew Boiea885d042013-09-13 17:41:20 -070060
Tom Cherry81f5d3e2017-06-22 12:53:17 -070061 if (!StartsWith(path, "/devices/pci")) return false;
Andrew Boiea885d042013-09-13 17:41:20 -070062
63 /* Beginning of the prefix is the initial "pci" after "/devices/" */
Tom Cherry2e344f92017-04-04 17:53:45 -070064 std::string::size_type start = 9;
Andrew Boiea885d042013-09-13 17:41:20 -070065
66 /* End of the prefix is two path '/' later, capturing the domain/bus number
67 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
Tom Cherry2e344f92017-04-04 17:53:45 -070068 auto end = path.find('/', start);
69 if (end == std::string::npos) return false;
Andrew Boiea885d042013-09-13 17:41:20 -070070
Tom Cherry2e344f92017-04-04 17:53:45 -070071 end = path.find('/', end + 1);
72 if (end == std::string::npos) return false;
Andrew Boiea885d042013-09-13 17:41:20 -070073
Tom Cherry2e344f92017-04-04 17:53:45 -070074 auto length = end - start;
75 if (length <= 4) {
76 // The minimum string that will get to this check is 'pci/', which is malformed,
77 // so return false
78 return false;
79 }
80
81 *result = path.substr(start, length);
82 return true;
Andrew Boiea885d042013-09-13 17:41:20 -070083}
84
Jeremy Compostella937309d2017-03-03 16:27:29 +010085/* Given a path that may start with a virtual block device, populate
86 * the supplied buffer with the virtual block device ID and return 0.
87 * If it doesn't start with a virtual block device, or there is some
88 * error, return -1 */
Tom Cherryed506f72017-05-25 15:58:59 -070089static bool FindVbdDevicePrefix(const std::string& path, std::string* result) {
Tom Cherry2e344f92017-04-04 17:53:45 -070090 result->clear();
91
Tom Cherry81f5d3e2017-06-22 12:53:17 -070092 if (!StartsWith(path, "/devices/vbd-")) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +010093
94 /* Beginning of the prefix is the initial "vbd-" after "/devices/" */
Tom Cherry2e344f92017-04-04 17:53:45 -070095 std::string::size_type start = 13;
Jeremy Compostella937309d2017-03-03 16:27:29 +010096
97 /* End of the prefix is one path '/' later, capturing the
98 virtual block device ID. Example: 768 */
Tom Cherry2e344f92017-04-04 17:53:45 -070099 auto end = path.find('/', start);
100 if (end == std::string::npos) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100101
Tom Cherry2e344f92017-04-04 17:53:45 -0700102 auto length = end - start;
103 if (length == 0) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100104
Tom Cherry2e344f92017-04-04 17:53:45 -0700105 *result = path.substr(start, length);
106 return true;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100107}
108
Mark Salyzyne419a792019-03-06 15:18:46 -0800109// Given a path that may start with a virtual dm block device, populate
110// the supplied buffer with the dm module's instantiated name.
111// If it doesn't start with a virtual block device, or there is some
112// error, return false.
David Anderson924858c2019-06-26 17:00:00 -0700113static bool FindDmDevice(const std::string& path, std::string* name, std::string* uuid) {
Mark Salyzyne419a792019-03-06 15:18:46 -0800114 if (!StartsWith(path, "/devices/virtual/block/dm-")) return false;
Mark Salyzyne419a792019-03-06 15:18:46 -0800115
David Anderson924858c2019-06-26 17:00:00 -0700116 if (!ReadFileToString("/sys" + path + "/dm/name", name)) {
117 return false;
Mark Salyzyne419a792019-03-06 15:18:46 -0800118 }
David Anderson924858c2019-06-26 17:00:00 -0700119 ReadFileToString("/sys" + path + "/dm/uuid", uuid);
120
121 *name = android::base::Trim(*name);
122 *uuid = android::base::Trim(*uuid);
Mark Salyzyne419a792019-03-06 15:18:46 -0800123 return true;
124}
125
Tom Cherryed506f72017-05-25 15:58:59 -0700126Permissions::Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid)
127 : name_(name), perm_(perm), uid_(uid), gid_(gid), prefix_(false), wildcard_(false) {
128 // Set 'prefix_' or 'wildcard_' based on the below cases:
129 //
130 // 1) No '*' in 'name' -> Neither are set and Match() checks a given path for strict
131 // equality with 'name'
132 //
133 // 2) '*' only appears as the last character in 'name' -> 'prefix'_ is set to true and
134 // Match() checks if 'name' is a prefix of a given path.
135 //
136 // 3) '*' appears elsewhere -> 'wildcard_' is set to true and Match() uses fnmatch()
137 // with FNM_PATHNAME to compare 'name' to a given path.
138
139 auto wildcard_position = name_.find('*');
140 if (wildcard_position != std::string::npos) {
141 if (wildcard_position == name_.length() - 1) {
142 prefix_ = true;
143 name_.pop_back();
144 } else {
145 wildcard_ = true;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700146 }
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800147 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700148}
149
Tom Cherryed506f72017-05-25 15:58:59 -0700150bool Permissions::Match(const std::string& path) const {
Elliott Hughes579e6822017-12-20 09:41:00 -0800151 if (prefix_) return StartsWith(path, name_);
Tom Cherryed506f72017-05-25 15:58:59 -0700152 if (wildcard_) return fnmatch(name_.c_str(), path.c_str(), FNM_PATHNAME) == 0;
153 return path == name_;
154}
155
156bool SysfsPermissions::MatchWithSubsystem(const std::string& path,
157 const std::string& subsystem) const {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700158 std::string path_basename = Basename(path);
Tom Cherryed506f72017-05-25 15:58:59 -0700159 if (name().find(subsystem) != std::string::npos) {
160 if (Match("/sys/class/" + subsystem + "/" + path_basename)) return true;
161 if (Match("/sys/bus/" + subsystem + "/devices/" + path_basename)) return true;
162 }
163 return Match(path);
164}
165
166void SysfsPermissions::SetPermissions(const std::string& path) const {
167 std::string attribute_file = path + "/" + attribute_;
168 LOG(VERBOSE) << "fixup " << attribute_file << " " << uid() << " " << gid() << " " << std::oct
169 << perm();
170
171 if (access(attribute_file.c_str(), F_OK) == 0) {
172 if (chown(attribute_file.c_str(), uid(), gid()) != 0) {
173 PLOG(ERROR) << "chown(" << attribute_file << ", " << uid() << ", " << gid()
174 << ") failed";
175 }
176 if (chmod(attribute_file.c_str(), perm()) != 0) {
177 PLOG(ERROR) << "chmod(" << attribute_file << ", " << perm() << ") failed";
178 }
179 }
180}
181
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700182// Given a path that may start with a platform device, find the parent platform device by finding a
183// parent directory with a 'subsystem' symlink that points to the platform bus.
184// If it doesn't start with a platform device, return false
185bool DeviceHandler::FindPlatformDevice(std::string path, std::string* platform_device_path) const {
186 platform_device_path->clear();
187
188 // Uevents don't contain the mount point, so we need to add it here.
189 path.insert(0, sysfs_mount_point_);
190
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700191 std::string directory = Dirname(path);
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700192
193 while (directory != "/" && directory != ".") {
194 std::string subsystem_link_path;
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700195 if (Realpath(directory + "/subsystem", &subsystem_link_path) &&
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700196 subsystem_link_path == sysfs_mount_point_ + "/bus/platform") {
197 // We need to remove the mount point that we added above before returning.
198 directory.erase(0, sysfs_mount_point_.size());
199 *platform_device_path = directory;
Tom Cherryed506f72017-05-25 15:58:59 -0700200 return true;
201 }
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700202
203 auto last_slash = path.rfind('/');
204 if (last_slash == std::string::npos) return false;
205
206 path.erase(last_slash);
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700207 directory = Dirname(path);
Tom Cherryed506f72017-05-25 15:58:59 -0700208 }
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700209
Tom Cherryed506f72017-05-25 15:58:59 -0700210 return false;
211}
212
213void DeviceHandler::FixupSysPermissions(const std::string& upath,
214 const std::string& subsystem) const {
215 // upaths omit the "/sys" that paths in this list
216 // contain, so we prepend it...
217 std::string path = "/sys" + upath;
218
219 for (const auto& s : sysfs_permissions_) {
220 if (s.MatchWithSubsystem(path, subsystem)) s.SetPermissions(path);
221 }
222
Tom Cherryc5833052017-05-16 15:35:41 -0700223 if (!skip_restorecon_ && access(path.c_str(), F_OK) == 0) {
Tom Cherryed506f72017-05-25 15:58:59 -0700224 LOG(VERBOSE) << "restorecon_recursive: " << path;
225 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
226 PLOG(ERROR) << "selinux_android_restorecon(" << path << ") failed";
227 }
228 }
229}
230
231std::tuple<mode_t, uid_t, gid_t> DeviceHandler::GetDevicePermissions(
232 const std::string& path, const std::vector<std::string>& links) const {
233 // Search the perms list in reverse so that ueventd.$hardware can override ueventd.rc.
234 for (auto it = dev_permissions_.crbegin(); it != dev_permissions_.crend(); ++it) {
235 if (it->Match(path) || std::any_of(links.cbegin(), links.cend(),
236 [it](const auto& link) { return it->Match(link); })) {
237 return {it->perm(), it->uid(), it->gid()};
238 }
239 }
240 /* Default if nothing found. */
241 return {0600, 0, 0};
242}
243
Tom Cherryb4dd8812017-06-23 12:43:48 -0700244void DeviceHandler::MakeDevice(const std::string& path, bool block, int major, int minor,
Tom Cherryed506f72017-05-25 15:58:59 -0700245 const std::vector<std::string>& links) const {
246 auto[mode, uid, gid] = GetDevicePermissions(path, links);
247 mode |= (block ? S_IFBLK : S_IFCHR);
248
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700249 std::string secontext;
250 if (!SelabelLookupFileContextBestMatch(path, links, mode, &secontext)) {
251 PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
252 return;
253 }
254 if (!secontext.empty()) {
255 setfscreatecon(secontext.c_str());
Tom Cherryed506f72017-05-25 15:58:59 -0700256 }
257
258 dev_t dev = makedev(major, minor);
259 /* Temporarily change egid to avoid race condition setting the gid of the
260 * device node. Unforunately changing the euid would prevent creation of
261 * some device nodes, so the uid has to be set with chown() and is still
262 * racy. Fixing the gid race at least fixed the issue with system_server
263 * opening dynamic input devices under the AID_INPUT gid. */
264 if (setegid(gid)) {
265 PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed";
266 goto out;
267 }
268 /* If the node already exists update its SELinux label to handle cases when
269 * it was created with the wrong context during coldboot procedure. */
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700270 if (mknod(path.c_str(), mode, dev) && (errno == EEXIST) && !secontext.empty()) {
Tom Cherryed506f72017-05-25 15:58:59 -0700271 char* fcon = nullptr;
272 int rc = lgetfilecon(path.c_str(), &fcon);
273 if (rc < 0) {
274 PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
275 goto out;
276 }
277
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700278 bool different = fcon != secontext;
Tom Cherryed506f72017-05-25 15:58:59 -0700279 freecon(fcon);
280
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700281 if (different && lsetfilecon(path.c_str(), secontext.c_str())) {
Tom Cherryed506f72017-05-25 15:58:59 -0700282 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path
283 << "' device";
284 }
285 }
286
287out:
288 chown(path.c_str(), uid, -1);
289 if (setegid(AID_ROOT)) {
290 PLOG(FATAL) << "setegid(AID_ROOT) failed";
291 }
292
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700293 if (!secontext.empty()) {
Tom Cherryed506f72017-05-25 15:58:59 -0700294 setfscreatecon(nullptr);
295 }
296}
297
Tom Cherryc44f6a42017-04-05 15:58:31 -0700298// replaces any unacceptable characters with '_', the
299// length of the resulting string is equal to the input string
Tom Cherryed506f72017-05-25 15:58:59 -0700300void SanitizePartitionName(std::string* string) {
Tom Cherryc44f6a42017-04-05 15:58:31 -0700301 const char* accept =
302 "abcdefghijklmnopqrstuvwxyz"
303 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
304 "0123456789"
305 "_-.";
306
Tom Cherry2e344f92017-04-04 17:53:45 -0700307 if (!string) return;
Tom Cherryc44f6a42017-04-05 15:58:31 -0700308
Tom Cherry2e344f92017-04-04 17:53:45 -0700309 std::string::size_type pos = 0;
310 while ((pos = string->find_first_not_of(accept, pos)) != std::string::npos) {
311 (*string)[pos] = '_';
Tom Cherryc44f6a42017-04-05 15:58:31 -0700312 }
313}
314
Tom Cherryed506f72017-05-25 15:58:59 -0700315std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uevent) const {
Tom Cherry2e344f92017-04-04 17:53:45 -0700316 std::string device;
Tom Cherry2e344f92017-04-04 17:53:45 -0700317 std::string type;
Mark Salyzyne419a792019-03-06 15:18:46 -0800318 std::string partition;
David Anderson924858c2019-06-26 17:00:00 -0700319 std::string uuid;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700320
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700321 if (FindPlatformDevice(uevent.path, &device)) {
Tom Cherry1ab8f552017-04-06 14:41:30 -0700322 // Skip /devices/platform or /devices/ if present
323 static const std::string devices_platform_prefix = "/devices/platform/";
324 static const std::string devices_prefix = "/devices/";
325
Elliott Hughes579e6822017-12-20 09:41:00 -0800326 if (StartsWith(device, devices_platform_prefix)) {
Tom Cherry1ab8f552017-04-06 14:41:30 -0700327 device = device.substr(devices_platform_prefix.length());
Elliott Hughes579e6822017-12-20 09:41:00 -0800328 } else if (StartsWith(device, devices_prefix)) {
Tom Cherry1ab8f552017-04-06 14:41:30 -0700329 device = device.substr(devices_prefix.length());
330 }
331
Andrew Boiea885d042013-09-13 17:41:20 -0700332 type = "platform";
Tom Cherryed506f72017-05-25 15:58:59 -0700333 } else if (FindPciDevicePrefix(uevent.path, &device)) {
Andrew Boiea885d042013-09-13 17:41:20 -0700334 type = "pci";
Tom Cherryed506f72017-05-25 15:58:59 -0700335 } else if (FindVbdDevicePrefix(uevent.path, &device)) {
Jeremy Compostella937309d2017-03-03 16:27:29 +0100336 type = "vbd";
David Anderson924858c2019-06-26 17:00:00 -0700337 } else if (FindDmDevice(uevent.path, &partition, &uuid)) {
338 std::vector<std::string> symlinks = {"/dev/block/mapper/" + partition};
339 if (!uuid.empty()) {
340 symlinks.emplace_back("/dev/block/mapper/by-uuid/" + uuid);
341 }
342 return symlinks;
Andrew Boiea885d042013-09-13 17:41:20 -0700343 } else {
Tom Cherry2e344f92017-04-04 17:53:45 -0700344 return {};
Andrew Boiea885d042013-09-13 17:41:20 -0700345 }
Dima Zavinf395c922013-03-06 16:23:57 -0800346
Tom Cherry2e344f92017-04-04 17:53:45 -0700347 std::vector<std::string> links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700348
Sandeep Patil35403eb2017-02-08 20:27:12 -0800349 LOG(VERBOSE) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700350
Tom Cherry2e344f92017-04-04 17:53:45 -0700351 auto link_path = "/dev/block/" + type + "/" + device;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700352
Tom Cherry2c56d7c2018-12-20 14:36:49 -0800353 bool is_boot_device = boot_devices_.find(device) != boot_devices_.end();
Tom Cherryed506f72017-05-25 15:58:59 -0700354 if (!uevent.partition_name.empty()) {
355 std::string partition_name_sanitized(uevent.partition_name);
356 SanitizePartitionName(&partition_name_sanitized);
357 if (partition_name_sanitized != uevent.partition_name) {
358 LOG(VERBOSE) << "Linking partition '" << uevent.partition_name << "' as '"
Tom Cherry2e344f92017-04-04 17:53:45 -0700359 << partition_name_sanitized << "'";
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700360 }
Tom Cherry2e344f92017-04-04 17:53:45 -0700361 links.emplace_back(link_path + "/by-name/" + partition_name_sanitized);
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800362 // Adds symlink: /dev/block/by-name/<partition_name>.
Tom Cherry2c56d7c2018-12-20 14:36:49 -0800363 if (is_boot_device) {
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800364 links.emplace_back("/dev/block/by-name/" + partition_name_sanitized);
365 }
Tom Cherry2c56d7c2018-12-20 14:36:49 -0800366 } else if (is_boot_device) {
367 // If we don't have a partition name but we are a partition on a boot device, create a
368 // symlink of /dev/block/by-name/<device_name> for symmetry.
369 links.emplace_back("/dev/block/by-name/" + uevent.device_name);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700370 }
371
Tom Cherryed506f72017-05-25 15:58:59 -0700372 auto last_slash = uevent.path.rfind('/');
373 links.emplace_back(link_path + "/" + uevent.path.substr(last_slash + 1));
Colin Crossb0ab94b2010-04-08 16:16:20 -0700374
375 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700376}
377
David Anderson924858c2019-06-26 17:00:00 -0700378static void RemoveDeviceMapperLinks(const std::string& devpath) {
379 std::vector<std::string> dirs = {
380 "/dev/block/mapper",
381 "/dev/block/mapper/by-uuid",
382 };
383 for (const auto& dir : dirs) {
384 if (access(dir.c_str(), F_OK) != 0) continue;
385
386 std::unique_ptr<DIR, decltype(&closedir)> dh(opendir(dir.c_str()), closedir);
387 if (!dh) {
388 PLOG(ERROR) << "Failed to open directory " << dir;
389 continue;
390 }
391
392 struct dirent* dp;
393 std::string link_path;
394 while ((dp = readdir(dh.get())) != nullptr) {
395 if (dp->d_type != DT_LNK) continue;
396
397 auto path = dir + "/" + dp->d_name;
398 if (Readlink(path, &link_path) && link_path == devpath) {
399 unlink(path.c_str());
400 }
401 }
402 }
403}
404
Tom Cherryb4dd8812017-06-23 12:43:48 -0700405void DeviceHandler::HandleDevice(const std::string& action, const std::string& devpath, bool block,
Tom Cherryed506f72017-05-25 15:58:59 -0700406 int major, int minor, const std::vector<std::string>& links) const {
Tom Cherrye3e48212017-04-11 13:53:37 -0700407 if (action == "add") {
Tom Cherryed506f72017-05-25 15:58:59 -0700408 MakeDevice(devpath, block, major, minor, links);
David Anderson924858c2019-06-26 17:00:00 -0700409 }
410
411 // We don't have full device-mapper information until a change event is fired.
412 if (action == "add" || (action == "change" && StartsWith(devpath, "/dev/block/dm-"))) {
Tom Cherry2e344f92017-04-04 17:53:45 -0700413 for (const auto& link : links) {
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700414 if (!mkdir_recursive(Dirname(link), 0755)) {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700415 PLOG(ERROR) << "Failed to create directory " << Dirname(link);
Tom Cherryed506f72017-05-25 15:58:59 -0700416 }
417
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800418 if (symlink(devpath.c_str(), link.c_str())) {
419 if (errno != EEXIST) {
420 PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link;
421 } else if (std::string link_path;
422 Readlink(link, &link_path) && link_path != devpath) {
423 PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link
424 << ", which already links to: " << link_path;
425 }
Tom Cherryed506f72017-05-25 15:58:59 -0700426 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700427 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700428 }
429
Tom Cherrye3e48212017-04-11 13:53:37 -0700430 if (action == "remove") {
David Anderson924858c2019-06-26 17:00:00 -0700431 if (StartsWith(devpath, "/dev/block/dm-")) {
432 RemoveDeviceMapperLinks(devpath);
433 }
Tom Cherry2e344f92017-04-04 17:53:45 -0700434 for (const auto& link : links) {
Tom Cherryed506f72017-05-25 15:58:59 -0700435 std::string link_path;
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700436 if (Readlink(link, &link_path) && link_path == devpath) {
Tom Cherryed506f72017-05-25 15:58:59 -0700437 unlink(link.c_str());
438 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700439 }
Tom Cherrye3e48212017-04-11 13:53:37 -0700440 unlink(devpath.c_str());
Colin Crossb0ab94b2010-04-08 16:16:20 -0700441 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700442}
443
Tom Cherry457e28f2018-08-01 13:12:20 -0700444void DeviceHandler::HandleUevent(const Uevent& uevent) {
Tom Cherryb4dd8812017-06-23 12:43:48 -0700445 if (uevent.action == "add" || uevent.action == "change" || uevent.action == "online") {
446 FixupSysPermissions(uevent.path, uevent.subsystem);
Tom Cherrye3e48212017-04-11 13:53:37 -0700447 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700448
Tom Cherry3fa46732017-04-11 14:19:50 -0700449 // if it's not a /dev device, nothing to do
Tom Cherryed506f72017-05-25 15:58:59 -0700450 if (uevent.major < 0 || uevent.minor < 0) return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800451
Tom Cherry3fa46732017-04-11 14:19:50 -0700452 std::string devpath;
Tom Cherryb4dd8812017-06-23 12:43:48 -0700453 std::vector<std::string> links;
454 bool block = false;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800455
Tom Cherryb4dd8812017-06-23 12:43:48 -0700456 if (uevent.subsystem == "block") {
457 block = true;
458 devpath = "/dev/block/" + Basename(uevent.path);
459
460 if (StartsWith(uevent.path, "/devices")) {
461 links = GetBlockDeviceSymlinks(uevent);
462 }
Tom Cherryed506f72017-05-25 15:58:59 -0700463 } else if (const auto subsystem =
464 std::find(subsystems_.cbegin(), subsystems_.cend(), uevent.subsystem);
465 subsystem != subsystems_.cend()) {
Tom Cherryfe062052017-04-24 16:59:05 -0700466 devpath = subsystem->ParseDevPath(uevent);
Tom Cherry9c8d6dd2017-08-17 09:38:01 -0700467 } else if (uevent.subsystem == "usb") {
468 if (!uevent.device_name.empty()) {
469 devpath = "/dev/" + uevent.device_name;
470 } else {
471 // This imitates the file system that would be created
472 // if we were using devfs instead.
473 // Minors are broken up into groups of 128, starting at "001"
474 int bus_id = uevent.minor / 128 + 1;
475 int device_id = uevent.minor % 128 + 1;
476 devpath = StringPrintf("/dev/bus/usb/%03d/%03d", bus_id, device_id);
477 }
478 } else if (StartsWith(uevent.subsystem, "usb")) {
479 // ignore other USB events
480 return;
Tom Cherry780a71e2017-04-04 16:30:40 -0700481 } else {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700482 devpath = "/dev/" + Basename(uevent.path);
Tom Cherry780a71e2017-04-04 16:30:40 -0700483 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700484
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700485 mkdir_recursive(Dirname(devpath), 0755);
Tom Cherryfe062052017-04-24 16:59:05 -0700486
Tom Cherryb4dd8812017-06-23 12:43:48 -0700487 HandleDevice(uevent.action, devpath, block, uevent.major, uevent.minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700488}
489
Tom Cherry457e28f2018-08-01 13:12:20 -0700490void DeviceHandler::ColdbootDone() {
Oleksiy Avramchenkodd5802a2018-11-02 10:06:47 +0100491 skip_restorecon_ = false;
Tom Cherry457e28f2018-08-01 13:12:20 -0700492}
493
Tom Cherryed506f72017-05-25 15:58:59 -0700494DeviceHandler::DeviceHandler(std::vector<Permissions> dev_permissions,
495 std::vector<SysfsPermissions> sysfs_permissions,
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800496 std::vector<Subsystem> subsystems, std::set<std::string> boot_devices,
497 bool skip_restorecon)
Tom Cherryed506f72017-05-25 15:58:59 -0700498 : dev_permissions_(std::move(dev_permissions)),
499 sysfs_permissions_(std::move(sysfs_permissions)),
500 subsystems_(std::move(subsystems)),
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800501 boot_devices_(std::move(boot_devices)),
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700502 skip_restorecon_(skip_restorecon),
503 sysfs_mount_point_("/sys") {}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700504
Tom Cherryed506f72017-05-25 15:58:59 -0700505DeviceHandler::DeviceHandler()
506 : DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{},
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800507 std::vector<Subsystem>{}, std::set<std::string>{}, false) {}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700508
509} // namespace init
510} // namespace android