blob: db1ec3c89a801634b7e225c7c8a9f36b1c8f554a [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 Deymoe5e5fe92015-10-05 09:28:19 -070026#include <base/files/file_path.h>
27#include <base/files/file_util.h>
Alex Deymod15c5462016-03-09 18:11:12 -080028#include <base/logging.h>
Alex Deymo0d298542016-03-30 18:31:49 -070029#include <base/strings/string_split.h>
Alex Deymo390efed2016-02-18 11:00:40 -080030#include <base/strings/string_util.h>
Alex Deymo461b2592015-07-24 20:10:52 -070031
Alex Deymo39910dc2015-11-09 17:04:30 -080032#include "update_engine/common/action_processor.h"
Alex Deymob15a0b82015-11-25 20:30:40 -030033#include "update_engine/common/boot_control_interface.h"
Alex Deymo14dbd332016-03-01 18:55:54 -080034#include "update_engine/common/platform_constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080035#include "update_engine/common/subprocess.h"
36#include "update_engine/common/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000037
Alex Deymo0d298542016-03-30 18:31:49 -070038namespace {
39
40// The file descriptor number from the postinstall program's perspective where
41// it can report status updates. This can be any number greater than 2 (stderr),
42// but must be kept in sync with the "bin/postinst_progress" defined in the
43// sample_images.sh file.
44const int kPostinstallStatusFd = 3;
45
46} // namespace
47
adlr@google.com3defe6a2009-12-04 20:57:17 +000048namespace chromeos_update_engine {
49
Alex Deymo0d298542016-03-30 18:31:49 -070050using brillo::MessageLoop;
adlr@google.com3defe6a2009-12-04 20:57:17 +000051using std::string;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070052using std::vector;
adlr@google.com3defe6a2009-12-04 20:57:17 +000053
adlr@google.com3defe6a2009-12-04 20:57:17 +000054void PostinstallRunnerAction::PerformAction() {
55 CHECK(HasInputObject());
Chris Sosad317e402013-06-12 13:47:09 -070056 install_plan_ = GetInputObject();
Darin Petkov6f03a3b2010-11-10 14:27:14 -080057
Chris Sosad317e402013-06-12 13:47:09 -070058 if (install_plan_.powerwash_required) {
Gilad Arnold30dedd82013-07-03 06:19:09 -070059 if (utils::CreatePowerwashMarkerFile(powerwash_marker_file_)) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070060 powerwash_marker_created_ = true;
61 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070062 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070063 }
64 }
65
Alex Deymo0d298542016-03-30 18:31:49 -070066 // Initialize all the partition weights.
67 partition_weight_.resize(install_plan_.partitions.size());
68 total_weight_ = 0;
69 for (size_t i = 0; i < install_plan_.partitions.size(); ++i) {
70 // TODO(deymo): This code sets the weight to all the postinstall commands,
71 // but we could remember how long they took in the past and use those
72 // values.
73 partition_weight_[i] = install_plan_.partitions[i].run_postinstall;
74 total_weight_ += partition_weight_[i];
75 }
76 accumulated_weight_ = 0;
77 ReportProgress(0);
78
Alex Deymoe5e5fe92015-10-05 09:28:19 -070079 PerformPartitionPostinstall();
80}
81
82void PostinstallRunnerAction::PerformPartitionPostinstall() {
Alex Deymo390efed2016-02-18 11:00:40 -080083 if (install_plan_.download_url.empty()) {
84 LOG(INFO) << "Skipping post-install during rollback";
85 return CompletePostinstall(ErrorCode::kSuccess);
86 }
87
Alex Deymoe5e5fe92015-10-05 09:28:19 -070088 // Skip all the partitions that don't have a post-install step.
89 while (current_partition_ < install_plan_.partitions.size() &&
90 !install_plan_.partitions[current_partition_].run_postinstall) {
91 VLOG(1) << "Skipping post-install on partition "
92 << install_plan_.partitions[current_partition_].name;
93 current_partition_++;
94 }
95 if (current_partition_ == install_plan_.partitions.size())
96 return CompletePostinstall(ErrorCode::kSuccess);
97
98 const InstallPlan::Partition& partition =
99 install_plan_.partitions[current_partition_];
100
101 const string mountable_device =
102 utils::MakePartitionNameForMount(partition.target_path);
103 if (mountable_device.empty()) {
104 LOG(ERROR) << "Cannot make mountable device from " << partition.target_path;
105 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
106 }
107
108 // Perform post-install for the current_partition_ partition. At this point we
109 // need to call CompletePartitionPostinstall to complete the operation and
110 // cleanup.
Alex Deymo390efed2016-02-18 11:00:40 -0800111#ifdef __ANDROID__
112 fs_mount_dir_ = "/postinstall";
113#else // __ANDROID__
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700114 TEST_AND_RETURN(
Alex Deymo390efed2016-02-18 11:00:40 -0800115 utils::MakeTempDirectory("au_postint_mount.XXXXXX", &fs_mount_dir_));
116#endif // __ANDROID__
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700117
Alex Deymocbc22742016-03-04 17:53:02 -0800118 base::FilePath postinstall_path(partition.postinstall_path);
119 if (postinstall_path.IsAbsolute()) {
120 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
121 "path instead: "
122 << partition.postinstall_path;
123 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
124 }
125
126 string abs_path =
127 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800128 if (!base::StartsWith(
129 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
130 LOG(ERROR) << "Invalid relative postinstall path: "
131 << partition.postinstall_path;
132 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
133 }
134
135 if (!utils::MountFilesystem(mountable_device,
136 fs_mount_dir_,
137 MS_RDONLY,
Alex Deymo14dbd332016-03-01 18:55:54 -0800138 partition.filesystem_type,
139 constants::kPostinstallMountOptions)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700140 return CompletePartitionPostinstall(
141 1, "Error mounting the device " + mountable_device);
142 }
143
Alex Deymo390efed2016-02-18 11:00:40 -0800144 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
145 << abs_path << ") installed on device " << partition.target_path
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700146 << " and mountable device " << mountable_device;
147
Alex Deymo032e7722014-03-25 17:53:56 -0700148 // Logs the file format of the postinstall script we are about to run. This
149 // will help debug when the postinstall script doesn't match the architecture
150 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800151 LOG(INFO) << "Format file for new " << partition.postinstall_path
152 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700153
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800154 // Runs the postinstall script asynchronously to free up the main loop while
155 // it's running.
Alex Deymo0d298542016-03-30 18:31:49 -0700156 vector<string> command = {abs_path};
157#ifdef __ANDROID__
158 // In Brillo and Android, we pass the slot number and status fd.
159 command.push_back(std::to_string(install_plan_.target_slot));
160 command.push_back(std::to_string(kPostinstallStatusFd));
161#else
162 // Chrome OS postinstall expects the target rootfs as the first parameter.
163 command.push_back(partition.target_path);
164#endif // __ANDROID__
165
166 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymod15c5462016-03-09 18:11:12 -0800167 command,
Alex Deymo0d298542016-03-30 18:31:49 -0700168 Subprocess::kRedirectStderrToStdout,
169 {kPostinstallStatusFd},
Alex Deymod15c5462016-03-09 18:11:12 -0800170 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
171 base::Unretained(this)));
172 // Subprocess::Exec should never return a negative process id.
173 CHECK_GE(current_command_, 0);
174
Alex Deymo0d298542016-03-30 18:31:49 -0700175 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700176 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo0d298542016-03-30 18:31:49 -0700177 return;
178 }
179
180 // Monitor the status file descriptor.
181 progress_fd_ =
182 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
183 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
184 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
185 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
186 }
187
188 progress_task_ = MessageLoop::current()->WatchFileDescriptor(
189 FROM_HERE,
190 progress_fd_,
191 MessageLoop::WatchMode::kWatchRead,
192 true,
193 base::Bind(&PostinstallRunnerAction::OnProgressFdReady,
194 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800195}
196
Alex Deymo0d298542016-03-30 18:31:49 -0700197void PostinstallRunnerAction::OnProgressFdReady() {
198 char buf[1024];
199 size_t bytes_read;
200 do {
201 bytes_read = 0;
202 bool eof;
203 bool ok =
204 utils::ReadAll(progress_fd_, buf, arraysize(buf), &bytes_read, &eof);
205 progress_buffer_.append(buf, bytes_read);
206 // Process every line.
207 vector<string> lines = base::SplitString(
208 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
209 if (!lines.empty()) {
210 progress_buffer_ = lines.back();
211 lines.pop_back();
212 for (const auto& line : lines) {
213 ProcessProgressLine(line);
214 }
215 }
216 if (!ok || eof) {
217 // There was either an error or an EOF condition, so we are done watching
218 // the file descriptor.
219 MessageLoop::current()->CancelTask(progress_task_);
220 progress_task_ = MessageLoop::kTaskIdNull;
221 return;
222 }
223 } while (bytes_read);
224}
225
226bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
227 double frac = 0;
228 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1) {
229 ReportProgress(frac);
230 return true;
231 }
232
233 return false;
234}
235
236void PostinstallRunnerAction::ReportProgress(double frac) {
237 if (!delegate_)
238 return;
239 if (current_partition_ >= partition_weight_.size()) {
240 delegate_->ProgressUpdate(1.);
241 return;
242 }
243 if (!isfinite(frac) || frac < 0)
244 frac = 0;
245 if (frac > 1)
246 frac = 1;
247 double postinst_action_progress =
248 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
249 total_weight_;
250 delegate_->ProgressUpdate(postinst_action_progress);
251}
252
253void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800254 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800255#ifndef __ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800256 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) {
257 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700258 }
Alex Deymod15c5462016-03-09 18:11:12 -0800259#endif // !__ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800260 fs_mount_dir_.clear();
Alex Deymo0d298542016-03-30 18:31:49 -0700261
262 progress_fd_ = -1;
263 if (progress_task_ != MessageLoop::kTaskIdNull) {
264 MessageLoop::current()->CancelTask(progress_task_);
265 progress_task_ = MessageLoop::kTaskIdNull;
266 }
267 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800268}
269
270void PostinstallRunnerAction::CompletePartitionPostinstall(
271 int return_code, const string& output) {
272 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700273 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700274
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800275 if (return_code != 0) {
276 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700277 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700278
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700279 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700280 // This special return code means that we tried to update firmware,
281 // but couldn't because we booted from FW B, and we need to reboot
282 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700283 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700284 }
Don Garrett81018e02013-07-30 18:46:31 -0700285
286 if (return_code == 4) {
287 // This special return code means that we tried to update firmware,
288 // but couldn't because we booted from FW B, and we need to reboot
289 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700290 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700291 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700292 return CompletePostinstall(error_code);
293 }
Alex Deymo0d298542016-03-30 18:31:49 -0700294 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700295 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700296 ReportProgress(0);
297
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700298 PerformPartitionPostinstall();
299}
300
301void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
302 // We only attempt to mark the new slot as active if all the postinstall
303 // steps succeeded.
304 if (error_code == ErrorCode::kSuccess &&
Alex Deymob15a0b82015-11-25 20:30:40 -0300305 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700306 error_code = ErrorCode::kPostinstallRunnerError;
307 }
308
309 ScopedActionCompleter completer(processor_, this);
Alex Deymocbc22742016-03-04 17:53:02 -0800310 completer.set_code(error_code);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700311
312 if (error_code != ErrorCode::kSuccess) {
313 LOG(ERROR) << "Postinstall action failed.";
314
315 // Undo any changes done to trigger Powerwash using clobber-state.
316 if (powerwash_marker_created_)
317 utils::DeletePowerwashMarkerFile(powerwash_marker_file_);
Don Garrett81018e02013-07-30 18:46:31 -0700318
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800319 return;
320 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700321
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700322 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700323 if (HasOutputPipe()) {
324 SetOutputObject(install_plan_);
325 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000326}
327
Alex Deymod15c5462016-03-09 18:11:12 -0800328void PostinstallRunnerAction::SuspendAction() {
329 if (!current_command_)
330 return;
331 if (kill(current_command_, SIGSTOP) != 0) {
332 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
333 }
334}
335
336void PostinstallRunnerAction::ResumeAction() {
337 if (!current_command_)
338 return;
339 if (kill(current_command_, SIGCONT) != 0) {
340 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
341 }
342}
343
344void PostinstallRunnerAction::TerminateProcessing() {
345 if (!current_command_)
346 return;
347 // Calling KillExec() will discard the callback we registered and therefore
348 // the unretained reference to this object.
349 Subprocess::Get().KillExec(current_command_);
350 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700351 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800352}
353
adlr@google.com3defe6a2009-12-04 20:57:17 +0000354} // namespace chromeos_update_engine