blob: 5a6eeab38b78125ea1180b9a698892e8f0bd873f [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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.com3defe6a2009-12-04 20:57:17 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/postinstall_runner_action.h"
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070018
Alex Deymo0d298542016-03-30 18:31:49 -070019#include <fcntl.h>
Alex Deymod15c5462016-03-09 18:11:12 -080020#include <signal.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000021#include <stdlib.h>
Alex Vakulenko44cab302014-07-23 13:12:15 -070022#include <sys/mount.h>
Alex Deymod15c5462016-03-09 18:11:12 -080023#include <sys/types.h>
Alex Deymo0d298542016-03-30 18:31:49 -070024#include <unistd.h>
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070025
Alex Deymo44b35672016-04-05 17:57:48 -070026#include <cmath>
Kelvin Zhangddc25802021-12-30 13:05:27 -080027#include <fstream>
28#include <string>
Alex Deymo44b35672016-04-05 17:57:48 -070029
Alex Deymoe5e5fe92015-10-05 09:28:19 -070030#include <base/files/file_path.h>
31#include <base/files/file_util.h>
Alex Deymod15c5462016-03-09 18:11:12 -080032#include <base/logging.h>
Alex Deymo0d298542016-03-30 18:31:49 -070033#include <base/strings/string_split.h>
Alex Deymo461b2592015-07-24 20:10:52 -070034
Alex Deymo39910dc2015-11-09 17:04:30 -080035#include "update_engine/common/action_processor.h"
Alex Deymob15a0b82015-11-25 20:30:40 -030036#include "update_engine/common/boot_control_interface.h"
Kelvin Zhang99cbbe72024-01-18 14:50:01 -080037#include "update_engine/common/error_code_utils.h"
38#include "update_engine/common/platform_constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080039#include "update_engine/common/subprocess.h"
40#include "update_engine/common/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000041
Alex Deymo0d298542016-03-30 18:31:49 -070042namespace {
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.
48const int kPostinstallStatusFd = 3;
49
Kelvin Zhangddc25802021-12-30 13:05:27 -080050static constexpr bool Contains(std::string_view haystack,
51 std::string_view needle) {
52 return haystack.find(needle) != std::string::npos;
53}
54
55static 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 Deymo0d298542016-03-30 18:31:49 -070075} // namespace
76
adlr@google.com3defe6a2009-12-04 20:57:17 +000077namespace chromeos_update_engine {
78
79using std::string;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070080using std::vector;
adlr@google.com3defe6a2009-12-04 20:57:17 +000081
Kelvin Zhange9def4e2020-12-02 14:04:09 -050082PostinstallRunnerAction::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 Zhang2379fa92020-12-09 14:39:04 -050092 CHECK(!fs_mount_dir_.empty());
Kelvin Zhang1df000a2022-02-09 16:00:17 -080093 EnsureUnmounted();
Kelvin Zhang2379fa92020-12-09 14:39:04 -050094 LOG(INFO) << "postinstall mount point: " << fs_mount_dir_;
Kelvin Zhange9def4e2020-12-02 14:04:09 -050095}
96
Kelvin Zhang1df000a2022-02-09 16:00:17 -080097void 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.com3defe6a2009-12-04 20:57:17 +0000104void PostinstallRunnerAction::PerformAction() {
105 CHECK(HasInputObject());
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400106 CHECK(boot_control_);
Chris Sosad317e402013-06-12 13:47:09 -0700107 install_plan_ = GetInputObject();
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800108
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400109 auto dynamic_control = boot_control_->GetDynamicPartitionControl();
110 CHECK(dynamic_control);
111
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000112 // Mount snapshot partitions for Virtual AB Compression Compression.
Kelvin Zhang06188352021-02-10 13:21:47 -0500113 if (dynamic_control->UpdateUsesSnapshotCompression()) {
Kelvin Zhang291ac8c2024-09-23 14:05:13 -0700114 // If we are switching slots, then we are required to MapAllPartitions,
115 // as FinishUpdate() requires all partitions to be mapped.
116 // And switching slots requires FinishUpdate() to be called first
117 if (!install_plan_.partitions.empty() ||
118 install_plan_.switch_slot_on_reboot) {
Kelvin Zhang263a5402022-12-08 23:03:32 -0800119 if (!dynamic_control->MapAllPartitions()) {
120 return CompletePostinstall(ErrorCode::kPostInstallMountError);
121 }
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400122 }
123 }
124
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800125 // We always powerwash when rolling back, however policy can determine
126 // if this is a full/normal powerwash, or a special rollback powerwash
127 // that retains a small amount of system state such as enrollment and
128 // network configuration. In both cases all user accounts are deleted.
Daniel Zhengad1eaea2024-07-31 16:09:34 -0700129 if (install_plan_.powerwash_required) {
Kelvin Zhang399bd4d2024-12-03 11:08:30 -0800130 if (hardware_->SchedulePowerwash()) {
Alex Deymofb905d92016-06-03 19:26:58 -0700131 powerwash_scheduled_ = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700132 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700133 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700134 }
135 }
136
Alex Deymo0d298542016-03-30 18:31:49 -0700137 // Initialize all the partition weights.
138 partition_weight_.resize(install_plan_.partitions.size());
139 total_weight_ = 0;
140 for (size_t i = 0; i < install_plan_.partitions.size(); ++i) {
Tianjie Xu087de9d2019-11-01 17:11:22 -0700141 auto& partition = install_plan_.partitions[i];
142 if (!install_plan_.run_post_install && partition.postinstall_optional) {
143 partition.run_postinstall = false;
144 LOG(INFO) << "Skipping optional post-install for partition "
145 << partition.name << " according to install plan.";
146 }
147
Alex Deymo0d298542016-03-30 18:31:49 -0700148 // TODO(deymo): This code sets the weight to all the postinstall commands,
149 // but we could remember how long they took in the past and use those
150 // values.
Tianjie Xu087de9d2019-11-01 17:11:22 -0700151 partition_weight_[i] = partition.run_postinstall;
Alex Deymo0d298542016-03-30 18:31:49 -0700152 total_weight_ += partition_weight_[i];
153 }
154 accumulated_weight_ = 0;
155 ReportProgress(0);
156
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700157 PerformPartitionPostinstall();
158}
159
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700160bool PostinstallRunnerAction::MountPartition(
161 const InstallPlan::Partition& partition) noexcept {
162 // Perform post-install for the current_partition_ partition. At this point we
163 // need to call CompletePartitionPostinstall to complete the operation and
164 // cleanup.
165 const auto mountable_device = partition.readonly_target_path;
166 if (!utils::FileExists(mountable_device.c_str())) {
167 LOG(ERROR) << "Mountable device " << mountable_device << " for partition "
168 << partition.name << " does not exist";
169 return false;
170 }
171
172 if (!utils::FileExists(fs_mount_dir_.c_str())) {
173 LOG(ERROR) << "Mount point " << fs_mount_dir_
174 << " does not exist, mount call will fail";
175 return false;
176 }
177 // Double check that the fs_mount_dir is not busy with a previous mounted
178 // filesystem from a previous crashed postinstall step.
Kelvin Zhang1df000a2022-02-09 16:00:17 -0800179 EnsureUnmounted();
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700180
181#ifdef __ANDROID__
182 // In Chromium OS, the postinstall step is allowed to write to the block
183 // device on the target image, so we don't mark it as read-only and should
184 // be read-write since we just wrote to it during the update.
185
186 // Mark the block device as read-only before mounting for post-install.
187 if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) {
188 return false;
189 }
190#endif // __ANDROID__
191
192 if (!utils::MountFilesystem(
193 mountable_device,
194 fs_mount_dir_,
195 MS_RDONLY,
196 partition.filesystem_type,
197 hardware_->GetPartitionMountOptions(partition.name))) {
198 return false;
199 }
200 return true;
201}
202
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700203void PostinstallRunnerAction::PerformPartitionPostinstall() {
Alex Deymo390efed2016-02-18 11:00:40 -0800204 if (install_plan_.download_url.empty()) {
Kelvin Zhang24287c32023-03-09 10:13:26 -0800205 LOG(INFO) << "Skipping post-install";
Alex Deymo390efed2016-02-18 11:00:40 -0800206 return CompletePostinstall(ErrorCode::kSuccess);
207 }
208
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700209 // Skip all the partitions that don't have a post-install step.
210 while (current_partition_ < install_plan_.partitions.size() &&
211 !install_plan_.partitions[current_partition_].run_postinstall) {
212 VLOG(1) << "Skipping post-install on partition "
213 << install_plan_.partitions[current_partition_].name;
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700214 // Attempt to mount a device if it has postinstall script configured, even
215 // if we want to skip running postinstall script.
216 // This is because we've seen bugs like b/198787355 which is only triggered
217 // when you attempt to mount a device. If device fails to mount, it will
218 // likely fail to mount during boot anyway, so it's better to catch any
219 // issues earlier.
220 // It's possible that some of the partitions aren't mountable, but these
221 // partitions shouldn't have postinstall configured. Therefore we guard this
222 // logic with |postinstall_path.empty()|.
223 const auto& partition = install_plan_.partitions[current_partition_];
224 if (!partition.postinstall_path.empty()) {
225 const auto mountable_device = partition.readonly_target_path;
226 if (!MountPartition(partition)) {
227 return CompletePostinstall(ErrorCode::kPostInstallMountError);
228 }
Kelvin Zhangddc25802021-12-30 13:05:27 -0800229 LogBuildInfoForPartition(fs_mount_dir_);
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700230 if (!utils::UnmountFilesystem(fs_mount_dir_)) {
231 return CompletePartitionPostinstall(
232 1, "Error unmounting the device " + mountable_device);
233 }
234 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700235 current_partition_++;
236 }
237 if (current_partition_ == install_plan_.partitions.size())
238 return CompletePostinstall(ErrorCode::kSuccess);
239
240 const InstallPlan::Partition& partition =
241 install_plan_.partitions[current_partition_];
242
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400243 const string mountable_device = partition.readonly_target_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700244 // Perform post-install for the current_partition_ partition. At this point we
245 // need to call CompletePartitionPostinstall to complete the operation and
246 // cleanup.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700247
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700248 if (!MountPartition(partition)) {
249 CompletePostinstall(ErrorCode::kPostInstallMountError);
250 return;
Kelvin Zhang4ce01102020-11-16 09:32:08 -0500251 }
Kelvin Zhangddc25802021-12-30 13:05:27 -0800252 LogBuildInfoForPartition(fs_mount_dir_);
Alex Deymocbc22742016-03-04 17:53:02 -0800253 base::FilePath postinstall_path(partition.postinstall_path);
254 if (postinstall_path.IsAbsolute()) {
255 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
256 "path instead: "
257 << partition.postinstall_path;
258 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
259 }
260
261 string abs_path =
262 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800263 if (!base::StartsWith(
264 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
265 LOG(ERROR) << "Invalid relative postinstall path: "
266 << partition.postinstall_path;
267 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
268 }
269
Alex Deymo390efed2016-02-18 11:00:40 -0800270 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400271 << abs_path << ") installed on mountable device "
272 << mountable_device;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700273
Alex Deymo032e7722014-03-25 17:53:56 -0700274 // Logs the file format of the postinstall script we are about to run. This
275 // will help debug when the postinstall script doesn't match the architecture
276 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800277 LOG(INFO) << "Format file for new " << partition.postinstall_path
278 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700279
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800280 // Runs the postinstall script asynchronously to free up the main loop while
281 // it's running.
Alex Deymo0d298542016-03-30 18:31:49 -0700282 vector<string> command = {abs_path};
283#ifdef __ANDROID__
284 // In Brillo and Android, we pass the slot number and status fd.
285 command.push_back(std::to_string(install_plan_.target_slot));
286 command.push_back(std::to_string(kPostinstallStatusFd));
287#else
288 // Chrome OS postinstall expects the target rootfs as the first parameter.
289 command.push_back(partition.target_path);
290#endif // __ANDROID__
291
292 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymod15c5462016-03-09 18:11:12 -0800293 command,
Alex Deymo0d298542016-03-30 18:31:49 -0700294 Subprocess::kRedirectStderrToStdout,
295 {kPostinstallStatusFd},
Alex Deymod15c5462016-03-09 18:11:12 -0800296 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
297 base::Unretained(this)));
298 // Subprocess::Exec should never return a negative process id.
299 CHECK_GE(current_command_, 0);
300
Alex Deymo0d298542016-03-30 18:31:49 -0700301 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700302 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo0d298542016-03-30 18:31:49 -0700303 return;
304 }
305
306 // Monitor the status file descriptor.
307 progress_fd_ =
308 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
309 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
310 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
311 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
312 }
313
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900314 progress_controller_ = base::FileDescriptorWatcher::WatchReadable(
Alex Deymo0d298542016-03-30 18:31:49 -0700315 progress_fd_,
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900316 base::BindRepeating(&PostinstallRunnerAction::OnProgressFdReady,
317 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800318}
319
Alex Deymo0d298542016-03-30 18:31:49 -0700320void PostinstallRunnerAction::OnProgressFdReady() {
321 char buf[1024];
322 size_t bytes_read;
323 do {
324 bytes_read = 0;
325 bool eof;
326 bool ok =
Kokoa Matsuda91aa2172024-10-16 16:01:04 +0900327 utils::ReadAll(progress_fd_, buf, std::size(buf), &bytes_read, &eof);
Alex Deymo0d298542016-03-30 18:31:49 -0700328 progress_buffer_.append(buf, bytes_read);
329 // Process every line.
330 vector<string> lines = base::SplitString(
331 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
332 if (!lines.empty()) {
333 progress_buffer_ = lines.back();
334 lines.pop_back();
335 for (const auto& line : lines) {
336 ProcessProgressLine(line);
337 }
338 }
339 if (!ok || eof) {
340 // There was either an error or an EOF condition, so we are done watching
341 // the file descriptor.
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900342 progress_controller_.reset();
Alex Deymo0d298542016-03-30 18:31:49 -0700343 return;
344 }
345 } while (bytes_read);
346}
347
348bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
349 double frac = 0;
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700350 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 &&
351 !std::isnan(frac)) {
Alex Deymo0d298542016-03-30 18:31:49 -0700352 ReportProgress(frac);
353 return true;
354 }
355
356 return false;
357}
358
359void PostinstallRunnerAction::ReportProgress(double frac) {
360 if (!delegate_)
361 return;
Yoshitaka Ishida128936f2018-02-16 18:20:07 +0900362 if (current_partition_ >= partition_weight_.size() || total_weight_ == 0) {
Alex Deymo0d298542016-03-30 18:31:49 -0700363 delegate_->ProgressUpdate(1.);
364 return;
365 }
Alex Deymo44b35672016-04-05 17:57:48 -0700366 if (!std::isfinite(frac) || frac < 0)
Alex Deymo0d298542016-03-30 18:31:49 -0700367 frac = 0;
368 if (frac > 1)
369 frac = 1;
370 double postinst_action_progress =
371 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
372 total_weight_;
373 delegate_->ProgressUpdate(postinst_action_progress);
374}
375
376void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800377 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800378#ifndef __ANDROID__
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500379#if BASE_VER < 800000
380 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), true)) {
381#else
hscham043355b2020-11-17 16:50:10 +0900382 if (!base::DeleteFile(base::FilePath(fs_mount_dir_))) {
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500383#endif
Alex Deymo390efed2016-02-18 11:00:40 -0800384 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700385 }
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500386#endif
Alex Deymo0d298542016-03-30 18:31:49 -0700387
388 progress_fd_ = -1;
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900389 progress_controller_.reset();
Tianjie55abd3c2020-06-19 00:22:59 -0700390
Alex Deymo0d298542016-03-30 18:31:49 -0700391 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800392}
393
394void PostinstallRunnerAction::CompletePartitionPostinstall(
395 int return_code, const string& output) {
396 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700397 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700398
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800399 if (return_code != 0) {
400 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700401 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700402
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700403 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700404 // This special return code means that we tried to update firmware,
405 // but couldn't because we booted from FW B, and we need to reboot
406 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700407 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700408 }
Don Garrett81018e02013-07-30 18:46:31 -0700409
410 if (return_code == 4) {
411 // This special return code means that we tried to update firmware,
412 // but couldn't because we booted from FW B, and we need to reboot
413 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700414 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700415 }
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700416
417 // If postinstall script for this partition is optional we can ignore the
418 // result.
419 if (install_plan_.partitions[current_partition_].postinstall_optional) {
420 LOG(INFO) << "Ignoring postinstall failure since it is optional";
421 } else {
422 return CompletePostinstall(error_code);
423 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700424 }
Alex Deymo0d298542016-03-30 18:31:49 -0700425 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700426 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700427 ReportProgress(0);
428
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700429 PerformPartitionPostinstall();
430}
431
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000432PostinstallRunnerAction::~PostinstallRunnerAction() {
433 if (!install_plan_.partitions.empty()) {
434 auto dynamic_control = boot_control_->GetDynamicPartitionControl();
435 CHECK(dynamic_control);
436 dynamic_control->UnmapAllPartitions();
437 LOG(INFO) << "Unmapped all partitions.";
438 }
439}
440
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700441void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
442 // We only attempt to mark the new slot as active if all the postinstall
443 // steps succeeded.
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000444 DEFER {
445 if (error_code != ErrorCode::kSuccess &&
446 error_code != ErrorCode::kUpdatedButNotActive) {
Kelvin Zhang99cbbe72024-01-18 14:50:01 -0800447 LOG(ERROR) << "Postinstall action failed. "
448 << utils::ErrorCodeToString(error_code);
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000449
450 // Undo any changes done to trigger Powerwash.
451 if (powerwash_scheduled_)
452 hardware_->CancelPowerwash();
453 }
454 processor_->ActionComplete(this, error_code);
455 };
Sen Jiang02c49422017-10-31 15:14:11 -0700456 if (error_code == ErrorCode::kSuccess) {
457 if (install_plan_.switch_slot_on_reboot) {
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000458 if (!boot_control_->GetDynamicPartitionControl()->FinishUpdate(
459 install_plan_.powerwash_required) ||
460 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Sen Jiang02c49422017-10-31 15:14:11 -0700461 error_code = ErrorCode::kPostinstallRunnerError;
Tianjie Xud6aa91f2019-11-14 11:55:10 -0800462 } else {
463 // Schedules warm reset on next reboot, ignores the error.
464 hardware_->SetWarmReset(true);
Tianjie838793d2021-01-14 22:05:13 -0800465 // Sets the vbmeta digest for the other slot to boot into.
466 hardware_->SetVbmetaDigestForInactiveSlot(false);
Sen Jiang02c49422017-10-31 15:14:11 -0700467 }
468 } else {
469 error_code = ErrorCode::kUpdatedButNotActive;
470 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700471 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700472
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700473 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700474 if (HasOutputPipe()) {
475 SetOutputObject(install_plan_);
476 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000477}
478
Alex Deymod15c5462016-03-09 18:11:12 -0800479void PostinstallRunnerAction::SuspendAction() {
480 if (!current_command_)
481 return;
482 if (kill(current_command_, SIGSTOP) != 0) {
483 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800484 } else {
485 is_current_command_suspended_ = true;
Alex Deymod15c5462016-03-09 18:11:12 -0800486 }
487}
488
489void PostinstallRunnerAction::ResumeAction() {
490 if (!current_command_)
491 return;
492 if (kill(current_command_, SIGCONT) != 0) {
493 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800494 } else {
495 is_current_command_suspended_ = false;
Alex Deymod15c5462016-03-09 18:11:12 -0800496 }
497}
498
499void PostinstallRunnerAction::TerminateProcessing() {
500 if (!current_command_)
501 return;
502 // Calling KillExec() will discard the callback we registered and therefore
503 // the unretained reference to this object.
504 Subprocess::Get().KillExec(current_command_);
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800505
506 // If the command has been suspended, resume it after KillExec() so that the
507 // process can process the SIGTERM sent by KillExec().
508 if (is_current_command_suspended_) {
509 ResumeAction();
510 }
511
Alex Deymod15c5462016-03-09 18:11:12 -0800512 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700513 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800514}
515
adlr@google.com3defe6a2009-12-04 20:57:17 +0000516} // namespace chromeos_update_engine