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> |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 24 | #if USE_WEAVE |
| 25 | #include <binderwrapper/binder_wrapper.h> |
| 26 | #endif // USE_WEAVE |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [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 | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 31 | using brillo::MessageLoop; |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 32 | |
| 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 | |
Alex Vakulenko | e119e6a | 2016-01-06 17:13:11 -0800 | [diff] [blame] | 48 | #if USE_WEAVE |
| 49 | android::BinderWrapper::Create(); |
| 50 | binder_watcher_.Init(); |
| 51 | #endif // USE_WEAVE |
| 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 | |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 74 | // Create the DBus service. |
Alex Deymo | a91cc48 | 2016-01-20 16:51:56 -0800 | [diff] [blame] | 75 | dbus_adaptor_.reset(new UpdateEngineAdaptor(real_system_state_.get(), bus)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 76 | update_attempter->set_dbus_adaptor(dbus_adaptor_.get()); |
| 77 | |
| 78 | dbus_adaptor_->RegisterAsync(base::Bind(&UpdateEngineDaemon::OnDBusRegistered, |
| 79 | base::Unretained(this))); |
| 80 | LOG(INFO) << "Waiting for DBus object to be registered."; |
| 81 | return EX_OK; |
| 82 | } |
| 83 | |
| 84 | void UpdateEngineDaemon::OnDBusRegistered(bool succeeded) { |
| 85 | if (!succeeded) { |
| 86 | LOG(ERROR) << "Registering the UpdateEngineAdaptor"; |
| 87 | QuitWithExitCode(1); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | // Take ownership of the service now that everything is initialized. We need |
| 92 | // to this now and not before to avoid exposing a well known DBus service |
| 93 | // path that doesn't have the service it is supposed to implement. |
| 94 | if (!dbus_adaptor_->RequestOwnership()) { |
| 95 | LOG(ERROR) << "Unable to take ownership of the DBus service, is there " |
| 96 | << "other update_engine daemon running?"; |
| 97 | QuitWithExitCode(1); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | // Initiate update checks. |
| 102 | UpdateAttempter* update_attempter = real_system_state_->update_attempter(); |
| 103 | update_attempter->ScheduleUpdates(); |
| 104 | |
| 105 | // Update boot flags after 45 seconds. |
| 106 | MessageLoop::current()->PostDelayedTask( |
| 107 | FROM_HERE, |
| 108 | base::Bind(&UpdateAttempter::UpdateBootFlags, |
| 109 | base::Unretained(update_attempter)), |
| 110 | base::TimeDelta::FromSeconds(45)); |
| 111 | |
| 112 | // Broadcast the update engine status on startup to ensure consistent system |
| 113 | // state on crashes. |
| 114 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 115 | &UpdateAttempter::BroadcastStatus, |
| 116 | base::Unretained(update_attempter))); |
| 117 | |
| 118 | // Run the UpdateEngineStarted() method on |update_attempter|. |
| 119 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 120 | &UpdateAttempter::UpdateEngineStarted, |
| 121 | base::Unretained(update_attempter))); |
| 122 | |
| 123 | LOG(INFO) << "Finished initialization. Now running the loop."; |
| 124 | } |
| 125 | |
| 126 | } // namespace chromeos_update_engine |