blob: ece151c1c65a426db67a6c0476c84e44b0d08454 [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>
23#include <base/time/time.h>
Casey Dahlina93cd532016-01-14 16:55:11 -080024#if USE_WEAVE || USE_BINDER
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080025#include <binderwrapper/binder_wrapper.h>
Casey Dahlina93cd532016-01-14 16:55:11 -080026#endif // USE_WEAVE || USE_BINDER
Alex Deymob7ca0962014-10-01 17:58:07 -070027
Alex Deymoe97b39c2016-01-20 13:22:17 -080028#if defined(__BRILLO__) || defined(__CHROMEOS__)
Alex Deymob7ca0962014-10-01 17:58:07 -070029#include "update_engine/update_attempter.h"
Alex Deymoe97b39c2016-01-20 13:22:17 -080030#endif // defined(__BRILLO__) || defined(__CHROMEOS__)
Alex Deymob7ca0962014-10-01 17:58:07 -070031
Alex Deymoe97b39c2016-01-20 13:22:17 -080032#if USE_DBUS
Alex Deymob7ca0962014-10-01 17:58:07 -070033namespace {
34const int kDBusSystemMaxWaitSeconds = 2 * 60;
35} // namespace
Alex Deymoe97b39c2016-01-20 13:22:17 -080036#endif // USE_DBUS
Alex Deymob7ca0962014-10-01 17:58:07 -070037
38namespace chromeos_update_engine {
39
Alex Deymob7ca0962014-10-01 17:58:07 -070040int UpdateEngineDaemon::OnInit() {
41 // Register the |subprocess_| singleton with this Daemon as the signal
42 // handler.
43 subprocess_.Init(this);
44
Alex Deymob7ca0962014-10-01 17:58:07 -070045 int exit_code = Daemon::OnInit();
46 if (exit_code != EX_OK)
47 return exit_code;
48
Casey Dahlina93cd532016-01-14 16:55:11 -080049#if USE_WEAVE || USE_BINDER
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080050 android::BinderWrapper::Create();
51 binder_watcher_.Init();
Casey Dahlina93cd532016-01-14 16:55:11 -080052#endif // USE_WEAVE || USE_BINDER
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080053
Alex Deymoe97b39c2016-01-20 13:22:17 -080054#if USE_DBUS
Alex Deymoa91cc482016-01-20 16:51:56 -080055 // We wait for the D-Bus connection for up two minutes to avoid re-spawning
56 // the daemon too fast causing thrashing if dbus-daemon is not running.
57 scoped_refptr<dbus::Bus> bus = dbus_connection_.ConnectWithTimeout(
58 base::TimeDelta::FromSeconds(kDBusSystemMaxWaitSeconds));
Alex Deymob7ca0962014-10-01 17:58:07 -070059
Alex Deymoa91cc482016-01-20 16:51:56 -080060 if (!bus) {
Alex Deymob7ca0962014-10-01 17:58:07 -070061 // TODO(deymo): Make it possible to run update_engine even if dbus-daemon
62 // is not running or constantly crashing.
63 LOG(ERROR) << "Failed to initialize DBus, aborting.";
64 return 1;
65 }
66
Alex Deymoa91cc482016-01-20 16:51:56 -080067 CHECK(bus->SetUpAsyncOperations());
Alex Deymoe97b39c2016-01-20 13:22:17 -080068#endif // USE_DBUS
Alex Deymob7ca0962014-10-01 17:58:07 -070069
Alex Deymoe97b39c2016-01-20 13:22:17 -080070#if defined(__BRILLO__) || defined(__CHROMEOS__)
Alex Deymob7ca0962014-10-01 17:58:07 -070071 // Initialize update engine global state but continue if something fails.
Alex Deymoa91cc482016-01-20 16:51:56 -080072 real_system_state_.reset(new RealSystemState(bus));
Alex Deymob7ca0962014-10-01 17:58:07 -070073 LOG_IF(ERROR, !real_system_state_->Initialize())
74 << "Failed to initialize system state.";
75 UpdateAttempter* update_attempter = real_system_state_->update_attempter();
76 CHECK(update_attempter);
Alex Deymoe97b39c2016-01-20 13:22:17 -080077#else // !(defined(__BRILLO__) || defined(__CHROMEOS__))
78 //TODO(deymo): Initialize non-Brillo state.
79#endif // defined(__BRILLO__) || defined(__CHROMEOS__)
Alex Deymob7ca0962014-10-01 17:58:07 -070080
Casey Dahlina93cd532016-01-14 16:55:11 -080081#if USE_BINDER
Alex Deymoe97b39c2016-01-20 13:22:17 -080082 // Create the Binder Service.
83#if defined(__BRILLO__) || defined(__CHROMEOS__)
Casey Dahlina93cd532016-01-14 16:55:11 -080084 service_ = new BinderUpdateEngineService{real_system_state_.get()};
Alex Deymoe97b39c2016-01-20 13:22:17 -080085#else // !(defined(__BRILLO__) || defined(__CHROMEOS__))
86 service_ = new BinderUpdateEngineAndroidService{};
87#endif // defined(__BRILLO__) || defined(__CHROMEOS__)
Casey Dahlina93cd532016-01-14 16:55:11 -080088 auto binder_wrapper = android::BinderWrapper::Get();
Casey Dahlina93cd532016-01-14 16:55:11 -080089 if (!binder_wrapper->RegisterService("android.brillo.UpdateEngineService",
90 service_)) {
91 LOG(ERROR) << "Failed to register binder service.";
92 }
Casey Dahlin40892492016-01-25 16:55:28 -080093
94#if defined(__BRILLO__) || defined(__CHROMEOS__)
95 update_attempter->set_binder_service(service_.get());
96#endif // defined(__BRILLO__) || defined(__CHROMEOS__)
Casey Dahlina93cd532016-01-14 16:55:11 -080097#endif // USE_BINDER
98
Alex Deymoe97b39c2016-01-20 13:22:17 -080099#if USE_DBUS
Alex Deymob7ca0962014-10-01 17:58:07 -0700100 // Create the DBus service.
Alex Deymoa91cc482016-01-20 16:51:56 -0800101 dbus_adaptor_.reset(new UpdateEngineAdaptor(real_system_state_.get(), bus));
Alex Deymob7ca0962014-10-01 17:58:07 -0700102 update_attempter->set_dbus_adaptor(dbus_adaptor_.get());
103
104 dbus_adaptor_->RegisterAsync(base::Bind(&UpdateEngineDaemon::OnDBusRegistered,
105 base::Unretained(this)));
106 LOG(INFO) << "Waiting for DBus object to be registered.";
Alex Deymoe97b39c2016-01-20 13:22:17 -0800107#else // !USE_DBUS
108#if defined(__BRILLO__) || defined(__CHROMEOS__)
109 real_system_state_->StartUpdater();
110#else // !(defined(__BRILLO__) || defined(__CHROMEOS__))
111 // TODO(deymo): Start non-Brillo service.
112#endif // defined(__BRILLO__) || defined(__CHROMEOS__)
113#endif // USE_DBUS
Alex Deymob7ca0962014-10-01 17:58:07 -0700114 return EX_OK;
115}
116
Alex Deymoe97b39c2016-01-20 13:22:17 -0800117#if USE_DBUS
Alex Deymob7ca0962014-10-01 17:58:07 -0700118void UpdateEngineDaemon::OnDBusRegistered(bool succeeded) {
119 if (!succeeded) {
120 LOG(ERROR) << "Registering the UpdateEngineAdaptor";
121 QuitWithExitCode(1);
122 return;
123 }
124
125 // Take ownership of the service now that everything is initialized. We need
126 // to this now and not before to avoid exposing a well known DBus service
127 // path that doesn't have the service it is supposed to implement.
128 if (!dbus_adaptor_->RequestOwnership()) {
129 LOG(ERROR) << "Unable to take ownership of the DBus service, is there "
130 << "other update_engine daemon running?";
131 QuitWithExitCode(1);
132 return;
133 }
Alex Deymoe97b39c2016-01-20 13:22:17 -0800134 real_system_state_->StartUpdater();
Alex Deymob7ca0962014-10-01 17:58:07 -0700135}
Alex Deymoe97b39c2016-01-20 13:22:17 -0800136#endif // USE_DBUS
Alex Deymob7ca0962014-10-01 17:58:07 -0700137
138} // namespace chromeos_update_engine