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