blob: 7873166551ec5bf7d2961ed391f03aa9c9336ee4 [file] [log] [blame]
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07001// 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
3// found in the LICENSE file.
4
5#include "update_engine/update_attempter.h"
Andrew de los Reyes63b96d72010-05-10 13:08:54 -07006
7// From 'man clock_gettime': feature test macro: _POSIX_C_SOURCE >= 199309L
8#ifndef _POSIX_C_SOURCE
9#define _POSIX_C_SOURCE 199309L
10#endif // _POSIX_C_SOURCE
11#include <time.h>
12
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070013#include <tr1/memory>
14#include <string>
15#include <vector>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070016
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070017#include <glib.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070018
19#include "metrics/metrics_library.h"
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070020#include "update_engine/dbus_service.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070021#include "update_engine/download_action.h"
22#include "update_engine/filesystem_copier_action.h"
23#include "update_engine/libcurl_http_fetcher.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070024#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070025#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070026#include "update_engine/omaha_response_handler_action.h"
27#include "update_engine/postinstall_runner_action.h"
28#include "update_engine/set_bootable_flag_action.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070029
Darin Petkovaf183052010-08-23 12:07:13 -070030using base::TimeDelta;
31using base::TimeTicks;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070032using std::tr1::shared_ptr;
33using std::string;
34using std::vector;
35
36namespace chromeos_update_engine {
37
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070038const char* kUpdateCompletedMarker = "/tmp/update_engine_autoupdate_completed";
39
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070040const char* UpdateStatusToString(UpdateStatus status) {
41 switch (status) {
42 case UPDATE_STATUS_IDLE:
43 return "UPDATE_STATUS_IDLE";
44 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
45 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
46 case UPDATE_STATUS_UPDATE_AVAILABLE:
47 return "UPDATE_STATUS_UPDATE_AVAILABLE";
48 case UPDATE_STATUS_DOWNLOADING:
49 return "UPDATE_STATUS_DOWNLOADING";
50 case UPDATE_STATUS_VERIFYING:
51 return "UPDATE_STATUS_VERIFYING";
52 case UPDATE_STATUS_FINALIZING:
53 return "UPDATE_STATUS_FINALIZING";
54 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
55 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
Darin Petkov09f96c32010-07-20 09:24:57 -070056 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
57 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070058 default:
59 return "unknown status";
60 }
61}
62
Darin Petkov777dbfa2010-07-20 15:03:37 -070063// Turns a generic kActionCodeError to a generic error code specific
64// to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is
65// not kActionCodeError, or the action is not matched, returns |code|
66// unchanged.
67ActionExitCode GetErrorCodeForAction(AbstractAction* action,
68 ActionExitCode code) {
69 if (code != kActionCodeError)
70 return code;
71
72 const string type = action->Type();
73 if (type == OmahaRequestAction::StaticType())
74 return kActionCodeOmahaRequestError;
75 if (type == OmahaResponseHandlerAction::StaticType())
76 return kActionCodeOmahaResponseHandlerError;
77 if (type == FilesystemCopierAction::StaticType())
78 return kActionCodeFilesystemCopierError;
79 if (type == PostinstallRunnerAction::StaticType())
80 return kActionCodePostinstallRunnerError;
81 if (type == SetBootableFlagAction::StaticType())
82 return kActionCodeSetBootableFlagError;
83
84 return code;
85}
86
Darin Petkovc6c135c2010-08-11 13:36:18 -070087UpdateAttempter::UpdateAttempter(PrefsInterface* prefs,
88 MetricsLibraryInterface* metrics_lib)
89 : dbus_service_(NULL),
90 prefs_(prefs),
91 metrics_lib_(metrics_lib),
92 priority_(utils::kProcessPriorityNormal),
93 manage_priority_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -070094 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -070095 status_(UPDATE_STATUS_IDLE),
96 download_progress_(0.0),
97 last_checked_time_(0),
98 new_version_("0.0.0.0"),
99 new_size_(0) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700100 if (utils::FileExists(kUpdateCompletedMarker))
101 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
102}
103
104UpdateAttempter::~UpdateAttempter() {
105 CleanupPriorityManagement();
106}
107
Darin Petkov5a7f5652010-07-22 21:40:09 -0700108void UpdateAttempter::Update(const std::string& app_version,
109 const std::string& omaha_url) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700110 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
111 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
112 << "reboot";
113 return;
114 }
115 if (status_ != UPDATE_STATUS_IDLE) {
116 // Update in progress. Do nothing
117 return;
118 }
Darin Petkov5a7f5652010-07-22 21:40:09 -0700119 if (!omaha_request_params_.Init(app_version, omaha_url)) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700120 LOG(ERROR) << "Unable to initialize Omaha request device params.";
121 return;
122 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700123 CHECK(!processor_.IsRunning());
124 processor_.set_delegate(this);
125
126 // Actions:
Darin Petkov6a5b3222010-07-13 14:55:28 -0700127 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700128 new OmahaRequestAction(prefs_,
129 omaha_request_params_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700130 NULL,
131 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700132 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
133 new OmahaResponseHandlerAction);
134 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700135 new FilesystemCopierAction(false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700136 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700137 new FilesystemCopierAction(true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700138 shared_ptr<OmahaRequestAction> download_started_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700139 new OmahaRequestAction(prefs_,
140 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700141 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700142 OmahaEvent::kTypeUpdateDownloadStarted),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700143 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700144 shared_ptr<DownloadAction> download_action(
145 new DownloadAction(new LibcurlHttpFetcher));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700146 shared_ptr<OmahaRequestAction> download_finished_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700147 new OmahaRequestAction(prefs_,
148 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700149 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700150 OmahaEvent::kTypeUpdateDownloadFinished),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700151 new LibcurlHttpFetcher));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700152 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit(
153 new PostinstallRunnerAction(true));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700154 shared_ptr<SetBootableFlagAction> set_bootable_flag_action(
155 new SetBootableFlagAction);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700156 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit(
157 new PostinstallRunnerAction(false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700158 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700159 new OmahaRequestAction(prefs_,
160 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700161 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700162 new LibcurlHttpFetcher));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700163
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700164 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700165 response_handler_action_ = response_handler_action;
166
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700167 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
168 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
169 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700170 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700171 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700172 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700173 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700174 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700175 actions_.push_back(shared_ptr<AbstractAction>(
176 postinstall_runner_action_precommit));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700177 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700178 actions_.push_back(shared_ptr<AbstractAction>(
179 postinstall_runner_action_postcommit));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700180 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700181
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700182 // Enqueue the actions
183 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
184 it != actions_.end(); ++it) {
185 processor_.EnqueueAction(it->get());
186 }
187
188 // Bond them together. We have to use the leaf-types when calling
189 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700190 BondActions(update_check_action.get(),
191 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700192 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700193 filesystem_copier_action.get());
194 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700195 kernel_filesystem_copier_action.get());
196 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700197 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700198 BondActions(download_action.get(),
199 postinstall_runner_action_precommit.get());
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700200 BondActions(postinstall_runner_action_precommit.get(),
201 set_bootable_flag_action.get());
202 BondActions(set_bootable_flag_action.get(),
203 postinstall_runner_action_postcommit.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700204
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700205 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700206 processor_.StartProcessing();
207}
208
Darin Petkov5a7f5652010-07-22 21:40:09 -0700209void UpdateAttempter::CheckForUpdate(const std::string& app_version,
210 const std::string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700211 if (status_ != UPDATE_STATUS_IDLE) {
212 LOG(INFO) << "Check for update requested, but status is "
213 << UpdateStatusToString(status_) << ", so not checking.";
214 return;
215 }
Darin Petkov5a7f5652010-07-22 21:40:09 -0700216 Update(app_version, omaha_url);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700217}
218
Darin Petkov296889c2010-07-23 16:20:54 -0700219bool UpdateAttempter::RebootIfNeeded() {
220 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
221 LOG(INFO) << "Reboot requested, but status is "
222 << UpdateStatusToString(status_) << ", so not rebooting.";
223 return false;
224 }
225 TEST_AND_RETURN_FALSE(utils::Reboot());
226 return true;
227}
228
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700229// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700230void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700231 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700232 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700233 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700234 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700235
Darin Petkovc6c135c2010-08-11 13:36:18 -0700236 // Reset process priority back to normal.
237 CleanupPriorityManagement();
238
Darin Petkov09f96c32010-07-20 09:24:57 -0700239 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
240 LOG(INFO) << "Error event sent.";
241 SetStatusAndNotify(UPDATE_STATUS_IDLE);
242 return;
243 }
244
Darin Petkovc1a8b422010-07-19 11:34:49 -0700245 if (code == kActionCodeSuccess) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700246 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700247 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700248
249 // Report the time it took to update the system.
250 int64_t update_time = time(NULL) - last_checked_time_;
251 metrics_lib_->SendToUMA("Installer.UpdateTime",
252 static_cast<int>(update_time), // sample
253 1, // min = 1 second
254 20 * 60, // max = 20 minutes
255 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700256 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700257 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700258
259 LOG(INFO) << "Update failed.";
260 if (ScheduleErrorEventAction())
261 return;
262 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700263}
264
265void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700266 // Reset process priority back to normal.
267 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700268 download_progress_ = 0.0;
269 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700270 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700271 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700272}
273
274// Called whenever an action has finished processing, either successfully
275// or otherwise.
276void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
277 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700278 ActionExitCode code) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700279 // Reset download progress regardless of whether or not the download action
280 // succeeded.
281 const string type = action->Type();
282 if (type == DownloadAction::StaticType())
283 download_progress_ = 0.0;
Darin Petkov09f96c32010-07-20 09:24:57 -0700284 if (code != kActionCodeSuccess) {
Darin Petkov777dbfa2010-07-20 15:03:37 -0700285 // On failure, schedule an error event to be sent to Omaha.
286 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700287 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700288 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700289 // Find out which action completed.
290 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700291 // Note that the status will be updated to DOWNLOADING when some
292 // bytes get actually downloaded from the server and the
293 // BytesReceived callback is invoked. This avoids notifying the
294 // user that a download has started in cases when the server and
295 // the client are unable to initiate the download.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700296 OmahaResponseHandlerAction* omaha_response_handler_action =
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700297 dynamic_cast<OmahaResponseHandlerAction*>(action);
298 CHECK(omaha_response_handler_action);
299 const InstallPlan& plan = omaha_response_handler_action->install_plan();
300 last_checked_time_ = time(NULL);
301 // TODO(adlr): put version in InstallPlan
302 new_version_ = "0.0.0.0";
303 new_size_ = plan.size;
Darin Petkovc6c135c2010-08-11 13:36:18 -0700304 SetupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700305 } else if (type == DownloadAction::StaticType()) {
306 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
307 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700308}
309
310// Stop updating. An attempt will be made to record status to the disk
311// so that updates can be resumed later.
312void UpdateAttempter::Terminate() {
313 // TODO(adlr): implement this method.
314 NOTIMPLEMENTED();
315}
316
317// Try to resume from a previously Terminate()d update.
318void UpdateAttempter::ResumeUpdating() {
319 // TODO(adlr): implement this method.
320 NOTIMPLEMENTED();
321}
322
Darin Petkov9d911fa2010-08-19 09:36:08 -0700323void UpdateAttempter::SetDownloadStatus(bool active) {
324 download_active_ = active;
325 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
326}
327
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700328void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700329 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700330 LOG(ERROR) << "BytesReceived called while not downloading.";
331 return;
332 }
Darin Petkovaf183052010-08-23 12:07:13 -0700333 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700334 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700335 // Self throttle based on progress. Also send notifications if
336 // progress is too slow.
337 const double kDeltaPercent = 0.01; // 1%
338 if (status_ != UPDATE_STATUS_DOWNLOADING ||
339 bytes_received == total ||
340 progress - download_progress_ >= kDeltaPercent ||
341 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
342 download_progress_ = progress;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700343 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
344 }
345}
346
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700347bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
348 double* progress,
349 std::string* current_operation,
350 std::string* new_version,
351 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700352 *last_checked_time = last_checked_time_;
353 *progress = download_progress_;
354 *current_operation = UpdateStatusToString(status_);
355 *new_version = new_version_;
356 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700357 return true;
358}
359
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700360void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
361 status_ = status;
362 if (!dbus_service_)
363 return;
Darin Petkovaf183052010-08-23 12:07:13 -0700364 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700365 update_engine_service_emit_status_update(
366 dbus_service_,
367 last_checked_time_,
368 download_progress_,
369 UpdateStatusToString(status_),
370 new_version_.c_str(),
371 new_size_);
372}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700373
Darin Petkov777dbfa2010-07-20 15:03:37 -0700374void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
375 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700376 if (error_event_.get()) {
377 // This shouldn't really happen.
378 LOG(WARNING) << "There's already an existing pending error event.";
379 return;
380 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700381
382 // For now assume that Omaha response action failure means that
383 // there's no update so don't send an event. Also, double check that
384 // the failure has not occurred while sending an error event -- in
385 // which case don't schedule another. This shouldn't really happen
386 // but just in case...
387 if (action->Type() == OmahaResponseHandlerAction::StaticType() ||
388 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
389 return;
390 }
391
392 code = GetErrorCodeForAction(action, code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700393 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
394 OmahaEvent::kResultError,
395 code));
396}
397
398bool UpdateAttempter::ScheduleErrorEventAction() {
399 if (error_event_.get() == NULL)
400 return false;
401
402 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700403 new OmahaRequestAction(prefs_,
404 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700405 error_event_.release(), // Pass ownership.
406 new LibcurlHttpFetcher));
407 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
408 processor_.EnqueueAction(error_event_action.get());
409 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
410 processor_.StartProcessing();
411 return true;
412}
413
Darin Petkovc6c135c2010-08-11 13:36:18 -0700414void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
415 if (priority_ == priority) {
416 return;
417 }
418 if (utils::SetProcessPriority(priority)) {
419 priority_ = priority;
420 LOG(INFO) << "Process priority = " << priority_;
421 }
422}
423
424void UpdateAttempter::SetupPriorityManagement() {
425 if (manage_priority_source_) {
426 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
427 CleanupPriorityManagement();
428 }
429 const int kPriorityTimeout = 10 * 60; // 10 minutes
430 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
431 g_source_set_callback(manage_priority_source_,
432 StaticManagePriorityCallback,
433 this,
434 NULL);
435 g_source_attach(manage_priority_source_, NULL);
436 SetPriority(utils::kProcessPriorityLow);
437}
438
439void UpdateAttempter::CleanupPriorityManagement() {
440 if (manage_priority_source_) {
441 g_source_destroy(manage_priority_source_);
442 manage_priority_source_ = NULL;
443 }
444 SetPriority(utils::kProcessPriorityNormal);
445}
446
447gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
448 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
449}
450
451bool UpdateAttempter::ManagePriorityCallback() {
452 // If the current process priority is below normal, set it to normal
453 // and let GLib invoke this callback again.
454 if (utils::ComparePriorities(priority_, utils::kProcessPriorityNormal) < 0) {
455 SetPriority(utils::kProcessPriorityNormal);
456 return true;
457 }
458 // Set the priority to high and let GLib destroy the timeout source.
459 SetPriority(utils::kProcessPriorityHigh);
460 manage_priority_source_ = NULL;
461 return false;
462}
463
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700464} // namespace chromeos_update_engine