blob: 9ecda488eb97c677e5676ca790f3279d8587ba3a [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
52using std::string;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070053using std::vector;
adlr@google.com3defe6a2009-12-04 20:57:17 +000054
adlr@google.com3defe6a2009-12-04 20:57:17 +000055void PostinstallRunnerAction::PerformAction() {
56 CHECK(HasInputObject());
Chris Sosad317e402013-06-12 13:47:09 -070057 install_plan_ = GetInputObject();
Darin Petkov6f03a3b2010-11-10 14:27:14 -080058
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -080059 // We always powerwash when rolling back, however policy can determine
60 // if this is a full/normal powerwash, or a special rollback powerwash
61 // that retains a small amount of system state such as enrollment and
62 // network configuration. In both cases all user accounts are deleted.
Marton Hunyady199152d2018-05-07 19:08:48 +020063 if (install_plan_.powerwash_required || install_plan_.is_rollback) {
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -080064 bool save_rollback_data =
65 install_plan_.is_rollback && install_plan_.rollback_data_save_requested;
66 if (hardware_->SchedulePowerwash(save_rollback_data)) {
Alex Deymofb905d92016-06-03 19:26:58 -070067 powerwash_scheduled_ = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070068 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070069 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070070 }
71 }
72
Alex Deymo0d298542016-03-30 18:31:49 -070073 // Initialize all the partition weights.
74 partition_weight_.resize(install_plan_.partitions.size());
75 total_weight_ = 0;
76 for (size_t i = 0; i < install_plan_.partitions.size(); ++i) {
77 // TODO(deymo): This code sets the weight to all the postinstall commands,
78 // but we could remember how long they took in the past and use those
79 // values.
80 partition_weight_[i] = install_plan_.partitions[i].run_postinstall;
81 total_weight_ += partition_weight_[i];
82 }
83 accumulated_weight_ = 0;
84 ReportProgress(0);
85
Alex Deymoe5e5fe92015-10-05 09:28:19 -070086 PerformPartitionPostinstall();
87}
88
89void PostinstallRunnerAction::PerformPartitionPostinstall() {
Sen Jiang02c49422017-10-31 15:14:11 -070090 if (!install_plan_.run_post_install) {
91 LOG(INFO) << "Skipping post-install according to install plan.";
92 return CompletePostinstall(ErrorCode::kSuccess);
93 }
94
Alex Deymo390efed2016-02-18 11:00:40 -080095 if (install_plan_.download_url.empty()) {
96 LOG(INFO) << "Skipping post-install during rollback";
97 return CompletePostinstall(ErrorCode::kSuccess);
98 }
99
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700100 // Skip all the partitions that don't have a post-install step.
101 while (current_partition_ < install_plan_.partitions.size() &&
102 !install_plan_.partitions[current_partition_].run_postinstall) {
103 VLOG(1) << "Skipping post-install on partition "
104 << install_plan_.partitions[current_partition_].name;
105 current_partition_++;
106 }
107 if (current_partition_ == install_plan_.partitions.size())
108 return CompletePostinstall(ErrorCode::kSuccess);
109
110 const InstallPlan::Partition& partition =
111 install_plan_.partitions[current_partition_];
112
Brian Norris7b428f52019-06-26 10:03:35 -0700113 const string mountable_device = partition.target_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700114 if (mountable_device.empty()) {
115 LOG(ERROR) << "Cannot make mountable device from " << partition.target_path;
116 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
117 }
118
119 // Perform post-install for the current_partition_ partition. At this point we
120 // need to call CompletePartitionPostinstall to complete the operation and
121 // cleanup.
Alex Deymo390efed2016-02-18 11:00:40 -0800122#ifdef __ANDROID__
123 fs_mount_dir_ = "/postinstall";
124#else // __ANDROID__
Sen Jiang371615b2016-04-13 15:54:29 -0700125 base::FilePath temp_dir;
126 TEST_AND_RETURN(base::CreateNewTempDirectory("au_postint_mount", &temp_dir));
127 fs_mount_dir_ = temp_dir.value();
Alex Deymo390efed2016-02-18 11:00:40 -0800128#endif // __ANDROID__
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700129
Alex Deymof4116502017-03-22 17:00:31 -0700130 // Double check that the fs_mount_dir is not busy with a previous mounted
131 // filesystem from a previous crashed postinstall step.
132 if (utils::IsMountpoint(fs_mount_dir_)) {
133 LOG(INFO) << "Found previously mounted filesystem at " << fs_mount_dir_;
134 utils::UnmountFilesystem(fs_mount_dir_);
135 }
136
Alex Deymocbc22742016-03-04 17:53:02 -0800137 base::FilePath postinstall_path(partition.postinstall_path);
138 if (postinstall_path.IsAbsolute()) {
139 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
140 "path instead: "
141 << partition.postinstall_path;
142 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
143 }
144
145 string abs_path =
146 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800147 if (!base::StartsWith(
148 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
149 LOG(ERROR) << "Invalid relative postinstall path: "
150 << partition.postinstall_path;
151 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
152 }
153
Alex Deymo5fb356c2016-03-25 18:48:22 -0700154#ifdef __ANDROID__
155 // In Chromium OS, the postinstall step is allowed to write to the block
156 // device on the target image, so we don't mark it as read-only and should
157 // be read-write since we just wrote to it during the update.
158
159 // Mark the block device as read-only before mounting for post-install.
160 if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) {
161 return CompletePartitionPostinstall(
162 1, "Error marking the device " + mountable_device + " read only.");
163 }
164#endif // __ANDROID__
165
Alex Deymo390efed2016-02-18 11:00:40 -0800166 if (!utils::MountFilesystem(mountable_device,
167 fs_mount_dir_,
168 MS_RDONLY,
Alex Deymo14dbd332016-03-01 18:55:54 -0800169 partition.filesystem_type,
170 constants::kPostinstallMountOptions)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700171 return CompletePartitionPostinstall(
172 1, "Error mounting the device " + mountable_device);
173 }
174
Alex Deymo390efed2016-02-18 11:00:40 -0800175 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
176 << abs_path << ") installed on device " << partition.target_path
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700177 << " and mountable device " << mountable_device;
178
Alex Deymo032e7722014-03-25 17:53:56 -0700179 // Logs the file format of the postinstall script we are about to run. This
180 // will help debug when the postinstall script doesn't match the architecture
181 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800182 LOG(INFO) << "Format file for new " << partition.postinstall_path
183 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700184
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800185 // Runs the postinstall script asynchronously to free up the main loop while
186 // it's running.
Alex Deymo0d298542016-03-30 18:31:49 -0700187 vector<string> command = {abs_path};
188#ifdef __ANDROID__
189 // In Brillo and Android, we pass the slot number and status fd.
190 command.push_back(std::to_string(install_plan_.target_slot));
191 command.push_back(std::to_string(kPostinstallStatusFd));
192#else
193 // Chrome OS postinstall expects the target rootfs as the first parameter.
194 command.push_back(partition.target_path);
195#endif // __ANDROID__
196
197 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymod15c5462016-03-09 18:11:12 -0800198 command,
Alex Deymo0d298542016-03-30 18:31:49 -0700199 Subprocess::kRedirectStderrToStdout,
200 {kPostinstallStatusFd},
Alex Deymod15c5462016-03-09 18:11:12 -0800201 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
202 base::Unretained(this)));
203 // Subprocess::Exec should never return a negative process id.
204 CHECK_GE(current_command_, 0);
205
Alex Deymo0d298542016-03-30 18:31:49 -0700206 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700207 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo0d298542016-03-30 18:31:49 -0700208 return;
209 }
210
211 // Monitor the status file descriptor.
212 progress_fd_ =
213 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
214 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
215 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
216 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
217 }
218
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900219 progress_controller_ = base::FileDescriptorWatcher::WatchReadable(
Alex Deymo0d298542016-03-30 18:31:49 -0700220 progress_fd_,
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900221 base::BindRepeating(&PostinstallRunnerAction::OnProgressFdReady,
222 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800223}
224
Alex Deymo0d298542016-03-30 18:31:49 -0700225void PostinstallRunnerAction::OnProgressFdReady() {
226 char buf[1024];
227 size_t bytes_read;
228 do {
229 bytes_read = 0;
230 bool eof;
231 bool ok =
232 utils::ReadAll(progress_fd_, buf, arraysize(buf), &bytes_read, &eof);
233 progress_buffer_.append(buf, bytes_read);
234 // Process every line.
235 vector<string> lines = base::SplitString(
236 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
237 if (!lines.empty()) {
238 progress_buffer_ = lines.back();
239 lines.pop_back();
240 for (const auto& line : lines) {
241 ProcessProgressLine(line);
242 }
243 }
244 if (!ok || eof) {
245 // There was either an error or an EOF condition, so we are done watching
246 // the file descriptor.
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900247 progress_controller_.reset();
Alex Deymo0d298542016-03-30 18:31:49 -0700248 return;
249 }
250 } while (bytes_read);
251}
252
253bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
254 double frac = 0;
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700255 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 &&
256 !std::isnan(frac)) {
Alex Deymo0d298542016-03-30 18:31:49 -0700257 ReportProgress(frac);
258 return true;
259 }
260
261 return false;
262}
263
264void PostinstallRunnerAction::ReportProgress(double frac) {
265 if (!delegate_)
266 return;
Yoshitaka Ishida128936f2018-02-16 18:20:07 +0900267 if (current_partition_ >= partition_weight_.size() || total_weight_ == 0) {
Alex Deymo0d298542016-03-30 18:31:49 -0700268 delegate_->ProgressUpdate(1.);
269 return;
270 }
Alex Deymo44b35672016-04-05 17:57:48 -0700271 if (!std::isfinite(frac) || frac < 0)
Alex Deymo0d298542016-03-30 18:31:49 -0700272 frac = 0;
273 if (frac > 1)
274 frac = 1;
275 double postinst_action_progress =
276 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
277 total_weight_;
278 delegate_->ProgressUpdate(postinst_action_progress);
279}
280
281void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800282 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800283#ifndef __ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800284 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) {
285 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700286 }
Alex Deymod15c5462016-03-09 18:11:12 -0800287#endif // !__ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800288 fs_mount_dir_.clear();
Alex Deymo0d298542016-03-30 18:31:49 -0700289
290 progress_fd_ = -1;
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900291 progress_controller_.reset();
Alex Deymo0d298542016-03-30 18:31:49 -0700292 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800293}
294
295void PostinstallRunnerAction::CompletePartitionPostinstall(
296 int return_code, const string& output) {
297 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700298 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700299
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800300 if (return_code != 0) {
301 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700302 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700303
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700304 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700305 // This special return code means that we tried to update firmware,
306 // but couldn't because we booted from FW B, and we need to reboot
307 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700308 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700309 }
Don Garrett81018e02013-07-30 18:46:31 -0700310
311 if (return_code == 4) {
312 // This special return code means that we tried to update firmware,
313 // but couldn't because we booted from FW B, and we need to reboot
314 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700315 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700316 }
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700317
318 // If postinstall script for this partition is optional we can ignore the
319 // result.
320 if (install_plan_.partitions[current_partition_].postinstall_optional) {
321 LOG(INFO) << "Ignoring postinstall failure since it is optional";
322 } else {
323 return CompletePostinstall(error_code);
324 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700325 }
Alex Deymo0d298542016-03-30 18:31:49 -0700326 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700327 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700328 ReportProgress(0);
329
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700330 PerformPartitionPostinstall();
331}
332
333void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
334 // We only attempt to mark the new slot as active if all the postinstall
335 // steps succeeded.
Sen Jiang02c49422017-10-31 15:14:11 -0700336 if (error_code == ErrorCode::kSuccess) {
337 if (install_plan_.switch_slot_on_reboot) {
338 if (!boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
339 error_code = ErrorCode::kPostinstallRunnerError;
340 }
341 } else {
342 error_code = ErrorCode::kUpdatedButNotActive;
343 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700344 }
345
346 ScopedActionCompleter completer(processor_, this);
Alex Deymocbc22742016-03-04 17:53:02 -0800347 completer.set_code(error_code);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700348
Sen Jiang02c49422017-10-31 15:14:11 -0700349 if (error_code != ErrorCode::kSuccess &&
350 error_code != ErrorCode::kUpdatedButNotActive) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700351 LOG(ERROR) << "Postinstall action failed.";
352
Alex Deymofb905d92016-06-03 19:26:58 -0700353 // Undo any changes done to trigger Powerwash.
354 if (powerwash_scheduled_)
355 hardware_->CancelPowerwash();
Don Garrett81018e02013-07-30 18:46:31 -0700356
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800357 return;
358 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700359
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700360 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700361 if (HasOutputPipe()) {
362 SetOutputObject(install_plan_);
363 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000364}
365
Alex Deymod15c5462016-03-09 18:11:12 -0800366void PostinstallRunnerAction::SuspendAction() {
367 if (!current_command_)
368 return;
369 if (kill(current_command_, SIGSTOP) != 0) {
370 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800371 } else {
372 is_current_command_suspended_ = true;
Alex Deymod15c5462016-03-09 18:11:12 -0800373 }
374}
375
376void PostinstallRunnerAction::ResumeAction() {
377 if (!current_command_)
378 return;
379 if (kill(current_command_, SIGCONT) != 0) {
380 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800381 } else {
382 is_current_command_suspended_ = false;
Alex Deymod15c5462016-03-09 18:11:12 -0800383 }
384}
385
386void PostinstallRunnerAction::TerminateProcessing() {
387 if (!current_command_)
388 return;
389 // Calling KillExec() will discard the callback we registered and therefore
390 // the unretained reference to this object.
391 Subprocess::Get().KillExec(current_command_);
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800392
393 // If the command has been suspended, resume it after KillExec() so that the
394 // process can process the SIGTERM sent by KillExec().
395 if (is_current_command_suspended_) {
396 ResumeAction();
397 }
398
Alex Deymod15c5462016-03-09 18:11:12 -0800399 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700400 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800401}
402
adlr@google.com3defe6a2009-12-04 20:57:17 +0000403} // namespace chromeos_update_engine