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> |
| 24 | #include <chromeos/message_loops/message_loop.h> |
| 25 | |
| 26 | #include "update_engine/clock.h" |
| 27 | #include "update_engine/update_attempter.h" |
| 28 | |
| 29 | using chromeos::MessageLoop; |
| 30 | |
| 31 | namespace { |
| 32 | const int kDBusSystemMaxWaitSeconds = 2 * 60; |
| 33 | } // namespace |
| 34 | |
| 35 | namespace chromeos_update_engine { |
| 36 | |
| 37 | namespace { |
| 38 | // Wait for passed |bus| DBus to be connected by attempting to connect it up to |
| 39 | // |timeout| time. Returns whether the Connect() eventually succeeded. |
| 40 | bool WaitForDBusSystem(dbus::Bus* bus, base::TimeDelta timeout) { |
| 41 | Clock clock; |
| 42 | base::Time deadline = clock.GetMonotonicTime() + timeout; |
| 43 | |
| 44 | while (clock.GetMonotonicTime() < deadline) { |
| 45 | if (bus->Connect()) |
| 46 | return true; |
| 47 | LOG(WARNING) << "Failed to get system bus, waiting."; |
| 48 | // Wait 1 second. |
| 49 | sleep(1); |
| 50 | } |
| 51 | LOG(ERROR) << "Failed to get system bus after " << timeout.InSeconds() |
| 52 | << " seconds."; |
| 53 | return false; |
| 54 | } |
| 55 | } // namespace |
| 56 | |
| 57 | int UpdateEngineDaemon::OnInit() { |
| 58 | // Register the |subprocess_| singleton with this Daemon as the signal |
| 59 | // handler. |
| 60 | subprocess_.Init(this); |
| 61 | |
| 62 | // We use Daemon::OnInit() and not DBusDaemon::OnInit() to gracefully wait for |
| 63 | // the D-Bus connection for up two minutes to avoid re-spawning the daemon |
| 64 | // too fast causing thrashing if dbus-daemon is not running. |
| 65 | int exit_code = Daemon::OnInit(); |
| 66 | if (exit_code != EX_OK) |
| 67 | return exit_code; |
| 68 | |
| 69 | dbus::Bus::Options options; |
| 70 | options.bus_type = dbus::Bus::SYSTEM; |
| 71 | bus_ = new dbus::Bus(options); |
| 72 | |
| 73 | // Wait for DBus to be ready and exit if it doesn't become available after |
| 74 | // the timeout. |
| 75 | if (!WaitForDBusSystem( |
| 76 | bus_.get(), |
| 77 | base::TimeDelta::FromSeconds(kDBusSystemMaxWaitSeconds))) { |
| 78 | // TODO(deymo): Make it possible to run update_engine even if dbus-daemon |
| 79 | // is not running or constantly crashing. |
| 80 | LOG(ERROR) << "Failed to initialize DBus, aborting."; |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | CHECK(bus_->SetUpAsyncOperations()); |
| 85 | |
| 86 | // Initialize update engine global state but continue if something fails. |
| 87 | real_system_state_.reset(new RealSystemState(bus_)); |
| 88 | LOG_IF(ERROR, !real_system_state_->Initialize()) |
| 89 | << "Failed to initialize system state."; |
| 90 | UpdateAttempter* update_attempter = real_system_state_->update_attempter(); |
| 91 | CHECK(update_attempter); |
| 92 | |
| 93 | // Sets static members for the certificate checker. |
| 94 | CertificateChecker::set_system_state(real_system_state_.get()); |
| 95 | CertificateChecker::set_openssl_wrapper(&openssl_wrapper_); |
| 96 | |
| 97 | // Create the DBus service. |
| 98 | dbus_adaptor_.reset(new UpdateEngineAdaptor(real_system_state_.get(), bus_)); |
| 99 | update_attempter->set_dbus_adaptor(dbus_adaptor_.get()); |
| 100 | |
| 101 | dbus_adaptor_->RegisterAsync(base::Bind(&UpdateEngineDaemon::OnDBusRegistered, |
| 102 | base::Unretained(this))); |
| 103 | LOG(INFO) << "Waiting for DBus object to be registered."; |
| 104 | return EX_OK; |
| 105 | } |
| 106 | |
| 107 | void UpdateEngineDaemon::OnDBusRegistered(bool succeeded) { |
| 108 | if (!succeeded) { |
| 109 | LOG(ERROR) << "Registering the UpdateEngineAdaptor"; |
| 110 | QuitWithExitCode(1); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | // Take ownership of the service now that everything is initialized. We need |
| 115 | // to this now and not before to avoid exposing a well known DBus service |
| 116 | // path that doesn't have the service it is supposed to implement. |
| 117 | if (!dbus_adaptor_->RequestOwnership()) { |
| 118 | LOG(ERROR) << "Unable to take ownership of the DBus service, is there " |
| 119 | << "other update_engine daemon running?"; |
| 120 | QuitWithExitCode(1); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | // Initiate update checks. |
| 125 | UpdateAttempter* update_attempter = real_system_state_->update_attempter(); |
| 126 | update_attempter->ScheduleUpdates(); |
| 127 | |
| 128 | // Update boot flags after 45 seconds. |
| 129 | MessageLoop::current()->PostDelayedTask( |
| 130 | FROM_HERE, |
| 131 | base::Bind(&UpdateAttempter::UpdateBootFlags, |
| 132 | base::Unretained(update_attempter)), |
| 133 | base::TimeDelta::FromSeconds(45)); |
| 134 | |
| 135 | // Broadcast the update engine status on startup to ensure consistent system |
| 136 | // state on crashes. |
| 137 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 138 | &UpdateAttempter::BroadcastStatus, |
| 139 | base::Unretained(update_attempter))); |
| 140 | |
| 141 | // Run the UpdateEngineStarted() method on |update_attempter|. |
| 142 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 143 | &UpdateAttempter::UpdateEngineStarted, |
| 144 | base::Unretained(update_attempter))); |
| 145 | |
| 146 | LOG(INFO) << "Finished initialization. Now running the loop."; |
| 147 | } |
| 148 | |
| 149 | } // namespace chromeos_update_engine |