update_engine_client_android.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #include <sysexits.h>
  17. #include <unistd.h>
  18. #include <string>
  19. #include <vector>
  20. #include <base/bind.h>
  21. #include <base/callback.h>
  22. #include <base/command_line.h>
  23. #include <base/logging.h>
  24. #include <base/strings/string_split.h>
  25. #include <binder/IServiceManager.h>
  26. #include <binderwrapper/binder_wrapper.h>
  27. #include <brillo/binder_watcher.h>
  28. #include <brillo/daemons/daemon.h>
  29. #include <brillo/flag_helper.h>
  30. #include <brillo/message_loops/message_loop.h>
  31. #include <brillo/syslog_logging.h>
  32. #include <utils/String16.h>
  33. #include <utils/StrongPointer.h>
  34. #include "android/os/BnUpdateEngineCallback.h"
  35. #include "android/os/IUpdateEngine.h"
  36. #include "update_engine/client_library/include/update_engine/update_status.h"
  37. #include "update_engine/common/error_code.h"
  38. #include "update_engine/common/error_code_utils.h"
  39. #include "update_engine/update_status_utils.h"
  40. using android::binder::Status;
  41. namespace chromeos_update_engine {
  42. namespace internal {
  43. class UpdateEngineClientAndroid : public brillo::Daemon {
  44. public:
  45. UpdateEngineClientAndroid(int argc, char** argv) : argc_(argc), argv_(argv) {}
  46. int ExitWhenIdle(const Status& status);
  47. int ExitWhenIdle(int return_code);
  48. private:
  49. class UECallback : public android::os::BnUpdateEngineCallback {
  50. public:
  51. explicit UECallback(UpdateEngineClientAndroid* client) : client_(client) {}
  52. // android::os::BnUpdateEngineCallback overrides.
  53. Status onStatusUpdate(int status_code, float progress) override;
  54. Status onPayloadApplicationComplete(int error_code) override;
  55. private:
  56. UpdateEngineClientAndroid* client_;
  57. };
  58. int OnInit() override;
  59. // Called whenever the UpdateEngine daemon dies.
  60. void UpdateEngineServiceDied();
  61. // Copy of argc and argv passed to main().
  62. int argc_;
  63. char** argv_;
  64. android::sp<android::os::IUpdateEngine> service_;
  65. android::sp<android::os::BnUpdateEngineCallback> callback_;
  66. brillo::BinderWatcher binder_watcher_;
  67. };
  68. Status UpdateEngineClientAndroid::UECallback::onStatusUpdate(int status_code,
  69. float progress) {
  70. update_engine::UpdateStatus status =
  71. static_cast<update_engine::UpdateStatus>(status_code);
  72. LOG(INFO) << "onStatusUpdate(" << UpdateStatusToString(status) << " ("
  73. << status_code << "), " << progress << ")";
  74. return Status::ok();
  75. }
  76. Status UpdateEngineClientAndroid::UECallback::onPayloadApplicationComplete(
  77. int error_code) {
  78. ErrorCode code = static_cast<ErrorCode>(error_code);
  79. LOG(INFO) << "onPayloadApplicationComplete(" << utils::ErrorCodeToString(code)
  80. << " (" << error_code << "))";
  81. client_->ExitWhenIdle(
  82. (code == ErrorCode::kSuccess || code == ErrorCode::kUpdatedButNotActive)
  83. ? EX_OK
  84. : 1);
  85. return Status::ok();
  86. }
  87. int UpdateEngineClientAndroid::OnInit() {
  88. int ret = Daemon::OnInit();
  89. if (ret != EX_OK)
  90. return ret;
  91. DEFINE_bool(update, false, "Start a new update, if no update in progress.");
  92. DEFINE_string(payload,
  93. "http://127.0.0.1:8080/payload",
  94. "The URI to the update payload to use.");
  95. DEFINE_int64(offset,
  96. 0,
  97. "The offset in the payload where the CrAU update starts. "
  98. "Used when --update is passed.");
  99. DEFINE_int64(size,
  100. 0,
  101. "The size of the CrAU part of the payload. If 0 is passed, it "
  102. "will be autodetected. Used when --update is passed.");
  103. DEFINE_string(headers,
  104. "",
  105. "A list of key-value pairs, one element of the list per line. "
  106. "Used when --update is passed.");
  107. DEFINE_bool(verify,
  108. false,
  109. "Given payload metadata, verify if the payload is applicable.");
  110. DEFINE_string(metadata,
  111. "/data/ota_package/metadata",
  112. "The path to the update payload metadata. "
  113. "Used when --verify is passed.");
  114. DEFINE_bool(suspend, false, "Suspend an ongoing update and exit.");
  115. DEFINE_bool(resume, false, "Resume a suspended update.");
  116. DEFINE_bool(cancel, false, "Cancel the ongoing update and exit.");
  117. DEFINE_bool(reset_status, false, "Reset an already applied update and exit.");
  118. DEFINE_bool(follow,
  119. false,
  120. "Follow status update changes until a final state is reached. "
  121. "Exit status is 0 if the update succeeded, and 1 otherwise.");
  122. // Boilerplate init commands.
  123. base::CommandLine::Init(argc_, argv_);
  124. brillo::FlagHelper::Init(argc_, argv_, "Android Update Engine Client");
  125. if (argc_ == 1) {
  126. LOG(ERROR) << "Nothing to do. Run with --help for help.";
  127. return 1;
  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. bool keep_running = false;
  139. brillo::InitLog(brillo::kLogToStderr);
  140. // Initialize a binder watcher early in the process before any interaction
  141. // with the binder driver.
  142. binder_watcher_.Init();
  143. android::status_t status = android::getService(
  144. android::String16("android.os.UpdateEngineService"), &service_);
  145. if (status != android::OK) {
  146. LOG(ERROR) << "Failed to get IUpdateEngine binder from service manager: "
  147. << Status::fromStatusT(status).toString8();
  148. return ExitWhenIdle(1);
  149. }
  150. if (FLAGS_suspend) {
  151. return ExitWhenIdle(service_->suspend());
  152. }
  153. if (FLAGS_resume) {
  154. return ExitWhenIdle(service_->resume());
  155. }
  156. if (FLAGS_cancel) {
  157. return ExitWhenIdle(service_->cancel());
  158. }
  159. if (FLAGS_reset_status) {
  160. return ExitWhenIdle(service_->resetStatus());
  161. }
  162. if (FLAGS_verify) {
  163. bool applicable = false;
  164. Status status = service_->verifyPayloadApplicable(
  165. android::String16{FLAGS_metadata.data(), FLAGS_metadata.size()},
  166. &applicable);
  167. LOG(INFO) << "Payload is " << (applicable ? "" : "not ") << "applicable.";
  168. return ExitWhenIdle(status);
  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. if (FLAGS_update) {
  181. std::vector<std::string> headers = base::SplitString(
  182. FLAGS_headers, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  183. std::vector<android::String16> and_headers;
  184. for (const auto& header : headers) {
  185. and_headers.push_back(android::String16{header.data(), header.size()});
  186. }
  187. Status status = service_->applyPayload(
  188. android::String16{FLAGS_payload.data(), FLAGS_payload.size()},
  189. FLAGS_offset,
  190. FLAGS_size,
  191. and_headers);
  192. if (!status.isOk())
  193. return ExitWhenIdle(status);
  194. }
  195. if (!keep_running)
  196. return ExitWhenIdle(EX_OK);
  197. // When following updates status changes, exit if the update_engine daemon
  198. // dies.
  199. android::BinderWrapper::Create();
  200. android::BinderWrapper::Get()->RegisterForDeathNotifications(
  201. android::os::IUpdateEngine::asBinder(service_),
  202. base::Bind(&UpdateEngineClientAndroid::UpdateEngineServiceDied,
  203. base::Unretained(this)));
  204. return EX_OK;
  205. }
  206. int UpdateEngineClientAndroid::ExitWhenIdle(const Status& status) {
  207. if (status.isOk())
  208. return ExitWhenIdle(EX_OK);
  209. LOG(ERROR) << status.toString8();
  210. return ExitWhenIdle(status.exceptionCode());
  211. }
  212. int UpdateEngineClientAndroid::ExitWhenIdle(int return_code) {
  213. auto delayed_exit = base::Bind(
  214. &Daemon::QuitWithExitCode, base::Unretained(this), return_code);
  215. if (!brillo::MessageLoop::current()->PostTask(delayed_exit))
  216. return 1;
  217. return EX_OK;
  218. }
  219. void UpdateEngineClientAndroid::UpdateEngineServiceDied() {
  220. LOG(ERROR) << "UpdateEngineService died.";
  221. QuitWithExitCode(1);
  222. }
  223. } // namespace internal
  224. } // namespace chromeos_update_engine
  225. int main(int argc, char** argv) {
  226. chromeos_update_engine::internal::UpdateEngineClientAndroid client(argc,
  227. argv);
  228. return client.Run();
  229. }