blob: 682bae71260633f77c1cc833bdb9bf20ce07b0b6 [file] [log] [blame]
Gilad Arnold1ebd8132012-03-05 10:19:29 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov1023a602010-08-30 13:47:51 -07002// 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 Rocha7f9aea22011-09-12 14:31:24 -07007#include "update_engine/certificate_checker.h"
Alex Deymo42432912013-07-12 20:21:15 -07008#include "update_engine/hardware_interface.h"
Gilad Arnold1ebd8132012-03-05 10:19:29 -08009#include "update_engine/http_common.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080010#include "update_engine/gpio_handler.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070011#include "update_engine/system_state.h"
Darin Petkov1023a602010-08-30 13:47:51 -070012#include "update_engine/utils.h"
13
14namespace chromeos_update_engine {
15
Gilad Arnold1ebd8132012-03-05 10:19:29 -080016// Default update check timeout interval/fuzz values, in seconds. Note that
17// actual fuzz is within +/- half of the indicated value.
18const int UpdateCheckScheduler::kTimeoutInitialInterval = 7 * 60;
19const int UpdateCheckScheduler::kTimeoutPeriodicInterval = 45 * 60;
20const int UpdateCheckScheduler::kTimeoutQuickInterval = 1 * 60;
21const int UpdateCheckScheduler::kTimeoutMaxBackoffInterval = 4 * 60 * 60;
22const int UpdateCheckScheduler::kTimeoutRegularFuzz = 10 * 60;
Darin Petkov1023a602010-08-30 13:47:51 -070023
Gilad Arnold4d740eb2012-05-15 08:48:13 -070024UpdateCheckScheduler::UpdateCheckScheduler(UpdateAttempter* update_attempter,
Jay Srinivasan08fce042012-06-07 16:31:01 -070025 SystemState* system_state)
Darin Petkov1023a602010-08-30 13:47:51 -070026 : update_attempter_(update_attempter),
27 enabled_(false),
28 scheduled_(false),
Darin Petkov85ced132010-09-01 10:20:56 -070029 last_interval_(0),
Gilad Arnold4d740eb2012-05-15 08:48:13 -070030 poll_interval_(0),
31 is_test_update_attempted_(false),
Jay Srinivasan08fce042012-06-07 16:31:01 -070032 system_state_(system_state) {}
Darin Petkov1023a602010-08-30 13:47:51 -070033
34UpdateCheckScheduler::~UpdateCheckScheduler() {}
35
36void UpdateCheckScheduler::Run() {
37 enabled_ = false;
38 update_attempter_->set_update_check_scheduler(NULL);
39
40 if (!IsOfficialBuild()) {
41 LOG(WARNING) << "Non-official build: periodic update checks disabled.";
42 return;
43 }
44 if (IsBootDeviceRemovable()) {
45 LOG(WARNING) << "Removable device boot: periodic update checks disabled.";
46 return;
47 }
48 enabled_ = true;
49
50 // Registers this scheduler with the update attempter so that scheduler can be
51 // notified of update status changes.
52 update_attempter_->set_update_check_scheduler(this);
53
54 // Kicks off periodic update checks. The first check is scheduled
Gilad Arnold1ebd8132012-03-05 10:19:29 -080055 // |kTimeoutInitialInterval| seconds from now. Subsequent checks are scheduled
56 // by ScheduleNextCheck, normally at |kTimeoutPeriodicInterval|-second
57 // intervals.
58 ScheduleCheck(kTimeoutInitialInterval, kTimeoutRegularFuzz);
Darin Petkov1023a602010-08-30 13:47:51 -070059}
60
61bool UpdateCheckScheduler::IsBootDeviceRemovable() {
Alex Vakulenko4f5b1442014-02-21 12:19:44 -080062 return utils::IsRemovableDevice(utils::GetDiskName(
Alex Deymo42432912013-07-12 20:21:15 -070063 system_state_->hardware()->BootDevice()));
Darin Petkov1023a602010-08-30 13:47:51 -070064}
65
66bool UpdateCheckScheduler::IsOfficialBuild() {
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070067 return system_state_->hardware()->IsOfficialBuild();
Darin Petkov1023a602010-08-30 13:47:51 -070068}
69
70guint UpdateCheckScheduler::GTimeoutAddSeconds(guint interval,
71 GSourceFunc function) {
72 return g_timeout_add_seconds(interval, function, this);
73}
74
75void UpdateCheckScheduler::ScheduleCheck(int interval, int fuzz) {
76 if (!CanSchedule()) {
77 return;
78 }
79 last_interval_ = interval;
80 interval = utils::FuzzInt(interval, fuzz);
81 if (interval < 0) {
82 interval = 0;
83 }
84 GTimeoutAddSeconds(interval, StaticCheck);
85 scheduled_ = true;
Gilad Arnoldd7b513d2012-05-10 14:25:27 -070086 LOG(INFO) << "Next update check in " << utils::FormatSecs(interval);
Darin Petkov1023a602010-08-30 13:47:51 -070087}
88
89gboolean UpdateCheckScheduler::StaticCheck(void* scheduler) {
90 UpdateCheckScheduler* me = reinterpret_cast<UpdateCheckScheduler*>(scheduler);
91 CHECK(me->scheduled_);
92 me->scheduled_ = false;
Gilad Arnold1ebd8132012-03-05 10:19:29 -080093
Gilad Arnold7c04e762012-05-23 10:54:02 -070094 bool is_test_mode = false;
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080095 GpioHandler* gpio_handler = me->system_state_->gpio_handler();
David Zeuthen639aa362014-02-03 16:23:44 -080096 base::Time time_oobe_complete;
97 if (me->system_state_->IsOOBEComplete(&time_oobe_complete) ||
Gilad Arnold7c04e762012-05-23 10:54:02 -070098 (is_test_mode = (!me->is_test_update_attempted_ &&
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080099 gpio_handler->IsTestModeSignaled()))) {
Gilad Arnold7c04e762012-05-23 10:54:02 -0700100 if (is_test_mode) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800101 LOG(WARNING)
102 << "test mode signaled, allowing update check prior to OOBE complete";
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700103 me->is_test_update_attempted_ = true;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800104 }
105
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700106 // Before updating, we flush any previously generated UMA reports.
107 CertificateChecker::FlushReport();
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800108 me->update_attempter_->Update("", "", false, false, is_test_mode);
Darin Petkov2a0e6332010-09-24 14:43:41 -0700109 } else {
110 // Skips all automatic update checks if the OOBE process is not complete and
111 // schedules a new check as if it is the first one.
112 LOG(WARNING) << "Skipping update check because OOBE is not complete.";
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800113 me->ScheduleCheck(kTimeoutInitialInterval, kTimeoutRegularFuzz);
Darin Petkov2a0e6332010-09-24 14:43:41 -0700114 }
Darin Petkov1023a602010-08-30 13:47:51 -0700115 // This check ensures that future update checks will be or are already
116 // scheduled. The check should never fail. A check failure means that there's
117 // a bug that will most likely prevent further automatic update checks. It
118 // seems better to crash in such cases and restart the update_engine daemon
119 // into, hopefully, a known good state.
120 CHECK(me->update_attempter_->status() != UPDATE_STATUS_IDLE ||
121 !me->CanSchedule());
122 return FALSE; // Don't run again.
123}
124
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800125void UpdateCheckScheduler::ComputeNextIntervalAndFuzz(const int forced_interval,
126 int* next_interval,
Darin Petkov1023a602010-08-30 13:47:51 -0700127 int* next_fuzz) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800128 CHECK(next_interval && next_fuzz);
Darin Petkov85ced132010-09-01 10:20:56 -0700129
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800130 int interval = forced_interval;
131 int fuzz = 0; // Use default fuzz value (see below)
132
133 if (interval == 0) {
134 int http_response_code;
135 if (poll_interval_ > 0) {
136 // Server-dictated poll interval.
137 interval = poll_interval_;
138 LOG(WARNING) << "Using server-dictated poll interval: " << interval;
139 } else if ((http_response_code = update_attempter_->http_response_code()) ==
140 kHttpResponseInternalServerError ||
141 http_response_code == kHttpResponseServiceUnavailable) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800142 // Implements exponential backoff on 500 (Internal Server Error) and 503
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800143 // (Service Unavailable) HTTP response codes.
144 interval = 2 * last_interval_;
Jay Srinivasan08262882012-12-28 19:29:43 -0800145 LOG(WARNING) << "Exponential backoff due to HTTP response code ("
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800146 << http_response_code << ")";
147 }
148
149 // Backoff cannot exceed a predetermined maximum period.
150 if (interval > kTimeoutMaxBackoffInterval)
151 interval = kTimeoutMaxBackoffInterval;
152
153 // Ensures that under normal conditions the regular update check interval
Jay Srinivasan08262882012-12-28 19:29:43 -0800154 // and fuzz are used. Also covers the case where backoff is required based
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800155 // on the initial update check.
156 if (interval < kTimeoutPeriodicInterval) {
157 interval = kTimeoutPeriodicInterval;
158 fuzz = kTimeoutRegularFuzz;
159 }
Darin Petkov1023a602010-08-30 13:47:51 -0700160 }
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800161
162 // Set default fuzz to +/- |interval|/2.
163 if (fuzz == 0)
164 fuzz = interval;
165
Darin Petkov1023a602010-08-30 13:47:51 -0700166 *next_interval = interval;
167 *next_fuzz = fuzz;
168}
169
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800170void UpdateCheckScheduler::ScheduleNextCheck(bool is_force_quick) {
Darin Petkov1023a602010-08-30 13:47:51 -0700171 int interval, fuzz;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800172 ComputeNextIntervalAndFuzz(is_force_quick ? kTimeoutQuickInterval : 0,
173 &interval, &fuzz);
Darin Petkov1023a602010-08-30 13:47:51 -0700174 ScheduleCheck(interval, fuzz);
175}
176
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800177void UpdateCheckScheduler::SetUpdateStatus(UpdateStatus status,
178 UpdateNotice notice) {
Thieu Le116fda32011-04-19 11:01:54 -0700179 // We want to schedule the update checks for when we're idle as well as
180 // after we've successfully applied an update and waiting for the user
181 // to reboot to ensure our active count is accurate.
182 if (status == UPDATE_STATUS_IDLE ||
183 status == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800184 ScheduleNextCheck(notice == kUpdateNoticeTestAddrFailed);
Darin Petkov1023a602010-08-30 13:47:51 -0700185 }
186}
187
188} // namespace chromeos_update_engine