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