Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 5 | #include "update_engine/download_action.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 6 | #include <errno.h> |
| 7 | #include <algorithm> |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 10 | #include <glib.h> |
| 11 | #include "update_engine/action_pipe.h" |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 12 | #include "update_engine/subprocess.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 13 | |
| 14 | using std::min; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 15 | using std::string; |
| 16 | using std::vector; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 17 | |
| 18 | namespace chromeos_update_engine { |
| 19 | |
Darin Petkov | e971f33 | 2010-09-22 16:57:25 -0700 | [diff] [blame] | 20 | // Use a buffer to reduce the number of IOPS on SSD devices. |
| 21 | const size_t kFileWriterBufferSize = 128 * 1024; // 128 KiB |
| 22 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 23 | DownloadAction::DownloadAction(PrefsInterface* prefs, |
| 24 | HttpFetcher* http_fetcher) |
| 25 | : prefs_(prefs), |
| 26 | writer_(NULL), |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 27 | http_fetcher_(http_fetcher), |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 28 | code_(kActionCodeSuccess), |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 29 | delegate_(NULL), |
Andrew de los Reyes | 21816e1 | 2011-04-07 14:18:56 -0700 | [diff] [blame] | 30 | bytes_received_(0), |
| 31 | skip_reporting_signature_fail_(NULL) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 32 | |
| 33 | DownloadAction::~DownloadAction() {} |
| 34 | |
| 35 | void DownloadAction::PerformAction() { |
| 36 | http_fetcher_->set_delegate(this); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 37 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 38 | // Get the InstallPlan and read it |
| 39 | CHECK(HasInputObject()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 40 | install_plan_ = GetInputObject(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 41 | bytes_received_ = 0; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 42 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 43 | install_plan_.Dump(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 44 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 45 | if (writer_) { |
| 46 | LOG(INFO) << "Using writer for test."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 47 | } else { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 48 | if (install_plan_.is_full_update) { |
| 49 | kernel_file_writer_.reset(new DirectFileWriter); |
| 50 | rootfs_file_writer_.reset(new DirectFileWriter); |
Darin Petkov | e971f33 | 2010-09-22 16:57:25 -0700 | [diff] [blame] | 51 | kernel_buffered_file_writer_.reset( |
| 52 | new BufferedFileWriter(kernel_file_writer_.get(), |
| 53 | kFileWriterBufferSize)); |
| 54 | rootfs_buffered_file_writer_.reset( |
| 55 | new BufferedFileWriter(rootfs_file_writer_.get(), |
| 56 | kFileWriterBufferSize)); |
| 57 | split_file_writer_.reset( |
| 58 | new SplitFileWriter(kernel_buffered_file_writer_.get(), |
| 59 | rootfs_buffered_file_writer_.get())); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 60 | split_file_writer_->SetFirstOpenArgs( |
| 61 | install_plan_.kernel_install_path.c_str(), |
| 62 | O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, |
| 63 | 0644); |
| 64 | decompressing_file_writer_.reset( |
| 65 | new GzipDecompressingFileWriter(split_file_writer_.get())); |
| 66 | writer_ = decompressing_file_writer_.get(); |
| 67 | } else { |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 68 | delta_performer_.reset(new DeltaPerformer(prefs_)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 69 | delta_performer_->set_current_kernel_hash(install_plan_.kernel_hash); |
| 70 | delta_performer_->set_current_rootfs_hash(install_plan_.rootfs_hash); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 71 | writer_ = delta_performer_.get(); |
| 72 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 73 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 74 | int rc = writer_->Open(install_plan_.install_path.c_str(), |
| 75 | O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE, |
| 76 | 0644); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 77 | if (rc < 0) { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 78 | LOG(ERROR) << "Unable to open output file " << install_plan_.install_path; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 79 | // report error to processor |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 80 | processor_->ActionComplete(this, kActionCodeInstallDeviceOpenError); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 81 | return; |
| 82 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 83 | if (!install_plan_.is_full_update) { |
| 84 | if (!delta_performer_->OpenKernel( |
| 85 | install_plan_.kernel_install_path.c_str())) { |
| 86 | LOG(ERROR) << "Unable to open kernel file " |
| 87 | << install_plan_.kernel_install_path.c_str(); |
| 88 | writer_->Close(); |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 89 | processor_->ActionComplete(this, kActionCodeKernelDeviceOpenError); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | } |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 93 | if (delegate_) { |
| 94 | delegate_->SetDownloadStatus(true); // Set to active. |
| 95 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 96 | http_fetcher_->BeginTransfer(install_plan_.download_url); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void DownloadAction::TerminateProcessing() { |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 100 | if (writer_) { |
| 101 | LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer."; |
| 102 | writer_ = NULL; |
| 103 | } |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 104 | if (delegate_) { |
| 105 | delegate_->SetDownloadStatus(false); // Set to inactive. |
| 106 | } |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 107 | // Terminates the transfer. The action is terminated, if necessary, when the |
| 108 | // TransferTerminated callback is received. |
| 109 | http_fetcher_->TerminateTransfer(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 112 | void DownloadAction::SeekToOffset(off_t offset) { |
| 113 | bytes_received_ = offset; |
| 114 | } |
| 115 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 116 | void DownloadAction::ReceivedBytes(HttpFetcher *fetcher, |
| 117 | const char* bytes, |
| 118 | int length) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 119 | bytes_received_ += length; |
| 120 | if (delegate_) |
| 121 | delegate_->BytesReceived(bytes_received_, install_plan_.size); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 122 | if (writer_ && writer_->Write(bytes, length) < 0) { |
| 123 | LOG(ERROR) << "Write error -- terminating processing."; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 124 | // Don't tell the action processor that the action is complete until we get |
| 125 | // the TransferTerminated callback. Otherwise, this and the HTTP fetcher |
| 126 | // objects may get destroyed before all callbacks are complete. |
| 127 | code_ = kActionCodeDownloadWriteError; |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 128 | TerminateProcessing(); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 129 | return; |
| 130 | } |
Darin Petkov | cdc20a9 | 2010-10-27 14:04:37 -0700 | [diff] [blame] | 131 | // DeltaPerformer checks the hashes for delta updates. |
| 132 | if (install_plan_.is_full_update) { |
| 133 | omaha_hash_calculator_.Update(bytes, length); |
| 134 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 137 | namespace { |
| 138 | void FlushLinuxCaches() { |
| 139 | vector<string> command; |
| 140 | command.push_back("/bin/sync"); |
| 141 | int rc; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 142 | LOG(INFO) << "FlushLinuxCaches-sync..."; |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame^] | 143 | Subprocess::SynchronousExec(command, &rc, NULL); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 144 | LOG(INFO) << "FlushLinuxCaches-drop_caches..."; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 145 | |
| 146 | const char* const drop_cmd = "3\n"; |
| 147 | utils::WriteFile("/proc/sys/vm/drop_caches", drop_cmd, strlen(drop_cmd)); |
| 148 | |
| 149 | LOG(INFO) << "FlushLinuxCaches done."; |
| 150 | } |
| 151 | } |
| 152 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 153 | void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) { |
| 154 | if (writer_) { |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 155 | LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 156 | writer_ = NULL; |
| 157 | } |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 158 | if (delegate_) { |
| 159 | delegate_->SetDownloadStatus(false); // Set to inactive. |
| 160 | } |
Andrew de los Reyes | 21816e1 | 2011-04-07 14:18:56 -0700 | [diff] [blame] | 161 | bool signature_verify_failed = false; |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 162 | ActionExitCode code = |
| 163 | successful ? kActionCodeSuccess : kActionCodeDownloadTransferError; |
| 164 | if (code == kActionCodeSuccess) { |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 165 | if (!install_plan_.is_full_update) { |
| 166 | if (!delta_performer_->VerifyPayload("", |
| 167 | install_plan_.download_hash, |
Andrew de los Reyes | fb830ba | 2011-04-04 11:42:43 -0700 | [diff] [blame] | 168 | install_plan_.size, |
Andrew de los Reyes | 21816e1 | 2011-04-07 14:18:56 -0700 | [diff] [blame] | 169 | &signature_verify_failed)) { |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 170 | LOG(ERROR) << "Download of " << install_plan_.download_url |
| 171 | << " failed due to payload verification error."; |
| 172 | code = kActionCodeDownloadPayloadVerificationError; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 173 | } else if (!delta_performer_->GetNewPartitionInfo( |
| 174 | &install_plan_.kernel_size, |
| 175 | &install_plan_.kernel_hash, |
| 176 | &install_plan_.rootfs_size, |
| 177 | &install_plan_.rootfs_hash)) { |
| 178 | LOG(ERROR) << "Unable to get new partition hash info."; |
| 179 | code = kActionCodeDownloadNewPartitionInfoError; |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 180 | } |
| 181 | } else { |
| 182 | // Makes sure the hash and size are correct for an old-style full update. |
| 183 | omaha_hash_calculator_.Finalize(); |
| 184 | if (omaha_hash_calculator_.hash() != install_plan_.download_hash) { |
| 185 | LOG(ERROR) << "Download of " << install_plan_.download_url |
| 186 | << " failed. Expected hash " << install_plan_.download_hash |
| 187 | << " but got hash " << omaha_hash_calculator_.hash(); |
| 188 | code = kActionCodeDownloadHashMismatchError; |
| 189 | } else if (bytes_received_ != install_plan_.size) { |
| 190 | LOG(ERROR) << "Download of " << install_plan_.download_url |
| 191 | << " failed. Expected size " << install_plan_.size |
| 192 | << " but got size " << bytes_received_; |
| 193 | code = kActionCodeDownloadSizeMismatchError; |
| 194 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 197 | |
Andrew de los Reyes | 21816e1 | 2011-04-07 14:18:56 -0700 | [diff] [blame] | 198 | if (skip_reporting_signature_fail_.get() && |
| 199 | (code != kActionCodeSuccess || !signature_verify_failed)) { |
| 200 | LOG(INFO) << "Suppressing signature pub key verification warning"; |
| 201 | skip_reporting_signature_fail_->Run(); |
| 202 | } |
| 203 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 204 | FlushLinuxCaches(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 205 | |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 206 | // Write the path to the output pipe if we're successful. |
| 207 | if (code == kActionCodeSuccess && HasOutputPipe()) |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 208 | SetOutputObject(install_plan_); |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 209 | processor_->ActionComplete(this, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 212 | void DownloadAction::TransferTerminated(HttpFetcher *fetcher) { |
| 213 | if (code_ != kActionCodeSuccess) { |
| 214 | processor_->ActionComplete(this, code_); |
| 215 | } |
| 216 | } |
| 217 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 218 | }; // namespace {} |