blob: fb474c1f504ff6573c9f1a4a0950774bc17dafa2 [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>
Jerry Zhang86ae9992018-05-30 17:11:09 -070042#if !defined(__ANDROID_RECOVERY__)
Jiyong Parkba8ace12017-05-15 15:44:39 +090043#include <vndksupport/linker.h>
Jerry Zhang86ae9992018-05-30 17:11:09 -070044#endif
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070045
Steven Moreland89909692018-05-30 14:11:19 -070046#include <android/hidl/manager/1.2/BnHwServiceManager.h>
47#include <android/hidl/manager/1.2/BpHwServiceManager.h>
48#include <android/hidl/manager/1.2/IServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070049
Yifan Hong9a22d1d2017-01-25 14:19:26 -080050#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
51#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
52static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
53
Steven Morelandc1cee2c2017-03-24 16:23:11 +000054using android::base::WaitForProperty;
55
Steven Moreland2a2678e2017-07-21 18:07:38 -070056using IServiceManager1_0 = android::hidl::manager::V1_0::IServiceManager;
57using IServiceManager1_1 = android::hidl::manager::V1_1::IServiceManager;
Steven Moreland89909692018-05-30 14:11:19 -070058using IServiceManager1_2 = android::hidl::manager::V1_2::IServiceManager;
Steven Moreland337e6b62017-01-18 17:25:13 -080059using android::hidl::manager::V1_0::IServiceNotification;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070060
61namespace android {
62namespace hardware {
63
Steven Morelandc1cee2c2017-03-24 16:23:11 +000064static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready";
65
Yifan Hongdeacf142018-07-10 17:33:34 -070066#if defined(__ANDROID_RECOVERY__)
67static constexpr bool kIsRecovery = true;
68#else
69static constexpr bool kIsRecovery = false;
70#endif
71
Steven Morelandc1cee2c2017-03-24 16:23:11 +000072void waitForHwServiceManager() {
73 using std::literals::chrono_literals::operator""s;
74
75 while (!WaitForProperty(kHwServicemanagerReadyProperty, "true", 1s)) {
76 LOG(WARNING) << "Waited for hwservicemanager.ready for a second, waiting another...";
77 }
78}
79
Steven Moreland405d7612017-04-07 20:31:22 -070080bool endsWith(const std::string &in, const std::string &suffix) {
81 return in.size() >= suffix.size() &&
82 in.substr(in.size() - suffix.size()) == suffix;
83}
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070084
Steven Moreland405d7612017-04-07 20:31:22 -070085bool startsWith(const std::string &in, const std::string &prefix) {
86 return in.size() >= prefix.size() &&
87 in.substr(0, prefix.size()) == prefix;
88}
89
90std::string binaryName() {
91 std::ifstream ifs("/proc/self/cmdline");
92 std::string cmdline;
93 if (!ifs.is_open()) {
94 return "";
95 }
96 ifs >> cmdline;
97
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -070098 size_t idx = cmdline.rfind('/');
Steven Moreland405d7612017-04-07 20:31:22 -070099 if (idx != std::string::npos) {
100 cmdline = cmdline.substr(idx + 1);
101 }
102
103 return cmdline;
104}
105
106void tryShortenProcessName(const std::string &packageName) {
107 std::string processName = binaryName();
108
109 if (!startsWith(processName, packageName)) {
110 return;
111 }
112
113 // e.x. android.hardware.module.foo@1.0 -> foo@1.0
114 size_t lastDot = packageName.rfind('.');
115 size_t secondDot = packageName.rfind('.', lastDot - 1);
116
117 if (secondDot == std::string::npos) {
118 return;
119 }
120
121 std::string newName = processName.substr(secondDot + 1,
122 16 /* TASK_COMM_LEN */ - 1);
123 ALOGI("Removing namespace from process name %s to %s.",
124 processName.c_str(), newName.c_str());
125
126 int rc = pthread_setname_np(pthread_self(), newName.c_str());
127 ALOGI_IF(rc != 0, "Removing namespace from process name %s failed.",
128 processName.c_str());
129}
130
131namespace details {
132
133void onRegistration(const std::string &packageName,
134 const std::string& /* interfaceName */,
135 const std::string& /* instanceName */) {
136 tryShortenProcessName(packageName);
137}
138
139} // details
140
Steven Moreland2a2678e2017-07-21 18:07:38 -0700141sp<IServiceManager1_0> defaultServiceManager() {
Steven Moreland89909692018-05-30 14:11:19 -0700142 return defaultServiceManager1_2();
Steven Moreland2a2678e2017-07-21 18:07:38 -0700143}
144sp<IServiceManager1_1> defaultServiceManager1_1() {
Steven Moreland89909692018-05-30 14:11:19 -0700145 return defaultServiceManager1_2();
146}
147sp<IServiceManager1_2> defaultServiceManager1_2() {
148 using android::hidl::manager::V1_2::BnHwServiceManager;
149 using android::hidl::manager::V1_2::BpHwServiceManager;
150
151 static std::mutex gDefaultServiceManagerLock;
152 static sp<IServiceManager1_2> gDefaultServiceManager;
153
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700154 {
Steven Moreland89909692018-05-30 14:11:19 -0700155 std::lock_guard<std::mutex> _l(gDefaultServiceManagerLock);
156 if (gDefaultServiceManager != nullptr) {
157 return gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -0700158 }
Steven Moreland405d7612017-04-07 20:31:22 -0700159
Yifan Hong8fb656b2017-03-16 14:30:40 -0700160 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
161 // HwBinder not available on this device or not accessible to
162 // this process.
163 return nullptr;
164 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000165
166 waitForHwServiceManager();
167
Steven Moreland89909692018-05-30 14:11:19 -0700168 while (gDefaultServiceManager == nullptr) {
169 gDefaultServiceManager =
170 fromBinder<IServiceManager1_2, BpHwServiceManager, BnHwServiceManager>(
171 ProcessState::self()->getContextObject(nullptr));
172 if (gDefaultServiceManager == nullptr) {
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000173 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700174 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -0700175 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700176 }
177 }
178
Steven Moreland89909692018-05-30 14:11:19 -0700179 return gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700180}
181
Steven Moreland0091c092017-01-20 23:15:18 +0000182std::vector<std::string> search(const std::string &path,
183 const std::string &prefix,
184 const std::string &suffix) {
185 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
186 if (!dir) return {};
187
188 std::vector<std::string> results{};
189
190 dirent* dp;
191 while ((dp = readdir(dir.get())) != nullptr) {
192 std::string name = dp->d_name;
193
Steven Moreland819c05d2017-04-06 17:24:22 -0700194 if (startsWith(name, prefix) &&
195 endsWith(name, suffix)) {
Steven Moreland0091c092017-01-20 23:15:18 +0000196 results.push_back(name);
197 }
198 }
199
200 return results;
201}
202
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700203bool matchPackageName(const std::string& lib, std::string* matchedName, std::string* implName) {
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800204 std::smatch match;
205 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
206 *matchedName = match.str(1) + "::I*";
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700207 *implName = match.str(2);
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800208 return true;
209 }
210 return false;
211}
212
Yifan Hong7f49f592017-02-03 15:11:44 -0800213static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700214 if (kIsRecovery) {
215 // No hwservicemanager in recovery.
216 return;
217 }
218
Steven Moreland2a2678e2017-07-21 18:07:38 -0700219 sp<IServiceManager1_0> binderizedManager = defaultServiceManager();
Yifan Hong7f49f592017-02-03 15:11:44 -0800220 if (binderizedManager == nullptr) {
221 LOG(WARNING) << "Could not registerReference for "
222 << interfaceName << "/" << instanceName
223 << ": null binderized manager.";
224 return;
225 }
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700226 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName);
Yifan Hong7f49f592017-02-03 15:11:44 -0800227 if (!ret.isOk()) {
228 LOG(WARNING) << "Could not registerReference for "
229 << interfaceName << "/" << instanceName
230 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700231 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800232 }
Steven Morelande681aa52017-02-15 16:22:37 -0800233 LOG(VERBOSE) << "Successfully registerReference for "
234 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800235}
236
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700237using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo;
238static inline void fetchPidsForPassthroughLibraries(
239 std::map<std::string, InstanceDebugInfo>* infos) {
240 static const std::string proc = "/proc/";
241
242 std::map<std::string, std::set<pid_t>> pids;
243 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir);
244 if (!dir) return;
245 dirent* dp;
246 while ((dp = readdir(dir.get())) != nullptr) {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700247 pid_t pid = strtoll(dp->d_name, nullptr, 0);
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700248 if (pid == 0) continue;
249 std::string mapsPath = proc + dp->d_name + "/maps";
250 std::ifstream ifs{mapsPath};
251 if (!ifs.is_open()) continue;
252
253 for (std::string line; std::getline(ifs, line);) {
254 // The last token of line should look like
255 // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so
256 // Use some simple filters to ignore bad lines before extracting libFileName
257 // and checking the key in info to make parsing faster.
258 if (line.back() != 'o') continue;
259 if (line.rfind('@') == std::string::npos) continue;
260
261 auto spacePos = line.rfind(' ');
262 if (spacePos == std::string::npos) continue;
263 auto libFileName = line.substr(spacePos + 1);
264 auto it = infos->find(libFileName);
265 if (it == infos->end()) continue;
266 pids[libFileName].insert(pid);
267 }
268 }
269 for (auto& pair : *infos) {
270 pair.second.clientPids =
271 std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()};
272 }
273}
274
Steven Moreland2a2678e2017-07-21 18:07:38 -0700275struct PassthroughServiceManager : IServiceManager1_1 {
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700276 static void openLibs(
277 const std::string& fqName,
278 const std::function<bool /* continue */ (void* /* handle */, const std::string& /* lib */,
279 const std::string& /* sym */)>& eachLib) {
Steven Moreland819c05d2017-04-06 17:24:22 -0700280 //fqName looks like android.hardware.foo@1.0::IFoo
Steven Moreland519306f2017-06-06 17:14:12 -0700281 size_t idx = fqName.find("::");
Steven Moreland819c05d2017-04-06 17:24:22 -0700282
283 if (idx == std::string::npos ||
Steven Moreland519306f2017-06-06 17:14:12 -0700284 idx + strlen("::") + 1 >= fqName.size()) {
Steven Moreland337e6b62017-01-18 17:25:13 -0800285 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
Steven Moreland519306f2017-06-06 17:14:12 -0700286 return;
Steven Moreland337e6b62017-01-18 17:25:13 -0800287 }
288
Steven Moreland519306f2017-06-06 17:14:12 -0700289 std::string packageAndVersion = fqName.substr(0, idx);
290 std::string ifaceName = fqName.substr(idx + strlen("::"));
Steven Moreland819c05d2017-04-06 17:24:22 -0700291
292 const std::string prefix = packageAndVersion + "-impl";
293 const std::string sym = "HIDL_FETCH_" + ifaceName;
Steven Moreland348802d2017-02-23 12:48:55 -0800294
Steven Moreland77f4c852017-10-12 11:05:53 -0700295 constexpr int dlMode = RTLD_LAZY;
296 void* handle = nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800297
Steven Morelanda29905c2017-03-01 10:42:35 -0800298 dlerror(); // clear
299
Justin Yun1f048102017-12-01 15:30:08 +0900300 static std::string halLibPathVndkSp = android::base::StringPrintf(
301 HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION, details::getVndkVersionStr().c_str());
Steven Morelandf7dea692017-07-14 12:49:28 -0700302 std::vector<std::string> paths = {HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR,
Justin Yun1f048102017-12-01 15:30:08 +0900303 halLibPathVndkSp, HAL_LIBRARY_PATH_SYSTEM};
Steven Moreland77f4c852017-10-12 11:05:53 -0700304
Steven Morelandf7dea692017-07-14 12:49:28 -0700305#ifdef LIBHIDL_TARGET_DEBUGGABLE
306 const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
307 const bool trebleTestingOverride = env && !strcmp(env, "true");
308 if (trebleTestingOverride) {
Steven Moreland77f4c852017-10-12 11:05:53 -0700309 // Load HAL implementations that are statically linked
310 handle = dlopen(nullptr, dlMode);
311 if (handle == nullptr) {
312 const char* error = dlerror();
313 LOG(ERROR) << "Failed to dlopen self: "
314 << (error == nullptr ? "unknown error" : error);
315 } else if (!eachLib(handle, "SELF", sym)) {
316 return;
317 }
318
Steven Morelandf7dea692017-07-14 12:49:28 -0700319 const char* vtsRootPath = std::getenv("VTS_ROOT_PATH");
320 if (vtsRootPath && strlen(vtsRootPath) > 0) {
321 const std::string halLibraryPathVtsOverride =
322 std::string(vtsRootPath) + HAL_LIBRARY_PATH_SYSTEM;
Steven Moreland77f4c852017-10-12 11:05:53 -0700323 paths.insert(paths.begin(), halLibraryPathVtsOverride);
Steven Morelandf7dea692017-07-14 12:49:28 -0700324 }
325 }
326#endif
Steven Moreland77f4c852017-10-12 11:05:53 -0700327
Steven Morelandf7dea692017-07-14 12:49:28 -0700328 for (const std::string& path : paths) {
Steven Moreland0091c092017-01-20 23:15:18 +0000329 std::vector<std::string> libs = search(path, prefix, ".so");
330
Steven Moreland0091c092017-01-20 23:15:18 +0000331 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800332 const std::string fullPath = path + lib;
333
Yifan Hongdeacf142018-07-10 17:33:34 -0700334 if (kIsRecovery || path == HAL_LIBRARY_PATH_SYSTEM) {
Jiyong Park3ab546c2017-04-06 20:28:07 +0900335 handle = dlopen(fullPath.c_str(), dlMode);
Steven Moreland65c00cb2017-10-12 11:35:22 -0700336 } else {
Jerry Zhang86ae9992018-05-30 17:11:09 -0700337#if !defined(__ANDROID_RECOVERY__)
Steven Moreland65c00cb2017-10-12 11:35:22 -0700338 handle = android_load_sphal_library(fullPath.c_str(), dlMode);
Jerry Zhang86ae9992018-05-30 17:11:09 -0700339#endif
Jiyong Park3ab546c2017-04-06 20:28:07 +0900340 }
341
Steven Moreland348802d2017-02-23 12:48:55 -0800342 if (handle == nullptr) {
343 const char* error = dlerror();
344 LOG(ERROR) << "Failed to dlopen " << lib << ": "
345 << (error == nullptr ? "unknown error" : error);
346 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000347 }
Steven Moreland348802d2017-02-23 12:48:55 -0800348
Steven Moreland519306f2017-06-06 17:14:12 -0700349 if (!eachLib(handle, lib, sym)) {
350 return;
Steven Moreland348802d2017-02-23 12:48:55 -0800351 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800352 }
353 }
Steven Moreland519306f2017-06-06 17:14:12 -0700354 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800355
Steven Moreland519306f2017-06-06 17:14:12 -0700356 Return<sp<IBase>> get(const hidl_string& fqName,
357 const hidl_string& name) override {
358 sp<IBase> ret = nullptr;
359
360 openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) {
361 IBase* (*generator)(const char* name);
362 *(void **)(&generator) = dlsym(handle, sym.c_str());
363 if(!generator) {
364 const char* error = dlerror();
365 LOG(ERROR) << "Passthrough lookup opened " << lib
366 << " but could not find symbol " << sym << ": "
367 << (error == nullptr ? "unknown error" : error);
368 dlclose(handle);
369 return true;
370 }
371
372 ret = (*generator)(name.c_str());
373
374 if (ret == nullptr) {
375 dlclose(handle);
376 return true; // this module doesn't provide this instance name
377 }
378
Steven Moreland83cb4b32018-03-14 10:55:42 -0700379 // Actual fqname might be a subclass.
380 // This assumption is tested in vts_treble_vintf_test
381 using ::android::hardware::details::getDescriptor;
382 std::string actualFqName = getDescriptor(ret.get());
383 CHECK(actualFqName.size() > 0);
384 registerReference(actualFqName, name);
Steven Moreland519306f2017-06-06 17:14:12 -0700385 return false;
386 });
387
388 return ret;
Steven Moreland337e6b62017-01-18 17:25:13 -0800389 }
390
Martijn Coenen67a02492017-03-06 13:04:48 +0100391 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800392 const sp<IBase>& /* service */) override {
393 LOG(FATAL) << "Cannot register services with passthrough service manager.";
394 return false;
395 }
396
Steven Morelandc601c5f2017-04-06 09:26:07 -0700397 Return<Transport> getTransport(const hidl_string& /* fqName */,
398 const hidl_string& /* name */) {
399 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
400 return Transport::EMPTY;
401 }
402
Yifan Hong705e5da2017-03-02 16:59:39 -0800403 Return<void> list(list_cb /* _hidl_cb */) override {
404 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800405 return Void();
406 }
407 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
408 listByInterface_cb /* _hidl_cb */) override {
409 // TODO: add this functionality
410 LOG(FATAL) << "Cannot list services with passthrough service manager.";
411 return Void();
412 }
413
414 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
415 const hidl_string& /* name */,
416 const sp<IServiceNotification>& /* callback */) override {
417 // This makes no sense.
418 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
419 return false;
420 }
421
Yifan Hong705e5da2017-03-02 16:59:39 -0800422 Return<void> debugDump(debugDump_cb _hidl_cb) override {
423 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700424 using std::literals::string_literals::operator""s;
Justin Yun1f048102017-12-01 15:30:08 +0900425 static std::string halLibPathVndkSp64 = android::base::StringPrintf(
426 HAL_LIBRARY_PATH_VNDK_SP_64BIT_FOR_VERSION, details::getVndkVersionStr().c_str());
427 static std::string halLibPathVndkSp32 = android::base::StringPrintf(
428 HAL_LIBRARY_PATH_VNDK_SP_32BIT_FOR_VERSION, details::getVndkVersionStr().c_str());
Jiyong Park56eb1492017-08-04 16:16:13 +0900429 static std::vector<std::pair<Arch, std::vector<const char*>>> sAllPaths{
430 {Arch::IS_64BIT,
431 {HAL_LIBRARY_PATH_ODM_64BIT, HAL_LIBRARY_PATH_VENDOR_64BIT,
Justin Yun1f048102017-12-01 15:30:08 +0900432 halLibPathVndkSp64.c_str(), HAL_LIBRARY_PATH_SYSTEM_64BIT}},
Jiyong Park56eb1492017-08-04 16:16:13 +0900433 {Arch::IS_32BIT,
434 {HAL_LIBRARY_PATH_ODM_32BIT, HAL_LIBRARY_PATH_VENDOR_32BIT,
Justin Yun1f048102017-12-01 15:30:08 +0900435 halLibPathVndkSp32.c_str(), HAL_LIBRARY_PATH_SYSTEM_32BIT}}};
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700436 std::map<std::string, InstanceDebugInfo> map;
Yifan Hong705e5da2017-03-02 16:59:39 -0800437 for (const auto &pair : sAllPaths) {
438 Arch arch = pair.first;
439 for (const auto &path : pair.second) {
440 std::vector<std::string> libs = search(path, "", ".so");
441 for (const std::string &lib : libs) {
442 std::string matchedName;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700443 std::string implName;
444 if (matchPackageName(lib, &matchedName, &implName)) {
445 std::string instanceName{"* ("s + path + ")"s};
446 if (!implName.empty()) instanceName += " ("s + implName + ")"s;
447 map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName,
448 .instanceName = instanceName,
449 .clientPids = {},
450 .arch = arch});
Yifan Hong705e5da2017-03-02 16:59:39 -0800451 }
452 }
453 }
454 }
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700455 fetchPidsForPassthroughLibraries(&map);
456 hidl_vec<InstanceDebugInfo> vec;
457 vec.resize(map.size());
458 size_t idx = 0;
459 for (auto&& pair : map) {
460 vec[idx++] = std::move(pair.second);
461 }
Yifan Hong705e5da2017-03-02 16:59:39 -0800462 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800463 return Void();
464 }
465
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700466 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override {
Yifan Hong7f49f592017-02-03 15:11:44 -0800467 // This makes no sense.
468 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
469 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800470 return Void();
471 }
472
Steven Moreland2a2678e2017-07-21 18:07:38 -0700473 Return<bool> unregisterForNotifications(const hidl_string& /* fqName */,
474 const hidl_string& /* name */,
475 const sp<IServiceNotification>& /* callback */) override {
476 // This makes no sense.
477 LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager.";
478 return false;
479 }
480
Steven Moreland337e6b62017-01-18 17:25:13 -0800481};
482
Steven Moreland2a2678e2017-07-21 18:07:38 -0700483sp<IServiceManager1_0> getPassthroughServiceManager() {
484 return getPassthroughServiceManager1_1();
485}
486sp<IServiceManager1_1> getPassthroughServiceManager1_1() {
Steven Moreland337e6b62017-01-18 17:25:13 -0800487 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
488 return manager;
489}
490
Steven Morelandcbefd352017-01-23 20:29:05 -0800491namespace details {
492
Steven Moreland519306f2017-06-06 17:14:12 -0700493void preloadPassthroughService(const std::string &descriptor) {
494 PassthroughServiceManager::openLibs(descriptor,
495 [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) {
496 // do nothing
497 return true; // open all libs
498 });
499}
500
Steven Morelandcbefd352017-01-23 20:29:05 -0800501struct Waiter : IServiceNotification {
Martijn Coenen773609e2017-10-24 09:57:53 +0200502 Waiter(const std::string& interface, const std::string& instanceName,
503 const sp<IServiceManager1_1>& sm) : mInterfaceName(interface),
504 mInstanceName(instanceName), mSm(sm) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100505 }
506
507 void onFirstRef() override {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200508 // If this process only has one binder thread, and we're calling wait() from
509 // that thread, it will block forever because we hung up the one and only
510 // binder thread on a condition variable that can only be notified by an
511 // incoming binder call.
Tobias Lindskog88e886f2018-01-05 10:32:43 +0100512 if (IPCThreadState::self()->isOnlyBinderThread()) {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200513 LOG(WARNING) << "Can't efficiently wait for " << mInterfaceName << "/"
514 << mInstanceName << ", because we are called from "
515 << "the only binder thread in this process.";
516 return;
517 }
518
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100519 Return<bool> ret = mSm->registerForNotifications(mInterfaceName, mInstanceName, this);
Martijn Coenen773609e2017-10-24 09:57:53 +0200520
521 if (!ret.isOk()) {
522 LOG(ERROR) << "Transport error, " << ret.description()
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100523 << ", during notification registration for " << mInterfaceName << "/"
524 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200525 return;
526 }
527
528 if (!ret) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100529 LOG(ERROR) << "Could not register for notifications for " << mInterfaceName << "/"
530 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200531 return;
532 }
533
534 mRegisteredForNotifications = true;
535 }
536
537 ~Waiter() {
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100538 if (!mDoneCalled) {
539 LOG(FATAL)
540 << "Waiter still registered for notifications, call done() before dropping ref!";
Martijn Coenen773609e2017-10-24 09:57:53 +0200541 }
542 }
543
Steven Morelandcbefd352017-01-23 20:29:05 -0800544 Return<void> onRegistration(const hidl_string& /* fqName */,
545 const hidl_string& /* name */,
546 bool /* preexisting */) override {
547 std::unique_lock<std::mutex> lock(mMutex);
548 if (mRegistered) {
549 return Void();
550 }
551 mRegistered = true;
552 lock.unlock();
553
554 mCondition.notify_one();
555 return Void();
556 }
557
Steven Moreland0771d8a2018-05-09 13:29:14 -0700558 void wait(bool timeout) {
Steven Moreland49605102017-03-28 09:33:06 -0700559 using std::literals::chrono_literals::operator""s;
560
Martijn Coenen773609e2017-10-24 09:57:53 +0200561 if (!mRegisteredForNotifications) {
562 // As an alternative, just sleep for a second and return
563 LOG(WARNING) << "Waiting one second for " << mInterfaceName << "/" << mInstanceName;
564 sleep(1);
565 return;
566 }
567
Steven Morelandcbefd352017-01-23 20:29:05 -0800568 std::unique_lock<std::mutex> lock(mMutex);
Steven Moreland0771d8a2018-05-09 13:29:14 -0700569 do {
Steven Moreland49605102017-03-28 09:33:06 -0700570 mCondition.wait_for(lock, 1s, [this]{
571 return mRegistered;
572 });
573
574 if (mRegistered) {
575 break;
576 }
577
Steven Moreland0771d8a2018-05-09 13:29:14 -0700578 LOG(WARNING) << "Waited one second for " << mInterfaceName << "/" << mInstanceName;
579 } while (!timeout);
Steven Morelandcbefd352017-01-23 20:29:05 -0800580 }
581
Martijn Coenen773609e2017-10-24 09:57:53 +0200582 // Be careful when using this; after calling reset(), you must always try to retrieve
583 // the corresponding service before blocking on the waiter; otherwise, you might run
584 // into a race-condition where the service has just (re-)registered, you clear the state
585 // here, and subsequently calling waiter->wait() will block forever.
586 void reset() {
587 std::unique_lock<std::mutex> lock(mMutex);
588 mRegistered = false;
589 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100590
591 // done() must be called before dropping the last strong ref to the Waiter, to make
592 // sure we can properly unregister with hwservicemanager.
593 void done() {
594 if (mRegisteredForNotifications) {
595 if (!mSm->unregisterForNotifications(mInterfaceName, mInstanceName, this)
596 .withDefault(false)) {
597 LOG(ERROR) << "Could not unregister service notification for " << mInterfaceName
598 << "/" << mInstanceName << ".";
599 } else {
600 mRegisteredForNotifications = false;
601 }
602 }
603 mDoneCalled = true;
604 }
605
606 private:
Martijn Coenen773609e2017-10-24 09:57:53 +0200607 const std::string mInterfaceName;
608 const std::string mInstanceName;
609 const sp<IServiceManager1_1>& mSm;
Steven Morelandcbefd352017-01-23 20:29:05 -0800610 std::mutex mMutex;
611 std::condition_variable mCondition;
612 bool mRegistered = false;
Martijn Coenen773609e2017-10-24 09:57:53 +0200613 bool mRegisteredForNotifications = false;
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100614 bool mDoneCalled = false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800615};
616
617void waitForHwService(
618 const std::string &interface, const std::string &instanceName) {
Martijn Coenen773609e2017-10-24 09:57:53 +0200619 sp<Waiter> waiter = new Waiter(interface, instanceName, defaultServiceManager1_1());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700620 waiter->wait(false /* timeout */);
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100621 waiter->done();
Martijn Coenen773609e2017-10-24 09:57:53 +0200622}
Steven Morelandcbefd352017-01-23 20:29:05 -0800623
Martijn Coenen773609e2017-10-24 09:57:53 +0200624// Prints relevant error/warning messages for error return values from
625// details::canCastInterface(), both transaction errors (!castReturn.isOk())
626// as well as actual cast failures (castReturn.isOk() && castReturn = false).
627// Returns 'true' if the error is non-fatal and it's useful to retry
628bool handleCastError(const Return<bool>& castReturn, const std::string& descriptor,
629 const std::string& instance) {
630 if (castReturn.isOk()) {
631 if (castReturn) {
632 details::logAlwaysFatal("Successful cast value passed into handleCastError.");
633 }
634 // This should never happen, and there's not really a point in retrying.
635 ALOGE("getService: received incompatible service (bug in hwservicemanager?) for "
636 "%s/%s.", descriptor.c_str(), instance.c_str());
637 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800638 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200639 if (castReturn.isDeadObject()) {
640 ALOGW("getService: found dead hwbinder service for %s/%s.", descriptor.c_str(),
641 instance.c_str());
642 return true;
Steven Morelandcbefd352017-01-23 20:29:05 -0800643 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200644 // This can happen due to:
645 // 1) No SELinux permissions
646 // 2) Other transaction failure (no buffer space, kernel error)
647 // The first isn't recoverable, but the second is.
648 // Since we can't yet differentiate between the two, and clients depend
649 // on us not blocking in case 1), treat this as a fatal error for now.
650 ALOGW("getService: unable to call into hwbinder service for %s/%s.",
651 descriptor.c_str(), instance.c_str());
652 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800653}
654
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200655sp<::android::hidl::base::V1_0::IBase> getRawServiceInternal(const std::string& descriptor,
656 const std::string& instance,
657 bool retry, bool getStub) {
658 using Transport = ::android::hidl::manager::V1_0::IServiceManager::Transport;
659 using ::android::hidl::base::V1_0::IBase;
660 using ::android::hidl::manager::V1_0::IServiceManager;
Martijn Coenen0e6f8ba2018-03-07 09:51:01 +0100661 sp<Waiter> waiter;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200662
Yifan Hongdeacf142018-07-10 17:33:34 -0700663 sp<IServiceManager1_1> sm;
664 Transport transport = Transport::EMPTY;
665 if (kIsRecovery) {
666 // TODO(b/80132328): Should check manifest in recovery as well.
667 transport = Transport::PASSTHROUGH;
668 // No hwbinder HALs in recovery.
669 getStub = true;
670 } else {
671 sm = defaultServiceManager1_1();
672 if (sm == nullptr) {
673 ALOGE("getService: defaultServiceManager() is null");
674 return nullptr;
675 }
676
677 Return<Transport> transportRet = sm->getTransport(descriptor, instance);
678
679 if (!transportRet.isOk()) {
680 ALOGE("getService: defaultServiceManager()->getTransport returns %s",
681 transportRet.description().c_str());
682 return nullptr;
683 }
684 transport = transportRet;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200685 }
686
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200687 const bool vintfHwbinder = (transport == Transport::HWBINDER);
688 const bool vintfPassthru = (transport == Transport::PASSTHROUGH);
689
Steven Moreland757394b2017-12-13 14:09:24 -0800690#ifdef ENFORCE_VINTF_MANIFEST
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200691
692#ifdef LIBHIDL_TARGET_DEBUGGABLE
693 const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
694 const bool trebleTestingOverride = env && !strcmp(env, "true");
695 const bool vintfLegacy = (transport == Transport::EMPTY) && trebleTestingOverride;
Steven Moreland757394b2017-12-13 14:09:24 -0800696#else // ENFORCE_VINTF_MANIFEST but not LIBHIDL_TARGET_DEBUGGABLE
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200697 const bool trebleTestingOverride = false;
698 const bool vintfLegacy = false;
699#endif // LIBHIDL_TARGET_DEBUGGABLE
700
Steven Moreland757394b2017-12-13 14:09:24 -0800701#else // not ENFORCE_VINTF_MANIFEST
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200702 const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
703 const bool trebleTestingOverride = env && !strcmp(env, "true");
704 const bool vintfLegacy = (transport == Transport::EMPTY);
Steven Moreland757394b2017-12-13 14:09:24 -0800705#endif // ENFORCE_VINTF_MANIFEST
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200706
Steven Moreland3ae9bf92018-04-19 17:11:39 -0700707 for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) {
708 if (waiter == nullptr && tries > 0) {
Martijn Coenen0e6f8ba2018-03-07 09:51:01 +0100709 waiter = new Waiter(descriptor, instance, sm);
710 }
Steven Moreland3ae9bf92018-04-19 17:11:39 -0700711 if (waiter != nullptr) {
712 waiter->reset(); // don't reorder this -- see comments on reset()
713 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200714 Return<sp<IBase>> ret = sm->get(descriptor, instance);
715 if (!ret.isOk()) {
716 ALOGE("getService: defaultServiceManager()->get returns %s for %s/%s.",
717 ret.description().c_str(), descriptor.c_str(), instance.c_str());
718 break;
719 }
720 sp<IBase> base = ret;
Martijn Coenen773609e2017-10-24 09:57:53 +0200721 if (base != nullptr) {
722 Return<bool> canCastRet =
723 details::canCastInterface(base.get(), descriptor.c_str(), true /* emitError */);
724
725 if (canCastRet.isOk() && canCastRet) {
Steven Moreland3ae9bf92018-04-19 17:11:39 -0700726 if (waiter != nullptr) {
727 waiter->done();
728 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200729 return base; // still needs to be wrapped by Bp class.
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200730 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200731
732 if (!handleCastError(canCastRet, descriptor, instance)) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200733 }
734
Martijn Coenen773609e2017-10-24 09:57:53 +0200735 // In case of legacy or we were not asked to retry, don't.
736 if (vintfLegacy || !retry) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200737
Steven Moreland3ae9bf92018-04-19 17:11:39 -0700738 if (waiter != nullptr) {
739 ALOGI("getService: Trying again for %s/%s...", descriptor.c_str(), instance.c_str());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700740 waiter->wait(true /* timeout */);
Steven Moreland3ae9bf92018-04-19 17:11:39 -0700741 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200742 }
743
Martijn Coenen0e6f8ba2018-03-07 09:51:01 +0100744 if (waiter != nullptr) {
745 waiter->done();
746 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100747
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200748 if (getStub || vintfPassthru || vintfLegacy) {
749 const sp<IServiceManager> pm = getPassthroughServiceManager();
750 if (pm != nullptr) {
751 sp<IBase> base = pm->get(descriptor, instance).withDefault(nullptr);
752 if (!getStub || trebleTestingOverride) {
753 base = wrapPassthrough(base);
754 }
755 return base;
756 }
757 }
758
759 return nullptr;
760}
761
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +0200762} // namespace details
Steven Morelandcbefd352017-01-23 20:29:05 -0800763
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +0200764} // namespace hardware
765} // namespace android