-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbus_service.h
159 lines (126 loc) · 5.33 KB
/
dbus_service.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CRYPTOHOME_DBUS_SERVICE_H_
#define CRYPTOHOME_DBUS_SERVICE_H_
#include <memory>
#include <brillo/daemons/dbus_daemon.h>
#include "cryptohome/service_userdataauth.h"
#include "cryptohome/userdataauth.h"
namespace cryptohome {
class UserDataAuthDaemon : public brillo::DBusServiceDaemon {
public:
UserDataAuthDaemon()
: DBusServiceDaemon(kUserDataAuthServiceName),
service_(new cryptohome::UserDataAuth()) {}
UserDataAuthDaemon(const UserDataAuthDaemon&) = delete;
UserDataAuthDaemon& operator=(const UserDataAuthDaemon&) = delete;
// Retrieve the UserDataAuth object, it holds the service's state and provides
// a good chunk of functionality.
UserDataAuth* GetUserDataAuth() { return service_.get(); }
protected:
void OnShutdown(int* exit_code) override {
// We need to cleanup the mount thread dbus, if any.
base::WaitableEvent on_cleanup_done;
service_->PostTaskToMountThread(
FROM_HERE,
base::Bind(&UserDataAuthDaemon::CleanupMountThreadDBus,
base::Unretained(this), base::Unretained(&on_cleanup_done)));
on_cleanup_done.Wait();
brillo::DBusServiceDaemon::OnShutdown(exit_code);
}
void RegisterDBusObjectsAsync(
brillo::dbus_utils::AsyncEventSequencer* sequencer) override {
// Initialize the UserDataAuth service.
// Note that the initialization should be done after setting the options.
CHECK(service_->Initialize());
service_->set_dbus(bus_);
service_->PostTaskToMountThread(
FROM_HERE,
base::Bind(&UserDataAuthDaemon::CreateMountThreadDBus,
base::Unretained(this),
sequencer->GetHandler("Failed to create dbus connection for "
"UserDataAuth's mount thread",
true)));
DCHECK(!dbus_object_);
dbus_object_ = std::make_unique<brillo::dbus_utils::DBusObject>(
nullptr, bus_, dbus::ObjectPath(kUserDataAuthServicePath));
userdataauth_adaptor_.reset(
new UserDataAuthAdaptor(bus_, dbus_object_.get(), service_.get()));
userdataauth_adaptor_->RegisterAsync();
arc_quota_adaptor_.reset(
new ArcQuotaAdaptor(bus_, dbus_object_.get(), service_.get()));
arc_quota_adaptor_->RegisterAsync();
pkcs11_adaptor_.reset(
new Pkcs11Adaptor(bus_, dbus_object_.get(), service_.get()));
pkcs11_adaptor_->RegisterAsync();
install_attributes_adaptor_.reset(
new InstallAttributesAdaptor(bus_, dbus_object_.get(), service_.get()));
install_attributes_adaptor_->RegisterAsync();
misc_adaptor_.reset(
new CryptohomeMiscAdaptor(bus_, dbus_object_.get(), service_.get()));
misc_adaptor_->RegisterAsync();
service_->PostDBusInitialize();
dbus_object_->RegisterAsync(
sequencer->GetHandler("RegisterAsync() for UserDataAuth failed", true));
}
private:
std::unique_ptr<UserDataAuthAdaptor> userdataauth_adaptor_;
std::unique_ptr<ArcQuotaAdaptor> arc_quota_adaptor_;
std::unique_ptr<Pkcs11Adaptor> pkcs11_adaptor_;
std::unique_ptr<InstallAttributesAdaptor> install_attributes_adaptor_;
std::unique_ptr<CryptohomeMiscAdaptor> misc_adaptor_;
std::unique_ptr<UserDataAuth> service_;
std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_;
// The dbus connection whose origin thread is UserDataAuth's mount thread.
std::unique_ptr<brillo::DBusConnection> mount_thread_bus_connection_;
scoped_refptr<::dbus::Bus> mount_thread_bus_;
// This create a dbus connection whose origin thread is UserDataAuth's mount
// thread.
void CreateMountThreadDBus(
brillo::dbus_utils::AsyncEventSequencer::Handler on_done) {
// This should be run on UserDataAuth's Mount Thread.
service_->AssertOnMountThread();
// This shouldn't be called twice.
DCHECK(!mount_thread_bus_connection_);
DCHECK(!mount_thread_bus_);
// Setup the connection.
mount_thread_bus_connection_.reset(new brillo::DBusConnection);
mount_thread_bus_ = mount_thread_bus_connection_->Connect();
if (!mount_thread_bus_) {
// Failed to create the mount thread dbus.
LOG(WARNING) << "Failed to connect to dbus on UserDataAuth mount thread.";
// Run the handler back on origin thread.
service_->PostTaskToOriginThread(
FROM_HERE,
base::Bind(
[](brillo::dbus_utils::AsyncEventSequencer::Handler on_done) {
on_done.Run(false);
},
on_done));
return;
}
service_->set_mount_thread_dbus(mount_thread_bus_);
// Run the handler back on origin thread.
service_->PostTaskToOriginThread(
FROM_HERE,
base::Bind(
[](brillo::dbus_utils::AsyncEventSequencer::Handler on_done) {
on_done.Run(true);
},
on_done));
}
void CleanupMountThreadDBus(base::WaitableEvent* on_done) {
if (mount_thread_bus_) {
mount_thread_bus_->ShutdownAndBlock();
service_->set_mount_thread_dbus(nullptr);
mount_thread_bus_.reset();
}
if (mount_thread_bus_connection_) {
mount_thread_bus_connection_.reset();
}
on_done->Signal();
}
};
} // namespace cryptohome
#endif // CRYPTOHOME_DBUS_SERVICE_H_