| 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. | 
|  | 79 | if (dynamic_control->GetVirtualAbCompressionFeatureFlag().IsEnabled()) { | 
|  | 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 |  | 
|  | 124 | void PostinstallRunnerAction::PerformPartitionPostinstall() { | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 125 | if (install_plan_.download_url.empty()) { | 
|  | 126 | LOG(INFO) << "Skipping post-install during rollback"; | 
|  | 127 | return CompletePostinstall(ErrorCode::kSuccess); | 
|  | 128 | } | 
|  | 129 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 130 | // Skip all the partitions that don't have a post-install step. | 
|  | 131 | while (current_partition_ < install_plan_.partitions.size() && | 
|  | 132 | !install_plan_.partitions[current_partition_].run_postinstall) { | 
|  | 133 | VLOG(1) << "Skipping post-install on partition " | 
|  | 134 | << install_plan_.partitions[current_partition_].name; | 
|  | 135 | current_partition_++; | 
|  | 136 | } | 
|  | 137 | if (current_partition_ == install_plan_.partitions.size()) | 
|  | 138 | return CompletePostinstall(ErrorCode::kSuccess); | 
|  | 139 |  | 
|  | 140 | const InstallPlan::Partition& partition = | 
|  | 141 | install_plan_.partitions[current_partition_]; | 
|  | 142 |  | 
| Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 143 | const string mountable_device = partition.postinstall_mount_device; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 144 | if (mountable_device.empty()) { | 
|  | 145 | LOG(ERROR) << "Cannot make mountable device from " << partition.target_path; | 
|  | 146 | return CompletePostinstall(ErrorCode::kPostinstallRunnerError); | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | // Perform post-install for the current_partition_ partition. At this point we | 
|  | 150 | // need to call CompletePartitionPostinstall to complete the operation and | 
|  | 151 | // cleanup. | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 152 |  | 
| Kelvin Zhang | 4ce0110 | 2020-11-16 09:32:08 -0500 | [diff] [blame] | 153 | if (!utils::FileExists(fs_mount_dir_.c_str())) { | 
|  | 154 | LOG(ERROR) << "Mount point " << fs_mount_dir_ | 
|  | 155 | << " does not exist, mount call will fail"; | 
|  | 156 | return CompletePostinstall(ErrorCode::kPostinstallRunnerError); | 
|  | 157 | } | 
| Alex Deymo | f411650 | 2017-03-22 17:00:31 -0700 | [diff] [blame] | 158 | // Double check that the fs_mount_dir is not busy with a previous mounted | 
|  | 159 | // filesystem from a previous crashed postinstall step. | 
|  | 160 | if (utils::IsMountpoint(fs_mount_dir_)) { | 
|  | 161 | LOG(INFO) << "Found previously mounted filesystem at " << fs_mount_dir_; | 
|  | 162 | utils::UnmountFilesystem(fs_mount_dir_); | 
|  | 163 | } | 
|  | 164 |  | 
| Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 165 | base::FilePath postinstall_path(partition.postinstall_path); | 
|  | 166 | if (postinstall_path.IsAbsolute()) { | 
|  | 167 | LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative" | 
|  | 168 | "path instead: " | 
|  | 169 | << partition.postinstall_path; | 
|  | 170 | return CompletePostinstall(ErrorCode::kPostinstallRunnerError); | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | string abs_path = | 
|  | 174 | base::FilePath(fs_mount_dir_).Append(postinstall_path).value(); | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 175 | if (!base::StartsWith( | 
|  | 176 | abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) { | 
|  | 177 | LOG(ERROR) << "Invalid relative postinstall path: " | 
|  | 178 | << partition.postinstall_path; | 
|  | 179 | return CompletePostinstall(ErrorCode::kPostinstallRunnerError); | 
|  | 180 | } | 
|  | 181 |  | 
| Alex Deymo | 5fb356c | 2016-03-25 18:48:22 -0700 | [diff] [blame] | 182 | #ifdef __ANDROID__ | 
|  | 183 | // In Chromium OS, the postinstall step is allowed to write to the block | 
|  | 184 | // device on the target image, so we don't mark it as read-only and should | 
|  | 185 | // be read-write since we just wrote to it during the update. | 
|  | 186 |  | 
|  | 187 | // Mark the block device as read-only before mounting for post-install. | 
|  | 188 | if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) { | 
|  | 189 | return CompletePartitionPostinstall( | 
|  | 190 | 1, "Error marking the device " + mountable_device + " read only."); | 
|  | 191 | } | 
|  | 192 | #endif  // __ANDROID__ | 
|  | 193 |  | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 194 | if (!utils::MountFilesystem(mountable_device, | 
|  | 195 | fs_mount_dir_, | 
|  | 196 | MS_RDONLY, | 
| Alex Deymo | 14dbd33 | 2016-03-01 18:55:54 -0800 | [diff] [blame] | 197 | partition.filesystem_type, | 
|  | 198 | constants::kPostinstallMountOptions)) { | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 199 | return CompletePartitionPostinstall( | 
|  | 200 | 1, "Error mounting the device " + mountable_device); | 
|  | 201 | } | 
|  | 202 |  | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 203 | LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at " | 
|  | 204 | << abs_path << ") installed on device " << partition.target_path | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 205 | << " and mountable device " << mountable_device; | 
|  | 206 |  | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 207 | // Logs the file format of the postinstall script we are about to run. This | 
|  | 208 | // will help debug when the postinstall script doesn't match the architecture | 
|  | 209 | // of our build. | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 210 | LOG(INFO) << "Format file for new " << partition.postinstall_path | 
|  | 211 | << " is: " << utils::GetFileFormat(abs_path); | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 212 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 213 | // Runs the postinstall script asynchronously to free up the main loop while | 
|  | 214 | // it's running. | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 215 | vector<string> command = {abs_path}; | 
|  | 216 | #ifdef __ANDROID__ | 
|  | 217 | // In Brillo and Android, we pass the slot number and status fd. | 
|  | 218 | command.push_back(std::to_string(install_plan_.target_slot)); | 
|  | 219 | command.push_back(std::to_string(kPostinstallStatusFd)); | 
|  | 220 | #else | 
|  | 221 | // Chrome OS postinstall expects the target rootfs as the first parameter. | 
|  | 222 | command.push_back(partition.target_path); | 
|  | 223 | #endif  // __ANDROID__ | 
|  | 224 |  | 
|  | 225 | current_command_ = Subprocess::Get().ExecFlags( | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 226 | command, | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 227 | Subprocess::kRedirectStderrToStdout, | 
|  | 228 | {kPostinstallStatusFd}, | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 229 | base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall, | 
|  | 230 | base::Unretained(this))); | 
|  | 231 | // Subprocess::Exec should never return a negative process id. | 
|  | 232 | CHECK_GE(current_command_, 0); | 
|  | 233 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 234 | if (!current_command_) { | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 235 | CompletePartitionPostinstall(1, "Postinstall didn't launch"); | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 236 | return; | 
|  | 237 | } | 
|  | 238 |  | 
|  | 239 | // Monitor the status file descriptor. | 
|  | 240 | progress_fd_ = | 
|  | 241 | Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd); | 
|  | 242 | int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK; | 
|  | 243 | if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) { | 
|  | 244 | PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_; | 
|  | 245 | } | 
|  | 246 |  | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 247 | progress_controller_ = base::FileDescriptorWatcher::WatchReadable( | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 248 | progress_fd_, | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 249 | base::BindRepeating(&PostinstallRunnerAction::OnProgressFdReady, | 
|  | 250 | base::Unretained(this))); | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 251 | } | 
|  | 252 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 253 | void PostinstallRunnerAction::OnProgressFdReady() { | 
|  | 254 | char buf[1024]; | 
|  | 255 | size_t bytes_read; | 
|  | 256 | do { | 
|  | 257 | bytes_read = 0; | 
|  | 258 | bool eof; | 
|  | 259 | bool ok = | 
| hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame] | 260 | utils::ReadAll(progress_fd_, buf, base::size(buf), &bytes_read, &eof); | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 261 | progress_buffer_.append(buf, bytes_read); | 
|  | 262 | // Process every line. | 
|  | 263 | vector<string> lines = base::SplitString( | 
|  | 264 | progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); | 
|  | 265 | if (!lines.empty()) { | 
|  | 266 | progress_buffer_ = lines.back(); | 
|  | 267 | lines.pop_back(); | 
|  | 268 | for (const auto& line : lines) { | 
|  | 269 | ProcessProgressLine(line); | 
|  | 270 | } | 
|  | 271 | } | 
|  | 272 | if (!ok || eof) { | 
|  | 273 | // There was either an error or an EOF condition, so we are done watching | 
|  | 274 | // the file descriptor. | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 275 | progress_controller_.reset(); | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 276 | return; | 
|  | 277 | } | 
|  | 278 | } while (bytes_read); | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | bool PostinstallRunnerAction::ProcessProgressLine(const string& line) { | 
|  | 282 | double frac = 0; | 
| Alex Deymo | a2ea1c2 | 2016-08-24 17:26:19 -0700 | [diff] [blame] | 283 | if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 && | 
|  | 284 | !std::isnan(frac)) { | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 285 | ReportProgress(frac); | 
|  | 286 | return true; | 
|  | 287 | } | 
|  | 288 |  | 
|  | 289 | return false; | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | void PostinstallRunnerAction::ReportProgress(double frac) { | 
|  | 293 | if (!delegate_) | 
|  | 294 | return; | 
| Yoshitaka Ishida | 128936f | 2018-02-16 18:20:07 +0900 | [diff] [blame] | 295 | if (current_partition_ >= partition_weight_.size() || total_weight_ == 0) { | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 296 | delegate_->ProgressUpdate(1.); | 
|  | 297 | return; | 
|  | 298 | } | 
| Alex Deymo | 44b3567 | 2016-04-05 17:57:48 -0700 | [diff] [blame] | 299 | if (!std::isfinite(frac) || frac < 0) | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 300 | frac = 0; | 
|  | 301 | if (frac > 1) | 
|  | 302 | frac = 1; | 
|  | 303 | double postinst_action_progress = | 
|  | 304 | (accumulated_weight_ + partition_weight_[current_partition_] * frac) / | 
|  | 305 | total_weight_; | 
|  | 306 | delegate_->ProgressUpdate(postinst_action_progress); | 
|  | 307 | } | 
|  | 308 |  | 
|  | 309 | void PostinstallRunnerAction::Cleanup() { | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 310 | utils::UnmountFilesystem(fs_mount_dir_); | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 311 | #ifndef __ANDROID__ | 
| Kelvin Zhang | 4aeaa12 | 2020-12-04 13:28:47 -0500 | [diff] [blame] | 312 | #if BASE_VER < 800000 | 
|  | 313 | if (!base::DeleteFile(base::FilePath(fs_mount_dir_), true)) { | 
|  | 314 | #else | 
| hscham | 043355b | 2020-11-17 16:50:10 +0900 | [diff] [blame] | 315 | if (!base::DeleteFile(base::FilePath(fs_mount_dir_))) { | 
| Kelvin Zhang | 4aeaa12 | 2020-12-04 13:28:47 -0500 | [diff] [blame] | 316 | #endif | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 317 | PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 318 | } | 
| Kelvin Zhang | 4aeaa12 | 2020-12-04 13:28:47 -0500 | [diff] [blame] | 319 | #endif | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 320 |  | 
|  | 321 | progress_fd_ = -1; | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 322 | progress_controller_.reset(); | 
| Tianjie | 55abd3c | 2020-06-19 00:22:59 -0700 | [diff] [blame] | 323 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 324 | progress_buffer_.clear(); | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 325 | } | 
|  | 326 |  | 
|  | 327 | void PostinstallRunnerAction::CompletePartitionPostinstall( | 
|  | 328 | int return_code, const string& output) { | 
|  | 329 | current_command_ = 0; | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 330 | Cleanup(); | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 331 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 332 | if (return_code != 0) { | 
|  | 333 | LOG(ERROR) << "Postinst command failed with code: " << return_code; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 334 | ErrorCode error_code = ErrorCode::kPostinstallRunnerError; | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 335 |  | 
| Andrew de los Reyes | fe57d54 | 2011-06-07 09:00:36 -0700 | [diff] [blame] | 336 | if (return_code == 3) { | 
| Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 337 | // This special return code means that we tried to update firmware, | 
|  | 338 | // but couldn't because we booted from FW B, and we need to reboot | 
|  | 339 | // to get back to FW A. | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 340 | error_code = ErrorCode::kPostinstallBootedFromFirmwareB; | 
| Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 341 | } | 
| Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 342 |  | 
|  | 343 | if (return_code == 4) { | 
|  | 344 | // This special return code means that we tried to update firmware, | 
|  | 345 | // but couldn't because we booted from FW B, and we need to reboot | 
|  | 346 | // to get back to FW A. | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 347 | error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable; | 
| Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 348 | } | 
| Alex Deymo | 5b91c6b | 2016-08-04 20:33:36 -0700 | [diff] [blame] | 349 |  | 
|  | 350 | // If postinstall script for this partition is optional we can ignore the | 
|  | 351 | // result. | 
|  | 352 | if (install_plan_.partitions[current_partition_].postinstall_optional) { | 
|  | 353 | LOG(INFO) << "Ignoring postinstall failure since it is optional"; | 
|  | 354 | } else { | 
|  | 355 | return CompletePostinstall(error_code); | 
|  | 356 | } | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 357 | } | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 358 | accumulated_weight_ += partition_weight_[current_partition_]; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 359 | current_partition_++; | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 360 | ReportProgress(0); | 
|  | 361 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 362 | PerformPartitionPostinstall(); | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) { | 
|  | 366 | // We only attempt to mark the new slot as active if all the postinstall | 
|  | 367 | // steps succeeded. | 
| Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 368 | if (error_code == ErrorCode::kSuccess) { | 
|  | 369 | if (install_plan_.switch_slot_on_reboot) { | 
| Yifan Hong | 7b3910a | 2020-03-24 17:47:32 -0700 | [diff] [blame] | 370 | if (!boot_control_->GetDynamicPartitionControl()->FinishUpdate( | 
|  | 371 | install_plan_.powerwash_required) || | 
| Yifan Hong | e858362 | 2019-11-07 11:36:31 -0800 | [diff] [blame] | 372 | !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) { | 
| Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 373 | error_code = ErrorCode::kPostinstallRunnerError; | 
| Tianjie Xu | d6aa91f | 2019-11-14 11:55:10 -0800 | [diff] [blame] | 374 | } else { | 
|  | 375 | // Schedules warm reset on next reboot, ignores the error. | 
|  | 376 | hardware_->SetWarmReset(true); | 
| Tianjie | 838793d | 2021-01-14 22:05:13 -0800 | [diff] [blame] | 377 | // Sets the vbmeta digest for the other slot to boot into. | 
|  | 378 | hardware_->SetVbmetaDigestForInactiveSlot(false); | 
| Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 379 | } | 
|  | 380 | } else { | 
|  | 381 | error_code = ErrorCode::kUpdatedButNotActive; | 
|  | 382 | } | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 383 | } | 
|  | 384 |  | 
|  | 385 | ScopedActionCompleter completer(processor_, this); | 
| Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 386 | completer.set_code(error_code); | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 387 |  | 
| Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 388 | if (error_code != ErrorCode::kSuccess && | 
|  | 389 | error_code != ErrorCode::kUpdatedButNotActive) { | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 390 | LOG(ERROR) << "Postinstall action failed."; | 
|  | 391 |  | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 392 | // Undo any changes done to trigger Powerwash. | 
|  | 393 | if (powerwash_scheduled_) | 
|  | 394 | hardware_->CancelPowerwash(); | 
| Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 395 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 396 | return; | 
|  | 397 | } | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 398 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 399 | LOG(INFO) << "All post-install commands succeeded"; | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 400 | if (HasOutputPipe()) { | 
|  | 401 | SetOutputObject(install_plan_); | 
|  | 402 | } | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 403 | } | 
|  | 404 |  | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 405 | void PostinstallRunnerAction::SuspendAction() { | 
|  | 406 | if (!current_command_) | 
|  | 407 | return; | 
|  | 408 | if (kill(current_command_, SIGSTOP) != 0) { | 
|  | 409 | PLOG(ERROR) << "Couldn't pause child process " << current_command_; | 
| Ben Chan | 7f4bc3f | 2017-01-10 15:32:11 -0800 | [diff] [blame] | 410 | } else { | 
|  | 411 | is_current_command_suspended_ = true; | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 412 | } | 
|  | 413 | } | 
|  | 414 |  | 
|  | 415 | void PostinstallRunnerAction::ResumeAction() { | 
|  | 416 | if (!current_command_) | 
|  | 417 | return; | 
|  | 418 | if (kill(current_command_, SIGCONT) != 0) { | 
|  | 419 | PLOG(ERROR) << "Couldn't resume child process " << current_command_; | 
| Ben Chan | 7f4bc3f | 2017-01-10 15:32:11 -0800 | [diff] [blame] | 420 | } else { | 
|  | 421 | is_current_command_suspended_ = false; | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 422 | } | 
|  | 423 | } | 
|  | 424 |  | 
|  | 425 | void PostinstallRunnerAction::TerminateProcessing() { | 
|  | 426 | if (!current_command_) | 
|  | 427 | return; | 
|  | 428 | // Calling KillExec() will discard the callback we registered and therefore | 
|  | 429 | // the unretained reference to this object. | 
|  | 430 | Subprocess::Get().KillExec(current_command_); | 
| Ben Chan | 7f4bc3f | 2017-01-10 15:32:11 -0800 | [diff] [blame] | 431 |  | 
|  | 432 | // If the command has been suspended, resume it after KillExec() so that the | 
|  | 433 | // process can process the SIGTERM sent by KillExec(). | 
|  | 434 | if (is_current_command_suspended_) { | 
|  | 435 | ResumeAction(); | 
|  | 436 | } | 
|  | 437 |  | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 438 | current_command_ = 0; | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 439 | Cleanup(); | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 440 | } | 
|  | 441 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 442 | }  // namespace chromeos_update_engine |