blob: 0218393e9642edd0652dd1c44db750858385d375 [file] [log] [blame]
Darin Petkov1023a602010-08-30 13:47:51 -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_check_scheduler.h"
6
Bruno Rocha7f9aea22011-09-12 14:31:24 -07007#include "update_engine/certificate_checker.h"
Darin Petkov1023a602010-08-30 13:47:51 -07008#include "update_engine/utils.h"
9
10namespace chromeos_update_engine {
11
12const int UpdateCheckScheduler::kTimeoutOnce = 7 * 60; // at 7 minutes
13const int UpdateCheckScheduler::kTimeoutPeriodic = 45 * 60; // every 45 minutes
14const int UpdateCheckScheduler::kTimeoutRegularFuzz = 10 * 60; // +/- 5 minutes
15const int UpdateCheckScheduler::kTimeoutMaxBackoff = 4 * 60 * 60; // 4 hours
16
17UpdateCheckScheduler::UpdateCheckScheduler(UpdateAttempter* update_attempter)
18 : update_attempter_(update_attempter),
19 enabled_(false),
20 scheduled_(false),
Darin Petkov85ced132010-09-01 10:20:56 -070021 last_interval_(0),
22 poll_interval_(0) {}
Darin Petkov1023a602010-08-30 13:47:51 -070023
24UpdateCheckScheduler::~UpdateCheckScheduler() {}
25
26void UpdateCheckScheduler::Run() {
27 enabled_ = false;
28 update_attempter_->set_update_check_scheduler(NULL);
29
30 if (!IsOfficialBuild()) {
31 LOG(WARNING) << "Non-official build: periodic update checks disabled.";
32 return;
33 }
34 if (IsBootDeviceRemovable()) {
35 LOG(WARNING) << "Removable device boot: periodic update checks disabled.";
36 return;
37 }
38 enabled_ = true;
39
40 // Registers this scheduler with the update attempter so that scheduler can be
41 // notified of update status changes.
42 update_attempter_->set_update_check_scheduler(this);
43
44 // Kicks off periodic update checks. The first check is scheduled
45 // |kTimeoutOnce| seconds from now. Subsequent checks are scheduled by
46 // ScheduleNextCheck, normally at |kTimeoutPeriodic|-second intervals.
47 ScheduleCheck(kTimeoutOnce, kTimeoutRegularFuzz);
48}
49
50bool UpdateCheckScheduler::IsBootDeviceRemovable() {
51 return utils::IsRemovableDevice(utils::RootDevice(utils::BootDevice()));
52}
53
Darin Petkov2a0e6332010-09-24 14:43:41 -070054bool UpdateCheckScheduler::IsOOBEComplete() {
55 return utils::IsOOBEComplete();
56}
57
Darin Petkov1023a602010-08-30 13:47:51 -070058bool UpdateCheckScheduler::IsOfficialBuild() {
59 return utils::IsOfficialBuild();
60}
61
62guint UpdateCheckScheduler::GTimeoutAddSeconds(guint interval,
63 GSourceFunc function) {
64 return g_timeout_add_seconds(interval, function, this);
65}
66
67void UpdateCheckScheduler::ScheduleCheck(int interval, int fuzz) {
68 if (!CanSchedule()) {
69 return;
70 }
71 last_interval_ = interval;
72 interval = utils::FuzzInt(interval, fuzz);
73 if (interval < 0) {
74 interval = 0;
75 }
76 GTimeoutAddSeconds(interval, StaticCheck);
77 scheduled_ = true;
78 LOG(INFO) << "Next update check in " << interval << " seconds.";
79}
80
81gboolean UpdateCheckScheduler::StaticCheck(void* scheduler) {
82 UpdateCheckScheduler* me = reinterpret_cast<UpdateCheckScheduler*>(scheduler);
83 CHECK(me->scheduled_);
84 me->scheduled_ = false;
Darin Petkov2a0e6332010-09-24 14:43:41 -070085 if (me->IsOOBEComplete()) {
Bruno Rocha7f9aea22011-09-12 14:31:24 -070086 // Before updating, we flush any previously generated UMA reports.
87 CertificateChecker::FlushReport();
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -070088 me->update_attempter_->Update("", "", false, false);
Darin Petkov2a0e6332010-09-24 14:43:41 -070089 } else {
90 // Skips all automatic update checks if the OOBE process is not complete and
91 // schedules a new check as if it is the first one.
92 LOG(WARNING) << "Skipping update check because OOBE is not complete.";
93 me->ScheduleCheck(kTimeoutOnce, kTimeoutRegularFuzz);
94 }
Darin Petkov1023a602010-08-30 13:47:51 -070095 // This check ensures that future update checks will be or are already
96 // scheduled. The check should never fail. A check failure means that there's
97 // a bug that will most likely prevent further automatic update checks. It
98 // seems better to crash in such cases and restart the update_engine daemon
99 // into, hopefully, a known good state.
100 CHECK(me->update_attempter_->status() != UPDATE_STATUS_IDLE ||
101 !me->CanSchedule());
102 return FALSE; // Don't run again.
103}
104
105void UpdateCheckScheduler::ComputeNextIntervalAndFuzz(int* next_interval,
106 int* next_fuzz) {
107 int interval = 0;
Darin Petkov85ced132010-09-01 10:20:56 -0700108 if (poll_interval_ > 0) {
109 // Server-dictated poll interval.
110 interval = poll_interval_;
111 LOG(WARNING) << "Using server-dictated poll interval: " << interval;
112 } else if (update_attempter_->http_response_code() == 500 ||
113 update_attempter_->http_response_code() == 503) {
114 // Implements exponential back off on 500 (Internal Server Error) and 503
115 // (Service Unavailable) HTTP response codes.
Darin Petkov1023a602010-08-30 13:47:51 -0700116 interval = 2 * last_interval_;
Darin Petkov85ced132010-09-01 10:20:56 -0700117 LOG(WARNING) << "Exponential back off due to 500/503 HTTP response code.";
Darin Petkov1023a602010-08-30 13:47:51 -0700118 }
Darin Petkov85ced132010-09-01 10:20:56 -0700119 if (interval > kTimeoutMaxBackoff) {
120 interval = kTimeoutMaxBackoff;
121 }
122 // Back off and server-dictated poll intervals are fuzzed by +/- |interval|/2.
123 int fuzz = interval;
124
Darin Petkov1023a602010-08-30 13:47:51 -0700125 // Ensures that under normal conditions the regular update check interval and
126 // fuzz are used. Also covers the case where back off is required based on the
127 // initial update check.
128 if (interval < kTimeoutPeriodic) {
129 interval = kTimeoutPeriodic;
130 fuzz = kTimeoutRegularFuzz;
131 }
132 *next_interval = interval;
133 *next_fuzz = fuzz;
134}
135
136void UpdateCheckScheduler::ScheduleNextCheck() {
137 int interval, fuzz;
138 ComputeNextIntervalAndFuzz(&interval, &fuzz);
139 ScheduleCheck(interval, fuzz);
140}
141
142void UpdateCheckScheduler::SetUpdateStatus(UpdateStatus status) {
Thieu Le116fda32011-04-19 11:01:54 -0700143 // We want to schedule the update checks for when we're idle as well as
144 // after we've successfully applied an update and waiting for the user
145 // to reboot to ensure our active count is accurate.
146 if (status == UPDATE_STATUS_IDLE ||
147 status == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
Darin Petkov1023a602010-08-30 13:47:51 -0700148 ScheduleNextCheck();
149 }
150}
151
152} // namespace chromeos_update_engine