Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -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/dbus_service.h" |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 6 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 7 | #include <set> |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 8 | #include <string> |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 9 | |
| 10 | #include <base/logging.h> |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 11 | #include <policy/device_policy.h> |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 12 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 13 | #include "update_engine/connection_manager.h" |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 14 | #include "update_engine/marshal.glibmarshal.h" |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 15 | #include "update_engine/omaha_request_params.h" |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 16 | #include "update_engine/update_attempter.h" |
| 17 | #include "update_engine/prefs.h" |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 18 | #include "update_engine/utils.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 19 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 20 | using std::set; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 21 | using std::string; |
| 22 | |
Darin Petkov | 820a77b | 2011-04-27 16:48:58 -0700 | [diff] [blame] | 23 | static const char kAUTestURLRequest[] = "autest"; |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 24 | // By default autest bypasses scattering. If we want to test scattering, |
| 25 | // we should use autest-scheduled. The Url used is same in both cases, but |
| 26 | // different params are passed to CheckForUpdate method. |
| 27 | static const char kScheduledAUTestURLRequest[] = "autest-scheduled"; |
| 28 | |
Darin Petkov | 820a77b | 2011-04-27 16:48:58 -0700 | [diff] [blame] | 29 | static const char kAUTestURL[] = |
Darin Petkov | 5445e16 | 2011-11-04 10:10:27 +0100 | [diff] [blame] | 30 | "https://omaha.sandbox.google.com/service/update2"; |
Darin Petkov | 820a77b | 2011-04-27 16:48:58 -0700 | [diff] [blame] | 31 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 32 | G_DEFINE_TYPE(UpdateEngineService, update_engine_service, G_TYPE_OBJECT) |
| 33 | |
| 34 | static void update_engine_service_finalize(GObject* object) { |
| 35 | G_OBJECT_CLASS(update_engine_service_parent_class)->finalize(object); |
| 36 | } |
| 37 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 38 | static guint status_update_signal = 0; |
| 39 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 40 | static void update_engine_service_class_init(UpdateEngineServiceClass* klass) { |
| 41 | GObjectClass *object_class; |
| 42 | object_class = G_OBJECT_CLASS(klass); |
| 43 | object_class->finalize = update_engine_service_finalize; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 44 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 45 | status_update_signal = g_signal_new( |
| 46 | "status_update", |
| 47 | G_OBJECT_CLASS_TYPE(klass), |
| 48 | G_SIGNAL_RUN_LAST, |
| 49 | 0, // 0 == no class method associated |
| 50 | NULL, // Accumulator |
| 51 | NULL, // Accumulator data |
| 52 | update_engine_VOID__INT64_DOUBLE_STRING_STRING_INT64, |
| 53 | G_TYPE_NONE, // Return type |
| 54 | 5, // param count: |
| 55 | G_TYPE_INT64, |
| 56 | G_TYPE_DOUBLE, |
| 57 | G_TYPE_STRING, |
| 58 | G_TYPE_STRING, |
| 59 | G_TYPE_INT64); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static void update_engine_service_init(UpdateEngineService* object) { |
| 63 | } |
| 64 | |
| 65 | UpdateEngineService* update_engine_service_new(void) { |
| 66 | return reinterpret_cast<UpdateEngineService*>( |
| 67 | g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL)); |
| 68 | } |
| 69 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 70 | gboolean update_engine_service_attempt_update(UpdateEngineService* self, |
| 71 | gchar* app_version, |
| 72 | gchar* omaha_url, |
| 73 | GError **error) { |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 74 | string update_app_version; |
| 75 | string update_omaha_url; |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 76 | bool interactive = true; |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 77 | |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 78 | // Only non-official (e.g., dev and test) builds can override the current |
Darin Petkov | 820a77b | 2011-04-27 16:48:58 -0700 | [diff] [blame] | 79 | // version and update server URL over D-Bus. However, pointing to the |
| 80 | // hardcoded test update server URL is always allowed. |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 81 | if (!chromeos_update_engine::utils::IsOfficialBuild()) { |
| 82 | if (app_version) { |
| 83 | update_app_version = app_version; |
| 84 | } |
| 85 | if (omaha_url) { |
| 86 | update_omaha_url = omaha_url; |
| 87 | } |
| 88 | } |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 89 | if (omaha_url) { |
| 90 | if (strcmp(omaha_url, kScheduledAUTestURLRequest) == 0) { |
| 91 | update_omaha_url = kAUTestURL; |
| 92 | // pretend that it's not user-initiated even though it is, |
| 93 | // so as to test scattering logic, etc. which get kicked off |
| 94 | // only in scheduled update checks. |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 95 | interactive = false; |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 96 | } else if (strcmp(omaha_url, kAUTestURLRequest) == 0) { |
| 97 | update_omaha_url = kAUTestURL; |
| 98 | } |
Darin Petkov | 820a77b | 2011-04-27 16:48:58 -0700 | [diff] [blame] | 99 | } |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 100 | LOG(INFO) << "Attempt update: app_version=\"" << update_app_version << "\" " |
Jay Srinivasan | e73acab | 2012-07-10 14:34:03 -0700 | [diff] [blame] | 101 | << "omaha_url=\"" << update_omaha_url << "\" " |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 102 | << "interactive=" << (interactive? "yes" : "no"); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 103 | self->system_state_->update_attempter()->CheckForUpdate(update_app_version, |
| 104 | update_omaha_url, |
| 105 | interactive); |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 106 | return TRUE; |
| 107 | } |
| 108 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 109 | gboolean update_engine_service_attempt_rollback(UpdateEngineService* self, |
| 110 | bool powerwash, |
| 111 | GError **error) { |
| 112 | LOG(INFO) << "Attempting rollback to non-active partitions."; |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame^] | 113 | return self->system_state_->update_attempter()->Rollback(powerwash, NULL); |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 116 | gboolean update_engine_service_reset_status(UpdateEngineService* self, |
| 117 | GError **error) { |
| 118 | *error = NULL; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 119 | return self->system_state_->update_attempter()->ResetStatus(); |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 123 | gboolean update_engine_service_get_status(UpdateEngineService* self, |
| 124 | int64_t* last_checked_time, |
| 125 | double* progress, |
| 126 | gchar** current_operation, |
| 127 | gchar** new_version, |
| 128 | int64_t* new_size, |
| 129 | GError **error) { |
| 130 | string current_op; |
| 131 | string new_version_str; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 132 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 133 | CHECK(self->system_state_->update_attempter()->GetStatus(last_checked_time, |
| 134 | progress, |
| 135 | ¤t_op, |
| 136 | &new_version_str, |
| 137 | new_size)); |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 138 | |
Satoru Takabayashi | d698231 | 2010-11-29 12:54:12 +0900 | [diff] [blame] | 139 | *current_operation = g_strdup(current_op.c_str()); |
| 140 | *new_version = g_strdup(new_version_str.c_str()); |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 141 | if (!(*current_operation && *new_version)) { |
| 142 | *error = NULL; |
| 143 | return FALSE; |
| 144 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 145 | return TRUE; |
| 146 | } |
| 147 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 148 | gboolean update_engine_service_reboot_if_needed(UpdateEngineService* self, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 149 | GError **error) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 150 | if (!self->system_state_->update_attempter()->RebootIfNeeded()) { |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 151 | *error = NULL; |
| 152 | return FALSE; |
| 153 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 154 | return TRUE; |
| 155 | } |
| 156 | |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 157 | gboolean update_engine_service_set_track(UpdateEngineService* self, |
| 158 | gchar* track, |
| 159 | GError **error) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 160 | // track == target channel. |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 161 | // TODO(jaysri): Remove this method once chromium:219292 is fixed. |
| 162 | // Since UI won't be ready for now, preserve the existing |
| 163 | // behavior for set_track by calling SetTargetChannel directly without the |
| 164 | // policy checks instead of calling update_engine_service_set_channel. |
| 165 | LOG(INFO) << "Setting destination track to: " << track; |
| 166 | if (!self->system_state_->request_params()->SetTargetChannel(track, false)) { |
| 167 | *error = NULL; |
| 168 | return FALSE; |
| 169 | } |
| 170 | |
| 171 | return TRUE; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | gboolean update_engine_service_get_track(UpdateEngineService* self, |
| 175 | gchar** track, |
| 176 | GError **error) { |
| 177 | // track == target channel. |
| 178 | return update_engine_service_get_channel(self, false, track, error); |
| 179 | } |
| 180 | |
| 181 | gboolean update_engine_service_set_channel(UpdateEngineService* self, |
| 182 | gchar* target_channel, |
| 183 | bool is_powerwash_allowed, |
| 184 | GError **error) { |
| 185 | if (!target_channel) |
| 186 | return FALSE; |
| 187 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 188 | const policy::DevicePolicy* device_policy = |
| 189 | self->system_state_->device_policy(); |
| 190 | if (!device_policy) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 191 | LOG(INFO) << "Cannot set target channel until device policy/settings are " |
| 192 | "known"; |
| 193 | return FALSE; |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 194 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 195 | |
| 196 | bool delegated = false; |
Chris Sosa | 1ed6078 | 2013-06-18 17:05:33 -0700 | [diff] [blame] | 197 | if (device_policy->GetReleaseChannelDelegated(&delegated) && !delegated) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 198 | LOG(INFO) << "Cannot set target channel explicitly when channel " |
| 199 | "policy/settings is not delegated"; |
| 200 | return FALSE; |
| 201 | } |
| 202 | |
| 203 | LOG(INFO) << "Setting destination channel to: " << target_channel; |
| 204 | if (!self->system_state_->request_params()->SetTargetChannel( |
| 205 | target_channel, is_powerwash_allowed)) { |
| 206 | *error = NULL; |
| 207 | return FALSE; |
| 208 | } |
| 209 | |
| 210 | return TRUE; |
| 211 | } |
| 212 | |
| 213 | gboolean update_engine_service_get_channel(UpdateEngineService* self, |
| 214 | bool get_current_channel, |
| 215 | gchar** channel, |
| 216 | GError **error) { |
| 217 | chromeos_update_engine::OmahaRequestParams* rp = |
| 218 | self->system_state_->request_params(); |
| 219 | |
| 220 | string channel_str = get_current_channel ? |
| 221 | rp->current_channel() : rp->target_channel(); |
| 222 | |
| 223 | *channel = g_strdup(channel_str.c_str()); |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 224 | return TRUE; |
| 225 | } |
| 226 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 227 | gboolean update_engine_service_set_update_over_cellular_permission( |
| 228 | UpdateEngineService* self, |
| 229 | bool allowed, |
| 230 | GError **error) { |
| 231 | set<string> allowed_types; |
| 232 | const policy::DevicePolicy* device_policy = |
| 233 | self->system_state_->device_policy(); |
| 234 | |
| 235 | // The device_policy is loaded in a lazy way before an update check. Load it |
| 236 | // now from the libchromeos cache if it wasn't already loaded. |
| 237 | if (!device_policy) { |
| 238 | chromeos_update_engine::UpdateAttempter* update_attempter = |
| 239 | self->system_state_->update_attempter(); |
| 240 | if (update_attempter) { |
| 241 | update_attempter->RefreshDevicePolicy(); |
| 242 | device_policy = self->system_state_->device_policy();; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // Check if this setting is allowed by the device policy. |
| 247 | if (device_policy && |
| 248 | device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) { |
| 249 | LOG(INFO) << "Ignoring the update over cellular setting since there's " |
| 250 | "a device policy enforcing this setting."; |
| 251 | *error = NULL; |
| 252 | return FALSE; |
| 253 | } |
| 254 | |
| 255 | // If the policy wasn't loaded yet, then it is still OK to change the local |
| 256 | // setting because the policy will be checked again during the update check. |
| 257 | |
| 258 | chromeos_update_engine::PrefsInterface* prefs = self->system_state_->prefs(); |
| 259 | |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 260 | if (!prefs->SetBoolean( |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 261 | chromeos_update_engine::kPrefsUpdateOverCellularPermission, |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 262 | allowed)) { |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 263 | LOG(ERROR) << "Error setting the update over cellular to " |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 264 | << (allowed ? "true" : "false"); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 265 | *error = NULL; |
| 266 | return FALSE; |
| 267 | } |
| 268 | |
| 269 | return TRUE; |
| 270 | } |
| 271 | |
| 272 | gboolean update_engine_service_get_update_over_cellular_permission( |
| 273 | UpdateEngineService* self, |
| 274 | bool* allowed, |
| 275 | GError **/*error*/) { |
| 276 | chromeos_update_engine::ConnectionManager* cm = |
| 277 | self->system_state_->connection_manager(); |
| 278 | |
| 279 | // The device_policy is loaded in a lazy way before an update check and is |
| 280 | // used to determine if an update is allowed over cellular. Load the device |
| 281 | // policy now from the libchromeos cache if it wasn't already loaded. |
| 282 | if (!self->system_state_->device_policy()) { |
| 283 | chromeos_update_engine::UpdateAttempter* update_attempter = |
| 284 | self->system_state_->update_attempter(); |
| 285 | if (update_attempter) |
| 286 | update_attempter->RefreshDevicePolicy(); |
| 287 | } |
| 288 | |
| 289 | // Return the current setting based on the same logic used while checking for |
| 290 | // updates. A log message could be printed as the result of this test. |
| 291 | LOG(INFO) << "Checking if updates over cellular networks are allowed:"; |
| 292 | *allowed = cm->IsUpdateAllowedOver(chromeos_update_engine::kNetCellular); |
| 293 | |
| 294 | return TRUE; |
| 295 | } |
| 296 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 297 | gboolean update_engine_service_emit_status_update( |
| 298 | UpdateEngineService* self, |
| 299 | gint64 last_checked_time, |
| 300 | gdouble progress, |
| 301 | const gchar* current_operation, |
| 302 | const gchar* new_version, |
| 303 | gint64 new_size) { |
| 304 | g_signal_emit(self, |
| 305 | status_update_signal, |
| 306 | 0, |
| 307 | last_checked_time, |
| 308 | progress, |
| 309 | current_operation, |
| 310 | new_version, |
| 311 | new_size); |
| 312 | return TRUE; |
| 313 | } |