blob: 366fb9a90fe0aad1676a3aefb02ba25115b2b3fe [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Deymob7ca0962014-10-01 17:58:07 -070016
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/cros/daemon_chromeos.h"
Alex Deymob7ca0962014-10-01 17:58:07 -070018
19#include <sysexits.h>
20
21#include <base/bind.h>
22#include <base/location.h>
Alex Deymob7ca0962014-10-01 17:58:07 -070023
Amin Hassaniec7bc112020-10-29 16:47:58 -070024#include "update_engine/cros/real_system_state.h"
Amin Hassani565331e2019-06-24 14:11:29 -070025
26using brillo::Daemon;
27using std::unique_ptr;
Alex Deymob7ca0962014-10-01 17:58:07 -070028
Alex Deymob7ca0962014-10-01 17:58:07 -070029namespace chromeos_update_engine {
30
Amin Hassani565331e2019-06-24 14:11:29 -070031unique_ptr<DaemonBase> DaemonBase::CreateInstance() {
32 return std::make_unique<DaemonChromeOS>();
33}
34
35int DaemonChromeOS::OnInit() {
Alex Deymob7ca0962014-10-01 17:58:07 -070036 // Register the |subprocess_| singleton with this Daemon as the signal
37 // handler.
38 subprocess_.Init(this);
39
Alex Deymob7ca0962014-10-01 17:58:07 -070040 int exit_code = Daemon::OnInit();
41 if (exit_code != EX_OK)
42 return exit_code;
43
Amin Hassani538bd592020-11-04 20:46:08 -080044 // 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 Hassaniddd68222020-11-30 16:45:59 -080048 RealSystemState::SetInstance(&system_state_);
Alex Deymob7ca0962014-10-01 17:58:07 -070049
Alex Deymob7ca0962014-10-01 17:58:07 -070050 // Create the DBus service.
Amin Hassani538bd592020-11-04 20:46:08 -080051 dbus_adaptor_.reset(new UpdateEngineAdaptor());
52 SystemState::Get()->update_attempter()->AddObserver(dbus_adaptor_.get());
Alex Deymob7ca0962014-10-01 17:58:07 -070053
Amin Hassani565331e2019-06-24 14:11:29 -070054 dbus_adaptor_->RegisterAsync(
55 base::Bind(&DaemonChromeOS::OnDBusRegistered, base::Unretained(this)));
Alex Deymob7ca0962014-10-01 17:58:07 -070056 LOG(INFO) << "Waiting for DBus object to be registered.";
57 return EX_OK;
58}
59
Amin Hassani565331e2019-06-24 14:11:29 -070060void DaemonChromeOS::OnDBusRegistered(bool succeeded) {
Alex Deymob7ca0962014-10-01 17:58:07 -070061 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 Hassani538bd592020-11-04 20:46:08 -080076 SystemState::Get()->update_attempter()->StartUpdater();
Alex Deymob7ca0962014-10-01 17:58:07 -070077}
Alex Deymob7ca0962014-10-01 17:58:07 -070078
79} // namespace chromeos_update_engine