| 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 | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #include "update_engine/cros/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 |  | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 24 | #include "update_engine/cros/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 |  | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 44 | // Initialize update engine global state. | 
|  | 45 | // TODO(deymo): Move the initialization to a factory method avoiding the | 
|  | 46 | // explicit re-usage of the |bus| instance, shared between D-Bus service and | 
|  | 47 | // D-Bus client calls. | 
| Amin Hassani | ddd6822 | 2020-11-30 16:45:59 -0800 | [diff] [blame] | 48 | RealSystemState::SetInstance(&system_state_); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 49 |  | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 50 | // Create the DBus service. | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 51 | dbus_adaptor_.reset(new UpdateEngineAdaptor()); | 
|  | 52 | SystemState::Get()->update_attempter()->AddObserver(dbus_adaptor_.get()); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 53 |  | 
| Amin Hassani | 565331e | 2019-06-24 14:11:29 -0700 | [diff] [blame] | 54 | dbus_adaptor_->RegisterAsync( | 
|  | 55 | base::Bind(&DaemonChromeOS::OnDBusRegistered, base::Unretained(this))); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 56 | LOG(INFO) << "Waiting for DBus object to be registered."; | 
|  | 57 | return EX_OK; | 
|  | 58 | } | 
|  | 59 |  | 
| Amin Hassani | 565331e | 2019-06-24 14:11:29 -0700 | [diff] [blame] | 60 | void DaemonChromeOS::OnDBusRegistered(bool succeeded) { | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 61 | if (!succeeded) { | 
|  | 62 | LOG(ERROR) << "Registering the UpdateEngineAdaptor"; | 
|  | 63 | QuitWithExitCode(1); | 
|  | 64 | return; | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | // Take ownership of the service now that everything is initialized. We need | 
|  | 68 | // to this now and not before to avoid exposing a well known DBus service | 
|  | 69 | // path that doesn't have the service it is supposed to implement. | 
|  | 70 | if (!dbus_adaptor_->RequestOwnership()) { | 
|  | 71 | LOG(ERROR) << "Unable to take ownership of the DBus service, is there " | 
|  | 72 | << "other update_engine daemon running?"; | 
|  | 73 | QuitWithExitCode(1); | 
|  | 74 | return; | 
|  | 75 | } | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 76 | SystemState::Get()->update_attempter()->StartUpdater(); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 77 | } | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 78 |  | 
|  | 79 | }  // namespace chromeos_update_engine |