blob: 98f4eaf2e995f3a3c29b56edd8ef393a7a57a7b8 [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 Deymo72ea95a2016-03-30 18:31:49 -070019#include <fcntl.h>
Alex Deymobb0c0672016-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 Deymobb0c0672016-03-09 18:11:12 -080023#include <sys/types.h>
Alex Deymo72ea95a2016-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 Deymobb0c0672016-03-09 18:11:12 -080028#include <base/logging.h>
Alex Deymo72ea95a2016-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 Deymo72ea95a2016-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 Deymo72ea95a2016-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 Deymo72ea95a2016-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 Deymoc975d7b2016-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
Alex Deymo2e1de4f2016-03-25 18:48:22 -0700135#ifdef __ANDROID__
136 // In Chromium OS, the postinstall step is allowed to write to the block
137 // device on the target image, so we don't mark it as read-only and should
138 // be read-write since we just wrote to it during the update.
139
140 // Mark the block device as read-only before mounting for post-install.
141 if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) {
142 return CompletePartitionPostinstall(
143 1, "Error marking the device " + mountable_device + " read only.");
144 }
145#endif // __ANDROID__
146
Alex Deymo390efed2016-02-18 11:00:40 -0800147 if (!utils::MountFilesystem(mountable_device,
148 fs_mount_dir_,
149 MS_RDONLY,
Alex Deymo14dbd332016-03-01 18:55:54 -0800150 partition.filesystem_type,
151 constants::kPostinstallMountOptions)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700152 return CompletePartitionPostinstall(
153 1, "Error mounting the device " + mountable_device);
154 }
155
Alex Deymo390efed2016-02-18 11:00:40 -0800156 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
157 << abs_path << ") installed on device " << partition.target_path
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700158 << " and mountable device " << mountable_device;
159
Alex Deymo032e7722014-03-25 17:53:56 -0700160 // Logs the file format of the postinstall script we are about to run. This
161 // will help debug when the postinstall script doesn't match the architecture
162 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800163 LOG(INFO) << "Format file for new " << partition.postinstall_path
164 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700165
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800166 // Runs the postinstall script asynchronously to free up the main loop while
167 // it's running.
Alex Deymo72ea95a2016-03-30 18:31:49 -0700168 vector<string> command = {abs_path};
169#ifdef __ANDROID__
170 // In Brillo and Android, we pass the slot number and status fd.
171 command.push_back(std::to_string(install_plan_.target_slot));
172 command.push_back(std::to_string(kPostinstallStatusFd));
173#else
174 // Chrome OS postinstall expects the target rootfs as the first parameter.
175 command.push_back(partition.target_path);
176#endif // __ANDROID__
177
178 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymobb0c0672016-03-09 18:11:12 -0800179 command,
Alex Deymo72ea95a2016-03-30 18:31:49 -0700180 Subprocess::kRedirectStderrToStdout,
181 {kPostinstallStatusFd},
Alex Deymobb0c0672016-03-09 18:11:12 -0800182 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
183 base::Unretained(this)));
184 // Subprocess::Exec should never return a negative process id.
185 CHECK_GE(current_command_, 0);
186
Alex Deymo72ea95a2016-03-30 18:31:49 -0700187 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700188 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo72ea95a2016-03-30 18:31:49 -0700189 return;
190 }
191
192 // Monitor the status file descriptor.
193 progress_fd_ =
194 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
195 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
196 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
197 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
198 }
199
200 progress_task_ = MessageLoop::current()->WatchFileDescriptor(
201 FROM_HERE,
202 progress_fd_,
203 MessageLoop::WatchMode::kWatchRead,
204 true,
205 base::Bind(&PostinstallRunnerAction::OnProgressFdReady,
206 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800207}
208
Alex Deymo72ea95a2016-03-30 18:31:49 -0700209void PostinstallRunnerAction::OnProgressFdReady() {
210 char buf[1024];
211 size_t bytes_read;
212 do {
213 bytes_read = 0;
214 bool eof;
215 bool ok =
216 utils::ReadAll(progress_fd_, buf, arraysize(buf), &bytes_read, &eof);
217 progress_buffer_.append(buf, bytes_read);
218 // Process every line.
219 vector<string> lines = base::SplitString(
220 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
221 if (!lines.empty()) {
222 progress_buffer_ = lines.back();
223 lines.pop_back();
224 for (const auto& line : lines) {
225 ProcessProgressLine(line);
226 }
227 }
228 if (!ok || eof) {
229 // There was either an error or an EOF condition, so we are done watching
230 // the file descriptor.
231 MessageLoop::current()->CancelTask(progress_task_);
232 progress_task_ = MessageLoop::kTaskIdNull;
233 return;
234 }
235 } while (bytes_read);
236}
237
238bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
239 double frac = 0;
240 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1) {
241 ReportProgress(frac);
242 return true;
243 }
244
245 return false;
246}
247
248void PostinstallRunnerAction::ReportProgress(double frac) {
249 if (!delegate_)
250 return;
251 if (current_partition_ >= partition_weight_.size()) {
252 delegate_->ProgressUpdate(1.);
253 return;
254 }
255 if (!isfinite(frac) || frac < 0)
256 frac = 0;
257 if (frac > 1)
258 frac = 1;
259 double postinst_action_progress =
260 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
261 total_weight_;
262 delegate_->ProgressUpdate(postinst_action_progress);
263}
264
265void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800266 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymobb0c0672016-03-09 18:11:12 -0800267#ifndef __ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800268 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) {
269 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700270 }
Alex Deymobb0c0672016-03-09 18:11:12 -0800271#endif // !__ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800272 fs_mount_dir_.clear();
Alex Deymo72ea95a2016-03-30 18:31:49 -0700273
274 progress_fd_ = -1;
275 if (progress_task_ != MessageLoop::kTaskIdNull) {
276 MessageLoop::current()->CancelTask(progress_task_);
277 progress_task_ = MessageLoop::kTaskIdNull;
278 }
279 progress_buffer_.clear();
Alex Deymobb0c0672016-03-09 18:11:12 -0800280}
281
282void PostinstallRunnerAction::CompletePartitionPostinstall(
283 int return_code, const string& output) {
284 current_command_ = 0;
Alex Deymo72ea95a2016-03-30 18:31:49 -0700285 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700286
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800287 if (return_code != 0) {
288 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700289 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700290
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700291 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700292 // This special return code means that we tried to update firmware,
293 // but couldn't because we booted from FW B, and we need to reboot
294 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700295 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700296 }
Don Garrett81018e02013-07-30 18:46:31 -0700297
298 if (return_code == 4) {
299 // This special return code means that we tried to update firmware,
300 // but couldn't because we booted from FW B, and we need to reboot
301 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700302 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700303 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700304 return CompletePostinstall(error_code);
305 }
Alex Deymo72ea95a2016-03-30 18:31:49 -0700306 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700307 current_partition_++;
Alex Deymo72ea95a2016-03-30 18:31:49 -0700308 ReportProgress(0);
309
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700310 PerformPartitionPostinstall();
311}
312
313void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
314 // We only attempt to mark the new slot as active if all the postinstall
315 // steps succeeded.
316 if (error_code == ErrorCode::kSuccess &&
Alex Deymob15a0b82015-11-25 20:30:40 -0300317 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700318 error_code = ErrorCode::kPostinstallRunnerError;
319 }
320
321 ScopedActionCompleter completer(processor_, this);
Alex Deymoc975d7b2016-03-04 17:53:02 -0800322 completer.set_code(error_code);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700323
324 if (error_code != ErrorCode::kSuccess) {
325 LOG(ERROR) << "Postinstall action failed.";
326
327 // Undo any changes done to trigger Powerwash using clobber-state.
328 if (powerwash_marker_created_)
329 utils::DeletePowerwashMarkerFile(powerwash_marker_file_);
Don Garrett81018e02013-07-30 18:46:31 -0700330
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800331 return;
332 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700333
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700334 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700335 if (HasOutputPipe()) {
336 SetOutputObject(install_plan_);
337 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000338}
339
Alex Deymobb0c0672016-03-09 18:11:12 -0800340void PostinstallRunnerAction::SuspendAction() {
341 if (!current_command_)
342 return;
343 if (kill(current_command_, SIGSTOP) != 0) {
344 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
345 }
346}
347
348void PostinstallRunnerAction::ResumeAction() {
349 if (!current_command_)
350 return;
351 if (kill(current_command_, SIGCONT) != 0) {
352 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
353 }
354}
355
356void PostinstallRunnerAction::TerminateProcessing() {
357 if (!current_command_)
358 return;
359 // Calling KillExec() will discard the callback we registered and therefore
360 // the unretained reference to this object.
361 Subprocess::Get().KillExec(current_command_);
362 current_command_ = 0;
Alex Deymo72ea95a2016-03-30 18:31:49 -0700363 Cleanup();
Alex Deymobb0c0672016-03-09 18:11:12 -0800364}
365
adlr@google.com3defe6a2009-12-04 20:57:17 +0000366} // namespace chromeos_update_engine