Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2011 The Android Open Source Project |
| 3 | // |
| 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 | // |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/postinstall_runner_action.h" |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 18 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 20 | #include <signal.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 | #include <stdlib.h> |
Alex Vakulenko | 44cab30 | 2014-07-23 13:12:15 -0700 | [diff] [blame] | 22 | #include <sys/mount.h> |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 23 | #include <sys/types.h> |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 24 | #include <unistd.h> |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 25 | |
Alex Deymo | 44b3567 | 2016-04-05 17:57:48 -0700 | [diff] [blame] | 26 | #include <cmath> |
Kelvin Zhang | ddc2580 | 2021-12-30 13:05:27 -0800 | [diff] [blame] | 27 | #include <fstream> |
| 28 | #include <string> |
Alex Deymo | 44b3567 | 2016-04-05 17:57:48 -0700 | [diff] [blame] | 29 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 30 | #include <base/files/file_path.h> |
| 31 | #include <base/files/file_util.h> |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 32 | #include <base/logging.h> |
hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame] | 33 | #include <base/stl_util.h> |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 34 | #include <base/strings/string_split.h> |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 35 | #include <base/strings/string_util.h> |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 36 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 37 | #include "update_engine/common/action_processor.h" |
Alex Deymo | b15a0b8 | 2015-11-25 20:30:40 -0300 | [diff] [blame] | 38 | #include "update_engine/common/boot_control_interface.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 39 | #include "update_engine/common/subprocess.h" |
| 40 | #include "update_engine/common/utils.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 41 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 42 | namespace { |
| 43 | |
| 44 | // The file descriptor number from the postinstall program's perspective where |
| 45 | // it can report status updates. This can be any number greater than 2 (stderr), |
| 46 | // but must be kept in sync with the "bin/postinst_progress" defined in the |
| 47 | // sample_images.sh file. |
| 48 | const int kPostinstallStatusFd = 3; |
| 49 | |
Kelvin Zhang | ddc2580 | 2021-12-30 13:05:27 -0800 | [diff] [blame] | 50 | static constexpr bool Contains(std::string_view haystack, |
| 51 | std::string_view needle) { |
| 52 | return haystack.find(needle) != std::string::npos; |
| 53 | } |
| 54 | |
| 55 | static void LogBuildInfoForPartition(std::string_view mount_point) { |
| 56 | static constexpr std::array<std::string_view, 3> kBuildPropFiles{ |
| 57 | "build.prop", "etc/build.prop", "system/build.prop"}; |
| 58 | for (const auto& file : kBuildPropFiles) { |
| 59 | auto path = std::string(mount_point); |
| 60 | if (path.back() != '/') { |
| 61 | path.push_back('/'); |
| 62 | } |
| 63 | path += file; |
| 64 | LOG(INFO) << "Trying to read " << path; |
| 65 | std::ifstream infile(path); |
| 66 | std::string line; |
| 67 | while (std::getline(infile, line)) { |
| 68 | if (Contains(line, "ro.build")) { |
| 69 | LOG(INFO) << line; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 75 | } // namespace |
| 76 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 77 | namespace chromeos_update_engine { |
| 78 | |
| 79 | using std::string; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 80 | using std::vector; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 81 | |
Kelvin Zhang | e9def4e | 2020-12-02 14:04:09 -0500 | [diff] [blame] | 82 | PostinstallRunnerAction::PostinstallRunnerAction( |
| 83 | BootControlInterface* boot_control, HardwareInterface* hardware) |
| 84 | : boot_control_(boot_control), hardware_(hardware) { |
| 85 | #ifdef __ANDROID__ |
| 86 | fs_mount_dir_ = "/postinstall"; |
| 87 | #else // __ANDROID__ |
| 88 | base::FilePath temp_dir; |
| 89 | TEST_AND_RETURN(base::CreateNewTempDirectory("au_postint_mount", &temp_dir)); |
| 90 | fs_mount_dir_ = temp_dir.value(); |
| 91 | #endif // __ANDROID__ |
Kelvin Zhang | 2379fa9 | 2020-12-09 14:39:04 -0500 | [diff] [blame] | 92 | CHECK(!fs_mount_dir_.empty()); |
Kelvin Zhang | 1df000a | 2022-02-09 16:00:17 -0800 | [diff] [blame] | 93 | EnsureUnmounted(); |
Kelvin Zhang | 2379fa9 | 2020-12-09 14:39:04 -0500 | [diff] [blame] | 94 | LOG(INFO) << "postinstall mount point: " << fs_mount_dir_; |
Kelvin Zhang | e9def4e | 2020-12-02 14:04:09 -0500 | [diff] [blame] | 95 | } |
| 96 | |
Kelvin Zhang | 1df000a | 2022-02-09 16:00:17 -0800 | [diff] [blame] | 97 | void PostinstallRunnerAction::EnsureUnmounted() { |
| 98 | if (utils::IsMountpoint(fs_mount_dir_)) { |
| 99 | LOG(INFO) << "Found previously mounted filesystem at " << fs_mount_dir_; |
| 100 | utils::UnmountFilesystem(fs_mount_dir_); |
| 101 | } |
| 102 | } |
| 103 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 104 | void PostinstallRunnerAction::PerformAction() { |
| 105 | CHECK(HasInputObject()); |
Kelvin Zhang | 8b1e0dc | 2020-10-26 12:27:53 -0400 | [diff] [blame] | 106 | CHECK(boot_control_); |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 107 | install_plan_ = GetInputObject(); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 108 | |
Kelvin Zhang | 8b1e0dc | 2020-10-26 12:27:53 -0400 | [diff] [blame] | 109 | auto dynamic_control = boot_control_->GetDynamicPartitionControl(); |
| 110 | CHECK(dynamic_control); |
| 111 | |
Kelvin Zhang | 19acf4f | 2024-01-08 21:18:28 +0000 | [diff] [blame] | 112 | // Mount snapshot partitions for Virtual AB Compression Compression. |
Kelvin Zhang | 0618835 | 2021-02-10 13:21:47 -0500 | [diff] [blame] | 113 | if (dynamic_control->UpdateUsesSnapshotCompression()) { |
Kelvin Zhang | 8b1e0dc | 2020-10-26 12:27:53 -0400 | [diff] [blame] | 114 | // Before calling MapAllPartitions to map snapshot devices, all CowWriters |
| 115 | // must be closed, and MapAllPartitions() should be called. |
Kelvin Zhang | 263a540 | 2022-12-08 23:03:32 -0800 | [diff] [blame] | 116 | if (!install_plan_.partitions.empty()) { |
Kelvin Zhang | 263a540 | 2022-12-08 23:03:32 -0800 | [diff] [blame] | 117 | if (!dynamic_control->MapAllPartitions()) { |
| 118 | return CompletePostinstall(ErrorCode::kPostInstallMountError); |
| 119 | } |
Kelvin Zhang | 8b1e0dc | 2020-10-26 12:27:53 -0400 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Zentaro Kavanagh | 28def4f | 2019-01-15 17:15:01 -0800 | [diff] [blame] | 123 | // We always powerwash when rolling back, however policy can determine |
| 124 | // if this is a full/normal powerwash, or a special rollback powerwash |
| 125 | // that retains a small amount of system state such as enrollment and |
| 126 | // network configuration. In both cases all user accounts are deleted. |
Marton Hunyady | 199152d | 2018-05-07 19:08:48 +0200 | [diff] [blame] | 127 | if (install_plan_.powerwash_required || install_plan_.is_rollback) { |
Miriam Polzer | aff7200 | 2020-08-27 08:20:39 +0200 | [diff] [blame] | 128 | if (hardware_->SchedulePowerwash( |
| 129 | install_plan_.rollback_data_save_requested)) { |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 130 | powerwash_scheduled_ = true; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 131 | } else { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 132 | return CompletePostinstall(ErrorCode::kPostinstallPowerwashError); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 136 | // Initialize all the partition weights. |
| 137 | partition_weight_.resize(install_plan_.partitions.size()); |
| 138 | total_weight_ = 0; |
| 139 | for (size_t i = 0; i < install_plan_.partitions.size(); ++i) { |
Tianjie Xu | 087de9d | 2019-11-01 17:11:22 -0700 | [diff] [blame] | 140 | auto& partition = install_plan_.partitions[i]; |
| 141 | if (!install_plan_.run_post_install && partition.postinstall_optional) { |
| 142 | partition.run_postinstall = false; |
| 143 | LOG(INFO) << "Skipping optional post-install for partition " |
| 144 | << partition.name << " according to install plan."; |
| 145 | } |
| 146 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 147 | // TODO(deymo): This code sets the weight to all the postinstall commands, |
| 148 | // but we could remember how long they took in the past and use those |
| 149 | // values. |
Tianjie Xu | 087de9d | 2019-11-01 17:11:22 -0700 | [diff] [blame] | 150 | partition_weight_[i] = partition.run_postinstall; |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 151 | total_weight_ += partition_weight_[i]; |
| 152 | } |
| 153 | accumulated_weight_ = 0; |
| 154 | ReportProgress(0); |
| 155 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 156 | PerformPartitionPostinstall(); |
| 157 | } |
| 158 | |
Kelvin Zhang | 06e654a | 2021-09-10 13:21:00 -0700 | [diff] [blame] | 159 | bool PostinstallRunnerAction::MountPartition( |
| 160 | const InstallPlan::Partition& partition) noexcept { |
| 161 | // Perform post-install for the current_partition_ partition. At this point we |
| 162 | // need to call CompletePartitionPostinstall to complete the operation and |
| 163 | // cleanup. |
| 164 | const auto mountable_device = partition.readonly_target_path; |
| 165 | if (!utils::FileExists(mountable_device.c_str())) { |
| 166 | LOG(ERROR) << "Mountable device " << mountable_device << " for partition " |
| 167 | << partition.name << " does not exist"; |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | if (!utils::FileExists(fs_mount_dir_.c_str())) { |
| 172 | LOG(ERROR) << "Mount point " << fs_mount_dir_ |
| 173 | << " does not exist, mount call will fail"; |
| 174 | return false; |
| 175 | } |
| 176 | // Double check that the fs_mount_dir is not busy with a previous mounted |
| 177 | // filesystem from a previous crashed postinstall step. |
Kelvin Zhang | 1df000a | 2022-02-09 16:00:17 -0800 | [diff] [blame] | 178 | EnsureUnmounted(); |
Kelvin Zhang | 06e654a | 2021-09-10 13:21:00 -0700 | [diff] [blame] | 179 | |
| 180 | #ifdef __ANDROID__ |
| 181 | // In Chromium OS, the postinstall step is allowed to write to the block |
| 182 | // device on the target image, so we don't mark it as read-only and should |
| 183 | // be read-write since we just wrote to it during the update. |
| 184 | |
| 185 | // Mark the block device as read-only before mounting for post-install. |
| 186 | if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) { |
| 187 | return false; |
| 188 | } |
| 189 | #endif // __ANDROID__ |
| 190 | |
| 191 | if (!utils::MountFilesystem( |
| 192 | mountable_device, |
| 193 | fs_mount_dir_, |
| 194 | MS_RDONLY, |
| 195 | partition.filesystem_type, |
| 196 | hardware_->GetPartitionMountOptions(partition.name))) { |
| 197 | return false; |
| 198 | } |
| 199 | return true; |
| 200 | } |
| 201 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 202 | void PostinstallRunnerAction::PerformPartitionPostinstall() { |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 203 | if (install_plan_.download_url.empty()) { |
Kelvin Zhang | 24287c3 | 2023-03-09 10:13:26 -0800 | [diff] [blame] | 204 | LOG(INFO) << "Skipping post-install"; |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 205 | return CompletePostinstall(ErrorCode::kSuccess); |
| 206 | } |
| 207 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 208 | // Skip all the partitions that don't have a post-install step. |
| 209 | while (current_partition_ < install_plan_.partitions.size() && |
| 210 | !install_plan_.partitions[current_partition_].run_postinstall) { |
| 211 | VLOG(1) << "Skipping post-install on partition " |
| 212 | << install_plan_.partitions[current_partition_].name; |
Kelvin Zhang | 06e654a | 2021-09-10 13:21:00 -0700 | [diff] [blame] | 213 | // Attempt to mount a device if it has postinstall script configured, even |
| 214 | // if we want to skip running postinstall script. |
| 215 | // This is because we've seen bugs like b/198787355 which is only triggered |
| 216 | // when you attempt to mount a device. If device fails to mount, it will |
| 217 | // likely fail to mount during boot anyway, so it's better to catch any |
| 218 | // issues earlier. |
| 219 | // It's possible that some of the partitions aren't mountable, but these |
| 220 | // partitions shouldn't have postinstall configured. Therefore we guard this |
| 221 | // logic with |postinstall_path.empty()|. |
| 222 | const auto& partition = install_plan_.partitions[current_partition_]; |
| 223 | if (!partition.postinstall_path.empty()) { |
| 224 | const auto mountable_device = partition.readonly_target_path; |
| 225 | if (!MountPartition(partition)) { |
| 226 | return CompletePostinstall(ErrorCode::kPostInstallMountError); |
| 227 | } |
Kelvin Zhang | ddc2580 | 2021-12-30 13:05:27 -0800 | [diff] [blame] | 228 | LogBuildInfoForPartition(fs_mount_dir_); |
Kelvin Zhang | 06e654a | 2021-09-10 13:21:00 -0700 | [diff] [blame] | 229 | if (!utils::UnmountFilesystem(fs_mount_dir_)) { |
| 230 | return CompletePartitionPostinstall( |
| 231 | 1, "Error unmounting the device " + mountable_device); |
| 232 | } |
| 233 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 234 | current_partition_++; |
| 235 | } |
| 236 | if (current_partition_ == install_plan_.partitions.size()) |
| 237 | return CompletePostinstall(ErrorCode::kSuccess); |
| 238 | |
| 239 | const InstallPlan::Partition& partition = |
| 240 | install_plan_.partitions[current_partition_]; |
| 241 | |
Kelvin Zhang | a9b5d8c | 2021-05-05 09:17:46 -0400 | [diff] [blame] | 242 | const string mountable_device = partition.readonly_target_path; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 243 | // Perform post-install for the current_partition_ partition. At this point we |
| 244 | // need to call CompletePartitionPostinstall to complete the operation and |
| 245 | // cleanup. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 246 | |
Kelvin Zhang | 06e654a | 2021-09-10 13:21:00 -0700 | [diff] [blame] | 247 | if (!MountPartition(partition)) { |
| 248 | CompletePostinstall(ErrorCode::kPostInstallMountError); |
| 249 | return; |
Kelvin Zhang | 4ce0110 | 2020-11-16 09:32:08 -0500 | [diff] [blame] | 250 | } |
Kelvin Zhang | ddc2580 | 2021-12-30 13:05:27 -0800 | [diff] [blame] | 251 | LogBuildInfoForPartition(fs_mount_dir_); |
Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 252 | base::FilePath postinstall_path(partition.postinstall_path); |
| 253 | if (postinstall_path.IsAbsolute()) { |
| 254 | LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative" |
| 255 | "path instead: " |
| 256 | << partition.postinstall_path; |
| 257 | return CompletePostinstall(ErrorCode::kPostinstallRunnerError); |
| 258 | } |
| 259 | |
| 260 | string abs_path = |
| 261 | base::FilePath(fs_mount_dir_).Append(postinstall_path).value(); |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 262 | if (!base::StartsWith( |
| 263 | abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) { |
| 264 | LOG(ERROR) << "Invalid relative postinstall path: " |
| 265 | << partition.postinstall_path; |
| 266 | return CompletePostinstall(ErrorCode::kPostinstallRunnerError); |
| 267 | } |
| 268 | |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 269 | LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at " |
Kelvin Zhang | be1c180 | 2021-06-21 10:03:36 -0400 | [diff] [blame] | 270 | << abs_path << ") installed on mountable device " |
| 271 | << mountable_device; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 272 | |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 273 | // Logs the file format of the postinstall script we are about to run. This |
| 274 | // will help debug when the postinstall script doesn't match the architecture |
| 275 | // of our build. |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 276 | LOG(INFO) << "Format file for new " << partition.postinstall_path |
| 277 | << " is: " << utils::GetFileFormat(abs_path); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 278 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 279 | // Runs the postinstall script asynchronously to free up the main loop while |
| 280 | // it's running. |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 281 | vector<string> command = {abs_path}; |
| 282 | #ifdef __ANDROID__ |
| 283 | // In Brillo and Android, we pass the slot number and status fd. |
| 284 | command.push_back(std::to_string(install_plan_.target_slot)); |
| 285 | command.push_back(std::to_string(kPostinstallStatusFd)); |
| 286 | #else |
| 287 | // Chrome OS postinstall expects the target rootfs as the first parameter. |
| 288 | command.push_back(partition.target_path); |
| 289 | #endif // __ANDROID__ |
| 290 | |
| 291 | current_command_ = Subprocess::Get().ExecFlags( |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 292 | command, |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 293 | Subprocess::kRedirectStderrToStdout, |
| 294 | {kPostinstallStatusFd}, |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 295 | base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall, |
| 296 | base::Unretained(this))); |
| 297 | // Subprocess::Exec should never return a negative process id. |
| 298 | CHECK_GE(current_command_, 0); |
| 299 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 300 | if (!current_command_) { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 301 | CompletePartitionPostinstall(1, "Postinstall didn't launch"); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 302 | return; |
| 303 | } |
| 304 | |
| 305 | // Monitor the status file descriptor. |
| 306 | progress_fd_ = |
| 307 | Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd); |
| 308 | int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK; |
| 309 | if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) { |
| 310 | PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_; |
| 311 | } |
| 312 | |
Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 313 | progress_controller_ = base::FileDescriptorWatcher::WatchReadable( |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 314 | progress_fd_, |
Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 315 | base::BindRepeating(&PostinstallRunnerAction::OnProgressFdReady, |
| 316 | base::Unretained(this))); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 319 | void PostinstallRunnerAction::OnProgressFdReady() { |
| 320 | char buf[1024]; |
| 321 | size_t bytes_read; |
| 322 | do { |
| 323 | bytes_read = 0; |
| 324 | bool eof; |
| 325 | bool ok = |
hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame] | 326 | utils::ReadAll(progress_fd_, buf, base::size(buf), &bytes_read, &eof); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 327 | progress_buffer_.append(buf, bytes_read); |
| 328 | // Process every line. |
| 329 | vector<string> lines = base::SplitString( |
| 330 | progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 331 | if (!lines.empty()) { |
| 332 | progress_buffer_ = lines.back(); |
| 333 | lines.pop_back(); |
| 334 | for (const auto& line : lines) { |
| 335 | ProcessProgressLine(line); |
| 336 | } |
| 337 | } |
| 338 | if (!ok || eof) { |
| 339 | // There was either an error or an EOF condition, so we are done watching |
| 340 | // the file descriptor. |
Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 341 | progress_controller_.reset(); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | } while (bytes_read); |
| 345 | } |
| 346 | |
| 347 | bool PostinstallRunnerAction::ProcessProgressLine(const string& line) { |
| 348 | double frac = 0; |
Alex Deymo | a2ea1c2 | 2016-08-24 17:26:19 -0700 | [diff] [blame] | 349 | if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 && |
| 350 | !std::isnan(frac)) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 351 | ReportProgress(frac); |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | void PostinstallRunnerAction::ReportProgress(double frac) { |
| 359 | if (!delegate_) |
| 360 | return; |
Yoshitaka Ishida | 128936f | 2018-02-16 18:20:07 +0900 | [diff] [blame] | 361 | if (current_partition_ >= partition_weight_.size() || total_weight_ == 0) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 362 | delegate_->ProgressUpdate(1.); |
| 363 | return; |
| 364 | } |
Alex Deymo | 44b3567 | 2016-04-05 17:57:48 -0700 | [diff] [blame] | 365 | if (!std::isfinite(frac) || frac < 0) |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 366 | frac = 0; |
| 367 | if (frac > 1) |
| 368 | frac = 1; |
| 369 | double postinst_action_progress = |
| 370 | (accumulated_weight_ + partition_weight_[current_partition_] * frac) / |
| 371 | total_weight_; |
| 372 | delegate_->ProgressUpdate(postinst_action_progress); |
| 373 | } |
| 374 | |
| 375 | void PostinstallRunnerAction::Cleanup() { |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 376 | utils::UnmountFilesystem(fs_mount_dir_); |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 377 | #ifndef __ANDROID__ |
Kelvin Zhang | 4aeaa12 | 2020-12-04 13:28:47 -0500 | [diff] [blame] | 378 | #if BASE_VER < 800000 |
| 379 | if (!base::DeleteFile(base::FilePath(fs_mount_dir_), true)) { |
| 380 | #else |
hscham | 043355b | 2020-11-17 16:50:10 +0900 | [diff] [blame] | 381 | if (!base::DeleteFile(base::FilePath(fs_mount_dir_))) { |
Kelvin Zhang | 4aeaa12 | 2020-12-04 13:28:47 -0500 | [diff] [blame] | 382 | #endif |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 383 | PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 384 | } |
Kelvin Zhang | 4aeaa12 | 2020-12-04 13:28:47 -0500 | [diff] [blame] | 385 | #endif |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 386 | |
| 387 | progress_fd_ = -1; |
Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 388 | progress_controller_.reset(); |
Tianjie | 55abd3c | 2020-06-19 00:22:59 -0700 | [diff] [blame] | 389 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 390 | progress_buffer_.clear(); |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void PostinstallRunnerAction::CompletePartitionPostinstall( |
| 394 | int return_code, const string& output) { |
| 395 | current_command_ = 0; |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 396 | Cleanup(); |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 397 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 398 | if (return_code != 0) { |
| 399 | LOG(ERROR) << "Postinst command failed with code: " << return_code; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 400 | ErrorCode error_code = ErrorCode::kPostinstallRunnerError; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 401 | |
Andrew de los Reyes | fe57d54 | 2011-06-07 09:00:36 -0700 | [diff] [blame] | 402 | if (return_code == 3) { |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 403 | // This special return code means that we tried to update firmware, |
| 404 | // but couldn't because we booted from FW B, and we need to reboot |
| 405 | // to get back to FW A. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 406 | error_code = ErrorCode::kPostinstallBootedFromFirmwareB; |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 407 | } |
Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 408 | |
| 409 | if (return_code == 4) { |
| 410 | // This special return code means that we tried to update firmware, |
| 411 | // but couldn't because we booted from FW B, and we need to reboot |
| 412 | // to get back to FW A. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 413 | error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable; |
Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 414 | } |
Alex Deymo | 5b91c6b | 2016-08-04 20:33:36 -0700 | [diff] [blame] | 415 | |
| 416 | // If postinstall script for this partition is optional we can ignore the |
| 417 | // result. |
| 418 | if (install_plan_.partitions[current_partition_].postinstall_optional) { |
| 419 | LOG(INFO) << "Ignoring postinstall failure since it is optional"; |
| 420 | } else { |
| 421 | return CompletePostinstall(error_code); |
| 422 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 423 | } |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 424 | accumulated_weight_ += partition_weight_[current_partition_]; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 425 | current_partition_++; |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 426 | ReportProgress(0); |
| 427 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 428 | PerformPartitionPostinstall(); |
| 429 | } |
| 430 | |
Kelvin Zhang | 19acf4f | 2024-01-08 21:18:28 +0000 | [diff] [blame] | 431 | PostinstallRunnerAction::~PostinstallRunnerAction() { |
| 432 | if (!install_plan_.partitions.empty()) { |
| 433 | auto dynamic_control = boot_control_->GetDynamicPartitionControl(); |
| 434 | CHECK(dynamic_control); |
| 435 | dynamic_control->UnmapAllPartitions(); |
| 436 | LOG(INFO) << "Unmapped all partitions."; |
| 437 | } |
| 438 | } |
| 439 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 440 | void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) { |
| 441 | // We only attempt to mark the new slot as active if all the postinstall |
| 442 | // steps succeeded. |
Kelvin Zhang | 19acf4f | 2024-01-08 21:18:28 +0000 | [diff] [blame] | 443 | DEFER { |
| 444 | if (error_code != ErrorCode::kSuccess && |
| 445 | error_code != ErrorCode::kUpdatedButNotActive) { |
| 446 | LOG(ERROR) << "Postinstall action failed."; |
| 447 | |
| 448 | // Undo any changes done to trigger Powerwash. |
| 449 | if (powerwash_scheduled_) |
| 450 | hardware_->CancelPowerwash(); |
| 451 | } |
| 452 | processor_->ActionComplete(this, error_code); |
| 453 | }; |
Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 454 | if (error_code == ErrorCode::kSuccess) { |
| 455 | if (install_plan_.switch_slot_on_reboot) { |
Kelvin Zhang | 19acf4f | 2024-01-08 21:18:28 +0000 | [diff] [blame] | 456 | if (!boot_control_->GetDynamicPartitionControl()->MapAllPartitions()) { |
| 457 | LOG(ERROR) << "Failed to map all partitions before marking snapshot as " |
| 458 | "ready for slot switch."; |
| 459 | return; |
| 460 | } |
| 461 | if (!boot_control_->GetDynamicPartitionControl()->FinishUpdate( |
| 462 | install_plan_.powerwash_required) || |
| 463 | !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) { |
Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 464 | error_code = ErrorCode::kPostinstallRunnerError; |
Tianjie Xu | d6aa91f | 2019-11-14 11:55:10 -0800 | [diff] [blame] | 465 | } else { |
| 466 | // Schedules warm reset on next reboot, ignores the error. |
| 467 | hardware_->SetWarmReset(true); |
Tianjie | 838793d | 2021-01-14 22:05:13 -0800 | [diff] [blame] | 468 | // Sets the vbmeta digest for the other slot to boot into. |
| 469 | hardware_->SetVbmetaDigestForInactiveSlot(false); |
Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 470 | } |
| 471 | } else { |
| 472 | error_code = ErrorCode::kUpdatedButNotActive; |
| 473 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 474 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 475 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 476 | LOG(INFO) << "All post-install commands succeeded"; |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 477 | if (HasOutputPipe()) { |
| 478 | SetOutputObject(install_plan_); |
| 479 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 482 | void PostinstallRunnerAction::SuspendAction() { |
| 483 | if (!current_command_) |
| 484 | return; |
| 485 | if (kill(current_command_, SIGSTOP) != 0) { |
| 486 | PLOG(ERROR) << "Couldn't pause child process " << current_command_; |
Ben Chan | 7f4bc3f | 2017-01-10 15:32:11 -0800 | [diff] [blame] | 487 | } else { |
| 488 | is_current_command_suspended_ = true; |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | void PostinstallRunnerAction::ResumeAction() { |
| 493 | if (!current_command_) |
| 494 | return; |
| 495 | if (kill(current_command_, SIGCONT) != 0) { |
| 496 | PLOG(ERROR) << "Couldn't resume child process " << current_command_; |
Ben Chan | 7f4bc3f | 2017-01-10 15:32:11 -0800 | [diff] [blame] | 497 | } else { |
| 498 | is_current_command_suspended_ = false; |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
| 502 | void PostinstallRunnerAction::TerminateProcessing() { |
| 503 | if (!current_command_) |
| 504 | return; |
| 505 | // Calling KillExec() will discard the callback we registered and therefore |
| 506 | // the unretained reference to this object. |
| 507 | Subprocess::Get().KillExec(current_command_); |
Ben Chan | 7f4bc3f | 2017-01-10 15:32:11 -0800 | [diff] [blame] | 508 | |
| 509 | // If the command has been suspended, resume it after KillExec() so that the |
| 510 | // process can process the SIGTERM sent by KillExec(). |
| 511 | if (is_current_command_suspended_) { |
| 512 | ResumeAction(); |
| 513 | } |
| 514 | |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 515 | current_command_ = 0; |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 516 | Cleanup(); |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 517 | } |
| 518 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 519 | } // namespace chromeos_update_engine |