Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 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 | // |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 16 | |
| 17 | #include "update_engine/daemon.h" |
| 18 | |
| 19 | #include <sysexits.h> |
| 20 | |
| 21 | #include <base/bind.h> |
| 22 | #include <base/location.h> |
| 23 | #include <base/time/time.h> |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 24 | #if USE_WEAVE || USE_BINDER |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 25 | #include <binderwrapper/binder_wrapper.h> |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 26 | #endif // USE_WEAVE || USE_BINDER |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 27 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 28 | #if defined(__BRILLO__) || defined(__CHROMEOS__) |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 29 | #include "update_engine/real_system_state.h" |
| 30 | #else // !(defined(__BRILLO__) || defined(__CHROMEOS__)) |
| 31 | #include "update_engine/daemon_state_android.h" |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 32 | #endif // defined(__BRILLO__) || defined(__CHROMEOS__) |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 33 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 34 | #if USE_DBUS |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 35 | namespace { |
| 36 | const int kDBusSystemMaxWaitSeconds = 2 * 60; |
| 37 | } // namespace |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 38 | #endif // USE_DBUS |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 39 | |
| 40 | namespace chromeos_update_engine { |
| 41 | |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 42 | int UpdateEngineDaemon::OnInit() { |
| 43 | // Register the |subprocess_| singleton with this Daemon as the signal |
| 44 | // handler. |
| 45 | subprocess_.Init(this); |
| 46 | |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 47 | int exit_code = Daemon::OnInit(); |
| 48 | if (exit_code != EX_OK) |
| 49 | return exit_code; |
| 50 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 51 | #if USE_WEAVE || USE_BINDER |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 52 | android::BinderWrapper::Create(); |
| 53 | binder_watcher_.Init(); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 54 | #endif // USE_WEAVE || USE_BINDER |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 55 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 56 | #if USE_DBUS |
Alex Deymo | a91cc48 | 2016-01-20 16:51:56 -0800 | [diff] [blame] | 57 | // We wait for the D-Bus connection for up two minutes to avoid re-spawning |
| 58 | // the daemon too fast causing thrashing if dbus-daemon is not running. |
| 59 | scoped_refptr<dbus::Bus> bus = dbus_connection_.ConnectWithTimeout( |
| 60 | base::TimeDelta::FromSeconds(kDBusSystemMaxWaitSeconds)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 61 | |
Alex Deymo | a91cc48 | 2016-01-20 16:51:56 -0800 | [diff] [blame] | 62 | if (!bus) { |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 63 | // TODO(deymo): Make it possible to run update_engine even if dbus-daemon |
| 64 | // is not running or constantly crashing. |
| 65 | LOG(ERROR) << "Failed to initialize DBus, aborting."; |
| 66 | return 1; |
| 67 | } |
| 68 | |
Alex Deymo | a91cc48 | 2016-01-20 16:51:56 -0800 | [diff] [blame] | 69 | CHECK(bus->SetUpAsyncOperations()); |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 70 | #endif // USE_DBUS |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 71 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 72 | #if defined(__BRILLO__) || defined(__CHROMEOS__) |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 73 | // Initialize update engine global state but continue if something fails. |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 74 | // TODO(deymo): Move the daemon_state_ initialization to a factory method |
| 75 | // avoiding the explicit re-usage of the |bus| instance, shared between |
| 76 | // D-Bus service and D-Bus client calls. |
| 77 | RealSystemState* real_system_state = new RealSystemState(bus); |
| 78 | daemon_state_.reset(real_system_state); |
| 79 | LOG_IF(ERROR, !real_system_state->Initialize()) |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 80 | << "Failed to initialize system state."; |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 81 | #else // !(defined(__BRILLO__) || defined(__CHROMEOS__)) |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 82 | DaemonStateAndroid* daemon_state_android = new DaemonStateAndroid(); |
| 83 | daemon_state_.reset(daemon_state_android); |
| 84 | LOG_IF(ERROR, !daemon_state_android->Initialize()) |
| 85 | << "Failed to initialize system state."; |
| 86 | #endif // defined(__BRILLO__) || defined(__CHROMEOS__) |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 87 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 88 | #if USE_BINDER |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 89 | // Create the Binder Service. |
| 90 | #if defined(__BRILLO__) || defined(__CHROMEOS__) |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 91 | binder_service_ = new BinderUpdateEngineBrilloService{real_system_state}; |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 92 | #else // !(defined(__BRILLO__) || defined(__CHROMEOS__)) |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 93 | binder_service_ = new BinderUpdateEngineAndroidService{ |
| 94 | daemon_state_android->service_delegate()}; |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 95 | #endif // defined(__BRILLO__) || defined(__CHROMEOS__) |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 96 | auto binder_wrapper = android::BinderWrapper::Get(); |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 97 | if (!binder_wrapper->RegisterService(binder_service_->ServiceName(), |
| 98 | binder_service_)) { |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 99 | LOG(ERROR) << "Failed to register binder service."; |
| 100 | } |
Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 101 | |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 102 | daemon_state_->AddObserver(binder_service_.get()); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 103 | #endif // USE_BINDER |
| 104 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 105 | #if USE_DBUS |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 106 | // Create the DBus service. |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 107 | dbus_adaptor_.reset(new UpdateEngineAdaptor(real_system_state, bus)); |
| 108 | daemon_state_->AddObserver(dbus_adaptor_.get()); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 109 | |
| 110 | dbus_adaptor_->RegisterAsync(base::Bind(&UpdateEngineDaemon::OnDBusRegistered, |
| 111 | base::Unretained(this))); |
| 112 | LOG(INFO) << "Waiting for DBus object to be registered."; |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 113 | #else // !USE_DBUS |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 114 | daemon_state_->StartUpdater(); |
| 115 | #endif // USE_DBUS |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 116 | return EX_OK; |
| 117 | } |
| 118 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 119 | #if USE_DBUS |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 120 | void UpdateEngineDaemon::OnDBusRegistered(bool succeeded) { |
| 121 | if (!succeeded) { |
| 122 | LOG(ERROR) << "Registering the UpdateEngineAdaptor"; |
| 123 | QuitWithExitCode(1); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | // Take ownership of the service now that everything is initialized. We need |
| 128 | // to this now and not before to avoid exposing a well known DBus service |
| 129 | // path that doesn't have the service it is supposed to implement. |
| 130 | if (!dbus_adaptor_->RequestOwnership()) { |
| 131 | LOG(ERROR) << "Unable to take ownership of the DBus service, is there " |
| 132 | << "other update_engine daemon running?"; |
| 133 | QuitWithExitCode(1); |
| 134 | return; |
| 135 | } |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 136 | daemon_state_->StartUpdater(); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 137 | } |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 138 | #endif // USE_DBUS |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 139 | |
| 140 | } // namespace chromeos_update_engine |