blob: d4255913a3e02cf7610ef23d37d9b9e80074ade7 [file] [log] [blame]
Steven Moreland5d5ef7f2016-10-20 19:19:55 -07001/*
2 * Copyright (C) 2016 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#define LOG_TAG "ServiceManagement"
18
Yifan Hong9a22d1d2017-01-25 14:19:26 -080019#include <condition_variable>
20#include <dlfcn.h>
21#include <dirent.h>
22#include <unistd.h>
23
24#include <mutex>
25#include <regex>
26
Martijn Coenen12f04d92016-12-07 17:29:41 +010027#include <hidl/HidlBinderSupport.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070028#include <hidl/ServiceManagement.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070029#include <hidl/Status.h>
30
Steven Moreland337e6b62017-01-18 17:25:13 -080031#include <android-base/logging.h>
Steven Moreland337e6b62017-01-18 17:25:13 -080032#include <hidl-util/FQName.h>
Steven Moreland0091c092017-01-20 23:15:18 +000033#include <hidl-util/StringHelper.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070034#include <hwbinder/IPCThreadState.h>
35#include <hwbinder/Parcel.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070036
37#include <android/hidl/manager/1.0/IServiceManager.h>
Yifan Hong4e925992017-01-09 17:47:17 -080038#include <android/hidl/manager/1.0/BpHwServiceManager.h>
39#include <android/hidl/manager/1.0/BnHwServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070040
Yifan Hong9a22d1d2017-01-25 14:19:26 -080041#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
42#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
43static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
44
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070045using android::hidl::manager::V1_0::IServiceManager;
Steven Moreland337e6b62017-01-18 17:25:13 -080046using android::hidl::manager::V1_0::IServiceNotification;
Yifan Hong4e925992017-01-09 17:47:17 -080047using android::hidl::manager::V1_0::BpHwServiceManager;
48using android::hidl::manager::V1_0::BnHwServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070049
50namespace android {
51namespace hardware {
52
Yifan Hong953e6b02017-03-16 14:52:40 -070053namespace details {
54extern Mutex gDefaultServiceManagerLock;
55extern sp<android::hidl::manager::V1_0::IServiceManager> gDefaultServiceManager;
56} // namespace details
57
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070058sp<IServiceManager> defaultServiceManager() {
59
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070060 {
Yifan Hong953e6b02017-03-16 14:52:40 -070061 AutoMutex _l(details::gDefaultServiceManagerLock);
62 if (details::gDefaultServiceManager != NULL) {
63 return details::gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -070064 }
65 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
66 // HwBinder not available on this device or not accessible to
67 // this process.
68 return nullptr;
69 }
Yifan Hong953e6b02017-03-16 14:52:40 -070070 while (details::gDefaultServiceManager == NULL) {
71 details::gDefaultServiceManager =
Yifan Hong8fb656b2017-03-16 14:30:40 -070072 fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>(
73 ProcessState::self()->getContextObject(NULL));
Yifan Hong953e6b02017-03-16 14:52:40 -070074 if (details::gDefaultServiceManager == NULL) {
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070075 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -070076 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070077 }
78 }
79
Yifan Hong953e6b02017-03-16 14:52:40 -070080 return details::gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070081}
82
Steven Moreland0091c092017-01-20 23:15:18 +000083std::vector<std::string> search(const std::string &path,
84 const std::string &prefix,
85 const std::string &suffix) {
86 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
87 if (!dir) return {};
88
89 std::vector<std::string> results{};
90
91 dirent* dp;
92 while ((dp = readdir(dir.get())) != nullptr) {
93 std::string name = dp->d_name;
94
95 if (StringHelper::StartsWith(name, prefix) &&
96 StringHelper::EndsWith(name, suffix)) {
97 results.push_back(name);
98 }
99 }
100
101 return results;
102}
103
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800104bool matchPackageName(const std::string &lib, std::string *matchedName) {
105 std::smatch match;
106 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
107 *matchedName = match.str(1) + "::I*";
108 return true;
109 }
110 return false;
111}
112
Yifan Hong7f49f592017-02-03 15:11:44 -0800113static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
114 sp<IServiceManager> binderizedManager = defaultServiceManager();
115 if (binderizedManager == nullptr) {
116 LOG(WARNING) << "Could not registerReference for "
117 << interfaceName << "/" << instanceName
118 << ": null binderized manager.";
119 return;
120 }
121 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName, getpid());
122 if (!ret.isOk()) {
123 LOG(WARNING) << "Could not registerReference for "
124 << interfaceName << "/" << instanceName
125 << ": " << ret.description();
126 }
Steven Morelande681aa52017-02-15 16:22:37 -0800127 LOG(VERBOSE) << "Successfully registerReference for "
128 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800129}
130
Steven Moreland337e6b62017-01-18 17:25:13 -0800131struct PassthroughServiceManager : IServiceManager {
132 Return<sp<IBase>> get(const hidl_string& fqName,
133 const hidl_string& name) override {
Steven Moreland348802d2017-02-23 12:48:55 -0800134 const FQName iface(fqName);
Steven Moreland337e6b62017-01-18 17:25:13 -0800135
136 if (!iface.isValid() ||
137 !iface.isFullyQualified() ||
138 iface.isIdentifier()) {
139 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
140 return nullptr;
141 }
142
Steven Moreland348802d2017-02-23 12:48:55 -0800143 const std::string prefix = iface.getPackageAndVersion().string() + "-impl";
144 const std::string sym = "HIDL_FETCH_" + iface.name();
145
Steven Moreland337e6b62017-01-18 17:25:13 -0800146 const int dlMode = RTLD_LAZY;
147 void *handle = nullptr;
148
Steven Moreland0091c092017-01-20 23:15:18 +0000149 // TODO: lookup in VINTF instead
150 // TODO(b/34135607): Remove HAL_LIBRARY_PATH_SYSTEM
151
Steven Morelanda29905c2017-03-01 10:42:35 -0800152 dlerror(); // clear
153
Steven Moreland337e6b62017-01-18 17:25:13 -0800154 for (const std::string &path : {
155 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM
156 }) {
Steven Moreland0091c092017-01-20 23:15:18 +0000157 std::vector<std::string> libs = search(path, prefix, ".so");
158
Steven Moreland0091c092017-01-20 23:15:18 +0000159 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800160 const std::string fullPath = path + lib;
161
162 handle = dlopen(fullPath.c_str(), dlMode);
163 if (handle == nullptr) {
164 const char* error = dlerror();
165 LOG(ERROR) << "Failed to dlopen " << lib << ": "
166 << (error == nullptr ? "unknown error" : error);
167 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000168 }
Steven Moreland348802d2017-02-23 12:48:55 -0800169
170 IBase* (*generator)(const char* name);
171 *(void **)(&generator) = dlsym(handle, sym.c_str());
172 if(!generator) {
173 const char* error = dlerror();
174 LOG(ERROR) << "Passthrough lookup opened " << lib
175 << " but could not find symbol " << sym << ": "
176 << (error == nullptr ? "unknown error" : error);
177 dlclose(handle);
178 continue;
179 }
180
181 IBase *interface = (*generator)(name);
182
183 if (interface == nullptr) {
184 dlclose(handle);
185 continue; // this module doesn't provide this instance name
186 }
187
188 registerReference(fqName, name);
189
190 return interface;
Steven Moreland337e6b62017-01-18 17:25:13 -0800191 }
192 }
193
Steven Moreland348802d2017-02-23 12:48:55 -0800194 return nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800195 }
196
Martijn Coenen67a02492017-03-06 13:04:48 +0100197 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800198 const sp<IBase>& /* service */) override {
199 LOG(FATAL) << "Cannot register services with passthrough service manager.";
200 return false;
201 }
202
Yifan Hong705e5da2017-03-02 16:59:39 -0800203 Return<void> list(list_cb /* _hidl_cb */) override {
204 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800205 return Void();
206 }
207 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
208 listByInterface_cb /* _hidl_cb */) override {
209 // TODO: add this functionality
210 LOG(FATAL) << "Cannot list services with passthrough service manager.";
211 return Void();
212 }
213
214 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
215 const hidl_string& /* name */,
216 const sp<IServiceNotification>& /* callback */) override {
217 // This makes no sense.
218 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
219 return false;
220 }
221
Yifan Hong705e5da2017-03-02 16:59:39 -0800222 Return<void> debugDump(debugDump_cb _hidl_cb) override {
223 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
224 static std::vector<std::pair<Arch, std::vector<const char *>>> sAllPaths{
225 {Arch::IS_64BIT, {HAL_LIBRARY_PATH_ODM_64BIT,
226 HAL_LIBRARY_PATH_VENDOR_64BIT,
227 HAL_LIBRARY_PATH_SYSTEM_64BIT}},
228 {Arch::IS_32BIT, {HAL_LIBRARY_PATH_ODM_32BIT,
229 HAL_LIBRARY_PATH_VENDOR_32BIT,
230 HAL_LIBRARY_PATH_SYSTEM_32BIT}}
231 };
232 std::vector<InstanceDebugInfo> vec;
233 for (const auto &pair : sAllPaths) {
234 Arch arch = pair.first;
235 for (const auto &path : pair.second) {
236 std::vector<std::string> libs = search(path, "", ".so");
237 for (const std::string &lib : libs) {
238 std::string matchedName;
239 if (matchPackageName(lib, &matchedName)) {
240 vec.push_back({
241 .interfaceName = matchedName,
242 .instanceName = "*",
243 .clientPids = {},
244 .arch = arch
245 });
246 }
247 }
248 }
249 }
250 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800251 return Void();
252 }
253
254 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &, int32_t) override {
255 // This makes no sense.
256 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
257 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800258 return Void();
259 }
260
Steven Moreland337e6b62017-01-18 17:25:13 -0800261};
262
263sp<IServiceManager> getPassthroughServiceManager() {
264 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
265 return manager;
266}
267
Steven Morelandcbefd352017-01-23 20:29:05 -0800268namespace details {
269
270struct Waiter : IServiceNotification {
271 Return<void> onRegistration(const hidl_string& /* fqName */,
272 const hidl_string& /* name */,
273 bool /* preexisting */) override {
274 std::unique_lock<std::mutex> lock(mMutex);
275 if (mRegistered) {
276 return Void();
277 }
278 mRegistered = true;
279 lock.unlock();
280
281 mCondition.notify_one();
282 return Void();
283 }
284
285 void wait() {
286 std::unique_lock<std::mutex> lock(mMutex);
287 mCondition.wait(lock, [this]{
288 return mRegistered;
289 });
290 }
291
292private:
293 std::mutex mMutex;
294 std::condition_variable mCondition;
295 bool mRegistered = false;
296};
297
298void waitForHwService(
299 const std::string &interface, const std::string &instanceName) {
300 const sp<IServiceManager> manager = defaultServiceManager();
301
302 if (manager == nullptr) {
303 LOG(ERROR) << "Could not get default service manager.";
304 return;
305 }
306
307 sp<Waiter> waiter = new Waiter();
308 Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter);
309
310 if (!ret.isOk()) {
311 LOG(ERROR) << "Transport error, " << ret.description()
312 << ", during notification registration for "
313 << interface << "/" << instanceName << ".";
314 return;
315 }
316
317 if (!ret) {
318 LOG(ERROR) << "Could not register for notifications for "
319 << interface << "/" << instanceName << ".";
320 return;
321 }
322
323 waiter->wait();
324}
325
326}; // namespace details
327
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700328}; // namespace hardware
329}; // namespace android