blob: 1f423a9aa716fd44085655fe8ef0f842d9ee352f [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>
Luca Stefanid1ddfee2019-01-03 21:20:42 +010022#include <selinux/selinux.h>
Alex Vakulenko44cab302014-07-23 13:12:15 -070023#include <sys/mount.h>
Alex Deymod15c5462016-03-09 18:11:12 -080024#include <sys/types.h>
Alex Deymo0d298542016-03-30 18:31:49 -070025#include <unistd.h>
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070026
Alex Deymo44b35672016-04-05 17:57:48 -070027#include <cmath>
Kelvin Zhangddc25802021-12-30 13:05:27 -080028#include <fstream>
29#include <string>
Alex Deymo44b35672016-04-05 17:57:48 -070030
Alex Deymoe5e5fe92015-10-05 09:28:19 -070031#include <base/files/file_path.h>
32#include <base/files/file_util.h>
Alex Deymod15c5462016-03-09 18:11:12 -080033#include <base/logging.h>
Alex Deymo0d298542016-03-30 18:31:49 -070034#include <base/strings/string_split.h>
Alex Deymo461b2592015-07-24 20:10:52 -070035
Alex Deymo39910dc2015-11-09 17:04:30 -080036#include "update_engine/common/action_processor.h"
Alex Deymob15a0b82015-11-25 20:30:40 -030037#include "update_engine/common/boot_control_interface.h"
Kelvin Zhang99cbbe72024-01-18 14:50:01 -080038#include "update_engine/common/error_code_utils.h"
39#include "update_engine/common/platform_constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080040#include "update_engine/common/subprocess.h"
41#include "update_engine/common/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000042
Alex Deymo0d298542016-03-30 18:31:49 -070043namespace {
44
45// The file descriptor number from the postinstall program's perspective where
46// it can report status updates. This can be any number greater than 2 (stderr),
47// but must be kept in sync with the "bin/postinst_progress" defined in the
48// sample_images.sh file.
49const int kPostinstallStatusFd = 3;
50
Kelvin Zhangddc25802021-12-30 13:05:27 -080051static constexpr bool Contains(std::string_view haystack,
52 std::string_view needle) {
53 return haystack.find(needle) != std::string::npos;
54}
55
56static void LogBuildInfoForPartition(std::string_view mount_point) {
57 static constexpr std::array<std::string_view, 3> kBuildPropFiles{
58 "build.prop", "etc/build.prop", "system/build.prop"};
59 for (const auto& file : kBuildPropFiles) {
60 auto path = std::string(mount_point);
61 if (path.back() != '/') {
62 path.push_back('/');
63 }
64 path += file;
65 LOG(INFO) << "Trying to read " << path;
66 std::ifstream infile(path);
67 std::string line;
68 while (std::getline(infile, line)) {
69 if (Contains(line, "ro.build")) {
70 LOG(INFO) << line;
71 }
72 }
73 }
74}
75
Alex Deymo0d298542016-03-30 18:31:49 -070076} // namespace
77
adlr@google.com3defe6a2009-12-04 20:57:17 +000078namespace chromeos_update_engine {
79
80using std::string;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070081using std::vector;
adlr@google.com3defe6a2009-12-04 20:57:17 +000082
Kelvin Zhange9def4e2020-12-02 14:04:09 -050083PostinstallRunnerAction::PostinstallRunnerAction(
84 BootControlInterface* boot_control, HardwareInterface* hardware)
85 : boot_control_(boot_control), hardware_(hardware) {
86#ifdef __ANDROID__
87 fs_mount_dir_ = "/postinstall";
88#else // __ANDROID__
89 base::FilePath temp_dir;
90 TEST_AND_RETURN(base::CreateNewTempDirectory("au_postint_mount", &temp_dir));
91 fs_mount_dir_ = temp_dir.value();
92#endif // __ANDROID__
Kelvin Zhang2379fa92020-12-09 14:39:04 -050093 CHECK(!fs_mount_dir_.empty());
Kelvin Zhang1df000a2022-02-09 16:00:17 -080094 EnsureUnmounted();
Kelvin Zhang2379fa92020-12-09 14:39:04 -050095 LOG(INFO) << "postinstall mount point: " << fs_mount_dir_;
Kelvin Zhange9def4e2020-12-02 14:04:09 -050096}
97
Kelvin Zhang1df000a2022-02-09 16:00:17 -080098void PostinstallRunnerAction::EnsureUnmounted() {
99 if (utils::IsMountpoint(fs_mount_dir_)) {
100 LOG(INFO) << "Found previously mounted filesystem at " << fs_mount_dir_;
101 utils::UnmountFilesystem(fs_mount_dir_);
102 }
103}
104
adlr@google.com3defe6a2009-12-04 20:57:17 +0000105void PostinstallRunnerAction::PerformAction() {
106 CHECK(HasInputObject());
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400107 CHECK(boot_control_);
Chris Sosad317e402013-06-12 13:47:09 -0700108 install_plan_ = GetInputObject();
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800109
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400110 auto dynamic_control = boot_control_->GetDynamicPartitionControl();
111 CHECK(dynamic_control);
112
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000113 // Mount snapshot partitions for Virtual AB Compression Compression.
Kelvin Zhang06188352021-02-10 13:21:47 -0500114 if (dynamic_control->UpdateUsesSnapshotCompression()) {
Kelvin Zhang291ac8c2024-09-23 14:05:13 -0700115 // If we are switching slots, then we are required to MapAllPartitions,
116 // as FinishUpdate() requires all partitions to be mapped.
117 // And switching slots requires FinishUpdate() to be called first
118 if (!install_plan_.partitions.empty() ||
119 install_plan_.switch_slot_on_reboot) {
Kelvin Zhang263a5402022-12-08 23:03:32 -0800120 if (!dynamic_control->MapAllPartitions()) {
121 return CompletePostinstall(ErrorCode::kPostInstallMountError);
122 }
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400123 }
124 }
125
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800126 // We always powerwash when rolling back, however policy can determine
127 // if this is a full/normal powerwash, or a special rollback powerwash
128 // that retains a small amount of system state such as enrollment and
129 // network configuration. In both cases all user accounts are deleted.
Daniel Zhengad1eaea2024-07-31 16:09:34 -0700130 if (install_plan_.powerwash_required) {
Kelvin Zhang399bd4d2024-12-03 11:08:30 -0800131 if (hardware_->SchedulePowerwash()) {
Alex Deymofb905d92016-06-03 19:26:58 -0700132 powerwash_scheduled_ = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700133 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700134 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700135 }
136 }
137
Alex Deymo0d298542016-03-30 18:31:49 -0700138 // Initialize all the partition weights.
139 partition_weight_.resize(install_plan_.partitions.size());
140 total_weight_ = 0;
141 for (size_t i = 0; i < install_plan_.partitions.size(); ++i) {
Tianjie Xu087de9d2019-11-01 17:11:22 -0700142 auto& partition = install_plan_.partitions[i];
143 if (!install_plan_.run_post_install && partition.postinstall_optional) {
144 partition.run_postinstall = false;
145 LOG(INFO) << "Skipping optional post-install for partition "
146 << partition.name << " according to install plan.";
147 }
148
Alex Deymo0d298542016-03-30 18:31:49 -0700149 // TODO(deymo): This code sets the weight to all the postinstall commands,
150 // but we could remember how long they took in the past and use those
151 // values.
Tianjie Xu087de9d2019-11-01 17:11:22 -0700152 partition_weight_[i] = partition.run_postinstall;
Alex Deymo0d298542016-03-30 18:31:49 -0700153 total_weight_ += partition_weight_[i];
154 }
155 accumulated_weight_ = 0;
156 ReportProgress(0);
157
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700158 PerformPartitionPostinstall();
159}
160
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700161bool PostinstallRunnerAction::MountPartition(
162 const InstallPlan::Partition& partition) noexcept {
163 // Perform post-install for the current_partition_ partition. At this point we
164 // need to call CompletePartitionPostinstall to complete the operation and
165 // cleanup.
166 const auto mountable_device = partition.readonly_target_path;
167 if (!utils::FileExists(mountable_device.c_str())) {
168 LOG(ERROR) << "Mountable device " << mountable_device << " for partition "
169 << partition.name << " does not exist";
170 return false;
171 }
172
173 if (!utils::FileExists(fs_mount_dir_.c_str())) {
174 LOG(ERROR) << "Mount point " << fs_mount_dir_
175 << " does not exist, mount call will fail";
176 return false;
177 }
178 // Double check that the fs_mount_dir is not busy with a previous mounted
179 // filesystem from a previous crashed postinstall step.
Kelvin Zhang1df000a2022-02-09 16:00:17 -0800180 EnsureUnmounted();
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700181
182#ifdef __ANDROID__
micky387cf367002023-11-15 22:05:10 +0100183#ifdef USE_WEEKLY_BUILD
Dan Pasanena747c632017-05-10 16:29:35 -0500184 // Check the currently installed /system partition to see if it's ever
185 // been mounted R/W. If it has, we'll run backuptool scripts for it
186 // since we can safely assume something on the partition has been
187 // changed and we won't be breaking verity (since it's already been
188 // broken). If it hasn't ever been mounted R/W, we can assume that
189 // the rom that the user is upgrading to will have everything they
190 // need and no addon.d scripts will need to be run to retain stuff
191 // after the upgrade.
192 //
193 // Use the following disk layout info to make the determination
194 // https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout
195 // Super block starts from block 0, offset 0x400
196 // 0x2C: len32 Mount time
197 // 0x30: len32 Write time
198 // 0x34: len16 Number of mounts since the last fsck
199 // 0x38: len16 Magic signature 0xEF53
200
201 string source_path;
202
203 if (install_plan_.source_slot != BootControlInterface::kInvalidSlot) {
204 boot_control_->GetPartitionDevice(partition.name, install_plan_.source_slot, &source_path);
205 }
206
207 uint16_t mount_count = 0;
208
209 if (!source_path.empty()) {
210 brillo::Blob chunk;
211
212 utils::ReadFileChunk(source_path, 0x400 + 0x34, sizeof(uint16_t), &chunk);
213 mount_count = *reinterpret_cast<uint16_t*>(chunk.data());
214 }
215
216 LOG(INFO) << source_path << " has been mounted R/W " << mount_count << " times.";
217
218 if (mount_count > 0) {
Christian Hoffmann76d57b92022-01-23 12:04:42 +0100219 if (!utils::SetBlockDeviceReadOnly(mountable_device, false)) {
220 LOG(ERROR) << "Error marking the device " << mountable_device << " writeable.";
221 return false;
222 }
Dan Pasanena747c632017-05-10 16:29:35 -0500223 // Mount the target partition R/W
224 LOG(INFO) << "Running backuptool scripts";
225 utils::MountFilesystem(mountable_device, fs_mount_dir_, MS_NOATIME | MS_NODEV | MS_NODIRATIME,
226 partition.filesystem_type, "seclabel");
227
Luca Stefanid1ddfee2019-01-03 21:20:42 +0100228 // Switch to a permissive domain
229 if (setexeccon("u:r:backuptool:s0")) {
230 LOG(ERROR) << "Failed to set backuptool context";
231 return false;
232 }
233
Dan Pasanena747c632017-05-10 16:29:35 -0500234 // Run backuptool script
235 int ret = system("/postinstall/system/bin/backuptool_postinstall.sh");
236 if (ret == -1 || WEXITSTATUS(ret) != 0) {
237 LOG(ERROR) << "Backuptool postinstall step failed. ret=" << ret;
238 }
Luca Stefanid1ddfee2019-01-03 21:20:42 +0100239
240 // Switch back to update_engine domain
241 if (setexeccon(nullptr)) {
242 LOG(ERROR) << "Failed to set update_engine context";
243 return false;
244 }
Dan Pasanena747c632017-05-10 16:29:35 -0500245 } else {
246 LOG(INFO) << "Skipping backuptool scripts";
247 }
248
249 utils::UnmountFilesystem(fs_mount_dir_);
micky387cf367002023-11-15 22:05:10 +0100250#endif // USE_WEEKLY_BUILD
Dan Pasanena747c632017-05-10 16:29:35 -0500251
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700252 // In Chromium OS, the postinstall step is allowed to write to the block
253 // device on the target image, so we don't mark it as read-only and should
254 // be read-write since we just wrote to it during the update.
255
256 // Mark the block device as read-only before mounting for post-install.
257 if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) {
258 return false;
259 }
260#endif // __ANDROID__
261
262 if (!utils::MountFilesystem(
263 mountable_device,
264 fs_mount_dir_,
265 MS_RDONLY,
266 partition.filesystem_type,
267 hardware_->GetPartitionMountOptions(partition.name))) {
268 return false;
269 }
270 return true;
271}
272
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700273void PostinstallRunnerAction::PerformPartitionPostinstall() {
Alex Deymo390efed2016-02-18 11:00:40 -0800274 if (install_plan_.download_url.empty()) {
Kelvin Zhang24287c32023-03-09 10:13:26 -0800275 LOG(INFO) << "Skipping post-install";
Alex Deymo390efed2016-02-18 11:00:40 -0800276 return CompletePostinstall(ErrorCode::kSuccess);
277 }
278
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700279 // Skip all the partitions that don't have a post-install step.
280 while (current_partition_ < install_plan_.partitions.size() &&
281 !install_plan_.partitions[current_partition_].run_postinstall) {
282 VLOG(1) << "Skipping post-install on partition "
283 << install_plan_.partitions[current_partition_].name;
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700284 // Attempt to mount a device if it has postinstall script configured, even
285 // if we want to skip running postinstall script.
286 // This is because we've seen bugs like b/198787355 which is only triggered
287 // when you attempt to mount a device. If device fails to mount, it will
288 // likely fail to mount during boot anyway, so it's better to catch any
289 // issues earlier.
290 // It's possible that some of the partitions aren't mountable, but these
291 // partitions shouldn't have postinstall configured. Therefore we guard this
292 // logic with |postinstall_path.empty()|.
293 const auto& partition = install_plan_.partitions[current_partition_];
294 if (!partition.postinstall_path.empty()) {
295 const auto mountable_device = partition.readonly_target_path;
296 if (!MountPartition(partition)) {
297 return CompletePostinstall(ErrorCode::kPostInstallMountError);
298 }
Kelvin Zhangddc25802021-12-30 13:05:27 -0800299 LogBuildInfoForPartition(fs_mount_dir_);
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700300 if (!utils::UnmountFilesystem(fs_mount_dir_)) {
301 return CompletePartitionPostinstall(
302 1, "Error unmounting the device " + mountable_device);
303 }
304 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700305 current_partition_++;
306 }
307 if (current_partition_ == install_plan_.partitions.size())
308 return CompletePostinstall(ErrorCode::kSuccess);
309
310 const InstallPlan::Partition& partition =
311 install_plan_.partitions[current_partition_];
312
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400313 const string mountable_device = partition.readonly_target_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700314 // Perform post-install for the current_partition_ partition. At this point we
315 // need to call CompletePartitionPostinstall to complete the operation and
316 // cleanup.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700317
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700318 if (!MountPartition(partition)) {
319 CompletePostinstall(ErrorCode::kPostInstallMountError);
320 return;
Kelvin Zhang4ce01102020-11-16 09:32:08 -0500321 }
Kelvin Zhangddc25802021-12-30 13:05:27 -0800322 LogBuildInfoForPartition(fs_mount_dir_);
Alex Deymocbc22742016-03-04 17:53:02 -0800323 base::FilePath postinstall_path(partition.postinstall_path);
324 if (postinstall_path.IsAbsolute()) {
325 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
326 "path instead: "
327 << partition.postinstall_path;
328 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
329 }
330
331 string abs_path =
332 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800333 if (!base::StartsWith(
334 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
335 LOG(ERROR) << "Invalid relative postinstall path: "
336 << partition.postinstall_path;
337 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
338 }
339
Alex Deymo390efed2016-02-18 11:00:40 -0800340 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400341 << abs_path << ") installed on mountable device "
342 << mountable_device;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700343
Alex Deymo032e7722014-03-25 17:53:56 -0700344 // Logs the file format of the postinstall script we are about to run. This
345 // will help debug when the postinstall script doesn't match the architecture
346 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800347 LOG(INFO) << "Format file for new " << partition.postinstall_path
348 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700349
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800350 // Runs the postinstall script asynchronously to free up the main loop while
351 // it's running.
Alex Deymo0d298542016-03-30 18:31:49 -0700352 vector<string> command = {abs_path};
353#ifdef __ANDROID__
354 // In Brillo and Android, we pass the slot number and status fd.
355 command.push_back(std::to_string(install_plan_.target_slot));
356 command.push_back(std::to_string(kPostinstallStatusFd));
357#else
358 // Chrome OS postinstall expects the target rootfs as the first parameter.
359 command.push_back(partition.target_path);
360#endif // __ANDROID__
361
362 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymod15c5462016-03-09 18:11:12 -0800363 command,
Alex Deymo0d298542016-03-30 18:31:49 -0700364 Subprocess::kRedirectStderrToStdout,
365 {kPostinstallStatusFd},
Alex Deymod15c5462016-03-09 18:11:12 -0800366 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
367 base::Unretained(this)));
368 // Subprocess::Exec should never return a negative process id.
369 CHECK_GE(current_command_, 0);
370
Alex Deymo0d298542016-03-30 18:31:49 -0700371 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700372 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo0d298542016-03-30 18:31:49 -0700373 return;
374 }
375
376 // Monitor the status file descriptor.
377 progress_fd_ =
378 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
379 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
380 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
381 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
382 }
383
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900384 progress_controller_ = base::FileDescriptorWatcher::WatchReadable(
Alex Deymo0d298542016-03-30 18:31:49 -0700385 progress_fd_,
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900386 base::BindRepeating(&PostinstallRunnerAction::OnProgressFdReady,
387 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800388}
389
Alex Deymo0d298542016-03-30 18:31:49 -0700390void PostinstallRunnerAction::OnProgressFdReady() {
391 char buf[1024];
392 size_t bytes_read;
393 do {
394 bytes_read = 0;
395 bool eof;
396 bool ok =
Kokoa Matsuda91aa2172024-10-16 16:01:04 +0900397 utils::ReadAll(progress_fd_, buf, std::size(buf), &bytes_read, &eof);
Alex Deymo0d298542016-03-30 18:31:49 -0700398 progress_buffer_.append(buf, bytes_read);
399 // Process every line.
400 vector<string> lines = base::SplitString(
401 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
402 if (!lines.empty()) {
403 progress_buffer_ = lines.back();
404 lines.pop_back();
405 for (const auto& line : lines) {
406 ProcessProgressLine(line);
407 }
408 }
409 if (!ok || eof) {
410 // There was either an error or an EOF condition, so we are done watching
411 // the file descriptor.
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900412 progress_controller_.reset();
Alex Deymo0d298542016-03-30 18:31:49 -0700413 return;
414 }
415 } while (bytes_read);
416}
417
418bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
419 double frac = 0;
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700420 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 &&
421 !std::isnan(frac)) {
Alex Deymo0d298542016-03-30 18:31:49 -0700422 ReportProgress(frac);
423 return true;
424 }
425
426 return false;
427}
428
429void PostinstallRunnerAction::ReportProgress(double frac) {
430 if (!delegate_)
431 return;
Yoshitaka Ishida128936f2018-02-16 18:20:07 +0900432 if (current_partition_ >= partition_weight_.size() || total_weight_ == 0) {
Alex Deymo0d298542016-03-30 18:31:49 -0700433 delegate_->ProgressUpdate(1.);
434 return;
435 }
Alex Deymo44b35672016-04-05 17:57:48 -0700436 if (!std::isfinite(frac) || frac < 0)
Alex Deymo0d298542016-03-30 18:31:49 -0700437 frac = 0;
438 if (frac > 1)
439 frac = 1;
440 double postinst_action_progress =
441 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
442 total_weight_;
443 delegate_->ProgressUpdate(postinst_action_progress);
444}
445
446void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800447 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800448#ifndef __ANDROID__
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500449#if BASE_VER < 800000
450 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), true)) {
451#else
hscham043355b2020-11-17 16:50:10 +0900452 if (!base::DeleteFile(base::FilePath(fs_mount_dir_))) {
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500453#endif
Alex Deymo390efed2016-02-18 11:00:40 -0800454 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700455 }
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500456#endif
Alex Deymo0d298542016-03-30 18:31:49 -0700457
458 progress_fd_ = -1;
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900459 progress_controller_.reset();
Tianjie55abd3c2020-06-19 00:22:59 -0700460
Alex Deymo0d298542016-03-30 18:31:49 -0700461 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800462}
463
464void PostinstallRunnerAction::CompletePartitionPostinstall(
465 int return_code, const string& output) {
466 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700467 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700468
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800469 if (return_code != 0) {
470 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700471 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700472
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700473 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700474 // This special return code means that we tried to update firmware,
475 // but couldn't because we booted from FW B, and we need to reboot
476 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700477 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700478 }
Don Garrett81018e02013-07-30 18:46:31 -0700479
480 if (return_code == 4) {
481 // This special return code means that we tried to update firmware,
482 // but couldn't because we booted from FW B, and we need to reboot
483 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700484 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700485 }
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700486
487 // If postinstall script for this partition is optional we can ignore the
488 // result.
489 if (install_plan_.partitions[current_partition_].postinstall_optional) {
490 LOG(INFO) << "Ignoring postinstall failure since it is optional";
491 } else {
492 return CompletePostinstall(error_code);
493 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700494 }
Alex Deymo0d298542016-03-30 18:31:49 -0700495 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700496 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700497 ReportProgress(0);
498
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700499 PerformPartitionPostinstall();
500}
501
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000502PostinstallRunnerAction::~PostinstallRunnerAction() {
503 if (!install_plan_.partitions.empty()) {
504 auto dynamic_control = boot_control_->GetDynamicPartitionControl();
505 CHECK(dynamic_control);
506 dynamic_control->UnmapAllPartitions();
507 LOG(INFO) << "Unmapped all partitions.";
508 }
509}
510
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700511void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
512 // We only attempt to mark the new slot as active if all the postinstall
513 // steps succeeded.
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000514 DEFER {
515 if (error_code != ErrorCode::kSuccess &&
516 error_code != ErrorCode::kUpdatedButNotActive) {
Kelvin Zhang99cbbe72024-01-18 14:50:01 -0800517 LOG(ERROR) << "Postinstall action failed. "
518 << utils::ErrorCodeToString(error_code);
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000519
520 // Undo any changes done to trigger Powerwash.
521 if (powerwash_scheduled_)
522 hardware_->CancelPowerwash();
523 }
524 processor_->ActionComplete(this, error_code);
525 };
Sen Jiang02c49422017-10-31 15:14:11 -0700526 if (error_code == ErrorCode::kSuccess) {
527 if (install_plan_.switch_slot_on_reboot) {
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000528 if (!boot_control_->GetDynamicPartitionControl()->FinishUpdate(
529 install_plan_.powerwash_required) ||
530 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Sen Jiang02c49422017-10-31 15:14:11 -0700531 error_code = ErrorCode::kPostinstallRunnerError;
Tianjie Xud6aa91f2019-11-14 11:55:10 -0800532 } else {
533 // Schedules warm reset on next reboot, ignores the error.
534 hardware_->SetWarmReset(true);
Tianjie838793d2021-01-14 22:05:13 -0800535 // Sets the vbmeta digest for the other slot to boot into.
536 hardware_->SetVbmetaDigestForInactiveSlot(false);
Sen Jiang02c49422017-10-31 15:14:11 -0700537 }
538 } else {
539 error_code = ErrorCode::kUpdatedButNotActive;
540 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700541 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700542
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700543 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700544 if (HasOutputPipe()) {
545 SetOutputObject(install_plan_);
546 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000547}
548
Alex Deymod15c5462016-03-09 18:11:12 -0800549void PostinstallRunnerAction::SuspendAction() {
550 if (!current_command_)
551 return;
552 if (kill(current_command_, SIGSTOP) != 0) {
553 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800554 } else {
555 is_current_command_suspended_ = true;
Alex Deymod15c5462016-03-09 18:11:12 -0800556 }
557}
558
559void PostinstallRunnerAction::ResumeAction() {
560 if (!current_command_)
561 return;
562 if (kill(current_command_, SIGCONT) != 0) {
563 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800564 } else {
565 is_current_command_suspended_ = false;
Alex Deymod15c5462016-03-09 18:11:12 -0800566 }
567}
568
569void PostinstallRunnerAction::TerminateProcessing() {
570 if (!current_command_)
571 return;
572 // Calling KillExec() will discard the callback we registered and therefore
573 // the unretained reference to this object.
574 Subprocess::Get().KillExec(current_command_);
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800575
576 // If the command has been suspended, resume it after KillExec() so that the
577 // process can process the SIGTERM sent by KillExec().
578 if (is_current_command_suspended_) {
579 ResumeAction();
580 }
581
Alex Deymod15c5462016-03-09 18:11:12 -0800582 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700583 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800584}
585
adlr@google.com3defe6a2009-12-04 20:57:17 +0000586} // namespace chromeos_update_engine