1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
//
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "update_engine/cros/download_action_chromeos.h"
#include <errno.h>
#include <algorithm>
#include <string>
#include <base/files/file_path.h>
#include <base/metrics/statistics_recorder.h>
#include <base/strings/stringprintf.h>
#include "update_engine/common/action_pipe.h"
#include "update_engine/common/boot_control_interface.h"
#include "update_engine/common/error_code_utils.h"
#include "update_engine/common/multi_range_http_fetcher.h"
#include "update_engine/common/system_state.h"
#include "update_engine/common/utils.h"
#include "update_engine/cros/omaha_request_params.h"
#include "update_engine/cros/p2p_manager.h"
#include "update_engine/cros/payload_state_interface.h"
using base::FilePath;
using std::string;
namespace chromeos_update_engine {
DownloadActionChromeos::DownloadActionChromeos(
PrefsInterface* prefs,
BootControlInterface* boot_control,
HardwareInterface* hardware,
HttpFetcher* http_fetcher,
bool interactive)
: prefs_(prefs),
boot_control_(boot_control),
hardware_(hardware),
http_fetcher_(new MultiRangeHttpFetcher(http_fetcher)),
interactive_(interactive),
writer_(nullptr),
code_(ErrorCode::kSuccess),
delegate_(nullptr),
p2p_sharing_fd_(-1),
p2p_visible_(true) {}
DownloadActionChromeos::~DownloadActionChromeos() {}
void DownloadActionChromeos::CloseP2PSharingFd(bool delete_p2p_file) {
if (p2p_sharing_fd_ != -1) {
if (close(p2p_sharing_fd_) != 0) {
PLOG(ERROR) << "Error closing p2p sharing fd";
}
p2p_sharing_fd_ = -1;
}
if (delete_p2p_file) {
FilePath path =
SystemState::Get()->p2p_manager()->FileGetPath(p2p_file_id_);
if (unlink(path.value().c_str()) != 0) {
PLOG(ERROR) << "Error deleting p2p file " << path.value();
} else {
LOG(INFO) << "Deleted p2p file " << path.value();
}
}
// Don't use p2p from this point onwards.
p2p_file_id_.clear();
}
bool DownloadActionChromeos::SetupP2PSharingFd() {
P2PManager* p2p_manager = SystemState::Get()->p2p_manager();
if (!p2p_manager->FileShare(p2p_file_id_, payload_->size)) {
LOG(ERROR) << "Unable to share file via p2p";
CloseP2PSharingFd(true); // delete p2p file
return false;
}
// File has already been created (and allocated, xattrs been
// populated etc.) by FileShare() so just open it for writing.
FilePath path = p2p_manager->FileGetPath(p2p_file_id_);
p2p_sharing_fd_ = open(path.value().c_str(), O_WRONLY);
if (p2p_sharing_fd_ == -1) {
PLOG(ERROR) << "Error opening file " << path.value();
CloseP2PSharingFd(true); // Delete p2p file.
return false;
}
// Ensure file to share is world-readable, otherwise
// p2p-server and p2p-http-server can't access it.
//
// (Q: Why doesn't the file have mode 0644 already? A: Because
// the process-wide umask is set to 0700 in main.cc.)
if (fchmod(p2p_sharing_fd_, 0644) != 0) {
PLOG(ERROR) << "Error setting mode 0644 on " << path.value();
CloseP2PSharingFd(true); // Delete p2p file.
return false;
}
// All good.
LOG(INFO) << "Writing payload contents to " << path.value();
p2p_manager->FileGetVisible(p2p_file_id_, &p2p_visible_);
return true;
}
void DownloadActionChromeos::WriteToP2PFile(const void* data,
size_t length,
off_t file_offset) {
if (p2p_sharing_fd_ == -1) {
if (!SetupP2PSharingFd())
return;
}
// Check that the file is at least |file_offset| bytes long - if
// it's not something is wrong and we must immediately delete the
// file to avoid propagating this problem to other peers.
//
// How can this happen? It could be that we're resuming an update
// after a system crash... in this case, it could be that
//
// 1. the p2p file didn't get properly synced to stable storage; or
// 2. the file was deleted at bootup (it's in /var/cache after all); or
// 3. other reasons
off_t p2p_size = utils::FileSize(p2p_sharing_fd_);
if (p2p_size < 0) {
PLOG(ERROR) << "Error getting file status for p2p file";
CloseP2PSharingFd(true); // Delete p2p file.
return;
}
if (p2p_size < file_offset) {
LOG(ERROR) << "Wanting to write to file offset " << file_offset
<< " but existing p2p file is only " << p2p_size << " bytes.";
CloseP2PSharingFd(true); // Delete p2p file.
return;
}
off_t cur_file_offset = lseek(p2p_sharing_fd_, file_offset, SEEK_SET);
if (cur_file_offset != static_cast<off_t>(file_offset)) {
PLOG(ERROR) << "Error seeking to position " << file_offset
<< " in p2p file";
CloseP2PSharingFd(true); // Delete p2p file.
} else {
// OK, seeking worked, now write the data
ssize_t bytes_written = write(p2p_sharing_fd_, data, length);
if (bytes_written != static_cast<ssize_t>(length)) {
PLOG(ERROR) << "Error writing " << length << " bytes at file offset "
<< file_offset << " in p2p file";
CloseP2PSharingFd(true); // Delete p2p file.
}
}
}
void DownloadActionChromeos::PerformAction() {
http_fetcher_->set_delegate(this);
// Get the InstallPlan and read it
CHECK(HasInputObject());
install_plan_ = GetInputObject();
install_plan_.Dump();
bytes_received_ = 0;
bytes_received_previous_payloads_ = 0;
bytes_total_ = 0;
for (const auto& payload : install_plan_.payloads)
bytes_total_ += payload.size;
if (install_plan_.is_resume) {
int64_t payload_index = 0;
if (prefs_->GetInt64(kPrefsUpdateStatePayloadIndex, &payload_index) &&
static_cast<size_t>(payload_index) < install_plan_.payloads.size()) {
// Save the index for the resume payload before downloading any previous
// payload, otherwise it will be overwritten.
resume_payload_index_ = payload_index;
for (int i = 0; i < payload_index; i++)
install_plan_.payloads[i].already_applied = true;
}
}
// TODO(senj): check that install plan has at least one payload.
if (!payload_)
payload_ = &install_plan_.payloads[0];
LOG(INFO) << "Marking new slot as unbootable";
if (!boot_control_->MarkSlotUnbootable(install_plan_.target_slot)) {
LOG(WARNING) << "Unable to mark new slot "
<< BootControlInterface::SlotName(install_plan_.target_slot)
<< ". Proceeding with the update anyway.";
}
StartDownloading();
}
bool DownloadActionChromeos::LoadCachedManifest(int64_t manifest_size) {
std::string cached_manifest_bytes;
if (!prefs_->GetString(kPrefsManifestBytes, &cached_manifest_bytes) ||
cached_manifest_bytes.size() <= 0) {
LOG(INFO) << "Cached Manifest data not found";
return false;
}
if (static_cast<int64_t>(cached_manifest_bytes.size()) != manifest_size) {
LOG(WARNING) << "Cached metadata has unexpected size: "
<< cached_manifest_bytes.size() << " vs. " << manifest_size;
return false;
}
ErrorCode error;
const bool success =
delta_performer_->Write(
cached_manifest_bytes.data(), cached_manifest_bytes.size(), &error) &&
delta_performer_->IsManifestValid();
if (success) {
LOG(INFO) << "Successfully parsed cached manifest";
} else {
// If parsing of cached data failed, fall back to fetch them using HTTP
LOG(WARNING) << "Cached manifest data fails to load, error code:"
<< static_cast<int>(error) << "," << error;
}
return success;
}
void DownloadActionChromeos::StartDownloading() {
download_active_ = true;
http_fetcher_->ClearRanges();
if (writer_ && writer_ != delta_performer_.get()) {
LOG(INFO) << "Using writer for test.";
} else {
delta_performer_.reset(new DeltaPerformer(prefs_,
boot_control_,
hardware_,
delegate_,
&install_plan_,
payload_,
interactive_));
writer_ = delta_performer_.get();
}
if (install_plan_.is_resume &&
payload_ == &install_plan_.payloads[resume_payload_index_]) {
// Resuming an update so parse the cached manifest first
int64_t manifest_metadata_size = 0;
int64_t manifest_signature_size = 0;
prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size);
// TODO(zhangkelvin) Add unittest for success and fallback route
if (!LoadCachedManifest(manifest_metadata_size + manifest_signature_size)) {
if (delta_performer_) {
// Create a new DeltaPerformer to reset all its state
delta_performer_ = std::make_unique<DeltaPerformer>(prefs_,
boot_control_,
hardware_,
delegate_,
&install_plan_,
payload_,
interactive_);
writer_ = delta_performer_.get();
}
http_fetcher_->AddRange(base_offset_,
manifest_metadata_size + manifest_signature_size);
}
// If there're remaining unprocessed data blobs, fetch them. Be careful not
// to request data beyond the end of the payload to avoid 416 HTTP response
// error codes.
int64_t next_data_offset = 0;
prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
uint64_t resume_offset =
manifest_metadata_size + manifest_signature_size + next_data_offset;
if (!payload_->size) {
http_fetcher_->AddRange(base_offset_ + resume_offset);
} else if (resume_offset < payload_->size) {
http_fetcher_->AddRange(base_offset_ + resume_offset,
payload_->size - resume_offset);
}
} else {
if (payload_->size) {
http_fetcher_->AddRange(base_offset_, payload_->size);
} else {
// If no payload size is passed we assume we read until the end of the
// stream.
http_fetcher_->AddRange(base_offset_);
}
}
if (SystemState::Get() != nullptr) {
const PayloadStateInterface* payload_state =
SystemState::Get()->payload_state();
string file_id = utils::CalculateP2PFileId(payload_->hash, payload_->size);
if (payload_state->GetUsingP2PForSharing()) {
// If we're sharing the update, store the file_id to convey
// that we should write to the file.
p2p_file_id_ = file_id;
LOG(INFO) << "p2p file id: " << p2p_file_id_;
} else {
// Even if we're not sharing the update, it could be that
// there's a partial file from a previous attempt with the same
// hash. If this is the case, we NEED to clean it up otherwise
// we're essentially timing out other peers downloading from us
// (since we're never going to complete the file).
FilePath path = SystemState::Get()->p2p_manager()->FileGetPath(file_id);
if (!path.empty()) {
if (unlink(path.value().c_str()) != 0) {
PLOG(ERROR) << "Error deleting p2p file " << path.value();
} else {
LOG(INFO) << "Deleting partial p2p file " << path.value()
<< " since we're not using p2p to share.";
}
}
}
// Tweak timeouts on the HTTP fetcher if we're downloading from a
// local peer.
if (payload_state->GetUsingP2PForDownloading() &&
payload_state->GetP2PUrl() == install_plan_.download_url) {
LOG(INFO) << "Tweaking HTTP fetcher since we're downloading via p2p";
http_fetcher_->set_low_speed_limit(kDownloadP2PLowSpeedLimitBps,
kDownloadP2PLowSpeedTimeSeconds);
http_fetcher_->set_max_retry_count(kDownloadP2PMaxRetryCount);
http_fetcher_->set_connect_timeout(kDownloadP2PConnectTimeoutSeconds);
}
}
http_fetcher_->BeginTransfer(install_plan_.download_url);
}
void DownloadActionChromeos::SuspendAction() {
http_fetcher_->Pause();
}
void DownloadActionChromeos::ResumeAction() {
http_fetcher_->Unpause();
}
void DownloadActionChromeos::TerminateProcessing() {
if (writer_) {
writer_->Close();
writer_ = nullptr;
}
download_active_ = false;
CloseP2PSharingFd(false); // Keep p2p file.
// Terminates the transfer. The action is terminated, if necessary, when the
// TransferTerminated callback is received.
http_fetcher_->TerminateTransfer();
}
void DownloadActionChromeos::SeekToOffset(off_t offset) {
bytes_received_ = offset;
}
bool DownloadActionChromeos::ReceivedBytes(HttpFetcher* fetcher,
const void* bytes,
size_t length) {
// Note that bytes_received_ is the current offset.
if (!p2p_file_id_.empty()) {
WriteToP2PFile(bytes, length, bytes_received_);
}
bytes_received_ += length;
uint64_t bytes_downloaded_total =
bytes_received_previous_payloads_ + bytes_received_;
if (delegate_ && download_active_) {
delegate_->BytesReceived(length, bytes_downloaded_total, bytes_total_);
}
if (writer_ && !writer_->Write(bytes, length, &code_)) {
if (code_ != ErrorCode::kSuccess) {
LOG(ERROR) << "Error " << utils::ErrorCodeToString(code_) << " (" << code_
<< ") in DeltaPerformer's Write method when "
<< "processing the received payload -- Terminating processing";
}
// Delete p2p file, if applicable.
if (!p2p_file_id_.empty())
CloseP2PSharingFd(true);
// Don't tell the action processor that the action is complete until we get
// the TransferTerminated callback. Otherwise, this and the HTTP fetcher
// objects may get destroyed before all callbacks are complete.
TerminateProcessing();
return false;
}
// Call p2p_manager_->FileMakeVisible() when we've successfully
// verified the manifest!
if (!p2p_visible_ && SystemState::Get() && delta_performer_.get() &&
delta_performer_->IsManifestValid()) {
LOG(INFO) << "Manifest has been validated. Making p2p file visible.";
SystemState::Get()->p2p_manager()->FileMakeVisible(p2p_file_id_);
p2p_visible_ = true;
}
return true;
}
void DownloadActionChromeos::TransferComplete(HttpFetcher* fetcher,
bool successful) {
if (writer_) {
LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
if (delta_performer_.get() == writer_) {
// no delta_performer_ in tests, so leave the test writer in place
writer_ = nullptr;
}
}
download_active_ = false;
ErrorCode code =
successful ? ErrorCode::kSuccess : ErrorCode::kDownloadTransferError;
if (code == ErrorCode::kSuccess) {
if (delta_performer_ && !payload_->already_applied)
code = delta_performer_->VerifyPayload(payload_->hash, payload_->size);
if (code == ErrorCode::kSuccess) {
if (payload_ < &install_plan_.payloads.back() &&
SystemState::Get()->payload_state()->NextPayload()) {
LOG(INFO) << "Incrementing to next payload";
// No need to reset if this payload was already applied.
if (delta_performer_ && !payload_->already_applied)
DeltaPerformer::ResetUpdateProgress(prefs_, false);
// Start downloading next payload.
bytes_received_previous_payloads_ += payload_->size;
payload_++;
install_plan_.download_url =
SystemState::Get()->payload_state()->GetCurrentUrl();
StartDownloading();
return;
}
// All payloads have been applied and verified.
if (delegate_)
delegate_->DownloadComplete();
std::string histogram_output;
base::StatisticsRecorder::WriteGraph(
"UpdateEngine.DownloadActionChromeos.", &histogram_output);
LOG(INFO) << histogram_output;
} else {
LOG(ERROR) << "Download of " << install_plan_.download_url
<< " failed due to payload verification error.";
// Delete p2p file, if applicable.
if (!p2p_file_id_.empty())
CloseP2PSharingFd(true);
}
}
// Write the path to the output pipe if we're successful.
if (code == ErrorCode::kSuccess && HasOutputPipe())
SetOutputObject(install_plan_);
processor_->ActionComplete(this, code);
}
void DownloadActionChromeos::TransferTerminated(HttpFetcher* fetcher) {
if (code_ != ErrorCode::kSuccess) {
processor_->ActionComplete(this, code_);
} else if (payload_->already_applied) {
LOG(INFO) << "TransferTerminated with ErrorCode::kSuccess when the current "
"payload has already applied, treating as TransferComplete.";
TransferComplete(fetcher, true);
}
}
} // namespace chromeos_update_engine
|