Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2016 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 <sysexits.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <base/bind.h> |
| 24 | #include <base/callback.h> |
| 25 | #include <base/command_line.h> |
| 26 | #include <base/logging.h> |
| 27 | #include <base/strings/string_split.h> |
| 28 | #include <binder/IServiceManager.h> |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame^] | 29 | #include <binderwrapper/binder_wrapper.h> |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 30 | #include <brillo/binder_watcher.h> |
| 31 | #include <brillo/daemons/daemon.h> |
| 32 | #include <brillo/flag_helper.h> |
| 33 | #include <brillo/message_loops/message_loop.h> |
| 34 | #include <brillo/syslog_logging.h> |
| 35 | #include <utils/String16.h> |
| 36 | #include <utils/StrongPointer.h> |
| 37 | |
| 38 | #include "android/os/BnUpdateEngineCallback.h" |
| 39 | #include "android/os/IUpdateEngine.h" |
| 40 | #include "update_engine/client_library/include/update_engine/update_status.h" |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 41 | #include "update_engine/common/error_code.h" |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame^] | 42 | #include "update_engine/update_status_utils.h" |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 43 | |
| 44 | using android::binder::Status; |
| 45 | |
| 46 | namespace chromeos_update_engine { |
| 47 | namespace internal { |
| 48 | |
| 49 | class UpdateEngineClientAndroid : public brillo::Daemon { |
| 50 | public: |
| 51 | UpdateEngineClientAndroid(int argc, char** argv) : argc_(argc), argv_(argv) { |
| 52 | } |
| 53 | |
| 54 | int ExitWhenIdle(const Status& status); |
| 55 | int ExitWhenIdle(int return_code); |
| 56 | |
| 57 | private: |
| 58 | class UECallback : public android::os::BnUpdateEngineCallback { |
| 59 | public: |
| 60 | UECallback(UpdateEngineClientAndroid* client) : client_(client) { |
| 61 | } |
| 62 | |
| 63 | // android::os::BnUpdateEngineCallback overrides. |
| 64 | Status onStatusUpdate(int status_code, float progress) override; |
| 65 | Status onPayloadApplicationComplete(int error_code) override; |
| 66 | |
| 67 | private: |
| 68 | UpdateEngineClientAndroid* client_; |
| 69 | }; |
| 70 | |
| 71 | int OnInit() override; |
| 72 | |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame^] | 73 | // Called whenever the UpdateEngine daemon dies. |
| 74 | void UpdateEngineServiceDied(); |
| 75 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 76 | // Copy of argc and argv passed to main(). |
| 77 | int argc_; |
| 78 | char** argv_; |
| 79 | |
| 80 | android::sp<android::os::IUpdateEngine> service_; |
| 81 | android::sp<android::os::BnUpdateEngineCallback> callback_; |
| 82 | |
| 83 | brillo::BinderWatcher binder_watcher_; |
| 84 | }; |
| 85 | |
| 86 | Status UpdateEngineClientAndroid::UECallback::onStatusUpdate( |
| 87 | int status_code, float progress) { |
| 88 | update_engine::UpdateStatus status = |
| 89 | static_cast<update_engine::UpdateStatus>(status_code); |
| 90 | LOG(INFO) << "onStatusUpdate(" << UpdateStatusToString(status) << " (" |
| 91 | << status_code << "), " << progress << ")"; |
| 92 | return Status::ok(); |
| 93 | } |
| 94 | |
| 95 | Status UpdateEngineClientAndroid::UECallback::onPayloadApplicationComplete( |
| 96 | int error_code) { |
| 97 | ErrorCode code = static_cast<ErrorCode>(error_code); |
| 98 | // TODO(deymo): Print the ErrorCode as a string. |
| 99 | LOG(INFO) << "onPayloadApplicationComplete(" << error_code << ")"; |
| 100 | client_->ExitWhenIdle(code == ErrorCode::kSuccess ? EX_OK : 1); |
| 101 | return Status::ok(); |
| 102 | } |
| 103 | |
| 104 | int UpdateEngineClientAndroid::OnInit() { |
| 105 | int ret = Daemon::OnInit(); |
| 106 | if (ret != EX_OK) |
| 107 | return ret; |
| 108 | |
| 109 | DEFINE_bool(update, false, "Start a new update, if no update in progress."); |
| 110 | DEFINE_string(payload, |
| 111 | "http://127.0.0.1:8080/payload", |
| 112 | "The URI to the update payload to use."); |
| 113 | DEFINE_string(headers, |
| 114 | "", |
| 115 | "A list of key-value pairs, one element of the list per line."); |
| 116 | |
| 117 | DEFINE_bool(suspend, false, "Suspend an ongoing update and exit."); |
| 118 | DEFINE_bool(resume, false, "Resume a suspended update."); |
| 119 | DEFINE_bool(cancel, false, "Cancel the ongoing update and exit."); |
| 120 | DEFINE_bool(follow, |
| 121 | false, |
| 122 | "Follow status update changes until a final state is reached. " |
| 123 | "Exit status is 0 if the update succeeded, and 1 otherwise."); |
| 124 | |
| 125 | // Boilerplate init commands. |
| 126 | base::CommandLine::Init(argc_, argv_); |
| 127 | brillo::FlagHelper::Init(argc_, argv_, "Android Update Engine Client"); |
| 128 | if (argc_ == 1) { |
| 129 | LOG(ERROR) << "Nothing to do. Run with --help for help."; |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | // Ensure there are no positional arguments. |
| 134 | const std::vector<std::string> positional_args = |
| 135 | base::CommandLine::ForCurrentProcess()->GetArgs(); |
| 136 | if (!positional_args.empty()) { |
| 137 | LOG(ERROR) << "Found a positional argument '" << positional_args.front() |
| 138 | << "'. If you want to pass a value to a flag, pass it as " |
| 139 | "--flag=value."; |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | bool keep_running = false; |
Alex Deymo | 95224dd | 2016-01-29 11:18:35 -0800 | [diff] [blame] | 144 | brillo::InitLog(brillo::kLogToStderr); |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame^] | 145 | |
| 146 | // Initialize a binder watcher early in the process before any interaction |
| 147 | // with the binder driver. |
| 148 | binder_watcher_.Init(); |
| 149 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 150 | android::status_t status = android::getService( |
| 151 | android::String16("android.os.UpdateEngineService"), &service_); |
| 152 | if (status != android::OK) { |
| 153 | LOG(ERROR) << "Failed to get IUpdateEngine binder from service manager: " |
| 154 | << Status::fromStatusT(status).toString8(); |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame^] | 155 | return ExitWhenIdle(1); |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | if (FLAGS_suspend) { |
| 159 | return ExitWhenIdle(service_->suspend()); |
| 160 | } |
| 161 | |
| 162 | if (FLAGS_resume) { |
| 163 | return ExitWhenIdle(service_->resume()); |
| 164 | } |
| 165 | |
| 166 | if (FLAGS_cancel) { |
| 167 | return ExitWhenIdle(service_->cancel()); |
| 168 | } |
| 169 | |
| 170 | if (FLAGS_follow) { |
| 171 | // Register a callback object with the service. |
| 172 | callback_ = new UECallback(this); |
| 173 | bool bound; |
| 174 | if (!service_->bind(callback_, &bound).isOk() || !bound) { |
| 175 | LOG(ERROR) << "Failed to bind() the UpdateEngine daemon."; |
| 176 | return 1; |
| 177 | } |
| 178 | keep_running = true; |
| 179 | } |
| 180 | |
| 181 | if (FLAGS_update) { |
| 182 | std::vector<std::string> headers = base::SplitString( |
| 183 | FLAGS_headers, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 184 | std::vector<android::String16> and_headers; |
| 185 | for (const auto& header : headers) { |
| 186 | and_headers.push_back(android::String16{header.data(), header.size()}); |
| 187 | } |
| 188 | Status status = service_->applyPayload( |
| 189 | android::String16{FLAGS_payload.data(), FLAGS_payload.size()}, |
| 190 | and_headers); |
| 191 | if (!status.isOk()) |
| 192 | return ExitWhenIdle(status); |
| 193 | } |
| 194 | |
| 195 | if (!keep_running) |
| 196 | return ExitWhenIdle(EX_OK); |
| 197 | |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame^] | 198 | // When following updates status changes, exit if the update_engine daemon |
| 199 | // dies. |
| 200 | android::BinderWrapper::Create(); |
| 201 | android::BinderWrapper::Get()->RegisterForDeathNotifications( |
| 202 | android::os::IUpdateEngine::asBinder(service_), |
| 203 | base::Bind(&UpdateEngineClientAndroid::UpdateEngineServiceDied, |
| 204 | base::Unretained(this))); |
| 205 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 206 | return EX_OK; |
| 207 | } |
| 208 | |
| 209 | int UpdateEngineClientAndroid::ExitWhenIdle(const Status& status) { |
| 210 | if (status.isOk()) |
| 211 | return ExitWhenIdle(EX_OK); |
| 212 | LOG(ERROR) << status.toString8(); |
| 213 | return ExitWhenIdle(status.exceptionCode()); |
| 214 | } |
| 215 | |
| 216 | int UpdateEngineClientAndroid::ExitWhenIdle(int return_code) { |
| 217 | auto delayed_exit = base::Bind( |
| 218 | &Daemon::QuitWithExitCode, base::Unretained(this), return_code); |
| 219 | if (!brillo::MessageLoop::current()->PostTask(delayed_exit)) |
| 220 | return 1; |
| 221 | return EX_OK; |
| 222 | } |
| 223 | |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame^] | 224 | void UpdateEngineClientAndroid::UpdateEngineServiceDied() { |
| 225 | LOG(ERROR) << "UpdateEngineService died."; |
| 226 | QuitWithExitCode(1); |
| 227 | } |
| 228 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 229 | } // namespace internal |
| 230 | } // namespace chromeos_update_engine |
| 231 | |
| 232 | int main(int argc, char** argv) { |
| 233 | chromeos_update_engine::internal::UpdateEngineClientAndroid client( |
| 234 | argc, argv); |
| 235 | return client.Run(); |
| 236 | } |