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