blob: f7f217094b2d58833b91d5bad90207c4a0f90ec7 [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>
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080024#if USE_WEAVE
25#include <binderwrapper/binder_wrapper.h>
26#endif // USE_WEAVE
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070027#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 Vakulenko3f39d5c2015-10-13 09:27:13 -070031using brillo::MessageLoop;
Alex Deymob7ca0962014-10-01 17:58:07 -070032
33namespace {
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
Alex Vakulenkoe119e6a2016-01-06 17:13:11 -080048#if USE_WEAVE
49 android::BinderWrapper::Create();
50 binder_watcher_.Init();
51#endif // USE_WEAVE
52
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
Alex Deymob7ca0962014-10-01 17:58:07 -070074 // Create the DBus service.
Alex Deymoa91cc482016-01-20 16:51:56 -080075 dbus_adaptor_.reset(new UpdateEngineAdaptor(real_system_state_.get(), bus));
Alex Deymob7ca0962014-10-01 17:58:07 -070076 update_attempter->set_dbus_adaptor(dbus_adaptor_.get());
77
78 dbus_adaptor_->RegisterAsync(base::Bind(&UpdateEngineDaemon::OnDBusRegistered,
79 base::Unretained(this)));
80 LOG(INFO) << "Waiting for DBus object to be registered.";
81 return EX_OK;
82}
83
84void UpdateEngineDaemon::OnDBusRegistered(bool succeeded) {
85 if (!succeeded) {
86 LOG(ERROR) << "Registering the UpdateEngineAdaptor";
87 QuitWithExitCode(1);
88 return;
89 }
90
91 // Take ownership of the service now that everything is initialized. We need
92 // to this now and not before to avoid exposing a well known DBus service
93 // path that doesn't have the service it is supposed to implement.
94 if (!dbus_adaptor_->RequestOwnership()) {
95 LOG(ERROR) << "Unable to take ownership of the DBus service, is there "
96 << "other update_engine daemon running?";
97 QuitWithExitCode(1);
98 return;
99 }
100
101 // Initiate update checks.
102 UpdateAttempter* update_attempter = real_system_state_->update_attempter();
103 update_attempter->ScheduleUpdates();
104
105 // Update boot flags after 45 seconds.
106 MessageLoop::current()->PostDelayedTask(
107 FROM_HERE,
108 base::Bind(&UpdateAttempter::UpdateBootFlags,
109 base::Unretained(update_attempter)),
110 base::TimeDelta::FromSeconds(45));
111
112 // Broadcast the update engine status on startup to ensure consistent system
113 // state on crashes.
114 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
115 &UpdateAttempter::BroadcastStatus,
116 base::Unretained(update_attempter)));
117
118 // Run the UpdateEngineStarted() method on |update_attempter|.
119 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
120 &UpdateAttempter::UpdateEngineStarted,
121 base::Unretained(update_attempter)));
122
123 LOG(INFO) << "Finished initialization. Now running the loop.";
124}
125
126} // namespace chromeos_update_engine