blob: a1b6f2571e035e15c5dc5ceb2cb66452c3d8a7d7 [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
Chris Sosad317e402013-06-12 13:47:09 -070060 if (install_plan_.powerwash_required) {
Alex Deymofb905d92016-06-03 19:26:58 -070061 if (hardware_->SchedulePowerwash()) {
62 powerwash_scheduled_ = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070063 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070064 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070065 }
66 }
67
Alex Deymo0d298542016-03-30 18:31:49 -070068 // Initialize all the partition weights.
69 partition_weight_.resize(install_plan_.partitions.size());
70 total_weight_ = 0;
71 for (size_t i = 0; i < install_plan_.partitions.size(); ++i) {
72 // TODO(deymo): This code sets the weight to all the postinstall commands,
73 // but we could remember how long they took in the past and use those
74 // values.
75 partition_weight_[i] = install_plan_.partitions[i].run_postinstall;
76 total_weight_ += partition_weight_[i];
77 }
78 accumulated_weight_ = 0;
79 ReportProgress(0);
80
Alex Deymoe5e5fe92015-10-05 09:28:19 -070081 PerformPartitionPostinstall();
82}
83
84void PostinstallRunnerAction::PerformPartitionPostinstall() {
Alex Deymo390efed2016-02-18 11:00:40 -080085 if (install_plan_.download_url.empty()) {
86 LOG(INFO) << "Skipping post-install during rollback";
87 return CompletePostinstall(ErrorCode::kSuccess);
88 }
89
Alex Deymoe5e5fe92015-10-05 09:28:19 -070090 // Skip all the partitions that don't have a post-install step.
91 while (current_partition_ < install_plan_.partitions.size() &&
92 !install_plan_.partitions[current_partition_].run_postinstall) {
93 VLOG(1) << "Skipping post-install on partition "
94 << install_plan_.partitions[current_partition_].name;
95 current_partition_++;
96 }
97 if (current_partition_ == install_plan_.partitions.size())
98 return CompletePostinstall(ErrorCode::kSuccess);
99
100 const InstallPlan::Partition& partition =
101 install_plan_.partitions[current_partition_];
102
103 const string mountable_device =
104 utils::MakePartitionNameForMount(partition.target_path);
105 if (mountable_device.empty()) {
106 LOG(ERROR) << "Cannot make mountable device from " << partition.target_path;
107 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
108 }
109
110 // Perform post-install for the current_partition_ partition. At this point we
111 // need to call CompletePartitionPostinstall to complete the operation and
112 // cleanup.
Alex Deymo390efed2016-02-18 11:00:40 -0800113#ifdef __ANDROID__
114 fs_mount_dir_ = "/postinstall";
115#else // __ANDROID__
Sen Jiang371615b2016-04-13 15:54:29 -0700116 base::FilePath temp_dir;
117 TEST_AND_RETURN(base::CreateNewTempDirectory("au_postint_mount", &temp_dir));
118 fs_mount_dir_ = temp_dir.value();
Alex Deymo390efed2016-02-18 11:00:40 -0800119#endif // __ANDROID__
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700120
Alex Deymocbc22742016-03-04 17:53:02 -0800121 base::FilePath postinstall_path(partition.postinstall_path);
122 if (postinstall_path.IsAbsolute()) {
123 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
124 "path instead: "
125 << partition.postinstall_path;
126 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
127 }
128
129 string abs_path =
130 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800131 if (!base::StartsWith(
132 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
133 LOG(ERROR) << "Invalid relative postinstall path: "
134 << partition.postinstall_path;
135 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
136 }
137
Alex Deymo5fb356c2016-03-25 18:48:22 -0700138#ifdef __ANDROID__
139 // In Chromium OS, the postinstall step is allowed to write to the block
140 // device on the target image, so we don't mark it as read-only and should
141 // be read-write since we just wrote to it during the update.
142
143 // Mark the block device as read-only before mounting for post-install.
144 if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) {
145 return CompletePartitionPostinstall(
146 1, "Error marking the device " + mountable_device + " read only.");
147 }
148#endif // __ANDROID__
149
Alex Deymo390efed2016-02-18 11:00:40 -0800150 if (!utils::MountFilesystem(mountable_device,
151 fs_mount_dir_,
152 MS_RDONLY,
Alex Deymo14dbd332016-03-01 18:55:54 -0800153 partition.filesystem_type,
154 constants::kPostinstallMountOptions)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700155 return CompletePartitionPostinstall(
156 1, "Error mounting the device " + mountable_device);
157 }
158
Alex Deymo390efed2016-02-18 11:00:40 -0800159 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
160 << abs_path << ") installed on device " << partition.target_path
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700161 << " and mountable device " << mountable_device;
162
Alex Deymo032e7722014-03-25 17:53:56 -0700163 // Logs the file format of the postinstall script we are about to run. This
164 // will help debug when the postinstall script doesn't match the architecture
165 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800166 LOG(INFO) << "Format file for new " << partition.postinstall_path
167 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700168
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800169 // Runs the postinstall script asynchronously to free up the main loop while
170 // it's running.
Alex Deymo0d298542016-03-30 18:31:49 -0700171 vector<string> command = {abs_path};
172#ifdef __ANDROID__
173 // In Brillo and Android, we pass the slot number and status fd.
174 command.push_back(std::to_string(install_plan_.target_slot));
175 command.push_back(std::to_string(kPostinstallStatusFd));
176#else
177 // Chrome OS postinstall expects the target rootfs as the first parameter.
178 command.push_back(partition.target_path);
179#endif // __ANDROID__
180
181 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymod15c5462016-03-09 18:11:12 -0800182 command,
Alex Deymo0d298542016-03-30 18:31:49 -0700183 Subprocess::kRedirectStderrToStdout,
184 {kPostinstallStatusFd},
Alex Deymod15c5462016-03-09 18:11:12 -0800185 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
186 base::Unretained(this)));
187 // Subprocess::Exec should never return a negative process id.
188 CHECK_GE(current_command_, 0);
189
Alex Deymo0d298542016-03-30 18:31:49 -0700190 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700191 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo0d298542016-03-30 18:31:49 -0700192 return;
193 }
194
195 // Monitor the status file descriptor.
196 progress_fd_ =
197 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
198 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
199 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
200 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
201 }
202
203 progress_task_ = MessageLoop::current()->WatchFileDescriptor(
204 FROM_HERE,
205 progress_fd_,
206 MessageLoop::WatchMode::kWatchRead,
207 true,
208 base::Bind(&PostinstallRunnerAction::OnProgressFdReady,
209 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800210}
211
Alex Deymo0d298542016-03-30 18:31:49 -0700212void PostinstallRunnerAction::OnProgressFdReady() {
213 char buf[1024];
214 size_t bytes_read;
215 do {
216 bytes_read = 0;
217 bool eof;
218 bool ok =
219 utils::ReadAll(progress_fd_, buf, arraysize(buf), &bytes_read, &eof);
220 progress_buffer_.append(buf, bytes_read);
221 // Process every line.
222 vector<string> lines = base::SplitString(
223 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
224 if (!lines.empty()) {
225 progress_buffer_ = lines.back();
226 lines.pop_back();
227 for (const auto& line : lines) {
228 ProcessProgressLine(line);
229 }
230 }
231 if (!ok || eof) {
232 // There was either an error or an EOF condition, so we are done watching
233 // the file descriptor.
234 MessageLoop::current()->CancelTask(progress_task_);
235 progress_task_ = MessageLoop::kTaskIdNull;
236 return;
237 }
238 } while (bytes_read);
239}
240
241bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
242 double frac = 0;
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700243 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 &&
244 !std::isnan(frac)) {
Alex Deymo0d298542016-03-30 18:31:49 -0700245 ReportProgress(frac);
246 return true;
247 }
248
249 return false;
250}
251
252void PostinstallRunnerAction::ReportProgress(double frac) {
253 if (!delegate_)
254 return;
255 if (current_partition_ >= partition_weight_.size()) {
256 delegate_->ProgressUpdate(1.);
257 return;
258 }
Alex Deymo44b35672016-04-05 17:57:48 -0700259 if (!std::isfinite(frac) || frac < 0)
Alex Deymo0d298542016-03-30 18:31:49 -0700260 frac = 0;
261 if (frac > 1)
262 frac = 1;
263 double postinst_action_progress =
264 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
265 total_weight_;
266 delegate_->ProgressUpdate(postinst_action_progress);
267}
268
269void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800270 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800271#ifndef __ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800272 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) {
273 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700274 }
Alex Deymod15c5462016-03-09 18:11:12 -0800275#endif // !__ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800276 fs_mount_dir_.clear();
Alex Deymo0d298542016-03-30 18:31:49 -0700277
278 progress_fd_ = -1;
279 if (progress_task_ != MessageLoop::kTaskIdNull) {
280 MessageLoop::current()->CancelTask(progress_task_);
281 progress_task_ = MessageLoop::kTaskIdNull;
282 }
283 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800284}
285
286void PostinstallRunnerAction::CompletePartitionPostinstall(
287 int return_code, const string& output) {
288 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700289 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700290
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800291 if (return_code != 0) {
292 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700293 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700294
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700295 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700296 // This special return code means that we tried to update firmware,
297 // but couldn't because we booted from FW B, and we need to reboot
298 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700299 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700300 }
Don Garrett81018e02013-07-30 18:46:31 -0700301
302 if (return_code == 4) {
303 // This special return code means that we tried to update firmware,
304 // but couldn't because we booted from FW B, and we need to reboot
305 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700306 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700307 }
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700308
309 // If postinstall script for this partition is optional we can ignore the
310 // result.
311 if (install_plan_.partitions[current_partition_].postinstall_optional) {
312 LOG(INFO) << "Ignoring postinstall failure since it is optional";
313 } else {
314 return CompletePostinstall(error_code);
315 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700316 }
Alex Deymo0d298542016-03-30 18:31:49 -0700317 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700318 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700319 ReportProgress(0);
320
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700321 PerformPartitionPostinstall();
322}
323
324void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
325 // We only attempt to mark the new slot as active if all the postinstall
326 // steps succeeded.
327 if (error_code == ErrorCode::kSuccess &&
Alex Deymob15a0b82015-11-25 20:30:40 -0300328 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700329 error_code = ErrorCode::kPostinstallRunnerError;
330 }
331
332 ScopedActionCompleter completer(processor_, this);
Alex Deymocbc22742016-03-04 17:53:02 -0800333 completer.set_code(error_code);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700334
335 if (error_code != ErrorCode::kSuccess) {
336 LOG(ERROR) << "Postinstall action failed.";
337
Alex Deymofb905d92016-06-03 19:26:58 -0700338 // Undo any changes done to trigger Powerwash.
339 if (powerwash_scheduled_)
340 hardware_->CancelPowerwash();
Don Garrett81018e02013-07-30 18:46:31 -0700341
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800342 return;
343 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700344
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700345 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700346 if (HasOutputPipe()) {
347 SetOutputObject(install_plan_);
348 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000349}
350
Alex Deymod15c5462016-03-09 18:11:12 -0800351void PostinstallRunnerAction::SuspendAction() {
352 if (!current_command_)
353 return;
354 if (kill(current_command_, SIGSTOP) != 0) {
355 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
356 }
357}
358
359void PostinstallRunnerAction::ResumeAction() {
360 if (!current_command_)
361 return;
362 if (kill(current_command_, SIGCONT) != 0) {
363 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
364 }
365}
366
367void PostinstallRunnerAction::TerminateProcessing() {
368 if (!current_command_)
369 return;
370 // Calling KillExec() will discard the callback we registered and therefore
371 // the unretained reference to this object.
372 Subprocess::Get().KillExec(current_command_);
373 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700374 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800375}
376
adlr@google.com3defe6a2009-12-04 20:57:17 +0000377} // namespace chromeos_update_engine