summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTreehugger Robot2018-05-18 20:45:21 -0500
committerGerrit Code Review2018-05-18 20:45:21 -0500
commit98214c81d157d4cef7f5ca6cbd6f447e596beb80 (patch)
tree475f2dcbe67504616ca712c66a457630c3b44977
parent4bfbd8e9a53a5c1d23e4d9aa7ba08f0de816a5d8 (diff)
parent8eec38f4e463d8cd980562ec49432c17972cc5cb (diff)
downloadplatform-system-core-98214c81d157d4cef7f5ca6cbd6f447e596beb80.tar.gz
platform-system-core-98214c81d157d4cef7f5ca6cbd6f447e596beb80.tar.xz
platform-system-core-98214c81d157d4cef7f5ca6cbd6f447e596beb80.zip
Merge "Adds /dev/block/by-name/<partition> symlinks"
-rw-r--r--fs_mgr/fs_mgr_fstab.cpp60
-rw-r--r--fs_mgr/include_fstab/fstab/fstab.h2
-rw-r--r--init/devices.cpp20
-rw-r--r--init/devices.h6
-rw-r--r--init/init_first_stage.cpp15
-rw-r--r--init/ueventd.cpp4
6 files changed, 95 insertions, 12 deletions
diff --git a/fs_mgr/fs_mgr_fstab.cpp b/fs_mgr/fs_mgr_fstab.cpp
index 472ab59c7..af4d6c1e1 100644
--- a/fs_mgr/fs_mgr_fstab.cpp
+++ b/fs_mgr/fs_mgr_fstab.cpp
@@ -686,6 +686,49 @@ static struct fstab *in_place_merge(struct fstab *a, struct fstab *b)
686 return a; 686 return a;
687} 687}
688 688
689/* Extracts <device>s from the by-name symlinks specified in a fstab:
690 * /dev/block/<type>/<device>/by-name/<partition>
691 *
692 * <type> can be: platform, pci or vbd.
693 *
694 * For example, given the following entries in the input fstab:
695 * /dev/block/platform/soc/1da4000.ufshc/by-name/system
696 * /dev/block/pci/soc.0/f9824900.sdhci/by-name/vendor
697 * it returns a set { "soc/1da4000.ufshc", "soc.0/f9824900.sdhci" }.
698 */
699static std::set<std::string> extract_boot_devices(const fstab& fstab) {
700 std::set<std::string> boot_devices;
701
702 for (int i = 0; i < fstab.num_entries; i++) {
703 std::string blk_device(fstab.recs[i].blk_device);
704 // Skips blk_device that doesn't conform to the format.
705 if (!android::base::StartsWith(blk_device, "/dev/block") ||
706 android::base::StartsWith(blk_device, "/dev/block/by-name") ||
707 android::base::StartsWith(blk_device, "/dev/block/bootdevice/by-name")) {
708 continue;
709 }
710 // Skips non-by_name blk_device.
711 // /dev/block/<type>/<device>/by-name/<partition>
712 // ^ slash_by_name
713 auto slash_by_name = blk_device.find("/by-name");
714 if (slash_by_name == std::string::npos) continue;
715 blk_device.erase(slash_by_name); // erases /by-name/<partition>
716
717 // Erases /dev/block/, now we have <type>/<device>
718 blk_device.erase(0, std::string("/dev/block/").size());
719
720 // <type>/<device>
721 // ^ first_slash
722 auto first_slash = blk_device.find('/');
723 if (first_slash == std::string::npos) continue;
724
725 auto boot_device = blk_device.substr(first_slash + 1);
726 if (!boot_device.empty()) boot_devices.insert(std::move(boot_device));
727 }
728
729 return boot_devices;
730}
731
689struct fstab *fs_mgr_read_fstab(const char *fstab_path) 732struct fstab *fs_mgr_read_fstab(const char *fstab_path)
690{ 733{
691 FILE *fstab_file; 734 FILE *fstab_file;
@@ -863,6 +906,23 @@ struct fstab_rec* fs_mgr_get_entry_for_mount_point(struct fstab* fstab, const st
863 return nullptr; 906 return nullptr;
864} 907}
865 908
909std::set<std::string> fs_mgr_get_boot_devices() {
910 // boot_devices can be specified in device tree.
911 std::string dt_value;
912 std::string file_name = get_android_dt_dir() + "/boot_devices";
913 if (read_dt_file(file_name, &dt_value)) {
914 auto boot_devices = android::base::Split(dt_value, ",");
915 return std::set<std::string>(boot_devices.begin(), boot_devices.end());
916 }
917
918 // Fallback to extract boot devices from fstab.
919 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
920 fs_mgr_free_fstab);
921 if (fstab) return extract_boot_devices(*fstab);
922
923 return {};
924}
925
866int fs_mgr_is_voldmanaged(const struct fstab_rec *fstab) 926int fs_mgr_is_voldmanaged(const struct fstab_rec *fstab)
867{ 927{
868 return fstab->fs_mgr_flags & MF_VOLDMANAGED; 928 return fstab->fs_mgr_flags & MF_VOLDMANAGED;
diff --git a/fs_mgr/include_fstab/fstab/fstab.h b/fs_mgr/include_fstab/fstab/fstab.h
index 04ccfc5c6..d232cca86 100644
--- a/fs_mgr/include_fstab/fstab/fstab.h
+++ b/fs_mgr/include_fstab/fstab/fstab.h
@@ -22,6 +22,7 @@
22#include <stdint.h> 22#include <stdint.h>
23#include <stdio.h> 23#include <stdio.h>
24 24
25#include <set>
25#include <string> 26#include <string>
26 27
27/* 28/*
@@ -89,5 +90,6 @@ int fs_mgr_is_logical(const struct fstab_rec* fstab);
89int fs_mgr_has_sysfs_path(const struct fstab_rec* fstab); 90int fs_mgr_has_sysfs_path(const struct fstab_rec* fstab);
90 91
91std::string fs_mgr_get_slot_suffix(); 92std::string fs_mgr_get_slot_suffix();
93std::set<std::string> fs_mgr_get_boot_devices();
92 94
93#endif /* __CORE_FS_TAB_H */ 95#endif /* __CORE_FS_TAB_H */
diff --git a/init/devices.cpp b/init/devices.cpp
index 688ad6196..ada1e2870 100644
--- a/init/devices.cpp
+++ b/init/devices.cpp
@@ -329,6 +329,10 @@ std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uev
329 << partition_name_sanitized << "'"; 329 << partition_name_sanitized << "'";
330 } 330 }
331 links.emplace_back(link_path + "/by-name/" + partition_name_sanitized); 331 links.emplace_back(link_path + "/by-name/" + partition_name_sanitized);
332 // Adds symlink: /dev/block/by-name/<partition_name>.
333 if (boot_devices_.find(device) != boot_devices_.end()) {
334 links.emplace_back("/dev/block/by-name/" + partition_name_sanitized);
335 }
332 } 336 }
333 337
334 auto last_slash = uevent.path.rfind('/'); 338 auto last_slash = uevent.path.rfind('/');
@@ -346,8 +350,14 @@ void DeviceHandler::HandleDevice(const std::string& action, const std::string& d
346 PLOG(ERROR) << "Failed to create directory " << Dirname(link); 350 PLOG(ERROR) << "Failed to create directory " << Dirname(link);
347 } 351 }
348 352
349 if (symlink(devpath.c_str(), link.c_str()) && errno != EEXIST) { 353 if (symlink(devpath.c_str(), link.c_str())) {
350 PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link; 354 if (errno != EEXIST) {
355 PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link;
356 } else if (std::string link_path;
357 Readlink(link, &link_path) && link_path != devpath) {
358 PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link
359 << ", which already links to: " << link_path;
360 }
351 } 361 }
352 } 362 }
353 } 363 }
@@ -411,16 +421,18 @@ void DeviceHandler::HandleDeviceEvent(const Uevent& uevent) {
411 421
412DeviceHandler::DeviceHandler(std::vector<Permissions> dev_permissions, 422DeviceHandler::DeviceHandler(std::vector<Permissions> dev_permissions,
413 std::vector<SysfsPermissions> sysfs_permissions, 423 std::vector<SysfsPermissions> sysfs_permissions,
414 std::vector<Subsystem> subsystems, bool skip_restorecon) 424 std::vector<Subsystem> subsystems, std::set<std::string> boot_devices,
425 bool skip_restorecon)
415 : dev_permissions_(std::move(dev_permissions)), 426 : dev_permissions_(std::move(dev_permissions)),
416 sysfs_permissions_(std::move(sysfs_permissions)), 427 sysfs_permissions_(std::move(sysfs_permissions)),
417 subsystems_(std::move(subsystems)), 428 subsystems_(std::move(subsystems)),
429 boot_devices_(std::move(boot_devices)),
418 skip_restorecon_(skip_restorecon), 430 skip_restorecon_(skip_restorecon),
419 sysfs_mount_point_("/sys") {} 431 sysfs_mount_point_("/sys") {}
420 432
421DeviceHandler::DeviceHandler() 433DeviceHandler::DeviceHandler()
422 : DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{}, 434 : DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{},
423 std::vector<Subsystem>{}, false) {} 435 std::vector<Subsystem>{}, std::set<std::string>{}, false) {}
424 436
425} // namespace init 437} // namespace init
426} // namespace android 438} // namespace android
diff --git a/init/devices.h b/init/devices.h
index 1f8f1e8a9..f9035da3d 100644
--- a/init/devices.h
+++ b/init/devices.h
@@ -21,6 +21,7 @@
21#include <sys/types.h> 21#include <sys/types.h>
22 22
23#include <algorithm> 23#include <algorithm>
24#include <set>
24#include <string> 25#include <string>
25#include <vector> 26#include <vector>
26 27
@@ -103,8 +104,8 @@ class DeviceHandler {
103 104
104 DeviceHandler(); 105 DeviceHandler();
105 DeviceHandler(std::vector<Permissions> dev_permissions, 106 DeviceHandler(std::vector<Permissions> dev_permissions,
106 std::vector<SysfsPermissions> sysfs_permissions, 107 std::vector<SysfsPermissions> sysfs_permissions, std::vector<Subsystem> subsystems,
107 std::vector<Subsystem> subsystems, bool skip_restorecon); 108 std::set<std::string> boot_devices, bool skip_restorecon);
108 ~DeviceHandler(){}; 109 ~DeviceHandler(){};
109 110
110 void HandleDeviceEvent(const Uevent& uevent); 111 void HandleDeviceEvent(const Uevent& uevent);
@@ -125,6 +126,7 @@ class DeviceHandler {
125 std::vector<Permissions> dev_permissions_; 126 std::vector<Permissions> dev_permissions_;
126 std::vector<SysfsPermissions> sysfs_permissions_; 127 std::vector<SysfsPermissions> sysfs_permissions_;
127 std::vector<Subsystem> subsystems_; 128 std::vector<Subsystem> subsystems_;
129 std::set<std::string> boot_devices_;
128 bool skip_restorecon_; 130 bool skip_restorecon_;
129 std::string sysfs_mount_point_; 131 std::string sysfs_mount_point_;
130}; 132};
diff --git a/init/init_first_stage.cpp b/init/init_first_stage.cpp
index 28df69658..34de640f3 100644
--- a/init/init_first_stage.cpp
+++ b/init/init_first_stage.cpp
@@ -77,7 +77,7 @@ class FirstStageMount {
77 std::unique_ptr<LogicalPartitionTable> dm_linear_table_; 77 std::unique_ptr<LogicalPartitionTable> dm_linear_table_;
78 std::vector<fstab_rec*> mount_fstab_recs_; 78 std::vector<fstab_rec*> mount_fstab_recs_;
79 std::set<std::string> required_devices_partition_names_; 79 std::set<std::string> required_devices_partition_names_;
80 DeviceHandler device_handler_; 80 std::unique_ptr<DeviceHandler> device_handler_;
81 UeventListener uevent_listener_; 81 UeventListener uevent_listener_;
82}; 82};
83 83
@@ -147,6 +147,11 @@ FirstStageMount::FirstStageMount()
147 if (IsDmLinearEnabled()) { 147 if (IsDmLinearEnabled()) {
148 dm_linear_table_ = android::fs_mgr::LoadPartitionsFromDeviceTree(); 148 dm_linear_table_ = android::fs_mgr::LoadPartitionsFromDeviceTree();
149 } 149 }
150
151 auto boot_devices = fs_mgr_get_boot_devices();
152 device_handler_ =
153 std::make_unique<DeviceHandler>(std::vector<Permissions>{}, std::vector<SysfsPermissions>{},
154 std::vector<Subsystem>{}, std::move(boot_devices), false);
150} 155}
151 156
152std::unique_ptr<FirstStageMount> FirstStageMount::Create() { 157std::unique_ptr<FirstStageMount> FirstStageMount::Create() {
@@ -205,7 +210,7 @@ bool FirstStageMount::InitRequiredDevices() {
205 bool found = false; 210 bool found = false;
206 auto dm_callback = [this, &dm_path, &found](const Uevent& uevent) { 211 auto dm_callback = [this, &dm_path, &found](const Uevent& uevent) {
207 if (uevent.path == dm_path) { 212 if (uevent.path == dm_path) {
208 device_handler_.HandleDeviceEvent(uevent); 213 device_handler_->HandleDeviceEvent(uevent);
209 found = true; 214 found = true;
210 return ListenerAction::kStop; 215 return ListenerAction::kStop;
211 } 216 }
@@ -262,7 +267,7 @@ ListenerAction FirstStageMount::HandleBlockDevice(const std::string& name, const
262 if (iter != required_devices_partition_names_.end()) { 267 if (iter != required_devices_partition_names_.end()) {
263 LOG(VERBOSE) << __PRETTY_FUNCTION__ << ": found partition: " << *iter; 268 LOG(VERBOSE) << __PRETTY_FUNCTION__ << ": found partition: " << *iter;
264 required_devices_partition_names_.erase(iter); 269 required_devices_partition_names_.erase(iter);
265 device_handler_.HandleDeviceEvent(uevent); 270 device_handler_->HandleDeviceEvent(uevent);
266 if (required_devices_partition_names_.empty()) { 271 if (required_devices_partition_names_.empty()) {
267 return ListenerAction::kStop; 272 return ListenerAction::kStop;
268 } else { 273 } else {
@@ -299,7 +304,7 @@ bool FirstStageMount::InitMappedDevice(const std::string& dm_device) {
299 auto verity_callback = [&device_name, &dm_device, this, &found](const Uevent& uevent) { 304 auto verity_callback = [&device_name, &dm_device, this, &found](const Uevent& uevent) {
300 if (uevent.device_name == device_name) { 305 if (uevent.device_name == device_name) {
301 LOG(VERBOSE) << "Creating device-mapper device : " << dm_device; 306 LOG(VERBOSE) << "Creating device-mapper device : " << dm_device;
302 device_handler_.HandleDeviceEvent(uevent); 307 device_handler_->HandleDeviceEvent(uevent);
303 found = true; 308 found = true;
304 return ListenerAction::kStop; 309 return ListenerAction::kStop;
305 } 310 }
@@ -469,7 +474,7 @@ ListenerAction FirstStageMountVBootV2::UeventCallback(const Uevent& uevent) {
469 // is not empty. e.g., 474 // is not empty. e.g.,
470 // - /dev/block/platform/soc.0/f9824900.sdhci/by-name/modem 475 // - /dev/block/platform/soc.0/f9824900.sdhci/by-name/modem
471 // - /dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1 476 // - /dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1
472 std::vector<std::string> links = device_handler_.GetBlockDeviceSymlinks(uevent); 477 std::vector<std::string> links = device_handler_->GetBlockDeviceSymlinks(uevent);
473 if (!links.empty()) { 478 if (!links.empty()) {
474 auto[it, inserted] = by_name_symlink_map_.emplace(uevent.partition_name, links[0]); 479 auto[it, inserted] = by_name_symlink_map_.emplace(uevent.partition_name, links[0]);
475 if (!inserted) { 480 if (!inserted) {
diff --git a/init/ueventd.cpp b/init/ueventd.cpp
index 1435d82ef..a284203ba 100644
--- a/init/ueventd.cpp
+++ b/init/ueventd.cpp
@@ -30,6 +30,7 @@
30#include <android-base/chrono_utils.h> 30#include <android-base/chrono_utils.h>
31#include <android-base/logging.h> 31#include <android-base/logging.h>
32#include <android-base/properties.h> 32#include <android-base/properties.h>
33#include <fstab/fstab.h>
33#include <selinux/android.h> 34#include <selinux/android.h>
34#include <selinux/selinux.h> 35#include <selinux/selinux.h>
35 36
@@ -242,8 +243,9 @@ DeviceHandler CreateDeviceHandler() {
242 std::string hardware = android::base::GetProperty("ro.hardware", ""); 243 std::string hardware = android::base::GetProperty("ro.hardware", "");
243 parser.ParseConfig("/ueventd." + hardware + ".rc"); 244 parser.ParseConfig("/ueventd." + hardware + ".rc");
244 245
246 auto boot_devices = fs_mgr_get_boot_devices();
245 return DeviceHandler(std::move(dev_permissions), std::move(sysfs_permissions), 247 return DeviceHandler(std::move(dev_permissions), std::move(sysfs_permissions),
246 std::move(subsystems), true); 248 std::move(subsystems), std::move(boot_devices), true);
247} 249}
248 250
249int ueventd_main(int argc, char** argv) { 251int ueventd_main(int argc, char** argv) {