OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "update_engine/update_attempter.h" | 5 #include "update_engine/update_attempter.h" |
6 | 6 |
7 // From 'man clock_gettime': feature test macro: _POSIX_C_SOURCE >= 199309L | 7 // From 'man clock_gettime': feature test macro: _POSIX_C_SOURCE >= 199309L |
8 #ifndef _POSIX_C_SOURCE | 8 #ifndef _POSIX_C_SOURCE |
9 #define _POSIX_C_SOURCE 199309L | 9 #define _POSIX_C_SOURCE 199309L |
10 #endif // _POSIX_C_SOURCE | 10 #endif // _POSIX_C_SOURCE |
11 #include <time.h> | 11 #include <time.h> |
12 | 12 |
13 #include <string> | 13 #include <string> |
14 #include <tr1/memory> | 14 #include <tr1/memory> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
| 17 #include <base/rand_util.h> |
17 #include <glib.h> | 18 #include <glib.h> |
18 #include <metrics/metrics_library.h> | 19 #include <metrics/metrics_library.h> |
19 | 20 |
20 #include "update_engine/dbus_service.h" | 21 #include "update_engine/dbus_service.h" |
21 #include "update_engine/download_action.h" | 22 #include "update_engine/download_action.h" |
22 #include "update_engine/filesystem_copier_action.h" | 23 #include "update_engine/filesystem_copier_action.h" |
23 #include "update_engine/libcurl_http_fetcher.h" | 24 #include "update_engine/libcurl_http_fetcher.h" |
24 #include "update_engine/multi_http_fetcher.h" | 25 #include "update_engine/multi_http_fetcher.h" |
25 #include "update_engine/omaha_request_action.h" | 26 #include "update_engine/omaha_request_action.h" |
26 #include "update_engine/omaha_request_params.h" | 27 #include "update_engine/omaha_request_params.h" |
27 #include "update_engine/omaha_response_handler_action.h" | 28 #include "update_engine/omaha_response_handler_action.h" |
28 #include "update_engine/postinstall_runner_action.h" | 29 #include "update_engine/postinstall_runner_action.h" |
29 #include "update_engine/prefs_interface.h" | 30 #include "update_engine/prefs_interface.h" |
30 #include "update_engine/update_check_scheduler.h" | 31 #include "update_engine/update_check_scheduler.h" |
31 | 32 |
32 using base::TimeDelta; | 33 using base::TimeDelta; |
33 using base::TimeTicks; | 34 using base::TimeTicks; |
34 using std::make_pair; | 35 using std::make_pair; |
35 using std::tr1::shared_ptr; | 36 using std::tr1::shared_ptr; |
36 using std::string; | 37 using std::string; |
37 using std::vector; | 38 using std::vector; |
38 | 39 |
39 namespace chromeos_update_engine { | 40 namespace chromeos_update_engine { |
40 | 41 |
41 const int UpdateAttempter::kMaxDeltaUpdateFailures = 3; | 42 const int UpdateAttempter::kMaxDeltaUpdateFailures = 3; |
42 | 43 |
43 const char* kUpdateCompletedMarker = | 44 const char* kUpdateCompletedMarker = |
44 "/var/run/update_engine_autoupdate_completed"; | 45 "/var/run/update_engine_autoupdate_completed"; |
45 | 46 |
| 47 namespace { |
| 48 const int kMaxConsecutiveObeyProxyRequests = 20; |
| 49 } // namespace {} |
| 50 |
46 const char* UpdateStatusToString(UpdateStatus status) { | 51 const char* UpdateStatusToString(UpdateStatus status) { |
47 switch (status) { | 52 switch (status) { |
48 case UPDATE_STATUS_IDLE: | 53 case UPDATE_STATUS_IDLE: |
49 return "UPDATE_STATUS_IDLE"; | 54 return "UPDATE_STATUS_IDLE"; |
50 case UPDATE_STATUS_CHECKING_FOR_UPDATE: | 55 case UPDATE_STATUS_CHECKING_FOR_UPDATE: |
51 return "UPDATE_STATUS_CHECKING_FOR_UPDATE"; | 56 return "UPDATE_STATUS_CHECKING_FOR_UPDATE"; |
52 case UPDATE_STATUS_UPDATE_AVAILABLE: | 57 case UPDATE_STATUS_UPDATE_AVAILABLE: |
53 return "UPDATE_STATUS_UPDATE_AVAILABLE"; | 58 return "UPDATE_STATUS_UPDATE_AVAILABLE"; |
54 case UPDATE_STATUS_DOWNLOADING: | 59 case UPDATE_STATUS_DOWNLOADING: |
55 return "UPDATE_STATUS_DOWNLOADING"; | 60 return "UPDATE_STATUS_DOWNLOADING"; |
(...skipping 26 matching lines...) Expand all Loading... |
82 return kActionCodeOmahaResponseHandlerError; | 87 return kActionCodeOmahaResponseHandlerError; |
83 if (type == FilesystemCopierAction::StaticType()) | 88 if (type == FilesystemCopierAction::StaticType()) |
84 return kActionCodeFilesystemCopierError; | 89 return kActionCodeFilesystemCopierError; |
85 if (type == PostinstallRunnerAction::StaticType()) | 90 if (type == PostinstallRunnerAction::StaticType()) |
86 return kActionCodePostinstallRunnerError; | 91 return kActionCodePostinstallRunnerError; |
87 | 92 |
88 return code; | 93 return code; |
89 } | 94 } |
90 | 95 |
91 UpdateAttempter::UpdateAttempter(PrefsInterface* prefs, | 96 UpdateAttempter::UpdateAttempter(PrefsInterface* prefs, |
92 MetricsLibraryInterface* metrics_lib) | 97 MetricsLibraryInterface* metrics_lib, |
| 98 DbusGlibInterface* dbus_iface) |
93 : processor_(new ActionProcessor()), | 99 : processor_(new ActionProcessor()), |
94 dbus_service_(NULL), | 100 dbus_service_(NULL), |
95 prefs_(prefs), | 101 prefs_(prefs), |
96 metrics_lib_(metrics_lib), | 102 metrics_lib_(metrics_lib), |
97 update_check_scheduler_(NULL), | 103 update_check_scheduler_(NULL), |
98 http_response_code_(0), | 104 http_response_code_(0), |
99 priority_(utils::kProcessPriorityNormal), | 105 priority_(utils::kProcessPriorityNormal), |
100 manage_priority_source_(NULL), | 106 manage_priority_source_(NULL), |
101 download_active_(false), | 107 download_active_(false), |
102 status_(UPDATE_STATUS_IDLE), | 108 status_(UPDATE_STATUS_IDLE), |
103 download_progress_(0.0), | 109 download_progress_(0.0), |
104 last_checked_time_(0), | 110 last_checked_time_(0), |
105 new_version_("0.0.0.0"), | 111 new_version_("0.0.0.0"), |
106 new_size_(0), | 112 new_size_(0), |
107 is_full_update_(false) { | 113 is_full_update_(false), |
| 114 proxy_manual_checks_(0), |
| 115 obeying_proxies_(true), |
| 116 chrome_proxy_resolver_(dbus_iface) { |
108 if (utils::FileExists(kUpdateCompletedMarker)) | 117 if (utils::FileExists(kUpdateCompletedMarker)) |
109 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT; | 118 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT; |
110 } | 119 } |
111 | 120 |
112 UpdateAttempter::~UpdateAttempter() { | 121 UpdateAttempter::~UpdateAttempter() { |
113 CleanupPriorityManagement(); | 122 CleanupPriorityManagement(); |
114 } | 123 } |
115 | 124 |
116 void UpdateAttempter::Update(const std::string& app_version, | 125 void UpdateAttempter::Update(const std::string& app_version, |
117 const std::string& omaha_url) { | 126 const std::string& omaha_url, |
| 127 bool obey_proxies) { |
118 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) { | 128 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
119 LOG(INFO) << "Not updating b/c we already updated and we're waiting for " | 129 LOG(INFO) << "Not updating b/c we already updated and we're waiting for " |
120 << "reboot"; | 130 << "reboot"; |
121 return; | 131 return; |
122 } | 132 } |
123 if (status_ != UPDATE_STATUS_IDLE) { | 133 if (status_ != UPDATE_STATUS_IDLE) { |
124 // Update in progress. Do nothing | 134 // Update in progress. Do nothing |
125 return; | 135 return; |
126 } | 136 } |
127 http_response_code_ = 0; | 137 http_response_code_ = 0; |
128 if (!omaha_request_params_.Init(app_version, omaha_url)) { | 138 if (!omaha_request_params_.Init(app_version, omaha_url)) { |
129 LOG(ERROR) << "Unable to initialize Omaha request device params."; | 139 LOG(ERROR) << "Unable to initialize Omaha request device params."; |
130 return; | 140 return; |
131 } | 141 } |
| 142 |
| 143 obeying_proxies_ = true; |
| 144 if (obey_proxies || proxy_manual_checks_ == 0) { |
| 145 LOG(INFO) << "forced to obey proxies"; |
| 146 // If forced to obey proxies, every 20th request will not use proxies |
| 147 proxy_manual_checks_++; |
| 148 LOG(INFO) << "proxy manual checks: " << proxy_manual_checks_; |
| 149 if (proxy_manual_checks_ >= kMaxConsecutiveObeyProxyRequests) { |
| 150 proxy_manual_checks_ = 0; |
| 151 obeying_proxies_ = false; |
| 152 } |
| 153 } else if (base::RandInt(0, 4) == 0) { |
| 154 obeying_proxies_ = false; |
| 155 } |
| 156 LOG_IF(INFO, !obeying_proxies_) << "To help ensure updates work, this update " |
| 157 "check we are ignoring the proxy settings and using " |
| 158 "direct connections."; |
| 159 |
132 DisableDeltaUpdateIfNeeded(); | 160 DisableDeltaUpdateIfNeeded(); |
133 CHECK(!processor_->IsRunning()); | 161 CHECK(!processor_->IsRunning()); |
134 processor_->set_delegate(this); | 162 processor_->set_delegate(this); |
135 | 163 |
136 // Actions: | 164 // Actions: |
137 shared_ptr<OmahaRequestAction> update_check_action( | 165 shared_ptr<OmahaRequestAction> update_check_action( |
138 new OmahaRequestAction(prefs_, | 166 new OmahaRequestAction(prefs_, |
139 omaha_request_params_, | 167 omaha_request_params_, |
140 NULL, | 168 NULL, |
141 new LibcurlHttpFetcher)); | 169 new LibcurlHttpFetcher(GetProxyResolver()))); |
142 shared_ptr<OmahaResponseHandlerAction> response_handler_action( | 170 shared_ptr<OmahaResponseHandlerAction> response_handler_action( |
143 new OmahaResponseHandlerAction(prefs_)); | 171 new OmahaResponseHandlerAction(prefs_)); |
144 shared_ptr<FilesystemCopierAction> filesystem_copier_action( | 172 shared_ptr<FilesystemCopierAction> filesystem_copier_action( |
145 new FilesystemCopierAction(false)); | 173 new FilesystemCopierAction(false)); |
146 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action( | 174 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action( |
147 new FilesystemCopierAction(true)); | 175 new FilesystemCopierAction(true)); |
148 shared_ptr<OmahaRequestAction> download_started_action( | 176 shared_ptr<OmahaRequestAction> download_started_action( |
149 new OmahaRequestAction(prefs_, | 177 new OmahaRequestAction(prefs_, |
150 omaha_request_params_, | 178 omaha_request_params_, |
151 new OmahaEvent( | 179 new OmahaEvent( |
152 OmahaEvent::kTypeUpdateDownloadStarted), | 180 OmahaEvent::kTypeUpdateDownloadStarted), |
153 new LibcurlHttpFetcher)); | 181 new LibcurlHttpFetcher(GetProxyResolver()))); |
154 shared_ptr<DownloadAction> download_action( | 182 shared_ptr<DownloadAction> download_action( |
155 new DownloadAction(prefs_, new MultiHttpFetcher<LibcurlHttpFetcher>)); | 183 new DownloadAction(prefs_, new MultiHttpFetcher<LibcurlHttpFetcher>( |
| 184 GetProxyResolver()))); |
156 shared_ptr<OmahaRequestAction> download_finished_action( | 185 shared_ptr<OmahaRequestAction> download_finished_action( |
157 new OmahaRequestAction(prefs_, | 186 new OmahaRequestAction(prefs_, |
158 omaha_request_params_, | 187 omaha_request_params_, |
159 new OmahaEvent( | 188 new OmahaEvent( |
160 OmahaEvent::kTypeUpdateDownloadFinished), | 189 OmahaEvent::kTypeUpdateDownloadFinished), |
161 new LibcurlHttpFetcher)); | 190 new LibcurlHttpFetcher(GetProxyResolver()))); |
162 shared_ptr<PostinstallRunnerAction> postinstall_runner_action( | 191 shared_ptr<PostinstallRunnerAction> postinstall_runner_action( |
163 new PostinstallRunnerAction); | 192 new PostinstallRunnerAction); |
164 shared_ptr<OmahaRequestAction> update_complete_action( | 193 shared_ptr<OmahaRequestAction> update_complete_action( |
165 new OmahaRequestAction(prefs_, | 194 new OmahaRequestAction(prefs_, |
166 omaha_request_params_, | 195 omaha_request_params_, |
167 new OmahaEvent(OmahaEvent::kTypeUpdateComplete), | 196 new OmahaEvent(OmahaEvent::kTypeUpdateComplete), |
168 new LibcurlHttpFetcher)); | 197 new LibcurlHttpFetcher(GetProxyResolver()))); |
169 | 198 |
170 download_action->set_delegate(this); | 199 download_action->set_delegate(this); |
171 response_handler_action_ = response_handler_action; | 200 response_handler_action_ = response_handler_action; |
172 download_action_ = download_action; | 201 download_action_ = download_action; |
173 | 202 |
174 actions_.push_back(shared_ptr<AbstractAction>(update_check_action)); | 203 actions_.push_back(shared_ptr<AbstractAction>(update_check_action)); |
175 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action)); | 204 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action)); |
176 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action)); | 205 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action)); |
177 actions_.push_back(shared_ptr<AbstractAction>( | 206 actions_.push_back(shared_ptr<AbstractAction>( |
178 kernel_filesystem_copier_action)); | 207 kernel_filesystem_copier_action)); |
(...skipping 26 matching lines...) Expand all Loading... |
205 processor_->StartProcessing(); | 234 processor_->StartProcessing(); |
206 } | 235 } |
207 | 236 |
208 void UpdateAttempter::CheckForUpdate(const std::string& app_version, | 237 void UpdateAttempter::CheckForUpdate(const std::string& app_version, |
209 const std::string& omaha_url) { | 238 const std::string& omaha_url) { |
210 if (status_ != UPDATE_STATUS_IDLE) { | 239 if (status_ != UPDATE_STATUS_IDLE) { |
211 LOG(INFO) << "Check for update requested, but status is " | 240 LOG(INFO) << "Check for update requested, but status is " |
212 << UpdateStatusToString(status_) << ", so not checking."; | 241 << UpdateStatusToString(status_) << ", so not checking."; |
213 return; | 242 return; |
214 } | 243 } |
215 Update(app_version, omaha_url); | 244 Update(app_version, omaha_url, true); |
216 } | 245 } |
217 | 246 |
218 bool UpdateAttempter::RebootIfNeeded() { | 247 bool UpdateAttempter::RebootIfNeeded() { |
219 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) { | 248 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
220 LOG(INFO) << "Reboot requested, but status is " | 249 LOG(INFO) << "Reboot requested, but status is " |
221 << UpdateStatusToString(status_) << ", so not rebooting."; | 250 << UpdateStatusToString(status_) << ", so not rebooting."; |
222 return false; | 251 return false; |
223 } | 252 } |
224 TEST_AND_RETURN_FALSE(utils::Reboot()); | 253 TEST_AND_RETURN_FALSE(utils::Reboot()); |
225 return true; | 254 return true; |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 | 457 |
429 bool UpdateAttempter::ScheduleErrorEventAction() { | 458 bool UpdateAttempter::ScheduleErrorEventAction() { |
430 if (error_event_.get() == NULL) | 459 if (error_event_.get() == NULL) |
431 return false; | 460 return false; |
432 | 461 |
433 LOG(INFO) << "Update failed -- reporting the error event."; | 462 LOG(INFO) << "Update failed -- reporting the error event."; |
434 shared_ptr<OmahaRequestAction> error_event_action( | 463 shared_ptr<OmahaRequestAction> error_event_action( |
435 new OmahaRequestAction(prefs_, | 464 new OmahaRequestAction(prefs_, |
436 omaha_request_params_, | 465 omaha_request_params_, |
437 error_event_.release(), // Pass ownership. | 466 error_event_.release(), // Pass ownership. |
438 new LibcurlHttpFetcher)); | 467 new LibcurlHttpFetcher(GetProxyResolver()))); |
439 actions_.push_back(shared_ptr<AbstractAction>(error_event_action)); | 468 actions_.push_back(shared_ptr<AbstractAction>(error_event_action)); |
440 processor_->EnqueueAction(error_event_action.get()); | 469 processor_->EnqueueAction(error_event_action.get()); |
441 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT); | 470 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT); |
442 processor_->StartProcessing(); | 471 processor_->StartProcessing(); |
443 return true; | 472 return true; |
444 } | 473 } |
445 | 474 |
446 void UpdateAttempter::SetPriority(utils::ProcessPriority priority) { | 475 void UpdateAttempter::SetPriority(utils::ProcessPriority priority) { |
447 if (priority_ == priority) { | 476 if (priority_ == priority) { |
448 return; | 477 return; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 if (resume_offset < response_handler_action_->install_plan().size) { | 556 if (resume_offset < response_handler_action_->install_plan().size) { |
528 ranges.push_back(make_pair(resume_offset, -1)); | 557 ranges.push_back(make_pair(resume_offset, -1)); |
529 } | 558 } |
530 } else { | 559 } else { |
531 ranges.push_back(make_pair(0, -1)); | 560 ranges.push_back(make_pair(0, -1)); |
532 } | 561 } |
533 fetcher->set_ranges(ranges); | 562 fetcher->set_ranges(ranges); |
534 } | 563 } |
535 | 564 |
536 } // namespace chromeos_update_engine | 565 } // namespace chromeos_update_engine |
OLD | NEW |