blob: e51549e3434c1ee181a234587a382b49552183bc [file] [log] [blame]
Darin Petkov85d02b72011-05-17 13:25:51 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rspangler@google.com49fdf182009-10-10 00:57:34 +00005#include "update_engine/download_action.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +00006#include <errno.h>
7#include <algorithm>
Andrew de los Reyesf9714432010-05-04 10:21:23 -07008#include <string>
9#include <vector>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000010#include <glib.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070011
Alex Vakulenko75039d72014-03-25 12:36:28 -070012#include <base/files/file_path.h>
13#include <base/strings/stringprintf.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070014
adlr@google.comc98a7ed2009-12-04 18:54:03 +000015#include "update_engine/action_pipe.h"
Gilad Arnold1f847232014-04-07 12:07:49 -070016#include "update_engine/omaha_request_params.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070017#include "update_engine/p2p_manager.h"
Andrew de los Reyesf9714432010-05-04 10:21:23 -070018#include "update_engine/subprocess.h"
David Zeuthen34135a92013-08-06 11:16:16 -070019#include "update_engine/utils.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000020
Alex Deymof329b932014-10-30 01:37:48 -070021using base::FilePath;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070022using std::string;
23using std::vector;
rspangler@google.com49fdf182009-10-10 00:57:34 +000024
25namespace chromeos_update_engine {
26
Darin Petkov73058b42010-10-06 16:32:19 -070027DownloadAction::DownloadAction(PrefsInterface* prefs,
Jay Srinivasanf0572052012-10-23 18:12:56 -070028 SystemState* system_state,
Darin Petkov73058b42010-10-06 16:32:19 -070029 HttpFetcher* http_fetcher)
30 : prefs_(prefs),
Jay Srinivasanedce2832012-10-24 18:57:47 -070031 system_state_(system_state),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070032 http_fetcher_(http_fetcher),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070033 writer_(nullptr),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070034 code_(ErrorCode::kSuccess),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070035 delegate_(nullptr),
David Zeuthen8f191b22013-08-06 12:27:50 -070036 bytes_received_(0),
37 p2p_sharing_fd_(-1),
38 p2p_visible_(true) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000039
40DownloadAction::~DownloadAction() {}
41
David Zeuthen8f191b22013-08-06 12:27:50 -070042void DownloadAction::CloseP2PSharingFd(bool delete_p2p_file) {
43 if (p2p_sharing_fd_ != -1) {
44 if (close(p2p_sharing_fd_) != 0) {
45 PLOG(ERROR) << "Error closing p2p sharing fd";
46 }
47 p2p_sharing_fd_ = -1;
48 }
49
50 if (delete_p2p_file) {
Alex Deymof329b932014-10-30 01:37:48 -070051 FilePath path =
Alex Vakulenko75039d72014-03-25 12:36:28 -070052 system_state_->p2p_manager()->FileGetPath(p2p_file_id_);
David Zeuthen8f191b22013-08-06 12:27:50 -070053 if (unlink(path.value().c_str()) != 0) {
54 PLOG(ERROR) << "Error deleting p2p file " << path.value();
55 } else {
56 LOG(INFO) << "Deleted p2p file " << path.value();
57 }
58 }
59
60 // Don't use p2p from this point onwards.
61 p2p_file_id_.clear();
62}
63
64bool DownloadAction::SetupP2PSharingFd() {
65 P2PManager *p2p_manager = system_state_->p2p_manager();
66
67 if (!p2p_manager->FileShare(p2p_file_id_, install_plan_.payload_size)) {
68 LOG(ERROR) << "Unable to share file via p2p";
Alex Vakulenkod2779df2014-06-16 13:19:00 -070069 CloseP2PSharingFd(true); // delete p2p file
David Zeuthen8f191b22013-08-06 12:27:50 -070070 return false;
71 }
72
73 // File has already been created (and allocated, xattrs been
74 // populated etc.) by FileShare() so just open it for writing.
Alex Deymof329b932014-10-30 01:37:48 -070075 FilePath path = p2p_manager->FileGetPath(p2p_file_id_);
David Zeuthen8f191b22013-08-06 12:27:50 -070076 p2p_sharing_fd_ = open(path.value().c_str(), O_WRONLY);
77 if (p2p_sharing_fd_ == -1) {
78 PLOG(ERROR) << "Error opening file " << path.value();
Alex Vakulenkod2779df2014-06-16 13:19:00 -070079 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -070080 return false;
81 }
82
83 // Ensure file to share is world-readable, otherwise
84 // p2p-server and p2p-http-server can't access it.
85 //
86 // (Q: Why doesn't the file have mode 0644 already? A: Because
87 // the process-wide umask is set to 0700 in main.cc.)
88 if (fchmod(p2p_sharing_fd_, 0644) != 0) {
89 PLOG(ERROR) << "Error setting mode 0644 on " << path.value();
Alex Vakulenkod2779df2014-06-16 13:19:00 -070090 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -070091 return false;
92 }
93
94 // All good.
95 LOG(INFO) << "Writing payload contents to " << path.value();
96 p2p_manager->FileGetVisible(p2p_file_id_, &p2p_visible_);
97 return true;
98}
99
100void DownloadAction::WriteToP2PFile(const char *data,
101 size_t length,
102 off_t file_offset) {
103 if (p2p_sharing_fd_ == -1) {
104 if (!SetupP2PSharingFd())
105 return;
106 }
107
108 // Check that the file is at least |file_offset| bytes long - if
109 // it's not something is wrong and we must immediately delete the
110 // file to avoid propagating this problem to other peers.
111 //
112 // How can this happen? It could be that we're resuming an update
113 // after a system crash... in this case, it could be that
114 //
115 // 1. the p2p file didn't get properly synced to stable storage; or
116 // 2. the file was deleted at bootup (it's in /var/cache after all); or
117 // 3. other reasons
Gabe Blacka77939e2014-09-09 23:35:08 -0700118 off_t p2p_size = utils::FileSize(p2p_sharing_fd_);
119 if (p2p_size < 0) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700120 PLOG(ERROR) << "Error getting file status for p2p file";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700121 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700122 return;
123 }
Gabe Blacka77939e2014-09-09 23:35:08 -0700124 if (p2p_size < file_offset) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700125 LOG(ERROR) << "Wanting to write to file offset " << file_offset
Gabe Blacka77939e2014-09-09 23:35:08 -0700126 << " but existing p2p file is only " << p2p_size
David Zeuthen8f191b22013-08-06 12:27:50 -0700127 << " bytes.";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700128 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700129 return;
130 }
131
132 off_t cur_file_offset = lseek(p2p_sharing_fd_, file_offset, SEEK_SET);
133 if (cur_file_offset != static_cast<off_t>(file_offset)) {
134 PLOG(ERROR) << "Error seeking to position "
135 << file_offset << " in p2p file";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700136 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700137 } else {
138 // OK, seeking worked, now write the data
139 ssize_t bytes_written = write(p2p_sharing_fd_, data, length);
140 if (bytes_written != static_cast<ssize_t>(length)) {
141 PLOG(ERROR) << "Error writing "
142 << length << " bytes at file offset "
143 << file_offset << " in p2p file";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700144 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700145 }
146 }
147}
148
rspangler@google.com49fdf182009-10-10 00:57:34 +0000149void DownloadAction::PerformAction() {
150 http_fetcher_->set_delegate(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000151
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000152 // Get the InstallPlan and read it
153 CHECK(HasInputObject());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700154 install_plan_ = GetInputObject();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700155 bytes_received_ = 0;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000156
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700157 install_plan_.Dump();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000158
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700159 if (writer_) {
160 LOG(INFO) << "Using writer for test.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000161 } else {
Jay Srinivasanf0572052012-10-23 18:12:56 -0700162 delta_performer_.reset(new DeltaPerformer(prefs_,
163 system_state_,
164 &install_plan_));
Darin Petkov7ed561b2011-10-04 02:59:03 -0700165 writer_ = delta_performer_.get();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000166 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700167 int rc = writer_->Open(install_plan_.install_path.c_str(),
168 O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE,
169 0644);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000170 if (rc < 0) {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700171 LOG(ERROR) << "Unable to open output file " << install_plan_.install_path;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000172 // report error to processor
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700173 processor_->ActionComplete(this, ErrorCode::kInstallDeviceOpenError);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000174 return;
175 }
Darin Petkov7ed561b2011-10-04 02:59:03 -0700176 if (delta_performer_.get() &&
177 !delta_performer_->OpenKernel(
178 install_plan_.kernel_install_path.c_str())) {
179 LOG(ERROR) << "Unable to open kernel file "
180 << install_plan_.kernel_install_path.c_str();
181 writer_->Close();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700182 processor_->ActionComplete(this, ErrorCode::kKernelDeviceOpenError);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700183 return;
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700184 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700185 if (delegate_) {
186 delegate_->SetDownloadStatus(true); // Set to active.
187 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700188
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700189 if (system_state_ != nullptr) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700190 string file_id = utils::CalculateP2PFileId(install_plan_.payload_hash,
191 install_plan_.payload_size);
192 if (system_state_->request_params()->use_p2p_for_sharing()) {
193 // If we're sharing the update, store the file_id to convey
194 // that we should write to the file.
195 p2p_file_id_ = file_id;
196 LOG(INFO) << "p2p file id: " << p2p_file_id_;
197 } else {
198 // Even if we're not sharing the update, it could be that
199 // there's a partial file from a previous attempt with the same
200 // hash. If this is the case, we NEED to clean it up otherwise
201 // we're essentially timing out other peers downloading from us
202 // (since we're never going to complete the file).
Alex Deymof329b932014-10-30 01:37:48 -0700203 FilePath path = system_state_->p2p_manager()->FileGetPath(file_id);
David Zeuthen8f191b22013-08-06 12:27:50 -0700204 if (!path.empty()) {
205 if (unlink(path.value().c_str()) != 0) {
206 PLOG(ERROR) << "Error deleting p2p file " << path.value();
207 } else {
208 LOG(INFO) << "Deleting partial p2p file " << path.value()
209 << " since we're not using p2p to share.";
210 }
211 }
212 }
213 }
214
David Zeuthen34135a92013-08-06 11:16:16 -0700215 // Tweak timeouts on the HTTP fetcher if we're downloading from a
216 // local peer.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700217 if (system_state_ != nullptr &&
David Zeuthen34135a92013-08-06 11:16:16 -0700218 system_state_->request_params()->use_p2p_for_downloading() &&
219 system_state_->request_params()->p2p_url() ==
220 install_plan_.download_url) {
221 LOG(INFO) << "Tweaking HTTP fetcher since we're downloading via p2p";
222 http_fetcher_->set_low_speed_limit(kDownloadP2PLowSpeedLimitBps,
223 kDownloadP2PLowSpeedTimeSeconds);
224 http_fetcher_->set_max_retry_count(kDownloadP2PMaxRetryCount);
225 http_fetcher_->set_connect_timeout(kDownloadP2PConnectTimeoutSeconds);
226 }
227
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700228 http_fetcher_->BeginTransfer(install_plan_.download_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000229}
230
231void DownloadAction::TerminateProcessing() {
Darin Petkov698d0412010-10-13 10:59:44 -0700232 if (writer_) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700233 writer_->Close();
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700234 writer_ = nullptr;
Darin Petkov698d0412010-10-13 10:59:44 -0700235 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700236 if (delegate_) {
237 delegate_->SetDownloadStatus(false); // Set to inactive.
238 }
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700239 CloseP2PSharingFd(false); // Keep p2p file.
Darin Petkov9ce452b2010-11-17 14:33:28 -0800240 // Terminates the transfer. The action is terminated, if necessary, when the
241 // TransferTerminated callback is received.
242 http_fetcher_->TerminateTransfer();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000243}
244
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700245void DownloadAction::SeekToOffset(off_t offset) {
246 bytes_received_ = offset;
247}
248
rspangler@google.com49fdf182009-10-10 00:57:34 +0000249void DownloadAction::ReceivedBytes(HttpFetcher *fetcher,
250 const char* bytes,
251 int length) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700252 // Note that bytes_received_ is the current offset.
253 if (!p2p_file_id_.empty()) {
254 WriteToP2PFile(bytes, length, bytes_received_);
255 }
256
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700257 bytes_received_ += length;
258 if (delegate_)
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700259 delegate_->BytesReceived(bytes_received_, install_plan_.payload_size);
260 if (writer_ && !writer_->Write(bytes, length, &code_)) {
261 LOG(ERROR) << "Error " << code_ << " in DeltaPerformer's Write method when "
262 << "processing the received payload -- Terminating processing";
David Zeuthen69bc2732013-11-26 16:08:21 -0800263 // Delete p2p file, if applicable.
264 if (!p2p_file_id_.empty())
265 CloseP2PSharingFd(true);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800266 // Don't tell the action processor that the action is complete until we get
267 // the TransferTerminated callback. Otherwise, this and the HTTP fetcher
268 // objects may get destroyed before all callbacks are complete.
Darin Petkov698d0412010-10-13 10:59:44 -0700269 TerminateProcessing();
Darin Petkov698d0412010-10-13 10:59:44 -0700270 return;
271 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700272
273 // Call p2p_manager_->FileMakeVisible() when we've successfully
274 // verified the manifest!
275 if (!p2p_visible_ &&
276 delta_performer_.get() && delta_performer_->IsManifestValid()) {
277 LOG(INFO) << "Manifest has been validated. Making p2p file visible.";
278 system_state_->p2p_manager()->FileMakeVisible(p2p_file_id_);
279 p2p_visible_ = true;
280 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000281}
282
283void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) {
284 if (writer_) {
Darin Petkov698d0412010-10-13 10:59:44 -0700285 LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700286 writer_ = nullptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000287 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700288 if (delegate_) {
289 delegate_->SetDownloadStatus(false); // Set to inactive.
290 }
David Zeuthena99981f2013-04-29 13:42:47 -0700291 ErrorCode code =
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700292 successful ? ErrorCode::kSuccess : ErrorCode::kDownloadTransferError;
293 if (code == ErrorCode::kSuccess && delta_performer_.get()) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700294 code = delta_performer_->VerifyPayload(install_plan_.payload_hash,
295 install_plan_.payload_size);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700296 if (code != ErrorCode::kSuccess) {
Darin Petkov7ed561b2011-10-04 02:59:03 -0700297 LOG(ERROR) << "Download of " << install_plan_.download_url
298 << " failed due to payload verification error.";
David Zeuthen69bc2732013-11-26 16:08:21 -0800299 // Delete p2p file, if applicable.
300 if (!p2p_file_id_.empty())
301 CloseP2PSharingFd(true);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700302 } else if (!delta_performer_->GetNewPartitionInfo(
303 &install_plan_.kernel_size,
304 &install_plan_.kernel_hash,
305 &install_plan_.rootfs_size,
306 &install_plan_.rootfs_hash)) {
307 LOG(ERROR) << "Unable to get new partition hash info.";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700308 code = ErrorCode::kDownloadNewPartitionInfoError;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000309 }
310 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700311
Darin Petkovc97435c2010-07-20 12:37:43 -0700312 // Write the path to the output pipe if we're successful.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700313 if (code == ErrorCode::kSuccess && HasOutputPipe())
Darin Petkov3aefa862010-12-07 14:45:00 -0800314 SetOutputObject(install_plan_);
Darin Petkovc97435c2010-07-20 12:37:43 -0700315 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000316}
317
Darin Petkov9ce452b2010-11-17 14:33:28 -0800318void DownloadAction::TransferTerminated(HttpFetcher *fetcher) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700319 if (code_ != ErrorCode::kSuccess) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800320 processor_->ActionComplete(this, code_);
321 }
322}
323
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700324} // namespace chromeos_update_engine