blob: 3f88a841084258651b760231d85df35196f1a8a0 [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 Dahlina93cd532016-01-14 16:55:11 -080093#endif // USE_BINDER
94
Alex Deymoe97b39c2016-01-20 13:22:17 -080095#if USE_DBUS
Alex Deymob7ca0962014-10-01 17:58:07 -070096 // Create the DBus service.
Alex Deymoa91cc482016-01-20 16:51:56 -080097 dbus_adaptor_.reset(new UpdateEngineAdaptor(real_system_state_.get(), bus));
Alex Deymob7ca0962014-10-01 17:58:07 -070098 update_attempter->set_dbus_adaptor(dbus_adaptor_.get());
99
100 dbus_adaptor_->RegisterAsync(base::Bind(&UpdateEngineDaemon::OnDBusRegistered,
101 base::Unretained(this)));
102 LOG(INFO) << "Waiting for DBus object to be registered.";
Alex Deymoe97b39c2016-01-20 13:22:17 -0800103#else // !USE_DBUS
104#if defined(__BRILLO__) || defined(__CHROMEOS__)
105 real_system_state_->StartUpdater();
106#else // !(defined(__BRILLO__) || defined(__CHROMEOS__))
107 // TODO(deymo): Start non-Brillo service.
108#endif // defined(__BRILLO__) || defined(__CHROMEOS__)
109#endif // USE_DBUS
Alex Deymob7ca0962014-10-01 17:58:07 -0700110 return EX_OK;
111}
112
Alex Deymoe97b39c2016-01-20 13:22:17 -0800113#if USE_DBUS
Alex Deymob7ca0962014-10-01 17:58:07 -0700114void UpdateEngineDaemon::OnDBusRegistered(bool succeeded) {
115 if (!succeeded) {
116 LOG(ERROR) << "Registering the UpdateEngineAdaptor";
117 QuitWithExitCode(1);
118 return;
119 }
120
121 // Take ownership of the service now that everything is initialized. We need
122 // to this now and not before to avoid exposing a well known DBus service
123 // path that doesn't have the service it is supposed to implement.
124 if (!dbus_adaptor_->RequestOwnership()) {
125 LOG(ERROR) << "Unable to take ownership of the DBus service, is there "
126 << "other update_engine daemon running?";
127 QuitWithExitCode(1);
128 return;
129 }
Alex Deymoe97b39c2016-01-20 13:22:17 -0800130 real_system_state_->StartUpdater();
Alex Deymob7ca0962014-10-01 17:58:07 -0700131}
Alex Deymoe97b39c2016-01-20 13:22:17 -0800132#endif // USE_DBUS
Alex Deymob7ca0962014-10-01 17:58:07 -0700133
134} // namespace chromeos_update_engine