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