blob: 6e10cea25efcb73e0d35a6119642320d9d2e0fae [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 Zhang8b1e0dc2020-10-26 12:27:53 -0400116 // Before calling MapAllPartitions to map snapshot devices, all CowWriters
117 // must be closed, and MapAllPartitions() should be called.
Kelvin Zhang263a5402022-12-08 23:03:32 -0800118 if (!install_plan_.partitions.empty()) {
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.
Marton Hunyady199152d2018-05-07 19:08:48 +0200129 if (install_plan_.powerwash_required || install_plan_.is_rollback) {
Miriam Polzeraff72002020-08-27 08:20:39 +0200130 if (hardware_->SchedulePowerwash(
131 install_plan_.rollback_data_save_requested)) {
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__
Dan Pasanena747c632017-05-10 16:29:35 -0500183 // Check the currently installed /system partition to see if it's ever
184 // been mounted R/W. If it has, we'll run backuptool scripts for it
185 // since we can safely assume something on the partition has been
186 // changed and we won't be breaking verity (since it's already been
187 // broken). If it hasn't ever been mounted R/W, we can assume that
188 // the rom that the user is upgrading to will have everything they
189 // need and no addon.d scripts will need to be run to retain stuff
190 // after the upgrade.
191 //
192 // Use the following disk layout info to make the determination
193 // https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout
194 // Super block starts from block 0, offset 0x400
195 // 0x2C: len32 Mount time
196 // 0x30: len32 Write time
197 // 0x34: len16 Number of mounts since the last fsck
198 // 0x38: len16 Magic signature 0xEF53
199
200 string source_path;
201
202 if (install_plan_.source_slot != BootControlInterface::kInvalidSlot) {
203 boot_control_->GetPartitionDevice(partition.name, install_plan_.source_slot, &source_path);
204 }
205
206 uint16_t mount_count = 0;
207
208 if (!source_path.empty()) {
209 brillo::Blob chunk;
210
211 utils::ReadFileChunk(source_path, 0x400 + 0x34, sizeof(uint16_t), &chunk);
212 mount_count = *reinterpret_cast<uint16_t*>(chunk.data());
213 }
214
215 LOG(INFO) << source_path << " has been mounted R/W " << mount_count << " times.";
216
217 if (mount_count > 0) {
Christian Hoffmann76d57b92022-01-23 12:04:42 +0100218 if (!utils::SetBlockDeviceReadOnly(mountable_device, false)) {
219 LOG(ERROR) << "Error marking the device " << mountable_device << " writeable.";
220 return false;
221 }
Dan Pasanena747c632017-05-10 16:29:35 -0500222 // Mount the target partition R/W
223 LOG(INFO) << "Running backuptool scripts";
224 utils::MountFilesystem(mountable_device, fs_mount_dir_, MS_NOATIME | MS_NODEV | MS_NODIRATIME,
225 partition.filesystem_type, "seclabel");
226
227 // Run backuptool script
228 int ret = system("/postinstall/system/bin/backuptool_postinstall.sh");
229 if (ret == -1 || WEXITSTATUS(ret) != 0) {
230 LOG(ERROR) << "Backuptool postinstall step failed. ret=" << ret;
231 }
232 } else {
233 LOG(INFO) << "Skipping backuptool scripts";
234 }
235
236 utils::UnmountFilesystem(fs_mount_dir_);
237
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700238 // In Chromium OS, the postinstall step is allowed to write to the block
239 // device on the target image, so we don't mark it as read-only and should
240 // be read-write since we just wrote to it during the update.
241
242 // Mark the block device as read-only before mounting for post-install.
243 if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) {
244 return false;
245 }
246#endif // __ANDROID__
247
248 if (!utils::MountFilesystem(
249 mountable_device,
250 fs_mount_dir_,
251 MS_RDONLY,
252 partition.filesystem_type,
253 hardware_->GetPartitionMountOptions(partition.name))) {
254 return false;
255 }
256 return true;
257}
258
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700259void PostinstallRunnerAction::PerformPartitionPostinstall() {
Alex Deymo390efed2016-02-18 11:00:40 -0800260 if (install_plan_.download_url.empty()) {
Kelvin Zhang24287c32023-03-09 10:13:26 -0800261 LOG(INFO) << "Skipping post-install";
Alex Deymo390efed2016-02-18 11:00:40 -0800262 return CompletePostinstall(ErrorCode::kSuccess);
263 }
264
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700265 // Skip all the partitions that don't have a post-install step.
266 while (current_partition_ < install_plan_.partitions.size() &&
267 !install_plan_.partitions[current_partition_].run_postinstall) {
268 VLOG(1) << "Skipping post-install on partition "
269 << install_plan_.partitions[current_partition_].name;
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700270 // Attempt to mount a device if it has postinstall script configured, even
271 // if we want to skip running postinstall script.
272 // This is because we've seen bugs like b/198787355 which is only triggered
273 // when you attempt to mount a device. If device fails to mount, it will
274 // likely fail to mount during boot anyway, so it's better to catch any
275 // issues earlier.
276 // It's possible that some of the partitions aren't mountable, but these
277 // partitions shouldn't have postinstall configured. Therefore we guard this
278 // logic with |postinstall_path.empty()|.
279 const auto& partition = install_plan_.partitions[current_partition_];
280 if (!partition.postinstall_path.empty()) {
281 const auto mountable_device = partition.readonly_target_path;
282 if (!MountPartition(partition)) {
283 return CompletePostinstall(ErrorCode::kPostInstallMountError);
284 }
Kelvin Zhangddc25802021-12-30 13:05:27 -0800285 LogBuildInfoForPartition(fs_mount_dir_);
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700286 if (!utils::UnmountFilesystem(fs_mount_dir_)) {
287 return CompletePartitionPostinstall(
288 1, "Error unmounting the device " + mountable_device);
289 }
290 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700291 current_partition_++;
292 }
293 if (current_partition_ == install_plan_.partitions.size())
294 return CompletePostinstall(ErrorCode::kSuccess);
295
296 const InstallPlan::Partition& partition =
297 install_plan_.partitions[current_partition_];
298
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400299 const string mountable_device = partition.readonly_target_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700300 // Perform post-install for the current_partition_ partition. At this point we
301 // need to call CompletePartitionPostinstall to complete the operation and
302 // cleanup.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700303
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700304 if (!MountPartition(partition)) {
305 CompletePostinstall(ErrorCode::kPostInstallMountError);
306 return;
Kelvin Zhang4ce01102020-11-16 09:32:08 -0500307 }
Kelvin Zhangddc25802021-12-30 13:05:27 -0800308 LogBuildInfoForPartition(fs_mount_dir_);
Alex Deymocbc22742016-03-04 17:53:02 -0800309 base::FilePath postinstall_path(partition.postinstall_path);
310 if (postinstall_path.IsAbsolute()) {
311 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
312 "path instead: "
313 << partition.postinstall_path;
314 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
315 }
316
317 string abs_path =
318 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800319 if (!base::StartsWith(
320 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
321 LOG(ERROR) << "Invalid relative postinstall path: "
322 << partition.postinstall_path;
323 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
324 }
325
Alex Deymo390efed2016-02-18 11:00:40 -0800326 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400327 << abs_path << ") installed on mountable device "
328 << mountable_device;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700329
Alex Deymo032e7722014-03-25 17:53:56 -0700330 // Logs the file format of the postinstall script we are about to run. This
331 // will help debug when the postinstall script doesn't match the architecture
332 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800333 LOG(INFO) << "Format file for new " << partition.postinstall_path
334 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700335
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800336 // Runs the postinstall script asynchronously to free up the main loop while
337 // it's running.
Alex Deymo0d298542016-03-30 18:31:49 -0700338 vector<string> command = {abs_path};
339#ifdef __ANDROID__
340 // In Brillo and Android, we pass the slot number and status fd.
341 command.push_back(std::to_string(install_plan_.target_slot));
342 command.push_back(std::to_string(kPostinstallStatusFd));
343#else
344 // Chrome OS postinstall expects the target rootfs as the first parameter.
345 command.push_back(partition.target_path);
346#endif // __ANDROID__
347
348 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymod15c5462016-03-09 18:11:12 -0800349 command,
Alex Deymo0d298542016-03-30 18:31:49 -0700350 Subprocess::kRedirectStderrToStdout,
351 {kPostinstallStatusFd},
Alex Deymod15c5462016-03-09 18:11:12 -0800352 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
353 base::Unretained(this)));
354 // Subprocess::Exec should never return a negative process id.
355 CHECK_GE(current_command_, 0);
356
Alex Deymo0d298542016-03-30 18:31:49 -0700357 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700358 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo0d298542016-03-30 18:31:49 -0700359 return;
360 }
361
362 // Monitor the status file descriptor.
363 progress_fd_ =
364 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
365 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
366 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
367 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
368 }
369
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900370 progress_controller_ = base::FileDescriptorWatcher::WatchReadable(
Alex Deymo0d298542016-03-30 18:31:49 -0700371 progress_fd_,
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900372 base::BindRepeating(&PostinstallRunnerAction::OnProgressFdReady,
373 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800374}
375
Alex Deymo0d298542016-03-30 18:31:49 -0700376void PostinstallRunnerAction::OnProgressFdReady() {
377 char buf[1024];
378 size_t bytes_read;
379 do {
380 bytes_read = 0;
381 bool eof;
382 bool ok =
hscham00b6aa22020-02-20 12:32:06 +0900383 utils::ReadAll(progress_fd_, buf, base::size(buf), &bytes_read, &eof);
Alex Deymo0d298542016-03-30 18:31:49 -0700384 progress_buffer_.append(buf, bytes_read);
385 // Process every line.
386 vector<string> lines = base::SplitString(
387 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
388 if (!lines.empty()) {
389 progress_buffer_ = lines.back();
390 lines.pop_back();
391 for (const auto& line : lines) {
392 ProcessProgressLine(line);
393 }
394 }
395 if (!ok || eof) {
396 // There was either an error or an EOF condition, so we are done watching
397 // the file descriptor.
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900398 progress_controller_.reset();
Alex Deymo0d298542016-03-30 18:31:49 -0700399 return;
400 }
401 } while (bytes_read);
402}
403
404bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
405 double frac = 0;
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700406 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 &&
407 !std::isnan(frac)) {
Alex Deymo0d298542016-03-30 18:31:49 -0700408 ReportProgress(frac);
409 return true;
410 }
411
412 return false;
413}
414
415void PostinstallRunnerAction::ReportProgress(double frac) {
416 if (!delegate_)
417 return;
Yoshitaka Ishida128936f2018-02-16 18:20:07 +0900418 if (current_partition_ >= partition_weight_.size() || total_weight_ == 0) {
Alex Deymo0d298542016-03-30 18:31:49 -0700419 delegate_->ProgressUpdate(1.);
420 return;
421 }
Alex Deymo44b35672016-04-05 17:57:48 -0700422 if (!std::isfinite(frac) || frac < 0)
Alex Deymo0d298542016-03-30 18:31:49 -0700423 frac = 0;
424 if (frac > 1)
425 frac = 1;
426 double postinst_action_progress =
427 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
428 total_weight_;
429 delegate_->ProgressUpdate(postinst_action_progress);
430}
431
432void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800433 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800434#ifndef __ANDROID__
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500435#if BASE_VER < 800000
436 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), true)) {
437#else
hscham043355b2020-11-17 16:50:10 +0900438 if (!base::DeleteFile(base::FilePath(fs_mount_dir_))) {
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500439#endif
Alex Deymo390efed2016-02-18 11:00:40 -0800440 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700441 }
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500442#endif
Alex Deymo0d298542016-03-30 18:31:49 -0700443
444 progress_fd_ = -1;
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900445 progress_controller_.reset();
Tianjie55abd3c2020-06-19 00:22:59 -0700446
Alex Deymo0d298542016-03-30 18:31:49 -0700447 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800448}
449
450void PostinstallRunnerAction::CompletePartitionPostinstall(
451 int return_code, const string& output) {
452 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700453 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700454
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800455 if (return_code != 0) {
456 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700457 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700458
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700459 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700460 // This special return code means that we tried to update firmware,
461 // but couldn't because we booted from FW B, and we need to reboot
462 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700463 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700464 }
Don Garrett81018e02013-07-30 18:46:31 -0700465
466 if (return_code == 4) {
467 // This special return code means that we tried to update firmware,
468 // but couldn't because we booted from FW B, and we need to reboot
469 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700470 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700471 }
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700472
473 // If postinstall script for this partition is optional we can ignore the
474 // result.
475 if (install_plan_.partitions[current_partition_].postinstall_optional) {
476 LOG(INFO) << "Ignoring postinstall failure since it is optional";
477 } else {
478 return CompletePostinstall(error_code);
479 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700480 }
Alex Deymo0d298542016-03-30 18:31:49 -0700481 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700482 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700483 ReportProgress(0);
484
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700485 PerformPartitionPostinstall();
486}
487
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000488PostinstallRunnerAction::~PostinstallRunnerAction() {
489 if (!install_plan_.partitions.empty()) {
490 auto dynamic_control = boot_control_->GetDynamicPartitionControl();
491 CHECK(dynamic_control);
492 dynamic_control->UnmapAllPartitions();
493 LOG(INFO) << "Unmapped all partitions.";
494 }
495}
496
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700497void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
498 // We only attempt to mark the new slot as active if all the postinstall
499 // steps succeeded.
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000500 DEFER {
501 if (error_code != ErrorCode::kSuccess &&
502 error_code != ErrorCode::kUpdatedButNotActive) {
Kelvin Zhang99cbbe72024-01-18 14:50:01 -0800503 LOG(ERROR) << "Postinstall action failed. "
504 << utils::ErrorCodeToString(error_code);
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000505
506 // Undo any changes done to trigger Powerwash.
507 if (powerwash_scheduled_)
508 hardware_->CancelPowerwash();
509 }
510 processor_->ActionComplete(this, error_code);
511 };
Sen Jiang02c49422017-10-31 15:14:11 -0700512 if (error_code == ErrorCode::kSuccess) {
513 if (install_plan_.switch_slot_on_reboot) {
Kelvin Zhang99cbbe72024-01-18 14:50:01 -0800514 if constexpr (!constants::kIsRecovery) {
515 if (!boot_control_->GetDynamicPartitionControl()->MapAllPartitions()) {
516 LOG(WARNING)
517 << "Failed to map all partitions before marking snapshot as "
518 "ready for slot switch. Subsequent FinishUpdate() call may or "
519 "may not work";
520 }
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000521 }
522 if (!boot_control_->GetDynamicPartitionControl()->FinishUpdate(
523 install_plan_.powerwash_required) ||
524 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Sen Jiang02c49422017-10-31 15:14:11 -0700525 error_code = ErrorCode::kPostinstallRunnerError;
Tianjie Xud6aa91f2019-11-14 11:55:10 -0800526 } else {
527 // Schedules warm reset on next reboot, ignores the error.
528 hardware_->SetWarmReset(true);
Tianjie838793d2021-01-14 22:05:13 -0800529 // Sets the vbmeta digest for the other slot to boot into.
530 hardware_->SetVbmetaDigestForInactiveSlot(false);
Sen Jiang02c49422017-10-31 15:14:11 -0700531 }
532 } else {
533 error_code = ErrorCode::kUpdatedButNotActive;
534 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700535 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700536
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700537 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700538 if (HasOutputPipe()) {
539 SetOutputObject(install_plan_);
540 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000541}
542
Alex Deymod15c5462016-03-09 18:11:12 -0800543void PostinstallRunnerAction::SuspendAction() {
544 if (!current_command_)
545 return;
546 if (kill(current_command_, SIGSTOP) != 0) {
547 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800548 } else {
549 is_current_command_suspended_ = true;
Alex Deymod15c5462016-03-09 18:11:12 -0800550 }
551}
552
553void PostinstallRunnerAction::ResumeAction() {
554 if (!current_command_)
555 return;
556 if (kill(current_command_, SIGCONT) != 0) {
557 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800558 } else {
559 is_current_command_suspended_ = false;
Alex Deymod15c5462016-03-09 18:11:12 -0800560 }
561}
562
563void PostinstallRunnerAction::TerminateProcessing() {
564 if (!current_command_)
565 return;
566 // Calling KillExec() will discard the callback we registered and therefore
567 // the unretained reference to this object.
568 Subprocess::Get().KillExec(current_command_);
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800569
570 // If the command has been suspended, resume it after KillExec() so that the
571 // process can process the SIGTERM sent by KillExec().
572 if (is_current_command_suspended_) {
573 ResumeAction();
574 }
575
Alex Deymod15c5462016-03-09 18:11:12 -0800576 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700577 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800578}
579
adlr@google.com3defe6a2009-12-04 20:57:17 +0000580} // namespace chromeos_update_engine