blob: 2adb2455da56c228ed1269ab2073fe95ef8bd001 [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
30using std::tr1::shared_ptr;
31using std::string;
32using std::vector;
33
34namespace chromeos_update_engine {
35
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070036const char* kUpdateCompletedMarker = "/tmp/update_engine_autoupdate_completed";
37
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070038namespace {
39// Returns true on success.
40bool GetCPUClockTime(struct timespec* out) {
41 return clock_gettime(CLOCK_REALTIME, out) == 0;
42}
43// Returns stop - start.
44struct timespec CPUClockTimeElapsed(const struct timespec& start,
45 const struct timespec& stop) {
46 CHECK(start.tv_sec >= 0);
47 CHECK(stop.tv_sec >= 0);
48 CHECK(start.tv_nsec >= 0);
49 CHECK(stop.tv_nsec >= 0);
50
51 const int64_t kOneBillion = 1000000000L;
52 const int64_t start64 = start.tv_sec * kOneBillion + start.tv_nsec;
53 const int64_t stop64 = stop.tv_sec * kOneBillion + stop.tv_nsec;
54
55 const int64_t result64 = stop64 - start64;
56
57 struct timespec ret;
58 ret.tv_sec = result64 / kOneBillion;
59 ret.tv_nsec = result64 % kOneBillion;
60
61 return ret;
62}
63bool CPUClockTimeGreaterThanHalfSecond(const struct timespec& spec) {
64 if (spec.tv_sec >= 1)
65 return true;
66 return (spec.tv_nsec > 500000000);
67}
68}
69
70const char* UpdateStatusToString(UpdateStatus status) {
71 switch (status) {
72 case UPDATE_STATUS_IDLE:
73 return "UPDATE_STATUS_IDLE";
74 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
75 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
76 case UPDATE_STATUS_UPDATE_AVAILABLE:
77 return "UPDATE_STATUS_UPDATE_AVAILABLE";
78 case UPDATE_STATUS_DOWNLOADING:
79 return "UPDATE_STATUS_DOWNLOADING";
80 case UPDATE_STATUS_VERIFYING:
81 return "UPDATE_STATUS_VERIFYING";
82 case UPDATE_STATUS_FINALIZING:
83 return "UPDATE_STATUS_FINALIZING";
84 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
85 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
Darin Petkov09f96c32010-07-20 09:24:57 -070086 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
87 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070088 default:
89 return "unknown status";
90 }
91}
92
Darin Petkov777dbfa2010-07-20 15:03:37 -070093// Turns a generic kActionCodeError to a generic error code specific
94// to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is
95// not kActionCodeError, or the action is not matched, returns |code|
96// unchanged.
97ActionExitCode GetErrorCodeForAction(AbstractAction* action,
98 ActionExitCode code) {
99 if (code != kActionCodeError)
100 return code;
101
102 const string type = action->Type();
103 if (type == OmahaRequestAction::StaticType())
104 return kActionCodeOmahaRequestError;
105 if (type == OmahaResponseHandlerAction::StaticType())
106 return kActionCodeOmahaResponseHandlerError;
107 if (type == FilesystemCopierAction::StaticType())
108 return kActionCodeFilesystemCopierError;
109 if (type == PostinstallRunnerAction::StaticType())
110 return kActionCodePostinstallRunnerError;
111 if (type == SetBootableFlagAction::StaticType())
112 return kActionCodeSetBootableFlagError;
113
114 return code;
115}
116
Darin Petkovc6c135c2010-08-11 13:36:18 -0700117UpdateAttempter::UpdateAttempter(PrefsInterface* prefs,
118 MetricsLibraryInterface* metrics_lib)
119 : dbus_service_(NULL),
120 prefs_(prefs),
121 metrics_lib_(metrics_lib),
122 priority_(utils::kProcessPriorityNormal),
123 manage_priority_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -0700124 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700125 status_(UPDATE_STATUS_IDLE),
126 download_progress_(0.0),
127 last_checked_time_(0),
128 new_version_("0.0.0.0"),
129 new_size_(0) {
130 last_notify_time_.tv_sec = 0;
131 last_notify_time_.tv_nsec = 0;
132 if (utils::FileExists(kUpdateCompletedMarker))
133 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
134}
135
136UpdateAttempter::~UpdateAttempter() {
137 CleanupPriorityManagement();
138}
139
Darin Petkov5a7f5652010-07-22 21:40:09 -0700140void UpdateAttempter::Update(const std::string& app_version,
141 const std::string& omaha_url) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700142 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
143 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
144 << "reboot";
145 return;
146 }
147 if (status_ != UPDATE_STATUS_IDLE) {
148 // Update in progress. Do nothing
149 return;
150 }
Darin Petkov5a7f5652010-07-22 21:40:09 -0700151 if (!omaha_request_params_.Init(app_version, omaha_url)) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700152 LOG(ERROR) << "Unable to initialize Omaha request device params.";
153 return;
154 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700155 CHECK(!processor_.IsRunning());
156 processor_.set_delegate(this);
157
158 // Actions:
Darin Petkov6a5b3222010-07-13 14:55:28 -0700159 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700160 new OmahaRequestAction(prefs_,
161 omaha_request_params_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700162 NULL,
163 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700164 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
165 new OmahaResponseHandlerAction);
166 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700167 new FilesystemCopierAction(false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700168 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700169 new FilesystemCopierAction(true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700170 shared_ptr<OmahaRequestAction> download_started_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700171 new OmahaRequestAction(prefs_,
172 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700173 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700174 OmahaEvent::kTypeUpdateDownloadStarted),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700175 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700176 shared_ptr<DownloadAction> download_action(
177 new DownloadAction(new LibcurlHttpFetcher));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700178 shared_ptr<OmahaRequestAction> download_finished_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700179 new OmahaRequestAction(prefs_,
180 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700181 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700182 OmahaEvent::kTypeUpdateDownloadFinished),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700183 new LibcurlHttpFetcher));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700184 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit(
185 new PostinstallRunnerAction(true));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700186 shared_ptr<SetBootableFlagAction> set_bootable_flag_action(
187 new SetBootableFlagAction);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700188 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit(
189 new PostinstallRunnerAction(false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700190 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700191 new OmahaRequestAction(prefs_,
192 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700193 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700194 new LibcurlHttpFetcher));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700195
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700196 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700197 response_handler_action_ = response_handler_action;
198
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700199 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
200 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
201 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700202 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700203 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700204 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700205 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700206 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700207 actions_.push_back(shared_ptr<AbstractAction>(
208 postinstall_runner_action_precommit));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700209 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700210 actions_.push_back(shared_ptr<AbstractAction>(
211 postinstall_runner_action_postcommit));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700212 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700213
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700214 // Enqueue the actions
215 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
216 it != actions_.end(); ++it) {
217 processor_.EnqueueAction(it->get());
218 }
219
220 // Bond them together. We have to use the leaf-types when calling
221 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700222 BondActions(update_check_action.get(),
223 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700224 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700225 filesystem_copier_action.get());
226 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700227 kernel_filesystem_copier_action.get());
228 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700229 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700230 BondActions(download_action.get(),
231 postinstall_runner_action_precommit.get());
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700232 BondActions(postinstall_runner_action_precommit.get(),
233 set_bootable_flag_action.get());
234 BondActions(set_bootable_flag_action.get(),
235 postinstall_runner_action_postcommit.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700236
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700237 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700238 processor_.StartProcessing();
239}
240
Darin Petkov5a7f5652010-07-22 21:40:09 -0700241void UpdateAttempter::CheckForUpdate(const std::string& app_version,
242 const std::string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700243 if (status_ != UPDATE_STATUS_IDLE) {
244 LOG(INFO) << "Check for update requested, but status is "
245 << UpdateStatusToString(status_) << ", so not checking.";
246 return;
247 }
Darin Petkov5a7f5652010-07-22 21:40:09 -0700248 Update(app_version, omaha_url);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700249}
250
Darin Petkov296889c2010-07-23 16:20:54 -0700251bool UpdateAttempter::RebootIfNeeded() {
252 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
253 LOG(INFO) << "Reboot requested, but status is "
254 << UpdateStatusToString(status_) << ", so not rebooting.";
255 return false;
256 }
257 TEST_AND_RETURN_FALSE(utils::Reboot());
258 return true;
259}
260
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700261// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700262void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700263 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700264 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700265 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700266 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700267
Darin Petkovc6c135c2010-08-11 13:36:18 -0700268 // Reset process priority back to normal.
269 CleanupPriorityManagement();
270
Darin Petkov09f96c32010-07-20 09:24:57 -0700271 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
272 LOG(INFO) << "Error event sent.";
273 SetStatusAndNotify(UPDATE_STATUS_IDLE);
274 return;
275 }
276
Darin Petkovc1a8b422010-07-19 11:34:49 -0700277 if (code == kActionCodeSuccess) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700278 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700279 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700280
281 // Report the time it took to update the system.
282 int64_t update_time = time(NULL) - last_checked_time_;
283 metrics_lib_->SendToUMA("Installer.UpdateTime",
284 static_cast<int>(update_time), // sample
285 1, // min = 1 second
286 20 * 60, // max = 20 minutes
287 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700288 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700289 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700290
291 LOG(INFO) << "Update failed.";
292 if (ScheduleErrorEventAction())
293 return;
294 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700295}
296
297void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700298 // Reset process priority back to normal.
299 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700300 download_progress_ = 0.0;
301 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700302 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700303 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700304}
305
306// Called whenever an action has finished processing, either successfully
307// or otherwise.
308void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
309 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700310 ActionExitCode code) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700311 // Reset download progress regardless of whether or not the download action
312 // succeeded.
313 const string type = action->Type();
314 if (type == DownloadAction::StaticType())
315 download_progress_ = 0.0;
Darin Petkov09f96c32010-07-20 09:24:57 -0700316 if (code != kActionCodeSuccess) {
Darin Petkov777dbfa2010-07-20 15:03:37 -0700317 // On failure, schedule an error event to be sent to Omaha.
318 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700319 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700320 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700321 // Find out which action completed.
322 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700323 // Note that the status will be updated to DOWNLOADING when some
324 // bytes get actually downloaded from the server and the
325 // BytesReceived callback is invoked. This avoids notifying the
326 // user that a download has started in cases when the server and
327 // the client are unable to initiate the download.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700328 OmahaResponseHandlerAction* omaha_response_handler_action =
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700329 dynamic_cast<OmahaResponseHandlerAction*>(action);
330 CHECK(omaha_response_handler_action);
331 const InstallPlan& plan = omaha_response_handler_action->install_plan();
332 last_checked_time_ = time(NULL);
333 // TODO(adlr): put version in InstallPlan
334 new_version_ = "0.0.0.0";
335 new_size_ = plan.size;
Darin Petkovc6c135c2010-08-11 13:36:18 -0700336 SetupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700337 } else if (type == DownloadAction::StaticType()) {
338 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
339 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700340}
341
342// Stop updating. An attempt will be made to record status to the disk
343// so that updates can be resumed later.
344void UpdateAttempter::Terminate() {
345 // TODO(adlr): implement this method.
346 NOTIMPLEMENTED();
347}
348
349// Try to resume from a previously Terminate()d update.
350void UpdateAttempter::ResumeUpdating() {
351 // TODO(adlr): implement this method.
352 NOTIMPLEMENTED();
353}
354
Darin Petkov9d911fa2010-08-19 09:36:08 -0700355void UpdateAttempter::SetDownloadStatus(bool active) {
356 download_active_ = active;
357 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
358}
359
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700360void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700361 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700362 LOG(ERROR) << "BytesReceived called while not downloading.";
363 return;
364 }
365 download_progress_ = static_cast<double>(bytes_received) /
366 static_cast<double>(total);
367 // We self throttle here
368 timespec now;
369 now.tv_sec = 0;
370 now.tv_nsec = 0;
371 if (GetCPUClockTime(&now) &&
372 CPUClockTimeGreaterThanHalfSecond(
373 CPUClockTimeElapsed(last_notify_time_, now))) {
374 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
375 }
376}
377
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700378bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
379 double* progress,
380 std::string* current_operation,
381 std::string* new_version,
382 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700383 *last_checked_time = last_checked_time_;
384 *progress = download_progress_;
385 *current_operation = UpdateStatusToString(status_);
386 *new_version = new_version_;
387 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700388 return true;
389}
390
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700391void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
392 status_ = status;
393 if (!dbus_service_)
394 return;
395 GetCPUClockTime(&last_notify_time_);
396 update_engine_service_emit_status_update(
397 dbus_service_,
398 last_checked_time_,
399 download_progress_,
400 UpdateStatusToString(status_),
401 new_version_.c_str(),
402 new_size_);
403}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700404
Darin Petkov777dbfa2010-07-20 15:03:37 -0700405void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
406 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700407 if (error_event_.get()) {
408 // This shouldn't really happen.
409 LOG(WARNING) << "There's already an existing pending error event.";
410 return;
411 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700412
413 // For now assume that Omaha response action failure means that
414 // there's no update so don't send an event. Also, double check that
415 // the failure has not occurred while sending an error event -- in
416 // which case don't schedule another. This shouldn't really happen
417 // but just in case...
418 if (action->Type() == OmahaResponseHandlerAction::StaticType() ||
419 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
420 return;
421 }
422
423 code = GetErrorCodeForAction(action, code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700424 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
425 OmahaEvent::kResultError,
426 code));
427}
428
429bool UpdateAttempter::ScheduleErrorEventAction() {
430 if (error_event_.get() == NULL)
431 return false;
432
433 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700434 new OmahaRequestAction(prefs_,
435 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700436 error_event_.release(), // Pass ownership.
437 new LibcurlHttpFetcher));
438 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
439 processor_.EnqueueAction(error_event_action.get());
440 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
441 processor_.StartProcessing();
442 return true;
443}
444
Darin Petkovc6c135c2010-08-11 13:36:18 -0700445void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
446 if (priority_ == priority) {
447 return;
448 }
449 if (utils::SetProcessPriority(priority)) {
450 priority_ = priority;
451 LOG(INFO) << "Process priority = " << priority_;
452 }
453}
454
455void UpdateAttempter::SetupPriorityManagement() {
456 if (manage_priority_source_) {
457 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
458 CleanupPriorityManagement();
459 }
460 const int kPriorityTimeout = 10 * 60; // 10 minutes
461 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
462 g_source_set_callback(manage_priority_source_,
463 StaticManagePriorityCallback,
464 this,
465 NULL);
466 g_source_attach(manage_priority_source_, NULL);
467 SetPriority(utils::kProcessPriorityLow);
468}
469
470void UpdateAttempter::CleanupPriorityManagement() {
471 if (manage_priority_source_) {
472 g_source_destroy(manage_priority_source_);
473 manage_priority_source_ = NULL;
474 }
475 SetPriority(utils::kProcessPriorityNormal);
476}
477
478gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
479 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
480}
481
482bool UpdateAttempter::ManagePriorityCallback() {
483 // If the current process priority is below normal, set it to normal
484 // and let GLib invoke this callback again.
485 if (utils::ComparePriorities(priority_, utils::kProcessPriorityNormal) < 0) {
486 SetPriority(utils::kProcessPriorityNormal);
487 return true;
488 }
489 // Set the priority to high and let GLib destroy the timeout source.
490 SetPriority(utils::kProcessPriorityHigh);
491 manage_priority_source_ = NULL;
492 return false;
493}
494
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700495} // namespace chromeos_update_engine