| 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 |  | 
| David Zeuthen | 9d73a72 | 2014-04-04 14:52:46 -0700 | [diff] [blame] | 7 | #include <chromeos/dbus/service_constants.h> | 
| Ben Chan | 46bf5c8 | 2013-06-24 11:17:41 -0700 | [diff] [blame] | 8 | #include <dbus/dbus.h> | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 9 | #include <gflags/gflags.h> | 
 | 10 | #include <glib.h> | 
 | 11 |  | 
 | 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; | 
| David Zeuthen | 75a4c3e | 2013-09-06 11:36:59 -0700 | [diff] [blame] | 23 | using chromeos_update_engine::AttemptUpdateFlags; | 
 | 24 | using chromeos_update_engine::kAttemptUpdateFlagNonInteractive; | 
| Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 25 | using chromeos_update_engine::utils::GetAndFreeGError; | 
| Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 26 | using std::string; | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 27 |  | 
| Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 28 | DEFINE_string(app_version, "", "Force the current app version."); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 29 | DEFINE_string(channel, "", | 
| Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 30 |               "Set the target channel. The device will be powerwashed if the " | 
 | 31 |               "target channel is more stable than the current channel unless " | 
 | 32 |               "--nopowerwash is specified."); | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 33 | DEFINE_bool(check_for_update, false, "Initiate check for updates."); | 
| Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 34 | DEFINE_bool(follow, false, "Wait for any update operations to complete." | 
 | 35 |             "Exit status is 0 if the update succeeded, and 1 otherwise."); | 
 | 36 | DEFINE_bool(interactive, true, "Mark the update request as interactive."); | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 37 | DEFINE_string(omaha_url, "", "The URL of the Omaha update server."); | 
| Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 38 | DEFINE_string(p2p_update, "", | 
 | 39 |               "Enables (\"yes\") or disables (\"no\") the peer-to-peer update " | 
 | 40 |               "sharing."); | 
 | 41 | DEFINE_bool(powerwash, true, "When performing rollback or channel change, " | 
 | 42 |             "do a powerwash or allow it respectively."); | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 43 | DEFINE_bool(reboot, false, "Initiate a reboot if needed."); | 
| David Zeuthen | 9d73a72 | 2014-04-04 14:52:46 -0700 | [diff] [blame] | 44 | DEFINE_bool(is_reboot_needed, false, "Exit status 0 if reboot is needed, " | 
 | 45 |             "2 if reboot is not needed or 1 if an error occurred."); | 
 | 46 | DEFINE_bool(block_until_reboot_is_needed, false, "Blocks until reboot is " | 
 | 47 |             "needed. Returns non-zero exit status if an error occurred."); | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 48 | DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle."); | 
 | 49 | DEFINE_bool(rollback, false, "Perform a rollback to the previous partition."); | 
| Alex Vakulenko | 59e253e | 2014-02-24 10:40:21 -0800 | [diff] [blame] | 50 | DEFINE_bool(can_rollback, false, "Shows whether rollback partition " | 
 | 51 |             "is available."); | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 52 | DEFINE_bool(show_channel, false, "Show the current and target channels."); | 
| Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 53 | DEFINE_bool(show_p2p_update, false, | 
 | 54 |             "Show the current setting for peer-to-peer update sharing."); | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 55 | DEFINE_bool(show_update_over_cellular, false, | 
 | 56 |             "Show the current setting for updates over cellular networks."); | 
| Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 57 | DEFINE_bool(status, false, "Print the status to stdout."); | 
 | 58 | DEFINE_bool(update, false, "Forces an update and waits for it to complete. " | 
 | 59 |             "Implies --follow."); | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 60 | DEFINE_string(update_over_cellular, "", | 
 | 61 |               "Enables (\"yes\") or disables (\"no\") the updates over " | 
 | 62 |               "cellular networks."); | 
| Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 63 | DEFINE_bool(watch_for_updates, false, | 
 | 64 |             "Listen for status updates and print them to the screen."); | 
| Alex Vakulenko | dea2eac | 2014-03-14 15:56:59 -0700 | [diff] [blame] | 65 | DEFINE_bool(prev_version, false, | 
 | 66 |             "Show the previous OS version used before the update reboot."); | 
| Alex Vakulenko | 2bddadd | 2014-03-27 13:23:46 -0700 | [diff] [blame] | 67 | DEFINE_bool(show_kernels, false, "Show the list of kernel patritions and " | 
 | 68 |             "whether each of them is bootable or not"); | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 69 |  | 
 | 70 | namespace { | 
 | 71 |  | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 72 | bool GetProxy(DBusGProxy** out_proxy) { | 
 | 73 |   DBusGConnection* bus; | 
| Andrew de los Reyes | 68ab6ed | 2011-08-09 14:46:39 -0700 | [diff] [blame] | 74 |   DBusGProxy* proxy = NULL; | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 75 |   GError* error = NULL; | 
| Andrew de los Reyes | 68ab6ed | 2011-08-09 14:46:39 -0700 | [diff] [blame] | 76 |   const int kTries = 4; | 
| Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 77 |   const int kRetrySeconds = 10; | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 78 |  | 
 | 79 |   bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); | 
| Richard Barnette | d793606 | 2013-01-18 13:38:51 -0800 | [diff] [blame] | 80 |   if (bus == NULL) { | 
 | 81 |     LOG(ERROR) << "Failed to get bus: " << GetAndFreeGError(&error); | 
 | 82 |     exit(1); | 
 | 83 |   } | 
| Andrew de los Reyes | 68ab6ed | 2011-08-09 14:46:39 -0700 | [diff] [blame] | 84 |   for (int i = 0; !proxy && i < kTries; ++i) { | 
| Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 85 |     if (i > 0) { | 
 | 86 |       LOG(INFO) << "Retrying to get dbus proxy. Try " | 
 | 87 |                 << (i + 1) << "/" << kTries; | 
| Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 88 |       g_usleep(kRetrySeconds * G_USEC_PER_SEC); | 
| Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 89 |     } | 
| Andrew de los Reyes | 68ab6ed | 2011-08-09 14:46:39 -0700 | [diff] [blame] | 90 |     proxy = dbus_g_proxy_new_for_name_owner(bus, | 
 | 91 |                                             kUpdateEngineServiceName, | 
 | 92 |                                             kUpdateEngineServicePath, | 
 | 93 |                                             kUpdateEngineServiceInterface, | 
 | 94 |                                             &error); | 
| Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 95 |     LOG_IF(WARNING, !proxy) << "Error getting dbus proxy for " | 
 | 96 |                             << kUpdateEngineServiceName << ": " | 
 | 97 |                             << GetAndFreeGError(&error); | 
| Andrew de los Reyes | 68ab6ed | 2011-08-09 14:46:39 -0700 | [diff] [blame] | 98 |   } | 
| Richard Barnette | d793606 | 2013-01-18 13:38:51 -0800 | [diff] [blame] | 99 |   if (proxy == NULL) { | 
 | 100 |     LOG(ERROR) << "Giving up -- unable to get dbus proxy for " | 
 | 101 |                << kUpdateEngineServiceName; | 
 | 102 |     exit(1); | 
 | 103 |   } | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 104 |   *out_proxy = proxy; | 
 | 105 |   return true; | 
 | 106 | } | 
 | 107 |  | 
 | 108 | static void StatusUpdateSignalHandler(DBusGProxy* proxy, | 
 | 109 |                                       int64_t last_checked_time, | 
 | 110 |                                       double progress, | 
 | 111 |                                       gchar* current_operation, | 
 | 112 |                                       gchar* new_version, | 
 | 113 |                                       int64_t new_size, | 
 | 114 |                                       void* user_data) { | 
 | 115 |   LOG(INFO) << "Got status update:"; | 
 | 116 |   LOG(INFO) << "  last_checked_time: " << last_checked_time; | 
 | 117 |   LOG(INFO) << "  progress: " << progress; | 
 | 118 |   LOG(INFO) << "  current_operation: " << current_operation; | 
 | 119 |   LOG(INFO) << "  new_version: " << new_version; | 
 | 120 |   LOG(INFO) << "  new_size: " << new_size; | 
 | 121 | } | 
 | 122 |  | 
| Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 123 | bool ResetStatus() { | 
 | 124 |   DBusGProxy* proxy; | 
 | 125 |   GError* error = NULL; | 
 | 126 |  | 
 | 127 |   CHECK(GetProxy(&proxy)); | 
 | 128 |  | 
| Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 129 |   gboolean rc = update_engine_client_reset_status(proxy, &error); | 
| Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 130 |   return rc; | 
 | 131 | } | 
 | 132 |  | 
 | 133 |  | 
| Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 134 | // If |op| is non-NULL, sets it to the current operation string or an | 
 | 135 | // empty string if unable to obtain the current status. | 
 | 136 | bool GetStatus(string* op) { | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 137 |   DBusGProxy* proxy; | 
 | 138 |   GError* error = NULL; | 
| Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 139 |  | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 140 |   CHECK(GetProxy(&proxy)); | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 141 |  | 
 | 142 |   gint64 last_checked_time = 0; | 
 | 143 |   gdouble progress = 0.0; | 
 | 144 |   char* current_op = NULL; | 
 | 145 |   char* new_version = NULL; | 
 | 146 |   gint64 new_size = 0; | 
 | 147 |  | 
| Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 148 |   gboolean rc = update_engine_client_get_status(proxy, | 
 | 149 |                                                 &last_checked_time, | 
 | 150 |                                                 &progress, | 
 | 151 |                                                 ¤t_op, | 
 | 152 |                                                 &new_version, | 
 | 153 |                                                 &new_size, | 
 | 154 |                                                 &error); | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 155 |   if (rc == FALSE) { | 
| Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 156 |     LOG(INFO) << "Error getting status: " << GetAndFreeGError(&error); | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 157 |   } | 
 | 158 |   printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n" | 
 | 159 |          "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n", | 
 | 160 |          last_checked_time, | 
 | 161 |          progress, | 
 | 162 |          current_op, | 
 | 163 |          new_version, | 
 | 164 |          new_size); | 
| Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 165 |   if (op) { | 
 | 166 |     *op = current_op ? current_op : ""; | 
 | 167 |   } | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 168 |   return true; | 
 | 169 | } | 
 | 170 |  | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 171 | // Should never return. | 
 | 172 | void WatchForUpdates() { | 
 | 173 |   DBusGProxy* proxy; | 
| Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 174 |  | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 175 |   CHECK(GetProxy(&proxy)); | 
| Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 176 |  | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 177 |   // Register marshaller | 
 | 178 |   dbus_g_object_register_marshaller( | 
| David Zeuthen | 7cb12f9 | 2014-04-08 10:35:39 -0700 | [diff] [blame] | 179 |       g_cclosure_marshal_generic, | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 180 |       G_TYPE_NONE, | 
 | 181 |       G_TYPE_INT64, | 
 | 182 |       G_TYPE_DOUBLE, | 
 | 183 |       G_TYPE_STRING, | 
 | 184 |       G_TYPE_STRING, | 
 | 185 |       G_TYPE_INT64, | 
 | 186 |       G_TYPE_INVALID); | 
| Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 187 |  | 
 | 188 |   static const char kStatusUpdate[] = "StatusUpdate"; | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 189 |   dbus_g_proxy_add_signal(proxy, | 
| Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 190 |                           kStatusUpdate, | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 191 |                           G_TYPE_INT64, | 
 | 192 |                           G_TYPE_DOUBLE, | 
 | 193 |                           G_TYPE_STRING, | 
 | 194 |                           G_TYPE_STRING, | 
 | 195 |                           G_TYPE_INT64, | 
 | 196 |                           G_TYPE_INVALID); | 
 | 197 |   GMainLoop* loop = g_main_loop_new (NULL, TRUE); | 
 | 198 |   dbus_g_proxy_connect_signal(proxy, | 
| Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 199 |                               kStatusUpdate, | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 200 |                               G_CALLBACK(StatusUpdateSignalHandler), | 
 | 201 |                               NULL, | 
 | 202 |                               NULL); | 
 | 203 |   g_main_loop_run(loop); | 
 | 204 |   g_main_loop_unref(loop); | 
 | 205 | } | 
 | 206 |  | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 207 | bool Rollback(bool rollback) { | 
 | 208 |   DBusGProxy* proxy; | 
 | 209 |   GError* error = NULL; | 
 | 210 |  | 
 | 211 |   CHECK(GetProxy(&proxy)); | 
 | 212 |  | 
| Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 213 |   gboolean rc = update_engine_client_attempt_rollback(proxy, | 
 | 214 |                                                       rollback, | 
 | 215 |                                                       &error); | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 216 |   CHECK_EQ(rc, TRUE) << "Error with rollback request: " | 
 | 217 |                      << GetAndFreeGError(&error); | 
 | 218 |   return true; | 
 | 219 | } | 
 | 220 |  | 
| Alex Vakulenko | 2bddadd | 2014-03-27 13:23:46 -0700 | [diff] [blame] | 221 | std::string GetRollbackPartition() { | 
| Alex Vakulenko | 59e253e | 2014-02-24 10:40:21 -0800 | [diff] [blame] | 222 |   DBusGProxy* proxy; | 
 | 223 |   GError* error = NULL; | 
 | 224 |  | 
 | 225 |   CHECK(GetProxy(&proxy)); | 
 | 226 |  | 
| Alex Vakulenko | 2bddadd | 2014-03-27 13:23:46 -0700 | [diff] [blame] | 227 |   char* rollback_partition = nullptr; | 
 | 228 |   gboolean rc = update_engine_client_get_rollback_partition(proxy, | 
 | 229 |                                                             &rollback_partition, | 
 | 230 |                                                             &error); | 
| Alex Vakulenko | 59e253e | 2014-02-24 10:40:21 -0800 | [diff] [blame] | 231 |   CHECK_EQ(rc, TRUE) << "Error while querying rollback partition availabilty: " | 
 | 232 |                      << GetAndFreeGError(&error); | 
| Alex Vakulenko | 2bddadd | 2014-03-27 13:23:46 -0700 | [diff] [blame] | 233 |   std::string partition = rollback_partition; | 
 | 234 |   g_free(rollback_partition); | 
 | 235 |   return partition; | 
| Alex Vakulenko | 59e253e | 2014-02-24 10:40:21 -0800 | [diff] [blame] | 236 | } | 
| Alex Vakulenko | 2bddadd | 2014-03-27 13:23:46 -0700 | [diff] [blame] | 237 |  | 
 | 238 | std::string GetKernelDevices() { | 
 | 239 |   DBusGProxy* proxy; | 
 | 240 |   GError* error = nullptr; | 
 | 241 |  | 
 | 242 |   CHECK(GetProxy(&proxy)); | 
 | 243 |  | 
 | 244 |   char* kernel_devices = nullptr; | 
 | 245 |   gboolean rc = update_engine_client_get_kernel_devices(proxy, | 
 | 246 |                                                         &kernel_devices, | 
 | 247 |                                                         &error); | 
 | 248 |   CHECK_EQ(rc, TRUE) << "Error while getting a list of kernel devices: " | 
 | 249 |     << GetAndFreeGError(&error); | 
 | 250 |   std::string devices = kernel_devices; | 
 | 251 |   g_free(kernel_devices); | 
 | 252 |   return devices; | 
 | 253 | } | 
 | 254 |  | 
| Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 255 | bool CheckForUpdates(const string& app_version, const string& omaha_url) { | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 256 |   DBusGProxy* proxy; | 
 | 257 |   GError* error = NULL; | 
| Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 258 |  | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 259 |   CHECK(GetProxy(&proxy)); | 
 | 260 |  | 
| David Zeuthen | 75a4c3e | 2013-09-06 11:36:59 -0700 | [diff] [blame] | 261 |   AttemptUpdateFlags flags = static_cast<AttemptUpdateFlags>( | 
 | 262 |       FLAGS_interactive ? 0 : kAttemptUpdateFlagNonInteractive); | 
 | 263 |   gboolean rc = | 
 | 264 |       update_engine_client_attempt_update_with_flags(proxy, | 
 | 265 |                                                      app_version.c_str(), | 
 | 266 |                                                      omaha_url.c_str(), | 
 | 267 |                                                      static_cast<gint>(flags), | 
 | 268 |                                                      &error); | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 269 |   CHECK_EQ(rc, TRUE) << "Error checking for update: " | 
| Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 270 |                      << GetAndFreeGError(&error); | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 271 |   return true; | 
 | 272 | } | 
 | 273 |  | 
| Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 274 | bool RebootIfNeeded() { | 
 | 275 |   DBusGProxy* proxy; | 
 | 276 |   GError* error = NULL; | 
 | 277 |  | 
 | 278 |   CHECK(GetProxy(&proxy)); | 
 | 279 |  | 
 | 280 |   gboolean rc = | 
| Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 281 |       update_engine_client_reboot_if_needed(proxy, &error); | 
| Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 282 |   // Reboot error code doesn't necessarily mean that a reboot | 
 | 283 |   // failed. For example, D-Bus may be shutdown before we receive the | 
 | 284 |   // result. | 
| Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 285 |   LOG_IF(INFO, !rc) << "Reboot error message: " << GetAndFreeGError(&error); | 
| Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 286 |   return true; | 
 | 287 | } | 
 | 288 |  | 
| Chris Sosa | cb7fa88 | 2013-07-25 17:02:59 -0700 | [diff] [blame] | 289 | void SetTargetChannel(const string& target_channel, bool allow_powerwash) { | 
| Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 290 |   DBusGProxy* proxy; | 
 | 291 |   GError* error = NULL; | 
 | 292 |  | 
 | 293 |   CHECK(GetProxy(&proxy)); | 
 | 294 |  | 
| Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 295 |   gboolean rc = update_engine_client_set_channel(proxy, | 
 | 296 |                                                  target_channel.c_str(), | 
 | 297 |                                                  allow_powerwash, | 
 | 298 |                                                  &error); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 299 |   CHECK_EQ(rc, true) << "Error setting the channel: " | 
| Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 300 |                      << GetAndFreeGError(&error); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 301 |   LOG(INFO) << "Channel permanently set to: " << target_channel; | 
| Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 302 | } | 
 | 303 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 304 | string GetChannel(bool get_current_channel) { | 
| Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 305 |   DBusGProxy* proxy; | 
 | 306 |   GError* error = NULL; | 
 | 307 |  | 
 | 308 |   CHECK(GetProxy(&proxy)); | 
 | 309 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 310 |   char* channel = NULL; | 
| Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 311 |   gboolean rc = update_engine_client_get_channel(proxy, | 
 | 312 |                                                  get_current_channel, | 
 | 313 |                                                  &channel, | 
 | 314 |                                                  &error); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 315 |   CHECK_EQ(rc, true) << "Error getting the channel: " | 
 | 316 |                      << GetAndFreeGError(&error); | 
 | 317 |   string output = channel; | 
 | 318 |   g_free(channel); | 
| Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 319 |   return output; | 
 | 320 | } | 
 | 321 |  | 
| Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 322 | void SetUpdateOverCellularPermission(gboolean allowed) { | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 323 |   DBusGProxy* proxy; | 
 | 324 |   GError* error = NULL; | 
 | 325 |  | 
 | 326 |   CHECK(GetProxy(&proxy)); | 
 | 327 |  | 
| Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 328 |   gboolean rc = update_engine_client_set_update_over_cellular_permission( | 
 | 329 |       proxy, | 
 | 330 |       allowed, | 
 | 331 |       &error); | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 332 |   CHECK_EQ(rc, true) << "Error setting the update over cellular setting: " | 
 | 333 |                      << GetAndFreeGError(&error); | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 334 | } | 
 | 335 |  | 
 | 336 | bool GetUpdateOverCellularPermission() { | 
 | 337 |   DBusGProxy* proxy; | 
 | 338 |   GError* error = NULL; | 
 | 339 |  | 
 | 340 |   CHECK(GetProxy(&proxy)); | 
 | 341 |  | 
 | 342 |   gboolean allowed; | 
| Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 343 |   gboolean rc = update_engine_client_get_update_over_cellular_permission( | 
 | 344 |       proxy, | 
 | 345 |       &allowed, | 
 | 346 |       &error); | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 347 |   CHECK_EQ(rc, true) << "Error getting the update over cellular setting: " | 
 | 348 |                      << GetAndFreeGError(&error); | 
 | 349 |   return allowed; | 
 | 350 | } | 
 | 351 |  | 
| Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 352 | void SetP2PUpdatePermission(gboolean enabled) { | 
 | 353 |   DBusGProxy* proxy; | 
 | 354 |   GError* error = NULL; | 
 | 355 |  | 
 | 356 |   CHECK(GetProxy(&proxy)); | 
 | 357 |  | 
| Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 358 |   gboolean rc = update_engine_client_set_p2p_update_permission( | 
 | 359 |       proxy, | 
 | 360 |       enabled, | 
 | 361 |       &error); | 
| Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 362 |   CHECK_EQ(rc, true) << "Error setting the peer-to-peer update setting: " | 
 | 363 |                      << GetAndFreeGError(&error); | 
 | 364 | } | 
 | 365 |  | 
 | 366 | bool GetP2PUpdatePermission() { | 
 | 367 |   DBusGProxy* proxy; | 
 | 368 |   GError* error = NULL; | 
 | 369 |  | 
 | 370 |   CHECK(GetProxy(&proxy)); | 
 | 371 |  | 
 | 372 |   gboolean enabled; | 
| Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 373 |   gboolean rc = update_engine_client_get_p2p_update_permission( | 
 | 374 |       proxy, | 
 | 375 |       &enabled, | 
 | 376 |       &error); | 
| Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 377 |   CHECK_EQ(rc, true) << "Error getting the peer-to-peer update setting: " | 
 | 378 |                      << GetAndFreeGError(&error); | 
 | 379 |   return enabled; | 
 | 380 | } | 
 | 381 |  | 
| Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 382 | static gboolean CompleteUpdateSource(gpointer data) { | 
 | 383 |   string current_op; | 
 | 384 |   if (!GetStatus(¤t_op) || current_op == "UPDATE_STATUS_IDLE") { | 
 | 385 |     LOG(ERROR) << "Update failed."; | 
 | 386 |     exit(1); | 
 | 387 |   } | 
 | 388 |   if (current_op == "UPDATE_STATUS_UPDATED_NEED_REBOOT") { | 
 | 389 |     LOG(INFO) << "Update succeeded -- reboot needed."; | 
 | 390 |     exit(0); | 
 | 391 |   } | 
 | 392 |   return TRUE; | 
 | 393 | } | 
 | 394 |  | 
 | 395 | // This is similar to watching for updates but rather than registering | 
 | 396 | // a signal watch, activelly poll the daemon just in case it stops | 
 | 397 | // sending notifications. | 
 | 398 | void CompleteUpdate() { | 
 | 399 |   GMainLoop* loop = g_main_loop_new (NULL, TRUE); | 
 | 400 |   g_timeout_add_seconds(5, CompleteUpdateSource, NULL); | 
 | 401 |   g_main_loop_run(loop); | 
 | 402 |   g_main_loop_unref(loop); | 
 | 403 | } | 
 | 404 |  | 
| Alex Vakulenko | dea2eac | 2014-03-14 15:56:59 -0700 | [diff] [blame] | 405 | void ShowPrevVersion() { | 
 | 406 |   DBusGProxy* proxy; | 
 | 407 |   GError* error = nullptr; | 
 | 408 |  | 
 | 409 |   CHECK(GetProxy(&proxy)); | 
 | 410 |  | 
 | 411 |   char* prev_version = nullptr; | 
 | 412 |  | 
 | 413 |   gboolean rc = update_engine_client_get_prev_version(proxy, | 
 | 414 |                                                       &prev_version, | 
 | 415 |                                                       &error); | 
 | 416 |   if (!rc) { | 
 | 417 |     LOG(ERROR) << "Error getting previous version: " | 
 | 418 |                << GetAndFreeGError(&error); | 
 | 419 |   } else { | 
 | 420 |     LOG(INFO) << "Previous version = " << prev_version; | 
 | 421 |     g_free(prev_version); | 
 | 422 |   } | 
 | 423 | } | 
 | 424 |  | 
| David Zeuthen | 9d73a72 | 2014-04-04 14:52:46 -0700 | [diff] [blame] | 425 | bool CheckIfRebootIsNeeded(DBusGProxy *proxy, bool *out_reboot_needed) { | 
 | 426 |   gint64 last_checked_time = 0; | 
 | 427 |   gdouble progress = 0.0; | 
 | 428 |   char* current_op = nullptr; | 
 | 429 |   char* new_version = nullptr; | 
 | 430 |   gint64 new_size = 0; | 
 | 431 |   GError* error = nullptr; | 
 | 432 |  | 
 | 433 |   if (!update_engine_client_get_status(proxy, | 
 | 434 |                                        &last_checked_time, | 
 | 435 |                                        &progress, | 
 | 436 |                                        ¤t_op, | 
 | 437 |                                        &new_version, | 
 | 438 |                                        &new_size, | 
 | 439 |                                        &error)) { | 
 | 440 |     LOG(INFO) << "Error getting status: " << GetAndFreeGError(&error); | 
 | 441 |     return false; | 
 | 442 |   } | 
 | 443 |   *out_reboot_needed = | 
 | 444 |       (g_strcmp0(current_op, | 
 | 445 |                  update_engine::kUpdateStatusUpdatedNeedReboot) == 0); | 
 | 446 |   g_free(current_op); | 
 | 447 |   g_free(new_version); | 
 | 448 |   return true; | 
 | 449 | } | 
 | 450 |  | 
 | 451 | // Determines if reboot is needed. The result is returned in | 
 | 452 | // |out_reboot_needed|. Returns true if the check succeeded, false | 
 | 453 | // otherwise. | 
 | 454 | bool IsRebootNeeded(bool *out_reboot_needed) { | 
 | 455 |   DBusGProxy* proxy = nullptr; | 
 | 456 |   CHECK(GetProxy(&proxy)); | 
 | 457 |   bool ret = CheckIfRebootIsNeeded(proxy, out_reboot_needed); | 
 | 458 |   g_object_unref(proxy); | 
 | 459 |   return ret; | 
 | 460 | } | 
 | 461 |  | 
 | 462 | static void OnBlockUntilRebootStatusCallback( | 
 | 463 |     DBusGProxy* proxy, | 
 | 464 |     int64_t last_checked_time, | 
 | 465 |     double progress, | 
 | 466 |     const gchar* current_operation, | 
 | 467 |     const gchar* new_version, | 
 | 468 |     int64_t new_size, | 
 | 469 |     void* user_data) { | 
 | 470 |   GMainLoop *loop = reinterpret_cast<GMainLoop*>(user_data); | 
 | 471 |   if (g_strcmp0(current_operation, | 
 | 472 |                 update_engine::kUpdateStatusUpdatedNeedReboot) == 0) { | 
 | 473 |     g_main_loop_quit(loop); | 
 | 474 |   } | 
 | 475 | } | 
 | 476 |  | 
 | 477 | bool CheckRebootNeeded(DBusGProxy *proxy, GMainLoop *loop) { | 
 | 478 |   bool reboot_needed; | 
 | 479 |   if (!CheckIfRebootIsNeeded(proxy, &reboot_needed)) | 
 | 480 |     return false; | 
 | 481 |   if (reboot_needed) | 
 | 482 |     return true; | 
 | 483 |   // This will block until OnBlockUntilRebootStatusCallback() calls | 
 | 484 |   // g_main_loop_quit(). | 
 | 485 |   g_main_loop_run(loop); | 
 | 486 |   return true; | 
 | 487 | } | 
 | 488 |  | 
 | 489 | // Blocks until a reboot is needed. Returns true if waiting succeeded, | 
 | 490 | // false if an error occurred. | 
 | 491 | bool BlockUntilRebootIsNeeded() { | 
 | 492 |   // The basic idea is to get a proxy, listen to signals and only then | 
 | 493 |   // check the status. If no reboot is needed, just sit and wait for | 
 | 494 |   // the StatusUpdate signal to convey that a reboot is pending. | 
 | 495 |   DBusGProxy* proxy = nullptr; | 
 | 496 |   CHECK(GetProxy(&proxy)); | 
 | 497 |   dbus_g_object_register_marshaller( | 
 | 498 |       g_cclosure_marshal_generic, | 
 | 499 |       G_TYPE_NONE, | 
 | 500 |       G_TYPE_INT64, | 
 | 501 |       G_TYPE_DOUBLE, | 
 | 502 |       G_TYPE_STRING, | 
 | 503 |       G_TYPE_STRING, | 
 | 504 |       G_TYPE_INT64, | 
 | 505 |       G_TYPE_INVALID); | 
 | 506 |   dbus_g_proxy_add_signal(proxy, | 
 | 507 |                           update_engine::kStatusUpdate,  // Signal name. | 
 | 508 |                           G_TYPE_INT64, | 
 | 509 |                           G_TYPE_DOUBLE, | 
 | 510 |                           G_TYPE_STRING, | 
 | 511 |                           G_TYPE_STRING, | 
 | 512 |                           G_TYPE_INT64, | 
 | 513 |                           G_TYPE_INVALID); | 
 | 514 |   GMainLoop* loop = g_main_loop_new(nullptr, TRUE); | 
 | 515 |   dbus_g_proxy_connect_signal(proxy, | 
 | 516 |                               update_engine::kStatusUpdate, | 
 | 517 |                               G_CALLBACK(OnBlockUntilRebootStatusCallback), | 
 | 518 |                               loop, | 
 | 519 |                               nullptr);  // free_data_func. | 
 | 520 |  | 
 | 521 |   bool ret = CheckRebootNeeded(proxy, loop); | 
 | 522 |  | 
 | 523 |   dbus_g_proxy_disconnect_signal(proxy, | 
 | 524 |                                  update_engine::kStatusUpdate, | 
 | 525 |                                  G_CALLBACK(OnBlockUntilRebootStatusCallback), | 
 | 526 |                                  loop); | 
 | 527 |   g_main_loop_unref(loop); | 
 | 528 |   g_object_unref(proxy); | 
 | 529 |   return ret; | 
 | 530 | } | 
 | 531 |  | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 532 | }  // namespace {} | 
 | 533 |  | 
 | 534 | int main(int argc, char** argv) { | 
 | 535 |   // Boilerplate init commands. | 
 | 536 |   g_type_init(); | 
| Ben Chan | 46bf5c8 | 2013-06-24 11:17:41 -0700 | [diff] [blame] | 537 |   dbus_threads_init_default(); | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 538 |   chromeos_update_engine::Subprocess::Init(); | 
 | 539 |   google::ParseCommandLineFlags(&argc, &argv, true); | 
| Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 540 |  | 
| Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 541 |   // Update the status if requested. | 
 | 542 |   if (FLAGS_reset_status) { | 
 | 543 |     LOG(INFO) << "Setting Update Engine status to idle ..."; | 
 | 544 |     if (!ResetStatus()) { | 
 | 545 |       LOG(ERROR) << "ResetStatus failed."; | 
 | 546 |       return 1; | 
 | 547 |     } | 
| Gilad Arnold | 50c6063 | 2013-01-25 10:27:19 -0800 | [diff] [blame] | 548 |     LOG(INFO) << "ResetStatus succeeded; to undo partition table changes run:\n" | 
 | 549 |                  "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo ${P#$D} " | 
 | 550 |                  "| sed 's/^[^0-9]*//')-1)) $D;)"; | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 551 |   } | 
| Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 552 |  | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 553 |   // Changes the current update over cellular network setting. | 
 | 554 |   if (!FLAGS_update_over_cellular.empty()) { | 
 | 555 |     gboolean allowed = FLAGS_update_over_cellular == "yes"; | 
 | 556 |     if (!allowed && FLAGS_update_over_cellular != "no") { | 
 | 557 |       LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular | 
 | 558 |                  << "\". Please specify \"yes\" or \"no\"."; | 
 | 559 |     } else { | 
 | 560 |       SetUpdateOverCellularPermission(allowed); | 
 | 561 |     } | 
 | 562 |   } | 
 | 563 |  | 
 | 564 |   // Show the current update over cellular network setting. | 
 | 565 |   if (FLAGS_show_update_over_cellular) { | 
 | 566 |     bool allowed = GetUpdateOverCellularPermission(); | 
 | 567 |     LOG(INFO) << "Current update over cellular network setting: " | 
 | 568 |               << (allowed ? "ENABLED" : "DISABLED"); | 
 | 569 |   } | 
 | 570 |  | 
| Chris Sosa | cb7fa88 | 2013-07-25 17:02:59 -0700 | [diff] [blame] | 571 |   if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) { | 
| Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 572 |     LOG(ERROR) << "powerwash flag only makes sense rollback or channel change"; | 
| Chris Sosa | cb7fa88 | 2013-07-25 17:02:59 -0700 | [diff] [blame] | 573 |     return 1; | 
 | 574 |   } | 
 | 575 |  | 
| Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 576 |   // Change the P2P enabled setting. | 
 | 577 |   if (!FLAGS_p2p_update.empty()) { | 
 | 578 |     gboolean enabled = FLAGS_p2p_update == "yes"; | 
 | 579 |     if (!enabled && FLAGS_p2p_update != "no") { | 
 | 580 |       LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update | 
 | 581 |                  << "\". Please specify \"yes\" or \"no\"."; | 
 | 582 |     } else { | 
 | 583 |       SetP2PUpdatePermission(enabled); | 
 | 584 |     } | 
 | 585 |   } | 
 | 586 |  | 
| Alex Vakulenko | 59e253e | 2014-02-24 10:40:21 -0800 | [diff] [blame] | 587 |   // Show the rollback availability. | 
 | 588 |   if (FLAGS_can_rollback) { | 
| Alex Vakulenko | 2bddadd | 2014-03-27 13:23:46 -0700 | [diff] [blame] | 589 |     std::string rollback_partition = GetRollbackPartition(); | 
| Chris Sosa | f5c0b9c | 2014-04-17 16:12:03 -0700 | [diff] [blame] | 590 |     bool can_rollback = true; | 
 | 591 |     if (rollback_partition.empty()) { | 
| Alex Vakulenko | 2bddadd | 2014-03-27 13:23:46 -0700 | [diff] [blame] | 592 |       rollback_partition = "UNAVAILABLE"; | 
| Chris Sosa | f5c0b9c | 2014-04-17 16:12:03 -0700 | [diff] [blame] | 593 |       can_rollback = false; | 
 | 594 |     } | 
| Alex Vakulenko | 2bddadd | 2014-03-27 13:23:46 -0700 | [diff] [blame] | 595 |     else | 
 | 596 |       rollback_partition = "AVAILABLE: " + rollback_partition; | 
 | 597 |  | 
 | 598 |     LOG(INFO) << "Rollback partition: " << rollback_partition; | 
| Chris Sosa | f5c0b9c | 2014-04-17 16:12:03 -0700 | [diff] [blame] | 599 |     if (!can_rollback) { | 
 | 600 |       return 1; | 
 | 601 |     } | 
| Alex Vakulenko | 59e253e | 2014-02-24 10:40:21 -0800 | [diff] [blame] | 602 |   } | 
 | 603 |  | 
| Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 604 |   // Show the current P2P enabled setting. | 
 | 605 |   if (FLAGS_show_p2p_update) { | 
 | 606 |     bool enabled = GetP2PUpdatePermission(); | 
 | 607 |     LOG(INFO) << "Current update using P2P setting: " | 
 | 608 |               << (enabled ? "ENABLED" : "DISABLED"); | 
 | 609 |   } | 
 | 610 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 611 |   // First, update the target channel if requested. | 
 | 612 |   if (!FLAGS_channel.empty()) | 
| Chris Sosa | cb7fa88 | 2013-07-25 17:02:59 -0700 | [diff] [blame] | 613 |     SetTargetChannel(FLAGS_channel, FLAGS_powerwash); | 
| Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 614 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 615 |   // Show the current and target channels if requested. | 
 | 616 |   if (FLAGS_show_channel) { | 
 | 617 |     string current_channel = GetChannel(true); | 
 | 618 |     LOG(INFO) << "Current Channel: " << current_channel; | 
 | 619 |  | 
 | 620 |     string target_channel = GetChannel(false); | 
 | 621 |     if (!target_channel.empty()) | 
 | 622 |       LOG(INFO) << "Target Channel (pending update): " << target_channel; | 
| Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 623 |   } | 
 | 624 |  | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 625 |   bool do_update_request = FLAGS_check_for_update | FLAGS_update | | 
 | 626 |       !FLAGS_app_version.empty() | !FLAGS_omaha_url.empty(); | 
| Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 627 |   if (FLAGS_update) | 
 | 628 |     FLAGS_follow = true; | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 629 |  | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 630 |   if (do_update_request && FLAGS_rollback) { | 
| Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 631 |     LOG(ERROR) << "Incompatible flags specified with rollback." | 
 | 632 |                << "Rollback should not include update-related flags."; | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 633 |     return 1; | 
 | 634 |   } | 
 | 635 |  | 
 | 636 |   if(FLAGS_rollback) { | 
 | 637 |     LOG(INFO) << "Requesting rollback."; | 
 | 638 |     CHECK(Rollback(FLAGS_powerwash)) << "Request for rollback failed."; | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 639 |   } | 
 | 640 |  | 
| Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 641 |   // Initiate an update check, if necessary. | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 642 |   if (do_update_request) { | 
| Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 643 |     LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored."; | 
| Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 644 |     string app_version = FLAGS_app_version; | 
 | 645 |     if (FLAGS_update && app_version.empty()) { | 
 | 646 |       app_version = "ForcedUpdate"; | 
 | 647 |       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] | 648 |     } | 
| Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 649 |     LOG(INFO) << "Initiating update check and install."; | 
 | 650 |     CHECK(CheckForUpdates(app_version, FLAGS_omaha_url)) | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 651 |         << "Update check/initiate update failed."; | 
| Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 652 |   } | 
| Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 653 |  | 
| Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 654 |   // These final options are all mutually exclusive with one another. | 
| David Zeuthen | 9d73a72 | 2014-04-04 14:52:46 -0700 | [diff] [blame] | 655 |   if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot + | 
 | 656 |       FLAGS_status + FLAGS_is_reboot_needed + | 
 | 657 |       FLAGS_block_until_reboot_is_needed > 1) | 
| Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 658 |   { | 
 | 659 |     LOG(ERROR) << "Multiple exclusive options selected. " | 
 | 660 |                << "Select only one of --follow, --watch_for_updates, --reboot, " | 
| David Zeuthen | 9d73a72 | 2014-04-04 14:52:46 -0700 | [diff] [blame] | 661 |                << "--is_reboot_needed, --block_until_reboot_is_needed, " | 
| Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 662 |                << "or --status."; | 
 | 663 |     return 1; | 
 | 664 |   } | 
 | 665 |  | 
 | 666 |   if (FLAGS_status) { | 
 | 667 |     LOG(INFO) << "Querying Update Engine status..."; | 
 | 668 |     if (!GetStatus(NULL)) { | 
 | 669 |       LOG(ERROR) << "GetStatus failed."; | 
| Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 670 |       return 1; | 
 | 671 |     } | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 672 |     return 0; | 
 | 673 |   } | 
| Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 674 |  | 
| Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 675 |   if (FLAGS_follow) { | 
 | 676 |     LOG(INFO) << "Waiting for update to complete."; | 
 | 677 |     CompleteUpdate();  // Should never return. | 
 | 678 |     return 1; | 
 | 679 |   } | 
 | 680 |  | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 681 |   if (FLAGS_watch_for_updates) { | 
 | 682 |     LOG(INFO) << "Watching for status updates."; | 
 | 683 |     WatchForUpdates();  // Should never return. | 
 | 684 |     return 1; | 
 | 685 |   } | 
| Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 686 |  | 
| Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 687 |   if (FLAGS_reboot) { | 
 | 688 |     LOG(INFO) << "Requesting a reboot..."; | 
 | 689 |     CHECK(RebootIfNeeded()); | 
 | 690 |     return 0; | 
 | 691 |   } | 
| Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 692 |  | 
| Alex Vakulenko | dea2eac | 2014-03-14 15:56:59 -0700 | [diff] [blame] | 693 |   if (FLAGS_prev_version) { | 
 | 694 |     ShowPrevVersion(); | 
 | 695 |   } | 
 | 696 |  | 
| Alex Vakulenko | 2bddadd | 2014-03-27 13:23:46 -0700 | [diff] [blame] | 697 |   if (FLAGS_show_kernels) { | 
 | 698 |     LOG(INFO) << "Kernel partitions:\n" | 
 | 699 |               << GetKernelDevices(); | 
 | 700 |   } | 
 | 701 |  | 
| David Zeuthen | 9d73a72 | 2014-04-04 14:52:46 -0700 | [diff] [blame] | 702 |   if (FLAGS_is_reboot_needed) { | 
 | 703 |     bool reboot_needed = false; | 
 | 704 |     if (!IsRebootNeeded(&reboot_needed)) | 
 | 705 |       return 1; | 
 | 706 |     else if (!reboot_needed) | 
 | 707 |       return 2; | 
 | 708 |   } | 
 | 709 |  | 
 | 710 |   if (FLAGS_block_until_reboot_is_needed) { | 
 | 711 |     if (!BlockUntilRebootIsNeeded()) | 
 | 712 |       return 1; | 
 | 713 |   } | 
 | 714 |  | 
| Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 715 |   LOG(INFO) << "Done."; | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 716 |   return 0; | 
 | 717 | } |