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