1 //
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 //
16 
17 #include "update_engine/cros/daemon_chromeos.h"
18 
19 #include <sysexits.h>
20 
21 #include <base/bind.h>
22 #include <base/location.h>
23 
24 #include "update_engine/cros/real_system_state.h"
25 
26 using brillo::Daemon;
27 using std::unique_ptr;
28 
29 namespace chromeos_update_engine {
30 
CreateInstance()31 unique_ptr<DaemonBase> DaemonBase::CreateInstance() {
32   return std::make_unique<DaemonChromeOS>();
33 }
34 
OnInit()35 int DaemonChromeOS::OnInit() {
36   // Register the |subprocess_| singleton with this Daemon as the signal
37   // handler.
38   subprocess_.Init(this);
39 
40   int exit_code = Daemon::OnInit();
41   if (exit_code != EX_OK)
42     return exit_code;
43 
44   // Initialize update engine global state.
45   // TODO(deymo): Move the initialization to a factory method avoiding the
46   // explicit re-usage of the |bus| instance, shared between D-Bus service and
47   // D-Bus client calls.
48   RealSystemState::SetInstance(&system_state_);
49 
50   // Create the DBus service.
51   dbus_adaptor_.reset(new UpdateEngineAdaptor());
52   SystemState::Get()->update_attempter()->AddObserver(dbus_adaptor_.get());
53 
54   dbus_adaptor_->RegisterAsync(
55       base::Bind(&DaemonChromeOS::OnDBusRegistered, base::Unretained(this)));
56   LOG(INFO) << "Waiting for DBus object to be registered.";
57   return EX_OK;
58 }
59 
OnDBusRegistered(bool succeeded)60 void DaemonChromeOS::OnDBusRegistered(bool succeeded) {
61   if (!succeeded) {
62     LOG(ERROR) << "Registering the UpdateEngineAdaptor";
63     QuitWithExitCode(1);
64     return;
65   }
66 
67   // Take ownership of the service now that everything is initialized. We need
68   // to this now and not before to avoid exposing a well known DBus service
69   // path that doesn't have the service it is supposed to implement.
70   if (!dbus_adaptor_->RequestOwnership()) {
71     LOG(ERROR) << "Unable to take ownership of the DBus service, is there "
72                << "other update_engine daemon running?";
73     QuitWithExitCode(1);
74     return;
75   }
76   SystemState::Get()->update_attempter()->StartUpdater();
77 }
78 
79 }  // namespace chromeos_update_engine
80