blob: f370564edd2d279bd5a59d07890396ba57bf116c [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
17#include "update_engine/daemon.h"
18
19#include <sysexits.h>
20
21#include <base/bind.h>
22#include <base/location.h>
Alex Deymodc066f12017-03-27 13:01:29 -070023#if USE_BINDER
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080024#include <binderwrapper/binder_wrapper.h>
Alex Deymodc066f12017-03-27 13:01:29 -070025#endif // USE_BINDER
Alex Deymob7ca0962014-10-01 17:58:07 -070026
Sen Jiang192a86a2016-05-19 17:21:24 -070027#if USE_OMAHA
Alex Deymofa78f142016-01-26 21:36:16 -080028#include "update_engine/real_system_state.h"
Sen Jiang192a86a2016-05-19 17:21:24 -070029#else // !USE_OMAHA
Alex Deymofa78f142016-01-26 21:36:16 -080030#include "update_engine/daemon_state_android.h"
Sen Jiang192a86a2016-05-19 17:21:24 -070031#endif // USE_OMAHA
Alex Deymob7ca0962014-10-01 17:58:07 -070032
Alex Deymob7ca0962014-10-01 17:58:07 -070033namespace chromeos_update_engine {
34
Alex Deymob7ca0962014-10-01 17:58:07 -070035int UpdateEngineDaemon::OnInit() {
36 // 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 Deymodc066f12017-03-27 13:01:29 -070044#if USE_BINDER
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080045 android::BinderWrapper::Create();
46 binder_watcher_.Init();
Alex Deymodc066f12017-03-27 13:01:29 -070047#endif // USE_BINDER
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080048
Sen Jiang192a86a2016-05-19 17:21:24 -070049#if USE_OMAHA
Alex Deymob7ca0962014-10-01 17:58:07 -070050 // Initialize update engine global state but continue if something fails.
Alex Deymofa78f142016-01-26 21:36:16 -080051 // TODO(deymo): Move the daemon_state_ initialization to a factory method
52 // avoiding the explicit re-usage of the |bus| instance, shared between
53 // D-Bus service and D-Bus client calls.
Sen Jiang299128e2016-06-03 17:48:18 -070054 RealSystemState* real_system_state = new RealSystemState();
Alex Deymofa78f142016-01-26 21:36:16 -080055 daemon_state_.reset(real_system_state);
56 LOG_IF(ERROR, !real_system_state->Initialize())
Alex Deymob7ca0962014-10-01 17:58:07 -070057 << "Failed to initialize system state.";
Sen Jiang192a86a2016-05-19 17:21:24 -070058#else // !USE_OMAHA
Alex Deymofa78f142016-01-26 21:36:16 -080059 DaemonStateAndroid* daemon_state_android = new DaemonStateAndroid();
60 daemon_state_.reset(daemon_state_android);
61 LOG_IF(ERROR, !daemon_state_android->Initialize())
62 << "Failed to initialize system state.";
Sen Jiang192a86a2016-05-19 17:21:24 -070063#endif // USE_OMAHA
Alex Deymob7ca0962014-10-01 17:58:07 -070064
Casey Dahlina93cd532016-01-14 16:55:11 -080065#if USE_BINDER
Alex Deymoe97b39c2016-01-20 13:22:17 -080066 // Create the Binder Service.
Alex Deymof8bfcff2016-02-02 21:22:11 -080067 binder_service_ = new BinderUpdateEngineAndroidService{
68 daemon_state_android->service_delegate()};
Casey Dahlina93cd532016-01-14 16:55:11 -080069 auto binder_wrapper = android::BinderWrapper::Get();
Alex Deymofa78f142016-01-26 21:36:16 -080070 if (!binder_wrapper->RegisterService(binder_service_->ServiceName(),
71 binder_service_)) {
Casey Dahlina93cd532016-01-14 16:55:11 -080072 LOG(ERROR) << "Failed to register binder service.";
73 }
Casey Dahlin40892492016-01-25 16:55:28 -080074
Alex Deymofa78f142016-01-26 21:36:16 -080075 daemon_state_->AddObserver(binder_service_.get());
Casey Dahlina93cd532016-01-14 16:55:11 -080076#endif // USE_BINDER
77
Alex Deymoe97b39c2016-01-20 13:22:17 -080078#if USE_DBUS
Alex Deymob7ca0962014-10-01 17:58:07 -070079 // Create the DBus service.
Sen Jiang299128e2016-06-03 17:48:18 -070080 dbus_adaptor_.reset(new UpdateEngineAdaptor(real_system_state));
Alex Deymofa78f142016-01-26 21:36:16 -080081 daemon_state_->AddObserver(dbus_adaptor_.get());
Alex Deymob7ca0962014-10-01 17:58:07 -070082
83 dbus_adaptor_->RegisterAsync(base::Bind(&UpdateEngineDaemon::OnDBusRegistered,
84 base::Unretained(this)));
85 LOG(INFO) << "Waiting for DBus object to be registered.";
Amin Hassani7cc8bb02019-01-14 16:29:47 -080086#else // !USE_DBUS
Alex Deymofa78f142016-01-26 21:36:16 -080087 daemon_state_->StartUpdater();
88#endif // USE_DBUS
Alex Deymob7ca0962014-10-01 17:58:07 -070089 return EX_OK;
90}
91
Alex Deymoe97b39c2016-01-20 13:22:17 -080092#if USE_DBUS
Alex Deymob7ca0962014-10-01 17:58:07 -070093void UpdateEngineDaemon::OnDBusRegistered(bool succeeded) {
94 if (!succeeded) {
95 LOG(ERROR) << "Registering the UpdateEngineAdaptor";
96 QuitWithExitCode(1);
97 return;
98 }
99
100 // Take ownership of the service now that everything is initialized. We need
101 // to this now and not before to avoid exposing a well known DBus service
102 // path that doesn't have the service it is supposed to implement.
103 if (!dbus_adaptor_->RequestOwnership()) {
104 LOG(ERROR) << "Unable to take ownership of the DBus service, is there "
105 << "other update_engine daemon running?";
106 QuitWithExitCode(1);
107 return;
108 }
Alex Deymofa78f142016-01-26 21:36:16 -0800109 daemon_state_->StartUpdater();
Alex Deymob7ca0962014-10-01 17:58:07 -0700110}
Alex Deymoe97b39c2016-01-20 13:22:17 -0800111#endif // USE_DBUS
Alex Deymob7ca0962014-10-01 17:58:07 -0700112
113} // namespace chromeos_update_engine