blob: 894ac7dae82d0a7e6b383ff25d3f2cf98e883e1a [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
Brian Norris7b428f52019-06-26 10:03:35 -0700114 const string mountable_device = partition.target_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700115 if (mountable_device.empty()) {
116 LOG(ERROR) << "Cannot make mountable device from " << partition.target_path;
117 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
118 }
119
120 // Perform post-install for the current_partition_ partition. At this point we
121 // need to call CompletePartitionPostinstall to complete the operation and
122 // cleanup.
Alex Deymo390efed2016-02-18 11:00:40 -0800123#ifdef __ANDROID__
124 fs_mount_dir_ = "/postinstall";
125#else // __ANDROID__
Sen Jiang371615b2016-04-13 15:54:29 -0700126 base::FilePath temp_dir;
127 TEST_AND_RETURN(base::CreateNewTempDirectory("au_postint_mount", &temp_dir));
128 fs_mount_dir_ = temp_dir.value();
Alex Deymo390efed2016-02-18 11:00:40 -0800129#endif // __ANDROID__
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700130
Alex Deymof4116502017-03-22 17:00:31 -0700131 // Double check that the fs_mount_dir is not busy with a previous mounted
132 // filesystem from a previous crashed postinstall step.
133 if (utils::IsMountpoint(fs_mount_dir_)) {
134 LOG(INFO) << "Found previously mounted filesystem at " << fs_mount_dir_;
135 utils::UnmountFilesystem(fs_mount_dir_);
136 }
137
Alex Deymocbc22742016-03-04 17:53:02 -0800138 base::FilePath postinstall_path(partition.postinstall_path);
139 if (postinstall_path.IsAbsolute()) {
140 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
141 "path instead: "
142 << partition.postinstall_path;
143 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
144 }
145
146 string abs_path =
147 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800148 if (!base::StartsWith(
149 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
150 LOG(ERROR) << "Invalid relative postinstall path: "
151 << partition.postinstall_path;
152 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
153 }
154
Alex Deymo5fb356c2016-03-25 18:48:22 -0700155#ifdef __ANDROID__
156 // In Chromium OS, the postinstall step is allowed to write to the block
157 // device on the target image, so we don't mark it as read-only and should
158 // be read-write since we just wrote to it during the update.
159
160 // Mark the block device as read-only before mounting for post-install.
161 if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) {
162 return CompletePartitionPostinstall(
163 1, "Error marking the device " + mountable_device + " read only.");
164 }
165#endif // __ANDROID__
166
Alex Deymo390efed2016-02-18 11:00:40 -0800167 if (!utils::MountFilesystem(mountable_device,
168 fs_mount_dir_,
169 MS_RDONLY,
Alex Deymo14dbd332016-03-01 18:55:54 -0800170 partition.filesystem_type,
171 constants::kPostinstallMountOptions)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700172 return CompletePartitionPostinstall(
173 1, "Error mounting the device " + mountable_device);
174 }
175
Alex Deymo390efed2016-02-18 11:00:40 -0800176 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
177 << abs_path << ") installed on device " << partition.target_path
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700178 << " and mountable device " << mountable_device;
179
Alex Deymo032e7722014-03-25 17:53:56 -0700180 // Logs the file format of the postinstall script we are about to run. This
181 // will help debug when the postinstall script doesn't match the architecture
182 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800183 LOG(INFO) << "Format file for new " << partition.postinstall_path
184 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700185
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800186 // Runs the postinstall script asynchronously to free up the main loop while
187 // it's running.
Alex Deymo0d298542016-03-30 18:31:49 -0700188 vector<string> command = {abs_path};
189#ifdef __ANDROID__
190 // In Brillo and Android, we pass the slot number and status fd.
191 command.push_back(std::to_string(install_plan_.target_slot));
192 command.push_back(std::to_string(kPostinstallStatusFd));
193#else
194 // Chrome OS postinstall expects the target rootfs as the first parameter.
195 command.push_back(partition.target_path);
196#endif // __ANDROID__
197
198 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymod15c5462016-03-09 18:11:12 -0800199 command,
Alex Deymo0d298542016-03-30 18:31:49 -0700200 Subprocess::kRedirectStderrToStdout,
201 {kPostinstallStatusFd},
Alex Deymod15c5462016-03-09 18:11:12 -0800202 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
203 base::Unretained(this)));
204 // Subprocess::Exec should never return a negative process id.
205 CHECK_GE(current_command_, 0);
206
Alex Deymo0d298542016-03-30 18:31:49 -0700207 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700208 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo0d298542016-03-30 18:31:49 -0700209 return;
210 }
211
212 // Monitor the status file descriptor.
213 progress_fd_ =
214 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
215 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
216 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
217 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
218 }
219
220 progress_task_ = MessageLoop::current()->WatchFileDescriptor(
221 FROM_HERE,
222 progress_fd_,
223 MessageLoop::WatchMode::kWatchRead,
224 true,
225 base::Bind(&PostinstallRunnerAction::OnProgressFdReady,
226 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800227}
228
Alex Deymo0d298542016-03-30 18:31:49 -0700229void PostinstallRunnerAction::OnProgressFdReady() {
230 char buf[1024];
231 size_t bytes_read;
232 do {
233 bytes_read = 0;
234 bool eof;
235 bool ok =
236 utils::ReadAll(progress_fd_, buf, arraysize(buf), &bytes_read, &eof);
237 progress_buffer_.append(buf, bytes_read);
238 // Process every line.
239 vector<string> lines = base::SplitString(
240 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
241 if (!lines.empty()) {
242 progress_buffer_ = lines.back();
243 lines.pop_back();
244 for (const auto& line : lines) {
245 ProcessProgressLine(line);
246 }
247 }
248 if (!ok || eof) {
249 // There was either an error or an EOF condition, so we are done watching
250 // the file descriptor.
251 MessageLoop::current()->CancelTask(progress_task_);
252 progress_task_ = MessageLoop::kTaskIdNull;
253 return;
254 }
255 } while (bytes_read);
256}
257
258bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
259 double frac = 0;
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700260 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 &&
261 !std::isnan(frac)) {
Alex Deymo0d298542016-03-30 18:31:49 -0700262 ReportProgress(frac);
263 return true;
264 }
265
266 return false;
267}
268
269void PostinstallRunnerAction::ReportProgress(double frac) {
270 if (!delegate_)
271 return;
Yoshitaka Ishida128936f2018-02-16 18:20:07 +0900272 if (current_partition_ >= partition_weight_.size() || total_weight_ == 0) {
Alex Deymo0d298542016-03-30 18:31:49 -0700273 delegate_->ProgressUpdate(1.);
274 return;
275 }
Alex Deymo44b35672016-04-05 17:57:48 -0700276 if (!std::isfinite(frac) || frac < 0)
Alex Deymo0d298542016-03-30 18:31:49 -0700277 frac = 0;
278 if (frac > 1)
279 frac = 1;
280 double postinst_action_progress =
281 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
282 total_weight_;
283 delegate_->ProgressUpdate(postinst_action_progress);
284}
285
286void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800287 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800288#ifndef __ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800289 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) {
290 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700291 }
Alex Deymod15c5462016-03-09 18:11:12 -0800292#endif // !__ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800293 fs_mount_dir_.clear();
Alex Deymo0d298542016-03-30 18:31:49 -0700294
295 progress_fd_ = -1;
296 if (progress_task_ != MessageLoop::kTaskIdNull) {
297 MessageLoop::current()->CancelTask(progress_task_);
298 progress_task_ = MessageLoop::kTaskIdNull;
299 }
300 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800301}
302
303void PostinstallRunnerAction::CompletePartitionPostinstall(
304 int return_code, const string& output) {
305 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700306 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700307
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800308 if (return_code != 0) {
309 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700310 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700311
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700312 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700313 // This special return code means that we tried to update firmware,
314 // but couldn't because we booted from FW B, and we need to reboot
315 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700316 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700317 }
Don Garrett81018e02013-07-30 18:46:31 -0700318
319 if (return_code == 4) {
320 // This special return code means that we tried to update firmware,
321 // but couldn't because we booted from FW B, and we need to reboot
322 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700323 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700324 }
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700325
326 // If postinstall script for this partition is optional we can ignore the
327 // result.
328 if (install_plan_.partitions[current_partition_].postinstall_optional) {
329 LOG(INFO) << "Ignoring postinstall failure since it is optional";
330 } else {
331 return CompletePostinstall(error_code);
332 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700333 }
Alex Deymo0d298542016-03-30 18:31:49 -0700334 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700335 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700336 ReportProgress(0);
337
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700338 PerformPartitionPostinstall();
339}
340
341void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
342 // We only attempt to mark the new slot as active if all the postinstall
343 // steps succeeded.
Sen Jiang02c49422017-10-31 15:14:11 -0700344 if (error_code == ErrorCode::kSuccess) {
345 if (install_plan_.switch_slot_on_reboot) {
346 if (!boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
347 error_code = ErrorCode::kPostinstallRunnerError;
348 }
349 } else {
350 error_code = ErrorCode::kUpdatedButNotActive;
351 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700352 }
353
354 ScopedActionCompleter completer(processor_, this);
Alex Deymocbc22742016-03-04 17:53:02 -0800355 completer.set_code(error_code);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700356
Sen Jiang02c49422017-10-31 15:14:11 -0700357 if (error_code != ErrorCode::kSuccess &&
358 error_code != ErrorCode::kUpdatedButNotActive) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700359 LOG(ERROR) << "Postinstall action failed.";
360
Alex Deymofb905d92016-06-03 19:26:58 -0700361 // Undo any changes done to trigger Powerwash.
362 if (powerwash_scheduled_)
363 hardware_->CancelPowerwash();
Don Garrett81018e02013-07-30 18:46:31 -0700364
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800365 return;
366 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700367
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700368 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700369 if (HasOutputPipe()) {
370 SetOutputObject(install_plan_);
371 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000372}
373
Alex Deymod15c5462016-03-09 18:11:12 -0800374void PostinstallRunnerAction::SuspendAction() {
375 if (!current_command_)
376 return;
377 if (kill(current_command_, SIGSTOP) != 0) {
378 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800379 } else {
380 is_current_command_suspended_ = true;
Alex Deymod15c5462016-03-09 18:11:12 -0800381 }
382}
383
384void PostinstallRunnerAction::ResumeAction() {
385 if (!current_command_)
386 return;
387 if (kill(current_command_, SIGCONT) != 0) {
388 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800389 } else {
390 is_current_command_suspended_ = false;
Alex Deymod15c5462016-03-09 18:11:12 -0800391 }
392}
393
394void PostinstallRunnerAction::TerminateProcessing() {
395 if (!current_command_)
396 return;
397 // Calling KillExec() will discard the callback we registered and therefore
398 // the unretained reference to this object.
399 Subprocess::Get().KillExec(current_command_);
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800400
401 // If the command has been suspended, resume it after KillExec() so that the
402 // process can process the SIGTERM sent by KillExec().
403 if (is_current_command_suspended_) {
404 ResumeAction();
405 }
406
Alex Deymod15c5462016-03-09 18:11:12 -0800407 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700408 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800409}
410
adlr@google.com3defe6a2009-12-04 20:57:17 +0000411} // namespace chromeos_update_engine