blob: 159c75e03e799a082d6a52d515ca1199b08a22e9 [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>
25#include <map>
James Hawkins588a2ca2016-02-18 14:52:46 -080026#include <memory>
Mark Salyzyne419a792019-03-06 15:18:46 -080027#include <string>
28#include <thread>
James Hawkins588a2ca2016-02-18 14:52:46 -080029
Mark Salyzyne419a792019-03-06 15:18:46 -080030#include <android-base/chrono_utils.h>
31#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070032#include <android-base/logging.h>
Rob Herring6de783a2016-05-06 10:06:59 -050033#include <android-base/stringprintf.h>
Tom Cherry2e344f92017-04-04 17:53:45 -070034#include <android-base/strings.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070035#include <private/android_filesystem_config.h>
36#include <selinux/android.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070037#include <selinux/selinux.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100038
Tom Cherry0c8d6d22017-08-10 12:22:44 -070039#include "selinux.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070040#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041
Tom Cherrye7656b72017-05-01 17:10:09 -070042#ifdef _INIT_INIT_H
Tom Cherry40acb372018-08-01 13:41:12 -070043#error "Do not include init.h in files used by ueventd; it will expose init's globals"
Tom Cherrye7656b72017-05-01 17:10:09 -070044#endif
45
Mark Salyzyne419a792019-03-06 15:18:46 -080046using namespace std::chrono_literals;
47
Tom Cherry81f5d3e2017-06-22 12:53:17 -070048using android::base::Basename;
49using android::base::Dirname;
Mark Salyzyne419a792019-03-06 15:18:46 -080050using android::base::ReadFileToString;
Tom Cherry81f5d3e2017-06-22 12:53:17 -070051using android::base::Readlink;
52using android::base::Realpath;
53using android::base::StartsWith;
54using android::base::StringPrintf;
Mark Salyzyne419a792019-03-06 15:18:46 -080055using android::base::Trim;
Tom Cherry81f5d3e2017-06-22 12:53:17 -070056
57namespace android {
58namespace init {
59
Andrew Boiea885d042013-09-13 17:41:20 -070060/* Given a path that may start with a PCI device, populate the supplied buffer
61 * with the PCI domain/bus number and the peripheral ID and return 0.
62 * If it doesn't start with a PCI device, or there is some error, return -1 */
Tom Cherryed506f72017-05-25 15:58:59 -070063static bool FindPciDevicePrefix(const std::string& path, std::string* result) {
Tom Cherry2e344f92017-04-04 17:53:45 -070064 result->clear();
Andrew Boiea885d042013-09-13 17:41:20 -070065
Tom Cherry81f5d3e2017-06-22 12:53:17 -070066 if (!StartsWith(path, "/devices/pci")) return false;
Andrew Boiea885d042013-09-13 17:41:20 -070067
68 /* Beginning of the prefix is the initial "pci" after "/devices/" */
Tom Cherry2e344f92017-04-04 17:53:45 -070069 std::string::size_type start = 9;
Andrew Boiea885d042013-09-13 17:41:20 -070070
71 /* End of the prefix is two path '/' later, capturing the domain/bus number
72 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
Tom Cherry2e344f92017-04-04 17:53:45 -070073 auto end = path.find('/', start);
74 if (end == std::string::npos) return false;
Andrew Boiea885d042013-09-13 17:41:20 -070075
Tom Cherry2e344f92017-04-04 17:53:45 -070076 end = path.find('/', end + 1);
77 if (end == std::string::npos) return false;
Andrew Boiea885d042013-09-13 17:41:20 -070078
Tom Cherry2e344f92017-04-04 17:53:45 -070079 auto length = end - start;
80 if (length <= 4) {
81 // The minimum string that will get to this check is 'pci/', which is malformed,
82 // so return false
83 return false;
84 }
85
86 *result = path.substr(start, length);
87 return true;
Andrew Boiea885d042013-09-13 17:41:20 -070088}
89
Jeremy Compostella937309d2017-03-03 16:27:29 +010090/* Given a path that may start with a virtual block device, populate
91 * the supplied buffer with the virtual block device ID and return 0.
92 * If it doesn't start with a virtual block device, or there is some
93 * error, return -1 */
Tom Cherryed506f72017-05-25 15:58:59 -070094static bool FindVbdDevicePrefix(const std::string& path, std::string* result) {
Tom Cherry2e344f92017-04-04 17:53:45 -070095 result->clear();
96
Tom Cherry81f5d3e2017-06-22 12:53:17 -070097 if (!StartsWith(path, "/devices/vbd-")) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +010098
99 /* Beginning of the prefix is the initial "vbd-" after "/devices/" */
Tom Cherry2e344f92017-04-04 17:53:45 -0700100 std::string::size_type start = 13;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100101
102 /* End of the prefix is one path '/' later, capturing the
103 virtual block device ID. Example: 768 */
Tom Cherry2e344f92017-04-04 17:53:45 -0700104 auto end = path.find('/', start);
105 if (end == std::string::npos) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100106
Tom Cherry2e344f92017-04-04 17:53:45 -0700107 auto length = end - start;
108 if (length == 0) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100109
Tom Cherry2e344f92017-04-04 17:53:45 -0700110 *result = path.substr(start, length);
111 return true;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100112}
113
Mark Salyzyne419a792019-03-06 15:18:46 -0800114// Given a path that may start with a virtual dm block device, populate
115// the supplied buffer with the dm module's instantiated name.
116// If it doesn't start with a virtual block device, or there is some
117// error, return false.
118static bool FindDmDevicePartition(const std::string& path, std::string* result) {
119 result->clear();
120 if (!StartsWith(path, "/devices/virtual/block/dm-")) return false;
121 if (getpid() == 1) return false; // first_stage_init has no sepolicy needs
122
123 static std::map<std::string, std::string> cache;
124 // wait_for_file will not work, the content is also delayed ...
125 for (android::base::Timer t; t.duration() < 200ms; std::this_thread::sleep_for(10ms)) {
126 if (ReadFileToString("/sys" + path + "/dm/name", result) && !result->empty()) {
127 // Got it, set cache with result, when node arrives
128 cache[path] = *result = Trim(*result);
129 return true;
130 }
131 }
132 auto it = cache.find(path);
133 if ((it == cache.end()) || (it->second.empty())) return false;
134 // Return cached results, when node goes away
135 *result = it->second;
136 return true;
137}
138
Tom Cherryed506f72017-05-25 15:58:59 -0700139Permissions::Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid)
140 : name_(name), perm_(perm), uid_(uid), gid_(gid), prefix_(false), wildcard_(false) {
141 // Set 'prefix_' or 'wildcard_' based on the below cases:
142 //
143 // 1) No '*' in 'name' -> Neither are set and Match() checks a given path for strict
144 // equality with 'name'
145 //
146 // 2) '*' only appears as the last character in 'name' -> 'prefix'_ is set to true and
147 // Match() checks if 'name' is a prefix of a given path.
148 //
149 // 3) '*' appears elsewhere -> 'wildcard_' is set to true and Match() uses fnmatch()
150 // with FNM_PATHNAME to compare 'name' to a given path.
151
152 auto wildcard_position = name_.find('*');
153 if (wildcard_position != std::string::npos) {
154 if (wildcard_position == name_.length() - 1) {
155 prefix_ = true;
156 name_.pop_back();
157 } else {
158 wildcard_ = true;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700159 }
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800160 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700161}
162
Tom Cherryed506f72017-05-25 15:58:59 -0700163bool Permissions::Match(const std::string& path) const {
Elliott Hughes579e6822017-12-20 09:41:00 -0800164 if (prefix_) return StartsWith(path, name_);
Tom Cherryed506f72017-05-25 15:58:59 -0700165 if (wildcard_) return fnmatch(name_.c_str(), path.c_str(), FNM_PATHNAME) == 0;
166 return path == name_;
167}
168
169bool SysfsPermissions::MatchWithSubsystem(const std::string& path,
170 const std::string& subsystem) const {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700171 std::string path_basename = Basename(path);
Tom Cherryed506f72017-05-25 15:58:59 -0700172 if (name().find(subsystem) != std::string::npos) {
173 if (Match("/sys/class/" + subsystem + "/" + path_basename)) return true;
174 if (Match("/sys/bus/" + subsystem + "/devices/" + path_basename)) return true;
175 }
176 return Match(path);
177}
178
179void SysfsPermissions::SetPermissions(const std::string& path) const {
180 std::string attribute_file = path + "/" + attribute_;
181 LOG(VERBOSE) << "fixup " << attribute_file << " " << uid() << " " << gid() << " " << std::oct
182 << perm();
183
184 if (access(attribute_file.c_str(), F_OK) == 0) {
185 if (chown(attribute_file.c_str(), uid(), gid()) != 0) {
186 PLOG(ERROR) << "chown(" << attribute_file << ", " << uid() << ", " << gid()
187 << ") failed";
188 }
189 if (chmod(attribute_file.c_str(), perm()) != 0) {
190 PLOG(ERROR) << "chmod(" << attribute_file << ", " << perm() << ") failed";
191 }
192 }
193}
194
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700195// Given a path that may start with a platform device, find the parent platform device by finding a
196// parent directory with a 'subsystem' symlink that points to the platform bus.
197// If it doesn't start with a platform device, return false
198bool DeviceHandler::FindPlatformDevice(std::string path, std::string* platform_device_path) const {
199 platform_device_path->clear();
200
201 // Uevents don't contain the mount point, so we need to add it here.
202 path.insert(0, sysfs_mount_point_);
203
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700204 std::string directory = Dirname(path);
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700205
206 while (directory != "/" && directory != ".") {
207 std::string subsystem_link_path;
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700208 if (Realpath(directory + "/subsystem", &subsystem_link_path) &&
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700209 subsystem_link_path == sysfs_mount_point_ + "/bus/platform") {
210 // We need to remove the mount point that we added above before returning.
211 directory.erase(0, sysfs_mount_point_.size());
212 *platform_device_path = directory;
Tom Cherryed506f72017-05-25 15:58:59 -0700213 return true;
214 }
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700215
216 auto last_slash = path.rfind('/');
217 if (last_slash == std::string::npos) return false;
218
219 path.erase(last_slash);
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700220 directory = Dirname(path);
Tom Cherryed506f72017-05-25 15:58:59 -0700221 }
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700222
Tom Cherryed506f72017-05-25 15:58:59 -0700223 return false;
224}
225
226void DeviceHandler::FixupSysPermissions(const std::string& upath,
227 const std::string& subsystem) const {
228 // upaths omit the "/sys" that paths in this list
229 // contain, so we prepend it...
230 std::string path = "/sys" + upath;
231
232 for (const auto& s : sysfs_permissions_) {
233 if (s.MatchWithSubsystem(path, subsystem)) s.SetPermissions(path);
234 }
235
Tom Cherryc5833052017-05-16 15:35:41 -0700236 if (!skip_restorecon_ && access(path.c_str(), F_OK) == 0) {
Tom Cherryed506f72017-05-25 15:58:59 -0700237 LOG(VERBOSE) << "restorecon_recursive: " << path;
238 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
239 PLOG(ERROR) << "selinux_android_restorecon(" << path << ") failed";
240 }
241 }
242}
243
244std::tuple<mode_t, uid_t, gid_t> DeviceHandler::GetDevicePermissions(
245 const std::string& path, const std::vector<std::string>& links) const {
246 // Search the perms list in reverse so that ueventd.$hardware can override ueventd.rc.
247 for (auto it = dev_permissions_.crbegin(); it != dev_permissions_.crend(); ++it) {
248 if (it->Match(path) || std::any_of(links.cbegin(), links.cend(),
249 [it](const auto& link) { return it->Match(link); })) {
250 return {it->perm(), it->uid(), it->gid()};
251 }
252 }
253 /* Default if nothing found. */
254 return {0600, 0, 0};
255}
256
Tom Cherryb4dd8812017-06-23 12:43:48 -0700257void DeviceHandler::MakeDevice(const std::string& path, bool block, int major, int minor,
Tom Cherryed506f72017-05-25 15:58:59 -0700258 const std::vector<std::string>& links) const {
259 auto[mode, uid, gid] = GetDevicePermissions(path, links);
260 mode |= (block ? S_IFBLK : S_IFCHR);
261
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700262 std::string secontext;
263 if (!SelabelLookupFileContextBestMatch(path, links, mode, &secontext)) {
264 PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
265 return;
266 }
267 if (!secontext.empty()) {
268 setfscreatecon(secontext.c_str());
Tom Cherryed506f72017-05-25 15:58:59 -0700269 }
270
271 dev_t dev = makedev(major, minor);
272 /* Temporarily change egid to avoid race condition setting the gid of the
273 * device node. Unforunately changing the euid would prevent creation of
274 * some device nodes, so the uid has to be set with chown() and is still
275 * racy. Fixing the gid race at least fixed the issue with system_server
276 * opening dynamic input devices under the AID_INPUT gid. */
277 if (setegid(gid)) {
278 PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed";
279 goto out;
280 }
281 /* If the node already exists update its SELinux label to handle cases when
282 * it was created with the wrong context during coldboot procedure. */
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700283 if (mknod(path.c_str(), mode, dev) && (errno == EEXIST) && !secontext.empty()) {
Tom Cherryed506f72017-05-25 15:58:59 -0700284 char* fcon = nullptr;
285 int rc = lgetfilecon(path.c_str(), &fcon);
286 if (rc < 0) {
287 PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
288 goto out;
289 }
290
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700291 bool different = fcon != secontext;
Tom Cherryed506f72017-05-25 15:58:59 -0700292 freecon(fcon);
293
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700294 if (different && lsetfilecon(path.c_str(), secontext.c_str())) {
Tom Cherryed506f72017-05-25 15:58:59 -0700295 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path
296 << "' device";
297 }
298 }
299
300out:
301 chown(path.c_str(), uid, -1);
302 if (setegid(AID_ROOT)) {
303 PLOG(FATAL) << "setegid(AID_ROOT) failed";
304 }
305
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700306 if (!secontext.empty()) {
Tom Cherryed506f72017-05-25 15:58:59 -0700307 setfscreatecon(nullptr);
308 }
309}
310
Tom Cherryc44f6a42017-04-05 15:58:31 -0700311// replaces any unacceptable characters with '_', the
312// length of the resulting string is equal to the input string
Tom Cherryed506f72017-05-25 15:58:59 -0700313void SanitizePartitionName(std::string* string) {
Tom Cherryc44f6a42017-04-05 15:58:31 -0700314 const char* accept =
315 "abcdefghijklmnopqrstuvwxyz"
316 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
317 "0123456789"
318 "_-.";
319
Tom Cherry2e344f92017-04-04 17:53:45 -0700320 if (!string) return;
Tom Cherryc44f6a42017-04-05 15:58:31 -0700321
Tom Cherry2e344f92017-04-04 17:53:45 -0700322 std::string::size_type pos = 0;
323 while ((pos = string->find_first_not_of(accept, pos)) != std::string::npos) {
324 (*string)[pos] = '_';
Tom Cherryc44f6a42017-04-05 15:58:31 -0700325 }
326}
327
Tom Cherryed506f72017-05-25 15:58:59 -0700328std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uevent) const {
Tom Cherry2e344f92017-04-04 17:53:45 -0700329 std::string device;
Tom Cherry2e344f92017-04-04 17:53:45 -0700330 std::string type;
Mark Salyzyne419a792019-03-06 15:18:46 -0800331 std::string partition;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700332
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700333 if (FindPlatformDevice(uevent.path, &device)) {
Tom Cherry1ab8f552017-04-06 14:41:30 -0700334 // Skip /devices/platform or /devices/ if present
335 static const std::string devices_platform_prefix = "/devices/platform/";
336 static const std::string devices_prefix = "/devices/";
337
Elliott Hughes579e6822017-12-20 09:41:00 -0800338 if (StartsWith(device, devices_platform_prefix)) {
Tom Cherry1ab8f552017-04-06 14:41:30 -0700339 device = device.substr(devices_platform_prefix.length());
Elliott Hughes579e6822017-12-20 09:41:00 -0800340 } else if (StartsWith(device, devices_prefix)) {
Tom Cherry1ab8f552017-04-06 14:41:30 -0700341 device = device.substr(devices_prefix.length());
342 }
343
Andrew Boiea885d042013-09-13 17:41:20 -0700344 type = "platform";
Tom Cherryed506f72017-05-25 15:58:59 -0700345 } else if (FindPciDevicePrefix(uevent.path, &device)) {
Andrew Boiea885d042013-09-13 17:41:20 -0700346 type = "pci";
Tom Cherryed506f72017-05-25 15:58:59 -0700347 } else if (FindVbdDevicePrefix(uevent.path, &device)) {
Jeremy Compostella937309d2017-03-03 16:27:29 +0100348 type = "vbd";
Mark Salyzyne419a792019-03-06 15:18:46 -0800349 } else if (FindDmDevicePartition(uevent.path, &partition)) {
350 return {"/dev/block/mapper/" + partition};
Andrew Boiea885d042013-09-13 17:41:20 -0700351 } else {
Tom Cherry2e344f92017-04-04 17:53:45 -0700352 return {};
Andrew Boiea885d042013-09-13 17:41:20 -0700353 }
Dima Zavinf395c922013-03-06 16:23:57 -0800354
Tom Cherry2e344f92017-04-04 17:53:45 -0700355 std::vector<std::string> links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700356
Sandeep Patil35403eb2017-02-08 20:27:12 -0800357 LOG(VERBOSE) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700358
Tom Cherry2e344f92017-04-04 17:53:45 -0700359 auto link_path = "/dev/block/" + type + "/" + device;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700360
Tom Cherry2c56d7c2018-12-20 14:36:49 -0800361 bool is_boot_device = boot_devices_.find(device) != boot_devices_.end();
Tom Cherryed506f72017-05-25 15:58:59 -0700362 if (!uevent.partition_name.empty()) {
363 std::string partition_name_sanitized(uevent.partition_name);
364 SanitizePartitionName(&partition_name_sanitized);
365 if (partition_name_sanitized != uevent.partition_name) {
366 LOG(VERBOSE) << "Linking partition '" << uevent.partition_name << "' as '"
Tom Cherry2e344f92017-04-04 17:53:45 -0700367 << partition_name_sanitized << "'";
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700368 }
Tom Cherry2e344f92017-04-04 17:53:45 -0700369 links.emplace_back(link_path + "/by-name/" + partition_name_sanitized);
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800370 // Adds symlink: /dev/block/by-name/<partition_name>.
Tom Cherry2c56d7c2018-12-20 14:36:49 -0800371 if (is_boot_device) {
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800372 links.emplace_back("/dev/block/by-name/" + partition_name_sanitized);
373 }
Tom Cherry2c56d7c2018-12-20 14:36:49 -0800374 } else if (is_boot_device) {
375 // If we don't have a partition name but we are a partition on a boot device, create a
376 // symlink of /dev/block/by-name/<device_name> for symmetry.
377 links.emplace_back("/dev/block/by-name/" + uevent.device_name);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700378 }
379
Tom Cherryed506f72017-05-25 15:58:59 -0700380 auto last_slash = uevent.path.rfind('/');
381 links.emplace_back(link_path + "/" + uevent.path.substr(last_slash + 1));
Colin Crossb0ab94b2010-04-08 16:16:20 -0700382
383 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700384}
385
Tom Cherryb4dd8812017-06-23 12:43:48 -0700386void DeviceHandler::HandleDevice(const std::string& action, const std::string& devpath, bool block,
Tom Cherryed506f72017-05-25 15:58:59 -0700387 int major, int minor, const std::vector<std::string>& links) const {
Tom Cherrye3e48212017-04-11 13:53:37 -0700388 if (action == "add") {
Tom Cherryed506f72017-05-25 15:58:59 -0700389 MakeDevice(devpath, block, major, minor, links);
Tom Cherry2e344f92017-04-04 17:53:45 -0700390 for (const auto& link : links) {
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700391 if (!mkdir_recursive(Dirname(link), 0755)) {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700392 PLOG(ERROR) << "Failed to create directory " << Dirname(link);
Tom Cherryed506f72017-05-25 15:58:59 -0700393 }
394
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800395 if (symlink(devpath.c_str(), link.c_str())) {
396 if (errno != EEXIST) {
397 PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link;
398 } else if (std::string link_path;
399 Readlink(link, &link_path) && link_path != devpath) {
400 PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link
401 << ", which already links to: " << link_path;
402 }
Tom Cherryed506f72017-05-25 15:58:59 -0700403 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700404 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700405 }
406
Tom Cherrye3e48212017-04-11 13:53:37 -0700407 if (action == "remove") {
Tom Cherry2e344f92017-04-04 17:53:45 -0700408 for (const auto& link : links) {
Tom Cherryed506f72017-05-25 15:58:59 -0700409 std::string link_path;
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700410 if (Readlink(link, &link_path) && link_path == devpath) {
Tom Cherryed506f72017-05-25 15:58:59 -0700411 unlink(link.c_str());
412 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700413 }
Tom Cherrye3e48212017-04-11 13:53:37 -0700414 unlink(devpath.c_str());
Colin Crossb0ab94b2010-04-08 16:16:20 -0700415 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700416}
417
Tom Cherry457e28f2018-08-01 13:12:20 -0700418void DeviceHandler::HandleUevent(const Uevent& uevent) {
Tom Cherryb4dd8812017-06-23 12:43:48 -0700419 if (uevent.action == "add" || uevent.action == "change" || uevent.action == "online") {
420 FixupSysPermissions(uevent.path, uevent.subsystem);
Tom Cherrye3e48212017-04-11 13:53:37 -0700421 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700422
Tom Cherry3fa46732017-04-11 14:19:50 -0700423 // if it's not a /dev device, nothing to do
Tom Cherryed506f72017-05-25 15:58:59 -0700424 if (uevent.major < 0 || uevent.minor < 0) return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800425
Tom Cherry3fa46732017-04-11 14:19:50 -0700426 std::string devpath;
Tom Cherryb4dd8812017-06-23 12:43:48 -0700427 std::vector<std::string> links;
428 bool block = false;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800429
Tom Cherryb4dd8812017-06-23 12:43:48 -0700430 if (uevent.subsystem == "block") {
431 block = true;
432 devpath = "/dev/block/" + Basename(uevent.path);
433
434 if (StartsWith(uevent.path, "/devices")) {
435 links = GetBlockDeviceSymlinks(uevent);
436 }
Tom Cherryed506f72017-05-25 15:58:59 -0700437 } else if (const auto subsystem =
438 std::find(subsystems_.cbegin(), subsystems_.cend(), uevent.subsystem);
439 subsystem != subsystems_.cend()) {
Tom Cherryfe062052017-04-24 16:59:05 -0700440 devpath = subsystem->ParseDevPath(uevent);
Tom Cherry9c8d6dd2017-08-17 09:38:01 -0700441 } else if (uevent.subsystem == "usb") {
442 if (!uevent.device_name.empty()) {
443 devpath = "/dev/" + uevent.device_name;
444 } else {
445 // This imitates the file system that would be created
446 // if we were using devfs instead.
447 // Minors are broken up into groups of 128, starting at "001"
448 int bus_id = uevent.minor / 128 + 1;
449 int device_id = uevent.minor % 128 + 1;
450 devpath = StringPrintf("/dev/bus/usb/%03d/%03d", bus_id, device_id);
451 }
452 } else if (StartsWith(uevent.subsystem, "usb")) {
453 // ignore other USB events
454 return;
Tom Cherry780a71e2017-04-04 16:30:40 -0700455 } else {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700456 devpath = "/dev/" + Basename(uevent.path);
Tom Cherry780a71e2017-04-04 16:30:40 -0700457 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700458
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700459 mkdir_recursive(Dirname(devpath), 0755);
Tom Cherryfe062052017-04-24 16:59:05 -0700460
Tom Cherryb4dd8812017-06-23 12:43:48 -0700461 HandleDevice(uevent.action, devpath, block, uevent.major, uevent.minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700462}
463
Tom Cherry457e28f2018-08-01 13:12:20 -0700464void DeviceHandler::ColdbootDone() {
Oleksiy Avramchenkodd5802a2018-11-02 10:06:47 +0100465 skip_restorecon_ = false;
Tom Cherry457e28f2018-08-01 13:12:20 -0700466}
467
Tom Cherryed506f72017-05-25 15:58:59 -0700468DeviceHandler::DeviceHandler(std::vector<Permissions> dev_permissions,
469 std::vector<SysfsPermissions> sysfs_permissions,
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800470 std::vector<Subsystem> subsystems, std::set<std::string> boot_devices,
471 bool skip_restorecon)
Tom Cherryed506f72017-05-25 15:58:59 -0700472 : dev_permissions_(std::move(dev_permissions)),
473 sysfs_permissions_(std::move(sysfs_permissions)),
474 subsystems_(std::move(subsystems)),
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800475 boot_devices_(std::move(boot_devices)),
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700476 skip_restorecon_(skip_restorecon),
477 sysfs_mount_point_("/sys") {}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700478
Tom Cherryed506f72017-05-25 15:58:59 -0700479DeviceHandler::DeviceHandler()
480 : DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{},
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800481 std::vector<Subsystem>{}, std::set<std::string>{}, false) {}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700482
483} // namespace init
484} // namespace android