blob: e7804ea8c1244591d69c2e4b0e7f946700705846 [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>
hscham00b6aa22020-02-20 12:32:06 +090034#include <base/stl_util.h>
Alex Deymo0d298542016-03-30 18:31:49 -070035#include <base/strings/string_split.h>
Alex Deymo390efed2016-02-18 11:00:40 -080036#include <base/strings/string_util.h>
Alex Deymo461b2592015-07-24 20:10:52 -070037
Alex Deymo39910dc2015-11-09 17:04:30 -080038#include "update_engine/common/action_processor.h"
Alex Deymob15a0b82015-11-25 20:30:40 -030039#include "update_engine/common/boot_control_interface.h"
Kelvin Zhang99cbbe72024-01-18 14:50:01 -080040#include "update_engine/common/error_code_utils.h"
41#include "update_engine/common/platform_constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080042#include "update_engine/common/subprocess.h"
43#include "update_engine/common/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000044
Alex Deymo0d298542016-03-30 18:31:49 -070045namespace {
46
47// The file descriptor number from the postinstall program's perspective where
48// it can report status updates. This can be any number greater than 2 (stderr),
49// but must be kept in sync with the "bin/postinst_progress" defined in the
50// sample_images.sh file.
51const int kPostinstallStatusFd = 3;
52
Kelvin Zhangddc25802021-12-30 13:05:27 -080053static constexpr bool Contains(std::string_view haystack,
54 std::string_view needle) {
55 return haystack.find(needle) != std::string::npos;
56}
57
58static void LogBuildInfoForPartition(std::string_view mount_point) {
59 static constexpr std::array<std::string_view, 3> kBuildPropFiles{
60 "build.prop", "etc/build.prop", "system/build.prop"};
61 for (const auto& file : kBuildPropFiles) {
62 auto path = std::string(mount_point);
63 if (path.back() != '/') {
64 path.push_back('/');
65 }
66 path += file;
67 LOG(INFO) << "Trying to read " << path;
68 std::ifstream infile(path);
69 std::string line;
70 while (std::getline(infile, line)) {
71 if (Contains(line, "ro.build")) {
72 LOG(INFO) << line;
73 }
74 }
75 }
76}
77
Alex Deymo0d298542016-03-30 18:31:49 -070078} // namespace
79
adlr@google.com3defe6a2009-12-04 20:57:17 +000080namespace chromeos_update_engine {
81
82using std::string;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070083using std::vector;
adlr@google.com3defe6a2009-12-04 20:57:17 +000084
Kelvin Zhange9def4e2020-12-02 14:04:09 -050085PostinstallRunnerAction::PostinstallRunnerAction(
86 BootControlInterface* boot_control, HardwareInterface* hardware)
87 : boot_control_(boot_control), hardware_(hardware) {
88#ifdef __ANDROID__
89 fs_mount_dir_ = "/postinstall";
90#else // __ANDROID__
91 base::FilePath temp_dir;
92 TEST_AND_RETURN(base::CreateNewTempDirectory("au_postint_mount", &temp_dir));
93 fs_mount_dir_ = temp_dir.value();
94#endif // __ANDROID__
Kelvin Zhang2379fa92020-12-09 14:39:04 -050095 CHECK(!fs_mount_dir_.empty());
Kelvin Zhang1df000a2022-02-09 16:00:17 -080096 EnsureUnmounted();
Kelvin Zhang2379fa92020-12-09 14:39:04 -050097 LOG(INFO) << "postinstall mount point: " << fs_mount_dir_;
Kelvin Zhange9def4e2020-12-02 14:04:09 -050098}
99
Kelvin Zhang1df000a2022-02-09 16:00:17 -0800100void PostinstallRunnerAction::EnsureUnmounted() {
101 if (utils::IsMountpoint(fs_mount_dir_)) {
102 LOG(INFO) << "Found previously mounted filesystem at " << fs_mount_dir_;
103 utils::UnmountFilesystem(fs_mount_dir_);
104 }
105}
106
adlr@google.com3defe6a2009-12-04 20:57:17 +0000107void PostinstallRunnerAction::PerformAction() {
108 CHECK(HasInputObject());
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400109 CHECK(boot_control_);
Chris Sosad317e402013-06-12 13:47:09 -0700110 install_plan_ = GetInputObject();
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800111
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400112 auto dynamic_control = boot_control_->GetDynamicPartitionControl();
113 CHECK(dynamic_control);
114
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000115 // Mount snapshot partitions for Virtual AB Compression Compression.
Kelvin Zhang06188352021-02-10 13:21:47 -0500116 if (dynamic_control->UpdateUsesSnapshotCompression()) {
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400117 // Before calling MapAllPartitions to map snapshot devices, all CowWriters
118 // must be closed, and MapAllPartitions() should be called.
Kelvin Zhang263a5402022-12-08 23:03:32 -0800119 if (!install_plan_.partitions.empty()) {
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.
Marton Hunyady199152d2018-05-07 19:08:48 +0200130 if (install_plan_.powerwash_required || install_plan_.is_rollback) {
Miriam Polzeraff72002020-08-27 08:20:39 +0200131 if (hardware_->SchedulePowerwash(
132 install_plan_.rollback_data_save_requested)) {
Alex Deymofb905d92016-06-03 19:26:58 -0700133 powerwash_scheduled_ = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700134 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700135 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700136 }
137 }
138
Alex Deymo0d298542016-03-30 18:31:49 -0700139 // Initialize all the partition weights.
140 partition_weight_.resize(install_plan_.partitions.size());
141 total_weight_ = 0;
142 for (size_t i = 0; i < install_plan_.partitions.size(); ++i) {
Tianjie Xu087de9d2019-11-01 17:11:22 -0700143 auto& partition = install_plan_.partitions[i];
144 if (!install_plan_.run_post_install && partition.postinstall_optional) {
145 partition.run_postinstall = false;
146 LOG(INFO) << "Skipping optional post-install for partition "
147 << partition.name << " according to install plan.";
148 }
149
Alex Deymo0d298542016-03-30 18:31:49 -0700150 // TODO(deymo): This code sets the weight to all the postinstall commands,
151 // but we could remember how long they took in the past and use those
152 // values.
Tianjie Xu087de9d2019-11-01 17:11:22 -0700153 partition_weight_[i] = partition.run_postinstall;
Alex Deymo0d298542016-03-30 18:31:49 -0700154 total_weight_ += partition_weight_[i];
155 }
156 accumulated_weight_ = 0;
157 ReportProgress(0);
158
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700159 PerformPartitionPostinstall();
160}
161
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700162bool PostinstallRunnerAction::MountPartition(
163 const InstallPlan::Partition& partition) noexcept {
164 // Perform post-install for the current_partition_ partition. At this point we
165 // need to call CompletePartitionPostinstall to complete the operation and
166 // cleanup.
167 const auto mountable_device = partition.readonly_target_path;
168 if (!utils::FileExists(mountable_device.c_str())) {
169 LOG(ERROR) << "Mountable device " << mountable_device << " for partition "
170 << partition.name << " does not exist";
171 return false;
172 }
173
174 if (!utils::FileExists(fs_mount_dir_.c_str())) {
175 LOG(ERROR) << "Mount point " << fs_mount_dir_
176 << " does not exist, mount call will fail";
177 return false;
178 }
179 // Double check that the fs_mount_dir is not busy with a previous mounted
180 // filesystem from a previous crashed postinstall step.
Kelvin Zhang1df000a2022-02-09 16:00:17 -0800181 EnsureUnmounted();
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700182
183#ifdef __ANDROID__
micky387cf367002023-11-15 22:05:10 +0100184#ifdef USE_WEEKLY_BUILD
Dan Pasanena747c632017-05-10 16:29:35 -0500185 // Check the currently installed /system partition to see if it's ever
186 // been mounted R/W. If it has, we'll run backuptool scripts for it
187 // since we can safely assume something on the partition has been
188 // changed and we won't be breaking verity (since it's already been
189 // broken). If it hasn't ever been mounted R/W, we can assume that
190 // the rom that the user is upgrading to will have everything they
191 // need and no addon.d scripts will need to be run to retain stuff
192 // after the upgrade.
193 //
194 // Use the following disk layout info to make the determination
195 // https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout
196 // Super block starts from block 0, offset 0x400
197 // 0x2C: len32 Mount time
198 // 0x30: len32 Write time
199 // 0x34: len16 Number of mounts since the last fsck
200 // 0x38: len16 Magic signature 0xEF53
201
202 string source_path;
203
204 if (install_plan_.source_slot != BootControlInterface::kInvalidSlot) {
205 boot_control_->GetPartitionDevice(partition.name, install_plan_.source_slot, &source_path);
206 }
207
208 uint16_t mount_count = 0;
209
210 if (!source_path.empty()) {
211 brillo::Blob chunk;
212
213 utils::ReadFileChunk(source_path, 0x400 + 0x34, sizeof(uint16_t), &chunk);
214 mount_count = *reinterpret_cast<uint16_t*>(chunk.data());
215 }
216
217 LOG(INFO) << source_path << " has been mounted R/W " << mount_count << " times.";
218
219 if (mount_count > 0) {
Christian Hoffmann76d57b92022-01-23 12:04:42 +0100220 if (!utils::SetBlockDeviceReadOnly(mountable_device, false)) {
221 LOG(ERROR) << "Error marking the device " << mountable_device << " writeable.";
222 return false;
223 }
Dan Pasanena747c632017-05-10 16:29:35 -0500224 // Mount the target partition R/W
225 LOG(INFO) << "Running backuptool scripts";
226 utils::MountFilesystem(mountable_device, fs_mount_dir_, MS_NOATIME | MS_NODEV | MS_NODIRATIME,
227 partition.filesystem_type, "seclabel");
228
Luca Stefanid1ddfee2019-01-03 21:20:42 +0100229 // Switch to a permissive domain
230 if (setexeccon("u:r:backuptool:s0")) {
231 LOG(ERROR) << "Failed to set backuptool context";
232 return false;
233 }
234
Dan Pasanena747c632017-05-10 16:29:35 -0500235 // Run backuptool script
236 int ret = system("/postinstall/system/bin/backuptool_postinstall.sh");
237 if (ret == -1 || WEXITSTATUS(ret) != 0) {
238 LOG(ERROR) << "Backuptool postinstall step failed. ret=" << ret;
239 }
Luca Stefanid1ddfee2019-01-03 21:20:42 +0100240
241 // Switch back to update_engine domain
242 if (setexeccon(nullptr)) {
243 LOG(ERROR) << "Failed to set update_engine context";
244 return false;
245 }
Dan Pasanena747c632017-05-10 16:29:35 -0500246 } else {
247 LOG(INFO) << "Skipping backuptool scripts";
248 }
249
250 utils::UnmountFilesystem(fs_mount_dir_);
micky387cf367002023-11-15 22:05:10 +0100251#endif // USE_WEEKLY_BUILD
Dan Pasanena747c632017-05-10 16:29:35 -0500252
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700253 // In Chromium OS, the postinstall step is allowed to write to the block
254 // device on the target image, so we don't mark it as read-only and should
255 // be read-write since we just wrote to it during the update.
256
257 // Mark the block device as read-only before mounting for post-install.
258 if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) {
259 return false;
260 }
261#endif // __ANDROID__
262
263 if (!utils::MountFilesystem(
264 mountable_device,
265 fs_mount_dir_,
266 MS_RDONLY,
267 partition.filesystem_type,
268 hardware_->GetPartitionMountOptions(partition.name))) {
269 return false;
270 }
271 return true;
272}
273
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700274void PostinstallRunnerAction::PerformPartitionPostinstall() {
Alex Deymo390efed2016-02-18 11:00:40 -0800275 if (install_plan_.download_url.empty()) {
Kelvin Zhang24287c32023-03-09 10:13:26 -0800276 LOG(INFO) << "Skipping post-install";
Alex Deymo390efed2016-02-18 11:00:40 -0800277 return CompletePostinstall(ErrorCode::kSuccess);
278 }
279
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700280 // Skip all the partitions that don't have a post-install step.
281 while (current_partition_ < install_plan_.partitions.size() &&
282 !install_plan_.partitions[current_partition_].run_postinstall) {
283 VLOG(1) << "Skipping post-install on partition "
284 << install_plan_.partitions[current_partition_].name;
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700285 // Attempt to mount a device if it has postinstall script configured, even
286 // if we want to skip running postinstall script.
287 // This is because we've seen bugs like b/198787355 which is only triggered
288 // when you attempt to mount a device. If device fails to mount, it will
289 // likely fail to mount during boot anyway, so it's better to catch any
290 // issues earlier.
291 // It's possible that some of the partitions aren't mountable, but these
292 // partitions shouldn't have postinstall configured. Therefore we guard this
293 // logic with |postinstall_path.empty()|.
294 const auto& partition = install_plan_.partitions[current_partition_];
295 if (!partition.postinstall_path.empty()) {
296 const auto mountable_device = partition.readonly_target_path;
297 if (!MountPartition(partition)) {
298 return CompletePostinstall(ErrorCode::kPostInstallMountError);
299 }
Kelvin Zhangddc25802021-12-30 13:05:27 -0800300 LogBuildInfoForPartition(fs_mount_dir_);
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700301 if (!utils::UnmountFilesystem(fs_mount_dir_)) {
302 return CompletePartitionPostinstall(
303 1, "Error unmounting the device " + mountable_device);
304 }
305 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700306 current_partition_++;
307 }
308 if (current_partition_ == install_plan_.partitions.size())
309 return CompletePostinstall(ErrorCode::kSuccess);
310
311 const InstallPlan::Partition& partition =
312 install_plan_.partitions[current_partition_];
313
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400314 const string mountable_device = partition.readonly_target_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700315 // Perform post-install for the current_partition_ partition. At this point we
316 // need to call CompletePartitionPostinstall to complete the operation and
317 // cleanup.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700318
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700319 if (!MountPartition(partition)) {
320 CompletePostinstall(ErrorCode::kPostInstallMountError);
321 return;
Kelvin Zhang4ce01102020-11-16 09:32:08 -0500322 }
Kelvin Zhangddc25802021-12-30 13:05:27 -0800323 LogBuildInfoForPartition(fs_mount_dir_);
Alex Deymocbc22742016-03-04 17:53:02 -0800324 base::FilePath postinstall_path(partition.postinstall_path);
325 if (postinstall_path.IsAbsolute()) {
326 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
327 "path instead: "
328 << partition.postinstall_path;
329 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
330 }
331
332 string abs_path =
333 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800334 if (!base::StartsWith(
335 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
336 LOG(ERROR) << "Invalid relative postinstall path: "
337 << partition.postinstall_path;
338 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
339 }
340
Alex Deymo390efed2016-02-18 11:00:40 -0800341 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400342 << abs_path << ") installed on mountable device "
343 << mountable_device;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700344
Alex Deymo032e7722014-03-25 17:53:56 -0700345 // Logs the file format of the postinstall script we are about to run. This
346 // will help debug when the postinstall script doesn't match the architecture
347 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800348 LOG(INFO) << "Format file for new " << partition.postinstall_path
349 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700350
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800351 // Runs the postinstall script asynchronously to free up the main loop while
352 // it's running.
Alex Deymo0d298542016-03-30 18:31:49 -0700353 vector<string> command = {abs_path};
354#ifdef __ANDROID__
355 // In Brillo and Android, we pass the slot number and status fd.
356 command.push_back(std::to_string(install_plan_.target_slot));
357 command.push_back(std::to_string(kPostinstallStatusFd));
358#else
359 // Chrome OS postinstall expects the target rootfs as the first parameter.
360 command.push_back(partition.target_path);
361#endif // __ANDROID__
362
363 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymod15c5462016-03-09 18:11:12 -0800364 command,
Alex Deymo0d298542016-03-30 18:31:49 -0700365 Subprocess::kRedirectStderrToStdout,
366 {kPostinstallStatusFd},
Alex Deymod15c5462016-03-09 18:11:12 -0800367 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
368 base::Unretained(this)));
369 // Subprocess::Exec should never return a negative process id.
370 CHECK_GE(current_command_, 0);
371
Alex Deymo0d298542016-03-30 18:31:49 -0700372 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700373 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo0d298542016-03-30 18:31:49 -0700374 return;
375 }
376
377 // Monitor the status file descriptor.
378 progress_fd_ =
379 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
380 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
381 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
382 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
383 }
384
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900385 progress_controller_ = base::FileDescriptorWatcher::WatchReadable(
Alex Deymo0d298542016-03-30 18:31:49 -0700386 progress_fd_,
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900387 base::BindRepeating(&PostinstallRunnerAction::OnProgressFdReady,
388 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800389}
390
Alex Deymo0d298542016-03-30 18:31:49 -0700391void PostinstallRunnerAction::OnProgressFdReady() {
392 char buf[1024];
393 size_t bytes_read;
394 do {
395 bytes_read = 0;
396 bool eof;
397 bool ok =
hscham00b6aa22020-02-20 12:32:06 +0900398 utils::ReadAll(progress_fd_, buf, base::size(buf), &bytes_read, &eof);
Alex Deymo0d298542016-03-30 18:31:49 -0700399 progress_buffer_.append(buf, bytes_read);
400 // Process every line.
401 vector<string> lines = base::SplitString(
402 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
403 if (!lines.empty()) {
404 progress_buffer_ = lines.back();
405 lines.pop_back();
406 for (const auto& line : lines) {
407 ProcessProgressLine(line);
408 }
409 }
410 if (!ok || eof) {
411 // There was either an error or an EOF condition, so we are done watching
412 // the file descriptor.
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900413 progress_controller_.reset();
Alex Deymo0d298542016-03-30 18:31:49 -0700414 return;
415 }
416 } while (bytes_read);
417}
418
419bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
420 double frac = 0;
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700421 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 &&
422 !std::isnan(frac)) {
Alex Deymo0d298542016-03-30 18:31:49 -0700423 ReportProgress(frac);
424 return true;
425 }
426
427 return false;
428}
429
430void PostinstallRunnerAction::ReportProgress(double frac) {
431 if (!delegate_)
432 return;
Yoshitaka Ishida128936f2018-02-16 18:20:07 +0900433 if (current_partition_ >= partition_weight_.size() || total_weight_ == 0) {
Alex Deymo0d298542016-03-30 18:31:49 -0700434 delegate_->ProgressUpdate(1.);
435 return;
436 }
Alex Deymo44b35672016-04-05 17:57:48 -0700437 if (!std::isfinite(frac) || frac < 0)
Alex Deymo0d298542016-03-30 18:31:49 -0700438 frac = 0;
439 if (frac > 1)
440 frac = 1;
441 double postinst_action_progress =
442 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
443 total_weight_;
444 delegate_->ProgressUpdate(postinst_action_progress);
445}
446
447void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800448 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800449#ifndef __ANDROID__
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500450#if BASE_VER < 800000
451 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), true)) {
452#else
hscham043355b2020-11-17 16:50:10 +0900453 if (!base::DeleteFile(base::FilePath(fs_mount_dir_))) {
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500454#endif
Alex Deymo390efed2016-02-18 11:00:40 -0800455 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700456 }
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500457#endif
Alex Deymo0d298542016-03-30 18:31:49 -0700458
459 progress_fd_ = -1;
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900460 progress_controller_.reset();
Tianjie55abd3c2020-06-19 00:22:59 -0700461
Alex Deymo0d298542016-03-30 18:31:49 -0700462 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800463}
464
465void PostinstallRunnerAction::CompletePartitionPostinstall(
466 int return_code, const string& output) {
467 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700468 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700469
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800470 if (return_code != 0) {
471 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700472 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700473
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700474 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700475 // This special return code means that we tried to update firmware,
476 // but couldn't because we booted from FW B, and we need to reboot
477 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700478 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700479 }
Don Garrett81018e02013-07-30 18:46:31 -0700480
481 if (return_code == 4) {
482 // This special return code means that we tried to update firmware,
483 // but couldn't because we booted from FW B, and we need to reboot
484 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700485 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700486 }
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700487
488 // If postinstall script for this partition is optional we can ignore the
489 // result.
490 if (install_plan_.partitions[current_partition_].postinstall_optional) {
491 LOG(INFO) << "Ignoring postinstall failure since it is optional";
492 } else {
493 return CompletePostinstall(error_code);
494 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700495 }
Alex Deymo0d298542016-03-30 18:31:49 -0700496 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700497 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700498 ReportProgress(0);
499
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700500 PerformPartitionPostinstall();
501}
502
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000503PostinstallRunnerAction::~PostinstallRunnerAction() {
504 if (!install_plan_.partitions.empty()) {
505 auto dynamic_control = boot_control_->GetDynamicPartitionControl();
506 CHECK(dynamic_control);
507 dynamic_control->UnmapAllPartitions();
508 LOG(INFO) << "Unmapped all partitions.";
509 }
510}
511
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700512void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
513 // We only attempt to mark the new slot as active if all the postinstall
514 // steps succeeded.
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000515 DEFER {
516 if (error_code != ErrorCode::kSuccess &&
517 error_code != ErrorCode::kUpdatedButNotActive) {
Kelvin Zhang99cbbe72024-01-18 14:50:01 -0800518 LOG(ERROR) << "Postinstall action failed. "
519 << utils::ErrorCodeToString(error_code);
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000520
521 // Undo any changes done to trigger Powerwash.
522 if (powerwash_scheduled_)
523 hardware_->CancelPowerwash();
524 }
525 processor_->ActionComplete(this, error_code);
526 };
Sen Jiang02c49422017-10-31 15:14:11 -0700527 if (error_code == ErrorCode::kSuccess) {
528 if (install_plan_.switch_slot_on_reboot) {
Kelvin Zhang99cbbe72024-01-18 14:50:01 -0800529 if constexpr (!constants::kIsRecovery) {
530 if (!boot_control_->GetDynamicPartitionControl()->MapAllPartitions()) {
531 LOG(WARNING)
532 << "Failed to map all partitions before marking snapshot as "
533 "ready for slot switch. Subsequent FinishUpdate() call may or "
534 "may not work";
535 }
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000536 }
537 if (!boot_control_->GetDynamicPartitionControl()->FinishUpdate(
538 install_plan_.powerwash_required) ||
539 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Sen Jiang02c49422017-10-31 15:14:11 -0700540 error_code = ErrorCode::kPostinstallRunnerError;
Tianjie Xud6aa91f2019-11-14 11:55:10 -0800541 } else {
542 // Schedules warm reset on next reboot, ignores the error.
543 hardware_->SetWarmReset(true);
Tianjie838793d2021-01-14 22:05:13 -0800544 // Sets the vbmeta digest for the other slot to boot into.
545 hardware_->SetVbmetaDigestForInactiveSlot(false);
Sen Jiang02c49422017-10-31 15:14:11 -0700546 }
547 } else {
548 error_code = ErrorCode::kUpdatedButNotActive;
549 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700550 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700551
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700552 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700553 if (HasOutputPipe()) {
554 SetOutputObject(install_plan_);
555 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000556}
557
Alex Deymod15c5462016-03-09 18:11:12 -0800558void PostinstallRunnerAction::SuspendAction() {
559 if (!current_command_)
560 return;
561 if (kill(current_command_, SIGSTOP) != 0) {
562 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800563 } else {
564 is_current_command_suspended_ = true;
Alex Deymod15c5462016-03-09 18:11:12 -0800565 }
566}
567
568void PostinstallRunnerAction::ResumeAction() {
569 if (!current_command_)
570 return;
571 if (kill(current_command_, SIGCONT) != 0) {
572 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800573 } else {
574 is_current_command_suspended_ = false;
Alex Deymod15c5462016-03-09 18:11:12 -0800575 }
576}
577
578void PostinstallRunnerAction::TerminateProcessing() {
579 if (!current_command_)
580 return;
581 // Calling KillExec() will discard the callback we registered and therefore
582 // the unretained reference to this object.
583 Subprocess::Get().KillExec(current_command_);
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800584
585 // If the command has been suspended, resume it after KillExec() so that the
586 // process can process the SIGTERM sent by KillExec().
587 if (is_current_command_suspended_) {
588 ResumeAction();
589 }
590
Alex Deymod15c5462016-03-09 18:11:12 -0800591 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700592 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800593}
594
adlr@google.com3defe6a2009-12-04 20:57:17 +0000595} // namespace chromeos_update_engine