2008-10-21 16:00:00 +02:00
|
|
|
/*
|
2017-05-26 00:58:59 +02:00
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
2008-10-21 16:00:00 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-04-06 02:55:46 +02:00
|
|
|
#include "devices.h"
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <errno.h>
|
2012-07-02 20:32:30 +02:00
|
|
|
#include <fnmatch.h>
|
2017-05-18 18:13:15 +02:00
|
|
|
#include <sys/sysmacros.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-03-07 00:18:46 +01:00
|
|
|
#include <chrono>
|
2020-10-20 04:54:18 +02:00
|
|
|
#include <filesystem>
|
2016-02-18 23:52:46 +01:00
|
|
|
#include <memory>
|
2019-03-07 00:18:46 +01:00
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
2016-02-18 23:52:46 +01:00
|
|
|
|
2019-03-07 00:18:46 +01:00
|
|
|
#include <android-base/chrono_utils.h>
|
|
|
|
#include <android-base/file.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <android-base/logging.h>
|
2016-05-06 17:06:59 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2017-04-05 02:53:45 +02:00
|
|
|
#include <android-base/strings.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
#include <selinux/android.h>
|
|
|
|
#include <selinux/selinux.h>
|
2011-04-25 05:08:17 +02:00
|
|
|
|
2019-05-29 00:58:35 +02:00
|
|
|
#include "selabel.h"
|
2010-04-09 01:16:20 +02:00
|
|
|
#include "util.h"
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2019-03-07 00:18:46 +01:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
using android::base::Basename;
|
|
|
|
using android::base::Dirname;
|
2019-03-07 00:18:46 +01:00
|
|
|
using android::base::ReadFileToString;
|
2017-06-22 21:53:17 +02:00
|
|
|
using android::base::Readlink;
|
|
|
|
using android::base::Realpath;
|
|
|
|
using android::base::StartsWith;
|
|
|
|
using android::base::StringPrintf;
|
2019-03-07 00:18:46 +01:00
|
|
|
using android::base::Trim;
|
2017-06-22 21:53:17 +02:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
/* Given a path that may start with a PCI device, populate the supplied buffer
|
|
|
|
* with the PCI domain/bus number and the peripheral ID and return 0.
|
|
|
|
* If it doesn't start with a PCI device, or there is some error, return -1 */
|
|
|
|
static bool FindPciDevicePrefix(const std::string& path, std::string* result) {
|
|
|
|
result->clear();
|
2017-04-25 01:59:05 +02:00
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
if (!StartsWith(path, "/devices/pci")) return false;
|
2017-04-25 01:59:05 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
/* Beginning of the prefix is the initial "pci" after "/devices/" */
|
|
|
|
std::string::size_type start = 9;
|
2017-04-25 01:59:05 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
/* End of the prefix is two path '/' later, capturing the domain/bus number
|
|
|
|
* and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
|
|
|
|
auto end = path.find('/', start);
|
|
|
|
if (end == std::string::npos) return false;
|
2017-04-25 01:59:05 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
end = path.find('/', end + 1);
|
|
|
|
if (end == std::string::npos) return false;
|
2017-04-25 01:59:05 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
auto length = end - start;
|
|
|
|
if (length <= 4) {
|
|
|
|
// The minimum string that will get to this check is 'pci/', which is malformed,
|
|
|
|
// so return false
|
2017-04-25 01:59:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
*result = path.substr(start, length);
|
2017-04-25 01:59:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
/* Given a path that may start with a virtual block device, populate
|
|
|
|
* the supplied buffer with the virtual block device ID and return 0.
|
|
|
|
* If it doesn't start with a virtual block device, or there is some
|
|
|
|
* error, return -1 */
|
|
|
|
static bool FindVbdDevicePrefix(const std::string& path, std::string* result) {
|
|
|
|
result->clear();
|
2017-04-25 01:59:05 +02:00
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
if (!StartsWith(path, "/devices/vbd-")) return false;
|
2017-04-25 01:59:05 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
/* Beginning of the prefix is the initial "vbd-" after "/devices/" */
|
|
|
|
std::string::size_type start = 13;
|
2017-04-25 01:59:05 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
/* End of the prefix is one path '/' later, capturing the
|
|
|
|
virtual block device ID. Example: 768 */
|
|
|
|
auto end = path.find('/', start);
|
|
|
|
if (end == std::string::npos) return false;
|
2017-04-25 01:59:05 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
auto length = end - start;
|
|
|
|
if (length == 0) return false;
|
2017-04-25 01:59:05 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
*result = path.substr(start, length);
|
2017-04-25 01:59:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:18:46 +01:00
|
|
|
// Given a path that may start with a virtual dm block device, populate
|
|
|
|
// the supplied buffer with the dm module's instantiated name.
|
|
|
|
// If it doesn't start with a virtual block device, or there is some
|
|
|
|
// error, return false.
|
libdm: Improve the reliability of dm device paths.
This fixes a race condition where WaitForFile() after
GetDmDevicePathByName appears to succeed, but a subsequent operation on
the path fails. This can happen when CreateDevice() is called
immediately after a call to DeleteDevice (from any process), and the
path is re-used, enqueuing udev events to remove and re-add the block
device.
The fix for this is to introduce a new variant of CreateDevice() that
has a timeout parameter. When the timeout is positive, CreateDevice()
will wait for a /dev/block/mapper/by-uuid symlink to be created, which
signals that ueventd has finished processing the operation.
ueventd will now create these by-uuid symlinks for device-mapper nodes.
Unfortunately, the uuid is only available during "change" events, so we
have to special case device-mapper symlink creation. And since the uuid
is not available during "remove" events, we simply find matching links
to remove them.
This ensures that callers of CreateDevice() can use the device path
knowing that no asynchronous removals are pending. Code that uses the
old CreateDevice+WaitForFile pattern will be transitioned to the new
method.
Note that it is safe to ignore the timeout, or to use the "unsafe"
CreateDevice, if the caller ensures the path by other means. For example
first-stage init has no device removal, and regenerates uevents until
it has acquired all the paths it needs.
Finally, since libdm now inspects sysfs unconditionally, libdm consumers
need r_dir_file perms for sysfs_dm in their sepolicy. Additionally
linking to libdm now requires linking to libext2_uuid.
Bug: 135771280
Test: libdm_test
device flashes, boots
Change-Id: If5a7383ea38f32a7fbbcf24842dce6a668050a70
2019-06-27 02:00:00 +02:00
|
|
|
static bool FindDmDevice(const std::string& path, std::string* name, std::string* uuid) {
|
2019-03-07 00:18:46 +01:00
|
|
|
if (!StartsWith(path, "/devices/virtual/block/dm-")) return false;
|
libdm: Improve the reliability of dm device paths.
This fixes a race condition where WaitForFile() after
GetDmDevicePathByName appears to succeed, but a subsequent operation on
the path fails. This can happen when CreateDevice() is called
immediately after a call to DeleteDevice (from any process), and the
path is re-used, enqueuing udev events to remove and re-add the block
device.
The fix for this is to introduce a new variant of CreateDevice() that
has a timeout parameter. When the timeout is positive, CreateDevice()
will wait for a /dev/block/mapper/by-uuid symlink to be created, which
signals that ueventd has finished processing the operation.
ueventd will now create these by-uuid symlinks for device-mapper nodes.
Unfortunately, the uuid is only available during "change" events, so we
have to special case device-mapper symlink creation. And since the uuid
is not available during "remove" events, we simply find matching links
to remove them.
This ensures that callers of CreateDevice() can use the device path
knowing that no asynchronous removals are pending. Code that uses the
old CreateDevice+WaitForFile pattern will be transitioned to the new
method.
Note that it is safe to ignore the timeout, or to use the "unsafe"
CreateDevice, if the caller ensures the path by other means. For example
first-stage init has no device removal, and regenerates uevents until
it has acquired all the paths it needs.
Finally, since libdm now inspects sysfs unconditionally, libdm consumers
need r_dir_file perms for sysfs_dm in their sepolicy. Additionally
linking to libdm now requires linking to libext2_uuid.
Bug: 135771280
Test: libdm_test
device flashes, boots
Change-Id: If5a7383ea38f32a7fbbcf24842dce6a668050a70
2019-06-27 02:00:00 +02:00
|
|
|
|
|
|
|
if (!ReadFileToString("/sys" + path + "/dm/name", name)) {
|
|
|
|
return false;
|
2019-03-07 00:18:46 +01:00
|
|
|
}
|
libdm: Improve the reliability of dm device paths.
This fixes a race condition where WaitForFile() after
GetDmDevicePathByName appears to succeed, but a subsequent operation on
the path fails. This can happen when CreateDevice() is called
immediately after a call to DeleteDevice (from any process), and the
path is re-used, enqueuing udev events to remove and re-add the block
device.
The fix for this is to introduce a new variant of CreateDevice() that
has a timeout parameter. When the timeout is positive, CreateDevice()
will wait for a /dev/block/mapper/by-uuid symlink to be created, which
signals that ueventd has finished processing the operation.
ueventd will now create these by-uuid symlinks for device-mapper nodes.
Unfortunately, the uuid is only available during "change" events, so we
have to special case device-mapper symlink creation. And since the uuid
is not available during "remove" events, we simply find matching links
to remove them.
This ensures that callers of CreateDevice() can use the device path
knowing that no asynchronous removals are pending. Code that uses the
old CreateDevice+WaitForFile pattern will be transitioned to the new
method.
Note that it is safe to ignore the timeout, or to use the "unsafe"
CreateDevice, if the caller ensures the path by other means. For example
first-stage init has no device removal, and regenerates uevents until
it has acquired all the paths it needs.
Finally, since libdm now inspects sysfs unconditionally, libdm consumers
need r_dir_file perms for sysfs_dm in their sepolicy. Additionally
linking to libdm now requires linking to libext2_uuid.
Bug: 135771280
Test: libdm_test
device flashes, boots
Change-Id: If5a7383ea38f32a7fbbcf24842dce6a668050a70
2019-06-27 02:00:00 +02:00
|
|
|
ReadFileToString("/sys" + path + "/dm/uuid", uuid);
|
|
|
|
|
|
|
|
*name = android::base::Trim(*name);
|
|
|
|
*uuid = android::base::Trim(*uuid);
|
2019-03-07 00:18:46 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
Permissions::Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid)
|
|
|
|
: name_(name), perm_(perm), uid_(uid), gid_(gid), prefix_(false), wildcard_(false) {
|
|
|
|
// Set 'prefix_' or 'wildcard_' based on the below cases:
|
|
|
|
//
|
|
|
|
// 1) No '*' in 'name' -> Neither are set and Match() checks a given path for strict
|
|
|
|
// equality with 'name'
|
|
|
|
//
|
|
|
|
// 2) '*' only appears as the last character in 'name' -> 'prefix'_ is set to true and
|
|
|
|
// Match() checks if 'name' is a prefix of a given path.
|
|
|
|
//
|
|
|
|
// 3) '*' appears elsewhere -> 'wildcard_' is set to true and Match() uses fnmatch()
|
|
|
|
// with FNM_PATHNAME to compare 'name' to a given path.
|
|
|
|
|
|
|
|
auto wildcard_position = name_.find('*');
|
|
|
|
if (wildcard_position != std::string::npos) {
|
|
|
|
if (wildcard_position == name_.length() - 1) {
|
|
|
|
prefix_ = true;
|
|
|
|
name_.pop_back();
|
|
|
|
} else {
|
|
|
|
wildcard_ = true;
|
|
|
|
}
|
2017-04-25 01:59:05 +02:00
|
|
|
}
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
2017-04-25 01:59:05 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
bool Permissions::Match(const std::string& path) const {
|
2017-12-20 18:41:00 +01:00
|
|
|
if (prefix_) return StartsWith(path, name_);
|
2017-05-26 00:58:59 +02:00
|
|
|
if (wildcard_) return fnmatch(name_.c_str(), path.c_str(), FNM_PATHNAME) == 0;
|
|
|
|
return path == name_;
|
2017-04-25 01:59:05 +02:00
|
|
|
}
|
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
bool SysfsPermissions::MatchWithSubsystem(const std::string& path,
|
|
|
|
const std::string& subsystem) const {
|
2017-06-22 21:53:17 +02:00
|
|
|
std::string path_basename = Basename(path);
|
2017-05-26 00:58:59 +02:00
|
|
|
if (name().find(subsystem) != std::string::npos) {
|
|
|
|
if (Match("/sys/class/" + subsystem + "/" + path_basename)) return true;
|
|
|
|
if (Match("/sys/bus/" + subsystem + "/devices/" + path_basename)) return true;
|
2017-04-25 01:59:05 +02:00
|
|
|
}
|
2017-05-26 00:58:59 +02:00
|
|
|
return Match(path);
|
2017-04-25 01:59:05 +02:00
|
|
|
}
|
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
void SysfsPermissions::SetPermissions(const std::string& path) const {
|
|
|
|
std::string attribute_file = path + "/" + attribute_;
|
|
|
|
LOG(VERBOSE) << "fixup " << attribute_file << " " << uid() << " " << gid() << " " << std::oct
|
|
|
|
<< perm();
|
2017-04-25 01:59:05 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
if (access(attribute_file.c_str(), F_OK) == 0) {
|
|
|
|
if (chown(attribute_file.c_str(), uid(), gid()) != 0) {
|
|
|
|
PLOG(ERROR) << "chown(" << attribute_file << ", " << uid() << ", " << gid()
|
|
|
|
<< ") failed";
|
|
|
|
}
|
|
|
|
if (chmod(attribute_file.c_str(), perm()) != 0) {
|
|
|
|
PLOG(ERROR) << "chmod(" << attribute_file << ", " << perm() << ") failed";
|
|
|
|
}
|
2017-04-25 01:59:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 21:46:41 +02:00
|
|
|
// Given a path that may start with a platform device, find the parent platform device by finding a
|
|
|
|
// parent directory with a 'subsystem' symlink that points to the platform bus.
|
|
|
|
// If it doesn't start with a platform device, return false
|
|
|
|
bool DeviceHandler::FindPlatformDevice(std::string path, std::string* platform_device_path) const {
|
|
|
|
platform_device_path->clear();
|
|
|
|
|
|
|
|
// Uevents don't contain the mount point, so we need to add it here.
|
|
|
|
path.insert(0, sysfs_mount_point_);
|
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
std::string directory = Dirname(path);
|
2017-06-21 21:46:41 +02:00
|
|
|
|
|
|
|
while (directory != "/" && directory != ".") {
|
|
|
|
std::string subsystem_link_path;
|
2017-06-22 21:53:17 +02:00
|
|
|
if (Realpath(directory + "/subsystem", &subsystem_link_path) &&
|
2020-06-12 14:45:11 +02:00
|
|
|
(subsystem_link_path == sysfs_mount_point_ + "/bus/platform" ||
|
|
|
|
subsystem_link_path == sysfs_mount_point_ + "/bus/amba")) {
|
2017-06-21 21:46:41 +02:00
|
|
|
// We need to remove the mount point that we added above before returning.
|
|
|
|
directory.erase(0, sysfs_mount_point_.size());
|
|
|
|
*platform_device_path = directory;
|
2017-05-26 00:58:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-06-21 21:46:41 +02:00
|
|
|
|
|
|
|
auto last_slash = path.rfind('/');
|
|
|
|
if (last_slash == std::string::npos) return false;
|
|
|
|
|
|
|
|
path.erase(last_slash);
|
2017-06-22 21:53:17 +02:00
|
|
|
directory = Dirname(path);
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
2017-06-21 21:46:41 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
return false;
|
2017-04-25 01:59:05 +02:00
|
|
|
}
|
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
void DeviceHandler::FixupSysPermissions(const std::string& upath,
|
|
|
|
const std::string& subsystem) const {
|
2016-05-06 17:06:59 +02:00
|
|
|
// upaths omit the "/sys" that paths in this list
|
|
|
|
// contain, so we prepend it...
|
2017-04-06 02:55:46 +02:00
|
|
|
std::string path = "/sys" + upath;
|
2016-05-06 19:28:48 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
for (const auto& s : sysfs_permissions_) {
|
2017-04-06 02:55:46 +02:00
|
|
|
if (s.MatchWithSubsystem(path, subsystem)) s.SetPermissions(path);
|
2016-05-06 19:28:48 +02:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:35:41 +02:00
|
|
|
if (!skip_restorecon_ && access(path.c_str(), F_OK) == 0) {
|
2016-07-28 22:55:39 +02:00
|
|
|
LOG(VERBOSE) << "restorecon_recursive: " << path;
|
2017-05-26 00:58:59 +02:00
|
|
|
if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
|
|
|
|
PLOG(ERROR) << "selinux_android_restorecon(" << path << ") failed";
|
|
|
|
}
|
2016-05-06 19:28:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
std::tuple<mode_t, uid_t, gid_t> DeviceHandler::GetDevicePermissions(
|
|
|
|
const std::string& path, const std::vector<std::string>& links) const {
|
2017-04-06 02:55:46 +02:00
|
|
|
// Search the perms list in reverse so that ueventd.$hardware can override ueventd.rc.
|
2017-05-26 00:58:59 +02:00
|
|
|
for (auto it = dev_permissions_.crbegin(); it != dev_permissions_.crend(); ++it) {
|
|
|
|
if (it->Match(path) || std::any_of(links.cbegin(), links.cend(),
|
2017-04-06 02:55:46 +02:00
|
|
|
[it](const auto& link) { return it->Match(link); })) {
|
|
|
|
return {it->perm(), it->uid(), it->gid()};
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
2010-04-20 23:32:50 +02:00
|
|
|
/* Default if nothing found. */
|
2017-04-06 02:55:46 +02:00
|
|
|
return {0600, 0, 0};
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2017-06-23 21:43:48 +02:00
|
|
|
void DeviceHandler::MakeDevice(const std::string& path, bool block, int major, int minor,
|
2017-05-26 00:58:59 +02:00
|
|
|
const std::vector<std::string>& links) const {
|
|
|
|
auto[mode, uid, gid] = GetDevicePermissions(path, links);
|
2017-04-06 02:55:46 +02:00
|
|
|
mode |= (block ? S_IFBLK : S_IFCHR);
|
2012-10-17 08:07:05 +02:00
|
|
|
|
2017-08-10 21:22:44 +02:00
|
|
|
std::string secontext;
|
|
|
|
if (!SelabelLookupFileContextBestMatch(path, links, mode, &secontext)) {
|
|
|
|
PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!secontext.empty()) {
|
|
|
|
setfscreatecon(secontext.c_str());
|
2016-04-25 17:22:27 +02:00
|
|
|
}
|
2012-10-17 08:07:05 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
dev_t dev = makedev(major, minor);
|
2010-01-22 03:13:39 +01:00
|
|
|
/* Temporarily change egid to avoid race condition setting the gid of the
|
|
|
|
* device node. Unforunately changing the euid would prevent creation of
|
|
|
|
* some device nodes, so the uid has to be set with chown() and is still
|
|
|
|
* racy. Fixing the gid race at least fixed the issue with system_server
|
|
|
|
* opening dynamic input devices under the AID_INPUT gid. */
|
2017-02-23 22:46:09 +01:00
|
|
|
if (setegid(gid)) {
|
|
|
|
PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed";
|
|
|
|
goto out;
|
|
|
|
}
|
2016-04-25 17:22:27 +02:00
|
|
|
/* If the node already exists update its SELinux label to handle cases when
|
|
|
|
* it was created with the wrong context during coldboot procedure. */
|
2017-08-10 21:22:44 +02:00
|
|
|
if (mknod(path.c_str(), mode, dev) && (errno == EEXIST) && !secontext.empty()) {
|
2016-06-02 18:53:44 +02:00
|
|
|
char* fcon = nullptr;
|
2017-04-11 22:53:37 +02:00
|
|
|
int rc = lgetfilecon(path.c_str(), &fcon);
|
2016-06-02 18:53:44 +02:00
|
|
|
if (rc < 0) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
|
2016-06-02 18:53:44 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-08-10 21:22:44 +02:00
|
|
|
bool different = fcon != secontext;
|
2016-06-02 18:53:44 +02:00
|
|
|
freecon(fcon);
|
|
|
|
|
2017-08-10 21:22:44 +02:00
|
|
|
if (different && lsetfilecon(path.c_str(), secontext.c_str())) {
|
2017-05-26 00:58:59 +02:00
|
|
|
PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path
|
|
|
|
<< "' device";
|
2016-04-25 17:22:27 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-02 18:53:44 +02:00
|
|
|
|
|
|
|
out:
|
2017-04-11 22:53:37 +02:00
|
|
|
chown(path.c_str(), uid, -1);
|
2017-02-23 22:46:09 +01:00
|
|
|
if (setegid(AID_ROOT)) {
|
|
|
|
PLOG(FATAL) << "setegid(AID_ROOT) failed";
|
|
|
|
}
|
2012-10-17 08:07:05 +02:00
|
|
|
|
2017-08-10 21:22:44 +02:00
|
|
|
if (!secontext.empty()) {
|
2017-05-26 00:58:59 +02:00
|
|
|
setfscreatecon(nullptr);
|
2017-02-03 16:51:55 +01:00
|
|
|
}
|
2012-01-13 14:48:47 +01:00
|
|
|
}
|
|
|
|
|
2017-04-06 00:58:31 +02:00
|
|
|
// replaces any unacceptable characters with '_', the
|
|
|
|
// length of the resulting string is equal to the input string
|
2017-05-26 00:58:59 +02:00
|
|
|
void SanitizePartitionName(std::string* string) {
|
2017-04-06 00:58:31 +02:00
|
|
|
const char* accept =
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"0123456789"
|
|
|
|
"_-.";
|
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
if (!string) return;
|
2017-04-06 00:58:31 +02:00
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
std::string::size_type pos = 0;
|
|
|
|
while ((pos = string->find_first_not_of(accept, pos)) != std::string::npos) {
|
|
|
|
(*string)[pos] = '_';
|
2017-04-06 00:58:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uevent) const {
|
2017-04-05 02:53:45 +02:00
|
|
|
std::string device;
|
|
|
|
std::string type;
|
2019-03-07 00:18:46 +01:00
|
|
|
std::string partition;
|
libdm: Improve the reliability of dm device paths.
This fixes a race condition where WaitForFile() after
GetDmDevicePathByName appears to succeed, but a subsequent operation on
the path fails. This can happen when CreateDevice() is called
immediately after a call to DeleteDevice (from any process), and the
path is re-used, enqueuing udev events to remove and re-add the block
device.
The fix for this is to introduce a new variant of CreateDevice() that
has a timeout parameter. When the timeout is positive, CreateDevice()
will wait for a /dev/block/mapper/by-uuid symlink to be created, which
signals that ueventd has finished processing the operation.
ueventd will now create these by-uuid symlinks for device-mapper nodes.
Unfortunately, the uuid is only available during "change" events, so we
have to special case device-mapper symlink creation. And since the uuid
is not available during "remove" events, we simply find matching links
to remove them.
This ensures that callers of CreateDevice() can use the device path
knowing that no asynchronous removals are pending. Code that uses the
old CreateDevice+WaitForFile pattern will be transitioned to the new
method.
Note that it is safe to ignore the timeout, or to use the "unsafe"
CreateDevice, if the caller ensures the path by other means. For example
first-stage init has no device removal, and regenerates uevents until
it has acquired all the paths it needs.
Finally, since libdm now inspects sysfs unconditionally, libdm consumers
need r_dir_file perms for sysfs_dm in their sepolicy. Additionally
linking to libdm now requires linking to libext2_uuid.
Bug: 135771280
Test: libdm_test
device flashes, boots
Change-Id: If5a7383ea38f32a7fbbcf24842dce6a668050a70
2019-06-27 02:00:00 +02:00
|
|
|
std::string uuid;
|
2010-04-09 01:16:20 +02:00
|
|
|
|
2017-06-21 21:46:41 +02:00
|
|
|
if (FindPlatformDevice(uevent.path, &device)) {
|
2017-04-06 23:41:30 +02:00
|
|
|
// Skip /devices/platform or /devices/ if present
|
|
|
|
static const std::string devices_platform_prefix = "/devices/platform/";
|
|
|
|
static const std::string devices_prefix = "/devices/";
|
|
|
|
|
2017-12-20 18:41:00 +01:00
|
|
|
if (StartsWith(device, devices_platform_prefix)) {
|
2017-04-06 23:41:30 +02:00
|
|
|
device = device.substr(devices_platform_prefix.length());
|
2017-12-20 18:41:00 +01:00
|
|
|
} else if (StartsWith(device, devices_prefix)) {
|
2017-04-06 23:41:30 +02:00
|
|
|
device = device.substr(devices_prefix.length());
|
|
|
|
}
|
|
|
|
|
2013-09-14 02:41:20 +02:00
|
|
|
type = "platform";
|
2017-05-26 00:58:59 +02:00
|
|
|
} else if (FindPciDevicePrefix(uevent.path, &device)) {
|
2013-09-14 02:41:20 +02:00
|
|
|
type = "pci";
|
2017-05-26 00:58:59 +02:00
|
|
|
} else if (FindVbdDevicePrefix(uevent.path, &device)) {
|
2017-03-03 16:27:29 +01:00
|
|
|
type = "vbd";
|
libdm: Improve the reliability of dm device paths.
This fixes a race condition where WaitForFile() after
GetDmDevicePathByName appears to succeed, but a subsequent operation on
the path fails. This can happen when CreateDevice() is called
immediately after a call to DeleteDevice (from any process), and the
path is re-used, enqueuing udev events to remove and re-add the block
device.
The fix for this is to introduce a new variant of CreateDevice() that
has a timeout parameter. When the timeout is positive, CreateDevice()
will wait for a /dev/block/mapper/by-uuid symlink to be created, which
signals that ueventd has finished processing the operation.
ueventd will now create these by-uuid symlinks for device-mapper nodes.
Unfortunately, the uuid is only available during "change" events, so we
have to special case device-mapper symlink creation. And since the uuid
is not available during "remove" events, we simply find matching links
to remove them.
This ensures that callers of CreateDevice() can use the device path
knowing that no asynchronous removals are pending. Code that uses the
old CreateDevice+WaitForFile pattern will be transitioned to the new
method.
Note that it is safe to ignore the timeout, or to use the "unsafe"
CreateDevice, if the caller ensures the path by other means. For example
first-stage init has no device removal, and regenerates uevents until
it has acquired all the paths it needs.
Finally, since libdm now inspects sysfs unconditionally, libdm consumers
need r_dir_file perms for sysfs_dm in their sepolicy. Additionally
linking to libdm now requires linking to libext2_uuid.
Bug: 135771280
Test: libdm_test
device flashes, boots
Change-Id: If5a7383ea38f32a7fbbcf24842dce6a668050a70
2019-06-27 02:00:00 +02:00
|
|
|
} else if (FindDmDevice(uevent.path, &partition, &uuid)) {
|
|
|
|
std::vector<std::string> symlinks = {"/dev/block/mapper/" + partition};
|
|
|
|
if (!uuid.empty()) {
|
|
|
|
symlinks.emplace_back("/dev/block/mapper/by-uuid/" + uuid);
|
|
|
|
}
|
|
|
|
return symlinks;
|
2013-09-14 02:41:20 +02:00
|
|
|
} else {
|
2017-04-05 02:53:45 +02:00
|
|
|
return {};
|
2013-09-14 02:41:20 +02:00
|
|
|
}
|
2013-03-07 01:23:57 +01:00
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
std::vector<std::string> links;
|
2010-04-09 01:16:20 +02:00
|
|
|
|
2017-02-09 05:27:12 +01:00
|
|
|
LOG(VERBOSE) << "found " << type << " device " << device;
|
2011-03-31 03:32:12 +02:00
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
auto link_path = "/dev/block/" + type + "/" + device;
|
2010-04-09 01:16:20 +02:00
|
|
|
|
2018-12-20 23:36:49 +01:00
|
|
|
bool is_boot_device = boot_devices_.find(device) != boot_devices_.end();
|
2017-05-26 00:58:59 +02:00
|
|
|
if (!uevent.partition_name.empty()) {
|
|
|
|
std::string partition_name_sanitized(uevent.partition_name);
|
|
|
|
SanitizePartitionName(&partition_name_sanitized);
|
|
|
|
if (partition_name_sanitized != uevent.partition_name) {
|
|
|
|
LOG(VERBOSE) << "Linking partition '" << uevent.partition_name << "' as '"
|
2017-04-05 02:53:45 +02:00
|
|
|
<< partition_name_sanitized << "'";
|
2016-06-25 00:12:21 +02:00
|
|
|
}
|
2017-04-05 02:53:45 +02:00
|
|
|
links.emplace_back(link_path + "/by-name/" + partition_name_sanitized);
|
Adds /dev/block/by-name/<partition> symlinks
During uevent processing, some "by-name" symlinks will be created.
/dev/block/<type>/<device>/by-name/<partition>
<type> can be: platform, pci or vbd.
<device> might be: soc.0/f9824900.sdhci, soc.0/f9824900.sdhci, etc.
<partition> might be: system, vendor, system_a, system_b, etc.
e.g., on a non-A/B device:
/dev/block/platform/soc.0/f9824900.sdhci/by-name/system
/dev/block/platform/soc.0/f9824900.sdhci/by-name/vendor
On a A/B device:
/dev/block/platform/soc/1da4000.ufshc/by-name/system_a
/dev/block/platform/soc/1da4000.ufshc/by-name/system_b
/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_a
/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_b
However, those symlinks are "device-specific".
This change adds the "generic" symlinks in ueventd, in addition to
the existing symlinks, when the possible "boot devices" are specified
in device tree. e.g.,
&firmware_android {
compatible = "android,firmware";
boot_devices ="soc/1da4000.ufshc,soc.0/f9824900.sdhci";
}
The following symlinks will then be created on the aforementioned non-A/B
and A/B devices, respectively.
/dev/block/by-name/system
/dev/block/by-name/vendor
/dev/block/by-name/system_a
/dev/block/by-name/system_b
/dev/block/by-name/vendor_a
/dev/block/by-name/vendor_b
Note that both <type> and <device> are skipped in the newly create symlinks.
It assumes there is no more than one devices with the same <partition>,
which is the assumption of current first stage mount flow.
Finally, when 'boot_devices' in DT is absent, it fallbacks to extract
'boot_devices' from fstab settings. e.g., using 'soc/1da4000.ufshc',
'soc.0/f9824900.sdhci' for a fstab with the following content:
/dev/block/platform/soc/1da4000.ufshc/by-name/system
/dev/block/platform/soc.0/f9824900.sdhci/by-name/vendor
Bug: 78613232
Test: adb shell ls /dev/block/by-name
Change-Id: Iec920b5a72409b6a2bdbeeb290f0a3acd2046b5d
2018-05-16 12:33:44 +02:00
|
|
|
// Adds symlink: /dev/block/by-name/<partition_name>.
|
2018-12-20 23:36:49 +01:00
|
|
|
if (is_boot_device) {
|
Adds /dev/block/by-name/<partition> symlinks
During uevent processing, some "by-name" symlinks will be created.
/dev/block/<type>/<device>/by-name/<partition>
<type> can be: platform, pci or vbd.
<device> might be: soc.0/f9824900.sdhci, soc.0/f9824900.sdhci, etc.
<partition> might be: system, vendor, system_a, system_b, etc.
e.g., on a non-A/B device:
/dev/block/platform/soc.0/f9824900.sdhci/by-name/system
/dev/block/platform/soc.0/f9824900.sdhci/by-name/vendor
On a A/B device:
/dev/block/platform/soc/1da4000.ufshc/by-name/system_a
/dev/block/platform/soc/1da4000.ufshc/by-name/system_b
/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_a
/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_b
However, those symlinks are "device-specific".
This change adds the "generic" symlinks in ueventd, in addition to
the existing symlinks, when the possible "boot devices" are specified
in device tree. e.g.,
&firmware_android {
compatible = "android,firmware";
boot_devices ="soc/1da4000.ufshc,soc.0/f9824900.sdhci";
}
The following symlinks will then be created on the aforementioned non-A/B
and A/B devices, respectively.
/dev/block/by-name/system
/dev/block/by-name/vendor
/dev/block/by-name/system_a
/dev/block/by-name/system_b
/dev/block/by-name/vendor_a
/dev/block/by-name/vendor_b
Note that both <type> and <device> are skipped in the newly create symlinks.
It assumes there is no more than one devices with the same <partition>,
which is the assumption of current first stage mount flow.
Finally, when 'boot_devices' in DT is absent, it fallbacks to extract
'boot_devices' from fstab settings. e.g., using 'soc/1da4000.ufshc',
'soc.0/f9824900.sdhci' for a fstab with the following content:
/dev/block/platform/soc/1da4000.ufshc/by-name/system
/dev/block/platform/soc.0/f9824900.sdhci/by-name/vendor
Bug: 78613232
Test: adb shell ls /dev/block/by-name
Change-Id: Iec920b5a72409b6a2bdbeeb290f0a3acd2046b5d
2018-05-16 12:33:44 +02:00
|
|
|
links.emplace_back("/dev/block/by-name/" + partition_name_sanitized);
|
|
|
|
}
|
2018-12-20 23:36:49 +01:00
|
|
|
} else if (is_boot_device) {
|
|
|
|
// If we don't have a partition name but we are a partition on a boot device, create a
|
|
|
|
// symlink of /dev/block/by-name/<device_name> for symmetry.
|
|
|
|
links.emplace_back("/dev/block/by-name/" + uevent.device_name);
|
2010-04-09 01:16:20 +02:00
|
|
|
}
|
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
auto last_slash = uevent.path.rfind('/');
|
|
|
|
links.emplace_back(link_path + "/" + uevent.path.substr(last_slash + 1));
|
2010-04-09 01:16:20 +02:00
|
|
|
|
|
|
|
return links;
|
|
|
|
}
|
|
|
|
|
libdm: Improve the reliability of dm device paths.
This fixes a race condition where WaitForFile() after
GetDmDevicePathByName appears to succeed, but a subsequent operation on
the path fails. This can happen when CreateDevice() is called
immediately after a call to DeleteDevice (from any process), and the
path is re-used, enqueuing udev events to remove and re-add the block
device.
The fix for this is to introduce a new variant of CreateDevice() that
has a timeout parameter. When the timeout is positive, CreateDevice()
will wait for a /dev/block/mapper/by-uuid symlink to be created, which
signals that ueventd has finished processing the operation.
ueventd will now create these by-uuid symlinks for device-mapper nodes.
Unfortunately, the uuid is only available during "change" events, so we
have to special case device-mapper symlink creation. And since the uuid
is not available during "remove" events, we simply find matching links
to remove them.
This ensures that callers of CreateDevice() can use the device path
knowing that no asynchronous removals are pending. Code that uses the
old CreateDevice+WaitForFile pattern will be transitioned to the new
method.
Note that it is safe to ignore the timeout, or to use the "unsafe"
CreateDevice, if the caller ensures the path by other means. For example
first-stage init has no device removal, and regenerates uevents until
it has acquired all the paths it needs.
Finally, since libdm now inspects sysfs unconditionally, libdm consumers
need r_dir_file perms for sysfs_dm in their sepolicy. Additionally
linking to libdm now requires linking to libext2_uuid.
Bug: 135771280
Test: libdm_test
device flashes, boots
Change-Id: If5a7383ea38f32a7fbbcf24842dce6a668050a70
2019-06-27 02:00:00 +02:00
|
|
|
static void RemoveDeviceMapperLinks(const std::string& devpath) {
|
|
|
|
std::vector<std::string> dirs = {
|
|
|
|
"/dev/block/mapper",
|
|
|
|
"/dev/block/mapper/by-uuid",
|
|
|
|
};
|
|
|
|
for (const auto& dir : dirs) {
|
|
|
|
if (access(dir.c_str(), F_OK) != 0) continue;
|
|
|
|
|
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> dh(opendir(dir.c_str()), closedir);
|
|
|
|
if (!dh) {
|
|
|
|
PLOG(ERROR) << "Failed to open directory " << dir;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent* dp;
|
|
|
|
std::string link_path;
|
|
|
|
while ((dp = readdir(dh.get())) != nullptr) {
|
|
|
|
if (dp->d_type != DT_LNK) continue;
|
|
|
|
|
|
|
|
auto path = dir + "/" + dp->d_name;
|
|
|
|
if (Readlink(path, &link_path) && link_path == devpath) {
|
|
|
|
unlink(path.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-23 21:43:48 +02:00
|
|
|
void DeviceHandler::HandleDevice(const std::string& action, const std::string& devpath, bool block,
|
2017-05-26 00:58:59 +02:00
|
|
|
int major, int minor, const std::vector<std::string>& links) const {
|
2017-04-11 22:53:37 +02:00
|
|
|
if (action == "add") {
|
2017-05-26 00:58:59 +02:00
|
|
|
MakeDevice(devpath, block, major, minor, links);
|
libdm: Improve the reliability of dm device paths.
This fixes a race condition where WaitForFile() after
GetDmDevicePathByName appears to succeed, but a subsequent operation on
the path fails. This can happen when CreateDevice() is called
immediately after a call to DeleteDevice (from any process), and the
path is re-used, enqueuing udev events to remove and re-add the block
device.
The fix for this is to introduce a new variant of CreateDevice() that
has a timeout parameter. When the timeout is positive, CreateDevice()
will wait for a /dev/block/mapper/by-uuid symlink to be created, which
signals that ueventd has finished processing the operation.
ueventd will now create these by-uuid symlinks for device-mapper nodes.
Unfortunately, the uuid is only available during "change" events, so we
have to special case device-mapper symlink creation. And since the uuid
is not available during "remove" events, we simply find matching links
to remove them.
This ensures that callers of CreateDevice() can use the device path
knowing that no asynchronous removals are pending. Code that uses the
old CreateDevice+WaitForFile pattern will be transitioned to the new
method.
Note that it is safe to ignore the timeout, or to use the "unsafe"
CreateDevice, if the caller ensures the path by other means. For example
first-stage init has no device removal, and regenerates uevents until
it has acquired all the paths it needs.
Finally, since libdm now inspects sysfs unconditionally, libdm consumers
need r_dir_file perms for sysfs_dm in their sepolicy. Additionally
linking to libdm now requires linking to libext2_uuid.
Bug: 135771280
Test: libdm_test
device flashes, boots
Change-Id: If5a7383ea38f32a7fbbcf24842dce6a668050a70
2019-06-27 02:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have full device-mapper information until a change event is fired.
|
|
|
|
if (action == "add" || (action == "change" && StartsWith(devpath, "/dev/block/dm-"))) {
|
2017-04-05 02:53:45 +02:00
|
|
|
for (const auto& link : links) {
|
2017-08-10 21:22:44 +02:00
|
|
|
if (!mkdir_recursive(Dirname(link), 0755)) {
|
2017-06-22 21:53:17 +02:00
|
|
|
PLOG(ERROR) << "Failed to create directory " << Dirname(link);
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
|
Adds /dev/block/by-name/<partition> symlinks
During uevent processing, some "by-name" symlinks will be created.
/dev/block/<type>/<device>/by-name/<partition>
<type> can be: platform, pci or vbd.
<device> might be: soc.0/f9824900.sdhci, soc.0/f9824900.sdhci, etc.
<partition> might be: system, vendor, system_a, system_b, etc.
e.g., on a non-A/B device:
/dev/block/platform/soc.0/f9824900.sdhci/by-name/system
/dev/block/platform/soc.0/f9824900.sdhci/by-name/vendor
On a A/B device:
/dev/block/platform/soc/1da4000.ufshc/by-name/system_a
/dev/block/platform/soc/1da4000.ufshc/by-name/system_b
/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_a
/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_b
However, those symlinks are "device-specific".
This change adds the "generic" symlinks in ueventd, in addition to
the existing symlinks, when the possible "boot devices" are specified
in device tree. e.g.,
&firmware_android {
compatible = "android,firmware";
boot_devices ="soc/1da4000.ufshc,soc.0/f9824900.sdhci";
}
The following symlinks will then be created on the aforementioned non-A/B
and A/B devices, respectively.
/dev/block/by-name/system
/dev/block/by-name/vendor
/dev/block/by-name/system_a
/dev/block/by-name/system_b
/dev/block/by-name/vendor_a
/dev/block/by-name/vendor_b
Note that both <type> and <device> are skipped in the newly create symlinks.
It assumes there is no more than one devices with the same <partition>,
which is the assumption of current first stage mount flow.
Finally, when 'boot_devices' in DT is absent, it fallbacks to extract
'boot_devices' from fstab settings. e.g., using 'soc/1da4000.ufshc',
'soc.0/f9824900.sdhci' for a fstab with the following content:
/dev/block/platform/soc/1da4000.ufshc/by-name/system
/dev/block/platform/soc.0/f9824900.sdhci/by-name/vendor
Bug: 78613232
Test: adb shell ls /dev/block/by-name
Change-Id: Iec920b5a72409b6a2bdbeeb290f0a3acd2046b5d
2018-05-16 12:33:44 +02:00
|
|
|
if (symlink(devpath.c_str(), link.c_str())) {
|
|
|
|
if (errno != EEXIST) {
|
|
|
|
PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link;
|
|
|
|
} else if (std::string link_path;
|
|
|
|
Readlink(link, &link_path) && link_path != devpath) {
|
|
|
|
PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link
|
|
|
|
<< ", which already links to: " << link_path;
|
|
|
|
}
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
2010-04-09 01:16:20 +02:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
if (action == "remove") {
|
libdm: Improve the reliability of dm device paths.
This fixes a race condition where WaitForFile() after
GetDmDevicePathByName appears to succeed, but a subsequent operation on
the path fails. This can happen when CreateDevice() is called
immediately after a call to DeleteDevice (from any process), and the
path is re-used, enqueuing udev events to remove and re-add the block
device.
The fix for this is to introduce a new variant of CreateDevice() that
has a timeout parameter. When the timeout is positive, CreateDevice()
will wait for a /dev/block/mapper/by-uuid symlink to be created, which
signals that ueventd has finished processing the operation.
ueventd will now create these by-uuid symlinks for device-mapper nodes.
Unfortunately, the uuid is only available during "change" events, so we
have to special case device-mapper symlink creation. And since the uuid
is not available during "remove" events, we simply find matching links
to remove them.
This ensures that callers of CreateDevice() can use the device path
knowing that no asynchronous removals are pending. Code that uses the
old CreateDevice+WaitForFile pattern will be transitioned to the new
method.
Note that it is safe to ignore the timeout, or to use the "unsafe"
CreateDevice, if the caller ensures the path by other means. For example
first-stage init has no device removal, and regenerates uevents until
it has acquired all the paths it needs.
Finally, since libdm now inspects sysfs unconditionally, libdm consumers
need r_dir_file perms for sysfs_dm in their sepolicy. Additionally
linking to libdm now requires linking to libext2_uuid.
Bug: 135771280
Test: libdm_test
device flashes, boots
Change-Id: If5a7383ea38f32a7fbbcf24842dce6a668050a70
2019-06-27 02:00:00 +02:00
|
|
|
if (StartsWith(devpath, "/dev/block/dm-")) {
|
|
|
|
RemoveDeviceMapperLinks(devpath);
|
|
|
|
}
|
2017-04-05 02:53:45 +02:00
|
|
|
for (const auto& link : links) {
|
2017-05-26 00:58:59 +02:00
|
|
|
std::string link_path;
|
2017-06-22 21:53:17 +02:00
|
|
|
if (Readlink(link, &link_path) && link_path == devpath) {
|
2017-05-26 00:58:59 +02:00
|
|
|
unlink(link.c_str());
|
|
|
|
}
|
2010-04-09 01:16:20 +02:00
|
|
|
}
|
2017-04-11 22:53:37 +02:00
|
|
|
unlink(devpath.c_str());
|
2010-04-09 01:16:20 +02:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2019-09-24 22:00:43 +02:00
|
|
|
void DeviceHandler::HandleAshmemUevent(const Uevent& uevent) {
|
|
|
|
if (uevent.device_name == "ashmem") {
|
|
|
|
static const std::string boot_id_path = "/proc/sys/kernel/random/boot_id";
|
|
|
|
std::string boot_id;
|
|
|
|
if (!ReadFileToString(boot_id_path, &boot_id)) {
|
|
|
|
PLOG(ERROR) << "Cannot duplicate ashmem device node. Failed to read " << boot_id_path;
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
boot_id = Trim(boot_id);
|
|
|
|
|
|
|
|
Uevent dup_ashmem_uevent = uevent;
|
|
|
|
dup_ashmem_uevent.device_name += boot_id;
|
|
|
|
dup_ashmem_uevent.path += boot_id;
|
|
|
|
HandleUevent(dup_ashmem_uevent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 22:12:20 +02:00
|
|
|
void DeviceHandler::HandleUevent(const Uevent& uevent) {
|
2017-06-23 21:43:48 +02:00
|
|
|
if (uevent.action == "add" || uevent.action == "change" || uevent.action == "online") {
|
|
|
|
FixupSysPermissions(uevent.path, uevent.subsystem);
|
2017-04-11 22:53:37 +02:00
|
|
|
}
|
2011-03-31 02:37:17 +02:00
|
|
|
|
2017-04-11 23:19:50 +02:00
|
|
|
// if it's not a /dev device, nothing to do
|
2017-05-26 00:58:59 +02:00
|
|
|
if (uevent.major < 0 || uevent.minor < 0) return;
|
2011-03-31 02:37:17 +02:00
|
|
|
|
2017-04-11 23:19:50 +02:00
|
|
|
std::string devpath;
|
2017-06-23 21:43:48 +02:00
|
|
|
std::vector<std::string> links;
|
|
|
|
bool block = false;
|
|
|
|
|
|
|
|
if (uevent.subsystem == "block") {
|
|
|
|
block = true;
|
|
|
|
devpath = "/dev/block/" + Basename(uevent.path);
|
2013-11-19 00:24:40 +01:00
|
|
|
|
2017-06-23 21:43:48 +02:00
|
|
|
if (StartsWith(uevent.path, "/devices")) {
|
|
|
|
links = GetBlockDeviceSymlinks(uevent);
|
|
|
|
}
|
2017-05-26 00:58:59 +02:00
|
|
|
} else if (const auto subsystem =
|
|
|
|
std::find(subsystems_.cbegin(), subsystems_.cend(), uevent.subsystem);
|
|
|
|
subsystem != subsystems_.cend()) {
|
2017-04-25 01:59:05 +02:00
|
|
|
devpath = subsystem->ParseDevPath(uevent);
|
2017-08-17 18:38:01 +02:00
|
|
|
} else if (uevent.subsystem == "usb") {
|
|
|
|
if (!uevent.device_name.empty()) {
|
|
|
|
devpath = "/dev/" + uevent.device_name;
|
|
|
|
} else {
|
|
|
|
// This imitates the file system that would be created
|
|
|
|
// if we were using devfs instead.
|
|
|
|
// Minors are broken up into groups of 128, starting at "001"
|
|
|
|
int bus_id = uevent.minor / 128 + 1;
|
|
|
|
int device_id = uevent.minor % 128 + 1;
|
|
|
|
devpath = StringPrintf("/dev/bus/usb/%03d/%03d", bus_id, device_id);
|
|
|
|
}
|
|
|
|
} else if (StartsWith(uevent.subsystem, "usb")) {
|
|
|
|
// ignore other USB events
|
|
|
|
return;
|
2020-10-20 04:54:18 +02:00
|
|
|
} else if (uevent.subsystem == "misc" && StartsWith(uevent.device_name, "dm-user/")) {
|
|
|
|
devpath = "/dev/dm-user/" + uevent.device_name.substr(8);
|
2017-04-05 01:30:40 +02:00
|
|
|
} else {
|
2017-06-22 21:53:17 +02:00
|
|
|
devpath = "/dev/" + Basename(uevent.path);
|
2017-04-05 01:30:40 +02:00
|
|
|
}
|
|
|
|
|
2017-08-10 21:22:44 +02:00
|
|
|
mkdir_recursive(Dirname(devpath), 0755);
|
2017-04-25 01:59:05 +02:00
|
|
|
|
2017-06-23 21:43:48 +02:00
|
|
|
HandleDevice(uevent.action, devpath, block, uevent.major, uevent.minor, links);
|
2019-09-24 22:00:43 +02:00
|
|
|
|
|
|
|
// Duplicate /dev/ashmem device and name it /dev/ashmem<boot_id>.
|
|
|
|
// TODO(b/111903542): remove once all users of /dev/ashmem are migrated to libcutils API.
|
|
|
|
HandleAshmemUevent(uevent);
|
2011-03-31 02:37:17 +02:00
|
|
|
}
|
|
|
|
|
2018-08-01 22:12:20 +02:00
|
|
|
void DeviceHandler::ColdbootDone() {
|
2018-11-02 10:06:47 +01:00
|
|
|
skip_restorecon_ = false;
|
2018-08-01 22:12:20 +02:00
|
|
|
}
|
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
DeviceHandler::DeviceHandler(std::vector<Permissions> dev_permissions,
|
|
|
|
std::vector<SysfsPermissions> sysfs_permissions,
|
Adds /dev/block/by-name/<partition> symlinks
During uevent processing, some "by-name" symlinks will be created.
/dev/block/<type>/<device>/by-name/<partition>
<type> can be: platform, pci or vbd.
<device> might be: soc.0/f9824900.sdhci, soc.0/f9824900.sdhci, etc.
<partition> might be: system, vendor, system_a, system_b, etc.
e.g., on a non-A/B device:
/dev/block/platform/soc.0/f9824900.sdhci/by-name/system
/dev/block/platform/soc.0/f9824900.sdhci/by-name/vendor
On a A/B device:
/dev/block/platform/soc/1da4000.ufshc/by-name/system_a
/dev/block/platform/soc/1da4000.ufshc/by-name/system_b
/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_a
/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_b
However, those symlinks are "device-specific".
This change adds the "generic" symlinks in ueventd, in addition to
the existing symlinks, when the possible "boot devices" are specified
in device tree. e.g.,
&firmware_android {
compatible = "android,firmware";
boot_devices ="soc/1da4000.ufshc,soc.0/f9824900.sdhci";
}
The following symlinks will then be created on the aforementioned non-A/B
and A/B devices, respectively.
/dev/block/by-name/system
/dev/block/by-name/vendor
/dev/block/by-name/system_a
/dev/block/by-name/system_b
/dev/block/by-name/vendor_a
/dev/block/by-name/vendor_b
Note that both <type> and <device> are skipped in the newly create symlinks.
It assumes there is no more than one devices with the same <partition>,
which is the assumption of current first stage mount flow.
Finally, when 'boot_devices' in DT is absent, it fallbacks to extract
'boot_devices' from fstab settings. e.g., using 'soc/1da4000.ufshc',
'soc.0/f9824900.sdhci' for a fstab with the following content:
/dev/block/platform/soc/1da4000.ufshc/by-name/system
/dev/block/platform/soc.0/f9824900.sdhci/by-name/vendor
Bug: 78613232
Test: adb shell ls /dev/block/by-name
Change-Id: Iec920b5a72409b6a2bdbeeb290f0a3acd2046b5d
2018-05-16 12:33:44 +02:00
|
|
|
std::vector<Subsystem> subsystems, std::set<std::string> boot_devices,
|
|
|
|
bool skip_restorecon)
|
2017-05-26 00:58:59 +02:00
|
|
|
: dev_permissions_(std::move(dev_permissions)),
|
|
|
|
sysfs_permissions_(std::move(sysfs_permissions)),
|
|
|
|
subsystems_(std::move(subsystems)),
|
Adds /dev/block/by-name/<partition> symlinks
During uevent processing, some "by-name" symlinks will be created.
/dev/block/<type>/<device>/by-name/<partition>
<type> can be: platform, pci or vbd.
<device> might be: soc.0/f9824900.sdhci, soc.0/f9824900.sdhci, etc.
<partition> might be: system, vendor, system_a, system_b, etc.
e.g., on a non-A/B device:
/dev/block/platform/soc.0/f9824900.sdhci/by-name/system
/dev/block/platform/soc.0/f9824900.sdhci/by-name/vendor
On a A/B device:
/dev/block/platform/soc/1da4000.ufshc/by-name/system_a
/dev/block/platform/soc/1da4000.ufshc/by-name/system_b
/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_a
/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_b
However, those symlinks are "device-specific".
This change adds the "generic" symlinks in ueventd, in addition to
the existing symlinks, when the possible "boot devices" are specified
in device tree. e.g.,
&firmware_android {
compatible = "android,firmware";
boot_devices ="soc/1da4000.ufshc,soc.0/f9824900.sdhci";
}
The following symlinks will then be created on the aforementioned non-A/B
and A/B devices, respectively.
/dev/block/by-name/system
/dev/block/by-name/vendor
/dev/block/by-name/system_a
/dev/block/by-name/system_b
/dev/block/by-name/vendor_a
/dev/block/by-name/vendor_b
Note that both <type> and <device> are skipped in the newly create symlinks.
It assumes there is no more than one devices with the same <partition>,
which is the assumption of current first stage mount flow.
Finally, when 'boot_devices' in DT is absent, it fallbacks to extract
'boot_devices' from fstab settings. e.g., using 'soc/1da4000.ufshc',
'soc.0/f9824900.sdhci' for a fstab with the following content:
/dev/block/platform/soc/1da4000.ufshc/by-name/system
/dev/block/platform/soc.0/f9824900.sdhci/by-name/vendor
Bug: 78613232
Test: adb shell ls /dev/block/by-name
Change-Id: Iec920b5a72409b6a2bdbeeb290f0a3acd2046b5d
2018-05-16 12:33:44 +02:00
|
|
|
boot_devices_(std::move(boot_devices)),
|
2017-06-21 21:46:41 +02:00
|
|
|
skip_restorecon_(skip_restorecon),
|
|
|
|
sysfs_mount_point_("/sys") {}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
DeviceHandler::DeviceHandler()
|
|
|
|
: DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{},
|
Adds /dev/block/by-name/<partition> symlinks
During uevent processing, some "by-name" symlinks will be created.
/dev/block/<type>/<device>/by-name/<partition>
<type> can be: platform, pci or vbd.
<device> might be: soc.0/f9824900.sdhci, soc.0/f9824900.sdhci, etc.
<partition> might be: system, vendor, system_a, system_b, etc.
e.g., on a non-A/B device:
/dev/block/platform/soc.0/f9824900.sdhci/by-name/system
/dev/block/platform/soc.0/f9824900.sdhci/by-name/vendor
On a A/B device:
/dev/block/platform/soc/1da4000.ufshc/by-name/system_a
/dev/block/platform/soc/1da4000.ufshc/by-name/system_b
/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_a
/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_b
However, those symlinks are "device-specific".
This change adds the "generic" symlinks in ueventd, in addition to
the existing symlinks, when the possible "boot devices" are specified
in device tree. e.g.,
&firmware_android {
compatible = "android,firmware";
boot_devices ="soc/1da4000.ufshc,soc.0/f9824900.sdhci";
}
The following symlinks will then be created on the aforementioned non-A/B
and A/B devices, respectively.
/dev/block/by-name/system
/dev/block/by-name/vendor
/dev/block/by-name/system_a
/dev/block/by-name/system_b
/dev/block/by-name/vendor_a
/dev/block/by-name/vendor_b
Note that both <type> and <device> are skipped in the newly create symlinks.
It assumes there is no more than one devices with the same <partition>,
which is the assumption of current first stage mount flow.
Finally, when 'boot_devices' in DT is absent, it fallbacks to extract
'boot_devices' from fstab settings. e.g., using 'soc/1da4000.ufshc',
'soc.0/f9824900.sdhci' for a fstab with the following content:
/dev/block/platform/soc/1da4000.ufshc/by-name/system
/dev/block/platform/soc.0/f9824900.sdhci/by-name/vendor
Bug: 78613232
Test: adb shell ls /dev/block/by-name
Change-Id: Iec920b5a72409b6a2bdbeeb290f0a3acd2046b5d
2018-05-16 12:33:44 +02:00
|
|
|
std::vector<Subsystem>{}, std::set<std::string>{}, false) {}
|
2017-06-22 21:53:17 +02:00
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|