daemon.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Copyright (C) 2015 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 "update_engine/daemon.h"
  17. #include <sysexits.h>
  18. #include <base/bind.h>
  19. #include <base/location.h>
  20. #if USE_BINDER
  21. #include <binderwrapper/binder_wrapper.h>
  22. #endif // USE_BINDER
  23. #if USE_OMAHA
  24. #include "update_engine/real_system_state.h"
  25. #else // !USE_OMAHA
  26. #include "update_engine/daemon_state_android.h"
  27. #endif // USE_OMAHA
  28. namespace chromeos_update_engine {
  29. int UpdateEngineDaemon::OnInit() {
  30. // Register the |subprocess_| singleton with this Daemon as the signal
  31. // handler.
  32. subprocess_.Init(this);
  33. int exit_code = Daemon::OnInit();
  34. if (exit_code != EX_OK)
  35. return exit_code;
  36. #if USE_BINDER
  37. android::BinderWrapper::Create();
  38. binder_watcher_.Init();
  39. #endif // USE_BINDER
  40. #if USE_OMAHA
  41. // Initialize update engine global state but continue if something fails.
  42. // TODO(deymo): Move the daemon_state_ initialization to a factory method
  43. // avoiding the explicit re-usage of the |bus| instance, shared between
  44. // D-Bus service and D-Bus client calls.
  45. RealSystemState* real_system_state = new RealSystemState();
  46. daemon_state_.reset(real_system_state);
  47. LOG_IF(ERROR, !real_system_state->Initialize())
  48. << "Failed to initialize system state.";
  49. #else // !USE_OMAHA
  50. DaemonStateAndroid* daemon_state_android = new DaemonStateAndroid();
  51. daemon_state_.reset(daemon_state_android);
  52. LOG_IF(ERROR, !daemon_state_android->Initialize())
  53. << "Failed to initialize system state.";
  54. #endif // USE_OMAHA
  55. #if USE_BINDER
  56. // Create the Binder Service.
  57. #if USE_OMAHA
  58. binder_service_ = new BinderUpdateEngineBrilloService{real_system_state};
  59. #else // !USE_OMAHA
  60. binder_service_ = new BinderUpdateEngineAndroidService{
  61. daemon_state_android->service_delegate()};
  62. #endif // USE_OMAHA
  63. auto binder_wrapper = android::BinderWrapper::Get();
  64. if (!binder_wrapper->RegisterService(binder_service_->ServiceName(),
  65. binder_service_)) {
  66. LOG(ERROR) << "Failed to register binder service.";
  67. }
  68. daemon_state_->AddObserver(binder_service_.get());
  69. #endif // USE_BINDER
  70. #if USE_DBUS
  71. // Create the DBus service.
  72. dbus_adaptor_.reset(new UpdateEngineAdaptor(real_system_state));
  73. daemon_state_->AddObserver(dbus_adaptor_.get());
  74. dbus_adaptor_->RegisterAsync(base::Bind(&UpdateEngineDaemon::OnDBusRegistered,
  75. base::Unretained(this)));
  76. LOG(INFO) << "Waiting for DBus object to be registered.";
  77. #else // !USE_DBUS
  78. daemon_state_->StartUpdater();
  79. #endif // USE_DBUS
  80. return EX_OK;
  81. }
  82. #if USE_DBUS
  83. void UpdateEngineDaemon::OnDBusRegistered(bool succeeded) {
  84. if (!succeeded) {
  85. LOG(ERROR) << "Registering the UpdateEngineAdaptor";
  86. QuitWithExitCode(1);
  87. return;
  88. }
  89. // Take ownership of the service now that everything is initialized. We need
  90. // to this now and not before to avoid exposing a well known DBus service
  91. // path that doesn't have the service it is supposed to implement.
  92. if (!dbus_adaptor_->RequestOwnership()) {
  93. LOG(ERROR) << "Unable to take ownership of the DBus service, is there "
  94. << "other update_engine daemon running?";
  95. QuitWithExitCode(1);
  96. return;
  97. }
  98. daemon_state_->StartUpdater();
  99. }
  100. #endif // USE_DBUS
  101. } // namespace chromeos_update_engine