blob: c24590e44f08b870118328bf2b9658b9d2ea9057 [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__
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;
243 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1) {
244 ReportProgress(frac);
245 return true;
246 }
247
248 return false;
249}
250
251void PostinstallRunnerAction::ReportProgress(double frac) {
252 if (!delegate_)
253 return;
254 if (current_partition_ >= partition_weight_.size()) {
255 delegate_->ProgressUpdate(1.);
256 return;
257 }
Alex Deymo44b35672016-04-05 17:57:48 -0700258 if (!std::isfinite(frac) || frac < 0)
Alex Deymo0d298542016-03-30 18:31:49 -0700259 frac = 0;
260 if (frac > 1)
261 frac = 1;
262 double postinst_action_progress =
263 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
264 total_weight_;
265 delegate_->ProgressUpdate(postinst_action_progress);
266}
267
268void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800269 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800270#ifndef __ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800271 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) {
272 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700273 }
Alex Deymod15c5462016-03-09 18:11:12 -0800274#endif // !__ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800275 fs_mount_dir_.clear();
Alex Deymo0d298542016-03-30 18:31:49 -0700276
277 progress_fd_ = -1;
278 if (progress_task_ != MessageLoop::kTaskIdNull) {
279 MessageLoop::current()->CancelTask(progress_task_);
280 progress_task_ = MessageLoop::kTaskIdNull;
281 }
282 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800283}
284
285void PostinstallRunnerAction::CompletePartitionPostinstall(
286 int return_code, const string& output) {
287 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700288 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700289
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800290 if (return_code != 0) {
291 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700292 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700293
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700294 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700295 // This special return code means that we tried to update firmware,
296 // but couldn't because we booted from FW B, and we need to reboot
297 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700298 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700299 }
Don Garrett81018e02013-07-30 18:46:31 -0700300
301 if (return_code == 4) {
302 // This special return code means that we tried to update firmware,
303 // but couldn't because we booted from FW B, and we need to reboot
304 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700305 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700306 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700307 return CompletePostinstall(error_code);
308 }
Alex Deymo0d298542016-03-30 18:31:49 -0700309 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700310 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700311 ReportProgress(0);
312
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700313 PerformPartitionPostinstall();
314}
315
316void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
317 // We only attempt to mark the new slot as active if all the postinstall
318 // steps succeeded.
319 if (error_code == ErrorCode::kSuccess &&
Alex Deymob15a0b82015-11-25 20:30:40 -0300320 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700321 error_code = ErrorCode::kPostinstallRunnerError;
322 }
323
324 ScopedActionCompleter completer(processor_, this);
Alex Deymocbc22742016-03-04 17:53:02 -0800325 completer.set_code(error_code);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700326
327 if (error_code != ErrorCode::kSuccess) {
328 LOG(ERROR) << "Postinstall action failed.";
329
330 // Undo any changes done to trigger Powerwash using clobber-state.
331 if (powerwash_marker_created_)
332 utils::DeletePowerwashMarkerFile(powerwash_marker_file_);
Don Garrett81018e02013-07-30 18:46:31 -0700333
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800334 return;
335 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700336
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700337 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700338 if (HasOutputPipe()) {
339 SetOutputObject(install_plan_);
340 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000341}
342
Alex Deymod15c5462016-03-09 18:11:12 -0800343void PostinstallRunnerAction::SuspendAction() {
344 if (!current_command_)
345 return;
346 if (kill(current_command_, SIGSTOP) != 0) {
347 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
348 }
349}
350
351void PostinstallRunnerAction::ResumeAction() {
352 if (!current_command_)
353 return;
354 if (kill(current_command_, SIGCONT) != 0) {
355 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
356 }
357}
358
359void PostinstallRunnerAction::TerminateProcessing() {
360 if (!current_command_)
361 return;
362 // Calling KillExec() will discard the callback we registered and therefore
363 // the unretained reference to this object.
364 Subprocess::Get().KillExec(current_command_);
365 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700366 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800367}
368
adlr@google.com3defe6a2009-12-04 20:57:17 +0000369} // namespace chromeos_update_engine