blob: feb125456969418c81bc8904c5910795d10d1fc3 [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
Jiyong Park3ab546c2017-04-06 20:28:07 +090019#include <android/dlext.h>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080020#include <condition_variable>
21#include <dlfcn.h>
22#include <dirent.h>
Steven Moreland405d7612017-04-07 20:31:22 -070023#include <fstream>
24#include <pthread.h>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080025#include <unistd.h>
26
27#include <mutex>
28#include <regex>
Yifan Hongbd0d8f72017-05-24 19:43:51 -070029#include <set>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080030
Martijn Coenen12f04d92016-12-07 17:29:41 +010031#include <hidl/HidlBinderSupport.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070032#include <hidl/ServiceManagement.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070033#include <hidl/Status.h>
34
Steven Moreland337e6b62017-01-18 17:25:13 -080035#include <android-base/logging.h>
Steven Morelandc1cee2c2017-03-24 16:23:11 +000036#include <android-base/properties.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070037#include <hwbinder/IPCThreadState.h>
38#include <hwbinder/Parcel.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070039
Steven Moreland2a2678e2017-07-21 18:07:38 -070040#include <android/hidl/manager/1.1/IServiceManager.h>
41#include <android/hidl/manager/1.1/BpHwServiceManager.h>
42#include <android/hidl/manager/1.1/BnHwServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070043
Yifan Hong9a22d1d2017-01-25 14:19:26 -080044#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
45#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
46static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
47
Jiyong Park3ab546c2017-04-06 20:28:07 +090048extern "C" {
49 android_namespace_t* android_get_exported_namespace(const char*);
50}
51
Steven Morelandc1cee2c2017-03-24 16:23:11 +000052using android::base::WaitForProperty;
53
Steven Moreland2a2678e2017-07-21 18:07:38 -070054using IServiceManager1_0 = android::hidl::manager::V1_0::IServiceManager;
55using IServiceManager1_1 = android::hidl::manager::V1_1::IServiceManager;
Steven Moreland337e6b62017-01-18 17:25:13 -080056using android::hidl::manager::V1_0::IServiceNotification;
Steven Moreland2a2678e2017-07-21 18:07:38 -070057using android::hidl::manager::V1_1::BpHwServiceManager;
58using android::hidl::manager::V1_1::BnHwServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070059
60namespace android {
61namespace hardware {
62
Yifan Hong953e6b02017-03-16 14:52:40 -070063namespace details {
64extern Mutex gDefaultServiceManagerLock;
Steven Moreland2a2678e2017-07-21 18:07:38 -070065extern sp<android::hidl::manager::V1_1::IServiceManager> gDefaultServiceManager;
Yifan Hong953e6b02017-03-16 14:52:40 -070066} // namespace details
67
Steven Morelandc1cee2c2017-03-24 16:23:11 +000068static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready";
69
70void waitForHwServiceManager() {
71 using std::literals::chrono_literals::operator""s;
72
73 while (!WaitForProperty(kHwServicemanagerReadyProperty, "true", 1s)) {
74 LOG(WARNING) << "Waited for hwservicemanager.ready for a second, waiting another...";
75 }
76}
77
Steven Moreland405d7612017-04-07 20:31:22 -070078bool endsWith(const std::string &in, const std::string &suffix) {
79 return in.size() >= suffix.size() &&
80 in.substr(in.size() - suffix.size()) == suffix;
81}
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070082
Steven Moreland405d7612017-04-07 20:31:22 -070083bool startsWith(const std::string &in, const std::string &prefix) {
84 return in.size() >= prefix.size() &&
85 in.substr(0, prefix.size()) == prefix;
86}
87
88std::string binaryName() {
89 std::ifstream ifs("/proc/self/cmdline");
90 std::string cmdline;
91 if (!ifs.is_open()) {
92 return "";
93 }
94 ifs >> cmdline;
95
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -070096 size_t idx = cmdline.rfind('/');
Steven Moreland405d7612017-04-07 20:31:22 -070097 if (idx != std::string::npos) {
98 cmdline = cmdline.substr(idx + 1);
99 }
100
101 return cmdline;
102}
103
104void tryShortenProcessName(const std::string &packageName) {
105 std::string processName = binaryName();
106
107 if (!startsWith(processName, packageName)) {
108 return;
109 }
110
111 // e.x. android.hardware.module.foo@1.0 -> foo@1.0
112 size_t lastDot = packageName.rfind('.');
113 size_t secondDot = packageName.rfind('.', lastDot - 1);
114
115 if (secondDot == std::string::npos) {
116 return;
117 }
118
119 std::string newName = processName.substr(secondDot + 1,
120 16 /* TASK_COMM_LEN */ - 1);
121 ALOGI("Removing namespace from process name %s to %s.",
122 processName.c_str(), newName.c_str());
123
124 int rc = pthread_setname_np(pthread_self(), newName.c_str());
125 ALOGI_IF(rc != 0, "Removing namespace from process name %s failed.",
126 processName.c_str());
127}
128
129namespace details {
130
131void onRegistration(const std::string &packageName,
132 const std::string& /* interfaceName */,
133 const std::string& /* instanceName */) {
134 tryShortenProcessName(packageName);
135}
136
137} // details
138
Steven Moreland2a2678e2017-07-21 18:07:38 -0700139sp<IServiceManager1_0> defaultServiceManager() {
140 return defaultServiceManager1_1();
141}
142sp<IServiceManager1_1> defaultServiceManager1_1() {
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700143 {
Yifan Hong953e6b02017-03-16 14:52:40 -0700144 AutoMutex _l(details::gDefaultServiceManagerLock);
145 if (details::gDefaultServiceManager != NULL) {
146 return details::gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -0700147 }
Steven Moreland405d7612017-04-07 20:31:22 -0700148
Yifan Hong8fb656b2017-03-16 14:30:40 -0700149 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
150 // HwBinder not available on this device or not accessible to
151 // this process.
152 return nullptr;
153 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000154
155 waitForHwServiceManager();
156
Yifan Hong953e6b02017-03-16 14:52:40 -0700157 while (details::gDefaultServiceManager == NULL) {
158 details::gDefaultServiceManager =
Steven Moreland2a2678e2017-07-21 18:07:38 -0700159 fromBinder<IServiceManager1_1, BpHwServiceManager, BnHwServiceManager>(
Yifan Hong8fb656b2017-03-16 14:30:40 -0700160 ProcessState::self()->getContextObject(NULL));
Yifan Hong953e6b02017-03-16 14:52:40 -0700161 if (details::gDefaultServiceManager == NULL) {
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000162 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700163 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -0700164 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700165 }
166 }
167
Yifan Hong953e6b02017-03-16 14:52:40 -0700168 return details::gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700169}
170
Steven Moreland0091c092017-01-20 23:15:18 +0000171std::vector<std::string> search(const std::string &path,
172 const std::string &prefix,
173 const std::string &suffix) {
174 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
175 if (!dir) return {};
176
177 std::vector<std::string> results{};
178
179 dirent* dp;
180 while ((dp = readdir(dir.get())) != nullptr) {
181 std::string name = dp->d_name;
182
Steven Moreland819c05d2017-04-06 17:24:22 -0700183 if (startsWith(name, prefix) &&
184 endsWith(name, suffix)) {
Steven Moreland0091c092017-01-20 23:15:18 +0000185 results.push_back(name);
186 }
187 }
188
189 return results;
190}
191
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700192bool matchPackageName(const std::string& lib, std::string* matchedName, std::string* implName) {
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800193 std::smatch match;
194 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
195 *matchedName = match.str(1) + "::I*";
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700196 *implName = match.str(2);
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800197 return true;
198 }
199 return false;
200}
201
Yifan Hong7f49f592017-02-03 15:11:44 -0800202static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
Steven Moreland2a2678e2017-07-21 18:07:38 -0700203 sp<IServiceManager1_0> binderizedManager = defaultServiceManager();
Yifan Hong7f49f592017-02-03 15:11:44 -0800204 if (binderizedManager == nullptr) {
205 LOG(WARNING) << "Could not registerReference for "
206 << interfaceName << "/" << instanceName
207 << ": null binderized manager.";
208 return;
209 }
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700210 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName);
Yifan Hong7f49f592017-02-03 15:11:44 -0800211 if (!ret.isOk()) {
212 LOG(WARNING) << "Could not registerReference for "
213 << interfaceName << "/" << instanceName
214 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700215 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800216 }
Steven Morelande681aa52017-02-15 16:22:37 -0800217 LOG(VERBOSE) << "Successfully registerReference for "
218 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800219}
220
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700221using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo;
222static inline void fetchPidsForPassthroughLibraries(
223 std::map<std::string, InstanceDebugInfo>* infos) {
224 static const std::string proc = "/proc/";
225
226 std::map<std::string, std::set<pid_t>> pids;
227 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir);
228 if (!dir) return;
229 dirent* dp;
230 while ((dp = readdir(dir.get())) != nullptr) {
231 pid_t pid = strtoll(dp->d_name, NULL, 0);
232 if (pid == 0) continue;
233 std::string mapsPath = proc + dp->d_name + "/maps";
234 std::ifstream ifs{mapsPath};
235 if (!ifs.is_open()) continue;
236
237 for (std::string line; std::getline(ifs, line);) {
238 // The last token of line should look like
239 // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so
240 // Use some simple filters to ignore bad lines before extracting libFileName
241 // and checking the key in info to make parsing faster.
242 if (line.back() != 'o') continue;
243 if (line.rfind('@') == std::string::npos) continue;
244
245 auto spacePos = line.rfind(' ');
246 if (spacePos == std::string::npos) continue;
247 auto libFileName = line.substr(spacePos + 1);
248 auto it = infos->find(libFileName);
249 if (it == infos->end()) continue;
250 pids[libFileName].insert(pid);
251 }
252 }
253 for (auto& pair : *infos) {
254 pair.second.clientPids =
255 std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()};
256 }
257}
258
Steven Moreland2a2678e2017-07-21 18:07:38 -0700259struct PassthroughServiceManager : IServiceManager1_1 {
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700260 static void openLibs(
261 const std::string& fqName,
262 const std::function<bool /* continue */ (void* /* handle */, const std::string& /* lib */,
263 const std::string& /* sym */)>& eachLib) {
Steven Moreland819c05d2017-04-06 17:24:22 -0700264 //fqName looks like android.hardware.foo@1.0::IFoo
Steven Moreland519306f2017-06-06 17:14:12 -0700265 size_t idx = fqName.find("::");
Steven Moreland819c05d2017-04-06 17:24:22 -0700266
267 if (idx == std::string::npos ||
Steven Moreland519306f2017-06-06 17:14:12 -0700268 idx + strlen("::") + 1 >= fqName.size()) {
Steven Moreland337e6b62017-01-18 17:25:13 -0800269 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
Steven Moreland519306f2017-06-06 17:14:12 -0700270 return;
Steven Moreland337e6b62017-01-18 17:25:13 -0800271 }
272
Steven Moreland519306f2017-06-06 17:14:12 -0700273 std::string packageAndVersion = fqName.substr(0, idx);
274 std::string ifaceName = fqName.substr(idx + strlen("::"));
Steven Moreland819c05d2017-04-06 17:24:22 -0700275
276 const std::string prefix = packageAndVersion + "-impl";
277 const std::string sym = "HIDL_FETCH_" + ifaceName;
Steven Moreland348802d2017-02-23 12:48:55 -0800278
Jiyong Park3ab546c2017-04-06 20:28:07 +0900279 const android_namespace_t* sphal_namespace = android_get_exported_namespace("sphal");
Steven Moreland337e6b62017-01-18 17:25:13 -0800280 const int dlMode = RTLD_LAZY;
281 void *handle = nullptr;
282
Steven Morelanda29905c2017-03-01 10:42:35 -0800283 dlerror(); // clear
284
Steven Morelandf7dea692017-07-14 12:49:28 -0700285 std::vector<std::string> paths = {HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR,
286 HAL_LIBRARY_PATH_SYSTEM};
287#ifdef LIBHIDL_TARGET_DEBUGGABLE
288 const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
289 const bool trebleTestingOverride = env && !strcmp(env, "true");
290 if (trebleTestingOverride) {
291 const char* vtsRootPath = std::getenv("VTS_ROOT_PATH");
292 if (vtsRootPath && strlen(vtsRootPath) > 0) {
293 const std::string halLibraryPathVtsOverride =
294 std::string(vtsRootPath) + HAL_LIBRARY_PATH_SYSTEM;
295 paths.push_back(halLibraryPathVtsOverride);
296 }
297 }
298#endif
299 for (const std::string& path : paths) {
Steven Moreland0091c092017-01-20 23:15:18 +0000300 std::vector<std::string> libs = search(path, prefix, ".so");
301
Steven Moreland0091c092017-01-20 23:15:18 +0000302 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800303 const std::string fullPath = path + lib;
304
Jiyong Park3ab546c2017-04-06 20:28:07 +0900305 // If sphal namespace is available, try to load from the
306 // namespace first. If it fails, fall back to the original
307 // dlopen, which loads from the current namespace.
308 if (sphal_namespace != nullptr && path != HAL_LIBRARY_PATH_SYSTEM) {
309 const android_dlextinfo dlextinfo = {
310 .flags = ANDROID_DLEXT_USE_NAMESPACE,
311 // const_cast is dirty but required because
312 // library_namespace field is non-const.
313 .library_namespace = const_cast<android_namespace_t*>(sphal_namespace),
314 };
315 handle = android_dlopen_ext(fullPath.c_str(), dlMode, &dlextinfo);
316 if (handle == nullptr) {
317 const char* error = dlerror();
318 LOG(WARNING) << "Failed to dlopen " << lib << " from sphal namespace:"
319 << (error == nullptr ? "unknown error" : error);
320 } else {
321 LOG(DEBUG) << lib << " loaded from sphal namespace.";
322 }
323 }
324 if (handle == nullptr) {
325 handle = dlopen(fullPath.c_str(), dlMode);
326 }
327
Steven Moreland348802d2017-02-23 12:48:55 -0800328 if (handle == nullptr) {
329 const char* error = dlerror();
330 LOG(ERROR) << "Failed to dlopen " << lib << ": "
331 << (error == nullptr ? "unknown error" : error);
332 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000333 }
Steven Moreland348802d2017-02-23 12:48:55 -0800334
Steven Moreland519306f2017-06-06 17:14:12 -0700335 if (!eachLib(handle, lib, sym)) {
336 return;
Steven Moreland348802d2017-02-23 12:48:55 -0800337 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800338 }
339 }
Steven Moreland519306f2017-06-06 17:14:12 -0700340 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800341
Steven Moreland519306f2017-06-06 17:14:12 -0700342 Return<sp<IBase>> get(const hidl_string& fqName,
343 const hidl_string& name) override {
344 sp<IBase> ret = nullptr;
345
346 openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) {
347 IBase* (*generator)(const char* name);
348 *(void **)(&generator) = dlsym(handle, sym.c_str());
349 if(!generator) {
350 const char* error = dlerror();
351 LOG(ERROR) << "Passthrough lookup opened " << lib
352 << " but could not find symbol " << sym << ": "
353 << (error == nullptr ? "unknown error" : error);
354 dlclose(handle);
355 return true;
356 }
357
358 ret = (*generator)(name.c_str());
359
360 if (ret == nullptr) {
361 dlclose(handle);
362 return true; // this module doesn't provide this instance name
363 }
364
365 registerReference(fqName, name);
366 return false;
367 });
368
369 return ret;
Steven Moreland337e6b62017-01-18 17:25:13 -0800370 }
371
Martijn Coenen67a02492017-03-06 13:04:48 +0100372 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800373 const sp<IBase>& /* service */) override {
374 LOG(FATAL) << "Cannot register services with passthrough service manager.";
375 return false;
376 }
377
Steven Morelandc601c5f2017-04-06 09:26:07 -0700378 Return<Transport> getTransport(const hidl_string& /* fqName */,
379 const hidl_string& /* name */) {
380 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
381 return Transport::EMPTY;
382 }
383
Yifan Hong705e5da2017-03-02 16:59:39 -0800384 Return<void> list(list_cb /* _hidl_cb */) override {
385 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800386 return Void();
387 }
388 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
389 listByInterface_cb /* _hidl_cb */) override {
390 // TODO: add this functionality
391 LOG(FATAL) << "Cannot list services with passthrough service manager.";
392 return Void();
393 }
394
395 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
396 const hidl_string& /* name */,
397 const sp<IServiceNotification>& /* callback */) override {
398 // This makes no sense.
399 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
400 return false;
401 }
402
Yifan Hong705e5da2017-03-02 16:59:39 -0800403 Return<void> debugDump(debugDump_cb _hidl_cb) override {
404 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700405 using std::literals::string_literals::operator""s;
Yifan Hong705e5da2017-03-02 16:59:39 -0800406 static std::vector<std::pair<Arch, std::vector<const char *>>> sAllPaths{
407 {Arch::IS_64BIT, {HAL_LIBRARY_PATH_ODM_64BIT,
408 HAL_LIBRARY_PATH_VENDOR_64BIT,
409 HAL_LIBRARY_PATH_SYSTEM_64BIT}},
410 {Arch::IS_32BIT, {HAL_LIBRARY_PATH_ODM_32BIT,
411 HAL_LIBRARY_PATH_VENDOR_32BIT,
412 HAL_LIBRARY_PATH_SYSTEM_32BIT}}
413 };
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700414 std::map<std::string, InstanceDebugInfo> map;
Yifan Hong705e5da2017-03-02 16:59:39 -0800415 for (const auto &pair : sAllPaths) {
416 Arch arch = pair.first;
417 for (const auto &path : pair.second) {
418 std::vector<std::string> libs = search(path, "", ".so");
419 for (const std::string &lib : libs) {
420 std::string matchedName;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700421 std::string implName;
422 if (matchPackageName(lib, &matchedName, &implName)) {
423 std::string instanceName{"* ("s + path + ")"s};
424 if (!implName.empty()) instanceName += " ("s + implName + ")"s;
425 map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName,
426 .instanceName = instanceName,
427 .clientPids = {},
428 .arch = arch});
Yifan Hong705e5da2017-03-02 16:59:39 -0800429 }
430 }
431 }
432 }
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700433 fetchPidsForPassthroughLibraries(&map);
434 hidl_vec<InstanceDebugInfo> vec;
435 vec.resize(map.size());
436 size_t idx = 0;
437 for (auto&& pair : map) {
438 vec[idx++] = std::move(pair.second);
439 }
Yifan Hong705e5da2017-03-02 16:59:39 -0800440 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800441 return Void();
442 }
443
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700444 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override {
Yifan Hong7f49f592017-02-03 15:11:44 -0800445 // This makes no sense.
446 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
447 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800448 return Void();
449 }
450
Steven Moreland2a2678e2017-07-21 18:07:38 -0700451 Return<bool> unregisterForNotifications(const hidl_string& /* fqName */,
452 const hidl_string& /* name */,
453 const sp<IServiceNotification>& /* callback */) override {
454 // This makes no sense.
455 LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager.";
456 return false;
457 }
458
Steven Moreland337e6b62017-01-18 17:25:13 -0800459};
460
Steven Moreland2a2678e2017-07-21 18:07:38 -0700461sp<IServiceManager1_0> getPassthroughServiceManager() {
462 return getPassthroughServiceManager1_1();
463}
464sp<IServiceManager1_1> getPassthroughServiceManager1_1() {
Steven Moreland337e6b62017-01-18 17:25:13 -0800465 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
466 return manager;
467}
468
Steven Morelandcbefd352017-01-23 20:29:05 -0800469namespace details {
470
Steven Moreland519306f2017-06-06 17:14:12 -0700471void preloadPassthroughService(const std::string &descriptor) {
472 PassthroughServiceManager::openLibs(descriptor,
473 [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) {
474 // do nothing
475 return true; // open all libs
476 });
477}
478
Steven Morelandcbefd352017-01-23 20:29:05 -0800479struct Waiter : IServiceNotification {
480 Return<void> onRegistration(const hidl_string& /* fqName */,
481 const hidl_string& /* name */,
482 bool /* preexisting */) override {
483 std::unique_lock<std::mutex> lock(mMutex);
484 if (mRegistered) {
485 return Void();
486 }
487 mRegistered = true;
488 lock.unlock();
489
490 mCondition.notify_one();
491 return Void();
492 }
493
Steven Moreland49605102017-03-28 09:33:06 -0700494 void wait(const std::string &interface, const std::string &instanceName) {
495 using std::literals::chrono_literals::operator""s;
496
Steven Morelandcbefd352017-01-23 20:29:05 -0800497 std::unique_lock<std::mutex> lock(mMutex);
Steven Moreland49605102017-03-28 09:33:06 -0700498 while(true) {
499 mCondition.wait_for(lock, 1s, [this]{
500 return mRegistered;
501 });
502
503 if (mRegistered) {
504 break;
505 }
506
507 LOG(WARNING) << "Waited one second for "
508 << interface << "/" << instanceName
509 << ". Waiting another...";
510 }
Steven Morelandcbefd352017-01-23 20:29:05 -0800511 }
512
513private:
514 std::mutex mMutex;
515 std::condition_variable mCondition;
516 bool mRegistered = false;
517};
518
519void waitForHwService(
520 const std::string &interface, const std::string &instanceName) {
Steven Moreland2a2678e2017-07-21 18:07:38 -0700521 const sp<IServiceManager1_1> manager = defaultServiceManager1_1();
Steven Morelandcbefd352017-01-23 20:29:05 -0800522
523 if (manager == nullptr) {
524 LOG(ERROR) << "Could not get default service manager.";
525 return;
526 }
527
528 sp<Waiter> waiter = new Waiter();
529 Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter);
530
531 if (!ret.isOk()) {
532 LOG(ERROR) << "Transport error, " << ret.description()
533 << ", during notification registration for "
534 << interface << "/" << instanceName << ".";
535 return;
536 }
537
538 if (!ret) {
539 LOG(ERROR) << "Could not register for notifications for "
540 << interface << "/" << instanceName << ".";
541 return;
542 }
543
Steven Moreland49605102017-03-28 09:33:06 -0700544 waiter->wait(interface, instanceName);
Steven Moreland2a2678e2017-07-21 18:07:38 -0700545
546 if (!manager->unregisterForNotifications(interface, instanceName, waiter).withDefault(false)) {
547 LOG(ERROR) << "Could not unregister service notification for "
548 << interface << "/" << instanceName << ".";
549 }
Steven Morelandcbefd352017-01-23 20:29:05 -0800550}
551
552}; // namespace details
553
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700554}; // namespace hardware
555}; // namespace android