blob: e489dfcd75f6252aec1288aaa63bba5a25284bd4 [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"
Alex Deymo14dbd332016-03-01 18:55:54 -080039#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());
94 LOG(INFO) << "postinstall mount point: " << fs_mount_dir_;
Kelvin Zhange9def4e2020-12-02 14:04:09 -050095}
96
adlr@google.com3defe6a2009-12-04 20:57:17 +000097void PostinstallRunnerAction::PerformAction() {
98 CHECK(HasInputObject());
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -040099 CHECK(boot_control_);
Chris Sosad317e402013-06-12 13:47:09 -0700100 install_plan_ = GetInputObject();
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800101
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400102 auto dynamic_control = boot_control_->GetDynamicPartitionControl();
103 CHECK(dynamic_control);
104
105 // Mount snapshot partitions for Virtual AB Compression Compression.
Kelvin Zhang06188352021-02-10 13:21:47 -0500106 if (dynamic_control->UpdateUsesSnapshotCompression()) {
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400107 // Before calling MapAllPartitions to map snapshot devices, all CowWriters
108 // must be closed, and MapAllPartitions() should be called.
109 dynamic_control->UnmapAllPartitions();
110 if (!dynamic_control->MapAllPartitions()) {
111 return CompletePostinstall(ErrorCode::kPostInstallMountError);
112 }
113 }
114
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800115 // We always powerwash when rolling back, however policy can determine
116 // if this is a full/normal powerwash, or a special rollback powerwash
117 // that retains a small amount of system state such as enrollment and
118 // network configuration. In both cases all user accounts are deleted.
Marton Hunyady199152d2018-05-07 19:08:48 +0200119 if (install_plan_.powerwash_required || install_plan_.is_rollback) {
Miriam Polzeraff72002020-08-27 08:20:39 +0200120 if (hardware_->SchedulePowerwash(
121 install_plan_.rollback_data_save_requested)) {
Alex Deymofb905d92016-06-03 19:26:58 -0700122 powerwash_scheduled_ = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700123 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700124 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700125 }
126 }
127
Alex Deymo0d298542016-03-30 18:31:49 -0700128 // Initialize all the partition weights.
129 partition_weight_.resize(install_plan_.partitions.size());
130 total_weight_ = 0;
131 for (size_t i = 0; i < install_plan_.partitions.size(); ++i) {
Tianjie Xu087de9d2019-11-01 17:11:22 -0700132 auto& partition = install_plan_.partitions[i];
133 if (!install_plan_.run_post_install && partition.postinstall_optional) {
134 partition.run_postinstall = false;
135 LOG(INFO) << "Skipping optional post-install for partition "
136 << partition.name << " according to install plan.";
137 }
138
Alex Deymo0d298542016-03-30 18:31:49 -0700139 // TODO(deymo): This code sets the weight to all the postinstall commands,
140 // but we could remember how long they took in the past and use those
141 // values.
Tianjie Xu087de9d2019-11-01 17:11:22 -0700142 partition_weight_[i] = partition.run_postinstall;
Alex Deymo0d298542016-03-30 18:31:49 -0700143 total_weight_ += partition_weight_[i];
144 }
145 accumulated_weight_ = 0;
146 ReportProgress(0);
147
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700148 PerformPartitionPostinstall();
149}
150
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700151bool PostinstallRunnerAction::MountPartition(
152 const InstallPlan::Partition& partition) noexcept {
153 // Perform post-install for the current_partition_ partition. At this point we
154 // need to call CompletePartitionPostinstall to complete the operation and
155 // cleanup.
156 const auto mountable_device = partition.readonly_target_path;
157 if (!utils::FileExists(mountable_device.c_str())) {
158 LOG(ERROR) << "Mountable device " << mountable_device << " for partition "
159 << partition.name << " does not exist";
160 return false;
161 }
162
163 if (!utils::FileExists(fs_mount_dir_.c_str())) {
164 LOG(ERROR) << "Mount point " << fs_mount_dir_
165 << " does not exist, mount call will fail";
166 return false;
167 }
168 // Double check that the fs_mount_dir is not busy with a previous mounted
169 // filesystem from a previous crashed postinstall step.
170 if (utils::IsMountpoint(fs_mount_dir_)) {
171 LOG(INFO) << "Found previously mounted filesystem at " << fs_mount_dir_;
172 utils::UnmountFilesystem(fs_mount_dir_);
173 }
174
175#ifdef __ANDROID__
176 // In Chromium OS, the postinstall step is allowed to write to the block
177 // device on the target image, so we don't mark it as read-only and should
178 // be read-write since we just wrote to it during the update.
179
180 // Mark the block device as read-only before mounting for post-install.
181 if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) {
182 return false;
183 }
184#endif // __ANDROID__
185
186 if (!utils::MountFilesystem(
187 mountable_device,
188 fs_mount_dir_,
189 MS_RDONLY,
190 partition.filesystem_type,
191 hardware_->GetPartitionMountOptions(partition.name))) {
192 return false;
193 }
194 return true;
195}
196
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700197void PostinstallRunnerAction::PerformPartitionPostinstall() {
Alex Deymo390efed2016-02-18 11:00:40 -0800198 if (install_plan_.download_url.empty()) {
199 LOG(INFO) << "Skipping post-install during rollback";
200 return CompletePostinstall(ErrorCode::kSuccess);
201 }
202
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700203 // Skip all the partitions that don't have a post-install step.
204 while (current_partition_ < install_plan_.partitions.size() &&
205 !install_plan_.partitions[current_partition_].run_postinstall) {
206 VLOG(1) << "Skipping post-install on partition "
207 << install_plan_.partitions[current_partition_].name;
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700208 // Attempt to mount a device if it has postinstall script configured, even
209 // if we want to skip running postinstall script.
210 // This is because we've seen bugs like b/198787355 which is only triggered
211 // when you attempt to mount a device. If device fails to mount, it will
212 // likely fail to mount during boot anyway, so it's better to catch any
213 // issues earlier.
214 // It's possible that some of the partitions aren't mountable, but these
215 // partitions shouldn't have postinstall configured. Therefore we guard this
216 // logic with |postinstall_path.empty()|.
217 const auto& partition = install_plan_.partitions[current_partition_];
218 if (!partition.postinstall_path.empty()) {
219 const auto mountable_device = partition.readonly_target_path;
220 if (!MountPartition(partition)) {
221 return CompletePostinstall(ErrorCode::kPostInstallMountError);
222 }
Kelvin Zhangddc25802021-12-30 13:05:27 -0800223 LogBuildInfoForPartition(fs_mount_dir_);
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700224 if (!utils::UnmountFilesystem(fs_mount_dir_)) {
225 return CompletePartitionPostinstall(
226 1, "Error unmounting the device " + mountable_device);
227 }
228 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700229 current_partition_++;
230 }
231 if (current_partition_ == install_plan_.partitions.size())
232 return CompletePostinstall(ErrorCode::kSuccess);
233
234 const InstallPlan::Partition& partition =
235 install_plan_.partitions[current_partition_];
236
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400237 const string mountable_device = partition.readonly_target_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700238 // Perform post-install for the current_partition_ partition. At this point we
239 // need to call CompletePartitionPostinstall to complete the operation and
240 // cleanup.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700241
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700242 if (!MountPartition(partition)) {
243 CompletePostinstall(ErrorCode::kPostInstallMountError);
244 return;
Kelvin Zhang4ce01102020-11-16 09:32:08 -0500245 }
Kelvin Zhangddc25802021-12-30 13:05:27 -0800246 LogBuildInfoForPartition(fs_mount_dir_);
Alex Deymocbc22742016-03-04 17:53:02 -0800247 base::FilePath postinstall_path(partition.postinstall_path);
248 if (postinstall_path.IsAbsolute()) {
249 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
250 "path instead: "
251 << partition.postinstall_path;
252 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
253 }
254
255 string abs_path =
256 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800257 if (!base::StartsWith(
258 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
259 LOG(ERROR) << "Invalid relative postinstall path: "
260 << partition.postinstall_path;
261 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
262 }
263
Alex Deymo390efed2016-02-18 11:00:40 -0800264 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400265 << abs_path << ") installed on mountable device "
266 << mountable_device;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700267
Alex Deymo032e7722014-03-25 17:53:56 -0700268 // Logs the file format of the postinstall script we are about to run. This
269 // will help debug when the postinstall script doesn't match the architecture
270 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800271 LOG(INFO) << "Format file for new " << partition.postinstall_path
272 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700273
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800274 // Runs the postinstall script asynchronously to free up the main loop while
275 // it's running.
Alex Deymo0d298542016-03-30 18:31:49 -0700276 vector<string> command = {abs_path};
277#ifdef __ANDROID__
278 // In Brillo and Android, we pass the slot number and status fd.
279 command.push_back(std::to_string(install_plan_.target_slot));
280 command.push_back(std::to_string(kPostinstallStatusFd));
281#else
282 // Chrome OS postinstall expects the target rootfs as the first parameter.
283 command.push_back(partition.target_path);
284#endif // __ANDROID__
285
286 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymod15c5462016-03-09 18:11:12 -0800287 command,
Alex Deymo0d298542016-03-30 18:31:49 -0700288 Subprocess::kRedirectStderrToStdout,
289 {kPostinstallStatusFd},
Alex Deymod15c5462016-03-09 18:11:12 -0800290 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
291 base::Unretained(this)));
292 // Subprocess::Exec should never return a negative process id.
293 CHECK_GE(current_command_, 0);
294
Alex Deymo0d298542016-03-30 18:31:49 -0700295 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700296 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo0d298542016-03-30 18:31:49 -0700297 return;
298 }
299
300 // Monitor the status file descriptor.
301 progress_fd_ =
302 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
303 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
304 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
305 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
306 }
307
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900308 progress_controller_ = base::FileDescriptorWatcher::WatchReadable(
Alex Deymo0d298542016-03-30 18:31:49 -0700309 progress_fd_,
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900310 base::BindRepeating(&PostinstallRunnerAction::OnProgressFdReady,
311 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800312}
313
Alex Deymo0d298542016-03-30 18:31:49 -0700314void PostinstallRunnerAction::OnProgressFdReady() {
315 char buf[1024];
316 size_t bytes_read;
317 do {
318 bytes_read = 0;
319 bool eof;
320 bool ok =
hscham00b6aa22020-02-20 12:32:06 +0900321 utils::ReadAll(progress_fd_, buf, base::size(buf), &bytes_read, &eof);
Alex Deymo0d298542016-03-30 18:31:49 -0700322 progress_buffer_.append(buf, bytes_read);
323 // Process every line.
324 vector<string> lines = base::SplitString(
325 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
326 if (!lines.empty()) {
327 progress_buffer_ = lines.back();
328 lines.pop_back();
329 for (const auto& line : lines) {
330 ProcessProgressLine(line);
331 }
332 }
333 if (!ok || eof) {
334 // There was either an error or an EOF condition, so we are done watching
335 // the file descriptor.
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900336 progress_controller_.reset();
Alex Deymo0d298542016-03-30 18:31:49 -0700337 return;
338 }
339 } while (bytes_read);
340}
341
342bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
343 double frac = 0;
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700344 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 &&
345 !std::isnan(frac)) {
Alex Deymo0d298542016-03-30 18:31:49 -0700346 ReportProgress(frac);
347 return true;
348 }
349
350 return false;
351}
352
353void PostinstallRunnerAction::ReportProgress(double frac) {
354 if (!delegate_)
355 return;
Yoshitaka Ishida128936f2018-02-16 18:20:07 +0900356 if (current_partition_ >= partition_weight_.size() || total_weight_ == 0) {
Alex Deymo0d298542016-03-30 18:31:49 -0700357 delegate_->ProgressUpdate(1.);
358 return;
359 }
Alex Deymo44b35672016-04-05 17:57:48 -0700360 if (!std::isfinite(frac) || frac < 0)
Alex Deymo0d298542016-03-30 18:31:49 -0700361 frac = 0;
362 if (frac > 1)
363 frac = 1;
364 double postinst_action_progress =
365 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
366 total_weight_;
367 delegate_->ProgressUpdate(postinst_action_progress);
368}
369
370void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800371 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800372#ifndef __ANDROID__
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500373#if BASE_VER < 800000
374 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), true)) {
375#else
hscham043355b2020-11-17 16:50:10 +0900376 if (!base::DeleteFile(base::FilePath(fs_mount_dir_))) {
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500377#endif
Alex Deymo390efed2016-02-18 11:00:40 -0800378 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700379 }
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500380#endif
Alex Deymo0d298542016-03-30 18:31:49 -0700381
382 progress_fd_ = -1;
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900383 progress_controller_.reset();
Tianjie55abd3c2020-06-19 00:22:59 -0700384
Alex Deymo0d298542016-03-30 18:31:49 -0700385 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800386}
387
388void PostinstallRunnerAction::CompletePartitionPostinstall(
389 int return_code, const string& output) {
390 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700391 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700392
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800393 if (return_code != 0) {
394 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700395 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700396
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700397 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700398 // This special return code means that we tried to update firmware,
399 // but couldn't because we booted from FW B, and we need to reboot
400 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700401 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700402 }
Don Garrett81018e02013-07-30 18:46:31 -0700403
404 if (return_code == 4) {
405 // This special return code means that we tried to update firmware,
406 // but couldn't because we booted from FW B, and we need to reboot
407 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700408 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700409 }
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700410
411 // If postinstall script for this partition is optional we can ignore the
412 // result.
413 if (install_plan_.partitions[current_partition_].postinstall_optional) {
414 LOG(INFO) << "Ignoring postinstall failure since it is optional";
415 } else {
416 return CompletePostinstall(error_code);
417 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700418 }
Alex Deymo0d298542016-03-30 18:31:49 -0700419 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700420 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700421 ReportProgress(0);
422
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700423 PerformPartitionPostinstall();
424}
425
426void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
427 // We only attempt to mark the new slot as active if all the postinstall
428 // steps succeeded.
Sen Jiang02c49422017-10-31 15:14:11 -0700429 if (error_code == ErrorCode::kSuccess) {
430 if (install_plan_.switch_slot_on_reboot) {
Yifan Hong7b3910a2020-03-24 17:47:32 -0700431 if (!boot_control_->GetDynamicPartitionControl()->FinishUpdate(
432 install_plan_.powerwash_required) ||
Yifan Honge8583622019-11-07 11:36:31 -0800433 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Sen Jiang02c49422017-10-31 15:14:11 -0700434 error_code = ErrorCode::kPostinstallRunnerError;
Tianjie Xud6aa91f2019-11-14 11:55:10 -0800435 } else {
436 // Schedules warm reset on next reboot, ignores the error.
437 hardware_->SetWarmReset(true);
Tianjie838793d2021-01-14 22:05:13 -0800438 // Sets the vbmeta digest for the other slot to boot into.
439 hardware_->SetVbmetaDigestForInactiveSlot(false);
Sen Jiang02c49422017-10-31 15:14:11 -0700440 }
441 } else {
442 error_code = ErrorCode::kUpdatedButNotActive;
443 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700444 }
445
Kelvin Zhang433d6c42021-03-31 22:26:59 -0400446 auto dynamic_control = boot_control_->GetDynamicPartitionControl();
447 CHECK(dynamic_control);
448 dynamic_control->UnmapAllPartitions();
449 LOG(INFO) << "Unmapped all partitions.";
450
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700451 ScopedActionCompleter completer(processor_, this);
Alex Deymocbc22742016-03-04 17:53:02 -0800452 completer.set_code(error_code);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700453
Sen Jiang02c49422017-10-31 15:14:11 -0700454 if (error_code != ErrorCode::kSuccess &&
455 error_code != ErrorCode::kUpdatedButNotActive) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700456 LOG(ERROR) << "Postinstall action failed.";
457
Alex Deymofb905d92016-06-03 19:26:58 -0700458 // Undo any changes done to trigger Powerwash.
459 if (powerwash_scheduled_)
460 hardware_->CancelPowerwash();
Don Garrett81018e02013-07-30 18:46:31 -0700461
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800462 return;
463 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700464
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700465 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700466 if (HasOutputPipe()) {
467 SetOutputObject(install_plan_);
468 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000469}
470
Alex Deymod15c5462016-03-09 18:11:12 -0800471void PostinstallRunnerAction::SuspendAction() {
472 if (!current_command_)
473 return;
474 if (kill(current_command_, SIGSTOP) != 0) {
475 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800476 } else {
477 is_current_command_suspended_ = true;
Alex Deymod15c5462016-03-09 18:11:12 -0800478 }
479}
480
481void PostinstallRunnerAction::ResumeAction() {
482 if (!current_command_)
483 return;
484 if (kill(current_command_, SIGCONT) != 0) {
485 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800486 } else {
487 is_current_command_suspended_ = false;
Alex Deymod15c5462016-03-09 18:11:12 -0800488 }
489}
490
491void PostinstallRunnerAction::TerminateProcessing() {
492 if (!current_command_)
493 return;
494 // Calling KillExec() will discard the callback we registered and therefore
495 // the unretained reference to this object.
496 Subprocess::Get().KillExec(current_command_);
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800497
498 // If the command has been suspended, resume it after KillExec() so that the
499 // process can process the SIGTERM sent by KillExec().
500 if (is_current_command_suspended_) {
501 ResumeAction();
502 }
503
Alex Deymod15c5462016-03-09 18:11:12 -0800504 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700505 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800506}
507
adlr@google.com3defe6a2009-12-04 20:57:17 +0000508} // namespace chromeos_update_engine