Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [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 | |
| 5 | #include "update_engine/update_check_scheduler.h" |
| 6 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 7 | #include "update_engine/certificate_checker.h" |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 8 | #include "update_engine/http_common.h" |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 9 | #include "update_engine/utils.h" |
| 10 | |
| 11 | namespace chromeos_update_engine { |
| 12 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 13 | // Default update check timeout interval/fuzz values, in seconds. Note that |
| 14 | // actual fuzz is within +/- half of the indicated value. |
| 15 | const int UpdateCheckScheduler::kTimeoutInitialInterval = 7 * 60; |
| 16 | const int UpdateCheckScheduler::kTimeoutPeriodicInterval = 45 * 60; |
| 17 | const int UpdateCheckScheduler::kTimeoutQuickInterval = 1 * 60; |
| 18 | const int UpdateCheckScheduler::kTimeoutMaxBackoffInterval = 4 * 60 * 60; |
| 19 | const int UpdateCheckScheduler::kTimeoutRegularFuzz = 10 * 60; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 20 | |
| 21 | UpdateCheckScheduler::UpdateCheckScheduler(UpdateAttempter* update_attempter) |
| 22 | : update_attempter_(update_attempter), |
| 23 | enabled_(false), |
| 24 | scheduled_(false), |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 25 | last_interval_(0), |
| 26 | poll_interval_(0) {} |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 27 | |
| 28 | UpdateCheckScheduler::~UpdateCheckScheduler() {} |
| 29 | |
| 30 | void UpdateCheckScheduler::Run() { |
| 31 | enabled_ = false; |
| 32 | update_attempter_->set_update_check_scheduler(NULL); |
| 33 | |
| 34 | if (!IsOfficialBuild()) { |
| 35 | LOG(WARNING) << "Non-official build: periodic update checks disabled."; |
| 36 | return; |
| 37 | } |
| 38 | if (IsBootDeviceRemovable()) { |
| 39 | LOG(WARNING) << "Removable device boot: periodic update checks disabled."; |
| 40 | return; |
| 41 | } |
| 42 | enabled_ = true; |
| 43 | |
| 44 | // Registers this scheduler with the update attempter so that scheduler can be |
| 45 | // notified of update status changes. |
| 46 | update_attempter_->set_update_check_scheduler(this); |
| 47 | |
| 48 | // Kicks off periodic update checks. The first check is scheduled |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 49 | // |kTimeoutInitialInterval| seconds from now. Subsequent checks are scheduled |
| 50 | // by ScheduleNextCheck, normally at |kTimeoutPeriodicInterval|-second |
| 51 | // intervals. |
| 52 | ScheduleCheck(kTimeoutInitialInterval, kTimeoutRegularFuzz); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | bool UpdateCheckScheduler::IsBootDeviceRemovable() { |
| 56 | return utils::IsRemovableDevice(utils::RootDevice(utils::BootDevice())); |
| 57 | } |
| 58 | |
Darin Petkov | 2a0e633 | 2010-09-24 14:43:41 -0700 | [diff] [blame] | 59 | bool UpdateCheckScheduler::IsOOBEComplete() { |
| 60 | return utils::IsOOBEComplete(); |
| 61 | } |
| 62 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 63 | bool UpdateCheckScheduler::IsOfficialBuild() { |
| 64 | return utils::IsOfficialBuild(); |
| 65 | } |
| 66 | |
| 67 | guint UpdateCheckScheduler::GTimeoutAddSeconds(guint interval, |
| 68 | GSourceFunc function) { |
| 69 | return g_timeout_add_seconds(interval, function, this); |
| 70 | } |
| 71 | |
| 72 | void UpdateCheckScheduler::ScheduleCheck(int interval, int fuzz) { |
| 73 | if (!CanSchedule()) { |
| 74 | return; |
| 75 | } |
| 76 | last_interval_ = interval; |
| 77 | interval = utils::FuzzInt(interval, fuzz); |
| 78 | if (interval < 0) { |
| 79 | interval = 0; |
| 80 | } |
| 81 | GTimeoutAddSeconds(interval, StaticCheck); |
| 82 | scheduled_ = true; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 83 | LOG(INFO) << "Next update check in " << utils::SecsToHourMinSecStr(interval); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | gboolean UpdateCheckScheduler::StaticCheck(void* scheduler) { |
| 87 | UpdateCheckScheduler* me = reinterpret_cast<UpdateCheckScheduler*>(scheduler); |
| 88 | CHECK(me->scheduled_); |
| 89 | me->scheduled_ = false; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 90 | |
| 91 | static bool is_test_used_once = false; |
| 92 | bool is_test = false; |
| 93 | if (me->IsOOBEComplete() || |
| 94 | (is_test = !is_test_used_once && GpioHandler::IsGpioSignalingTest())) { |
| 95 | if (is_test) { |
| 96 | LOG(WARNING) |
| 97 | << "test mode signaled, allowing update check prior to OOBE complete"; |
| 98 | is_test_used_once = true; |
| 99 | } |
| 100 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 101 | // Before updating, we flush any previously generated UMA reports. |
| 102 | CertificateChecker::FlushReport(); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 103 | me->update_attempter_->Update("", "", false, false, is_test); |
Darin Petkov | 2a0e633 | 2010-09-24 14:43:41 -0700 | [diff] [blame] | 104 | } else { |
| 105 | // Skips all automatic update checks if the OOBE process is not complete and |
| 106 | // schedules a new check as if it is the first one. |
| 107 | LOG(WARNING) << "Skipping update check because OOBE is not complete."; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 108 | me->ScheduleCheck(kTimeoutInitialInterval, kTimeoutRegularFuzz); |
Darin Petkov | 2a0e633 | 2010-09-24 14:43:41 -0700 | [diff] [blame] | 109 | } |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 110 | // This check ensures that future update checks will be or are already |
| 111 | // scheduled. The check should never fail. A check failure means that there's |
| 112 | // a bug that will most likely prevent further automatic update checks. It |
| 113 | // seems better to crash in such cases and restart the update_engine daemon |
| 114 | // into, hopefully, a known good state. |
| 115 | CHECK(me->update_attempter_->status() != UPDATE_STATUS_IDLE || |
| 116 | !me->CanSchedule()); |
| 117 | return FALSE; // Don't run again. |
| 118 | } |
| 119 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 120 | void UpdateCheckScheduler::ComputeNextIntervalAndFuzz(const int forced_interval, |
| 121 | int* next_interval, |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 122 | int* next_fuzz) { |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 123 | CHECK(next_interval && next_fuzz); |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 124 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 125 | int interval = forced_interval; |
| 126 | int fuzz = 0; // Use default fuzz value (see below) |
| 127 | |
| 128 | if (interval == 0) { |
| 129 | int http_response_code; |
| 130 | if (poll_interval_ > 0) { |
| 131 | // Server-dictated poll interval. |
| 132 | interval = poll_interval_; |
| 133 | LOG(WARNING) << "Using server-dictated poll interval: " << interval; |
| 134 | } else if ((http_response_code = update_attempter_->http_response_code()) == |
| 135 | kHttpResponseInternalServerError || |
| 136 | http_response_code == kHttpResponseServiceUnavailable) { |
| 137 | // Implements exponential back off on 500 (Internal Server Error) and 503 |
| 138 | // (Service Unavailable) HTTP response codes. |
| 139 | interval = 2 * last_interval_; |
| 140 | LOG(WARNING) << "Exponential back off due to HTTP response code (" |
| 141 | << http_response_code << ")"; |
| 142 | } |
| 143 | |
| 144 | // Backoff cannot exceed a predetermined maximum period. |
| 145 | if (interval > kTimeoutMaxBackoffInterval) |
| 146 | interval = kTimeoutMaxBackoffInterval; |
| 147 | |
| 148 | // Ensures that under normal conditions the regular update check interval |
| 149 | // and fuzz are used. Also covers the case where back off is required based |
| 150 | // on the initial update check. |
| 151 | if (interval < kTimeoutPeriodicInterval) { |
| 152 | interval = kTimeoutPeriodicInterval; |
| 153 | fuzz = kTimeoutRegularFuzz; |
| 154 | } |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 155 | } |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 156 | |
| 157 | // Set default fuzz to +/- |interval|/2. |
| 158 | if (fuzz == 0) |
| 159 | fuzz = interval; |
| 160 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 161 | *next_interval = interval; |
| 162 | *next_fuzz = fuzz; |
| 163 | } |
| 164 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 165 | void UpdateCheckScheduler::ScheduleNextCheck(bool is_force_quick) { |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 166 | int interval, fuzz; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 167 | ComputeNextIntervalAndFuzz(is_force_quick ? kTimeoutQuickInterval : 0, |
| 168 | &interval, &fuzz); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 169 | ScheduleCheck(interval, fuzz); |
| 170 | } |
| 171 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 172 | void UpdateCheckScheduler::SetUpdateStatus(UpdateStatus status, |
| 173 | UpdateNotice notice) { |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 174 | // We want to schedule the update checks for when we're idle as well as |
| 175 | // after we've successfully applied an update and waiting for the user |
| 176 | // to reboot to ensure our active count is accurate. |
| 177 | if (status == UPDATE_STATUS_IDLE || |
| 178 | status == UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 179 | ScheduleNextCheck(notice == kUpdateNoticeTestAddrFailed); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | } // namespace chromeos_update_engine |