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