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