blob: 6a6d687afd47de155b3b0991efc8873d656c6936 [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>
27
Alex Deymoe5e5fe92015-10-05 09:28:19 -070028#include <base/files/file_path.h>
29#include <base/files/file_util.h>
Alex Deymod15c5462016-03-09 18:11:12 -080030#include <base/logging.h>
hscham00b6aa22020-02-20 12:32:06 +090031#include <base/stl_util.h>
Alex Deymo0d298542016-03-30 18:31:49 -070032#include <base/strings/string_split.h>
Alex Deymo390efed2016-02-18 11:00:40 -080033#include <base/strings/string_util.h>
Alex Deymo461b2592015-07-24 20:10:52 -070034
Alex Deymo39910dc2015-11-09 17:04:30 -080035#include "update_engine/common/action_processor.h"
Alex Deymob15a0b82015-11-25 20:30:40 -030036#include "update_engine/common/boot_control_interface.h"
Alex Deymo14dbd332016-03-01 18:55:54 -080037#include "update_engine/common/platform_constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080038#include "update_engine/common/subprocess.h"
39#include "update_engine/common/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000040
Alex Deymo0d298542016-03-30 18:31:49 -070041namespace {
42
43// The file descriptor number from the postinstall program's perspective where
44// it can report status updates. This can be any number greater than 2 (stderr),
45// but must be kept in sync with the "bin/postinst_progress" defined in the
46// sample_images.sh file.
47const int kPostinstallStatusFd = 3;
48
49} // namespace
50
adlr@google.com3defe6a2009-12-04 20:57:17 +000051namespace chromeos_update_engine {
52
53using std::string;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070054using std::vector;
adlr@google.com3defe6a2009-12-04 20:57:17 +000055
Kelvin Zhange9def4e2020-12-02 14:04:09 -050056PostinstallRunnerAction::PostinstallRunnerAction(
57 BootControlInterface* boot_control, HardwareInterface* hardware)
58 : boot_control_(boot_control), hardware_(hardware) {
59#ifdef __ANDROID__
60 fs_mount_dir_ = "/postinstall";
61#else // __ANDROID__
62 base::FilePath temp_dir;
63 TEST_AND_RETURN(base::CreateNewTempDirectory("au_postint_mount", &temp_dir));
64 fs_mount_dir_ = temp_dir.value();
65#endif // __ANDROID__
66}
67
adlr@google.com3defe6a2009-12-04 20:57:17 +000068void PostinstallRunnerAction::PerformAction() {
69 CHECK(HasInputObject());
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -040070 CHECK(boot_control_);
Chris Sosad317e402013-06-12 13:47:09 -070071 install_plan_ = GetInputObject();
Darin Petkov6f03a3b2010-11-10 14:27:14 -080072
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -040073 auto dynamic_control = boot_control_->GetDynamicPartitionControl();
74 CHECK(dynamic_control);
75
76 // Mount snapshot partitions for Virtual AB Compression Compression.
77 if (dynamic_control->GetVirtualAbCompressionFeatureFlag().IsEnabled()) {
78 // Before calling MapAllPartitions to map snapshot devices, all CowWriters
79 // must be closed, and MapAllPartitions() should be called.
80 dynamic_control->UnmapAllPartitions();
81 if (!dynamic_control->MapAllPartitions()) {
82 return CompletePostinstall(ErrorCode::kPostInstallMountError);
83 }
84 }
85
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -080086 // We always powerwash when rolling back, however policy can determine
87 // if this is a full/normal powerwash, or a special rollback powerwash
88 // that retains a small amount of system state such as enrollment and
89 // network configuration. In both cases all user accounts are deleted.
Marton Hunyady199152d2018-05-07 19:08:48 +020090 if (install_plan_.powerwash_required || install_plan_.is_rollback) {
Miriam Polzeraff72002020-08-27 08:20:39 +020091 if (hardware_->SchedulePowerwash(
92 install_plan_.rollback_data_save_requested)) {
Alex Deymofb905d92016-06-03 19:26:58 -070093 powerwash_scheduled_ = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070094 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070095 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070096 }
97 }
98
Alex Deymo0d298542016-03-30 18:31:49 -070099 // Initialize all the partition weights.
100 partition_weight_.resize(install_plan_.partitions.size());
101 total_weight_ = 0;
102 for (size_t i = 0; i < install_plan_.partitions.size(); ++i) {
Tianjie Xu087de9d2019-11-01 17:11:22 -0700103 auto& partition = install_plan_.partitions[i];
104 if (!install_plan_.run_post_install && partition.postinstall_optional) {
105 partition.run_postinstall = false;
106 LOG(INFO) << "Skipping optional post-install for partition "
107 << partition.name << " according to install plan.";
108 }
109
Alex Deymo0d298542016-03-30 18:31:49 -0700110 // TODO(deymo): This code sets the weight to all the postinstall commands,
111 // but we could remember how long they took in the past and use those
112 // values.
Tianjie Xu087de9d2019-11-01 17:11:22 -0700113 partition_weight_[i] = partition.run_postinstall;
Alex Deymo0d298542016-03-30 18:31:49 -0700114 total_weight_ += partition_weight_[i];
115 }
116 accumulated_weight_ = 0;
117 ReportProgress(0);
118
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700119 PerformPartitionPostinstall();
120}
121
122void PostinstallRunnerAction::PerformPartitionPostinstall() {
Alex Deymo390efed2016-02-18 11:00:40 -0800123 if (install_plan_.download_url.empty()) {
124 LOG(INFO) << "Skipping post-install during rollback";
125 return CompletePostinstall(ErrorCode::kSuccess);
126 }
127
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700128 // Skip all the partitions that don't have a post-install step.
129 while (current_partition_ < install_plan_.partitions.size() &&
130 !install_plan_.partitions[current_partition_].run_postinstall) {
131 VLOG(1) << "Skipping post-install on partition "
132 << install_plan_.partitions[current_partition_].name;
133 current_partition_++;
134 }
135 if (current_partition_ == install_plan_.partitions.size())
136 return CompletePostinstall(ErrorCode::kSuccess);
137
138 const InstallPlan::Partition& partition =
139 install_plan_.partitions[current_partition_];
140
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500141 const string mountable_device = partition.postinstall_mount_device;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700142 if (mountable_device.empty()) {
143 LOG(ERROR) << "Cannot make mountable device from " << partition.target_path;
144 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
145 }
146
147 // Perform post-install for the current_partition_ partition. At this point we
148 // need to call CompletePartitionPostinstall to complete the operation and
149 // cleanup.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700150
Kelvin Zhang4ce01102020-11-16 09:32:08 -0500151 if (!utils::FileExists(fs_mount_dir_.c_str())) {
152 LOG(ERROR) << "Mount point " << fs_mount_dir_
153 << " does not exist, mount call will fail";
154 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
155 }
Alex Deymof4116502017-03-22 17:00:31 -0700156 // Double check that the fs_mount_dir is not busy with a previous mounted
157 // filesystem from a previous crashed postinstall step.
158 if (utils::IsMountpoint(fs_mount_dir_)) {
159 LOG(INFO) << "Found previously mounted filesystem at " << fs_mount_dir_;
160 utils::UnmountFilesystem(fs_mount_dir_);
161 }
162
Alex Deymocbc22742016-03-04 17:53:02 -0800163 base::FilePath postinstall_path(partition.postinstall_path);
164 if (postinstall_path.IsAbsolute()) {
165 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
166 "path instead: "
167 << partition.postinstall_path;
168 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
169 }
170
171 string abs_path =
172 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800173 if (!base::StartsWith(
174 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
175 LOG(ERROR) << "Invalid relative postinstall path: "
176 << partition.postinstall_path;
177 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
178 }
179
Alex Deymo5fb356c2016-03-25 18:48:22 -0700180#ifdef __ANDROID__
181 // In Chromium OS, the postinstall step is allowed to write to the block
182 // device on the target image, so we don't mark it as read-only and should
183 // be read-write since we just wrote to it during the update.
184
185 // Mark the block device as read-only before mounting for post-install.
186 if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) {
187 return CompletePartitionPostinstall(
188 1, "Error marking the device " + mountable_device + " read only.");
189 }
190#endif // __ANDROID__
191
Alex Deymo390efed2016-02-18 11:00:40 -0800192 if (!utils::MountFilesystem(mountable_device,
193 fs_mount_dir_,
194 MS_RDONLY,
Alex Deymo14dbd332016-03-01 18:55:54 -0800195 partition.filesystem_type,
196 constants::kPostinstallMountOptions)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700197 return CompletePartitionPostinstall(
198 1, "Error mounting the device " + mountable_device);
199 }
200
Alex Deymo390efed2016-02-18 11:00:40 -0800201 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
202 << abs_path << ") installed on device " << partition.target_path
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700203 << " and mountable device " << mountable_device;
204
Alex Deymo032e7722014-03-25 17:53:56 -0700205 // Logs the file format of the postinstall script we are about to run. This
206 // will help debug when the postinstall script doesn't match the architecture
207 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800208 LOG(INFO) << "Format file for new " << partition.postinstall_path
209 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700210
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800211 // Runs the postinstall script asynchronously to free up the main loop while
212 // it's running.
Alex Deymo0d298542016-03-30 18:31:49 -0700213 vector<string> command = {abs_path};
214#ifdef __ANDROID__
215 // In Brillo and Android, we pass the slot number and status fd.
216 command.push_back(std::to_string(install_plan_.target_slot));
217 command.push_back(std::to_string(kPostinstallStatusFd));
218#else
219 // Chrome OS postinstall expects the target rootfs as the first parameter.
220 command.push_back(partition.target_path);
221#endif // __ANDROID__
222
223 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymod15c5462016-03-09 18:11:12 -0800224 command,
Alex Deymo0d298542016-03-30 18:31:49 -0700225 Subprocess::kRedirectStderrToStdout,
226 {kPostinstallStatusFd},
Alex Deymod15c5462016-03-09 18:11:12 -0800227 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
228 base::Unretained(this)));
229 // Subprocess::Exec should never return a negative process id.
230 CHECK_GE(current_command_, 0);
231
Alex Deymo0d298542016-03-30 18:31:49 -0700232 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700233 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo0d298542016-03-30 18:31:49 -0700234 return;
235 }
236
237 // Monitor the status file descriptor.
238 progress_fd_ =
239 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
240 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
241 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
242 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
243 }
244
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900245 progress_controller_ = base::FileDescriptorWatcher::WatchReadable(
Alex Deymo0d298542016-03-30 18:31:49 -0700246 progress_fd_,
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900247 base::BindRepeating(&PostinstallRunnerAction::OnProgressFdReady,
248 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800249}
250
Alex Deymo0d298542016-03-30 18:31:49 -0700251void PostinstallRunnerAction::OnProgressFdReady() {
252 char buf[1024];
253 size_t bytes_read;
254 do {
255 bytes_read = 0;
256 bool eof;
257 bool ok =
hscham00b6aa22020-02-20 12:32:06 +0900258 utils::ReadAll(progress_fd_, buf, base::size(buf), &bytes_read, &eof);
Alex Deymo0d298542016-03-30 18:31:49 -0700259 progress_buffer_.append(buf, bytes_read);
260 // Process every line.
261 vector<string> lines = base::SplitString(
262 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
263 if (!lines.empty()) {
264 progress_buffer_ = lines.back();
265 lines.pop_back();
266 for (const auto& line : lines) {
267 ProcessProgressLine(line);
268 }
269 }
270 if (!ok || eof) {
271 // There was either an error or an EOF condition, so we are done watching
272 // the file descriptor.
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900273 progress_controller_.reset();
Alex Deymo0d298542016-03-30 18:31:49 -0700274 return;
275 }
276 } while (bytes_read);
277}
278
279bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
280 double frac = 0;
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700281 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 &&
282 !std::isnan(frac)) {
Alex Deymo0d298542016-03-30 18:31:49 -0700283 ReportProgress(frac);
284 return true;
285 }
286
287 return false;
288}
289
290void PostinstallRunnerAction::ReportProgress(double frac) {
291 if (!delegate_)
292 return;
Yoshitaka Ishida128936f2018-02-16 18:20:07 +0900293 if (current_partition_ >= partition_weight_.size() || total_weight_ == 0) {
Alex Deymo0d298542016-03-30 18:31:49 -0700294 delegate_->ProgressUpdate(1.);
295 return;
296 }
Alex Deymo44b35672016-04-05 17:57:48 -0700297 if (!std::isfinite(frac) || frac < 0)
Alex Deymo0d298542016-03-30 18:31:49 -0700298 frac = 0;
299 if (frac > 1)
300 frac = 1;
301 double postinst_action_progress =
302 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
303 total_weight_;
304 delegate_->ProgressUpdate(postinst_action_progress);
305}
306
307void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800308 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800309#ifndef __ANDROID__
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500310#if BASE_VER < 800000
311 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), true)) {
312#else
hscham043355b2020-11-17 16:50:10 +0900313 if (!base::DeleteFile(base::FilePath(fs_mount_dir_))) {
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500314#endif
Alex Deymo390efed2016-02-18 11:00:40 -0800315 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700316 }
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500317#endif
Alex Deymo390efed2016-02-18 11:00:40 -0800318 fs_mount_dir_.clear();
Alex Deymo0d298542016-03-30 18:31:49 -0700319
320 progress_fd_ = -1;
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900321 progress_controller_.reset();
Tianjie55abd3c2020-06-19 00:22:59 -0700322
Alex Deymo0d298542016-03-30 18:31:49 -0700323 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800324}
325
326void PostinstallRunnerAction::CompletePartitionPostinstall(
327 int return_code, const string& output) {
328 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700329 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700330
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800331 if (return_code != 0) {
332 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700333 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700334
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700335 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700336 // This special return code means that we tried to update firmware,
337 // but couldn't because we booted from FW B, and we need to reboot
338 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700339 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700340 }
Don Garrett81018e02013-07-30 18:46:31 -0700341
342 if (return_code == 4) {
343 // This special return code means that we tried to update firmware,
344 // but couldn't because we booted from FW B, and we need to reboot
345 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700346 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700347 }
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700348
349 // If postinstall script for this partition is optional we can ignore the
350 // result.
351 if (install_plan_.partitions[current_partition_].postinstall_optional) {
352 LOG(INFO) << "Ignoring postinstall failure since it is optional";
353 } else {
354 return CompletePostinstall(error_code);
355 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700356 }
Alex Deymo0d298542016-03-30 18:31:49 -0700357 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700358 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700359 ReportProgress(0);
360
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700361 PerformPartitionPostinstall();
362}
363
364void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
365 // We only attempt to mark the new slot as active if all the postinstall
366 // steps succeeded.
Sen Jiang02c49422017-10-31 15:14:11 -0700367 if (error_code == ErrorCode::kSuccess) {
368 if (install_plan_.switch_slot_on_reboot) {
Yifan Hong7b3910a2020-03-24 17:47:32 -0700369 if (!boot_control_->GetDynamicPartitionControl()->FinishUpdate(
370 install_plan_.powerwash_required) ||
Yifan Honge8583622019-11-07 11:36:31 -0800371 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Sen Jiang02c49422017-10-31 15:14:11 -0700372 error_code = ErrorCode::kPostinstallRunnerError;
Tianjie Xud6aa91f2019-11-14 11:55:10 -0800373 } else {
374 // Schedules warm reset on next reboot, ignores the error.
375 hardware_->SetWarmReset(true);
Sen Jiang02c49422017-10-31 15:14:11 -0700376 }
377 } else {
378 error_code = ErrorCode::kUpdatedButNotActive;
379 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700380 }
381
382 ScopedActionCompleter completer(processor_, this);
Alex Deymocbc22742016-03-04 17:53:02 -0800383 completer.set_code(error_code);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700384
Sen Jiang02c49422017-10-31 15:14:11 -0700385 if (error_code != ErrorCode::kSuccess &&
386 error_code != ErrorCode::kUpdatedButNotActive) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700387 LOG(ERROR) << "Postinstall action failed.";
388
Alex Deymofb905d92016-06-03 19:26:58 -0700389 // Undo any changes done to trigger Powerwash.
390 if (powerwash_scheduled_)
391 hardware_->CancelPowerwash();
Don Garrett81018e02013-07-30 18:46:31 -0700392
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800393 return;
394 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700395
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700396 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700397 if (HasOutputPipe()) {
398 SetOutputObject(install_plan_);
399 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000400}
401
Alex Deymod15c5462016-03-09 18:11:12 -0800402void PostinstallRunnerAction::SuspendAction() {
403 if (!current_command_)
404 return;
405 if (kill(current_command_, SIGSTOP) != 0) {
406 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800407 } else {
408 is_current_command_suspended_ = true;
Alex Deymod15c5462016-03-09 18:11:12 -0800409 }
410}
411
412void PostinstallRunnerAction::ResumeAction() {
413 if (!current_command_)
414 return;
415 if (kill(current_command_, SIGCONT) != 0) {
416 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800417 } else {
418 is_current_command_suspended_ = false;
Alex Deymod15c5462016-03-09 18:11:12 -0800419 }
420}
421
422void PostinstallRunnerAction::TerminateProcessing() {
423 if (!current_command_)
424 return;
425 // Calling KillExec() will discard the callback we registered and therefore
426 // the unretained reference to this object.
427 Subprocess::Get().KillExec(current_command_);
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800428
429 // If the command has been suspended, resume it after KillExec() so that the
430 // process can process the SIGTERM sent by KillExec().
431 if (is_current_command_suspended_) {
432 ResumeAction();
433 }
434
Alex Deymod15c5462016-03-09 18:11:12 -0800435 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700436 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800437}
438
adlr@google.com3defe6a2009-12-04 20:57:17 +0000439} // namespace chromeos_update_engine