blob: 21740d811ba0ee4e964dd4349ce2af04e4cabf60 [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 Hassani565331e2019-06-24 14:11:29 -070017#include "update_engine/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
Alex Deymofa78f142016-01-26 21:36:16 -080024#include "update_engine/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
Alex Deymob7ca0962014-10-01 17:58:07 -070044 // Initialize update engine global state but continue if something fails.
Alex Deymofa78f142016-01-26 21:36:16 -080045 // 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 Jiang299128e2016-06-03 17:48:18 -070048 RealSystemState* real_system_state = new RealSystemState();
Alex Deymofa78f142016-01-26 21:36:16 -080049 daemon_state_.reset(real_system_state);
50 LOG_IF(ERROR, !real_system_state->Initialize())
Alex Deymob7ca0962014-10-01 17:58:07 -070051 << "Failed to initialize system state.";
Alex Deymob7ca0962014-10-01 17:58:07 -070052
Alex Deymob7ca0962014-10-01 17:58:07 -070053 // Create the DBus service.
Sen Jiang299128e2016-06-03 17:48:18 -070054 dbus_adaptor_.reset(new UpdateEngineAdaptor(real_system_state));
Alex Deymofa78f142016-01-26 21:36:16 -080055 daemon_state_->AddObserver(dbus_adaptor_.get());
Alex Deymob7ca0962014-10-01 17:58:07 -070056
Amin Hassani565331e2019-06-24 14:11:29 -070057 dbus_adaptor_->RegisterAsync(
58 base::Bind(&DaemonChromeOS::OnDBusRegistered, base::Unretained(this)));
Alex Deymob7ca0962014-10-01 17:58:07 -070059 LOG(INFO) << "Waiting for DBus object to be registered.";
60 return EX_OK;
61}
62
Amin Hassani565331e2019-06-24 14:11:29 -070063void DaemonChromeOS::OnDBusRegistered(bool succeeded) {
Alex Deymob7ca0962014-10-01 17:58:07 -070064 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 Deymofa78f142016-01-26 21:36:16 -080079 daemon_state_->StartUpdater();
Alex Deymob7ca0962014-10-01 17:58:07 -070080}
Alex Deymob7ca0962014-10-01 17:58:07 -070081
82} // namespace chromeos_update_engine