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 // 16 17 #include <inttypes.h> 18 #include <sysexits.h> 19 #include <unistd.h> 20 21 #include <memory> 22 #include <string> 23 #include <vector> 24 25 #include <base/bind.h> 26 #include <base/command_line.h> 27 #include <base/logging.h> 28 #include <base/macros.h> 29 #include <brillo/daemons/daemon.h> 30 #include <brillo/flag_helper.h> 31 32 #include "update_engine/client.h" 33 #include "update_engine/common/error_code.h" 34 #include "update_engine/common/error_code_utils.h" 35 #include "update_engine/omaha_utils.h" 36 #include "update_engine/status_update_handler.h" 37 #include "update_engine/update_status.h" 38 #include "update_engine/update_status_utils.h" 39 40 using chromeos_update_engine::EolStatus; 41 using chromeos_update_engine::ErrorCode; 42 using chromeos_update_engine::UpdateStatusToString; 43 using chromeos_update_engine::utils::ErrorCodeToString; 44 using std::string; 45 using std::unique_ptr; 46 using std::vector; 47 using update_engine::UpdateStatus; 48 49 namespace { 50 51 // Constant to signal that we need to continue running the daemon after 52 // initialization. 53 const int kContinueRunning = -1; 54 55 class UpdateEngineClient : public brillo::Daemon { 56 public: 57 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) { 58 } 59 60 ~UpdateEngineClient() override = default; 61 62 protected: 63 int OnInit() override { 64 int ret = Daemon::OnInit(); 65 if (ret != EX_OK) return ret; 66 67 client_ = update_engine::UpdateEngineClient::CreateInstance(); 68 69 if (!client_) { 70 LOG(ERROR) << "UpdateEngineService not available."; 71 return 1; 72 } 73 74 // We can't call QuitWithExitCode from OnInit(), so we delay the execution 75 // of the ProcessFlags method after the Daemon initialization is done. 76 base::MessageLoop::current()->PostTask( 77 FROM_HERE, 78 base::Bind(&UpdateEngineClient::ProcessFlagsAndExit, 79 base::Unretained(this))); 80 return EX_OK; 81 } 82 83 private: 84 // Show the status of the update engine in stdout. 85 bool ShowStatus(); 86 87 // Return whether we need to reboot. 0 if reboot is needed, 1 if an error 88 // occurred, 2 if no reboot is needed. 89 int GetNeedReboot(); 90 91 // Main method that parses and triggers all the actions based on the passed 92 // flags. Returns the exit code of the program of kContinueRunning if it 93 // should not exit. 94 int ProcessFlags(); 95 96 // Processes the flags and exits the program accordingly. 97 void ProcessFlagsAndExit(); 98 99 // Copy of argc and argv passed to main(). 100 int argc_; 101 char** argv_; 102 103 // Library-based client 104 unique_ptr<update_engine::UpdateEngineClient> client_; 105 106 // Pointers to handlers for cleanup 107 vector<unique_ptr<update_engine::StatusUpdateHandler>> handlers_; 108 109 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient); 110 }; 111 112 class ExitingStatusUpdateHandler : public update_engine::StatusUpdateHandler { 113 public: 114 ~ExitingStatusUpdateHandler() override = default; 115 116 void IPCError(const string& error) override; 117 }; 118 119 void ExitingStatusUpdateHandler::IPCError(const string& error) { 120 LOG(ERROR) << error; 121 exit(1); 122 } 123 124 class WatchingStatusUpdateHandler : public ExitingStatusUpdateHandler { 125 public: 126 ~WatchingStatusUpdateHandler() override = default; 127 128 void HandleStatusUpdate(int64_t last_checked_time, 129 double progress, 130 UpdateStatus current_operation, 131 const string& new_version, 132 int64_t new_size) override; 133 }; 134 135 void WatchingStatusUpdateHandler::HandleStatusUpdate( 136 int64_t last_checked_time, double progress, UpdateStatus current_operation, 137 const string& new_version, int64_t new_size) { 138 LOG(INFO) << "Got status update:"; 139 LOG(INFO) << " last_checked_time: " << last_checked_time; 140 LOG(INFO) << " progress: " << progress; 141 LOG(INFO) << " current_operation: " 142 << UpdateStatusToString(current_operation); 143 LOG(INFO) << " new_version: " << new_version; 144 LOG(INFO) << " new_size: " << new_size; 145 } 146 147 bool UpdateEngineClient::ShowStatus() { 148 int64_t last_checked_time = 0; 149 double progress = 0.0; 150 UpdateStatus current_op; 151 string new_version; 152 int64_t new_size = 0; 153 154 if (!client_->GetStatus(&last_checked_time, &progress, ¤t_op, 155 &new_version, &new_size)) { 156 return false; 157 } 158 159 printf("LAST_CHECKED_TIME=%" PRIi64 160 "\nPROGRESS=%f\nCURRENT_OP=%s\n" 161 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n", 162 last_checked_time, progress, UpdateStatusToString(current_op), 163 new_version.c_str(), new_size); 164 165 return true; 166 } 167 168 int UpdateEngineClient::GetNeedReboot() { 169 int64_t last_checked_time = 0; 170 double progress = 0.0; 171 UpdateStatus current_op; 172 string new_version; 173 int64_t new_size = 0; 174 175 if (!client_->GetStatus(&last_checked_time, &progress, ¤t_op, 176 &new_version, &new_size)) { 177 return 1; 178 } 179 180 if (current_op == UpdateStatus::UPDATED_NEED_REBOOT) { 181 return 0; 182 } 183 184 return 2; 185 } 186 187 class UpdateWaitHandler : public ExitingStatusUpdateHandler { 188 public: 189 explicit UpdateWaitHandler(bool exit_on_error, 190 update_engine::UpdateEngineClient* client) 191 : exit_on_error_(exit_on_error), client_(client) {} 192 193 ~UpdateWaitHandler() override = default; 194 195 void HandleStatusUpdate(int64_t last_checked_time, 196 double progress, 197 UpdateStatus current_operation, 198 const string& new_version, 199 int64_t new_size) override; 200 201 private: 202 bool exit_on_error_; 203 update_engine::UpdateEngineClient* client_; 204 }; 205 206 void UpdateWaitHandler::HandleStatusUpdate(int64_t /* last_checked_time */, 207 double /* progress */, 208 UpdateStatus current_operation, 209 const string& /* new_version */, 210 int64_t /* new_size */) { 211 if (exit_on_error_ && current_operation == UpdateStatus::IDLE) { 212 int last_attempt_error; 213 ErrorCode code = ErrorCode::kSuccess; 214 if (client_ && client_->GetLastAttemptError(&last_attempt_error)) 215 code = static_cast<ErrorCode>(last_attempt_error); 216 217 LOG(ERROR) << "Update failed, current operation is " 218 << UpdateStatusToString(current_operation) 219 << ", last error code is " << ErrorCodeToString(code) << "(" 220 << last_attempt_error << ")"; 221 exit(1); 222 } 223 if (current_operation == UpdateStatus::UPDATED_NEED_REBOOT) { 224 LOG(INFO) << "Update succeeded -- reboot needed."; 225 exit(0); 226 } 227 } 228 229 int UpdateEngineClient::ProcessFlags() { 230 DEFINE_string(app_version, "", "Force the current app version."); 231 DEFINE_string(channel, "", 232 "Set the target channel. The device will be powerwashed if the " 233 "target channel is more stable than the current channel unless " 234 "--nopowerwash is specified."); 235 DEFINE_bool(check_for_update, false, "Initiate check for updates."); 236 DEFINE_string( 237 cohort_hint, "", "Set the current cohort hint to the passed value."); 238 DEFINE_bool(follow, false, 239 "Wait for any update operations to complete." 240 "Exit status is 0 if the update succeeded, and 1 otherwise."); 241 DEFINE_bool(interactive, true, "Mark the update request as interactive."); 242 DEFINE_string(omaha_url, "", "The URL of the Omaha update server."); 243 DEFINE_string(p2p_update, "", 244 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update" 245 " sharing."); 246 DEFINE_bool(powerwash, true, 247 "When performing rollback or channel change, " 248 "do a powerwash or allow it respectively."); 249 DEFINE_bool(reboot, false, "Initiate a reboot if needed."); 250 DEFINE_bool(is_reboot_needed, false, 251 "Exit status 0 if reboot is needed, " 252 "2 if reboot is not needed or 1 if an error occurred."); 253 DEFINE_bool(block_until_reboot_is_needed, false, 254 "Blocks until reboot is " 255 "needed. Returns non-zero exit status if an error occurred."); 256 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle."); 257 DEFINE_bool(rollback, false, 258 "Perform a rollback to the previous partition. The device will " 259 "be powerwashed unless --nopowerwash is specified."); 260 DEFINE_bool(can_rollback, false, 261 "Shows whether rollback partition " 262 "is available."); 263 DEFINE_bool(show_channel, false, "Show the current and target channels."); 264 DEFINE_bool(show_cohort_hint, false, "Show the current cohort hint."); 265 DEFINE_bool(show_p2p_update, false, 266 "Show the current setting for peer-to-peer update sharing."); 267 DEFINE_bool(show_update_over_cellular, false, 268 "Show the current setting for updates over cellular networks."); 269 DEFINE_bool(status, false, "Print the status to stdout."); 270 DEFINE_bool(update, false, 271 "Forces an update and waits for it to complete. " 272 "Implies --follow."); 273 DEFINE_string(update_over_cellular, "", 274 "Enables (\"yes\") or disables (\"no\") the updates over " 275 "cellular networks."); 276 DEFINE_bool(watch_for_updates, false, 277 "Listen for status updates and print them to the screen."); 278 DEFINE_bool(prev_version, false, 279 "Show the previous OS version used before the update reboot."); 280 DEFINE_bool(last_attempt_error, false, "Show the last attempt error."); 281 DEFINE_bool(eol_status, false, "Show the current end-of-life status."); 282 283 // Boilerplate init commands. 284 base::CommandLine::Init(argc_, argv_); 285 brillo::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client"); 286 287 // Ensure there are no positional arguments. 288 const vector<string> positional_args = 289 base::CommandLine::ForCurrentProcess()->GetArgs(); 290 if (!positional_args.empty()) { 291 LOG(ERROR) << "Found a positional argument '" << positional_args.front() 292 << "'. If you want to pass a value to a flag, pass it as " 293 "--flag=value."; 294 return 1; 295 } 296 297 // Update the status if requested. 298 if (FLAGS_reset_status) { 299 LOG(INFO) << "Setting Update Engine status to idle ..."; 300 301 if (client_->ResetStatus()) { 302 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes " 303 "run:\n" 304 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo " 305 "${P#$D} | sed 's/^[^0-9]*//')-1)) $D;)"; 306 } else { 307 LOG(ERROR) << "ResetStatus failed"; 308 return 1; 309 } 310 } 311 312 // Changes the current update over cellular network setting. 313 if (!FLAGS_update_over_cellular.empty()) { 314 bool allowed = FLAGS_update_over_cellular == "yes"; 315 if (!allowed && FLAGS_update_over_cellular != "no") { 316 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular 317 << "\". Please specify \"yes\" or \"no\"."; 318 } else { 319 if (!client_->SetUpdateOverCellularPermission(allowed)) { 320 LOG(ERROR) << "Error setting the update over cellular setting."; 321 return 1; 322 } 323 } 324 } 325 326 // Show the current update over cellular network setting. 327 if (FLAGS_show_update_over_cellular) { 328 bool allowed; 329 330 if (!client_->GetUpdateOverCellularPermission(&allowed)) { 331 LOG(ERROR) << "Error getting the update over cellular setting."; 332 return 1; 333 } 334 335 LOG(INFO) << "Current update over cellular network setting: " 336 << (allowed ? "ENABLED" : "DISABLED"); 337 } 338 339 // Change/show the cohort hint. 340 bool set_cohort_hint = 341 base::CommandLine::ForCurrentProcess()->HasSwitch("cohort_hint"); 342 if (set_cohort_hint) { 343 LOG(INFO) << "Setting cohort hint to: \"" << FLAGS_cohort_hint << "\""; 344 if (!client_->SetCohortHint(FLAGS_cohort_hint)) { 345 LOG(ERROR) << "Error setting the cohort hint."; 346 return 1; 347 } 348 } 349 350 if (FLAGS_show_cohort_hint || set_cohort_hint) { 351 string cohort_hint; 352 if (!client_->GetCohortHint(&cohort_hint)) { 353 LOG(ERROR) << "Error getting the cohort hint."; 354 return 1; 355 } 356 357 LOG(INFO) << "Current cohort hint: \"" << cohort_hint << "\""; 358 } 359 360 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) { 361 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change"; 362 return 1; 363 } 364 365 // Change the P2P enabled setting. 366 if (!FLAGS_p2p_update.empty()) { 367 bool enabled = FLAGS_p2p_update == "yes"; 368 if (!enabled && FLAGS_p2p_update != "no") { 369 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update 370 << "\". Please specify \"yes\" or \"no\"."; 371 } else { 372 if (!client_->SetP2PUpdatePermission(enabled)) { 373 LOG(ERROR) << "Error setting the peer-to-peer update setting."; 374 return 1; 375 } 376 } 377 } 378 379 // Show the rollback availability. 380 if (FLAGS_can_rollback) { 381 string rollback_partition; 382 383 if (!client_->GetRollbackPartition(&rollback_partition)) { 384 LOG(ERROR) << "Error while querying rollback partition availabilty."; 385 return 1; 386 } 387 388 bool can_rollback = true; 389 if (rollback_partition.empty()) { 390 rollback_partition = "UNAVAILABLE"; 391 can_rollback = false; 392 } else { 393 rollback_partition = "AVAILABLE: " + rollback_partition; 394 } 395 396 LOG(INFO) << "Rollback partition: " << rollback_partition; 397 if (!can_rollback) { 398 return 1; 399 } 400 } 401 402 // Show the current P2P enabled setting. 403 if (FLAGS_show_p2p_update) { 404 bool enabled; 405 406 if (!client_->GetP2PUpdatePermission(&enabled)) { 407 LOG(ERROR) << "Error getting the peer-to-peer update setting."; 408 return 1; 409 } 410 411 LOG(INFO) << "Current update using P2P setting: " 412 << (enabled ? "ENABLED" : "DISABLED"); 413 } 414 415 // First, update the target channel if requested. 416 if (!FLAGS_channel.empty()) { 417 if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) { 418 LOG(ERROR) << "Error setting the channel."; 419 return 1; 420 } 421 422 LOG(INFO) << "Channel permanently set to: " << FLAGS_channel; 423 } 424 425 // Show the current and target channels if requested. 426 if (FLAGS_show_channel) { 427 string current_channel; 428 string target_channel; 429 430 if (!client_->GetChannel(¤t_channel)) { 431 LOG(ERROR) << "Error getting the current channel."; 432 return 1; 433 } 434 435 if (!client_->GetTargetChannel(&target_channel)) { 436 LOG(ERROR) << "Error getting the target channel."; 437 return 1; 438 } 439 440 LOG(INFO) << "Current Channel: " << current_channel; 441 442 if (!target_channel.empty()) 443 LOG(INFO) << "Target Channel (pending update): " << target_channel; 444 } 445 446 bool do_update_request = FLAGS_check_for_update | FLAGS_update | 447 !FLAGS_app_version.empty() | 448 !FLAGS_omaha_url.empty(); 449 if (FLAGS_update) FLAGS_follow = true; 450 451 if (do_update_request && FLAGS_rollback) { 452 LOG(ERROR) << "Incompatible flags specified with rollback." 453 << "Rollback should not include update-related flags."; 454 return 1; 455 } 456 457 if (FLAGS_rollback) { 458 LOG(INFO) << "Requesting rollback."; 459 if (!client_->Rollback(FLAGS_powerwash)) { 460 LOG(ERROR) << "Rollback request failed."; 461 return 1; 462 } 463 } 464 465 // Initiate an update check, if necessary. 466 if (do_update_request) { 467 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored."; 468 string app_version = FLAGS_app_version; 469 if (FLAGS_update && app_version.empty()) { 470 app_version = "ForcedUpdate"; 471 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate."; 472 } 473 LOG(INFO) << "Initiating update check and install."; 474 if (!client_->AttemptUpdate(app_version, FLAGS_omaha_url, 475 FLAGS_interactive)) { 476 LOG(ERROR) << "Error checking for update."; 477 return 1; 478 } 479 } 480 481 // These final options are all mutually exclusive with one another. 482 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot + FLAGS_status + 483 FLAGS_is_reboot_needed + FLAGS_block_until_reboot_is_needed > 484 1) { 485 LOG(ERROR) << "Multiple exclusive options selected. " 486 << "Select only one of --follow, --watch_for_updates, --reboot, " 487 << "--is_reboot_needed, --block_until_reboot_is_needed, " 488 << "or --status."; 489 return 1; 490 } 491 492 if (FLAGS_status) { 493 LOG(INFO) << "Querying Update Engine status..."; 494 if (!ShowStatus()) { 495 LOG(ERROR) << "Failed to query status"; 496 return 1; 497 } 498 return 0; 499 } 500 501 if (FLAGS_follow) { 502 LOG(INFO) << "Waiting for update to complete."; 503 auto handler = new UpdateWaitHandler(true, client_.get()); 504 handlers_.emplace_back(handler); 505 client_->RegisterStatusUpdateHandler(handler); 506 return kContinueRunning; 507 } 508 509 if (FLAGS_watch_for_updates) { 510 LOG(INFO) << "Watching for status updates."; 511 auto handler = new WatchingStatusUpdateHandler(); 512 handlers_.emplace_back(handler); 513 client_->RegisterStatusUpdateHandler(handler); 514 return kContinueRunning; 515 } 516 517 if (FLAGS_reboot) { 518 LOG(INFO) << "Requesting a reboot..."; 519 client_->RebootIfNeeded(); 520 return 0; 521 } 522 523 if (FLAGS_prev_version) { 524 string prev_version; 525 526 if (!client_->GetPrevVersion(&prev_version)) { 527 LOG(ERROR) << "Error getting previous version."; 528 } else { 529 LOG(INFO) << "Previous version = " << prev_version; 530 } 531 } 532 533 if (FLAGS_is_reboot_needed) { 534 int ret = GetNeedReboot(); 535 536 if (ret == 1) { 537 LOG(ERROR) << "Could not query the current operation."; 538 } 539 540 return ret; 541 } 542 543 if (FLAGS_block_until_reboot_is_needed) { 544 auto handler = new UpdateWaitHandler(false, nullptr); 545 handlers_.emplace_back(handler); 546 client_->RegisterStatusUpdateHandler(handler); 547 return kContinueRunning; 548 } 549 550 if (FLAGS_last_attempt_error) { 551 int last_attempt_error; 552 if (!client_->GetLastAttemptError(&last_attempt_error)) { 553 LOG(ERROR) << "Error getting last attempt error."; 554 } else { 555 ErrorCode code = static_cast<ErrorCode>(last_attempt_error); 556 printf( 557 "ERROR_CODE=%i\n" 558 "ERROR_MESSAGE=%s\n", 559 last_attempt_error, 560 ErrorCodeToString(code).c_str()); 561 } 562 } 563 564 if (FLAGS_eol_status) { 565 int eol_status; 566 if (!client_->GetEolStatus(&eol_status)) { 567 LOG(ERROR) << "Error getting the end-of-life status."; 568 } else { 569 EolStatus eol_status_code = static_cast<EolStatus>(eol_status); 570 printf("EOL_STATUS=%s\n", EolStatusToString(eol_status_code)); 571 } 572 } 573 574 return 0; 575 } 576 577 void UpdateEngineClient::ProcessFlagsAndExit() { 578 int ret = ProcessFlags(); 579 if (ret != kContinueRunning) 580 QuitWithExitCode(ret); 581 } 582 583 } // namespace 584 585 int main(int argc, char** argv) { 586 UpdateEngineClient client(argc, argv); 587 return client.Run(); 588 } 589