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