blob: 3f0ebc6746ceeb9aa85ac875586730d3b438dbc5 [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>
Jiyong Parkba8ace12017-05-15 15:44:39 +090039#include <vndksupport/linker.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070040
Steven Moreland2a2678e2017-07-21 18:07:38 -070041#include <android/hidl/manager/1.1/IServiceManager.h>
42#include <android/hidl/manager/1.1/BpHwServiceManager.h>
43#include <android/hidl/manager/1.1/BnHwServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070044
Yifan Hong9a22d1d2017-01-25 14:19:26 -080045#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
46#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
47static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
48
Steven Morelandc1cee2c2017-03-24 16:23:11 +000049using android::base::WaitForProperty;
50
Steven Moreland2a2678e2017-07-21 18:07:38 -070051using IServiceManager1_0 = android::hidl::manager::V1_0::IServiceManager;
52using IServiceManager1_1 = android::hidl::manager::V1_1::IServiceManager;
Steven Moreland337e6b62017-01-18 17:25:13 -080053using android::hidl::manager::V1_0::IServiceNotification;
Steven Moreland2a2678e2017-07-21 18:07:38 -070054using android::hidl::manager::V1_1::BpHwServiceManager;
55using android::hidl::manager::V1_1::BnHwServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070056
57namespace android {
58namespace hardware {
59
Yifan Hong953e6b02017-03-16 14:52:40 -070060namespace details {
61extern Mutex gDefaultServiceManagerLock;
Steven Moreland2a2678e2017-07-21 18:07:38 -070062extern sp<android::hidl::manager::V1_1::IServiceManager> gDefaultServiceManager;
Yifan Hong953e6b02017-03-16 14:52:40 -070063} // namespace details
64
Steven Morelandc1cee2c2017-03-24 16:23:11 +000065static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready";
66
67void waitForHwServiceManager() {
68 using std::literals::chrono_literals::operator""s;
69
70 while (!WaitForProperty(kHwServicemanagerReadyProperty, "true", 1s)) {
71 LOG(WARNING) << "Waited for hwservicemanager.ready for a second, waiting another...";
72 }
73}
74
Steven Moreland405d7612017-04-07 20:31:22 -070075bool endsWith(const std::string &in, const std::string &suffix) {
76 return in.size() >= suffix.size() &&
77 in.substr(in.size() - suffix.size()) == suffix;
78}
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070079
Steven Moreland405d7612017-04-07 20:31:22 -070080bool startsWith(const std::string &in, const std::string &prefix) {
81 return in.size() >= prefix.size() &&
82 in.substr(0, prefix.size()) == prefix;
83}
84
85std::string binaryName() {
86 std::ifstream ifs("/proc/self/cmdline");
87 std::string cmdline;
88 if (!ifs.is_open()) {
89 return "";
90 }
91 ifs >> cmdline;
92
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -070093 size_t idx = cmdline.rfind('/');
Steven Moreland405d7612017-04-07 20:31:22 -070094 if (idx != std::string::npos) {
95 cmdline = cmdline.substr(idx + 1);
96 }
97
98 return cmdline;
99}
100
101void tryShortenProcessName(const std::string &packageName) {
102 std::string processName = binaryName();
103
104 if (!startsWith(processName, packageName)) {
105 return;
106 }
107
108 // e.x. android.hardware.module.foo@1.0 -> foo@1.0
109 size_t lastDot = packageName.rfind('.');
110 size_t secondDot = packageName.rfind('.', lastDot - 1);
111
112 if (secondDot == std::string::npos) {
113 return;
114 }
115
116 std::string newName = processName.substr(secondDot + 1,
117 16 /* TASK_COMM_LEN */ - 1);
118 ALOGI("Removing namespace from process name %s to %s.",
119 processName.c_str(), newName.c_str());
120
121 int rc = pthread_setname_np(pthread_self(), newName.c_str());
122 ALOGI_IF(rc != 0, "Removing namespace from process name %s failed.",
123 processName.c_str());
124}
125
126namespace details {
127
128void onRegistration(const std::string &packageName,
129 const std::string& /* interfaceName */,
130 const std::string& /* instanceName */) {
131 tryShortenProcessName(packageName);
132}
133
134} // details
135
Steven Moreland2a2678e2017-07-21 18:07:38 -0700136sp<IServiceManager1_0> defaultServiceManager() {
137 return defaultServiceManager1_1();
138}
139sp<IServiceManager1_1> defaultServiceManager1_1() {
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700140 {
Yifan Hong953e6b02017-03-16 14:52:40 -0700141 AutoMutex _l(details::gDefaultServiceManagerLock);
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700142 if (details::gDefaultServiceManager != nullptr) {
Yifan Hong953e6b02017-03-16 14:52:40 -0700143 return details::gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -0700144 }
Steven Moreland405d7612017-04-07 20:31:22 -0700145
Yifan Hong8fb656b2017-03-16 14:30:40 -0700146 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
147 // HwBinder not available on this device or not accessible to
148 // this process.
149 return nullptr;
150 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000151
152 waitForHwServiceManager();
153
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700154 while (details::gDefaultServiceManager == nullptr) {
Yifan Hong953e6b02017-03-16 14:52:40 -0700155 details::gDefaultServiceManager =
Steven Moreland2a2678e2017-07-21 18:07:38 -0700156 fromBinder<IServiceManager1_1, BpHwServiceManager, BnHwServiceManager>(
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700157 ProcessState::self()->getContextObject(nullptr));
158 if (details::gDefaultServiceManager == nullptr) {
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000159 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700160 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -0700161 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700162 }
163 }
164
Yifan Hong953e6b02017-03-16 14:52:40 -0700165 return details::gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700166}
167
Steven Moreland0091c092017-01-20 23:15:18 +0000168std::vector<std::string> search(const std::string &path,
169 const std::string &prefix,
170 const std::string &suffix) {
171 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
172 if (!dir) return {};
173
174 std::vector<std::string> results{};
175
176 dirent* dp;
177 while ((dp = readdir(dir.get())) != nullptr) {
178 std::string name = dp->d_name;
179
Steven Moreland819c05d2017-04-06 17:24:22 -0700180 if (startsWith(name, prefix) &&
181 endsWith(name, suffix)) {
Steven Moreland0091c092017-01-20 23:15:18 +0000182 results.push_back(name);
183 }
184 }
185
186 return results;
187}
188
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700189bool matchPackageName(const std::string& lib, std::string* matchedName, std::string* implName) {
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800190 std::smatch match;
191 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
192 *matchedName = match.str(1) + "::I*";
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700193 *implName = match.str(2);
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800194 return true;
195 }
196 return false;
197}
198
Yifan Hong7f49f592017-02-03 15:11:44 -0800199static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
Steven Moreland2a2678e2017-07-21 18:07:38 -0700200 sp<IServiceManager1_0> binderizedManager = defaultServiceManager();
Yifan Hong7f49f592017-02-03 15:11:44 -0800201 if (binderizedManager == nullptr) {
202 LOG(WARNING) << "Could not registerReference for "
203 << interfaceName << "/" << instanceName
204 << ": null binderized manager.";
205 return;
206 }
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700207 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName);
Yifan Hong7f49f592017-02-03 15:11:44 -0800208 if (!ret.isOk()) {
209 LOG(WARNING) << "Could not registerReference for "
210 << interfaceName << "/" << instanceName
211 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700212 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800213 }
Steven Morelande681aa52017-02-15 16:22:37 -0800214 LOG(VERBOSE) << "Successfully registerReference for "
215 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800216}
217
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700218using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo;
219static inline void fetchPidsForPassthroughLibraries(
220 std::map<std::string, InstanceDebugInfo>* infos) {
221 static const std::string proc = "/proc/";
222
223 std::map<std::string, std::set<pid_t>> pids;
224 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir);
225 if (!dir) return;
226 dirent* dp;
227 while ((dp = readdir(dir.get())) != nullptr) {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700228 pid_t pid = strtoll(dp->d_name, nullptr, 0);
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700229 if (pid == 0) continue;
230 std::string mapsPath = proc + dp->d_name + "/maps";
231 std::ifstream ifs{mapsPath};
232 if (!ifs.is_open()) continue;
233
234 for (std::string line; std::getline(ifs, line);) {
235 // The last token of line should look like
236 // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so
237 // Use some simple filters to ignore bad lines before extracting libFileName
238 // and checking the key in info to make parsing faster.
239 if (line.back() != 'o') continue;
240 if (line.rfind('@') == std::string::npos) continue;
241
242 auto spacePos = line.rfind(' ');
243 if (spacePos == std::string::npos) continue;
244 auto libFileName = line.substr(spacePos + 1);
245 auto it = infos->find(libFileName);
246 if (it == infos->end()) continue;
247 pids[libFileName].insert(pid);
248 }
249 }
250 for (auto& pair : *infos) {
251 pair.second.clientPids =
252 std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()};
253 }
254}
255
Steven Moreland2a2678e2017-07-21 18:07:38 -0700256struct PassthroughServiceManager : IServiceManager1_1 {
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700257 static void openLibs(
258 const std::string& fqName,
259 const std::function<bool /* continue */ (void* /* handle */, const std::string& /* lib */,
260 const std::string& /* sym */)>& eachLib) {
Steven Moreland819c05d2017-04-06 17:24:22 -0700261 //fqName looks like android.hardware.foo@1.0::IFoo
Steven Moreland519306f2017-06-06 17:14:12 -0700262 size_t idx = fqName.find("::");
Steven Moreland819c05d2017-04-06 17:24:22 -0700263
264 if (idx == std::string::npos ||
Steven Moreland519306f2017-06-06 17:14:12 -0700265 idx + strlen("::") + 1 >= fqName.size()) {
Steven Moreland337e6b62017-01-18 17:25:13 -0800266 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
Steven Moreland519306f2017-06-06 17:14:12 -0700267 return;
Steven Moreland337e6b62017-01-18 17:25:13 -0800268 }
269
Steven Moreland519306f2017-06-06 17:14:12 -0700270 std::string packageAndVersion = fqName.substr(0, idx);
271 std::string ifaceName = fqName.substr(idx + strlen("::"));
Steven Moreland819c05d2017-04-06 17:24:22 -0700272
273 const std::string prefix = packageAndVersion + "-impl";
274 const std::string sym = "HIDL_FETCH_" + ifaceName;
Steven Moreland348802d2017-02-23 12:48:55 -0800275
Steven Moreland337e6b62017-01-18 17:25:13 -0800276 const int dlMode = RTLD_LAZY;
277 void *handle = nullptr;
278
Steven Morelanda29905c2017-03-01 10:42:35 -0800279 dlerror(); // clear
280
Steven Morelandf7dea692017-07-14 12:49:28 -0700281 std::vector<std::string> paths = {HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR,
Jiyong Park56eb1492017-08-04 16:16:13 +0900282 HAL_LIBRARY_PATH_VNDK_SP, HAL_LIBRARY_PATH_SYSTEM};
Steven Morelandf7dea692017-07-14 12:49:28 -0700283#ifdef LIBHIDL_TARGET_DEBUGGABLE
284 const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
285 const bool trebleTestingOverride = env && !strcmp(env, "true");
286 if (trebleTestingOverride) {
287 const char* vtsRootPath = std::getenv("VTS_ROOT_PATH");
288 if (vtsRootPath && strlen(vtsRootPath) > 0) {
289 const std::string halLibraryPathVtsOverride =
290 std::string(vtsRootPath) + HAL_LIBRARY_PATH_SYSTEM;
291 paths.push_back(halLibraryPathVtsOverride);
292 }
293 }
294#endif
295 for (const std::string& path : paths) {
Steven Moreland0091c092017-01-20 23:15:18 +0000296 std::vector<std::string> libs = search(path, prefix, ".so");
297
Steven Moreland0091c092017-01-20 23:15:18 +0000298 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800299 const std::string fullPath = path + lib;
300
Steven Moreland65c00cb2017-10-12 11:35:22 -0700301 if (path == HAL_LIBRARY_PATH_SYSTEM) {
Jiyong Park3ab546c2017-04-06 20:28:07 +0900302 handle = dlopen(fullPath.c_str(), dlMode);
Steven Moreland65c00cb2017-10-12 11:35:22 -0700303 } else {
304 handle = android_load_sphal_library(fullPath.c_str(), dlMode);
Jiyong Park3ab546c2017-04-06 20:28:07 +0900305 }
306
Steven Moreland348802d2017-02-23 12:48:55 -0800307 if (handle == nullptr) {
308 const char* error = dlerror();
309 LOG(ERROR) << "Failed to dlopen " << lib << ": "
310 << (error == nullptr ? "unknown error" : error);
311 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000312 }
Steven Moreland348802d2017-02-23 12:48:55 -0800313
Steven Moreland519306f2017-06-06 17:14:12 -0700314 if (!eachLib(handle, lib, sym)) {
315 return;
Steven Moreland348802d2017-02-23 12:48:55 -0800316 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800317 }
318 }
Steven Moreland519306f2017-06-06 17:14:12 -0700319 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800320
Steven Moreland519306f2017-06-06 17:14:12 -0700321 Return<sp<IBase>> get(const hidl_string& fqName,
322 const hidl_string& name) override {
323 sp<IBase> ret = nullptr;
324
325 openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) {
326 IBase* (*generator)(const char* name);
327 *(void **)(&generator) = dlsym(handle, sym.c_str());
328 if(!generator) {
329 const char* error = dlerror();
330 LOG(ERROR) << "Passthrough lookup opened " << lib
331 << " but could not find symbol " << sym << ": "
332 << (error == nullptr ? "unknown error" : error);
333 dlclose(handle);
334 return true;
335 }
336
337 ret = (*generator)(name.c_str());
338
339 if (ret == nullptr) {
340 dlclose(handle);
341 return true; // this module doesn't provide this instance name
342 }
343
344 registerReference(fqName, name);
345 return false;
346 });
347
348 return ret;
Steven Moreland337e6b62017-01-18 17:25:13 -0800349 }
350
Martijn Coenen67a02492017-03-06 13:04:48 +0100351 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800352 const sp<IBase>& /* service */) override {
353 LOG(FATAL) << "Cannot register services with passthrough service manager.";
354 return false;
355 }
356
Steven Morelandc601c5f2017-04-06 09:26:07 -0700357 Return<Transport> getTransport(const hidl_string& /* fqName */,
358 const hidl_string& /* name */) {
359 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
360 return Transport::EMPTY;
361 }
362
Yifan Hong705e5da2017-03-02 16:59:39 -0800363 Return<void> list(list_cb /* _hidl_cb */) override {
364 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800365 return Void();
366 }
367 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
368 listByInterface_cb /* _hidl_cb */) override {
369 // TODO: add this functionality
370 LOG(FATAL) << "Cannot list services with passthrough service manager.";
371 return Void();
372 }
373
374 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
375 const hidl_string& /* name */,
376 const sp<IServiceNotification>& /* callback */) override {
377 // This makes no sense.
378 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
379 return false;
380 }
381
Yifan Hong705e5da2017-03-02 16:59:39 -0800382 Return<void> debugDump(debugDump_cb _hidl_cb) override {
383 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700384 using std::literals::string_literals::operator""s;
Jiyong Park56eb1492017-08-04 16:16:13 +0900385 static std::vector<std::pair<Arch, std::vector<const char*>>> sAllPaths{
386 {Arch::IS_64BIT,
387 {HAL_LIBRARY_PATH_ODM_64BIT, HAL_LIBRARY_PATH_VENDOR_64BIT,
388 HAL_LIBRARY_PATH_VNDK_SP_64BIT, HAL_LIBRARY_PATH_SYSTEM_64BIT}},
389 {Arch::IS_32BIT,
390 {HAL_LIBRARY_PATH_ODM_32BIT, HAL_LIBRARY_PATH_VENDOR_32BIT,
391 HAL_LIBRARY_PATH_VNDK_SP_32BIT, HAL_LIBRARY_PATH_SYSTEM_32BIT}}};
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700392 std::map<std::string, InstanceDebugInfo> map;
Yifan Hong705e5da2017-03-02 16:59:39 -0800393 for (const auto &pair : sAllPaths) {
394 Arch arch = pair.first;
395 for (const auto &path : pair.second) {
396 std::vector<std::string> libs = search(path, "", ".so");
397 for (const std::string &lib : libs) {
398 std::string matchedName;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700399 std::string implName;
400 if (matchPackageName(lib, &matchedName, &implName)) {
401 std::string instanceName{"* ("s + path + ")"s};
402 if (!implName.empty()) instanceName += " ("s + implName + ")"s;
403 map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName,
404 .instanceName = instanceName,
405 .clientPids = {},
406 .arch = arch});
Yifan Hong705e5da2017-03-02 16:59:39 -0800407 }
408 }
409 }
410 }
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700411 fetchPidsForPassthroughLibraries(&map);
412 hidl_vec<InstanceDebugInfo> vec;
413 vec.resize(map.size());
414 size_t idx = 0;
415 for (auto&& pair : map) {
416 vec[idx++] = std::move(pair.second);
417 }
Yifan Hong705e5da2017-03-02 16:59:39 -0800418 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800419 return Void();
420 }
421
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700422 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override {
Yifan Hong7f49f592017-02-03 15:11:44 -0800423 // This makes no sense.
424 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
425 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800426 return Void();
427 }
428
Steven Moreland2a2678e2017-07-21 18:07:38 -0700429 Return<bool> unregisterForNotifications(const hidl_string& /* fqName */,
430 const hidl_string& /* name */,
431 const sp<IServiceNotification>& /* callback */) override {
432 // This makes no sense.
433 LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager.";
434 return false;
435 }
436
Steven Moreland337e6b62017-01-18 17:25:13 -0800437};
438
Steven Moreland2a2678e2017-07-21 18:07:38 -0700439sp<IServiceManager1_0> getPassthroughServiceManager() {
440 return getPassthroughServiceManager1_1();
441}
442sp<IServiceManager1_1> getPassthroughServiceManager1_1() {
Steven Moreland337e6b62017-01-18 17:25:13 -0800443 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
444 return manager;
445}
446
Steven Morelandcbefd352017-01-23 20:29:05 -0800447namespace details {
448
Steven Moreland519306f2017-06-06 17:14:12 -0700449void preloadPassthroughService(const std::string &descriptor) {
450 PassthroughServiceManager::openLibs(descriptor,
451 [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) {
452 // do nothing
453 return true; // open all libs
454 });
455}
456
Steven Morelandcbefd352017-01-23 20:29:05 -0800457struct Waiter : IServiceNotification {
458 Return<void> onRegistration(const hidl_string& /* fqName */,
459 const hidl_string& /* name */,
460 bool /* preexisting */) override {
461 std::unique_lock<std::mutex> lock(mMutex);
462 if (mRegistered) {
463 return Void();
464 }
465 mRegistered = true;
466 lock.unlock();
467
468 mCondition.notify_one();
469 return Void();
470 }
471
Steven Moreland49605102017-03-28 09:33:06 -0700472 void wait(const std::string &interface, const std::string &instanceName) {
473 using std::literals::chrono_literals::operator""s;
474
Steven Morelandcbefd352017-01-23 20:29:05 -0800475 std::unique_lock<std::mutex> lock(mMutex);
Steven Moreland49605102017-03-28 09:33:06 -0700476 while(true) {
477 mCondition.wait_for(lock, 1s, [this]{
478 return mRegistered;
479 });
480
481 if (mRegistered) {
482 break;
483 }
484
485 LOG(WARNING) << "Waited one second for "
486 << interface << "/" << instanceName
487 << ". Waiting another...";
488 }
Steven Morelandcbefd352017-01-23 20:29:05 -0800489 }
490
491private:
492 std::mutex mMutex;
493 std::condition_variable mCondition;
494 bool mRegistered = false;
495};
496
497void waitForHwService(
498 const std::string &interface, const std::string &instanceName) {
Steven Moreland2a2678e2017-07-21 18:07:38 -0700499 const sp<IServiceManager1_1> manager = defaultServiceManager1_1();
Steven Morelandcbefd352017-01-23 20:29:05 -0800500
501 if (manager == nullptr) {
502 LOG(ERROR) << "Could not get default service manager.";
503 return;
504 }
505
506 sp<Waiter> waiter = new Waiter();
507 Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter);
508
509 if (!ret.isOk()) {
510 LOG(ERROR) << "Transport error, " << ret.description()
511 << ", during notification registration for "
512 << interface << "/" << instanceName << ".";
513 return;
514 }
515
516 if (!ret) {
517 LOG(ERROR) << "Could not register for notifications for "
518 << interface << "/" << instanceName << ".";
519 return;
520 }
521
Steven Moreland49605102017-03-28 09:33:06 -0700522 waiter->wait(interface, instanceName);
Steven Moreland2a2678e2017-07-21 18:07:38 -0700523
524 if (!manager->unregisterForNotifications(interface, instanceName, waiter).withDefault(false)) {
525 LOG(ERROR) << "Could not unregister service notification for "
526 << interface << "/" << instanceName << ".";
527 }
Steven Morelandcbefd352017-01-23 20:29:05 -0800528}
529
530}; // namespace details
531
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700532}; // namespace hardware
533}; // namespace android