blob: cf550f5327bd5b31a0bde196b103459928b706e1 [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//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/download_action.h"
Alex Deymoaab50e32014-11-10 19:55:35 -080018
adlr@google.comc98a7ed2009-12-04 18:54:03 +000019#include <errno.h>
Alex Deymoe5e5fe92015-10-05 09:28:19 -070020
adlr@google.comc98a7ed2009-12-04 18:54:03 +000021#include <algorithm>
Andrew de los Reyesf9714432010-05-04 10:21:23 -070022#include <string>
David Zeuthen8f191b22013-08-06 12:27:50 -070023
Alex Vakulenko75039d72014-03-25 12:36:28 -070024#include <base/files/file_path.h>
Lann Martin39f57142017-07-14 09:18:42 -060025#include <base/metrics/statistics_recorder.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070026#include <base/strings/stringprintf.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070027
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/action_pipe.h"
29#include "update_engine/common/boot_control_interface.h"
Alex Deymoed9cc182016-06-15 16:19:29 -070030#include "update_engine/common/error_code_utils.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080031#include "update_engine/common/utils.h"
Gilad Arnold1f847232014-04-07 12:07:49 -070032#include "update_engine/omaha_request_params.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070033#include "update_engine/p2p_manager.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070034#include "update_engine/payload_state_interface.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000035
Alex Deymof329b932014-10-30 01:37:48 -070036using base::FilePath;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070037using std::string;
rspangler@google.com49fdf182009-10-10 00:57:34 +000038
39namespace chromeos_update_engine {
40
Darin Petkov73058b42010-10-06 16:32:19 -070041DownloadAction::DownloadAction(PrefsInterface* prefs,
Alex Deymo1b3556c2016-02-03 09:54:02 -080042 BootControlInterface* boot_control,
43 HardwareInterface* hardware,
Jay Srinivasanf0572052012-10-23 18:12:56 -070044 SystemState* system_state,
Amin Hassani7ecda262017-07-11 17:10:50 -070045 HttpFetcher* http_fetcher,
46 bool is_interactive)
Darin Petkov73058b42010-10-06 16:32:19 -070047 : prefs_(prefs),
Alex Deymo1b3556c2016-02-03 09:54:02 -080048 boot_control_(boot_control),
49 hardware_(hardware),
Jay Srinivasanedce2832012-10-24 18:57:47 -070050 system_state_(system_state),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070051 http_fetcher_(http_fetcher),
Amin Hassani7ecda262017-07-11 17:10:50 -070052 is_interactive_(is_interactive),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070053 writer_(nullptr),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070054 code_(ErrorCode::kSuccess),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070055 delegate_(nullptr),
David Zeuthen8f191b22013-08-06 12:27:50 -070056 bytes_received_(0),
57 p2p_sharing_fd_(-1),
Alex Deymo1b3556c2016-02-03 09:54:02 -080058 p2p_visible_(true) {
Lann Martin39f57142017-07-14 09:18:42 -060059 base::StatisticsRecorder::Initialize();
Alex Deymo1b3556c2016-02-03 09:54:02 -080060}
rspangler@google.com49fdf182009-10-10 00:57:34 +000061
62DownloadAction::~DownloadAction() {}
63
David Zeuthen8f191b22013-08-06 12:27:50 -070064void DownloadAction::CloseP2PSharingFd(bool delete_p2p_file) {
65 if (p2p_sharing_fd_ != -1) {
66 if (close(p2p_sharing_fd_) != 0) {
67 PLOG(ERROR) << "Error closing p2p sharing fd";
68 }
69 p2p_sharing_fd_ = -1;
70 }
71
72 if (delete_p2p_file) {
Alex Deymof329b932014-10-30 01:37:48 -070073 FilePath path =
Alex Vakulenko75039d72014-03-25 12:36:28 -070074 system_state_->p2p_manager()->FileGetPath(p2p_file_id_);
David Zeuthen8f191b22013-08-06 12:27:50 -070075 if (unlink(path.value().c_str()) != 0) {
76 PLOG(ERROR) << "Error deleting p2p file " << path.value();
77 } else {
78 LOG(INFO) << "Deleted p2p file " << path.value();
79 }
80 }
81
82 // Don't use p2p from this point onwards.
83 p2p_file_id_.clear();
84}
85
86bool DownloadAction::SetupP2PSharingFd() {
87 P2PManager *p2p_manager = system_state_->p2p_manager();
88
89 if (!p2p_manager->FileShare(p2p_file_id_, install_plan_.payload_size)) {
90 LOG(ERROR) << "Unable to share file via p2p";
Alex Vakulenkod2779df2014-06-16 13:19:00 -070091 CloseP2PSharingFd(true); // delete p2p file
David Zeuthen8f191b22013-08-06 12:27:50 -070092 return false;
93 }
94
95 // File has already been created (and allocated, xattrs been
96 // populated etc.) by FileShare() so just open it for writing.
Alex Deymof329b932014-10-30 01:37:48 -070097 FilePath path = p2p_manager->FileGetPath(p2p_file_id_);
David Zeuthen8f191b22013-08-06 12:27:50 -070098 p2p_sharing_fd_ = open(path.value().c_str(), O_WRONLY);
99 if (p2p_sharing_fd_ == -1) {
100 PLOG(ERROR) << "Error opening file " << path.value();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700101 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700102 return false;
103 }
104
105 // Ensure file to share is world-readable, otherwise
106 // p2p-server and p2p-http-server can't access it.
107 //
108 // (Q: Why doesn't the file have mode 0644 already? A: Because
109 // the process-wide umask is set to 0700 in main.cc.)
110 if (fchmod(p2p_sharing_fd_, 0644) != 0) {
111 PLOG(ERROR) << "Error setting mode 0644 on " << path.value();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700112 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700113 return false;
114 }
115
116 // All good.
117 LOG(INFO) << "Writing payload contents to " << path.value();
118 p2p_manager->FileGetVisible(p2p_file_id_, &p2p_visible_);
119 return true;
120}
121
Alex Deymo60ca1a72015-06-18 18:19:15 -0700122void DownloadAction::WriteToP2PFile(const void* data,
David Zeuthen8f191b22013-08-06 12:27:50 -0700123 size_t length,
124 off_t file_offset) {
125 if (p2p_sharing_fd_ == -1) {
126 if (!SetupP2PSharingFd())
127 return;
128 }
129
130 // Check that the file is at least |file_offset| bytes long - if
131 // it's not something is wrong and we must immediately delete the
132 // file to avoid propagating this problem to other peers.
133 //
134 // How can this happen? It could be that we're resuming an update
135 // after a system crash... in this case, it could be that
136 //
137 // 1. the p2p file didn't get properly synced to stable storage; or
138 // 2. the file was deleted at bootup (it's in /var/cache after all); or
139 // 3. other reasons
Gabe Blacka77939e2014-09-09 23:35:08 -0700140 off_t p2p_size = utils::FileSize(p2p_sharing_fd_);
141 if (p2p_size < 0) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700142 PLOG(ERROR) << "Error getting file status for p2p file";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700143 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700144 return;
145 }
Gabe Blacka77939e2014-09-09 23:35:08 -0700146 if (p2p_size < file_offset) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700147 LOG(ERROR) << "Wanting to write to file offset " << file_offset
Gabe Blacka77939e2014-09-09 23:35:08 -0700148 << " but existing p2p file is only " << p2p_size
David Zeuthen8f191b22013-08-06 12:27:50 -0700149 << " bytes.";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700150 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700151 return;
152 }
153
154 off_t cur_file_offset = lseek(p2p_sharing_fd_, file_offset, SEEK_SET);
155 if (cur_file_offset != static_cast<off_t>(file_offset)) {
156 PLOG(ERROR) << "Error seeking to position "
157 << file_offset << " in p2p file";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700158 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700159 } else {
160 // OK, seeking worked, now write the data
161 ssize_t bytes_written = write(p2p_sharing_fd_, data, length);
162 if (bytes_written != static_cast<ssize_t>(length)) {
163 PLOG(ERROR) << "Error writing "
164 << length << " bytes at file offset "
165 << file_offset << " in p2p file";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700166 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700167 }
168 }
169}
170
rspangler@google.com49fdf182009-10-10 00:57:34 +0000171void DownloadAction::PerformAction() {
172 http_fetcher_->set_delegate(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000173
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000174 // Get the InstallPlan and read it
175 CHECK(HasInputObject());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700176 install_plan_ = GetInputObject();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700177 bytes_received_ = 0;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000178
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700179 install_plan_.Dump();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000180
Alex Deymo5ed695e2015-10-05 16:59:23 -0700181 LOG(INFO) << "Marking new slot as unbootable";
Alex Deymo1b3556c2016-02-03 09:54:02 -0800182 if (!boot_control_->MarkSlotUnbootable(install_plan_.target_slot)) {
Alex Deymo5ed695e2015-10-05 16:59:23 -0700183 LOG(WARNING) << "Unable to mark new slot "
184 << BootControlInterface::SlotName(install_plan_.target_slot)
185 << ". Proceeding with the update anyway.";
186 }
187
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700188 if (writer_) {
189 LOG(INFO) << "Using writer for test.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000190 } else {
Amin Hassani7ecda262017-07-11 17:10:50 -0700191 delta_performer_.reset(new DeltaPerformer(prefs_,
192 boot_control_,
193 hardware_,
194 delegate_,
195 &install_plan_,
196 is_interactive_));
Darin Petkov7ed561b2011-10-04 02:59:03 -0700197 writer_ = delta_performer_.get();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000198 }
Alex Deymo1b3556c2016-02-03 09:54:02 -0800199 download_active_ = true;
David Zeuthen8f191b22013-08-06 12:27:50 -0700200
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700201 if (system_state_ != nullptr) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700202 const PayloadStateInterface* payload_state = system_state_->payload_state();
David Zeuthen8f191b22013-08-06 12:27:50 -0700203 string file_id = utils::CalculateP2PFileId(install_plan_.payload_hash,
204 install_plan_.payload_size);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700205 if (payload_state->GetUsingP2PForSharing()) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700206 // If we're sharing the update, store the file_id to convey
207 // that we should write to the file.
208 p2p_file_id_ = file_id;
209 LOG(INFO) << "p2p file id: " << p2p_file_id_;
210 } else {
211 // Even if we're not sharing the update, it could be that
212 // there's a partial file from a previous attempt with the same
213 // hash. If this is the case, we NEED to clean it up otherwise
214 // we're essentially timing out other peers downloading from us
215 // (since we're never going to complete the file).
Alex Deymof329b932014-10-30 01:37:48 -0700216 FilePath path = system_state_->p2p_manager()->FileGetPath(file_id);
David Zeuthen8f191b22013-08-06 12:27:50 -0700217 if (!path.empty()) {
218 if (unlink(path.value().c_str()) != 0) {
219 PLOG(ERROR) << "Error deleting p2p file " << path.value();
220 } else {
221 LOG(INFO) << "Deleting partial p2p file " << path.value()
222 << " since we're not using p2p to share.";
223 }
224 }
225 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700226
Gilad Arnold74b5f552014-10-07 08:17:16 -0700227 // Tweak timeouts on the HTTP fetcher if we're downloading from a
228 // local peer.
229 if (payload_state->GetUsingP2PForDownloading() &&
230 payload_state->GetP2PUrl() == install_plan_.download_url) {
231 LOG(INFO) << "Tweaking HTTP fetcher since we're downloading via p2p";
232 http_fetcher_->set_low_speed_limit(kDownloadP2PLowSpeedLimitBps,
233 kDownloadP2PLowSpeedTimeSeconds);
234 http_fetcher_->set_max_retry_count(kDownloadP2PMaxRetryCount);
235 http_fetcher_->set_connect_timeout(kDownloadP2PConnectTimeoutSeconds);
236 }
David Zeuthen34135a92013-08-06 11:16:16 -0700237 }
238
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700239 http_fetcher_->BeginTransfer(install_plan_.download_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000240}
241
Alex Deymof2858572016-02-25 11:20:13 -0800242void DownloadAction::SuspendAction() {
243 http_fetcher_->Pause();
244}
245
246void DownloadAction::ResumeAction() {
247 http_fetcher_->Unpause();
248}
249
rspangler@google.com49fdf182009-10-10 00:57:34 +0000250void DownloadAction::TerminateProcessing() {
Darin Petkov698d0412010-10-13 10:59:44 -0700251 if (writer_) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700252 writer_->Close();
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700253 writer_ = nullptr;
Darin Petkov698d0412010-10-13 10:59:44 -0700254 }
Alex Deymo1b3556c2016-02-03 09:54:02 -0800255 download_active_ = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700256 CloseP2PSharingFd(false); // Keep p2p file.
Darin Petkov9ce452b2010-11-17 14:33:28 -0800257 // Terminates the transfer. The action is terminated, if necessary, when the
258 // TransferTerminated callback is received.
259 http_fetcher_->TerminateTransfer();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000260}
261
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700262void DownloadAction::SeekToOffset(off_t offset) {
263 bytes_received_ = offset;
264}
265
Alex Deymo60ca1a72015-06-18 18:19:15 -0700266void DownloadAction::ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800267 const void* bytes,
268 size_t length) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700269 // Note that bytes_received_ is the current offset.
270 if (!p2p_file_id_.empty()) {
271 WriteToP2PFile(bytes, length, bytes_received_);
272 }
273
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700274 bytes_received_ += length;
Alex Deymo542c19b2015-12-03 07:43:31 -0300275 if (delegate_ && download_active_) {
276 delegate_->BytesReceived(
277 length, bytes_received_, install_plan_.payload_size);
278 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700279 if (writer_ && !writer_->Write(bytes, length, &code_)) {
Alex Deymoed9cc182016-06-15 16:19:29 -0700280 LOG(ERROR) << "Error " << utils::ErrorCodeToString(code_) << " (" << code_
281 << ") in DeltaPerformer's Write method when "
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700282 << "processing the received payload -- Terminating processing";
David Zeuthen69bc2732013-11-26 16:08:21 -0800283 // Delete p2p file, if applicable.
284 if (!p2p_file_id_.empty())
285 CloseP2PSharingFd(true);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800286 // Don't tell the action processor that the action is complete until we get
287 // the TransferTerminated callback. Otherwise, this and the HTTP fetcher
288 // objects may get destroyed before all callbacks are complete.
Darin Petkov698d0412010-10-13 10:59:44 -0700289 TerminateProcessing();
Darin Petkov698d0412010-10-13 10:59:44 -0700290 return;
291 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700292
293 // Call p2p_manager_->FileMakeVisible() when we've successfully
294 // verified the manifest!
Alex Deymo1b3556c2016-02-03 09:54:02 -0800295 if (!p2p_visible_ && system_state_ && delta_performer_.get() &&
296 delta_performer_->IsManifestValid()) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700297 LOG(INFO) << "Manifest has been validated. Making p2p file visible.";
298 system_state_->p2p_manager()->FileMakeVisible(p2p_file_id_);
299 p2p_visible_ = true;
300 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000301}
302
Alex Deymo60ca1a72015-06-18 18:19:15 -0700303void DownloadAction::TransferComplete(HttpFetcher* fetcher, bool successful) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000304 if (writer_) {
Darin Petkov698d0412010-10-13 10:59:44 -0700305 LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700306 writer_ = nullptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000307 }
Alex Deymo1b3556c2016-02-03 09:54:02 -0800308 download_active_ = false;
David Zeuthena99981f2013-04-29 13:42:47 -0700309 ErrorCode code =
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700310 successful ? ErrorCode::kSuccess : ErrorCode::kDownloadTransferError;
311 if (code == ErrorCode::kSuccess && delta_performer_.get()) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700312 code = delta_performer_->VerifyPayload(install_plan_.payload_hash,
313 install_plan_.payload_size);
Lann Martin39f57142017-07-14 09:18:42 -0600314 if (code == ErrorCode::kSuccess) {
315 // Log UpdateEngine.DownloadAction.* histograms to help diagnose
316 // long-blocking oeprations.
317 std::string histogram_output;
318 base::StatisticsRecorder::WriteGraph(
319 "UpdateEngine.DownloadAction.", &histogram_output);
320 LOG(INFO) << histogram_output;
321 } else {
Darin Petkov7ed561b2011-10-04 02:59:03 -0700322 LOG(ERROR) << "Download of " << install_plan_.download_url
323 << " failed due to payload verification error.";
David Zeuthen69bc2732013-11-26 16:08:21 -0800324 // Delete p2p file, if applicable.
325 if (!p2p_file_id_.empty())
326 CloseP2PSharingFd(true);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000327 }
328 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700329
Darin Petkovc97435c2010-07-20 12:37:43 -0700330 // Write the path to the output pipe if we're successful.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700331 if (code == ErrorCode::kSuccess && HasOutputPipe())
Darin Petkov3aefa862010-12-07 14:45:00 -0800332 SetOutputObject(install_plan_);
Darin Petkovc97435c2010-07-20 12:37:43 -0700333 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000334}
335
Darin Petkov9ce452b2010-11-17 14:33:28 -0800336void DownloadAction::TransferTerminated(HttpFetcher *fetcher) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700337 if (code_ != ErrorCode::kSuccess) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800338 processor_->ActionComplete(this, code_);
339 }
340}
341
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700342} // namespace chromeos_update_engine