blob: cc528a44cad84561ef8f29060c45e0efcaadcd5d [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 Vakulenkobdd765e2016-01-23 21:07:14 +000027#include <brillo/message_loops/message_loop.h>
Alex Deymob7ca0962014-10-01 17:58:07 -070028
Alex Deymob7ca0962014-10-01 17:58:07 -070029#include "update_engine/update_attempter.h"
30
Alex Vakulenkobdd765e2016-01-23 21:07:14 +000031using brillo::MessageLoop;
32
Alex Deymob7ca0962014-10-01 17:58:07 -070033namespace {
34const int kDBusSystemMaxWaitSeconds = 2 * 60;
35} // namespace
36
37namespace chromeos_update_engine {
38
Alex Deymob7ca0962014-10-01 17:58:07 -070039int UpdateEngineDaemon::OnInit() {
40 // Register the |subprocess_| singleton with this Daemon as the signal
41 // handler.
42 subprocess_.Init(this);
43
Alex Deymob7ca0962014-10-01 17:58:07 -070044 int exit_code = Daemon::OnInit();
45 if (exit_code != EX_OK)
46 return exit_code;
47
Casey Dahlina93cd532016-01-14 16:55:11 -080048#if USE_WEAVE || USE_BINDER
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080049 android::BinderWrapper::Create();
50 binder_watcher_.Init();
Casey Dahlina93cd532016-01-14 16:55:11 -080051#endif // USE_WEAVE || USE_BINDER
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080052
Alex Deymoa91cc482016-01-20 16:51:56 -080053 // We wait for the D-Bus connection for up two minutes to avoid re-spawning
54 // the daemon too fast causing thrashing if dbus-daemon is not running.
55 scoped_refptr<dbus::Bus> bus = dbus_connection_.ConnectWithTimeout(
56 base::TimeDelta::FromSeconds(kDBusSystemMaxWaitSeconds));
Alex Deymob7ca0962014-10-01 17:58:07 -070057
Alex Deymoa91cc482016-01-20 16:51:56 -080058 if (!bus) {
Alex Deymob7ca0962014-10-01 17:58:07 -070059 // TODO(deymo): Make it possible to run update_engine even if dbus-daemon
60 // is not running or constantly crashing.
61 LOG(ERROR) << "Failed to initialize DBus, aborting.";
62 return 1;
63 }
64
Alex Deymoa91cc482016-01-20 16:51:56 -080065 CHECK(bus->SetUpAsyncOperations());
Alex Deymob7ca0962014-10-01 17:58:07 -070066
67 // Initialize update engine global state but continue if something fails.
Alex Deymoa91cc482016-01-20 16:51:56 -080068 real_system_state_.reset(new RealSystemState(bus));
Alex Deymob7ca0962014-10-01 17:58:07 -070069 LOG_IF(ERROR, !real_system_state_->Initialize())
70 << "Failed to initialize system state.";
71 UpdateAttempter* update_attempter = real_system_state_->update_attempter();
72 CHECK(update_attempter);
73
Casey Dahlina93cd532016-01-14 16:55:11 -080074#if USE_BINDER
Alex Vakulenkobdd765e2016-01-23 21:07:14 +000075 // Create the Binder Service
Casey Dahlina93cd532016-01-14 16:55:11 -080076 service_ = new BinderUpdateEngineService{real_system_state_.get()};
77 auto binder_wrapper = android::BinderWrapper::Get();
Alex Vakulenkobdd765e2016-01-23 21:07:14 +000078 sleep(10);
Casey Dahlina93cd532016-01-14 16:55:11 -080079 if (!binder_wrapper->RegisterService("android.brillo.UpdateEngineService",
80 service_)) {
81 LOG(ERROR) << "Failed to register binder service.";
82 }
Alex Vakulenkobdd765e2016-01-23 21:07:14 +000083
Casey Dahlina93cd532016-01-14 16:55:11 -080084#endif // USE_BINDER
85
Alex Deymob7ca0962014-10-01 17:58:07 -070086 // Create the DBus service.
Alex Deymoa91cc482016-01-20 16:51:56 -080087 dbus_adaptor_.reset(new UpdateEngineAdaptor(real_system_state_.get(), bus));
Alex Deymob7ca0962014-10-01 17:58:07 -070088 update_attempter->set_dbus_adaptor(dbus_adaptor_.get());
89
90 dbus_adaptor_->RegisterAsync(base::Bind(&UpdateEngineDaemon::OnDBusRegistered,
91 base::Unretained(this)));
92 LOG(INFO) << "Waiting for DBus object to be registered.";
93 return EX_OK;
94}
95
96void UpdateEngineDaemon::OnDBusRegistered(bool succeeded) {
97 if (!succeeded) {
98 LOG(ERROR) << "Registering the UpdateEngineAdaptor";
99 QuitWithExitCode(1);
100 return;
101 }
102
103 // Take ownership of the service now that everything is initialized. We need
104 // to this now and not before to avoid exposing a well known DBus service
105 // path that doesn't have the service it is supposed to implement.
106 if (!dbus_adaptor_->RequestOwnership()) {
107 LOG(ERROR) << "Unable to take ownership of the DBus service, is there "
108 << "other update_engine daemon running?";
109 QuitWithExitCode(1);
110 return;
111 }
Alex Vakulenkobdd765e2016-01-23 21:07:14 +0000112
113 // Initiate update checks.
114 UpdateAttempter* update_attempter = real_system_state_->update_attempter();
115 update_attempter->ScheduleUpdates();
116
117 // Update boot flags after 45 seconds.
118 MessageLoop::current()->PostDelayedTask(
119 FROM_HERE,
120 base::Bind(&UpdateAttempter::UpdateBootFlags,
121 base::Unretained(update_attempter)),
122 base::TimeDelta::FromSeconds(45));
123
124 // Broadcast the update engine status on startup to ensure consistent system
125 // state on crashes.
126 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
127 &UpdateAttempter::BroadcastStatus,
128 base::Unretained(update_attempter)));
129
130 // Run the UpdateEngineStarted() method on |update_attempter|.
131 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
132 &UpdateAttempter::UpdateEngineStarted,
133 base::Unretained(update_attempter)));
134
135 LOG(INFO) << "Finished initialization. Now running the loop.";
Alex Deymob7ca0962014-10-01 17:58:07 -0700136}
Alex Deymob7ca0962014-10-01 17:58:07 -0700137
138} // namespace chromeos_update_engine