Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -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 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 5 | #include <string> |
| 6 | |
Ben Chan | 46bf5c8 | 2013-06-24 11:17:41 -0700 | [diff] [blame] | 7 | #include <dbus/dbus.h> |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 8 | #include <gflags/gflags.h> |
| 9 | #include <glib.h> |
| 10 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 11 | #include "update_engine/marshal.glibmarshal.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 12 | #include "update_engine/dbus_constants.h" |
| 13 | #include "update_engine/subprocess.h" |
| 14 | #include "update_engine/utils.h" |
| 15 | |
| 16 | extern "C" { |
| 17 | #include "update_engine/update_engine.dbusclient.h" |
| 18 | } |
| 19 | |
| 20 | using chromeos_update_engine::kUpdateEngineServiceName; |
| 21 | using chromeos_update_engine::kUpdateEngineServicePath; |
| 22 | using chromeos_update_engine::kUpdateEngineServiceInterface; |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 23 | using chromeos_update_engine::utils::GetAndFreeGError; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 24 | using std::string; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 25 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 26 | DEFINE_string(app_version, "", "Force the current app version."); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 27 | DEFINE_string(channel, "", |
| 28 | "Set the target channel. The device will be powerwashed if the target " |
| 29 | "channel is more stable than the current channel."); |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 30 | DEFINE_bool(check_for_update, false, "Initiate check for updates."); |
| 31 | DEFINE_string(omaha_url, "", "The URL of the Omaha update server."); |
| 32 | DEFINE_bool(reboot, false, "Initiate a reboot if needed."); |
| 33 | DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle."); |
| 34 | DEFINE_bool(rollback, false, "Perform a rollback to the previous partition."); |
| 35 | DEFINE_bool(show_channel, false, "Show the current and target channels."); |
Chris Sosa | cb7fa88 | 2013-07-25 17:02:59 -0700 | [diff] [blame] | 36 | DEFINE_bool(powerwash, true, "When performing rollback or channel change, " |
| 37 | "do a powerwash or allow it respectively."); |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 38 | DEFINE_bool(status, false, "Print the status to stdout."); |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 39 | DEFINE_bool(update, false, "Forces an update and waits for its completion. " |
| 40 | "Exit status is 0 if the update succeeded, and 1 otherwise."); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 41 | DEFINE_bool(watch_for_updates, false, |
| 42 | "Listen for status updates and print them to the screen."); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 43 | DEFINE_bool(show_update_over_cellular, false, |
| 44 | "Show the current setting for updates over cellular networks."); |
| 45 | DEFINE_string(update_over_cellular, "", |
| 46 | "Enables (\"yes\") or disables (\"no\") the updates over " |
| 47 | "cellular networks."); |
Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame^] | 48 | DEFINE_bool(show_p2p_update, false, |
| 49 | "Show the current setting for peer-to-peer update sharing."); |
| 50 | DEFINE_string(p2p_update, "", |
| 51 | "Enables (\"yes\") or disables (\"no\") the peer-to-peer update " |
| 52 | "sharing."); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 53 | |
| 54 | namespace { |
| 55 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 56 | bool GetProxy(DBusGProxy** out_proxy) { |
| 57 | DBusGConnection* bus; |
Andrew de los Reyes | 68ab6ed | 2011-08-09 14:46:39 -0700 | [diff] [blame] | 58 | DBusGProxy* proxy = NULL; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 59 | GError* error = NULL; |
Andrew de los Reyes | 68ab6ed | 2011-08-09 14:46:39 -0700 | [diff] [blame] | 60 | const int kTries = 4; |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 61 | const int kRetrySeconds = 10; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 62 | |
| 63 | bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); |
Richard Barnette | d793606 | 2013-01-18 13:38:51 -0800 | [diff] [blame] | 64 | if (bus == NULL) { |
| 65 | LOG(ERROR) << "Failed to get bus: " << GetAndFreeGError(&error); |
| 66 | exit(1); |
| 67 | } |
Andrew de los Reyes | 68ab6ed | 2011-08-09 14:46:39 -0700 | [diff] [blame] | 68 | for (int i = 0; !proxy && i < kTries; ++i) { |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 69 | if (i > 0) { |
| 70 | LOG(INFO) << "Retrying to get dbus proxy. Try " |
| 71 | << (i + 1) << "/" << kTries; |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 72 | g_usleep(kRetrySeconds * G_USEC_PER_SEC); |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 73 | } |
Andrew de los Reyes | 68ab6ed | 2011-08-09 14:46:39 -0700 | [diff] [blame] | 74 | proxy = dbus_g_proxy_new_for_name_owner(bus, |
| 75 | kUpdateEngineServiceName, |
| 76 | kUpdateEngineServicePath, |
| 77 | kUpdateEngineServiceInterface, |
| 78 | &error); |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 79 | LOG_IF(WARNING, !proxy) << "Error getting dbus proxy for " |
| 80 | << kUpdateEngineServiceName << ": " |
| 81 | << GetAndFreeGError(&error); |
Andrew de los Reyes | 68ab6ed | 2011-08-09 14:46:39 -0700 | [diff] [blame] | 82 | } |
Richard Barnette | d793606 | 2013-01-18 13:38:51 -0800 | [diff] [blame] | 83 | if (proxy == NULL) { |
| 84 | LOG(ERROR) << "Giving up -- unable to get dbus proxy for " |
| 85 | << kUpdateEngineServiceName; |
| 86 | exit(1); |
| 87 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 88 | *out_proxy = proxy; |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | static void StatusUpdateSignalHandler(DBusGProxy* proxy, |
| 93 | int64_t last_checked_time, |
| 94 | double progress, |
| 95 | gchar* current_operation, |
| 96 | gchar* new_version, |
| 97 | int64_t new_size, |
| 98 | void* user_data) { |
| 99 | LOG(INFO) << "Got status update:"; |
| 100 | LOG(INFO) << " last_checked_time: " << last_checked_time; |
| 101 | LOG(INFO) << " progress: " << progress; |
| 102 | LOG(INFO) << " current_operation: " << current_operation; |
| 103 | LOG(INFO) << " new_version: " << new_version; |
| 104 | LOG(INFO) << " new_size: " << new_size; |
| 105 | } |
| 106 | |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 107 | bool ResetStatus() { |
| 108 | DBusGProxy* proxy; |
| 109 | GError* error = NULL; |
| 110 | |
| 111 | CHECK(GetProxy(&proxy)); |
| 112 | |
| 113 | gboolean rc = |
| 114 | org_chromium_UpdateEngineInterface_reset_status(proxy, &error); |
| 115 | return rc; |
| 116 | } |
| 117 | |
| 118 | |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 119 | // If |op| is non-NULL, sets it to the current operation string or an |
| 120 | // empty string if unable to obtain the current status. |
| 121 | bool GetStatus(string* op) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 122 | DBusGProxy* proxy; |
| 123 | GError* error = NULL; |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 124 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 125 | CHECK(GetProxy(&proxy)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 126 | |
| 127 | gint64 last_checked_time = 0; |
| 128 | gdouble progress = 0.0; |
| 129 | char* current_op = NULL; |
| 130 | char* new_version = NULL; |
| 131 | gint64 new_size = 0; |
| 132 | |
| 133 | gboolean rc = org_chromium_UpdateEngineInterface_get_status( |
| 134 | proxy, |
| 135 | &last_checked_time, |
| 136 | &progress, |
| 137 | ¤t_op, |
| 138 | &new_version, |
| 139 | &new_size, |
| 140 | &error); |
| 141 | if (rc == FALSE) { |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 142 | LOG(INFO) << "Error getting status: " << GetAndFreeGError(&error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 143 | } |
| 144 | printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n" |
| 145 | "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n", |
| 146 | last_checked_time, |
| 147 | progress, |
| 148 | current_op, |
| 149 | new_version, |
| 150 | new_size); |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 151 | if (op) { |
| 152 | *op = current_op ? current_op : ""; |
| 153 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 154 | return true; |
| 155 | } |
| 156 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 157 | // Should never return. |
| 158 | void WatchForUpdates() { |
| 159 | DBusGProxy* proxy; |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 160 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 161 | CHECK(GetProxy(&proxy)); |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 162 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 163 | // Register marshaller |
| 164 | dbus_g_object_register_marshaller( |
| 165 | update_engine_VOID__INT64_DOUBLE_STRING_STRING_INT64, |
| 166 | G_TYPE_NONE, |
| 167 | G_TYPE_INT64, |
| 168 | G_TYPE_DOUBLE, |
| 169 | G_TYPE_STRING, |
| 170 | G_TYPE_STRING, |
| 171 | G_TYPE_INT64, |
| 172 | G_TYPE_INVALID); |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 173 | |
| 174 | static const char kStatusUpdate[] = "StatusUpdate"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 175 | dbus_g_proxy_add_signal(proxy, |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 176 | kStatusUpdate, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 177 | G_TYPE_INT64, |
| 178 | G_TYPE_DOUBLE, |
| 179 | G_TYPE_STRING, |
| 180 | G_TYPE_STRING, |
| 181 | G_TYPE_INT64, |
| 182 | G_TYPE_INVALID); |
| 183 | GMainLoop* loop = g_main_loop_new (NULL, TRUE); |
| 184 | dbus_g_proxy_connect_signal(proxy, |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 185 | kStatusUpdate, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 186 | G_CALLBACK(StatusUpdateSignalHandler), |
| 187 | NULL, |
| 188 | NULL); |
| 189 | g_main_loop_run(loop); |
| 190 | g_main_loop_unref(loop); |
| 191 | } |
| 192 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 193 | bool Rollback(bool rollback) { |
| 194 | DBusGProxy* proxy; |
| 195 | GError* error = NULL; |
| 196 | |
| 197 | CHECK(GetProxy(&proxy)); |
| 198 | |
| 199 | gboolean rc = |
| 200 | org_chromium_UpdateEngineInterface_attempt_rollback(proxy, |
| 201 | rollback, |
| 202 | &error); |
| 203 | CHECK_EQ(rc, TRUE) << "Error with rollback request: " |
| 204 | << GetAndFreeGError(&error); |
| 205 | return true; |
| 206 | } |
| 207 | |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 208 | bool CheckForUpdates(const string& app_version, const string& omaha_url) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 209 | DBusGProxy* proxy; |
| 210 | GError* error = NULL; |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 211 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 212 | CHECK(GetProxy(&proxy)); |
| 213 | |
| 214 | gboolean rc = |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 215 | org_chromium_UpdateEngineInterface_attempt_update(proxy, |
| 216 | app_version.c_str(), |
| 217 | omaha_url.c_str(), |
| 218 | &error); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 219 | CHECK_EQ(rc, TRUE) << "Error checking for update: " |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 220 | << GetAndFreeGError(&error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 224 | bool RebootIfNeeded() { |
| 225 | DBusGProxy* proxy; |
| 226 | GError* error = NULL; |
| 227 | |
| 228 | CHECK(GetProxy(&proxy)); |
| 229 | |
| 230 | gboolean rc = |
| 231 | org_chromium_UpdateEngineInterface_reboot_if_needed(proxy, &error); |
| 232 | // Reboot error code doesn't necessarily mean that a reboot |
| 233 | // failed. For example, D-Bus may be shutdown before we receive the |
| 234 | // result. |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 235 | LOG_IF(INFO, !rc) << "Reboot error message: " << GetAndFreeGError(&error); |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 236 | return true; |
| 237 | } |
| 238 | |
Chris Sosa | cb7fa88 | 2013-07-25 17:02:59 -0700 | [diff] [blame] | 239 | void SetTargetChannel(const string& target_channel, bool allow_powerwash) { |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 240 | DBusGProxy* proxy; |
| 241 | GError* error = NULL; |
| 242 | |
| 243 | CHECK(GetProxy(&proxy)); |
| 244 | |
| 245 | gboolean rc = |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 246 | org_chromium_UpdateEngineInterface_set_channel(proxy, |
| 247 | target_channel.c_str(), |
Chris Sosa | cb7fa88 | 2013-07-25 17:02:59 -0700 | [diff] [blame] | 248 | allow_powerwash, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 249 | &error); |
| 250 | CHECK_EQ(rc, true) << "Error setting the channel: " |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 251 | << GetAndFreeGError(&error); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 252 | LOG(INFO) << "Channel permanently set to: " << target_channel; |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 255 | string GetChannel(bool get_current_channel) { |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 256 | DBusGProxy* proxy; |
| 257 | GError* error = NULL; |
| 258 | |
| 259 | CHECK(GetProxy(&proxy)); |
| 260 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 261 | char* channel = NULL; |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 262 | gboolean rc = |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 263 | org_chromium_UpdateEngineInterface_get_channel(proxy, |
| 264 | get_current_channel, |
| 265 | &channel, |
| 266 | &error); |
| 267 | CHECK_EQ(rc, true) << "Error getting the channel: " |
| 268 | << GetAndFreeGError(&error); |
| 269 | string output = channel; |
| 270 | g_free(channel); |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 271 | return output; |
| 272 | } |
| 273 | |
Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame^] | 274 | void SetUpdateOverCellularPermission(gboolean allowed) { |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 275 | DBusGProxy* proxy; |
| 276 | GError* error = NULL; |
| 277 | |
| 278 | CHECK(GetProxy(&proxy)); |
| 279 | |
| 280 | gboolean rc = |
| 281 | org_chromium_UpdateEngineInterface_set_update_over_cellular_permission( |
| 282 | proxy, |
| 283 | allowed, |
| 284 | &error); |
| 285 | CHECK_EQ(rc, true) << "Error setting the update over cellular setting: " |
| 286 | << GetAndFreeGError(&error); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | bool GetUpdateOverCellularPermission() { |
| 290 | DBusGProxy* proxy; |
| 291 | GError* error = NULL; |
| 292 | |
| 293 | CHECK(GetProxy(&proxy)); |
| 294 | |
| 295 | gboolean allowed; |
| 296 | gboolean rc = |
| 297 | org_chromium_UpdateEngineInterface_get_update_over_cellular_permission( |
| 298 | proxy, |
| 299 | &allowed, |
| 300 | &error); |
| 301 | CHECK_EQ(rc, true) << "Error getting the update over cellular setting: " |
| 302 | << GetAndFreeGError(&error); |
| 303 | return allowed; |
| 304 | } |
| 305 | |
Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame^] | 306 | void SetP2PUpdatePermission(gboolean enabled) { |
| 307 | DBusGProxy* proxy; |
| 308 | GError* error = NULL; |
| 309 | |
| 310 | CHECK(GetProxy(&proxy)); |
| 311 | |
| 312 | gboolean rc = |
| 313 | org_chromium_UpdateEngineInterface_set_p2_pupdate_permission( |
| 314 | proxy, |
| 315 | enabled, |
| 316 | &error); |
| 317 | CHECK_EQ(rc, true) << "Error setting the peer-to-peer update setting: " |
| 318 | << GetAndFreeGError(&error); |
| 319 | } |
| 320 | |
| 321 | bool GetP2PUpdatePermission() { |
| 322 | DBusGProxy* proxy; |
| 323 | GError* error = NULL; |
| 324 | |
| 325 | CHECK(GetProxy(&proxy)); |
| 326 | |
| 327 | gboolean enabled; |
| 328 | gboolean rc = |
| 329 | org_chromium_UpdateEngineInterface_get_p2_pupdate_permission( |
| 330 | proxy, |
| 331 | &enabled, |
| 332 | &error); |
| 333 | CHECK_EQ(rc, true) << "Error getting the peer-to-peer update setting: " |
| 334 | << GetAndFreeGError(&error); |
| 335 | return enabled; |
| 336 | } |
| 337 | |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 338 | static gboolean CompleteUpdateSource(gpointer data) { |
| 339 | string current_op; |
| 340 | if (!GetStatus(¤t_op) || current_op == "UPDATE_STATUS_IDLE") { |
| 341 | LOG(ERROR) << "Update failed."; |
| 342 | exit(1); |
| 343 | } |
| 344 | if (current_op == "UPDATE_STATUS_UPDATED_NEED_REBOOT") { |
| 345 | LOG(INFO) << "Update succeeded -- reboot needed."; |
| 346 | exit(0); |
| 347 | } |
| 348 | return TRUE; |
| 349 | } |
| 350 | |
| 351 | // This is similar to watching for updates but rather than registering |
| 352 | // a signal watch, activelly poll the daemon just in case it stops |
| 353 | // sending notifications. |
| 354 | void CompleteUpdate() { |
| 355 | GMainLoop* loop = g_main_loop_new (NULL, TRUE); |
| 356 | g_timeout_add_seconds(5, CompleteUpdateSource, NULL); |
| 357 | g_main_loop_run(loop); |
| 358 | g_main_loop_unref(loop); |
| 359 | } |
| 360 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 361 | } // namespace {} |
| 362 | |
| 363 | int main(int argc, char** argv) { |
| 364 | // Boilerplate init commands. |
| 365 | g_type_init(); |
Ben Chan | 46bf5c8 | 2013-06-24 11:17:41 -0700 | [diff] [blame] | 366 | dbus_threads_init_default(); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 367 | chromeos_update_engine::Subprocess::Init(); |
| 368 | google::ParseCommandLineFlags(&argc, &argv, true); |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 369 | |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 370 | // Update the status if requested. |
| 371 | if (FLAGS_reset_status) { |
| 372 | LOG(INFO) << "Setting Update Engine status to idle ..."; |
| 373 | if (!ResetStatus()) { |
| 374 | LOG(ERROR) << "ResetStatus failed."; |
| 375 | return 1; |
| 376 | } |
| 377 | |
Gilad Arnold | 50c6063 | 2013-01-25 10:27:19 -0800 | [diff] [blame] | 378 | LOG(INFO) << "ResetStatus succeeded; to undo partition table changes run:\n" |
| 379 | "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo ${P#$D} " |
| 380 | "| sed 's/^[^0-9]*//')-1)) $D;)"; |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 385 | if (FLAGS_status) { |
| 386 | LOG(INFO) << "Querying Update Engine status..."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 387 | if (!GetStatus(NULL)) { |
| 388 | LOG(FATAL) << "GetStatus failed."; |
| 389 | return 1; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 390 | } |
| 391 | return 0; |
| 392 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 393 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 394 | // Changes the current update over cellular network setting. |
| 395 | if (!FLAGS_update_over_cellular.empty()) { |
| 396 | gboolean allowed = FLAGS_update_over_cellular == "yes"; |
| 397 | if (!allowed && FLAGS_update_over_cellular != "no") { |
| 398 | LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular |
| 399 | << "\". Please specify \"yes\" or \"no\"."; |
| 400 | } else { |
| 401 | SetUpdateOverCellularPermission(allowed); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // Show the current update over cellular network setting. |
| 406 | if (FLAGS_show_update_over_cellular) { |
| 407 | bool allowed = GetUpdateOverCellularPermission(); |
| 408 | LOG(INFO) << "Current update over cellular network setting: " |
| 409 | << (allowed ? "ENABLED" : "DISABLED"); |
| 410 | } |
| 411 | |
Chris Sosa | cb7fa88 | 2013-07-25 17:02:59 -0700 | [diff] [blame] | 412 | if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) { |
| 413 | LOG(FATAL) << "powerwash flag only makes sense rollback or channel change"; |
| 414 | return 1; |
| 415 | } |
| 416 | |
Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame^] | 417 | // Change the P2P enabled setting. |
| 418 | if (!FLAGS_p2p_update.empty()) { |
| 419 | gboolean enabled = FLAGS_p2p_update == "yes"; |
| 420 | if (!enabled && FLAGS_p2p_update != "no") { |
| 421 | LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update |
| 422 | << "\". Please specify \"yes\" or \"no\"."; |
| 423 | } else { |
| 424 | SetP2PUpdatePermission(enabled); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // Show the current P2P enabled setting. |
| 429 | if (FLAGS_show_p2p_update) { |
| 430 | bool enabled = GetP2PUpdatePermission(); |
| 431 | LOG(INFO) << "Current update using P2P setting: " |
| 432 | << (enabled ? "ENABLED" : "DISABLED"); |
| 433 | } |
| 434 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 435 | // First, update the target channel if requested. |
| 436 | if (!FLAGS_channel.empty()) |
Chris Sosa | cb7fa88 | 2013-07-25 17:02:59 -0700 | [diff] [blame] | 437 | SetTargetChannel(FLAGS_channel, FLAGS_powerwash); |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 438 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 439 | // Show the current and target channels if requested. |
| 440 | if (FLAGS_show_channel) { |
| 441 | string current_channel = GetChannel(true); |
| 442 | LOG(INFO) << "Current Channel: " << current_channel; |
| 443 | |
| 444 | string target_channel = GetChannel(false); |
| 445 | if (!target_channel.empty()) |
| 446 | LOG(INFO) << "Target Channel (pending update): " << target_channel; |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 447 | } |
| 448 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 449 | bool do_update_request = FLAGS_check_for_update | FLAGS_update | |
| 450 | !FLAGS_app_version.empty() | !FLAGS_omaha_url.empty(); |
| 451 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 452 | if (do_update_request && FLAGS_rollback) { |
| 453 | LOG(FATAL) << "Update should not be requested with rollback!"; |
| 454 | return 1; |
| 455 | } |
| 456 | |
| 457 | if(FLAGS_rollback) { |
| 458 | LOG(INFO) << "Requesting rollback."; |
| 459 | CHECK(Rollback(FLAGS_powerwash)) << "Request for rollback failed."; |
| 460 | return 0; |
| 461 | } |
| 462 | |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 463 | // Initiate an update check, if necessary. |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 464 | if (do_update_request) { |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 465 | LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 466 | string app_version = FLAGS_app_version; |
| 467 | if (FLAGS_update && app_version.empty()) { |
| 468 | app_version = "ForcedUpdate"; |
| 469 | LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate."; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 470 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 471 | LOG(INFO) << "Initiating update check and install."; |
| 472 | CHECK(CheckForUpdates(app_version, FLAGS_omaha_url)) |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 473 | << "Update check/initiate update failed."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 474 | |
| 475 | // Wait for an update to complete. |
| 476 | if (FLAGS_update) { |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 477 | LOG(INFO) << "Waiting for update to complete."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 478 | CompleteUpdate(); // Should never return. |
| 479 | return 1; |
| 480 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 481 | return 0; |
| 482 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 483 | |
| 484 | // Start watching for updates. |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 485 | if (FLAGS_watch_for_updates) { |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 486 | LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored."; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 487 | LOG(INFO) << "Watching for status updates."; |
| 488 | WatchForUpdates(); // Should never return. |
| 489 | return 1; |
| 490 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 491 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 492 | if (FLAGS_reboot) { |
| 493 | LOG(INFO) << "Requesting a reboot..."; |
| 494 | CHECK(RebootIfNeeded()); |
| 495 | return 0; |
| 496 | } |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 497 | |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 498 | LOG(INFO) << "Done."; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 499 | return 0; |
| 500 | } |