blob: 4de75aa7c871743abf7ae9843abb0888edaaee49 [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>
hscham00b6aa22020-02-20 12:32:06 +090033#include <base/stl_util.h>
Alex Deymo0d298542016-03-30 18:31:49 -070034#include <base/strings/string_split.h>
Alex Deymo390efed2016-02-18 11:00:40 -080035#include <base/strings/string_util.h>
Alex Deymo461b2592015-07-24 20:10:52 -070036
Alex Deymo39910dc2015-11-09 17:04:30 -080037#include "update_engine/common/action_processor.h"
Alex Deymob15a0b82015-11-25 20:30:40 -030038#include "update_engine/common/boot_control_interface.h"
Kelvin Zhang99cbbe72024-01-18 14:50:01 -080039#include "update_engine/common/error_code_utils.h"
40#include "update_engine/common/platform_constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080041#include "update_engine/common/subprocess.h"
42#include "update_engine/common/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000043
Alex Deymo0d298542016-03-30 18:31:49 -070044namespace {
45
46// The file descriptor number from the postinstall program's perspective where
47// it can report status updates. This can be any number greater than 2 (stderr),
48// but must be kept in sync with the "bin/postinst_progress" defined in the
49// sample_images.sh file.
50const int kPostinstallStatusFd = 3;
51
Kelvin Zhangddc25802021-12-30 13:05:27 -080052static constexpr bool Contains(std::string_view haystack,
53 std::string_view needle) {
54 return haystack.find(needle) != std::string::npos;
55}
56
57static void LogBuildInfoForPartition(std::string_view mount_point) {
58 static constexpr std::array<std::string_view, 3> kBuildPropFiles{
59 "build.prop", "etc/build.prop", "system/build.prop"};
60 for (const auto& file : kBuildPropFiles) {
61 auto path = std::string(mount_point);
62 if (path.back() != '/') {
63 path.push_back('/');
64 }
65 path += file;
66 LOG(INFO) << "Trying to read " << path;
67 std::ifstream infile(path);
68 std::string line;
69 while (std::getline(infile, line)) {
70 if (Contains(line, "ro.build")) {
71 LOG(INFO) << line;
72 }
73 }
74 }
75}
76
Alex Deymo0d298542016-03-30 18:31:49 -070077} // namespace
78
adlr@google.com3defe6a2009-12-04 20:57:17 +000079namespace chromeos_update_engine {
80
81using std::string;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070082using std::vector;
adlr@google.com3defe6a2009-12-04 20:57:17 +000083
Kelvin Zhange9def4e2020-12-02 14:04:09 -050084PostinstallRunnerAction::PostinstallRunnerAction(
85 BootControlInterface* boot_control, HardwareInterface* hardware)
86 : boot_control_(boot_control), hardware_(hardware) {
87#ifdef __ANDROID__
88 fs_mount_dir_ = "/postinstall";
89#else // __ANDROID__
90 base::FilePath temp_dir;
91 TEST_AND_RETURN(base::CreateNewTempDirectory("au_postint_mount", &temp_dir));
92 fs_mount_dir_ = temp_dir.value();
93#endif // __ANDROID__
Kelvin Zhang2379fa92020-12-09 14:39:04 -050094 CHECK(!fs_mount_dir_.empty());
Kelvin Zhang1df000a2022-02-09 16:00:17 -080095 EnsureUnmounted();
Kelvin Zhang2379fa92020-12-09 14:39:04 -050096 LOG(INFO) << "postinstall mount point: " << fs_mount_dir_;
Kelvin Zhange9def4e2020-12-02 14:04:09 -050097}
98
Kelvin Zhang1df000a2022-02-09 16:00:17 -080099void PostinstallRunnerAction::EnsureUnmounted() {
100 if (utils::IsMountpoint(fs_mount_dir_)) {
101 LOG(INFO) << "Found previously mounted filesystem at " << fs_mount_dir_;
102 utils::UnmountFilesystem(fs_mount_dir_);
103 }
104}
105
adlr@google.com3defe6a2009-12-04 20:57:17 +0000106void PostinstallRunnerAction::PerformAction() {
107 CHECK(HasInputObject());
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400108 CHECK(boot_control_);
Chris Sosad317e402013-06-12 13:47:09 -0700109 install_plan_ = GetInputObject();
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800110
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400111 auto dynamic_control = boot_control_->GetDynamicPartitionControl();
112 CHECK(dynamic_control);
113
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000114 // Mount snapshot partitions for Virtual AB Compression Compression.
Kelvin Zhang06188352021-02-10 13:21:47 -0500115 if (dynamic_control->UpdateUsesSnapshotCompression()) {
Kelvin Zhang291ac8c2024-09-23 14:05:13 -0700116 // If we are switching slots, then we are required to MapAllPartitions,
117 // as FinishUpdate() requires all partitions to be mapped.
118 // And switching slots requires FinishUpdate() to be called first
119 if (!install_plan_.partitions.empty() ||
120 install_plan_.switch_slot_on_reboot) {
Kelvin Zhang263a5402022-12-08 23:03:32 -0800121 if (!dynamic_control->MapAllPartitions()) {
122 return CompletePostinstall(ErrorCode::kPostInstallMountError);
123 }
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400124 }
125 }
126
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800127 // We always powerwash when rolling back, however policy can determine
128 // if this is a full/normal powerwash, or a special rollback powerwash
129 // that retains a small amount of system state such as enrollment and
130 // network configuration. In both cases all user accounts are deleted.
Daniel Zhengad1eaea2024-07-31 16:09:34 -0700131 if (install_plan_.powerwash_required) {
Miriam Polzeraff72002020-08-27 08:20:39 +0200132 if (hardware_->SchedulePowerwash(
133 install_plan_.rollback_data_save_requested)) {
Alex Deymofb905d92016-06-03 19:26:58 -0700134 powerwash_scheduled_ = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700135 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700136 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700137 }
138 }
139
Alex Deymo0d298542016-03-30 18:31:49 -0700140 // Initialize all the partition weights.
141 partition_weight_.resize(install_plan_.partitions.size());
142 total_weight_ = 0;
143 for (size_t i = 0; i < install_plan_.partitions.size(); ++i) {
Tianjie Xu087de9d2019-11-01 17:11:22 -0700144 auto& partition = install_plan_.partitions[i];
145 if (!install_plan_.run_post_install && partition.postinstall_optional) {
146 partition.run_postinstall = false;
147 LOG(INFO) << "Skipping optional post-install for partition "
148 << partition.name << " according to install plan.";
149 }
150
Alex Deymo0d298542016-03-30 18:31:49 -0700151 // TODO(deymo): This code sets the weight to all the postinstall commands,
152 // but we could remember how long they took in the past and use those
153 // values.
Tianjie Xu087de9d2019-11-01 17:11:22 -0700154 partition_weight_[i] = partition.run_postinstall;
Alex Deymo0d298542016-03-30 18:31:49 -0700155 total_weight_ += partition_weight_[i];
156 }
157 accumulated_weight_ = 0;
158 ReportProgress(0);
159
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700160 PerformPartitionPostinstall();
161}
162
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700163bool PostinstallRunnerAction::MountPartition(
164 const InstallPlan::Partition& partition) noexcept {
165 // Perform post-install for the current_partition_ partition. At this point we
166 // need to call CompletePartitionPostinstall to complete the operation and
167 // cleanup.
168 const auto mountable_device = partition.readonly_target_path;
169 if (!utils::FileExists(mountable_device.c_str())) {
170 LOG(ERROR) << "Mountable device " << mountable_device << " for partition "
171 << partition.name << " does not exist";
172 return false;
173 }
174
175 if (!utils::FileExists(fs_mount_dir_.c_str())) {
176 LOG(ERROR) << "Mount point " << fs_mount_dir_
177 << " does not exist, mount call will fail";
178 return false;
179 }
180 // Double check that the fs_mount_dir is not busy with a previous mounted
181 // filesystem from a previous crashed postinstall step.
Kelvin Zhang1df000a2022-02-09 16:00:17 -0800182 EnsureUnmounted();
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700183
184#ifdef __ANDROID__
185 // In Chromium OS, the postinstall step is allowed to write to the block
186 // device on the target image, so we don't mark it as read-only and should
187 // be read-write since we just wrote to it during the update.
188
189 // Mark the block device as read-only before mounting for post-install.
190 if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) {
191 return false;
192 }
193#endif // __ANDROID__
194
195 if (!utils::MountFilesystem(
196 mountable_device,
197 fs_mount_dir_,
198 MS_RDONLY,
199 partition.filesystem_type,
200 hardware_->GetPartitionMountOptions(partition.name))) {
201 return false;
202 }
203 return true;
204}
205
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700206void PostinstallRunnerAction::PerformPartitionPostinstall() {
Alex Deymo390efed2016-02-18 11:00:40 -0800207 if (install_plan_.download_url.empty()) {
Kelvin Zhang24287c32023-03-09 10:13:26 -0800208 LOG(INFO) << "Skipping post-install";
Alex Deymo390efed2016-02-18 11:00:40 -0800209 return CompletePostinstall(ErrorCode::kSuccess);
210 }
211
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700212 // Skip all the partitions that don't have a post-install step.
213 while (current_partition_ < install_plan_.partitions.size() &&
214 !install_plan_.partitions[current_partition_].run_postinstall) {
215 VLOG(1) << "Skipping post-install on partition "
216 << install_plan_.partitions[current_partition_].name;
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700217 // Attempt to mount a device if it has postinstall script configured, even
218 // if we want to skip running postinstall script.
219 // This is because we've seen bugs like b/198787355 which is only triggered
220 // when you attempt to mount a device. If device fails to mount, it will
221 // likely fail to mount during boot anyway, so it's better to catch any
222 // issues earlier.
223 // It's possible that some of the partitions aren't mountable, but these
224 // partitions shouldn't have postinstall configured. Therefore we guard this
225 // logic with |postinstall_path.empty()|.
226 const auto& partition = install_plan_.partitions[current_partition_];
227 if (!partition.postinstall_path.empty()) {
228 const auto mountable_device = partition.readonly_target_path;
229 if (!MountPartition(partition)) {
230 return CompletePostinstall(ErrorCode::kPostInstallMountError);
231 }
Kelvin Zhangddc25802021-12-30 13:05:27 -0800232 LogBuildInfoForPartition(fs_mount_dir_);
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700233 if (!utils::UnmountFilesystem(fs_mount_dir_)) {
234 return CompletePartitionPostinstall(
235 1, "Error unmounting the device " + mountable_device);
236 }
237 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700238 current_partition_++;
239 }
240 if (current_partition_ == install_plan_.partitions.size())
241 return CompletePostinstall(ErrorCode::kSuccess);
242
243 const InstallPlan::Partition& partition =
244 install_plan_.partitions[current_partition_];
245
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400246 const string mountable_device = partition.readonly_target_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700247 // Perform post-install for the current_partition_ partition. At this point we
248 // need to call CompletePartitionPostinstall to complete the operation and
249 // cleanup.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700250
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700251 if (!MountPartition(partition)) {
252 CompletePostinstall(ErrorCode::kPostInstallMountError);
253 return;
Kelvin Zhang4ce01102020-11-16 09:32:08 -0500254 }
Kelvin Zhangddc25802021-12-30 13:05:27 -0800255 LogBuildInfoForPartition(fs_mount_dir_);
Alex Deymocbc22742016-03-04 17:53:02 -0800256 base::FilePath postinstall_path(partition.postinstall_path);
257 if (postinstall_path.IsAbsolute()) {
258 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
259 "path instead: "
260 << partition.postinstall_path;
261 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
262 }
263
264 string abs_path =
265 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800266 if (!base::StartsWith(
267 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
268 LOG(ERROR) << "Invalid relative postinstall path: "
269 << partition.postinstall_path;
270 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
271 }
272
Alex Deymo390efed2016-02-18 11:00:40 -0800273 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400274 << abs_path << ") installed on mountable device "
275 << mountable_device;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700276
Alex Deymo032e7722014-03-25 17:53:56 -0700277 // Logs the file format of the postinstall script we are about to run. This
278 // will help debug when the postinstall script doesn't match the architecture
279 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800280 LOG(INFO) << "Format file for new " << partition.postinstall_path
281 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700282
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800283 // Runs the postinstall script asynchronously to free up the main loop while
284 // it's running.
Alex Deymo0d298542016-03-30 18:31:49 -0700285 vector<string> command = {abs_path};
286#ifdef __ANDROID__
287 // In Brillo and Android, we pass the slot number and status fd.
288 command.push_back(std::to_string(install_plan_.target_slot));
289 command.push_back(std::to_string(kPostinstallStatusFd));
290#else
291 // Chrome OS postinstall expects the target rootfs as the first parameter.
292 command.push_back(partition.target_path);
293#endif // __ANDROID__
294
295 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymod15c5462016-03-09 18:11:12 -0800296 command,
Alex Deymo0d298542016-03-30 18:31:49 -0700297 Subprocess::kRedirectStderrToStdout,
298 {kPostinstallStatusFd},
Alex Deymod15c5462016-03-09 18:11:12 -0800299 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
300 base::Unretained(this)));
301 // Subprocess::Exec should never return a negative process id.
302 CHECK_GE(current_command_, 0);
303
Alex Deymo0d298542016-03-30 18:31:49 -0700304 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700305 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo0d298542016-03-30 18:31:49 -0700306 return;
307 }
308
309 // Monitor the status file descriptor.
310 progress_fd_ =
311 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
312 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
313 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
314 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
315 }
316
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900317 progress_controller_ = base::FileDescriptorWatcher::WatchReadable(
Alex Deymo0d298542016-03-30 18:31:49 -0700318 progress_fd_,
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900319 base::BindRepeating(&PostinstallRunnerAction::OnProgressFdReady,
320 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800321}
322
Alex Deymo0d298542016-03-30 18:31:49 -0700323void PostinstallRunnerAction::OnProgressFdReady() {
324 char buf[1024];
325 size_t bytes_read;
326 do {
327 bytes_read = 0;
328 bool eof;
329 bool ok =
hscham00b6aa22020-02-20 12:32:06 +0900330 utils::ReadAll(progress_fd_, buf, base::size(buf), &bytes_read, &eof);
Alex Deymo0d298542016-03-30 18:31:49 -0700331 progress_buffer_.append(buf, bytes_read);
332 // Process every line.
333 vector<string> lines = base::SplitString(
334 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
335 if (!lines.empty()) {
336 progress_buffer_ = lines.back();
337 lines.pop_back();
338 for (const auto& line : lines) {
339 ProcessProgressLine(line);
340 }
341 }
342 if (!ok || eof) {
343 // There was either an error or an EOF condition, so we are done watching
344 // the file descriptor.
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900345 progress_controller_.reset();
Alex Deymo0d298542016-03-30 18:31:49 -0700346 return;
347 }
348 } while (bytes_read);
349}
350
351bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
352 double frac = 0;
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700353 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 &&
354 !std::isnan(frac)) {
Alex Deymo0d298542016-03-30 18:31:49 -0700355 ReportProgress(frac);
356 return true;
357 }
358
359 return false;
360}
361
362void PostinstallRunnerAction::ReportProgress(double frac) {
363 if (!delegate_)
364 return;
Yoshitaka Ishida128936f2018-02-16 18:20:07 +0900365 if (current_partition_ >= partition_weight_.size() || total_weight_ == 0) {
Alex Deymo0d298542016-03-30 18:31:49 -0700366 delegate_->ProgressUpdate(1.);
367 return;
368 }
Alex Deymo44b35672016-04-05 17:57:48 -0700369 if (!std::isfinite(frac) || frac < 0)
Alex Deymo0d298542016-03-30 18:31:49 -0700370 frac = 0;
371 if (frac > 1)
372 frac = 1;
373 double postinst_action_progress =
374 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
375 total_weight_;
376 delegate_->ProgressUpdate(postinst_action_progress);
377}
378
379void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800380 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800381#ifndef __ANDROID__
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500382#if BASE_VER < 800000
383 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), true)) {
384#else
hscham043355b2020-11-17 16:50:10 +0900385 if (!base::DeleteFile(base::FilePath(fs_mount_dir_))) {
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500386#endif
Alex Deymo390efed2016-02-18 11:00:40 -0800387 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700388 }
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500389#endif
Alex Deymo0d298542016-03-30 18:31:49 -0700390
391 progress_fd_ = -1;
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900392 progress_controller_.reset();
Tianjie55abd3c2020-06-19 00:22:59 -0700393
Alex Deymo0d298542016-03-30 18:31:49 -0700394 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800395}
396
397void PostinstallRunnerAction::CompletePartitionPostinstall(
398 int return_code, const string& output) {
399 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700400 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700401
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800402 if (return_code != 0) {
403 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700404 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700405
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700406 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700407 // This special return code means that we tried to update firmware,
408 // but couldn't because we booted from FW B, and we need to reboot
409 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700410 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700411 }
Don Garrett81018e02013-07-30 18:46:31 -0700412
413 if (return_code == 4) {
414 // This special return code means that we tried to update firmware,
415 // but couldn't because we booted from FW B, and we need to reboot
416 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700417 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700418 }
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700419
420 // If postinstall script for this partition is optional we can ignore the
421 // result.
422 if (install_plan_.partitions[current_partition_].postinstall_optional) {
423 LOG(INFO) << "Ignoring postinstall failure since it is optional";
424 } else {
425 return CompletePostinstall(error_code);
426 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700427 }
Alex Deymo0d298542016-03-30 18:31:49 -0700428 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700429 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700430 ReportProgress(0);
431
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700432 PerformPartitionPostinstall();
433}
434
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000435PostinstallRunnerAction::~PostinstallRunnerAction() {
436 if (!install_plan_.partitions.empty()) {
437 auto dynamic_control = boot_control_->GetDynamicPartitionControl();
438 CHECK(dynamic_control);
439 dynamic_control->UnmapAllPartitions();
440 LOG(INFO) << "Unmapped all partitions.";
441 }
442}
443
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700444void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
445 // We only attempt to mark the new slot as active if all the postinstall
446 // steps succeeded.
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000447 DEFER {
448 if (error_code != ErrorCode::kSuccess &&
449 error_code != ErrorCode::kUpdatedButNotActive) {
Kelvin Zhang99cbbe72024-01-18 14:50:01 -0800450 LOG(ERROR) << "Postinstall action failed. "
451 << utils::ErrorCodeToString(error_code);
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000452
453 // Undo any changes done to trigger Powerwash.
454 if (powerwash_scheduled_)
455 hardware_->CancelPowerwash();
456 }
457 processor_->ActionComplete(this, error_code);
458 };
Sen Jiang02c49422017-10-31 15:14:11 -0700459 if (error_code == ErrorCode::kSuccess) {
460 if (install_plan_.switch_slot_on_reboot) {
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000461 if (!boot_control_->GetDynamicPartitionControl()->FinishUpdate(
462 install_plan_.powerwash_required) ||
463 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Sen Jiang02c49422017-10-31 15:14:11 -0700464 error_code = ErrorCode::kPostinstallRunnerError;
Tianjie Xud6aa91f2019-11-14 11:55:10 -0800465 } else {
466 // Schedules warm reset on next reboot, ignores the error.
467 hardware_->SetWarmReset(true);
Tianjie838793d2021-01-14 22:05:13 -0800468 // Sets the vbmeta digest for the other slot to boot into.
469 hardware_->SetVbmetaDigestForInactiveSlot(false);
Sen Jiang02c49422017-10-31 15:14:11 -0700470 }
471 } else {
472 error_code = ErrorCode::kUpdatedButNotActive;
473 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700474 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700475
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700476 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700477 if (HasOutputPipe()) {
478 SetOutputObject(install_plan_);
479 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000480}
481
Alex Deymod15c5462016-03-09 18:11:12 -0800482void PostinstallRunnerAction::SuspendAction() {
483 if (!current_command_)
484 return;
485 if (kill(current_command_, SIGSTOP) != 0) {
486 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800487 } else {
488 is_current_command_suspended_ = true;
Alex Deymod15c5462016-03-09 18:11:12 -0800489 }
490}
491
492void PostinstallRunnerAction::ResumeAction() {
493 if (!current_command_)
494 return;
495 if (kill(current_command_, SIGCONT) != 0) {
496 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800497 } else {
498 is_current_command_suspended_ = false;
Alex Deymod15c5462016-03-09 18:11:12 -0800499 }
500}
501
502void PostinstallRunnerAction::TerminateProcessing() {
503 if (!current_command_)
504 return;
505 // Calling KillExec() will discard the callback we registered and therefore
506 // the unretained reference to this object.
507 Subprocess::Get().KillExec(current_command_);
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800508
509 // If the command has been suspended, resume it after KillExec() so that the
510 // process can process the SIGTERM sent by KillExec().
511 if (is_current_command_suspended_) {
512 ResumeAction();
513 }
514
Alex Deymod15c5462016-03-09 18:11:12 -0800515 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700516 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800517}
518
adlr@google.com3defe6a2009-12-04 20:57:17 +0000519} // namespace chromeos_update_engine