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