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 | |
Amin Hassani | 565331e | 2019-06-24 14:11:29 -0700 | [diff] [blame] | 17 | #include "update_engine/daemon_chromeos.h" |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 18 | |
| 19 | #include <sysexits.h> |
| 20 | |
| 21 | #include <base/bind.h> |
| 22 | #include <base/location.h> |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 23 | |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 24 | #include "update_engine/real_system_state.h" |
Amin Hassani | 565331e | 2019-06-24 14:11:29 -0700 | [diff] [blame] | 25 | |
| 26 | using brillo::Daemon; |
| 27 | using std::unique_ptr; |
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 | namespace chromeos_update_engine { |
| 30 | |
Amin Hassani | 565331e | 2019-06-24 14:11:29 -0700 | [diff] [blame] | 31 | unique_ptr<DaemonBase> DaemonBase::CreateInstance() { |
| 32 | return std::make_unique<DaemonChromeOS>(); |
| 33 | } |
| 34 | |
| 35 | int DaemonChromeOS::OnInit() { |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 36 | // Register the |subprocess_| singleton with this Daemon as the signal |
| 37 | // handler. |
| 38 | subprocess_.Init(this); |
| 39 | |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 40 | int exit_code = Daemon::OnInit(); |
| 41 | if (exit_code != EX_OK) |
| 42 | return exit_code; |
| 43 | |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 44 | // Initialize update engine global state but continue if something fails. |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 45 | // TODO(deymo): Move the daemon_state_ initialization to a factory method |
| 46 | // avoiding the explicit re-usage of the |bus| instance, shared between |
| 47 | // D-Bus service and D-Bus client calls. |
Sen Jiang | 299128e | 2016-06-03 17:48:18 -0700 | [diff] [blame] | 48 | RealSystemState* real_system_state = new RealSystemState(); |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 49 | daemon_state_.reset(real_system_state); |
| 50 | LOG_IF(ERROR, !real_system_state->Initialize()) |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 51 | << "Failed to initialize system state."; |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 52 | |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 53 | // Create the DBus service. |
Sen Jiang | 299128e | 2016-06-03 17:48:18 -0700 | [diff] [blame] | 54 | dbus_adaptor_.reset(new UpdateEngineAdaptor(real_system_state)); |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 55 | daemon_state_->AddObserver(dbus_adaptor_.get()); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 56 | |
Amin Hassani | 565331e | 2019-06-24 14:11:29 -0700 | [diff] [blame] | 57 | dbus_adaptor_->RegisterAsync( |
| 58 | base::Bind(&DaemonChromeOS::OnDBusRegistered, base::Unretained(this))); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 59 | LOG(INFO) << "Waiting for DBus object to be registered."; |
| 60 | return EX_OK; |
| 61 | } |
| 62 | |
Amin Hassani | 565331e | 2019-06-24 14:11:29 -0700 | [diff] [blame] | 63 | void DaemonChromeOS::OnDBusRegistered(bool succeeded) { |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 64 | if (!succeeded) { |
| 65 | LOG(ERROR) << "Registering the UpdateEngineAdaptor"; |
| 66 | QuitWithExitCode(1); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | // Take ownership of the service now that everything is initialized. We need |
| 71 | // to this now and not before to avoid exposing a well known DBus service |
| 72 | // path that doesn't have the service it is supposed to implement. |
| 73 | if (!dbus_adaptor_->RequestOwnership()) { |
| 74 | LOG(ERROR) << "Unable to take ownership of the DBus service, is there " |
| 75 | << "other update_engine daemon running?"; |
| 76 | QuitWithExitCode(1); |
| 77 | return; |
| 78 | } |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 79 | daemon_state_->StartUpdater(); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 80 | } |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 81 | |
| 82 | } // namespace chromeos_update_engine |