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 | |
Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 113 | gboolean rc = update_engine_client_reset_status(proxy, &error); |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 114 | return rc; |
| 115 | } |
| 116 | |
| 117 | |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 118 | // If |op| is non-NULL, sets it to the current operation string or an |
| 119 | // empty string if unable to obtain the current status. |
| 120 | bool GetStatus(string* op) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 121 | DBusGProxy* proxy; |
| 122 | GError* error = NULL; |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 123 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 124 | CHECK(GetProxy(&proxy)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 125 | |
| 126 | gint64 last_checked_time = 0; |
| 127 | gdouble progress = 0.0; |
| 128 | char* current_op = NULL; |
| 129 | char* new_version = NULL; |
| 130 | gint64 new_size = 0; |
| 131 | |
Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 132 | gboolean rc = update_engine_client_get_status(proxy, |
| 133 | &last_checked_time, |
| 134 | &progress, |
| 135 | ¤t_op, |
| 136 | &new_version, |
| 137 | &new_size, |
| 138 | &error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 139 | if (rc == FALSE) { |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 140 | LOG(INFO) << "Error getting status: " << GetAndFreeGError(&error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 141 | } |
| 142 | printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n" |
| 143 | "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n", |
| 144 | last_checked_time, |
| 145 | progress, |
| 146 | current_op, |
| 147 | new_version, |
| 148 | new_size); |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 149 | if (op) { |
| 150 | *op = current_op ? current_op : ""; |
| 151 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 152 | return true; |
| 153 | } |
| 154 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 155 | // Should never return. |
| 156 | void WatchForUpdates() { |
| 157 | DBusGProxy* proxy; |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 158 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 159 | CHECK(GetProxy(&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 | // Register marshaller |
| 162 | dbus_g_object_register_marshaller( |
| 163 | update_engine_VOID__INT64_DOUBLE_STRING_STRING_INT64, |
| 164 | G_TYPE_NONE, |
| 165 | G_TYPE_INT64, |
| 166 | G_TYPE_DOUBLE, |
| 167 | G_TYPE_STRING, |
| 168 | G_TYPE_STRING, |
| 169 | G_TYPE_INT64, |
| 170 | G_TYPE_INVALID); |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 171 | |
| 172 | static const char kStatusUpdate[] = "StatusUpdate"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 173 | dbus_g_proxy_add_signal(proxy, |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 174 | kStatusUpdate, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 175 | G_TYPE_INT64, |
| 176 | G_TYPE_DOUBLE, |
| 177 | G_TYPE_STRING, |
| 178 | G_TYPE_STRING, |
| 179 | G_TYPE_INT64, |
| 180 | G_TYPE_INVALID); |
| 181 | GMainLoop* loop = g_main_loop_new (NULL, TRUE); |
| 182 | dbus_g_proxy_connect_signal(proxy, |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 183 | kStatusUpdate, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 184 | G_CALLBACK(StatusUpdateSignalHandler), |
| 185 | NULL, |
| 186 | NULL); |
| 187 | g_main_loop_run(loop); |
| 188 | g_main_loop_unref(loop); |
| 189 | } |
| 190 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 191 | bool Rollback(bool rollback) { |
| 192 | DBusGProxy* proxy; |
| 193 | GError* error = NULL; |
| 194 | |
| 195 | CHECK(GetProxy(&proxy)); |
| 196 | |
Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 197 | gboolean rc = update_engine_client_attempt_rollback(proxy, |
| 198 | rollback, |
| 199 | &error); |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 200 | CHECK_EQ(rc, TRUE) << "Error with rollback request: " |
| 201 | << GetAndFreeGError(&error); |
| 202 | return true; |
| 203 | } |
| 204 | |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 205 | bool CheckForUpdates(const string& app_version, const string& omaha_url) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 206 | DBusGProxy* proxy; |
| 207 | GError* error = NULL; |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 208 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 209 | CHECK(GetProxy(&proxy)); |
| 210 | |
Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 211 | gboolean rc = update_engine_client_attempt_update(proxy, |
| 212 | app_version.c_str(), |
| 213 | omaha_url.c_str(), |
| 214 | &error); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 215 | CHECK_EQ(rc, TRUE) << "Error checking for update: " |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 216 | << GetAndFreeGError(&error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 220 | bool RebootIfNeeded() { |
| 221 | DBusGProxy* proxy; |
| 222 | GError* error = NULL; |
| 223 | |
| 224 | CHECK(GetProxy(&proxy)); |
| 225 | |
| 226 | gboolean rc = |
Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 227 | update_engine_client_reboot_if_needed(proxy, &error); |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 228 | // Reboot error code doesn't necessarily mean that a reboot |
| 229 | // failed. For example, D-Bus may be shutdown before we receive the |
| 230 | // result. |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 231 | LOG_IF(INFO, !rc) << "Reboot error message: " << GetAndFreeGError(&error); |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 232 | return true; |
| 233 | } |
| 234 | |
Chris Sosa | cb7fa88 | 2013-07-25 17:02:59 -0700 | [diff] [blame] | 235 | void SetTargetChannel(const string& target_channel, bool allow_powerwash) { |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 236 | DBusGProxy* proxy; |
| 237 | GError* error = NULL; |
| 238 | |
| 239 | CHECK(GetProxy(&proxy)); |
| 240 | |
Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 241 | gboolean rc = update_engine_client_set_channel(proxy, |
| 242 | target_channel.c_str(), |
| 243 | allow_powerwash, |
| 244 | &error); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 245 | CHECK_EQ(rc, true) << "Error setting the channel: " |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 246 | << GetAndFreeGError(&error); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 247 | LOG(INFO) << "Channel permanently set to: " << target_channel; |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 250 | string GetChannel(bool get_current_channel) { |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 251 | DBusGProxy* proxy; |
| 252 | GError* error = NULL; |
| 253 | |
| 254 | CHECK(GetProxy(&proxy)); |
| 255 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 256 | char* channel = NULL; |
Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 257 | gboolean rc = update_engine_client_get_channel(proxy, |
| 258 | get_current_channel, |
| 259 | &channel, |
| 260 | &error); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 261 | CHECK_EQ(rc, true) << "Error getting the channel: " |
| 262 | << GetAndFreeGError(&error); |
| 263 | string output = channel; |
| 264 | g_free(channel); |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 265 | return output; |
| 266 | } |
| 267 | |
Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 268 | void SetUpdateOverCellularPermission(gboolean allowed) { |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 269 | DBusGProxy* proxy; |
| 270 | GError* error = NULL; |
| 271 | |
| 272 | CHECK(GetProxy(&proxy)); |
| 273 | |
Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 274 | gboolean rc = update_engine_client_set_update_over_cellular_permission( |
| 275 | proxy, |
| 276 | allowed, |
| 277 | &error); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 278 | CHECK_EQ(rc, true) << "Error setting the update over cellular setting: " |
| 279 | << GetAndFreeGError(&error); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | bool GetUpdateOverCellularPermission() { |
| 283 | DBusGProxy* proxy; |
| 284 | GError* error = NULL; |
| 285 | |
| 286 | CHECK(GetProxy(&proxy)); |
| 287 | |
| 288 | gboolean allowed; |
Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 289 | gboolean rc = update_engine_client_get_update_over_cellular_permission( |
| 290 | proxy, |
| 291 | &allowed, |
| 292 | &error); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 293 | CHECK_EQ(rc, true) << "Error getting the update over cellular setting: " |
| 294 | << GetAndFreeGError(&error); |
| 295 | return allowed; |
| 296 | } |
| 297 | |
Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 298 | void SetP2PUpdatePermission(gboolean enabled) { |
| 299 | DBusGProxy* proxy; |
| 300 | GError* error = NULL; |
| 301 | |
| 302 | CHECK(GetProxy(&proxy)); |
| 303 | |
Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 304 | gboolean rc = update_engine_client_set_p2p_update_permission( |
| 305 | proxy, |
| 306 | enabled, |
| 307 | &error); |
Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 308 | CHECK_EQ(rc, true) << "Error setting the peer-to-peer update setting: " |
| 309 | << GetAndFreeGError(&error); |
| 310 | } |
| 311 | |
| 312 | bool GetP2PUpdatePermission() { |
| 313 | DBusGProxy* proxy; |
| 314 | GError* error = NULL; |
| 315 | |
| 316 | CHECK(GetProxy(&proxy)); |
| 317 | |
| 318 | gboolean enabled; |
Alex Deymo | 36dc2f3 | 2013-08-29 15:45:06 -0700 | [diff] [blame] | 319 | gboolean rc = update_engine_client_get_p2p_update_permission( |
| 320 | proxy, |
| 321 | &enabled, |
| 322 | &error); |
Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 323 | CHECK_EQ(rc, true) << "Error getting the peer-to-peer update setting: " |
| 324 | << GetAndFreeGError(&error); |
| 325 | return enabled; |
| 326 | } |
| 327 | |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 328 | static gboolean CompleteUpdateSource(gpointer data) { |
| 329 | string current_op; |
| 330 | if (!GetStatus(¤t_op) || current_op == "UPDATE_STATUS_IDLE") { |
| 331 | LOG(ERROR) << "Update failed."; |
| 332 | exit(1); |
| 333 | } |
| 334 | if (current_op == "UPDATE_STATUS_UPDATED_NEED_REBOOT") { |
| 335 | LOG(INFO) << "Update succeeded -- reboot needed."; |
| 336 | exit(0); |
| 337 | } |
| 338 | return TRUE; |
| 339 | } |
| 340 | |
| 341 | // This is similar to watching for updates but rather than registering |
| 342 | // a signal watch, activelly poll the daemon just in case it stops |
| 343 | // sending notifications. |
| 344 | void CompleteUpdate() { |
| 345 | GMainLoop* loop = g_main_loop_new (NULL, TRUE); |
| 346 | g_timeout_add_seconds(5, CompleteUpdateSource, NULL); |
| 347 | g_main_loop_run(loop); |
| 348 | g_main_loop_unref(loop); |
| 349 | } |
| 350 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 351 | } // namespace {} |
| 352 | |
| 353 | int main(int argc, char** argv) { |
| 354 | // Boilerplate init commands. |
| 355 | g_type_init(); |
Ben Chan | 46bf5c8 | 2013-06-24 11:17:41 -0700 | [diff] [blame] | 356 | dbus_threads_init_default(); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 357 | chromeos_update_engine::Subprocess::Init(); |
| 358 | google::ParseCommandLineFlags(&argc, &argv, true); |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 359 | |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 360 | // Update the status if requested. |
| 361 | if (FLAGS_reset_status) { |
| 362 | LOG(INFO) << "Setting Update Engine status to idle ..."; |
| 363 | if (!ResetStatus()) { |
| 364 | LOG(ERROR) << "ResetStatus failed."; |
| 365 | return 1; |
| 366 | } |
| 367 | |
Gilad Arnold | 50c6063 | 2013-01-25 10:27:19 -0800 | [diff] [blame] | 368 | LOG(INFO) << "ResetStatus succeeded; to undo partition table changes run:\n" |
| 369 | "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo ${P#$D} " |
| 370 | "| sed 's/^[^0-9]*//')-1)) $D;)"; |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 375 | if (FLAGS_status) { |
| 376 | LOG(INFO) << "Querying Update Engine status..."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 377 | if (!GetStatus(NULL)) { |
| 378 | LOG(FATAL) << "GetStatus failed."; |
| 379 | return 1; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 380 | } |
| 381 | return 0; |
| 382 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 383 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 384 | // Changes the current update over cellular network setting. |
| 385 | if (!FLAGS_update_over_cellular.empty()) { |
| 386 | gboolean allowed = FLAGS_update_over_cellular == "yes"; |
| 387 | if (!allowed && FLAGS_update_over_cellular != "no") { |
| 388 | LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular |
| 389 | << "\". Please specify \"yes\" or \"no\"."; |
| 390 | } else { |
| 391 | SetUpdateOverCellularPermission(allowed); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | // Show the current update over cellular network setting. |
| 396 | if (FLAGS_show_update_over_cellular) { |
| 397 | bool allowed = GetUpdateOverCellularPermission(); |
| 398 | LOG(INFO) << "Current update over cellular network setting: " |
| 399 | << (allowed ? "ENABLED" : "DISABLED"); |
| 400 | } |
| 401 | |
Chris Sosa | cb7fa88 | 2013-07-25 17:02:59 -0700 | [diff] [blame] | 402 | if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) { |
| 403 | LOG(FATAL) << "powerwash flag only makes sense rollback or channel change"; |
| 404 | return 1; |
| 405 | } |
| 406 | |
Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 407 | // Change the P2P enabled setting. |
| 408 | if (!FLAGS_p2p_update.empty()) { |
| 409 | gboolean enabled = FLAGS_p2p_update == "yes"; |
| 410 | if (!enabled && FLAGS_p2p_update != "no") { |
| 411 | LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update |
| 412 | << "\". Please specify \"yes\" or \"no\"."; |
| 413 | } else { |
| 414 | SetP2PUpdatePermission(enabled); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | // Show the current P2P enabled setting. |
| 419 | if (FLAGS_show_p2p_update) { |
| 420 | bool enabled = GetP2PUpdatePermission(); |
| 421 | LOG(INFO) << "Current update using P2P setting: " |
| 422 | << (enabled ? "ENABLED" : "DISABLED"); |
| 423 | } |
| 424 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 425 | // First, update the target channel if requested. |
| 426 | if (!FLAGS_channel.empty()) |
Chris Sosa | cb7fa88 | 2013-07-25 17:02:59 -0700 | [diff] [blame] | 427 | SetTargetChannel(FLAGS_channel, FLAGS_powerwash); |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 428 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 429 | // Show the current and target channels if requested. |
| 430 | if (FLAGS_show_channel) { |
| 431 | string current_channel = GetChannel(true); |
| 432 | LOG(INFO) << "Current Channel: " << current_channel; |
| 433 | |
| 434 | string target_channel = GetChannel(false); |
| 435 | if (!target_channel.empty()) |
| 436 | LOG(INFO) << "Target Channel (pending update): " << target_channel; |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 437 | } |
| 438 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 439 | bool do_update_request = FLAGS_check_for_update | FLAGS_update | |
| 440 | !FLAGS_app_version.empty() | !FLAGS_omaha_url.empty(); |
| 441 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 442 | if (do_update_request && FLAGS_rollback) { |
| 443 | LOG(FATAL) << "Update should not be requested with rollback!"; |
| 444 | return 1; |
| 445 | } |
| 446 | |
| 447 | if(FLAGS_rollback) { |
| 448 | LOG(INFO) << "Requesting rollback."; |
| 449 | CHECK(Rollback(FLAGS_powerwash)) << "Request for rollback failed."; |
| 450 | return 0; |
| 451 | } |
| 452 | |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 453 | // Initiate an update check, if necessary. |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 454 | if (do_update_request) { |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 455 | LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 456 | string app_version = FLAGS_app_version; |
| 457 | if (FLAGS_update && app_version.empty()) { |
| 458 | app_version = "ForcedUpdate"; |
| 459 | 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] | 460 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 461 | LOG(INFO) << "Initiating update check and install."; |
| 462 | CHECK(CheckForUpdates(app_version, FLAGS_omaha_url)) |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 463 | << "Update check/initiate update failed."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 464 | |
| 465 | // Wait for an update to complete. |
| 466 | if (FLAGS_update) { |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 467 | LOG(INFO) << "Waiting for update to complete."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 468 | CompleteUpdate(); // Should never return. |
| 469 | return 1; |
| 470 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 471 | return 0; |
| 472 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 473 | |
| 474 | // Start watching for updates. |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 475 | if (FLAGS_watch_for_updates) { |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 476 | LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored."; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 477 | LOG(INFO) << "Watching for status updates."; |
| 478 | WatchForUpdates(); // Should never return. |
| 479 | return 1; |
| 480 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 481 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 482 | if (FLAGS_reboot) { |
| 483 | LOG(INFO) << "Requesting a reboot..."; |
| 484 | CHECK(RebootIfNeeded()); |
| 485 | return 0; |
| 486 | } |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 487 | |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 488 | LOG(INFO) << "Done."; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 489 | return 0; |
| 490 | } |