blob: f11acab2b86468edd2986e1c00cff179d471024a [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>
Justin Yun1f048102017-12-01 15:30:08 +090032#include <hidl/HidlInternal.h>
Steven Moreland83cb4b32018-03-14 10:55:42 -070033#include <hidl/HidlTransportUtils.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070034#include <hidl/ServiceManagement.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070035#include <hidl/Status.h>
36
Steven Moreland337e6b62017-01-18 17:25:13 -080037#include <android-base/logging.h>
Steven Morelandc1cee2c2017-03-24 16:23:11 +000038#include <android-base/properties.h>
Justin Yun1f048102017-12-01 15:30:08 +090039#include <android-base/stringprintf.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070040#include <hwbinder/IPCThreadState.h>
41#include <hwbinder/Parcel.h>
Jiyong Parkba8ace12017-05-15 15:44:39 +090042#include <vndksupport/linker.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070043
Steven Moreland2a2678e2017-07-21 18:07:38 -070044#include <android/hidl/manager/1.1/IServiceManager.h>
45#include <android/hidl/manager/1.1/BpHwServiceManager.h>
46#include <android/hidl/manager/1.1/BnHwServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070047
Yifan Hong9a22d1d2017-01-25 14:19:26 -080048#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
49#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
50static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
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);
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700145 if (details::gDefaultServiceManager != nullptr) {
Yifan Hong953e6b02017-03-16 14:52:40 -0700146 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
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700157 while (details::gDefaultServiceManager == nullptr) {
Yifan Hong953e6b02017-03-16 14:52:40 -0700158 details::gDefaultServiceManager =
Steven Moreland2a2678e2017-07-21 18:07:38 -0700159 fromBinder<IServiceManager1_1, BpHwServiceManager, BnHwServiceManager>(
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700160 ProcessState::self()->getContextObject(nullptr));
161 if (details::gDefaultServiceManager == nullptr) {
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) {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700231 pid_t pid = strtoll(dp->d_name, nullptr, 0);
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700232 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
Steven Moreland77f4c852017-10-12 11:05:53 -0700279 constexpr int dlMode = RTLD_LAZY;
280 void* handle = nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800281
Steven Morelanda29905c2017-03-01 10:42:35 -0800282 dlerror(); // clear
283
Justin Yun1f048102017-12-01 15:30:08 +0900284 static std::string halLibPathVndkSp = android::base::StringPrintf(
285 HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION, details::getVndkVersionStr().c_str());
Steven Morelandf7dea692017-07-14 12:49:28 -0700286 std::vector<std::string> paths = {HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR,
Justin Yun1f048102017-12-01 15:30:08 +0900287 halLibPathVndkSp, HAL_LIBRARY_PATH_SYSTEM};
Steven Moreland77f4c852017-10-12 11:05:53 -0700288
Steven Morelandf7dea692017-07-14 12:49:28 -0700289#ifdef LIBHIDL_TARGET_DEBUGGABLE
290 const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
291 const bool trebleTestingOverride = env && !strcmp(env, "true");
292 if (trebleTestingOverride) {
Steven Moreland77f4c852017-10-12 11:05:53 -0700293 // Load HAL implementations that are statically linked
294 handle = dlopen(nullptr, dlMode);
295 if (handle == nullptr) {
296 const char* error = dlerror();
297 LOG(ERROR) << "Failed to dlopen self: "
298 << (error == nullptr ? "unknown error" : error);
299 } else if (!eachLib(handle, "SELF", sym)) {
300 return;
301 }
302
Steven Morelandf7dea692017-07-14 12:49:28 -0700303 const char* vtsRootPath = std::getenv("VTS_ROOT_PATH");
304 if (vtsRootPath && strlen(vtsRootPath) > 0) {
305 const std::string halLibraryPathVtsOverride =
306 std::string(vtsRootPath) + HAL_LIBRARY_PATH_SYSTEM;
Steven Moreland77f4c852017-10-12 11:05:53 -0700307 paths.insert(paths.begin(), halLibraryPathVtsOverride);
Steven Morelandf7dea692017-07-14 12:49:28 -0700308 }
309 }
310#endif
Steven Moreland77f4c852017-10-12 11:05:53 -0700311
Steven Morelandf7dea692017-07-14 12:49:28 -0700312 for (const std::string& path : paths) {
Steven Moreland0091c092017-01-20 23:15:18 +0000313 std::vector<std::string> libs = search(path, prefix, ".so");
314
Steven Moreland0091c092017-01-20 23:15:18 +0000315 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800316 const std::string fullPath = path + lib;
317
Steven Moreland65c00cb2017-10-12 11:35:22 -0700318 if (path == HAL_LIBRARY_PATH_SYSTEM) {
Jiyong Park3ab546c2017-04-06 20:28:07 +0900319 handle = dlopen(fullPath.c_str(), dlMode);
Steven Moreland65c00cb2017-10-12 11:35:22 -0700320 } else {
321 handle = android_load_sphal_library(fullPath.c_str(), dlMode);
Jiyong Park3ab546c2017-04-06 20:28:07 +0900322 }
323
Steven Moreland348802d2017-02-23 12:48:55 -0800324 if (handle == nullptr) {
325 const char* error = dlerror();
326 LOG(ERROR) << "Failed to dlopen " << lib << ": "
327 << (error == nullptr ? "unknown error" : error);
328 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000329 }
Steven Moreland348802d2017-02-23 12:48:55 -0800330
Steven Moreland519306f2017-06-06 17:14:12 -0700331 if (!eachLib(handle, lib, sym)) {
332 return;
Steven Moreland348802d2017-02-23 12:48:55 -0800333 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800334 }
335 }
Steven Moreland519306f2017-06-06 17:14:12 -0700336 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800337
Steven Moreland519306f2017-06-06 17:14:12 -0700338 Return<sp<IBase>> get(const hidl_string& fqName,
339 const hidl_string& name) override {
340 sp<IBase> ret = nullptr;
341
342 openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) {
343 IBase* (*generator)(const char* name);
344 *(void **)(&generator) = dlsym(handle, sym.c_str());
345 if(!generator) {
346 const char* error = dlerror();
347 LOG(ERROR) << "Passthrough lookup opened " << lib
348 << " but could not find symbol " << sym << ": "
349 << (error == nullptr ? "unknown error" : error);
350 dlclose(handle);
351 return true;
352 }
353
354 ret = (*generator)(name.c_str());
355
356 if (ret == nullptr) {
357 dlclose(handle);
358 return true; // this module doesn't provide this instance name
359 }
360
Steven Moreland83cb4b32018-03-14 10:55:42 -0700361 // Actual fqname might be a subclass.
362 // This assumption is tested in vts_treble_vintf_test
363 using ::android::hardware::details::getDescriptor;
364 std::string actualFqName = getDescriptor(ret.get());
365 CHECK(actualFqName.size() > 0);
366 registerReference(actualFqName, name);
Steven Moreland519306f2017-06-06 17:14:12 -0700367 return false;
368 });
369
370 return ret;
Steven Moreland337e6b62017-01-18 17:25:13 -0800371 }
372
Martijn Coenen67a02492017-03-06 13:04:48 +0100373 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800374 const sp<IBase>& /* service */) override {
375 LOG(FATAL) << "Cannot register services with passthrough service manager.";
376 return false;
377 }
378
Steven Morelandc601c5f2017-04-06 09:26:07 -0700379 Return<Transport> getTransport(const hidl_string& /* fqName */,
380 const hidl_string& /* name */) {
381 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
382 return Transport::EMPTY;
383 }
384
Yifan Hong705e5da2017-03-02 16:59:39 -0800385 Return<void> list(list_cb /* _hidl_cb */) override {
386 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800387 return Void();
388 }
389 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
390 listByInterface_cb /* _hidl_cb */) override {
391 // TODO: add this functionality
392 LOG(FATAL) << "Cannot list services with passthrough service manager.";
393 return Void();
394 }
395
396 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
397 const hidl_string& /* name */,
398 const sp<IServiceNotification>& /* callback */) override {
399 // This makes no sense.
400 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
401 return false;
402 }
403
Yifan Hong705e5da2017-03-02 16:59:39 -0800404 Return<void> debugDump(debugDump_cb _hidl_cb) override {
405 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700406 using std::literals::string_literals::operator""s;
Justin Yun1f048102017-12-01 15:30:08 +0900407 static std::string halLibPathVndkSp64 = android::base::StringPrintf(
408 HAL_LIBRARY_PATH_VNDK_SP_64BIT_FOR_VERSION, details::getVndkVersionStr().c_str());
409 static std::string halLibPathVndkSp32 = android::base::StringPrintf(
410 HAL_LIBRARY_PATH_VNDK_SP_32BIT_FOR_VERSION, details::getVndkVersionStr().c_str());
Jiyong Park56eb1492017-08-04 16:16:13 +0900411 static std::vector<std::pair<Arch, std::vector<const char*>>> sAllPaths{
412 {Arch::IS_64BIT,
413 {HAL_LIBRARY_PATH_ODM_64BIT, HAL_LIBRARY_PATH_VENDOR_64BIT,
Justin Yun1f048102017-12-01 15:30:08 +0900414 halLibPathVndkSp64.c_str(), HAL_LIBRARY_PATH_SYSTEM_64BIT}},
Jiyong Park56eb1492017-08-04 16:16:13 +0900415 {Arch::IS_32BIT,
416 {HAL_LIBRARY_PATH_ODM_32BIT, HAL_LIBRARY_PATH_VENDOR_32BIT,
Justin Yun1f048102017-12-01 15:30:08 +0900417 halLibPathVndkSp32.c_str(), HAL_LIBRARY_PATH_SYSTEM_32BIT}}};
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700418 std::map<std::string, InstanceDebugInfo> map;
Yifan Hong705e5da2017-03-02 16:59:39 -0800419 for (const auto &pair : sAllPaths) {
420 Arch arch = pair.first;
421 for (const auto &path : pair.second) {
422 std::vector<std::string> libs = search(path, "", ".so");
423 for (const std::string &lib : libs) {
424 std::string matchedName;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700425 std::string implName;
426 if (matchPackageName(lib, &matchedName, &implName)) {
427 std::string instanceName{"* ("s + path + ")"s};
428 if (!implName.empty()) instanceName += " ("s + implName + ")"s;
429 map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName,
430 .instanceName = instanceName,
431 .clientPids = {},
432 .arch = arch});
Yifan Hong705e5da2017-03-02 16:59:39 -0800433 }
434 }
435 }
436 }
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700437 fetchPidsForPassthroughLibraries(&map);
438 hidl_vec<InstanceDebugInfo> vec;
439 vec.resize(map.size());
440 size_t idx = 0;
441 for (auto&& pair : map) {
442 vec[idx++] = std::move(pair.second);
443 }
Yifan Hong705e5da2017-03-02 16:59:39 -0800444 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800445 return Void();
446 }
447
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700448 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override {
Yifan Hong7f49f592017-02-03 15:11:44 -0800449 // This makes no sense.
450 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
451 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800452 return Void();
453 }
454
Steven Moreland2a2678e2017-07-21 18:07:38 -0700455 Return<bool> unregisterForNotifications(const hidl_string& /* fqName */,
456 const hidl_string& /* name */,
457 const sp<IServiceNotification>& /* callback */) override {
458 // This makes no sense.
459 LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager.";
460 return false;
461 }
462
Steven Moreland337e6b62017-01-18 17:25:13 -0800463};
464
Steven Moreland2a2678e2017-07-21 18:07:38 -0700465sp<IServiceManager1_0> getPassthroughServiceManager() {
466 return getPassthroughServiceManager1_1();
467}
468sp<IServiceManager1_1> getPassthroughServiceManager1_1() {
Steven Moreland337e6b62017-01-18 17:25:13 -0800469 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
470 return manager;
471}
472
Steven Morelandcbefd352017-01-23 20:29:05 -0800473namespace details {
474
Steven Moreland519306f2017-06-06 17:14:12 -0700475void preloadPassthroughService(const std::string &descriptor) {
476 PassthroughServiceManager::openLibs(descriptor,
477 [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) {
478 // do nothing
479 return true; // open all libs
480 });
481}
482
Steven Morelandcbefd352017-01-23 20:29:05 -0800483struct Waiter : IServiceNotification {
Martijn Coenen773609e2017-10-24 09:57:53 +0200484 Waiter(const std::string& interface, const std::string& instanceName,
485 const sp<IServiceManager1_1>& sm) : mInterfaceName(interface),
486 mInstanceName(instanceName), mSm(sm) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100487 }
488
489 void onFirstRef() override {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200490 // If this process only has one binder thread, and we're calling wait() from
491 // that thread, it will block forever because we hung up the one and only
492 // binder thread on a condition variable that can only be notified by an
493 // incoming binder call.
Tobias Lindskog88e886f2018-01-05 10:32:43 +0100494 if (IPCThreadState::self()->isOnlyBinderThread()) {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200495 LOG(WARNING) << "Can't efficiently wait for " << mInterfaceName << "/"
496 << mInstanceName << ", because we are called from "
497 << "the only binder thread in this process.";
498 return;
499 }
500
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100501 Return<bool> ret = mSm->registerForNotifications(mInterfaceName, mInstanceName, this);
Martijn Coenen773609e2017-10-24 09:57:53 +0200502
503 if (!ret.isOk()) {
504 LOG(ERROR) << "Transport error, " << ret.description()
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100505 << ", during notification registration for " << mInterfaceName << "/"
506 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200507 return;
508 }
509
510 if (!ret) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100511 LOG(ERROR) << "Could not register for notifications for " << mInterfaceName << "/"
512 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200513 return;
514 }
515
516 mRegisteredForNotifications = true;
517 }
518
519 ~Waiter() {
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100520 if (!mDoneCalled) {
521 LOG(FATAL)
522 << "Waiter still registered for notifications, call done() before dropping ref!";
Martijn Coenen773609e2017-10-24 09:57:53 +0200523 }
524 }
525
Steven Morelandcbefd352017-01-23 20:29:05 -0800526 Return<void> onRegistration(const hidl_string& /* fqName */,
527 const hidl_string& /* name */,
528 bool /* preexisting */) override {
529 std::unique_lock<std::mutex> lock(mMutex);
530 if (mRegistered) {
531 return Void();
532 }
533 mRegistered = true;
534 lock.unlock();
535
536 mCondition.notify_one();
537 return Void();
538 }
539
Martijn Coenen773609e2017-10-24 09:57:53 +0200540 void wait() {
Steven Moreland49605102017-03-28 09:33:06 -0700541 using std::literals::chrono_literals::operator""s;
542
Martijn Coenen773609e2017-10-24 09:57:53 +0200543 if (!mRegisteredForNotifications) {
544 // As an alternative, just sleep for a second and return
545 LOG(WARNING) << "Waiting one second for " << mInterfaceName << "/" << mInstanceName;
546 sleep(1);
547 return;
548 }
549
Steven Morelandcbefd352017-01-23 20:29:05 -0800550 std::unique_lock<std::mutex> lock(mMutex);
Steven Moreland49605102017-03-28 09:33:06 -0700551 while(true) {
552 mCondition.wait_for(lock, 1s, [this]{
553 return mRegistered;
554 });
555
556 if (mRegistered) {
557 break;
558 }
559
Martijn Coenen773609e2017-10-24 09:57:53 +0200560 LOG(WARNING) << "Waited one second for " << mInterfaceName << "/" << mInstanceName
Steven Moreland49605102017-03-28 09:33:06 -0700561 << ". Waiting another...";
562 }
Steven Morelandcbefd352017-01-23 20:29:05 -0800563 }
564
Martijn Coenen773609e2017-10-24 09:57:53 +0200565 // Be careful when using this; after calling reset(), you must always try to retrieve
566 // the corresponding service before blocking on the waiter; otherwise, you might run
567 // into a race-condition where the service has just (re-)registered, you clear the state
568 // here, and subsequently calling waiter->wait() will block forever.
569 void reset() {
570 std::unique_lock<std::mutex> lock(mMutex);
571 mRegistered = false;
572 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100573
574 // done() must be called before dropping the last strong ref to the Waiter, to make
575 // sure we can properly unregister with hwservicemanager.
576 void done() {
577 if (mRegisteredForNotifications) {
578 if (!mSm->unregisterForNotifications(mInterfaceName, mInstanceName, this)
579 .withDefault(false)) {
580 LOG(ERROR) << "Could not unregister service notification for " << mInterfaceName
581 << "/" << mInstanceName << ".";
582 } else {
583 mRegisteredForNotifications = false;
584 }
585 }
586 mDoneCalled = true;
587 }
588
589 private:
Martijn Coenen773609e2017-10-24 09:57:53 +0200590 const std::string mInterfaceName;
591 const std::string mInstanceName;
592 const sp<IServiceManager1_1>& mSm;
Steven Morelandcbefd352017-01-23 20:29:05 -0800593 std::mutex mMutex;
594 std::condition_variable mCondition;
595 bool mRegistered = false;
Martijn Coenen773609e2017-10-24 09:57:53 +0200596 bool mRegisteredForNotifications = false;
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100597 bool mDoneCalled = false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800598};
599
600void waitForHwService(
601 const std::string &interface, const std::string &instanceName) {
Martijn Coenen773609e2017-10-24 09:57:53 +0200602 sp<Waiter> waiter = new Waiter(interface, instanceName, defaultServiceManager1_1());
603 waiter->wait();
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100604 waiter->done();
Martijn Coenen773609e2017-10-24 09:57:53 +0200605}
Steven Morelandcbefd352017-01-23 20:29:05 -0800606
Martijn Coenen773609e2017-10-24 09:57:53 +0200607// Prints relevant error/warning messages for error return values from
608// details::canCastInterface(), both transaction errors (!castReturn.isOk())
609// as well as actual cast failures (castReturn.isOk() && castReturn = false).
610// Returns 'true' if the error is non-fatal and it's useful to retry
611bool handleCastError(const Return<bool>& castReturn, const std::string& descriptor,
612 const std::string& instance) {
613 if (castReturn.isOk()) {
614 if (castReturn) {
615 details::logAlwaysFatal("Successful cast value passed into handleCastError.");
616 }
617 // This should never happen, and there's not really a point in retrying.
618 ALOGE("getService: received incompatible service (bug in hwservicemanager?) for "
619 "%s/%s.", descriptor.c_str(), instance.c_str());
620 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800621 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200622 if (castReturn.isDeadObject()) {
623 ALOGW("getService: found dead hwbinder service for %s/%s.", descriptor.c_str(),
624 instance.c_str());
625 return true;
Steven Morelandcbefd352017-01-23 20:29:05 -0800626 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200627 // This can happen due to:
628 // 1) No SELinux permissions
629 // 2) Other transaction failure (no buffer space, kernel error)
630 // The first isn't recoverable, but the second is.
631 // Since we can't yet differentiate between the two, and clients depend
632 // on us not blocking in case 1), treat this as a fatal error for now.
633 ALOGW("getService: unable to call into hwbinder service for %s/%s.",
634 descriptor.c_str(), instance.c_str());
635 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800636}
637
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200638sp<::android::hidl::base::V1_0::IBase> getRawServiceInternal(const std::string& descriptor,
639 const std::string& instance,
640 bool retry, bool getStub) {
641 using Transport = ::android::hidl::manager::V1_0::IServiceManager::Transport;
642 using ::android::hidl::base::V1_0::IBase;
643 using ::android::hidl::manager::V1_0::IServiceManager;
Martijn Coenen0e6f8ba2018-03-07 09:51:01 +0100644 sp<Waiter> waiter;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200645
Martijn Coenen773609e2017-10-24 09:57:53 +0200646 const sp<IServiceManager1_1> sm = defaultServiceManager1_1();
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200647 if (sm == nullptr) {
648 ALOGE("getService: defaultServiceManager() is null");
649 return nullptr;
650 }
651
652 Return<Transport> transportRet = sm->getTransport(descriptor, instance);
653
654 if (!transportRet.isOk()) {
655 ALOGE("getService: defaultServiceManager()->getTransport returns %s",
656 transportRet.description().c_str());
657 return nullptr;
658 }
659 Transport transport = transportRet;
660 const bool vintfHwbinder = (transport == Transport::HWBINDER);
661 const bool vintfPassthru = (transport == Transport::PASSTHROUGH);
662
Steven Moreland757394b2017-12-13 14:09:24 -0800663#ifdef ENFORCE_VINTF_MANIFEST
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200664
665#ifdef LIBHIDL_TARGET_DEBUGGABLE
666 const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
667 const bool trebleTestingOverride = env && !strcmp(env, "true");
668 const bool vintfLegacy = (transport == Transport::EMPTY) && trebleTestingOverride;
Steven Moreland757394b2017-12-13 14:09:24 -0800669#else // ENFORCE_VINTF_MANIFEST but not LIBHIDL_TARGET_DEBUGGABLE
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200670 const bool trebleTestingOverride = false;
671 const bool vintfLegacy = false;
672#endif // LIBHIDL_TARGET_DEBUGGABLE
673
Steven Moreland757394b2017-12-13 14:09:24 -0800674#else // not ENFORCE_VINTF_MANIFEST
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200675 const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
676 const bool trebleTestingOverride = env && !strcmp(env, "true");
677 const bool vintfLegacy = (transport == Transport::EMPTY);
Steven Moreland757394b2017-12-13 14:09:24 -0800678#endif // ENFORCE_VINTF_MANIFEST
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200679
Steven Moreland3ae9bf92018-04-19 17:11:39 -0700680 for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) {
681 if (waiter == nullptr && tries > 0) {
Martijn Coenen0e6f8ba2018-03-07 09:51:01 +0100682 waiter = new Waiter(descriptor, instance, sm);
683 }
Steven Moreland3ae9bf92018-04-19 17:11:39 -0700684 if (waiter != nullptr) {
685 waiter->reset(); // don't reorder this -- see comments on reset()
686 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200687 Return<sp<IBase>> ret = sm->get(descriptor, instance);
688 if (!ret.isOk()) {
689 ALOGE("getService: defaultServiceManager()->get returns %s for %s/%s.",
690 ret.description().c_str(), descriptor.c_str(), instance.c_str());
691 break;
692 }
693 sp<IBase> base = ret;
Martijn Coenen773609e2017-10-24 09:57:53 +0200694 if (base != nullptr) {
695 Return<bool> canCastRet =
696 details::canCastInterface(base.get(), descriptor.c_str(), true /* emitError */);
697
698 if (canCastRet.isOk() && canCastRet) {
Steven Moreland3ae9bf92018-04-19 17:11:39 -0700699 if (waiter != nullptr) {
700 waiter->done();
701 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200702 return base; // still needs to be wrapped by Bp class.
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200703 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200704
705 if (!handleCastError(canCastRet, descriptor, instance)) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200706 }
707
Martijn Coenen773609e2017-10-24 09:57:53 +0200708 // In case of legacy or we were not asked to retry, don't.
709 if (vintfLegacy || !retry) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200710
Steven Moreland3ae9bf92018-04-19 17:11:39 -0700711 if (waiter != nullptr) {
712 ALOGI("getService: Trying again for %s/%s...", descriptor.c_str(), instance.c_str());
713 waiter->wait();
714 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200715 }
716
Martijn Coenen0e6f8ba2018-03-07 09:51:01 +0100717 if (waiter != nullptr) {
718 waiter->done();
719 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100720
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200721 if (getStub || vintfPassthru || vintfLegacy) {
722 const sp<IServiceManager> pm = getPassthroughServiceManager();
723 if (pm != nullptr) {
724 sp<IBase> base = pm->get(descriptor, instance).withDefault(nullptr);
725 if (!getStub || trebleTestingOverride) {
726 base = wrapPassthrough(base);
727 }
728 return base;
729 }
730 }
731
732 return nullptr;
733}
734
Steven Morelandcbefd352017-01-23 20:29:05 -0800735}; // namespace details
736
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700737}; // namespace hardware
738}; // namespace android