Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 17 | #include <inttypes.h> |
| 18 | #include <sysexits.h> |
| 19 | #include <unistd.h> |
| 20 | |
Casey Dahlin | e844c1a | 2015-12-16 14:30:58 -0800 | [diff] [blame] | 21 | #include <memory> |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 22 | #include <string> |
| 23 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 24 | #include <base/bind.h> |
Alex Deymo | 8ce80d6 | 2015-01-27 15:10:43 -0800 | [diff] [blame] | 25 | #include <base/command_line.h> |
Alex Deymo | 44666f9 | 2014-07-22 20:29:24 -0700 | [diff] [blame] | 26 | #include <base/logging.h> |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 27 | #include <base/macros.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 28 | #include <brillo/daemons/dbus_daemon.h> |
| 29 | #include <brillo/flag_helper.h> |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 30 | #include <dbus/bus.h> |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 31 | |
| 32 | #include "update_engine/client.h" |
| 33 | #include "update_engine/update_status.h" |
| 34 | #include "update_engine/dbus-constants.h" |
| 35 | #include "update_engine/dbus-proxies.h" |
| 36 | #include "update_status_utils.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 37 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 38 | using std::string; |
Casey Dahlin | e844c1a | 2015-12-16 14:30:58 -0800 | [diff] [blame] | 39 | using std::unique_ptr; |
Alex Deymo | d6deb1d | 2015-08-28 15:54:37 -0700 | [diff] [blame] | 40 | using update_engine::kAttemptUpdateFlagNonInteractive; |
| 41 | using update_engine::kUpdateEngineServiceName; |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 42 | using chromeos_update_engine::UpdateStatusToString; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 43 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 44 | namespace { |
| 45 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 46 | // Constant to signal that we need to continue running the daemon after |
| 47 | // initialization. |
| 48 | const int kContinueRunning = -1; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 49 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 50 | class UpdateEngineClient : public brillo::DBusDaemon { |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 51 | public: |
Casey Dahlin | e844c1a | 2015-12-16 14:30:58 -0800 | [diff] [blame] | 52 | UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) { |
| 53 | client_ = update_engine::UpdateEngineClient::CreateInstance(); |
| 54 | } |
| 55 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 56 | ~UpdateEngineClient() override = default; |
| 57 | |
| 58 | protected: |
| 59 | int OnInit() override { |
| 60 | int ret = DBusDaemon::OnInit(); |
| 61 | if (ret != EX_OK) |
| 62 | return ret; |
| 63 | if (!InitProxy()) |
| 64 | return 1; |
Alex Deymo | 13e0dd6 | 2015-06-30 23:11:44 -0700 | [diff] [blame] | 65 | // Wait for the UpdateEngine to be available or timeout. |
| 66 | proxy_->GetObjectProxy()->WaitForServiceToBeAvailable( |
| 67 | base::Bind(&UpdateEngineClient::OnServiceAvailable, |
| 68 | base::Unretained(this))); |
| 69 | base::MessageLoop::current()->PostDelayedTask( |
| 70 | FROM_HERE, |
| 71 | base::Bind(&UpdateEngineClient::OnServiceAvailableTimeout, |
| 72 | base::Unretained(this)), |
| 73 | base::TimeDelta::FromSeconds(10)); |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 74 | return EX_OK; |
Andrew de los Reyes | 68ab6ed | 2011-08-09 14:46:39 -0700 | [diff] [blame] | 75 | } |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 76 | |
| 77 | private: |
| 78 | bool InitProxy(); |
| 79 | |
Alex Deymo | 13e0dd6 | 2015-06-30 23:11:44 -0700 | [diff] [blame] | 80 | // Callback called when the UpdateEngine service becomes available. |
| 81 | void OnServiceAvailable(bool service_is_available); |
| 82 | |
| 83 | // Callback called when the UpdateEngine service doesn't become available |
| 84 | // after a timeout. |
| 85 | void OnServiceAvailableTimeout(); |
| 86 | |
| 87 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 88 | // Callback called when a StatusUpdate signal is received. |
| 89 | void OnStatusUpdateSignal(int64_t last_checked_time, |
| 90 | double progress, |
| 91 | const string& current_operation, |
| 92 | const string& new_version, |
| 93 | int64_t new_size); |
| 94 | // Callback called when the OnStatusUpdateSignal() handler is registered. |
| 95 | void OnStatusUpdateSignalRegistration(const string& interface, |
| 96 | const string& signal_name, |
| 97 | bool success); |
| 98 | |
| 99 | // Registers a callback that prints on stderr the received StatusUpdate |
| 100 | // signals. |
| 101 | // The daemon should continue running for this to work. |
| 102 | void WatchForUpdates(); |
| 103 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 104 | // Show the status of the update engine in stdout. |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 105 | bool ShowStatus(); |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 106 | |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 107 | // Return whether we need to reboot. 0 if reboot is needed, 1 if an error |
| 108 | // occurred, 2 if no reboot is needed. |
| 109 | int GetNeedReboot(); |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 110 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 111 | // This is similar to watching for updates but rather than registering |
| 112 | // a signal watch, actively poll the daemon just in case it stops |
| 113 | // sending notifications. |
| 114 | void WaitForUpdateComplete(); |
| 115 | void OnUpdateCompleteCheck(int64_t last_checked_time, |
| 116 | double progress, |
| 117 | const string& current_operation, |
| 118 | const string& new_version, |
| 119 | int64_t new_size); |
| 120 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 121 | // Blocks until a reboot is needed. If the reboot is needed, exits the program |
| 122 | // with 0. Otherwise it exits the program with 1 if an error occurs before |
| 123 | // the reboot is needed. |
| 124 | void WaitForRebootNeeded(); |
| 125 | void OnRebootNeededCheck(int64_t last_checked_time, |
| 126 | double progress, |
| 127 | const string& current_operation, |
| 128 | const string& new_version, |
| 129 | int64_t new_size); |
| 130 | // Callback called when the OnRebootNeededCheck() handler is registered. This |
| 131 | // is useful to check at this point if the reboot is needed, without loosing |
| 132 | // any StatusUpdate signals and avoiding the race condition. |
| 133 | void OnRebootNeededCheckRegistration(const string& interface, |
| 134 | const string& signal_name, |
| 135 | bool success); |
| 136 | |
| 137 | // Main method that parses and triggers all the actions based on the passed |
| 138 | // flags. |
| 139 | int ProcessFlags(); |
| 140 | |
| 141 | // DBus Proxy to the update_engine daemon object used for all the calls. |
| 142 | std::unique_ptr<org::chromium::UpdateEngineInterfaceProxy> proxy_; |
| 143 | |
| 144 | // Copy of argc and argv passed to main(). |
| 145 | int argc_; |
| 146 | char** argv_; |
| 147 | |
Casey Dahlin | e844c1a | 2015-12-16 14:30:58 -0800 | [diff] [blame] | 148 | // Library-based client |
| 149 | unique_ptr<update_engine::UpdateEngineClient> client_; |
| 150 | |
Alex Deymo | 13e0dd6 | 2015-06-30 23:11:44 -0700 | [diff] [blame] | 151 | // Tell whether the UpdateEngine service is available after startup. |
| 152 | bool service_is_available_{false}; |
| 153 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 154 | DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient); |
| 155 | }; |
| 156 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 157 | bool UpdateEngineClient::InitProxy() { |
| 158 | proxy_.reset(new org::chromium::UpdateEngineInterfaceProxy(bus_)); |
| 159 | |
| 160 | if (!proxy_->GetObjectProxy()) { |
| 161 | LOG(ERROR) << "Error getting dbus proxy for " << kUpdateEngineServiceName; |
| 162 | return false; |
Richard Barnette | d793606 | 2013-01-18 13:38:51 -0800 | [diff] [blame] | 163 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
Alex Deymo | 13e0dd6 | 2015-06-30 23:11:44 -0700 | [diff] [blame] | 167 | void UpdateEngineClient::OnServiceAvailable(bool service_is_available) { |
| 168 | service_is_available_ = service_is_available; |
| 169 | if (!service_is_available) { |
| 170 | LOG(ERROR) << "UpdateEngineService not available."; |
| 171 | QuitWithExitCode(-1); |
| 172 | } |
| 173 | int ret = ProcessFlags(); |
| 174 | if (ret != kContinueRunning) |
| 175 | QuitWithExitCode(ret); |
| 176 | } |
| 177 | |
| 178 | void UpdateEngineClient::OnServiceAvailableTimeout() { |
| 179 | if (!service_is_available_) { |
| 180 | LOG(ERROR) << "Waiting for UpdateEngineService timeout. Is update_engine " |
| 181 | "daemon running?"; |
| 182 | QuitWithExitCode(-1); |
| 183 | } |
| 184 | } |
| 185 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 186 | void UpdateEngineClient::OnStatusUpdateSignal( |
| 187 | int64_t last_checked_time, |
| 188 | double progress, |
| 189 | const string& current_operation, |
| 190 | const string& new_version, |
| 191 | int64_t new_size) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 192 | LOG(INFO) << "Got status update:"; |
| 193 | LOG(INFO) << " last_checked_time: " << last_checked_time; |
| 194 | LOG(INFO) << " progress: " << progress; |
| 195 | LOG(INFO) << " current_operation: " << current_operation; |
| 196 | LOG(INFO) << " new_version: " << new_version; |
| 197 | LOG(INFO) << " new_size: " << new_size; |
| 198 | } |
| 199 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 200 | void UpdateEngineClient::OnStatusUpdateSignalRegistration( |
| 201 | const string& interface, |
| 202 | const string& signal_name, |
| 203 | bool success) { |
| 204 | VLOG(1) << "OnStatusUpdateSignalRegistration(" << interface << ", " |
| 205 | << signal_name << ", " << success << ");"; |
| 206 | if (!success) { |
| 207 | LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal."; |
| 208 | exit(1); |
| 209 | } |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 212 | void UpdateEngineClient::WatchForUpdates() { |
| 213 | proxy_->RegisterStatusUpdateSignalHandler( |
| 214 | base::Bind(&UpdateEngineClient::OnStatusUpdateSignal, |
| 215 | base::Unretained(this)), |
| 216 | base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration, |
| 217 | base::Unretained(this))); |
| 218 | } |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 219 | |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 220 | bool UpdateEngineClient::ShowStatus() { |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 221 | int64_t last_checked_time = 0; |
| 222 | double progress = 0.0; |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 223 | update_engine::UpdateStatus current_op; |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 224 | string new_version; |
| 225 | int64_t new_size = 0; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 226 | |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 227 | if (!client_->GetStatus( |
| 228 | &last_checked_time, &progress, ¤t_op, &new_version, &new_size)) { |
| 229 | return false; |
| 230 | } |
| 231 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 232 | printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n" |
| 233 | "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n", |
| 234 | last_checked_time, |
| 235 | progress, |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 236 | UpdateStatusToString(current_op), |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 237 | new_version.c_str(), |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 238 | new_size); |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 239 | |
| 240 | return true; |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 243 | int UpdateEngineClient::GetNeedReboot() { |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 244 | int64_t last_checked_time = 0; |
| 245 | double progress = 0.0; |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 246 | update_engine::UpdateStatus current_op; |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 247 | string new_version; |
| 248 | int64_t new_size = 0; |
| 249 | |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 250 | if (!client_->GetStatus( |
| 251 | &last_checked_time, &progress, ¤t_op, &new_version, &new_size)) { |
| 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | if (current_op == update_engine::UpdateStatus::UPDATED_NEED_REBOOT) { |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | return 2; |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 262 | void UpdateEngineClient::OnUpdateCompleteCheck( |
| 263 | int64_t /* last_checked_time */, |
| 264 | double /* progress */, |
| 265 | const string& current_operation, |
| 266 | const string& /* new_version */, |
| 267 | int64_t /* new_size */) { |
| 268 | if (current_operation == update_engine::kUpdateStatusIdle) { |
| 269 | LOG(ERROR) << "Update failed, current operations is " << current_operation; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 270 | exit(1); |
| 271 | } |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 272 | if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) { |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 273 | LOG(INFO) << "Update succeeded -- reboot needed."; |
| 274 | exit(0); |
| 275 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 278 | void UpdateEngineClient::WaitForUpdateComplete() { |
| 279 | proxy_->RegisterStatusUpdateSignalHandler( |
| 280 | base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck, |
| 281 | base::Unretained(this)), |
| 282 | base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration, |
| 283 | base::Unretained(this))); |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 286 | void UpdateEngineClient::OnRebootNeededCheck( |
| 287 | int64_t /* last_checked_time */, |
| 288 | double /* progress */, |
| 289 | const string& current_operation, |
| 290 | const string& /* new_version */, |
| 291 | int64_t /* new_size */) { |
| 292 | if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) { |
| 293 | LOG(INFO) << "Reboot needed."; |
| 294 | exit(0); |
David Zeuthen | 9d73a72 | 2014-04-04 14:52:46 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 298 | void UpdateEngineClient::OnRebootNeededCheckRegistration( |
| 299 | const string& interface, |
| 300 | const string& signal_name, |
| 301 | bool success) { |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 302 | int got = GetNeedReboot(); |
| 303 | |
| 304 | if (got == 1) { |
| 305 | LOG(ERROR) << "Could not query the current operation."; |
| 306 | exit(1); |
| 307 | } |
| 308 | |
| 309 | if (got == 0) { |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 310 | exit(0); |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 313 | if (!success) { |
| 314 | LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal."; |
| 315 | exit(1); |
| 316 | } |
David Zeuthen | 9d73a72 | 2014-04-04 14:52:46 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // Blocks until a reboot is needed. Returns true if waiting succeeded, |
| 320 | // false if an error occurred. |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 321 | void UpdateEngineClient::WaitForRebootNeeded() { |
| 322 | proxy_->RegisterStatusUpdateSignalHandler( |
| 323 | base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck, |
| 324 | base::Unretained(this)), |
| 325 | base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration, |
| 326 | base::Unretained(this))); |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 327 | int got = GetNeedReboot(); |
| 328 | |
| 329 | if (got == 1) { |
| 330 | LOG(ERROR) << "Could not query the current operation."; |
| 331 | exit(1); |
| 332 | } |
| 333 | |
| 334 | if (got == 0) |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 335 | exit(0); |
David Zeuthen | 9d73a72 | 2014-04-04 14:52:46 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 338 | int UpdateEngineClient::ProcessFlags() { |
Steve Fung | 97b6f5a | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 339 | DEFINE_string(app_version, "", "Force the current app version."); |
| 340 | DEFINE_string(channel, "", |
| 341 | "Set the target channel. The device will be powerwashed if the " |
| 342 | "target channel is more stable than the current channel unless " |
| 343 | "--nopowerwash is specified."); |
| 344 | DEFINE_bool(check_for_update, false, "Initiate check for updates."); |
| 345 | DEFINE_bool(follow, false, "Wait for any update operations to complete." |
| 346 | "Exit status is 0 if the update succeeded, and 1 otherwise."); |
| 347 | DEFINE_bool(interactive, true, "Mark the update request as interactive."); |
| 348 | DEFINE_string(omaha_url, "", "The URL of the Omaha update server."); |
| 349 | DEFINE_string(p2p_update, "", |
| 350 | "Enables (\"yes\") or disables (\"no\") the peer-to-peer update" |
| 351 | " sharing."); |
| 352 | DEFINE_bool(powerwash, true, "When performing rollback or channel change, " |
| 353 | "do a powerwash or allow it respectively."); |
| 354 | DEFINE_bool(reboot, false, "Initiate a reboot if needed."); |
| 355 | DEFINE_bool(is_reboot_needed, false, "Exit status 0 if reboot is needed, " |
| 356 | "2 if reboot is not needed or 1 if an error occurred."); |
| 357 | DEFINE_bool(block_until_reboot_is_needed, false, "Blocks until reboot is " |
| 358 | "needed. Returns non-zero exit status if an error occurred."); |
| 359 | DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle."); |
Alex Deymo | 1ac8b59 | 2015-01-26 13:22:58 -0800 | [diff] [blame] | 360 | DEFINE_bool(rollback, false, |
| 361 | "Perform a rollback to the previous partition. The device will " |
| 362 | "be powerwashed unless --nopowerwash is specified."); |
Steve Fung | 97b6f5a | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 363 | DEFINE_bool(can_rollback, false, "Shows whether rollback partition " |
| 364 | "is available."); |
| 365 | DEFINE_bool(show_channel, false, "Show the current and target channels."); |
| 366 | DEFINE_bool(show_p2p_update, false, |
| 367 | "Show the current setting for peer-to-peer update sharing."); |
| 368 | DEFINE_bool(show_update_over_cellular, false, |
| 369 | "Show the current setting for updates over cellular networks."); |
| 370 | DEFINE_bool(status, false, "Print the status to stdout."); |
| 371 | DEFINE_bool(update, false, "Forces an update and waits for it to complete. " |
| 372 | "Implies --follow."); |
| 373 | DEFINE_string(update_over_cellular, "", |
| 374 | "Enables (\"yes\") or disables (\"no\") the updates over " |
| 375 | "cellular networks."); |
| 376 | DEFINE_bool(watch_for_updates, false, |
| 377 | "Listen for status updates and print them to the screen."); |
| 378 | DEFINE_bool(prev_version, false, |
| 379 | "Show the previous OS version used before the update reboot."); |
Steve Fung | 97b6f5a | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 380 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 381 | // Boilerplate init commands. |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 382 | base::CommandLine::Init(argc_, argv_); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 383 | brillo::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client"); |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 384 | |
Alex Deymo | 8ce80d6 | 2015-01-27 15:10:43 -0800 | [diff] [blame] | 385 | // Ensure there are no positional arguments. |
| 386 | const std::vector<string> positional_args = |
| 387 | base::CommandLine::ForCurrentProcess()->GetArgs(); |
| 388 | if (!positional_args.empty()) { |
| 389 | LOG(ERROR) << "Found a positional argument '" << positional_args.front() |
| 390 | << "'. If you want to pass a value to a flag, pass it as " |
| 391 | "--flag=value."; |
| 392 | return 1; |
| 393 | } |
| 394 | |
Jay Srinivasan | c1ba09a | 2012-08-14 14:15:57 -0700 | [diff] [blame] | 395 | // Update the status if requested. |
| 396 | if (FLAGS_reset_status) { |
| 397 | LOG(INFO) << "Setting Update Engine status to idle ..."; |
Casey Dahlin | e844c1a | 2015-12-16 14:30:58 -0800 | [diff] [blame] | 398 | |
| 399 | if (client_->ResetStatus()) { |
| 400 | LOG(INFO) << "ResetStatus succeeded; to undo partition table changes " |
| 401 | "run:\n" |
| 402 | "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo " |
| 403 | "${P#$D} | sed 's/^[^0-9]*//')-1)) $D;)"; |
| 404 | } else { |
| 405 | LOG(ERROR) << "ResetStatus failed"; |
| 406 | return 1; |
| 407 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 408 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 409 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 410 | // Changes the current update over cellular network setting. |
| 411 | if (!FLAGS_update_over_cellular.empty()) { |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 412 | bool allowed = FLAGS_update_over_cellular == "yes"; |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 413 | if (!allowed && FLAGS_update_over_cellular != "no") { |
| 414 | LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular |
| 415 | << "\". Please specify \"yes\" or \"no\"."; |
| 416 | } else { |
Casey Dahlin | ef36113 | 2015-12-17 13:02:37 -0800 | [diff] [blame] | 417 | if(!client_->SetUpdateOverCellularPermission(allowed)) { |
| 418 | LOG(ERROR) << "Error setting the update over cellular setting."; |
| 419 | return 1; |
| 420 | } |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | // Show the current update over cellular network setting. |
| 425 | if (FLAGS_show_update_over_cellular) { |
Casey Dahlin | ef36113 | 2015-12-17 13:02:37 -0800 | [diff] [blame] | 426 | bool allowed; |
| 427 | |
| 428 | if (!client_->GetUpdateOverCellularPermission(&allowed)) { |
| 429 | LOG(ERROR) << "Error getting the update over cellular setting."; |
| 430 | return 1; |
| 431 | } |
| 432 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 433 | LOG(INFO) << "Current update over cellular network setting: " |
| 434 | << (allowed ? "ENABLED" : "DISABLED"); |
| 435 | } |
| 436 | |
Chris Sosa | cb7fa88 | 2013-07-25 17:02:59 -0700 | [diff] [blame] | 437 | if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) { |
Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 438 | LOG(ERROR) << "powerwash flag only makes sense rollback or channel change"; |
Chris Sosa | cb7fa88 | 2013-07-25 17:02:59 -0700 | [diff] [blame] | 439 | return 1; |
| 440 | } |
| 441 | |
Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 442 | // Change the P2P enabled setting. |
| 443 | if (!FLAGS_p2p_update.empty()) { |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 444 | bool enabled = FLAGS_p2p_update == "yes"; |
Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 445 | if (!enabled && FLAGS_p2p_update != "no") { |
| 446 | LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update |
| 447 | << "\". Please specify \"yes\" or \"no\"."; |
| 448 | } else { |
Casey Dahlin | ef36113 | 2015-12-17 13:02:37 -0800 | [diff] [blame] | 449 | if (!client_->SetP2PUpdatePermission(enabled)) { |
| 450 | LOG(ERROR) << "Error setting the peer-to-peer update setting."; |
| 451 | return 1; |
| 452 | } |
Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
Alex Vakulenko | 59e253e | 2014-02-24 10:40:21 -0800 | [diff] [blame] | 456 | // Show the rollback availability. |
| 457 | if (FLAGS_can_rollback) { |
Casey Dahlin | ef36113 | 2015-12-17 13:02:37 -0800 | [diff] [blame] | 458 | string rollback_partition; |
| 459 | |
| 460 | if (!client_->GetRollbackPartition(&rollback_partition)) { |
| 461 | LOG(ERROR) << "Error while querying rollback partition availabilty."; |
| 462 | return 1; |
| 463 | } |
| 464 | |
Chris Sosa | f5c0b9c | 2014-04-17 16:12:03 -0700 | [diff] [blame] | 465 | bool can_rollback = true; |
| 466 | if (rollback_partition.empty()) { |
Alex Vakulenko | 2bddadd | 2014-03-27 13:23:46 -0700 | [diff] [blame] | 467 | rollback_partition = "UNAVAILABLE"; |
Chris Sosa | f5c0b9c | 2014-04-17 16:12:03 -0700 | [diff] [blame] | 468 | can_rollback = false; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 469 | } else { |
Alex Vakulenko | 2bddadd | 2014-03-27 13:23:46 -0700 | [diff] [blame] | 470 | rollback_partition = "AVAILABLE: " + rollback_partition; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 471 | } |
Alex Vakulenko | 2bddadd | 2014-03-27 13:23:46 -0700 | [diff] [blame] | 472 | |
| 473 | LOG(INFO) << "Rollback partition: " << rollback_partition; |
Chris Sosa | f5c0b9c | 2014-04-17 16:12:03 -0700 | [diff] [blame] | 474 | if (!can_rollback) { |
| 475 | return 1; |
| 476 | } |
Alex Vakulenko | 59e253e | 2014-02-24 10:40:21 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 479 | // Show the current P2P enabled setting. |
| 480 | if (FLAGS_show_p2p_update) { |
Casey Dahlin | ef36113 | 2015-12-17 13:02:37 -0800 | [diff] [blame] | 481 | bool enabled; |
| 482 | |
| 483 | if (!client_->GetP2PUpdatePermission(&enabled)) { |
| 484 | LOG(ERROR) << "Error getting the peer-to-peer update setting."; |
| 485 | return 1; |
| 486 | } |
| 487 | |
Alex Deymo | 5fdf776 | 2013-07-17 20:01:40 -0700 | [diff] [blame] | 488 | LOG(INFO) << "Current update using P2P setting: " |
| 489 | << (enabled ? "ENABLED" : "DISABLED"); |
| 490 | } |
| 491 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 492 | // First, update the target channel if requested. |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 493 | if (!FLAGS_channel.empty()) { |
| 494 | if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) { |
| 495 | LOG(ERROR) << "Error setting the channel."; |
| 496 | return 1; |
| 497 | } |
| 498 | |
| 499 | LOG(INFO) << "Channel permanently set to: " << FLAGS_channel; |
| 500 | } |
Darin Petkov | 8daa324 | 2010-10-25 13:28:47 -0700 | [diff] [blame] | 501 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 502 | // Show the current and target channels if requested. |
| 503 | if (FLAGS_show_channel) { |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 504 | string current_channel; |
| 505 | string target_channel; |
| 506 | |
| 507 | if (!client_->GetChannel(¤t_channel)) { |
| 508 | LOG(ERROR) << "Error getting the current channel."; |
| 509 | return 1; |
| 510 | } |
| 511 | |
| 512 | if (!client_->GetTargetChannel(&target_channel)) { |
| 513 | LOG(ERROR) << "Error getting the target channel."; |
| 514 | return 1; |
| 515 | } |
| 516 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 517 | LOG(INFO) << "Current Channel: " << current_channel; |
| 518 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 519 | if (!target_channel.empty()) |
| 520 | LOG(INFO) << "Target Channel (pending update): " << target_channel; |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 521 | } |
| 522 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 523 | bool do_update_request = FLAGS_check_for_update | FLAGS_update | |
| 524 | !FLAGS_app_version.empty() | !FLAGS_omaha_url.empty(); |
Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 525 | if (FLAGS_update) |
| 526 | FLAGS_follow = true; |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 527 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 528 | if (do_update_request && FLAGS_rollback) { |
Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 529 | LOG(ERROR) << "Incompatible flags specified with rollback." |
| 530 | << "Rollback should not include update-related flags."; |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 531 | return 1; |
| 532 | } |
| 533 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 534 | if (FLAGS_rollback) { |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 535 | LOG(INFO) << "Requesting rollback."; |
Casey Dahlin | ef36113 | 2015-12-17 13:02:37 -0800 | [diff] [blame] | 536 | if (!client_->Rollback(FLAGS_powerwash)) { |
| 537 | LOG(ERROR) << "Rollback request failed."; |
| 538 | return 1; |
| 539 | } |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 542 | // Initiate an update check, if necessary. |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 543 | if (do_update_request) { |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 544 | LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored."; |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 545 | string app_version = FLAGS_app_version; |
| 546 | if (FLAGS_update && app_version.empty()) { |
| 547 | app_version = "ForcedUpdate"; |
| 548 | 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] | 549 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 550 | LOG(INFO) << "Initiating update check and install."; |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 551 | if (!client_->AttemptUpdate(app_version, FLAGS_omaha_url, |
| 552 | FLAGS_interactive)) { |
| 553 | LOG(ERROR) << "Error checking for update."; |
| 554 | return 1; |
| 555 | } |
Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 556 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 557 | |
Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 558 | // These final options are all mutually exclusive with one another. |
David Zeuthen | 9d73a72 | 2014-04-04 14:52:46 -0700 | [diff] [blame] | 559 | if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot + |
| 560 | FLAGS_status + FLAGS_is_reboot_needed + |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 561 | FLAGS_block_until_reboot_is_needed > 1) { |
Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 562 | LOG(ERROR) << "Multiple exclusive options selected. " |
| 563 | << "Select only one of --follow, --watch_for_updates, --reboot, " |
David Zeuthen | 9d73a72 | 2014-04-04 14:52:46 -0700 | [diff] [blame] | 564 | << "--is_reboot_needed, --block_until_reboot_is_needed, " |
Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 565 | << "or --status."; |
| 566 | return 1; |
| 567 | } |
| 568 | |
| 569 | if (FLAGS_status) { |
| 570 | LOG(INFO) << "Querying Update Engine status..."; |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 571 | if (!ShowStatus()) { |
| 572 | LOG(ERROR) << "Failed to query status"; |
| 573 | return 1; |
| 574 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 575 | return 0; |
| 576 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 577 | |
Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 578 | if (FLAGS_follow) { |
| 579 | LOG(INFO) << "Waiting for update to complete."; |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 580 | WaitForUpdateComplete(); |
| 581 | return kContinueRunning; |
Chris Sosa | 192449e | 2013-10-28 14:16:19 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 584 | if (FLAGS_watch_for_updates) { |
| 585 | LOG(INFO) << "Watching for status updates."; |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 586 | WatchForUpdates(); |
| 587 | return kContinueRunning; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 588 | } |
Darin Petkov | 58529db | 2010-08-13 09:19:47 -0700 | [diff] [blame] | 589 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 590 | if (FLAGS_reboot) { |
| 591 | LOG(INFO) << "Requesting a reboot..."; |
Casey Dahlin | ef36113 | 2015-12-17 13:02:37 -0800 | [diff] [blame] | 592 | client_->RebootIfNeeded(); |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 593 | return 0; |
| 594 | } |
Andrew de los Reyes | ada4220 | 2010-07-15 22:23:20 -0700 | [diff] [blame] | 595 | |
Alex Vakulenko | dea2eac | 2014-03-14 15:56:59 -0700 | [diff] [blame] | 596 | if (FLAGS_prev_version) { |
Casey Dahlin | ef36113 | 2015-12-17 13:02:37 -0800 | [diff] [blame] | 597 | string prev_version; |
| 598 | |
| 599 | if (!client_->GetPrevVersion(&prev_version)) { |
| 600 | LOG(ERROR) << "Error getting previous version."; |
| 601 | } else { |
| 602 | LOG(INFO) << "Previous version = " << prev_version; |
| 603 | } |
Alex Vakulenko | dea2eac | 2014-03-14 15:56:59 -0700 | [diff] [blame] | 604 | } |
| 605 | |
David Zeuthen | 9d73a72 | 2014-04-04 14:52:46 -0700 | [diff] [blame] | 606 | if (FLAGS_is_reboot_needed) { |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 607 | int ret = GetNeedReboot(); |
| 608 | |
| 609 | if (ret == 1) { |
| 610 | LOG(ERROR) << "Could not query the current operation."; |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 611 | } |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 612 | |
| 613 | return ret; |
David Zeuthen | 9d73a72 | 2014-04-04 14:52:46 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | if (FLAGS_block_until_reboot_is_needed) { |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 617 | WaitForRebootNeeded(); |
| 618 | return kContinueRunning; |
David Zeuthen | 9d73a72 | 2014-04-04 14:52:46 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 621 | return 0; |
| 622 | } |
Alex Deymo | 72aa002 | 2015-06-19 21:16:49 -0700 | [diff] [blame] | 623 | |
| 624 | } // namespace |
| 625 | |
| 626 | int main(int argc, char** argv) { |
| 627 | UpdateEngineClient client(argc, argv); |
| 628 | return client.Run(); |
| 629 | } |