blob: c08cfc207e23d072fd021f6fea9b6baec45866a5 [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
Marton Hunyady199152d2018-05-07 19:08:48 +020060 // Currently we're always powerwashing when rolling back.
61 if (install_plan_.powerwash_required || install_plan_.is_rollback) {
Zentaro Kavanagh0418de32019-01-15 10:29:35 -080062 if (hardware_->SchedulePowerwash(install_plan_.is_rollback)) {
Alex Deymofb905d92016-06-03 19:26:58 -070063 powerwash_scheduled_ = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070064 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070065 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070066 }
67 }
68
Alex Deymo0d298542016-03-30 18:31:49 -070069 // Initialize all the partition weights.
70 partition_weight_.resize(install_plan_.partitions.size());
71 total_weight_ = 0;
72 for (size_t i = 0; i < install_plan_.partitions.size(); ++i) {
Tianjie Xu087de9d2019-11-01 17:11:22 -070073 auto& partition = install_plan_.partitions[i];
74 if (!install_plan_.run_post_install && partition.postinstall_optional) {
75 partition.run_postinstall = false;
76 LOG(INFO) << "Skipping optional post-install for partition "
77 << partition.name << " according to install plan.";
78 }
79
Alex Deymo0d298542016-03-30 18:31:49 -070080 // TODO(deymo): This code sets the weight to all the postinstall commands,
81 // but we could remember how long they took in the past and use those
82 // values.
Tianjie Xu087de9d2019-11-01 17:11:22 -070083 partition_weight_[i] = partition.run_postinstall;
Alex Deymo0d298542016-03-30 18:31:49 -070084 total_weight_ += partition_weight_[i];
85 }
86 accumulated_weight_ = 0;
87 ReportProgress(0);
88
Alex Deymoe5e5fe92015-10-05 09:28:19 -070089 PerformPartitionPostinstall();
90}
91
92void PostinstallRunnerAction::PerformPartitionPostinstall() {
Alex Deymo390efed2016-02-18 11:00:40 -080093 if (install_plan_.download_url.empty()) {
94 LOG(INFO) << "Skipping post-install during rollback";
95 return CompletePostinstall(ErrorCode::kSuccess);
96 }
97
Alex Deymoe5e5fe92015-10-05 09:28:19 -070098 // Skip all the partitions that don't have a post-install step.
99 while (current_partition_ < install_plan_.partitions.size() &&
100 !install_plan_.partitions[current_partition_].run_postinstall) {
101 VLOG(1) << "Skipping post-install on partition "
102 << install_plan_.partitions[current_partition_].name;
103 current_partition_++;
104 }
105 if (current_partition_ == install_plan_.partitions.size())
106 return CompletePostinstall(ErrorCode::kSuccess);
107
108 const InstallPlan::Partition& partition =
109 install_plan_.partitions[current_partition_];
110
111 const string mountable_device =
112 utils::MakePartitionNameForMount(partition.target_path);
113 if (mountable_device.empty()) {
114 LOG(ERROR) << "Cannot make mountable device from " << partition.target_path;
115 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
116 }
117
118 // Perform post-install for the current_partition_ partition. At this point we
119 // need to call CompletePartitionPostinstall to complete the operation and
120 // cleanup.
Alex Deymo390efed2016-02-18 11:00:40 -0800121#ifdef __ANDROID__
122 fs_mount_dir_ = "/postinstall";
123#else // __ANDROID__
Sen Jiang371615b2016-04-13 15:54:29 -0700124 base::FilePath temp_dir;
125 TEST_AND_RETURN(base::CreateNewTempDirectory("au_postint_mount", &temp_dir));
126 fs_mount_dir_ = temp_dir.value();
Alex Deymo390efed2016-02-18 11:00:40 -0800127#endif // __ANDROID__
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700128
Alex Deymof4116502017-03-22 17:00:31 -0700129 // Double check that the fs_mount_dir is not busy with a previous mounted
130 // filesystem from a previous crashed postinstall step.
131 if (utils::IsMountpoint(fs_mount_dir_)) {
132 LOG(INFO) << "Found previously mounted filesystem at " << fs_mount_dir_;
133 utils::UnmountFilesystem(fs_mount_dir_);
134 }
135
Alex Deymocbc22742016-03-04 17:53:02 -0800136 base::FilePath postinstall_path(partition.postinstall_path);
137 if (postinstall_path.IsAbsolute()) {
138 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
139 "path instead: "
140 << partition.postinstall_path;
141 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
142 }
143
144 string abs_path =
145 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800146 if (!base::StartsWith(
147 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
148 LOG(ERROR) << "Invalid relative postinstall path: "
149 << partition.postinstall_path;
150 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
151 }
152
Alex Deymo5fb356c2016-03-25 18:48:22 -0700153#ifdef __ANDROID__
154 // In Chromium OS, the postinstall step is allowed to write to the block
155 // device on the target image, so we don't mark it as read-only and should
156 // be read-write since we just wrote to it during the update.
157
158 // Mark the block device as read-only before mounting for post-install.
159 if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) {
160 return CompletePartitionPostinstall(
161 1, "Error marking the device " + mountable_device + " read only.");
162 }
163#endif // __ANDROID__
164
Alex Deymo390efed2016-02-18 11:00:40 -0800165 if (!utils::MountFilesystem(mountable_device,
166 fs_mount_dir_,
167 MS_RDONLY,
Alex Deymo14dbd332016-03-01 18:55:54 -0800168 partition.filesystem_type,
169 constants::kPostinstallMountOptions)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700170 return CompletePartitionPostinstall(
171 1, "Error mounting the device " + mountable_device);
172 }
173
Alex Deymo390efed2016-02-18 11:00:40 -0800174 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
175 << abs_path << ") installed on device " << partition.target_path
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700176 << " and mountable device " << mountable_device;
177
Alex Deymo032e7722014-03-25 17:53:56 -0700178 // Logs the file format of the postinstall script we are about to run. This
179 // will help debug when the postinstall script doesn't match the architecture
180 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800181 LOG(INFO) << "Format file for new " << partition.postinstall_path
182 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700183
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800184 // Runs the postinstall script asynchronously to free up the main loop while
185 // it's running.
Alex Deymo0d298542016-03-30 18:31:49 -0700186 vector<string> command = {abs_path};
187#ifdef __ANDROID__
188 // In Brillo and Android, we pass the slot number and status fd.
189 command.push_back(std::to_string(install_plan_.target_slot));
190 command.push_back(std::to_string(kPostinstallStatusFd));
191#else
192 // Chrome OS postinstall expects the target rootfs as the first parameter.
193 command.push_back(partition.target_path);
194#endif // __ANDROID__
195
196 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymod15c5462016-03-09 18:11:12 -0800197 command,
Alex Deymo0d298542016-03-30 18:31:49 -0700198 Subprocess::kRedirectStderrToStdout,
199 {kPostinstallStatusFd},
Alex Deymod15c5462016-03-09 18:11:12 -0800200 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
201 base::Unretained(this)));
202 // Subprocess::Exec should never return a negative process id.
203 CHECK_GE(current_command_, 0);
204
Alex Deymo0d298542016-03-30 18:31:49 -0700205 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700206 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo0d298542016-03-30 18:31:49 -0700207 return;
208 }
209
210 // Monitor the status file descriptor.
211 progress_fd_ =
212 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
213 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
214 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
215 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
216 }
217
218 progress_task_ = MessageLoop::current()->WatchFileDescriptor(
219 FROM_HERE,
220 progress_fd_,
221 MessageLoop::WatchMode::kWatchRead,
222 true,
223 base::Bind(&PostinstallRunnerAction::OnProgressFdReady,
224 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800225}
226
Alex Deymo0d298542016-03-30 18:31:49 -0700227void PostinstallRunnerAction::OnProgressFdReady() {
228 char buf[1024];
229 size_t bytes_read;
230 do {
231 bytes_read = 0;
232 bool eof;
233 bool ok =
234 utils::ReadAll(progress_fd_, buf, arraysize(buf), &bytes_read, &eof);
235 progress_buffer_.append(buf, bytes_read);
236 // Process every line.
237 vector<string> lines = base::SplitString(
238 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
239 if (!lines.empty()) {
240 progress_buffer_ = lines.back();
241 lines.pop_back();
242 for (const auto& line : lines) {
243 ProcessProgressLine(line);
244 }
245 }
246 if (!ok || eof) {
247 // There was either an error or an EOF condition, so we are done watching
248 // the file descriptor.
249 MessageLoop::current()->CancelTask(progress_task_);
250 progress_task_ = MessageLoop::kTaskIdNull;
251 return;
252 }
253 } while (bytes_read);
254}
255
256bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
257 double frac = 0;
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700258 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 &&
259 !std::isnan(frac)) {
Alex Deymo0d298542016-03-30 18:31:49 -0700260 ReportProgress(frac);
261 return true;
262 }
263
264 return false;
265}
266
267void PostinstallRunnerAction::ReportProgress(double frac) {
268 if (!delegate_)
269 return;
Yoshitaka Ishida128936f2018-02-16 18:20:07 +0900270 if (current_partition_ >= partition_weight_.size() || total_weight_ == 0) {
Alex Deymo0d298542016-03-30 18:31:49 -0700271 delegate_->ProgressUpdate(1.);
272 return;
273 }
Alex Deymo44b35672016-04-05 17:57:48 -0700274 if (!std::isfinite(frac) || frac < 0)
Alex Deymo0d298542016-03-30 18:31:49 -0700275 frac = 0;
276 if (frac > 1)
277 frac = 1;
278 double postinst_action_progress =
279 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
280 total_weight_;
281 delegate_->ProgressUpdate(postinst_action_progress);
282}
283
284void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800285 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800286#ifndef __ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800287 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) {
288 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700289 }
Alex Deymod15c5462016-03-09 18:11:12 -0800290#endif // !__ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800291 fs_mount_dir_.clear();
Alex Deymo0d298542016-03-30 18:31:49 -0700292
293 progress_fd_ = -1;
294 if (progress_task_ != MessageLoop::kTaskIdNull) {
295 MessageLoop::current()->CancelTask(progress_task_);
296 progress_task_ = MessageLoop::kTaskIdNull;
297 }
298 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800299}
300
301void PostinstallRunnerAction::CompletePartitionPostinstall(
302 int return_code, const string& output) {
303 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700304 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700305
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800306 if (return_code != 0) {
307 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700308 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700309
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700310 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700311 // This special return code means that we tried to update firmware,
312 // but couldn't because we booted from FW B, and we need to reboot
313 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700314 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700315 }
Don Garrett81018e02013-07-30 18:46:31 -0700316
317 if (return_code == 4) {
318 // This special return code means that we tried to update firmware,
319 // but couldn't because we booted from FW B, and we need to reboot
320 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700321 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700322 }
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700323
324 // If postinstall script for this partition is optional we can ignore the
325 // result.
326 if (install_plan_.partitions[current_partition_].postinstall_optional) {
327 LOG(INFO) << "Ignoring postinstall failure since it is optional";
328 } else {
329 return CompletePostinstall(error_code);
330 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700331 }
Alex Deymo0d298542016-03-30 18:31:49 -0700332 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700333 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700334 ReportProgress(0);
335
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700336 PerformPartitionPostinstall();
337}
338
339void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
340 // We only attempt to mark the new slot as active if all the postinstall
341 // steps succeeded.
Sen Jiang02c49422017-10-31 15:14:11 -0700342 if (error_code == ErrorCode::kSuccess) {
343 if (install_plan_.switch_slot_on_reboot) {
Yifan Hong7b3910a2020-03-24 17:47:32 -0700344 if (!boot_control_->GetDynamicPartitionControl()->FinishUpdate(
345 install_plan_.powerwash_required) ||
Yifan Honge8583622019-11-07 11:36:31 -0800346 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Sen Jiang02c49422017-10-31 15:14:11 -0700347 error_code = ErrorCode::kPostinstallRunnerError;
Tianjie Xud6aa91f2019-11-14 11:55:10 -0800348 } else {
349 // Schedules warm reset on next reboot, ignores the error.
350 hardware_->SetWarmReset(true);
Sen Jiang02c49422017-10-31 15:14:11 -0700351 }
352 } else {
353 error_code = ErrorCode::kUpdatedButNotActive;
354 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700355 }
356
357 ScopedActionCompleter completer(processor_, this);
Alex Deymocbc22742016-03-04 17:53:02 -0800358 completer.set_code(error_code);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700359
Sen Jiang02c49422017-10-31 15:14:11 -0700360 if (error_code != ErrorCode::kSuccess &&
361 error_code != ErrorCode::kUpdatedButNotActive) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700362 LOG(ERROR) << "Postinstall action failed.";
363
Alex Deymofb905d92016-06-03 19:26:58 -0700364 // Undo any changes done to trigger Powerwash.
365 if (powerwash_scheduled_)
366 hardware_->CancelPowerwash();
Don Garrett81018e02013-07-30 18:46:31 -0700367
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800368 return;
369 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700370
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700371 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700372 if (HasOutputPipe()) {
373 SetOutputObject(install_plan_);
374 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000375}
376
Alex Deymod15c5462016-03-09 18:11:12 -0800377void PostinstallRunnerAction::SuspendAction() {
378 if (!current_command_)
379 return;
380 if (kill(current_command_, SIGSTOP) != 0) {
381 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800382 } else {
383 is_current_command_suspended_ = true;
Alex Deymod15c5462016-03-09 18:11:12 -0800384 }
385}
386
387void PostinstallRunnerAction::ResumeAction() {
388 if (!current_command_)
389 return;
390 if (kill(current_command_, SIGCONT) != 0) {
391 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800392 } else {
393 is_current_command_suspended_ = false;
Alex Deymod15c5462016-03-09 18:11:12 -0800394 }
395}
396
397void PostinstallRunnerAction::TerminateProcessing() {
398 if (!current_command_)
399 return;
400 // Calling KillExec() will discard the callback we registered and therefore
401 // the unretained reference to this object.
402 Subprocess::Get().KillExec(current_command_);
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800403
404 // If the command has been suspended, resume it after KillExec() so that the
405 // process can process the SIGTERM sent by KillExec().
406 if (is_current_command_suspended_) {
407 ResumeAction();
408 }
409
Alex Deymod15c5462016-03-09 18:11:12 -0800410 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700411 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800412}
413
adlr@google.com3defe6a2009-12-04 20:57:17 +0000414} // namespace chromeos_update_engine