blob: cc3843d6740c0ef0a3b46cb26d3602fa3b6f4863 [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>
Alex Deymo0d298542016-03-30 18:31:49 -070031#include <base/strings/string_split.h>
Alex Deymo390efed2016-02-18 11:00:40 -080032#include <base/strings/string_util.h>
Alex Deymo461b2592015-07-24 20:10:52 -070033
Alex Deymo39910dc2015-11-09 17:04:30 -080034#include "update_engine/common/action_processor.h"
Alex Deymob15a0b82015-11-25 20:30:40 -030035#include "update_engine/common/boot_control_interface.h"
Alex Deymo14dbd332016-03-01 18:55:54 -080036#include "update_engine/common/platform_constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080037#include "update_engine/common/subprocess.h"
38#include "update_engine/common/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000039
Alex Deymo0d298542016-03-30 18:31:49 -070040namespace {
41
42// The file descriptor number from the postinstall program's perspective where
43// it can report status updates. This can be any number greater than 2 (stderr),
44// but must be kept in sync with the "bin/postinst_progress" defined in the
45// sample_images.sh file.
46const int kPostinstallStatusFd = 3;
47
48} // namespace
49
adlr@google.com3defe6a2009-12-04 20:57:17 +000050namespace chromeos_update_engine {
51
Alex Deymo0d298542016-03-30 18:31:49 -070052using brillo::MessageLoop;
adlr@google.com3defe6a2009-12-04 20:57:17 +000053using std::string;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070054using std::vector;
adlr@google.com3defe6a2009-12-04 20:57:17 +000055
adlr@google.com3defe6a2009-12-04 20:57:17 +000056void PostinstallRunnerAction::PerformAction() {
57 CHECK(HasInputObject());
Chris Sosad317e402013-06-12 13:47:09 -070058 install_plan_ = GetInputObject();
Darin Petkov6f03a3b2010-11-10 14:27:14 -080059
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -080060 // We always powerwash when rolling back, however policy can determine
61 // if this is a full/normal powerwash, or a special rollback powerwash
62 // that retains a small amount of system state such as enrollment and
63 // network configuration. In both cases all user accounts are deleted.
Marton Hunyady199152d2018-05-07 19:08:48 +020064 if (install_plan_.powerwash_required || install_plan_.is_rollback) {
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -080065 bool save_rollback_data =
66 install_plan_.is_rollback && install_plan_.rollback_data_save_requested;
67 if (hardware_->SchedulePowerwash(save_rollback_data)) {
Alex Deymofb905d92016-06-03 19:26:58 -070068 powerwash_scheduled_ = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070069 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070070 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070071 }
72 }
73
Alex Deymo0d298542016-03-30 18:31:49 -070074 // Initialize all the partition weights.
75 partition_weight_.resize(install_plan_.partitions.size());
76 total_weight_ = 0;
77 for (size_t i = 0; i < install_plan_.partitions.size(); ++i) {
78 // TODO(deymo): This code sets the weight to all the postinstall commands,
79 // but we could remember how long they took in the past and use those
80 // values.
81 partition_weight_[i] = install_plan_.partitions[i].run_postinstall;
82 total_weight_ += partition_weight_[i];
83 }
84 accumulated_weight_ = 0;
85 ReportProgress(0);
86
Alex Deymoe5e5fe92015-10-05 09:28:19 -070087 PerformPartitionPostinstall();
88}
89
90void PostinstallRunnerAction::PerformPartitionPostinstall() {
Sen Jiang02c49422017-10-31 15:14:11 -070091 if (!install_plan_.run_post_install) {
92 LOG(INFO) << "Skipping post-install according to install plan.";
93 return CompletePostinstall(ErrorCode::kSuccess);
94 }
95
Alex Deymo390efed2016-02-18 11:00:40 -080096 if (install_plan_.download_url.empty()) {
97 LOG(INFO) << "Skipping post-install during rollback";
98 return CompletePostinstall(ErrorCode::kSuccess);
99 }
100
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700101 // Skip all the partitions that don't have a post-install step.
102 while (current_partition_ < install_plan_.partitions.size() &&
103 !install_plan_.partitions[current_partition_].run_postinstall) {
104 VLOG(1) << "Skipping post-install on partition "
105 << install_plan_.partitions[current_partition_].name;
106 current_partition_++;
107 }
108 if (current_partition_ == install_plan_.partitions.size())
109 return CompletePostinstall(ErrorCode::kSuccess);
110
111 const InstallPlan::Partition& partition =
112 install_plan_.partitions[current_partition_];
113
114 const string mountable_device =
115 utils::MakePartitionNameForMount(partition.target_path);
116 if (mountable_device.empty()) {
117 LOG(ERROR) << "Cannot make mountable device from " << partition.target_path;
118 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
119 }
120
121 // Perform post-install for the current_partition_ partition. At this point we
122 // need to call CompletePartitionPostinstall to complete the operation and
123 // cleanup.
Alex Deymo390efed2016-02-18 11:00:40 -0800124#ifdef __ANDROID__
125 fs_mount_dir_ = "/postinstall";
126#else // __ANDROID__
Sen Jiang371615b2016-04-13 15:54:29 -0700127 base::FilePath temp_dir;
128 TEST_AND_RETURN(base::CreateNewTempDirectory("au_postint_mount", &temp_dir));
129 fs_mount_dir_ = temp_dir.value();
Alex Deymo390efed2016-02-18 11:00:40 -0800130#endif // __ANDROID__
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700131
Alex Deymof4116502017-03-22 17:00:31 -0700132 // Double check that the fs_mount_dir is not busy with a previous mounted
133 // filesystem from a previous crashed postinstall step.
134 if (utils::IsMountpoint(fs_mount_dir_)) {
135 LOG(INFO) << "Found previously mounted filesystem at " << fs_mount_dir_;
136 utils::UnmountFilesystem(fs_mount_dir_);
137 }
138
Alex Deymocbc22742016-03-04 17:53:02 -0800139 base::FilePath postinstall_path(partition.postinstall_path);
140 if (postinstall_path.IsAbsolute()) {
141 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
142 "path instead: "
143 << partition.postinstall_path;
144 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
145 }
146
147 string abs_path =
148 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800149 if (!base::StartsWith(
150 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
151 LOG(ERROR) << "Invalid relative postinstall path: "
152 << partition.postinstall_path;
153 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
154 }
155
Alex Deymo5fb356c2016-03-25 18:48:22 -0700156#ifdef __ANDROID__
157 // In Chromium OS, the postinstall step is allowed to write to the block
158 // device on the target image, so we don't mark it as read-only and should
159 // be read-write since we just wrote to it during the update.
160
161 // Mark the block device as read-only before mounting for post-install.
162 if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) {
163 return CompletePartitionPostinstall(
164 1, "Error marking the device " + mountable_device + " read only.");
165 }
166#endif // __ANDROID__
167
Alex Deymo390efed2016-02-18 11:00:40 -0800168 if (!utils::MountFilesystem(mountable_device,
169 fs_mount_dir_,
170 MS_RDONLY,
Alex Deymo14dbd332016-03-01 18:55:54 -0800171 partition.filesystem_type,
172 constants::kPostinstallMountOptions)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700173 return CompletePartitionPostinstall(
174 1, "Error mounting the device " + mountable_device);
175 }
176
Alex Deymo390efed2016-02-18 11:00:40 -0800177 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
178 << abs_path << ") installed on device " << partition.target_path
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700179 << " and mountable device " << mountable_device;
180
Alex Deymo032e7722014-03-25 17:53:56 -0700181 // Logs the file format of the postinstall script we are about to run. This
182 // will help debug when the postinstall script doesn't match the architecture
183 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800184 LOG(INFO) << "Format file for new " << partition.postinstall_path
185 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700186
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800187 // Runs the postinstall script asynchronously to free up the main loop while
188 // it's running.
Alex Deymo0d298542016-03-30 18:31:49 -0700189 vector<string> command = {abs_path};
190#ifdef __ANDROID__
191 // In Brillo and Android, we pass the slot number and status fd.
192 command.push_back(std::to_string(install_plan_.target_slot));
193 command.push_back(std::to_string(kPostinstallStatusFd));
194#else
195 // Chrome OS postinstall expects the target rootfs as the first parameter.
196 command.push_back(partition.target_path);
197#endif // __ANDROID__
198
199 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymod15c5462016-03-09 18:11:12 -0800200 command,
Alex Deymo0d298542016-03-30 18:31:49 -0700201 Subprocess::kRedirectStderrToStdout,
202 {kPostinstallStatusFd},
Alex Deymod15c5462016-03-09 18:11:12 -0800203 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
204 base::Unretained(this)));
205 // Subprocess::Exec should never return a negative process id.
206 CHECK_GE(current_command_, 0);
207
Alex Deymo0d298542016-03-30 18:31:49 -0700208 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700209 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo0d298542016-03-30 18:31:49 -0700210 return;
211 }
212
213 // Monitor the status file descriptor.
214 progress_fd_ =
215 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
216 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
217 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
218 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
219 }
220
221 progress_task_ = MessageLoop::current()->WatchFileDescriptor(
222 FROM_HERE,
223 progress_fd_,
224 MessageLoop::WatchMode::kWatchRead,
225 true,
226 base::Bind(&PostinstallRunnerAction::OnProgressFdReady,
227 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800228}
229
Alex Deymo0d298542016-03-30 18:31:49 -0700230void PostinstallRunnerAction::OnProgressFdReady() {
231 char buf[1024];
232 size_t bytes_read;
233 do {
234 bytes_read = 0;
235 bool eof;
236 bool ok =
237 utils::ReadAll(progress_fd_, buf, arraysize(buf), &bytes_read, &eof);
238 progress_buffer_.append(buf, bytes_read);
239 // Process every line.
240 vector<string> lines = base::SplitString(
241 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
242 if (!lines.empty()) {
243 progress_buffer_ = lines.back();
244 lines.pop_back();
245 for (const auto& line : lines) {
246 ProcessProgressLine(line);
247 }
248 }
249 if (!ok || eof) {
250 // There was either an error or an EOF condition, so we are done watching
251 // the file descriptor.
252 MessageLoop::current()->CancelTask(progress_task_);
253 progress_task_ = MessageLoop::kTaskIdNull;
254 return;
255 }
256 } while (bytes_read);
257}
258
259bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
260 double frac = 0;
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700261 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 &&
262 !std::isnan(frac)) {
Alex Deymo0d298542016-03-30 18:31:49 -0700263 ReportProgress(frac);
264 return true;
265 }
266
267 return false;
268}
269
270void PostinstallRunnerAction::ReportProgress(double frac) {
271 if (!delegate_)
272 return;
Yoshitaka Ishida128936f2018-02-16 18:20:07 +0900273 if (current_partition_ >= partition_weight_.size() || total_weight_ == 0) {
Alex Deymo0d298542016-03-30 18:31:49 -0700274 delegate_->ProgressUpdate(1.);
275 return;
276 }
Alex Deymo44b35672016-04-05 17:57:48 -0700277 if (!std::isfinite(frac) || frac < 0)
Alex Deymo0d298542016-03-30 18:31:49 -0700278 frac = 0;
279 if (frac > 1)
280 frac = 1;
281 double postinst_action_progress =
282 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
283 total_weight_;
284 delegate_->ProgressUpdate(postinst_action_progress);
285}
286
287void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800288 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800289#ifndef __ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800290 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) {
291 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700292 }
Alex Deymod15c5462016-03-09 18:11:12 -0800293#endif // !__ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800294 fs_mount_dir_.clear();
Alex Deymo0d298542016-03-30 18:31:49 -0700295
296 progress_fd_ = -1;
297 if (progress_task_ != MessageLoop::kTaskIdNull) {
298 MessageLoop::current()->CancelTask(progress_task_);
299 progress_task_ = MessageLoop::kTaskIdNull;
300 }
301 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800302}
303
304void PostinstallRunnerAction::CompletePartitionPostinstall(
305 int return_code, const string& output) {
306 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700307 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700308
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800309 if (return_code != 0) {
310 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700311 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700312
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700313 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700314 // This special return code means that we tried to update firmware,
315 // but couldn't because we booted from FW B, and we need to reboot
316 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700317 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700318 }
Don Garrett81018e02013-07-30 18:46:31 -0700319
320 if (return_code == 4) {
321 // This special return code means that we tried to update firmware,
322 // but couldn't because we booted from FW B, and we need to reboot
323 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700324 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700325 }
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700326
327 // If postinstall script for this partition is optional we can ignore the
328 // result.
329 if (install_plan_.partitions[current_partition_].postinstall_optional) {
330 LOG(INFO) << "Ignoring postinstall failure since it is optional";
331 } else {
332 return CompletePostinstall(error_code);
333 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700334 }
Alex Deymo0d298542016-03-30 18:31:49 -0700335 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700336 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700337 ReportProgress(0);
338
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700339 PerformPartitionPostinstall();
340}
341
342void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
343 // We only attempt to mark the new slot as active if all the postinstall
344 // steps succeeded.
Sen Jiang02c49422017-10-31 15:14:11 -0700345 if (error_code == ErrorCode::kSuccess) {
346 if (install_plan_.switch_slot_on_reboot) {
347 if (!boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
348 error_code = ErrorCode::kPostinstallRunnerError;
349 }
350 } else {
351 error_code = ErrorCode::kUpdatedButNotActive;
352 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700353 }
354
355 ScopedActionCompleter completer(processor_, this);
Alex Deymocbc22742016-03-04 17:53:02 -0800356 completer.set_code(error_code);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700357
Sen Jiang02c49422017-10-31 15:14:11 -0700358 if (error_code != ErrorCode::kSuccess &&
359 error_code != ErrorCode::kUpdatedButNotActive) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700360 LOG(ERROR) << "Postinstall action failed.";
361
Alex Deymofb905d92016-06-03 19:26:58 -0700362 // Undo any changes done to trigger Powerwash.
363 if (powerwash_scheduled_)
364 hardware_->CancelPowerwash();
Don Garrett81018e02013-07-30 18:46:31 -0700365
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800366 return;
367 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700368
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700369 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700370 if (HasOutputPipe()) {
371 SetOutputObject(install_plan_);
372 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000373}
374
Alex Deymod15c5462016-03-09 18:11:12 -0800375void PostinstallRunnerAction::SuspendAction() {
376 if (!current_command_)
377 return;
378 if (kill(current_command_, SIGSTOP) != 0) {
379 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800380 } else {
381 is_current_command_suspended_ = true;
Alex Deymod15c5462016-03-09 18:11:12 -0800382 }
383}
384
385void PostinstallRunnerAction::ResumeAction() {
386 if (!current_command_)
387 return;
388 if (kill(current_command_, SIGCONT) != 0) {
389 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800390 } else {
391 is_current_command_suspended_ = false;
Alex Deymod15c5462016-03-09 18:11:12 -0800392 }
393}
394
395void PostinstallRunnerAction::TerminateProcessing() {
396 if (!current_command_)
397 return;
398 // Calling KillExec() will discard the callback we registered and therefore
399 // the unretained reference to this object.
400 Subprocess::Get().KillExec(current_command_);
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800401
402 // If the command has been suspended, resume it after KillExec() so that the
403 // process can process the SIGTERM sent by KillExec().
404 if (is_current_command_suspended_) {
405 ResumeAction();
406 }
407
Alex Deymod15c5462016-03-09 18:11:12 -0800408 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700409 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800410}
411
adlr@google.com3defe6a2009-12-04 20:57:17 +0000412} // namespace chromeos_update_engine