blob: fa89857ec828d6952c4f1ea47e48524392d5e638 [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) {
Gilad Arnold30dedd82013-07-03 06:19:09 -070061 if (utils::CreatePowerwashMarkerFile(powerwash_marker_file_)) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070062 powerwash_marker_created_ = true;
63 } 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__
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700116 TEST_AND_RETURN(
Alex Deymo390efed2016-02-18 11:00:40 -0800117 utils::MakeTempDirectory("au_postint_mount.XXXXXX", &fs_mount_dir_));
118#endif // __ANDROID__
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700119
Alex Deymocbc22742016-03-04 17:53:02 -0800120 base::FilePath postinstall_path(partition.postinstall_path);
121 if (postinstall_path.IsAbsolute()) {
122 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
123 "path instead: "
124 << partition.postinstall_path;
125 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
126 }
127
128 string abs_path =
129 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800130 if (!base::StartsWith(
131 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
132 LOG(ERROR) << "Invalid relative postinstall path: "
133 << partition.postinstall_path;
134 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
135 }
136
Alex Deymo5fb356c2016-03-25 18:48:22 -0700137#ifdef __ANDROID__
138 // In Chromium OS, the postinstall step is allowed to write to the block
139 // device on the target image, so we don't mark it as read-only and should
140 // be read-write since we just wrote to it during the update.
141
142 // Mark the block device as read-only before mounting for post-install.
143 if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) {
144 return CompletePartitionPostinstall(
145 1, "Error marking the device " + mountable_device + " read only.");
146 }
147#endif // __ANDROID__
148
Alex Deymo390efed2016-02-18 11:00:40 -0800149 if (!utils::MountFilesystem(mountable_device,
150 fs_mount_dir_,
151 MS_RDONLY,
Alex Deymo14dbd332016-03-01 18:55:54 -0800152 partition.filesystem_type,
153 constants::kPostinstallMountOptions)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700154 return CompletePartitionPostinstall(
155 1, "Error mounting the device " + mountable_device);
156 }
157
Alex Deymo390efed2016-02-18 11:00:40 -0800158 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
159 << abs_path << ") installed on device " << partition.target_path
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700160 << " and mountable device " << mountable_device;
161
Alex Deymo032e7722014-03-25 17:53:56 -0700162 // Logs the file format of the postinstall script we are about to run. This
163 // will help debug when the postinstall script doesn't match the architecture
164 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800165 LOG(INFO) << "Format file for new " << partition.postinstall_path
166 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700167
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800168 // Runs the postinstall script asynchronously to free up the main loop while
169 // it's running.
Alex Deymo0d298542016-03-30 18:31:49 -0700170 vector<string> command = {abs_path};
171#ifdef __ANDROID__
172 // In Brillo and Android, we pass the slot number and status fd.
173 command.push_back(std::to_string(install_plan_.target_slot));
174 command.push_back(std::to_string(kPostinstallStatusFd));
175#else
176 // Chrome OS postinstall expects the target rootfs as the first parameter.
177 command.push_back(partition.target_path);
178#endif // __ANDROID__
179
180 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymod15c5462016-03-09 18:11:12 -0800181 command,
Alex Deymo0d298542016-03-30 18:31:49 -0700182 Subprocess::kRedirectStderrToStdout,
183 {kPostinstallStatusFd},
Alex Deymod15c5462016-03-09 18:11:12 -0800184 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
185 base::Unretained(this)));
186 // Subprocess::Exec should never return a negative process id.
187 CHECK_GE(current_command_, 0);
188
Alex Deymo0d298542016-03-30 18:31:49 -0700189 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700190 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo0d298542016-03-30 18:31:49 -0700191 return;
192 }
193
194 // Monitor the status file descriptor.
195 progress_fd_ =
196 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
197 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
198 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
199 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
200 }
201
202 progress_task_ = MessageLoop::current()->WatchFileDescriptor(
203 FROM_HERE,
204 progress_fd_,
205 MessageLoop::WatchMode::kWatchRead,
206 true,
207 base::Bind(&PostinstallRunnerAction::OnProgressFdReady,
208 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800209}
210
Alex Deymo0d298542016-03-30 18:31:49 -0700211void PostinstallRunnerAction::OnProgressFdReady() {
212 char buf[1024];
213 size_t bytes_read;
214 do {
215 bytes_read = 0;
216 bool eof;
217 bool ok =
218 utils::ReadAll(progress_fd_, buf, arraysize(buf), &bytes_read, &eof);
219 progress_buffer_.append(buf, bytes_read);
220 // Process every line.
221 vector<string> lines = base::SplitString(
222 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
223 if (!lines.empty()) {
224 progress_buffer_ = lines.back();
225 lines.pop_back();
226 for (const auto& line : lines) {
227 ProcessProgressLine(line);
228 }
229 }
230 if (!ok || eof) {
231 // There was either an error or an EOF condition, so we are done watching
232 // the file descriptor.
233 MessageLoop::current()->CancelTask(progress_task_);
234 progress_task_ = MessageLoop::kTaskIdNull;
235 return;
236 }
237 } while (bytes_read);
238}
239
240bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
241 double frac = 0;
242 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1) {
243 ReportProgress(frac);
244 return true;
245 }
246
247 return false;
248}
249
250void PostinstallRunnerAction::ReportProgress(double frac) {
251 if (!delegate_)
252 return;
253 if (current_partition_ >= partition_weight_.size()) {
254 delegate_->ProgressUpdate(1.);
255 return;
256 }
Alex Deymo44b35672016-04-05 17:57:48 -0700257 if (!std::isfinite(frac) || frac < 0)
Alex Deymo0d298542016-03-30 18:31:49 -0700258 frac = 0;
259 if (frac > 1)
260 frac = 1;
261 double postinst_action_progress =
262 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
263 total_weight_;
264 delegate_->ProgressUpdate(postinst_action_progress);
265}
266
267void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800268 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800269#ifndef __ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800270 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) {
271 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700272 }
Alex Deymod15c5462016-03-09 18:11:12 -0800273#endif // !__ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800274 fs_mount_dir_.clear();
Alex Deymo0d298542016-03-30 18:31:49 -0700275
276 progress_fd_ = -1;
277 if (progress_task_ != MessageLoop::kTaskIdNull) {
278 MessageLoop::current()->CancelTask(progress_task_);
279 progress_task_ = MessageLoop::kTaskIdNull;
280 }
281 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800282}
283
284void PostinstallRunnerAction::CompletePartitionPostinstall(
285 int return_code, const string& output) {
286 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700287 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700288
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800289 if (return_code != 0) {
290 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700291 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700292
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700293 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700294 // This special return code means that we tried to update firmware,
295 // but couldn't because we booted from FW B, and we need to reboot
296 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700297 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700298 }
Don Garrett81018e02013-07-30 18:46:31 -0700299
300 if (return_code == 4) {
301 // This special return code means that we tried to update firmware,
302 // but couldn't because we booted from FW B, and we need to reboot
303 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700304 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700305 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700306 return CompletePostinstall(error_code);
307 }
Alex Deymo0d298542016-03-30 18:31:49 -0700308 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700309 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700310 ReportProgress(0);
311
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700312 PerformPartitionPostinstall();
313}
314
315void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
316 // We only attempt to mark the new slot as active if all the postinstall
317 // steps succeeded.
318 if (error_code == ErrorCode::kSuccess &&
Alex Deymob15a0b82015-11-25 20:30:40 -0300319 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700320 error_code = ErrorCode::kPostinstallRunnerError;
321 }
322
323 ScopedActionCompleter completer(processor_, this);
Alex Deymocbc22742016-03-04 17:53:02 -0800324 completer.set_code(error_code);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700325
326 if (error_code != ErrorCode::kSuccess) {
327 LOG(ERROR) << "Postinstall action failed.";
328
329 // Undo any changes done to trigger Powerwash using clobber-state.
330 if (powerwash_marker_created_)
331 utils::DeletePowerwashMarkerFile(powerwash_marker_file_);
Don Garrett81018e02013-07-30 18:46:31 -0700332
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800333 return;
334 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700335
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700336 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700337 if (HasOutputPipe()) {
338 SetOutputObject(install_plan_);
339 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000340}
341
Alex Deymod15c5462016-03-09 18:11:12 -0800342void PostinstallRunnerAction::SuspendAction() {
343 if (!current_command_)
344 return;
345 if (kill(current_command_, SIGSTOP) != 0) {
346 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
347 }
348}
349
350void PostinstallRunnerAction::ResumeAction() {
351 if (!current_command_)
352 return;
353 if (kill(current_command_, SIGCONT) != 0) {
354 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
355 }
356}
357
358void PostinstallRunnerAction::TerminateProcessing() {
359 if (!current_command_)
360 return;
361 // Calling KillExec() will discard the callback we registered and therefore
362 // the unretained reference to this object.
363 Subprocess::Get().KillExec(current_command_);
364 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700365 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800366}
367
adlr@google.com3defe6a2009-12-04 20:57:17 +0000368} // namespace chromeos_update_engine