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