blob: e8e6cd788eb8f2eb786e70e5c621a80db154c8b7 [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
Vic Yang92c236e2019-05-28 15:58:35 -070039#include "selabel.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070040#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041
Mark Salyzyne419a792019-03-06 15:18:46 -080042using namespace std::chrono_literals;
43
Tom Cherry81f5d3e2017-06-22 12:53:17 -070044using android::base::Basename;
45using android::base::Dirname;
Mark Salyzyne419a792019-03-06 15:18:46 -080046using android::base::ReadFileToString;
Tom Cherry81f5d3e2017-06-22 12:53:17 -070047using android::base::Readlink;
48using android::base::Realpath;
49using android::base::StartsWith;
50using android::base::StringPrintf;
Mark Salyzyne419a792019-03-06 15:18:46 -080051using android::base::Trim;
Tom Cherry81f5d3e2017-06-22 12:53:17 -070052
53namespace android {
54namespace init {
55
Andrew Boiea885d042013-09-13 17:41:20 -070056/* Given a path that may start with a PCI device, populate the supplied buffer
57 * with the PCI domain/bus number and the peripheral ID and return 0.
58 * If it doesn't start with a PCI device, or there is some error, return -1 */
Tom Cherryed506f72017-05-25 15:58:59 -070059static bool FindPciDevicePrefix(const std::string& path, std::string* result) {
Tom Cherry2e344f92017-04-04 17:53:45 -070060 result->clear();
Andrew Boiea885d042013-09-13 17:41:20 -070061
Tom Cherry81f5d3e2017-06-22 12:53:17 -070062 if (!StartsWith(path, "/devices/pci")) return false;
Andrew Boiea885d042013-09-13 17:41:20 -070063
64 /* Beginning of the prefix is the initial "pci" after "/devices/" */
Tom Cherry2e344f92017-04-04 17:53:45 -070065 std::string::size_type start = 9;
Andrew Boiea885d042013-09-13 17:41:20 -070066
67 /* End of the prefix is two path '/' later, capturing the domain/bus number
68 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
Tom Cherry2e344f92017-04-04 17:53:45 -070069 auto end = path.find('/', start);
70 if (end == std::string::npos) return false;
Andrew Boiea885d042013-09-13 17:41:20 -070071
Tom Cherry2e344f92017-04-04 17:53:45 -070072 end = path.find('/', end + 1);
73 if (end == std::string::npos) return false;
Andrew Boiea885d042013-09-13 17:41:20 -070074
Tom Cherry2e344f92017-04-04 17:53:45 -070075 auto length = end - start;
76 if (length <= 4) {
77 // The minimum string that will get to this check is 'pci/', which is malformed,
78 // so return false
79 return false;
80 }
81
82 *result = path.substr(start, length);
83 return true;
Andrew Boiea885d042013-09-13 17:41:20 -070084}
85
Jeremy Compostella937309d2017-03-03 16:27:29 +010086/* Given a path that may start with a virtual block device, populate
87 * the supplied buffer with the virtual block device ID and return 0.
88 * If it doesn't start with a virtual block device, or there is some
89 * error, return -1 */
Tom Cherryed506f72017-05-25 15:58:59 -070090static bool FindVbdDevicePrefix(const std::string& path, std::string* result) {
Tom Cherry2e344f92017-04-04 17:53:45 -070091 result->clear();
92
Tom Cherry81f5d3e2017-06-22 12:53:17 -070093 if (!StartsWith(path, "/devices/vbd-")) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +010094
95 /* Beginning of the prefix is the initial "vbd-" after "/devices/" */
Tom Cherry2e344f92017-04-04 17:53:45 -070096 std::string::size_type start = 13;
Jeremy Compostella937309d2017-03-03 16:27:29 +010097
98 /* End of the prefix is one path '/' later, capturing the
99 virtual block device ID. Example: 768 */
Tom Cherry2e344f92017-04-04 17:53:45 -0700100 auto end = path.find('/', start);
101 if (end == std::string::npos) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100102
Tom Cherry2e344f92017-04-04 17:53:45 -0700103 auto length = end - start;
104 if (length == 0) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100105
Tom Cherry2e344f92017-04-04 17:53:45 -0700106 *result = path.substr(start, length);
107 return true;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100108}
109
Mark Salyzyne419a792019-03-06 15:18:46 -0800110// Given a path that may start with a virtual dm block device, populate
111// the supplied buffer with the dm module's instantiated name.
112// If it doesn't start with a virtual block device, or there is some
113// error, return false.
114static bool FindDmDevicePartition(const std::string& path, std::string* result) {
115 result->clear();
116 if (!StartsWith(path, "/devices/virtual/block/dm-")) return false;
117 if (getpid() == 1) return false; // first_stage_init has no sepolicy needs
118
119 static std::map<std::string, std::string> cache;
120 // wait_for_file will not work, the content is also delayed ...
121 for (android::base::Timer t; t.duration() < 200ms; std::this_thread::sleep_for(10ms)) {
122 if (ReadFileToString("/sys" + path + "/dm/name", result) && !result->empty()) {
123 // Got it, set cache with result, when node arrives
124 cache[path] = *result = Trim(*result);
125 return true;
126 }
127 }
128 auto it = cache.find(path);
129 if ((it == cache.end()) || (it->second.empty())) return false;
130 // Return cached results, when node goes away
131 *result = it->second;
132 return true;
133}
134
Tom Cherryed506f72017-05-25 15:58:59 -0700135Permissions::Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid)
136 : name_(name), perm_(perm), uid_(uid), gid_(gid), prefix_(false), wildcard_(false) {
137 // Set 'prefix_' or 'wildcard_' based on the below cases:
138 //
139 // 1) No '*' in 'name' -> Neither are set and Match() checks a given path for strict
140 // equality with 'name'
141 //
142 // 2) '*' only appears as the last character in 'name' -> 'prefix'_ is set to true and
143 // Match() checks if 'name' is a prefix of a given path.
144 //
145 // 3) '*' appears elsewhere -> 'wildcard_' is set to true and Match() uses fnmatch()
146 // with FNM_PATHNAME to compare 'name' to a given path.
147
148 auto wildcard_position = name_.find('*');
149 if (wildcard_position != std::string::npos) {
150 if (wildcard_position == name_.length() - 1) {
151 prefix_ = true;
152 name_.pop_back();
153 } else {
154 wildcard_ = true;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700155 }
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800156 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700157}
158
Tom Cherryed506f72017-05-25 15:58:59 -0700159bool Permissions::Match(const std::string& path) const {
Elliott Hughes579e6822017-12-20 09:41:00 -0800160 if (prefix_) return StartsWith(path, name_);
Tom Cherryed506f72017-05-25 15:58:59 -0700161 if (wildcard_) return fnmatch(name_.c_str(), path.c_str(), FNM_PATHNAME) == 0;
162 return path == name_;
163}
164
165bool SysfsPermissions::MatchWithSubsystem(const std::string& path,
166 const std::string& subsystem) const {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700167 std::string path_basename = Basename(path);
Tom Cherryed506f72017-05-25 15:58:59 -0700168 if (name().find(subsystem) != std::string::npos) {
169 if (Match("/sys/class/" + subsystem + "/" + path_basename)) return true;
170 if (Match("/sys/bus/" + subsystem + "/devices/" + path_basename)) return true;
171 }
172 return Match(path);
173}
174
175void SysfsPermissions::SetPermissions(const std::string& path) const {
176 std::string attribute_file = path + "/" + attribute_;
177 LOG(VERBOSE) << "fixup " << attribute_file << " " << uid() << " " << gid() << " " << std::oct
178 << perm();
179
180 if (access(attribute_file.c_str(), F_OK) == 0) {
181 if (chown(attribute_file.c_str(), uid(), gid()) != 0) {
182 PLOG(ERROR) << "chown(" << attribute_file << ", " << uid() << ", " << gid()
183 << ") failed";
184 }
185 if (chmod(attribute_file.c_str(), perm()) != 0) {
186 PLOG(ERROR) << "chmod(" << attribute_file << ", " << perm() << ") failed";
187 }
188 }
189}
190
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700191// Given a path that may start with a platform device, find the parent platform device by finding a
192// parent directory with a 'subsystem' symlink that points to the platform bus.
193// If it doesn't start with a platform device, return false
194bool DeviceHandler::FindPlatformDevice(std::string path, std::string* platform_device_path) const {
195 platform_device_path->clear();
196
197 // Uevents don't contain the mount point, so we need to add it here.
198 path.insert(0, sysfs_mount_point_);
199
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700200 std::string directory = Dirname(path);
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700201
202 while (directory != "/" && directory != ".") {
203 std::string subsystem_link_path;
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700204 if (Realpath(directory + "/subsystem", &subsystem_link_path) &&
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700205 subsystem_link_path == sysfs_mount_point_ + "/bus/platform") {
206 // We need to remove the mount point that we added above before returning.
207 directory.erase(0, sysfs_mount_point_.size());
208 *platform_device_path = directory;
Tom Cherryed506f72017-05-25 15:58:59 -0700209 return true;
210 }
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700211
212 auto last_slash = path.rfind('/');
213 if (last_slash == std::string::npos) return false;
214
215 path.erase(last_slash);
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700216 directory = Dirname(path);
Tom Cherryed506f72017-05-25 15:58:59 -0700217 }
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700218
Tom Cherryed506f72017-05-25 15:58:59 -0700219 return false;
220}
221
222void DeviceHandler::FixupSysPermissions(const std::string& upath,
223 const std::string& subsystem) const {
224 // upaths omit the "/sys" that paths in this list
225 // contain, so we prepend it...
226 std::string path = "/sys" + upath;
227
228 for (const auto& s : sysfs_permissions_) {
229 if (s.MatchWithSubsystem(path, subsystem)) s.SetPermissions(path);
230 }
231
Tom Cherryc5833052017-05-16 15:35:41 -0700232 if (!skip_restorecon_ && access(path.c_str(), F_OK) == 0) {
Tom Cherryed506f72017-05-25 15:58:59 -0700233 LOG(VERBOSE) << "restorecon_recursive: " << path;
234 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
235 PLOG(ERROR) << "selinux_android_restorecon(" << path << ") failed";
236 }
237 }
238}
239
240std::tuple<mode_t, uid_t, gid_t> DeviceHandler::GetDevicePermissions(
241 const std::string& path, const std::vector<std::string>& links) const {
242 // Search the perms list in reverse so that ueventd.$hardware can override ueventd.rc.
243 for (auto it = dev_permissions_.crbegin(); it != dev_permissions_.crend(); ++it) {
244 if (it->Match(path) || std::any_of(links.cbegin(), links.cend(),
245 [it](const auto& link) { return it->Match(link); })) {
246 return {it->perm(), it->uid(), it->gid()};
247 }
248 }
249 /* Default if nothing found. */
250 return {0600, 0, 0};
251}
252
Tom Cherryb4dd8812017-06-23 12:43:48 -0700253void DeviceHandler::MakeDevice(const std::string& path, bool block, int major, int minor,
Tom Cherryed506f72017-05-25 15:58:59 -0700254 const std::vector<std::string>& links) const {
255 auto[mode, uid, gid] = GetDevicePermissions(path, links);
256 mode |= (block ? S_IFBLK : S_IFCHR);
257
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700258 std::string secontext;
259 if (!SelabelLookupFileContextBestMatch(path, links, mode, &secontext)) {
260 PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
261 return;
262 }
263 if (!secontext.empty()) {
264 setfscreatecon(secontext.c_str());
Tom Cherryed506f72017-05-25 15:58:59 -0700265 }
266
267 dev_t dev = makedev(major, minor);
268 /* Temporarily change egid to avoid race condition setting the gid of the
269 * device node. Unforunately changing the euid would prevent creation of
270 * some device nodes, so the uid has to be set with chown() and is still
271 * racy. Fixing the gid race at least fixed the issue with system_server
272 * opening dynamic input devices under the AID_INPUT gid. */
273 if (setegid(gid)) {
274 PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed";
275 goto out;
276 }
277 /* If the node already exists update its SELinux label to handle cases when
278 * it was created with the wrong context during coldboot procedure. */
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700279 if (mknod(path.c_str(), mode, dev) && (errno == EEXIST) && !secontext.empty()) {
Tom Cherryed506f72017-05-25 15:58:59 -0700280 char* fcon = nullptr;
281 int rc = lgetfilecon(path.c_str(), &fcon);
282 if (rc < 0) {
283 PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
284 goto out;
285 }
286
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700287 bool different = fcon != secontext;
Tom Cherryed506f72017-05-25 15:58:59 -0700288 freecon(fcon);
289
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700290 if (different && lsetfilecon(path.c_str(), secontext.c_str())) {
Tom Cherryed506f72017-05-25 15:58:59 -0700291 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path
292 << "' device";
293 }
294 }
295
296out:
297 chown(path.c_str(), uid, -1);
298 if (setegid(AID_ROOT)) {
299 PLOG(FATAL) << "setegid(AID_ROOT) failed";
300 }
301
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700302 if (!secontext.empty()) {
Tom Cherryed506f72017-05-25 15:58:59 -0700303 setfscreatecon(nullptr);
304 }
305}
306
Tom Cherryc44f6a42017-04-05 15:58:31 -0700307// replaces any unacceptable characters with '_', the
308// length of the resulting string is equal to the input string
Tom Cherryed506f72017-05-25 15:58:59 -0700309void SanitizePartitionName(std::string* string) {
Tom Cherryc44f6a42017-04-05 15:58:31 -0700310 const char* accept =
311 "abcdefghijklmnopqrstuvwxyz"
312 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
313 "0123456789"
314 "_-.";
315
Tom Cherry2e344f92017-04-04 17:53:45 -0700316 if (!string) return;
Tom Cherryc44f6a42017-04-05 15:58:31 -0700317
Tom Cherry2e344f92017-04-04 17:53:45 -0700318 std::string::size_type pos = 0;
319 while ((pos = string->find_first_not_of(accept, pos)) != std::string::npos) {
320 (*string)[pos] = '_';
Tom Cherryc44f6a42017-04-05 15:58:31 -0700321 }
322}
323
Tom Cherryed506f72017-05-25 15:58:59 -0700324std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uevent) const {
Tom Cherry2e344f92017-04-04 17:53:45 -0700325 std::string device;
Tom Cherry2e344f92017-04-04 17:53:45 -0700326 std::string type;
Mark Salyzyne419a792019-03-06 15:18:46 -0800327 std::string partition;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700328
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700329 if (FindPlatformDevice(uevent.path, &device)) {
Tom Cherry1ab8f552017-04-06 14:41:30 -0700330 // Skip /devices/platform or /devices/ if present
331 static const std::string devices_platform_prefix = "/devices/platform/";
332 static const std::string devices_prefix = "/devices/";
333
Elliott Hughes579e6822017-12-20 09:41:00 -0800334 if (StartsWith(device, devices_platform_prefix)) {
Tom Cherry1ab8f552017-04-06 14:41:30 -0700335 device = device.substr(devices_platform_prefix.length());
Elliott Hughes579e6822017-12-20 09:41:00 -0800336 } else if (StartsWith(device, devices_prefix)) {
Tom Cherry1ab8f552017-04-06 14:41:30 -0700337 device = device.substr(devices_prefix.length());
338 }
339
Andrew Boiea885d042013-09-13 17:41:20 -0700340 type = "platform";
Tom Cherryed506f72017-05-25 15:58:59 -0700341 } else if (FindPciDevicePrefix(uevent.path, &device)) {
Andrew Boiea885d042013-09-13 17:41:20 -0700342 type = "pci";
Tom Cherryed506f72017-05-25 15:58:59 -0700343 } else if (FindVbdDevicePrefix(uevent.path, &device)) {
Jeremy Compostella937309d2017-03-03 16:27:29 +0100344 type = "vbd";
Mark Salyzyne419a792019-03-06 15:18:46 -0800345 } else if (FindDmDevicePartition(uevent.path, &partition)) {
346 return {"/dev/block/mapper/" + partition};
Andrew Boiea885d042013-09-13 17:41:20 -0700347 } else {
Tom Cherry2e344f92017-04-04 17:53:45 -0700348 return {};
Andrew Boiea885d042013-09-13 17:41:20 -0700349 }
Dima Zavinf395c922013-03-06 16:23:57 -0800350
Tom Cherry2e344f92017-04-04 17:53:45 -0700351 std::vector<std::string> links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700352
Sandeep Patil35403eb2017-02-08 20:27:12 -0800353 LOG(VERBOSE) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700354
Tom Cherry2e344f92017-04-04 17:53:45 -0700355 auto link_path = "/dev/block/" + type + "/" + device;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700356
Tom Cherry2c56d7c2018-12-20 14:36:49 -0800357 bool is_boot_device = boot_devices_.find(device) != boot_devices_.end();
Tom Cherryed506f72017-05-25 15:58:59 -0700358 if (!uevent.partition_name.empty()) {
359 std::string partition_name_sanitized(uevent.partition_name);
360 SanitizePartitionName(&partition_name_sanitized);
361 if (partition_name_sanitized != uevent.partition_name) {
362 LOG(VERBOSE) << "Linking partition '" << uevent.partition_name << "' as '"
Tom Cherry2e344f92017-04-04 17:53:45 -0700363 << partition_name_sanitized << "'";
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700364 }
Tom Cherry2e344f92017-04-04 17:53:45 -0700365 links.emplace_back(link_path + "/by-name/" + partition_name_sanitized);
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800366 // Adds symlink: /dev/block/by-name/<partition_name>.
Tom Cherry2c56d7c2018-12-20 14:36:49 -0800367 if (is_boot_device) {
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800368 links.emplace_back("/dev/block/by-name/" + partition_name_sanitized);
369 }
Tom Cherry2c56d7c2018-12-20 14:36:49 -0800370 } else if (is_boot_device) {
371 // If we don't have a partition name but we are a partition on a boot device, create a
372 // symlink of /dev/block/by-name/<device_name> for symmetry.
373 links.emplace_back("/dev/block/by-name/" + uevent.device_name);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700374 }
375
Tom Cherryed506f72017-05-25 15:58:59 -0700376 auto last_slash = uevent.path.rfind('/');
377 links.emplace_back(link_path + "/" + uevent.path.substr(last_slash + 1));
Colin Crossb0ab94b2010-04-08 16:16:20 -0700378
379 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700380}
381
Tom Cherryb4dd8812017-06-23 12:43:48 -0700382void DeviceHandler::HandleDevice(const std::string& action, const std::string& devpath, bool block,
Tom Cherryed506f72017-05-25 15:58:59 -0700383 int major, int minor, const std::vector<std::string>& links) const {
Tom Cherrye3e48212017-04-11 13:53:37 -0700384 if (action == "add") {
Tom Cherryed506f72017-05-25 15:58:59 -0700385 MakeDevice(devpath, block, major, minor, links);
Tom Cherry2e344f92017-04-04 17:53:45 -0700386 for (const auto& link : links) {
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700387 if (!mkdir_recursive(Dirname(link), 0755)) {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700388 PLOG(ERROR) << "Failed to create directory " << Dirname(link);
Tom Cherryed506f72017-05-25 15:58:59 -0700389 }
390
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800391 if (symlink(devpath.c_str(), link.c_str())) {
392 if (errno != EEXIST) {
393 PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link;
394 } else if (std::string link_path;
395 Readlink(link, &link_path) && link_path != devpath) {
396 PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link
397 << ", which already links to: " << link_path;
398 }
Tom Cherryed506f72017-05-25 15:58:59 -0700399 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700400 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700401 }
402
Tom Cherrye3e48212017-04-11 13:53:37 -0700403 if (action == "remove") {
Tom Cherry2e344f92017-04-04 17:53:45 -0700404 for (const auto& link : links) {
Tom Cherryed506f72017-05-25 15:58:59 -0700405 std::string link_path;
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700406 if (Readlink(link, &link_path) && link_path == devpath) {
Tom Cherryed506f72017-05-25 15:58:59 -0700407 unlink(link.c_str());
408 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700409 }
Tom Cherrye3e48212017-04-11 13:53:37 -0700410 unlink(devpath.c_str());
Colin Crossb0ab94b2010-04-08 16:16:20 -0700411 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700412}
413
Tom Cherry457e28f2018-08-01 13:12:20 -0700414void DeviceHandler::HandleUevent(const Uevent& uevent) {
Tom Cherryb4dd8812017-06-23 12:43:48 -0700415 if (uevent.action == "add" || uevent.action == "change" || uevent.action == "online") {
416 FixupSysPermissions(uevent.path, uevent.subsystem);
Tom Cherrye3e48212017-04-11 13:53:37 -0700417 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700418
Tom Cherry3fa46732017-04-11 14:19:50 -0700419 // if it's not a /dev device, nothing to do
Tom Cherryed506f72017-05-25 15:58:59 -0700420 if (uevent.major < 0 || uevent.minor < 0) return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800421
Tom Cherry3fa46732017-04-11 14:19:50 -0700422 std::string devpath;
Tom Cherryb4dd8812017-06-23 12:43:48 -0700423 std::vector<std::string> links;
424 bool block = false;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800425
Tom Cherryb4dd8812017-06-23 12:43:48 -0700426 if (uevent.subsystem == "block") {
427 block = true;
428 devpath = "/dev/block/" + Basename(uevent.path);
429
430 if (StartsWith(uevent.path, "/devices")) {
431 links = GetBlockDeviceSymlinks(uevent);
432 }
Tom Cherryed506f72017-05-25 15:58:59 -0700433 } else if (const auto subsystem =
434 std::find(subsystems_.cbegin(), subsystems_.cend(), uevent.subsystem);
435 subsystem != subsystems_.cend()) {
Tom Cherryfe062052017-04-24 16:59:05 -0700436 devpath = subsystem->ParseDevPath(uevent);
Tom Cherry9c8d6dd2017-08-17 09:38:01 -0700437 } else if (uevent.subsystem == "usb") {
438 if (!uevent.device_name.empty()) {
439 devpath = "/dev/" + uevent.device_name;
440 } else {
441 // This imitates the file system that would be created
442 // if we were using devfs instead.
443 // Minors are broken up into groups of 128, starting at "001"
444 int bus_id = uevent.minor / 128 + 1;
445 int device_id = uevent.minor % 128 + 1;
446 devpath = StringPrintf("/dev/bus/usb/%03d/%03d", bus_id, device_id);
447 }
448 } else if (StartsWith(uevent.subsystem, "usb")) {
449 // ignore other USB events
450 return;
Tom Cherry780a71e2017-04-04 16:30:40 -0700451 } else {
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700452 devpath = "/dev/" + Basename(uevent.path);
Tom Cherry780a71e2017-04-04 16:30:40 -0700453 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700454
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700455 mkdir_recursive(Dirname(devpath), 0755);
Tom Cherryfe062052017-04-24 16:59:05 -0700456
Tom Cherryb4dd8812017-06-23 12:43:48 -0700457 HandleDevice(uevent.action, devpath, block, uevent.major, uevent.minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700458}
459
Tom Cherry457e28f2018-08-01 13:12:20 -0700460void DeviceHandler::ColdbootDone() {
Oleksiy Avramchenkodd5802a2018-11-02 10:06:47 +0100461 skip_restorecon_ = false;
Tom Cherry457e28f2018-08-01 13:12:20 -0700462}
463
Tom Cherryed506f72017-05-25 15:58:59 -0700464DeviceHandler::DeviceHandler(std::vector<Permissions> dev_permissions,
465 std::vector<SysfsPermissions> sysfs_permissions,
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800466 std::vector<Subsystem> subsystems, std::set<std::string> boot_devices,
467 bool skip_restorecon)
Tom Cherryed506f72017-05-25 15:58:59 -0700468 : dev_permissions_(std::move(dev_permissions)),
469 sysfs_permissions_(std::move(sysfs_permissions)),
470 subsystems_(std::move(subsystems)),
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800471 boot_devices_(std::move(boot_devices)),
Sandeep Patilcd2ba0d2017-06-21 12:46:41 -0700472 skip_restorecon_(skip_restorecon),
473 sysfs_mount_point_("/sys") {}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700474
Tom Cherryed506f72017-05-25 15:58:59 -0700475DeviceHandler::DeviceHandler()
476 : DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{},
Bowgo Tsai8eec38f2018-05-16 18:33:44 +0800477 std::vector<Subsystem>{}, std::set<std::string>{}, false) {}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700478
479} // namespace init
480} // namespace android