blob: a31c8a3ed4b2f9c8c922264a7c1e26409406d646 [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
rspangler@google.com49fdf182009-10-10 00:57:34 +000017#include "update_engine/download_action.h"
Alex Deymoaab50e32014-11-10 19:55:35 -080018
adlr@google.comc98a7ed2009-12-04 18:54:03 +000019#include <errno.h>
20#include <algorithm>
Andrew de los Reyesf9714432010-05-04 10:21:23 -070021#include <string>
22#include <vector>
David Zeuthen8f191b22013-08-06 12:27:50 -070023
Alex Vakulenko75039d72014-03-25 12:36:28 -070024#include <base/files/file_path.h>
25#include <base/strings/stringprintf.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070026
adlr@google.comc98a7ed2009-12-04 18:54:03 +000027#include "update_engine/action_pipe.h"
Alex Deymo5ed695e2015-10-05 16:59:23 -070028#include "update_engine/boot_control_interface.h"
Gilad Arnold1f847232014-04-07 12:07:49 -070029#include "update_engine/omaha_request_params.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070030#include "update_engine/p2p_manager.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070031#include "update_engine/payload_state_interface.h"
David Zeuthen34135a92013-08-06 11:16:16 -070032#include "update_engine/utils.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000033
Alex Deymof329b932014-10-30 01:37:48 -070034using base::FilePath;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070035using std::string;
36using std::vector;
rspangler@google.com49fdf182009-10-10 00:57:34 +000037
38namespace chromeos_update_engine {
39
Darin Petkov73058b42010-10-06 16:32:19 -070040DownloadAction::DownloadAction(PrefsInterface* prefs,
Jay Srinivasanf0572052012-10-23 18:12:56 -070041 SystemState* system_state,
Darin Petkov73058b42010-10-06 16:32:19 -070042 HttpFetcher* http_fetcher)
43 : prefs_(prefs),
Jay Srinivasanedce2832012-10-24 18:57:47 -070044 system_state_(system_state),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070045 http_fetcher_(http_fetcher),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070046 writer_(nullptr),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070047 code_(ErrorCode::kSuccess),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070048 delegate_(nullptr),
David Zeuthen8f191b22013-08-06 12:27:50 -070049 bytes_received_(0),
50 p2p_sharing_fd_(-1),
51 p2p_visible_(true) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000052
53DownloadAction::~DownloadAction() {}
54
David Zeuthen8f191b22013-08-06 12:27:50 -070055void DownloadAction::CloseP2PSharingFd(bool delete_p2p_file) {
56 if (p2p_sharing_fd_ != -1) {
57 if (close(p2p_sharing_fd_) != 0) {
58 PLOG(ERROR) << "Error closing p2p sharing fd";
59 }
60 p2p_sharing_fd_ = -1;
61 }
62
63 if (delete_p2p_file) {
Alex Deymof329b932014-10-30 01:37:48 -070064 FilePath path =
Alex Vakulenko75039d72014-03-25 12:36:28 -070065 system_state_->p2p_manager()->FileGetPath(p2p_file_id_);
David Zeuthen8f191b22013-08-06 12:27:50 -070066 if (unlink(path.value().c_str()) != 0) {
67 PLOG(ERROR) << "Error deleting p2p file " << path.value();
68 } else {
69 LOG(INFO) << "Deleted p2p file " << path.value();
70 }
71 }
72
73 // Don't use p2p from this point onwards.
74 p2p_file_id_.clear();
75}
76
77bool DownloadAction::SetupP2PSharingFd() {
78 P2PManager *p2p_manager = system_state_->p2p_manager();
79
80 if (!p2p_manager->FileShare(p2p_file_id_, install_plan_.payload_size)) {
81 LOG(ERROR) << "Unable to share file via p2p";
Alex Vakulenkod2779df2014-06-16 13:19:00 -070082 CloseP2PSharingFd(true); // delete p2p file
David Zeuthen8f191b22013-08-06 12:27:50 -070083 return false;
84 }
85
86 // File has already been created (and allocated, xattrs been
87 // populated etc.) by FileShare() so just open it for writing.
Alex Deymof329b932014-10-30 01:37:48 -070088 FilePath path = p2p_manager->FileGetPath(p2p_file_id_);
David Zeuthen8f191b22013-08-06 12:27:50 -070089 p2p_sharing_fd_ = open(path.value().c_str(), O_WRONLY);
90 if (p2p_sharing_fd_ == -1) {
91 PLOG(ERROR) << "Error opening file " << path.value();
Alex Vakulenkod2779df2014-06-16 13:19:00 -070092 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -070093 return false;
94 }
95
96 // Ensure file to share is world-readable, otherwise
97 // p2p-server and p2p-http-server can't access it.
98 //
99 // (Q: Why doesn't the file have mode 0644 already? A: Because
100 // the process-wide umask is set to 0700 in main.cc.)
101 if (fchmod(p2p_sharing_fd_, 0644) != 0) {
102 PLOG(ERROR) << "Error setting mode 0644 on " << path.value();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700103 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700104 return false;
105 }
106
107 // All good.
108 LOG(INFO) << "Writing payload contents to " << path.value();
109 p2p_manager->FileGetVisible(p2p_file_id_, &p2p_visible_);
110 return true;
111}
112
Alex Deymo60ca1a72015-06-18 18:19:15 -0700113void DownloadAction::WriteToP2PFile(const void* data,
David Zeuthen8f191b22013-08-06 12:27:50 -0700114 size_t length,
115 off_t file_offset) {
116 if (p2p_sharing_fd_ == -1) {
117 if (!SetupP2PSharingFd())
118 return;
119 }
120
121 // Check that the file is at least |file_offset| bytes long - if
122 // it's not something is wrong and we must immediately delete the
123 // file to avoid propagating this problem to other peers.
124 //
125 // How can this happen? It could be that we're resuming an update
126 // after a system crash... in this case, it could be that
127 //
128 // 1. the p2p file didn't get properly synced to stable storage; or
129 // 2. the file was deleted at bootup (it's in /var/cache after all); or
130 // 3. other reasons
Gabe Blacka77939e2014-09-09 23:35:08 -0700131 off_t p2p_size = utils::FileSize(p2p_sharing_fd_);
132 if (p2p_size < 0) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700133 PLOG(ERROR) << "Error getting file status for p2p file";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700134 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700135 return;
136 }
Gabe Blacka77939e2014-09-09 23:35:08 -0700137 if (p2p_size < file_offset) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700138 LOG(ERROR) << "Wanting to write to file offset " << file_offset
Gabe Blacka77939e2014-09-09 23:35:08 -0700139 << " but existing p2p file is only " << p2p_size
David Zeuthen8f191b22013-08-06 12:27:50 -0700140 << " bytes.";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700141 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700142 return;
143 }
144
145 off_t cur_file_offset = lseek(p2p_sharing_fd_, file_offset, SEEK_SET);
146 if (cur_file_offset != static_cast<off_t>(file_offset)) {
147 PLOG(ERROR) << "Error seeking to position "
148 << file_offset << " in p2p file";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700149 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700150 } else {
151 // OK, seeking worked, now write the data
152 ssize_t bytes_written = write(p2p_sharing_fd_, data, length);
153 if (bytes_written != static_cast<ssize_t>(length)) {
154 PLOG(ERROR) << "Error writing "
155 << length << " bytes at file offset "
156 << file_offset << " in p2p file";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700157 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700158 }
159 }
160}
161
rspangler@google.com49fdf182009-10-10 00:57:34 +0000162void DownloadAction::PerformAction() {
163 http_fetcher_->set_delegate(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000164
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000165 // Get the InstallPlan and read it
166 CHECK(HasInputObject());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700167 install_plan_ = GetInputObject();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700168 bytes_received_ = 0;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000169
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700170 install_plan_.Dump();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000171
Alex Deymo5ed695e2015-10-05 16:59:23 -0700172 LOG(INFO) << "Marking new slot as unbootable";
173 if (!system_state_->boot_control()->MarkSlotUnbootable(
174 install_plan_.target_slot)) {
175 LOG(WARNING) << "Unable to mark new slot "
176 << BootControlInterface::SlotName(install_plan_.target_slot)
177 << ". Proceeding with the update anyway.";
178 }
179
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700180 if (writer_) {
181 LOG(INFO) << "Using writer for test.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000182 } else {
Jay Srinivasanf0572052012-10-23 18:12:56 -0700183 delta_performer_.reset(new DeltaPerformer(prefs_,
184 system_state_,
185 &install_plan_));
Darin Petkov7ed561b2011-10-04 02:59:03 -0700186 writer_ = delta_performer_.get();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000187 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700188 int rc = writer_->Open(install_plan_.install_path.c_str(),
189 O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE,
190 0644);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000191 if (rc < 0) {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700192 LOG(ERROR) << "Unable to open output file " << install_plan_.install_path;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000193 // report error to processor
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700194 processor_->ActionComplete(this, ErrorCode::kInstallDeviceOpenError);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000195 return;
196 }
Darin Petkov7ed561b2011-10-04 02:59:03 -0700197 if (delta_performer_.get() &&
198 !delta_performer_->OpenKernel(
199 install_plan_.kernel_install_path.c_str())) {
200 LOG(ERROR) << "Unable to open kernel file "
201 << install_plan_.kernel_install_path.c_str();
202 writer_->Close();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700203 processor_->ActionComplete(this, ErrorCode::kKernelDeviceOpenError);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700204 return;
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700205 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700206 if (delegate_) {
207 delegate_->SetDownloadStatus(true); // Set to active.
208 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700209
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700210 if (system_state_ != nullptr) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700211 const PayloadStateInterface* payload_state = system_state_->payload_state();
David Zeuthen8f191b22013-08-06 12:27:50 -0700212 string file_id = utils::CalculateP2PFileId(install_plan_.payload_hash,
213 install_plan_.payload_size);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700214 if (payload_state->GetUsingP2PForSharing()) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700215 // If we're sharing the update, store the file_id to convey
216 // that we should write to the file.
217 p2p_file_id_ = file_id;
218 LOG(INFO) << "p2p file id: " << p2p_file_id_;
219 } else {
220 // Even if we're not sharing the update, it could be that
221 // there's a partial file from a previous attempt with the same
222 // hash. If this is the case, we NEED to clean it up otherwise
223 // we're essentially timing out other peers downloading from us
224 // (since we're never going to complete the file).
Alex Deymof329b932014-10-30 01:37:48 -0700225 FilePath path = system_state_->p2p_manager()->FileGetPath(file_id);
David Zeuthen8f191b22013-08-06 12:27:50 -0700226 if (!path.empty()) {
227 if (unlink(path.value().c_str()) != 0) {
228 PLOG(ERROR) << "Error deleting p2p file " << path.value();
229 } else {
230 LOG(INFO) << "Deleting partial p2p file " << path.value()
231 << " since we're not using p2p to share.";
232 }
233 }
234 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700235
Gilad Arnold74b5f552014-10-07 08:17:16 -0700236 // Tweak timeouts on the HTTP fetcher if we're downloading from a
237 // local peer.
238 if (payload_state->GetUsingP2PForDownloading() &&
239 payload_state->GetP2PUrl() == install_plan_.download_url) {
240 LOG(INFO) << "Tweaking HTTP fetcher since we're downloading via p2p";
241 http_fetcher_->set_low_speed_limit(kDownloadP2PLowSpeedLimitBps,
242 kDownloadP2PLowSpeedTimeSeconds);
243 http_fetcher_->set_max_retry_count(kDownloadP2PMaxRetryCount);
244 http_fetcher_->set_connect_timeout(kDownloadP2PConnectTimeoutSeconds);
245 }
David Zeuthen34135a92013-08-06 11:16:16 -0700246 }
247
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700248 http_fetcher_->BeginTransfer(install_plan_.download_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000249}
250
251void DownloadAction::TerminateProcessing() {
Darin Petkov698d0412010-10-13 10:59:44 -0700252 if (writer_) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700253 writer_->Close();
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700254 writer_ = nullptr;
Darin Petkov698d0412010-10-13 10:59:44 -0700255 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700256 if (delegate_) {
257 delegate_->SetDownloadStatus(false); // Set to inactive.
258 }
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700259 CloseP2PSharingFd(false); // Keep p2p file.
Darin Petkov9ce452b2010-11-17 14:33:28 -0800260 // Terminates the transfer. The action is terminated, if necessary, when the
261 // TransferTerminated callback is received.
262 http_fetcher_->TerminateTransfer();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000263}
264
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700265void DownloadAction::SeekToOffset(off_t offset) {
266 bytes_received_ = offset;
267}
268
Alex Deymo60ca1a72015-06-18 18:19:15 -0700269void DownloadAction::ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800270 const void* bytes,
271 size_t length) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700272 // Note that bytes_received_ is the current offset.
273 if (!p2p_file_id_.empty()) {
274 WriteToP2PFile(bytes, length, bytes_received_);
275 }
276
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700277 bytes_received_ += length;
278 if (delegate_)
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700279 delegate_->BytesReceived(bytes_received_, install_plan_.payload_size);
280 if (writer_ && !writer_->Write(bytes, length, &code_)) {
281 LOG(ERROR) << "Error " << code_ << " in DeltaPerformer's Write method when "
282 << "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!
295 if (!p2p_visible_ &&
296 delta_performer_.get() && delta_performer_->IsManifestValid()) {
297 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 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700308 if (delegate_) {
309 delegate_->SetDownloadStatus(false); // Set to inactive.
310 }
David Zeuthena99981f2013-04-29 13:42:47 -0700311 ErrorCode code =
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700312 successful ? ErrorCode::kSuccess : ErrorCode::kDownloadTransferError;
313 if (code == ErrorCode::kSuccess && delta_performer_.get()) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700314 code = delta_performer_->VerifyPayload(install_plan_.payload_hash,
315 install_plan_.payload_size);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700316 if (code != ErrorCode::kSuccess) {
Darin Petkov7ed561b2011-10-04 02:59:03 -0700317 LOG(ERROR) << "Download of " << install_plan_.download_url
318 << " failed due to payload verification error.";
David Zeuthen69bc2732013-11-26 16:08:21 -0800319 // Delete p2p file, if applicable.
320 if (!p2p_file_id_.empty())
321 CloseP2PSharingFd(true);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700322 } else if (!delta_performer_->GetNewPartitionInfo(
323 &install_plan_.kernel_size,
324 &install_plan_.kernel_hash,
325 &install_plan_.rootfs_size,
326 &install_plan_.rootfs_hash)) {
327 LOG(ERROR) << "Unable to get new partition hash info.";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700328 code = ErrorCode::kDownloadNewPartitionInfoError;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000329 }
330 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700331
Darin Petkovc97435c2010-07-20 12:37:43 -0700332 // Write the path to the output pipe if we're successful.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700333 if (code == ErrorCode::kSuccess && HasOutputPipe())
Darin Petkov3aefa862010-12-07 14:45:00 -0800334 SetOutputObject(install_plan_);
Darin Petkovc97435c2010-07-20 12:37:43 -0700335 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000336}
337
Darin Petkov9ce452b2010-11-17 14:33:28 -0800338void DownloadAction::TransferTerminated(HttpFetcher *fetcher) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700339 if (code_ != ErrorCode::kSuccess) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800340 processor_->ActionComplete(this, code_);
341 }
342}
343
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700344} // namespace chromeos_update_engine