blob: b5e02dffeae1a0db0a569fcb7942ba65aa20c026 [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
Steven Morelandfcaa97f2019-06-24 12:07:22 -070017#define LOG_TAG "HidlServiceManagement"
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070018
Steven Moreland2056cf32019-09-17 17:41:02 -070019#ifdef __ANDROID__
Devin Moore22e6f1e2023-11-22 22:29:54 +000020#include <android/api-level.h>
Jiyong Park3ab546c2017-04-06 20:28:07 +090021#include <android/dlext.h>
Steven Moreland2056cf32019-09-17 17:41:02 -070022#endif // __ANDROID__
23
Yifan Hong9a22d1d2017-01-25 14:19:26 -080024#include <condition_variable>
25#include <dlfcn.h>
26#include <dirent.h>
Steven Moreland405d7612017-04-07 20:31:22 -070027#include <fstream>
28#include <pthread.h>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080029#include <unistd.h>
30
31#include <mutex>
32#include <regex>
Yifan Hongbd0d8f72017-05-24 19:43:51 -070033#include <set>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080034
Martijn Coenen12f04d92016-12-07 17:29:41 +010035#include <hidl/HidlBinderSupport.h>
Justin Yun1f048102017-12-01 15:30:08 +090036#include <hidl/HidlInternal.h>
Steven Morelande69e8d32018-03-14 10:55:42 -070037#include <hidl/HidlTransportUtils.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070038#include <hidl/ServiceManagement.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070039#include <hidl/Status.h>
Peter Kalauskas64ffced2018-09-05 16:41:08 -070040#include <utils/SystemClock.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070041
Peter Kalauskas64ffced2018-09-05 16:41:08 -070042#include <android-base/file.h>
Steven Moreland337e6b62017-01-18 17:25:13 -080043#include <android-base/logging.h>
Peter Kalauskas64ffced2018-09-05 16:41:08 -070044#include <android-base/parseint.h>
Steven Morelandc1cee2c2017-03-24 16:23:11 +000045#include <android-base/properties.h>
Justin Yun1f048102017-12-01 15:30:08 +090046#include <android-base/stringprintf.h>
Peter Kalauskas64ffced2018-09-05 16:41:08 -070047#include <android-base/strings.h>
Devin Moore17355cb2024-04-02 21:37:59 +000048#include <hwbinder/HidlSupport.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070049#include <hwbinder/IPCThreadState.h>
50#include <hwbinder/Parcel.h>
Steven Moreland2056cf32019-09-17 17:41:02 -070051#if !defined(__ANDROID_RECOVERY__) && defined(__ANDROID__)
Jiyong Parkba8ace12017-05-15 15:44:39 +090052#include <vndksupport/linker.h>
Jerry Zhang86ae9992018-05-30 17:11:09 -070053#endif
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070054
Steven Moreland89909692018-05-30 14:11:19 -070055#include <android/hidl/manager/1.2/BnHwServiceManager.h>
56#include <android/hidl/manager/1.2/BpHwServiceManager.h>
57#include <android/hidl/manager/1.2/IServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070058
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070059using ::android::hidl::base::V1_0::IBase;
Steven Moreland2a2678e2017-07-21 18:07:38 -070060using IServiceManager1_0 = android::hidl::manager::V1_0::IServiceManager;
61using IServiceManager1_1 = android::hidl::manager::V1_1::IServiceManager;
Steven Moreland89909692018-05-30 14:11:19 -070062using IServiceManager1_2 = android::hidl::manager::V1_2::IServiceManager;
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070063using ::android::hidl::manager::V1_0::IServiceNotification;
Devin Mooree1973ce2022-04-15 20:54:03 +000064using ::android::hidl::manager::V1_2::IClientCallback;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070065
66namespace android {
67namespace hardware {
68
Yifan Hongdeacf142018-07-10 17:33:34 -070069#if defined(__ANDROID_RECOVERY__)
70static constexpr bool kIsRecovery = true;
71#else
72static constexpr bool kIsRecovery = false;
73#endif
74
Devin Moore50ced9f2024-03-26 22:00:23 +000075bool isHidlSupported() {
Devin Moore17355cb2024-04-02 21:37:59 +000076 return isHwbinderSupportedBlocking();
Steven Morelandc1cee2c2017-03-24 16:23:11 +000077}
78
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070079static std::string binaryName() {
Steven Moreland405d7612017-04-07 20:31:22 -070080 std::ifstream ifs("/proc/self/cmdline");
81 std::string cmdline;
Steven Morelandd682c0e2020-07-21 17:47:32 +000082 if (!ifs) {
Steven Moreland405d7612017-04-07 20:31:22 -070083 return "";
84 }
85 ifs >> cmdline;
86
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -070087 size_t idx = cmdline.rfind('/');
Steven Moreland405d7612017-04-07 20:31:22 -070088 if (idx != std::string::npos) {
89 cmdline = cmdline.substr(idx + 1);
90 }
91
92 return cmdline;
93}
94
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070095static std::string packageWithoutVersion(const std::string& packageAndVersion) {
Steven Moreland7d09a402018-04-06 10:44:05 -070096 size_t at = packageAndVersion.find('@');
97 if (at == std::string::npos) return packageAndVersion;
98 return packageAndVersion.substr(0, at);
99}
100
Steven Morelandd682c0e2020-07-21 17:47:32 +0000101__attribute__((noinline)) static void tryShortenProcessName(const std::string& descriptor) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700102 const static std::string kTasks = "/proc/self/task/";
103
104 // make sure that this binary name is in the same package
Steven Moreland405d7612017-04-07 20:31:22 -0700105 std::string processName = binaryName();
106
Steven Moreland7d09a402018-04-06 10:44:05 -0700107 // e.x. android.hardware.foo is this package
Steven Moreland8092aa02018-10-12 15:54:56 -0700108 if (!base::StartsWith(packageWithoutVersion(processName), packageWithoutVersion(descriptor))) {
Steven Moreland405d7612017-04-07 20:31:22 -0700109 return;
110 }
111
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700112 // e.x. android.hardware.module.foo@1.2::IFoo -> foo@1.2
113 size_t lastDot = descriptor.rfind('.');
Steven Moreland7d09a402018-04-06 10:44:05 -0700114 if (lastDot == std::string::npos) return;
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700115 size_t secondDot = descriptor.rfind('.', lastDot - 1);
Steven Moreland7d09a402018-04-06 10:44:05 -0700116 if (secondDot == std::string::npos) return;
Steven Moreland405d7612017-04-07 20:31:22 -0700117
Steven Moreland7d09a402018-04-06 10:44:05 -0700118 std::string newName = processName.substr(secondDot + 1, std::string::npos);
119 ALOGI("Removing namespace from process name %s to %s.", processName.c_str(), newName.c_str());
120
121 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(kTasks.c_str()), closedir);
122 if (dir == nullptr) return;
123
124 dirent* dp;
125 while ((dp = readdir(dir.get())) != nullptr) {
126 if (dp->d_type != DT_DIR) continue;
127 if (dp->d_name[0] == '.') continue;
128
129 std::fstream fs(kTasks + dp->d_name + "/comm");
Steven Morelandd682c0e2020-07-21 17:47:32 +0000130 if (!fs) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700131 ALOGI("Could not rename process, failed read comm for %s.", dp->d_name);
132 continue;
133 }
134
135 std::string oldComm;
Steven Morelandd682c0e2020-07-21 17:47:32 +0000136 if (!(fs >> oldComm)) continue;
Steven Moreland7d09a402018-04-06 10:44:05 -0700137
138 // don't rename if it already has an explicit name
Steven Moreland8092aa02018-10-12 15:54:56 -0700139 if (base::StartsWith(descriptor, oldComm)) {
Steven Morelandd682c0e2020-07-21 17:47:32 +0000140 if (!fs.seekg(0, fs.beg)) continue;
Steven Moreland7d09a402018-04-06 10:44:05 -0700141 fs << newName;
142 }
Steven Moreland405d7612017-04-07 20:31:22 -0700143 }
Steven Moreland405d7612017-04-07 20:31:22 -0700144}
145
146namespace details {
147
Steven Morelandb939e7e2020-07-08 01:04:23 +0000148#ifdef ENFORCE_VINTF_MANIFEST
149static constexpr bool kEnforceVintfManifest = true;
150#else
151static constexpr bool kEnforceVintfManifest = false;
152#endif
153
Steven Moreland206d2812020-07-08 21:21:48 +0000154static bool* getTrebleTestingOverridePtr() {
155 static bool gTrebleTestingOverride = false;
156 return &gTrebleTestingOverride;
157}
Steven Morelandb939e7e2020-07-08 01:04:23 +0000158
159void setTrebleTestingOverride(bool testingOverride) {
Steven Moreland206d2812020-07-08 21:21:48 +0000160 *getTrebleTestingOverridePtr() = testingOverride;
Steven Morelandb939e7e2020-07-08 01:04:23 +0000161}
162
Steven Morelandf47cdf72021-04-07 22:13:13 +0000163static bool isDebuggable() {
164 static bool debuggable = base::GetBoolProperty("ro.debuggable", false);
165 return debuggable;
166}
167
Steven Morelandb939e7e2020-07-08 01:04:23 +0000168static inline bool isTrebleTestingOverride() {
Steven Moreland4acdab72022-08-09 16:27:46 +0000169 // return false early so we don't need to check the debuggable property
170 if (!*getTrebleTestingOverridePtr()) return false;
171
Steven Morelandf47cdf72021-04-07 22:13:13 +0000172 if (kEnforceVintfManifest && !isDebuggable()) {
Steven Morelandb939e7e2020-07-08 01:04:23 +0000173 // don't allow testing override in production
174 return false;
175 }
176
Steven Moreland4acdab72022-08-09 16:27:46 +0000177 return true;
Steven Morelandb939e7e2020-07-08 01:04:23 +0000178}
179
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700180static void onRegistrationImpl(const std::string& descriptor, const std::string& instanceName) {
Steven Moreland489e7572021-01-21 19:02:54 +0000181 LOG(INFO) << "Registered " << descriptor << "/" << instanceName;
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700182 tryShortenProcessName(descriptor);
183}
184
Steven Moreland911852c2020-05-11 10:51:26 -0700185// only used by prebuilts - should be able to remove
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700186void onRegistration(const std::string& packageName, const std::string& interfaceName,
187 const std::string& instanceName) {
188 return onRegistrationImpl(packageName + "::" + interfaceName, instanceName);
Steven Moreland405d7612017-04-07 20:31:22 -0700189}
190
191} // details
192
Steven Moreland2a2678e2017-07-21 18:07:38 -0700193sp<IServiceManager1_0> defaultServiceManager() {
Steven Moreland89909692018-05-30 14:11:19 -0700194 return defaultServiceManager1_2();
Steven Moreland2a2678e2017-07-21 18:07:38 -0700195}
196sp<IServiceManager1_1> defaultServiceManager1_1() {
Steven Moreland89909692018-05-30 14:11:19 -0700197 return defaultServiceManager1_2();
198}
Devin Mooree1973ce2022-04-15 20:54:03 +0000199static bool isServiceManager(const hidl_string& fqName) {
200 return fqName == IServiceManager1_0::descriptor || fqName == IServiceManager1_1::descriptor ||
201 fqName == IServiceManager1_2::descriptor;
202}
Devin Moore22e6f1e2023-11-22 22:29:54 +0000203
Devin Mooree1973ce2022-04-15 20:54:03 +0000204/*
205 * A replacement for hwservicemanager when it is not installed on a device.
206 *
207 * Clients in the framework need to continue supporting HIDL services through
208 * hwservicemanager for upgrading devices. Being unable to get an instance of
209 * hardware service manager is a hard error, so this implementation is returned
210 * to be able service the requests and tell clients there are no services
211 * registered.
212 */
Devin Moore2d2e4c02023-11-28 00:24:19 +0000213struct NoHwServiceManager : public IServiceManager1_2, hidl_death_recipient {
Devin Mooree1973ce2022-04-15 20:54:03 +0000214 Return<sp<IBase>> get(const hidl_string& fqName, const hidl_string&) override {
215 sp<IBase> ret = nullptr;
216
217 if (isServiceManager(fqName)) {
218 ret = defaultServiceManager1_2();
219 }
220 return ret;
221 }
222
223 Return<bool> add(const hidl_string& name, const sp<IBase>& /* service */) override {
224 LOG(INFO) << "Cannot add " << name << " without hwservicemanager";
225 return false;
226 }
227
228 Return<Transport> getTransport(const hidl_string& fqName, const hidl_string& name) {
Devin Moore0d309172023-11-28 00:20:32 +0000229 // We pretend like IServiceManager is declared for
230 // IServiceManager::getService to return this NoHwServiceManager
231 // instance
232 if (isServiceManager(fqName)) {
233 return Transport::HWBINDER;
234 }
Devin Mooree1973ce2022-04-15 20:54:03 +0000235 LOG(INFO) << "Trying to get transport of " << fqName << "/" << name
236 << " without hwservicemanager";
Devin Mooreadbf9a52023-06-06 23:07:08 +0000237 return Transport::EMPTY;
Devin Mooree1973ce2022-04-15 20:54:03 +0000238 }
239
240 Return<void> list(list_cb _hidl_cb) override {
241 _hidl_cb({});
242 LOG(INFO) << "Cannot list all services without hwservicemanager";
243 return Void();
244 }
245 Return<void> listByInterface(const hidl_string& fqName, listByInterface_cb _hidl_cb) override {
246 _hidl_cb({});
247 LOG(INFO) << "Cannot list service " << fqName << " without hwservicemanager";
248 return Void();
249 }
250
Devin Moore2d2e4c02023-11-28 00:24:19 +0000251 void serviceDied(uint64_t /* cookie */, const wp<IBase>& who) override {
252 sp<IBase> promoted = who.promote();
253 if (promoted) {
254 bool removed = false;
255 for (auto [name, callbacks] : mServiceNotifications) {
256 for (auto it = callbacks.begin(); it != callbacks.end();) {
257 if (interfacesEqual(*it, promoted)) {
258 callbacks.erase(it);
259 removed = true;
260 }
261 it++;
262 }
263 if (removed) return;
264 }
265 }
266 LOG(ERROR) << "Could not find a registered callback for a service who died. "
267 << "Service pointer: " << who.promote().get();
268 }
269
Devin Mooree1973ce2022-04-15 20:54:03 +0000270 Return<bool> registerForNotifications(const hidl_string& fqName, const hidl_string& name,
Devin Moore2d2e4c02023-11-28 00:24:19 +0000271 const sp<IServiceNotification>& callback) override {
272 LOG(INFO) << "Will not get any notifications for " << fqName << "/" << name
273 << " without hwservicemanager but keeping the callback.";
274 if (callback == nullptr) {
275 LOG(ERROR) << "Cannot register a null callback for " << fqName << "/" << name;
276 return false;
277 }
278 bool ret = callback->linkToDeath(this, 0);
279 if (!ret) {
280 LOG(ERROR) << "Failed to register death recipient for " << fqName << "/" << name;
281 return false;
282 }
283
284 auto [it, inserted] = mServiceNotifications[static_cast<std::string>(fqName) + "/" +
285 static_cast<std::string>(name)]
286 .insert(callback);
287 if (!inserted) {
288 LOG(WARNING) << "This callback for " << fqName << "/" << name
289 << " was already registered so "
290 << "we are not keeping a reference to this one.";
291 return false;
292 }
293 return true;
Devin Mooree1973ce2022-04-15 20:54:03 +0000294 }
295
296 Return<void> debugDump(debugDump_cb _hidl_cb) override {
297 _hidl_cb({});
298 return Void();
299 }
300
301 Return<void> registerPassthroughClient(const hidl_string& fqName,
302 const hidl_string& name) override {
303 LOG(INFO) << "This process is a client of " << fqName << "/" << name
304 << " passthrough HAL, but it won't show up in lshal because hwservicemanager is "
305 "not installed";
306 return Void();
307 }
308
Devin Moore2d2e4c02023-11-28 00:24:19 +0000309 Return<bool> unregisterForNotifications(const hidl_string& fqName, const hidl_string& name,
310 const sp<IServiceNotification>& callback) override {
311 auto ret = mServiceNotifications[static_cast<std::string>(fqName) + "/" +
312 static_cast<std::string>(name)]
313 .erase(callback);
314 if (!ret) {
315 LOG(WARNING) << "This callback for " << fqName << "/" << name << " was not previously "
316 << "registered so there is nothing to do.";
317 return false;
318 }
319 return true;
Devin Mooree1973ce2022-04-15 20:54:03 +0000320 }
Devin Moore2d2e4c02023-11-28 00:24:19 +0000321
Devin Mooree1973ce2022-04-15 20:54:03 +0000322 Return<bool> registerClientCallback(const hidl_string& fqName, const hidl_string& name,
323 const sp<IBase>&, const sp<IClientCallback>&) {
324 LOG(INFO) << "Cannot add client callback for " << fqName << "/" << name
325 << " without hwservicemanager";
326 return false;
327 }
328 Return<bool> unregisterClientCallback(const sp<IBase>&, const sp<IClientCallback>&) {
329 LOG(INFO) << "Cannot unregister client callbacks without hwservicemanager";
330 return false;
331 }
332 Return<bool> addWithChain(const hidl_string& fqName, const sp<IBase>&,
333 const hidl_vec<hidl_string>&) {
334 LOG(INFO) << "Cannot add " << fqName << " with chain without hwservicemanager";
335 return false;
336 }
337 Return<void> listManifestByInterface(const hidl_string& fqName, listManifestByInterface_cb) {
338 LOG(INFO) << "Cannot list manifest for " << fqName << " without hwservicemanager";
339 return Void();
340 }
341 Return<bool> tryUnregister(const hidl_string& fqName, const hidl_string& name,
342 const sp<IBase>&) {
343 LOG(INFO) << "Cannot unregister service " << fqName << "/" << name
344 << " without hwservicemanager";
345 return false;
346 }
Devin Moore2d2e4c02023-11-28 00:24:19 +0000347
348 private:
349 std::map<std::string, std::set<sp<IServiceNotification>>> mServiceNotifications;
Devin Mooree1973ce2022-04-15 20:54:03 +0000350};
351
Steven Moreland89909692018-05-30 14:11:19 -0700352sp<IServiceManager1_2> defaultServiceManager1_2() {
353 using android::hidl::manager::V1_2::BnHwServiceManager;
354 using android::hidl::manager::V1_2::BpHwServiceManager;
355
Steven Moreland10d55632020-04-21 14:10:53 -0700356 static std::mutex& gDefaultServiceManagerLock = *new std::mutex;
357 static sp<IServiceManager1_2>& gDefaultServiceManager = *new sp<IServiceManager1_2>;
Steven Moreland89909692018-05-30 14:11:19 -0700358
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700359 {
Steven Moreland89909692018-05-30 14:11:19 -0700360 std::lock_guard<std::mutex> _l(gDefaultServiceManagerLock);
361 if (gDefaultServiceManager != nullptr) {
362 return gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -0700363 }
Steven Moreland405d7612017-04-07 20:31:22 -0700364
Yifan Hong8fb656b2017-03-16 14:30:40 -0700365 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
366 // HwBinder not available on this device or not accessible to
367 // this process.
368 return nullptr;
369 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000370
Devin Moore50ced9f2024-03-26 22:00:23 +0000371 if (!isHidlSupported()) {
372 // hwservicemanager is not available on this device.
373 LOG(WARNING) << "hwservicemanager is not supported on the device.";
374 gDefaultServiceManager = sp<NoHwServiceManager>::make();
375 return gDefaultServiceManager;
376 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000377
Steven Moreland89909692018-05-30 14:11:19 -0700378 while (gDefaultServiceManager == nullptr) {
379 gDefaultServiceManager =
380 fromBinder<IServiceManager1_2, BpHwServiceManager, BnHwServiceManager>(
381 ProcessState::self()->getContextObject(nullptr));
382 if (gDefaultServiceManager == nullptr) {
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000383 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700384 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -0700385 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700386 }
387 }
388
Steven Moreland89909692018-05-30 14:11:19 -0700389 return gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700390}
391
Elliott Hughes19306142018-10-26 13:13:04 -0700392static std::vector<std::string> findFiles(const std::string& path, const std::string& prefix,
393 const std::string& suffix) {
Steven Moreland0091c092017-01-20 23:15:18 +0000394 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
395 if (!dir) return {};
396
397 std::vector<std::string> results{};
398
399 dirent* dp;
400 while ((dp = readdir(dir.get())) != nullptr) {
401 std::string name = dp->d_name;
402
Steven Moreland8092aa02018-10-12 15:54:56 -0700403 if (base::StartsWith(name, prefix) && base::EndsWith(name, suffix)) {
Steven Moreland0091c092017-01-20 23:15:18 +0000404 results.push_back(name);
405 }
406 }
407
408 return results;
409}
410
Steven Morelandccc3a972020-02-26 16:38:31 -0800411static bool matchPackageName(const std::string& lib, std::string* matchedName,
412 std::string* implName) {
413#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
414#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
415 static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
416#undef RE_PATH
417#undef RE_COMPONENT
418
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800419 std::smatch match;
420 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
421 *matchedName = match.str(1) + "::I*";
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700422 *implName = match.str(2);
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800423 return true;
424 }
425 return false;
426}
427
Yifan Hong7f49f592017-02-03 15:11:44 -0800428static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700429 if (kIsRecovery) {
430 // No hwservicemanager in recovery.
431 return;
432 }
433
Steven Moreland2a2678e2017-07-21 18:07:38 -0700434 sp<IServiceManager1_0> binderizedManager = defaultServiceManager();
Yifan Hong7f49f592017-02-03 15:11:44 -0800435 if (binderizedManager == nullptr) {
436 LOG(WARNING) << "Could not registerReference for "
437 << interfaceName << "/" << instanceName
438 << ": null binderized manager.";
439 return;
440 }
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700441 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName);
Yifan Hong7f49f592017-02-03 15:11:44 -0800442 if (!ret.isOk()) {
443 LOG(WARNING) << "Could not registerReference for "
444 << interfaceName << "/" << instanceName
445 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700446 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800447 }
Steven Morelande681aa52017-02-15 16:22:37 -0800448 LOG(VERBOSE) << "Successfully registerReference for "
449 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800450}
451
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700452using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo;
453static inline void fetchPidsForPassthroughLibraries(
454 std::map<std::string, InstanceDebugInfo>* infos) {
455 static const std::string proc = "/proc/";
456
457 std::map<std::string, std::set<pid_t>> pids;
458 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir);
459 if (!dir) return;
460 dirent* dp;
461 while ((dp = readdir(dir.get())) != nullptr) {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700462 pid_t pid = strtoll(dp->d_name, nullptr, 0);
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700463 if (pid == 0) continue;
464 std::string mapsPath = proc + dp->d_name + "/maps";
465 std::ifstream ifs{mapsPath};
466 if (!ifs.is_open()) continue;
467
468 for (std::string line; std::getline(ifs, line);) {
469 // The last token of line should look like
470 // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so
471 // Use some simple filters to ignore bad lines before extracting libFileName
472 // and checking the key in info to make parsing faster.
473 if (line.back() != 'o') continue;
474 if (line.rfind('@') == std::string::npos) continue;
475
476 auto spacePos = line.rfind(' ');
477 if (spacePos == std::string::npos) continue;
478 auto libFileName = line.substr(spacePos + 1);
479 auto it = infos->find(libFileName);
480 if (it == infos->end()) continue;
481 pids[libFileName].insert(pid);
482 }
483 }
484 for (auto& pair : *infos) {
485 pair.second.clientPids =
486 std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()};
487 }
488}
489
Steven Moreland2a2678e2017-07-21 18:07:38 -0700490struct PassthroughServiceManager : IServiceManager1_1 {
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700491 static void openLibs(
492 const std::string& fqName,
493 const std::function<bool /* continue */ (void* /* handle */, const std::string& /* lib */,
494 const std::string& /* sym */)>& eachLib) {
Steven Moreland819c05d2017-04-06 17:24:22 -0700495 //fqName looks like android.hardware.foo@1.0::IFoo
Steven Moreland519306f2017-06-06 17:14:12 -0700496 size_t idx = fqName.find("::");
Steven Moreland819c05d2017-04-06 17:24:22 -0700497
498 if (idx == std::string::npos ||
Steven Moreland519306f2017-06-06 17:14:12 -0700499 idx + strlen("::") + 1 >= fqName.size()) {
Steven Moreland337e6b62017-01-18 17:25:13 -0800500 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
Steven Moreland519306f2017-06-06 17:14:12 -0700501 return;
Steven Moreland337e6b62017-01-18 17:25:13 -0800502 }
503
Steven Moreland519306f2017-06-06 17:14:12 -0700504 std::string packageAndVersion = fqName.substr(0, idx);
505 std::string ifaceName = fqName.substr(idx + strlen("::"));
Steven Moreland819c05d2017-04-06 17:24:22 -0700506
507 const std::string prefix = packageAndVersion + "-impl";
508 const std::string sym = "HIDL_FETCH_" + ifaceName;
Steven Moreland348802d2017-02-23 12:48:55 -0800509
Steven Moreland77f4c852017-10-12 11:05:53 -0700510 constexpr int dlMode = RTLD_LAZY;
511 void* handle = nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800512
Steven Morelanda29905c2017-03-01 10:42:35 -0800513 dlerror(); // clear
514
Jooyung Han219106c2020-07-17 15:01:19 +0900515 static std::string halLibPathVndkSp = details::getVndkSpHwPath();
Justin Yun6a323ed2018-10-18 18:41:56 +0900516 std::vector<std::string> paths = {
517 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, halLibPathVndkSp,
518#ifndef __ANDROID_VNDK__
519 HAL_LIBRARY_PATH_SYSTEM,
520#endif
521 };
Steven Moreland77f4c852017-10-12 11:05:53 -0700522
Steven Morelandb939e7e2020-07-08 01:04:23 +0000523 if (details::isTrebleTestingOverride()) {
Steven Moreland77f4c852017-10-12 11:05:53 -0700524 // Load HAL implementations that are statically linked
525 handle = dlopen(nullptr, dlMode);
526 if (handle == nullptr) {
527 const char* error = dlerror();
528 LOG(ERROR) << "Failed to dlopen self: "
529 << (error == nullptr ? "unknown error" : error);
530 } else if (!eachLib(handle, "SELF", sym)) {
531 return;
532 }
Steven Morelandf7dea692017-07-14 12:49:28 -0700533 }
Steven Moreland77f4c852017-10-12 11:05:53 -0700534
Steven Morelandf7dea692017-07-14 12:49:28 -0700535 for (const std::string& path : paths) {
Elliott Hughes19306142018-10-26 13:13:04 -0700536 std::vector<std::string> libs = findFiles(path, prefix, ".so");
Steven Moreland0091c092017-01-20 23:15:18 +0000537
Steven Moreland0091c092017-01-20 23:15:18 +0000538 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800539 const std::string fullPath = path + lib;
540
Yifan Hongdeacf142018-07-10 17:33:34 -0700541 if (kIsRecovery || path == HAL_LIBRARY_PATH_SYSTEM) {
Jiyong Park3ab546c2017-04-06 20:28:07 +0900542 handle = dlopen(fullPath.c_str(), dlMode);
Steven Moreland65c00cb2017-10-12 11:35:22 -0700543 } else {
Steven Moreland2056cf32019-09-17 17:41:02 -0700544#if !defined(__ANDROID_RECOVERY__) && defined(__ANDROID__)
Steven Moreland65c00cb2017-10-12 11:35:22 -0700545 handle = android_load_sphal_library(fullPath.c_str(), dlMode);
Jerry Zhang86ae9992018-05-30 17:11:09 -0700546#endif
Jiyong Park3ab546c2017-04-06 20:28:07 +0900547 }
548
Steven Moreland348802d2017-02-23 12:48:55 -0800549 if (handle == nullptr) {
550 const char* error = dlerror();
551 LOG(ERROR) << "Failed to dlopen " << lib << ": "
552 << (error == nullptr ? "unknown error" : error);
553 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000554 }
Steven Moreland348802d2017-02-23 12:48:55 -0800555
Steven Moreland519306f2017-06-06 17:14:12 -0700556 if (!eachLib(handle, lib, sym)) {
557 return;
Steven Moreland348802d2017-02-23 12:48:55 -0800558 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800559 }
560 }
Steven Moreland519306f2017-06-06 17:14:12 -0700561 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800562
Steven Moreland519306f2017-06-06 17:14:12 -0700563 Return<sp<IBase>> get(const hidl_string& fqName,
564 const hidl_string& name) override {
565 sp<IBase> ret = nullptr;
Devin Mooree1973ce2022-04-15 20:54:03 +0000566 // This is required to run without hwservicemanager while we have
567 // passthrough HIDL services. Once the passthrough HIDL services have
568 // been removed, the PassthroughServiceManager will no longer be needed.
Devin Moore22e6f1e2023-11-22 22:29:54 +0000569 if (!isHidlSupported() && isServiceManager(fqName)) {
Devin Mooree1973ce2022-04-15 20:54:03 +0000570 return defaultServiceManager1_2();
571 }
Steven Moreland519306f2017-06-06 17:14:12 -0700572
573 openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) {
574 IBase* (*generator)(const char* name);
575 *(void **)(&generator) = dlsym(handle, sym.c_str());
576 if(!generator) {
577 const char* error = dlerror();
Steven Morelandedd49122021-02-17 22:41:51 +0000578 LOG(ERROR) << "Passthrough lookup opened " << lib << " but could not find symbol "
579 << sym << ": " << (error == nullptr ? "unknown error" : error)
580 << ". Keeping library open.";
581
582 // dlclose too problematic in multi-threaded environment
583 // dlclose(handle);
584
585 return true; // continue
Steven Moreland519306f2017-06-06 17:14:12 -0700586 }
587
588 ret = (*generator)(name.c_str());
589
590 if (ret == nullptr) {
Steven Morelandedd49122021-02-17 22:41:51 +0000591 LOG(ERROR) << "Could not find instance '" << name.c_str() << "' in library " << lib
592 << ". Keeping library open.";
593
594 // dlclose too problematic in multi-threaded environment
595 // dlclose(handle);
596
597 // this module doesn't provide this particular instance
598 return true; // continue
Steven Moreland519306f2017-06-06 17:14:12 -0700599 }
600
Steven Morelande69e8d32018-03-14 10:55:42 -0700601 // Actual fqname might be a subclass.
602 // This assumption is tested in vts_treble_vintf_test
603 using ::android::hardware::details::getDescriptor;
604 std::string actualFqName = getDescriptor(ret.get());
605 CHECK(actualFqName.size() > 0);
606 registerReference(actualFqName, name);
Steven Moreland519306f2017-06-06 17:14:12 -0700607 return false;
608 });
609
610 return ret;
Steven Moreland337e6b62017-01-18 17:25:13 -0800611 }
612
Martijn Coenen67a02492017-03-06 13:04:48 +0100613 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800614 const sp<IBase>& /* service */) override {
615 LOG(FATAL) << "Cannot register services with passthrough service manager.";
616 return false;
617 }
618
Steven Morelandc601c5f2017-04-06 09:26:07 -0700619 Return<Transport> getTransport(const hidl_string& /* fqName */,
620 const hidl_string& /* name */) {
621 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
622 return Transport::EMPTY;
623 }
624
Yifan Hong705e5da2017-03-02 16:59:39 -0800625 Return<void> list(list_cb /* _hidl_cb */) override {
626 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800627 return Void();
628 }
629 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
630 listByInterface_cb /* _hidl_cb */) override {
631 // TODO: add this functionality
632 LOG(FATAL) << "Cannot list services with passthrough service manager.";
633 return Void();
634 }
635
636 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
637 const hidl_string& /* name */,
638 const sp<IServiceNotification>& /* callback */) override {
639 // This makes no sense.
640 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
641 return false;
642 }
643
Yifan Hong705e5da2017-03-02 16:59:39 -0800644 Return<void> debugDump(debugDump_cb _hidl_cb) override {
645 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700646 using std::literals::string_literals::operator""s;
Jooyung Han219106c2020-07-17 15:01:19 +0900647 static std::string halLibPathVndkSp64 = details::getVndkSpHwPath("lib64");
648 static std::string halLibPathVndkSp32 = details::getVndkSpHwPath("lib");
Jiyong Park56eb1492017-08-04 16:16:13 +0900649 static std::vector<std::pair<Arch, std::vector<const char*>>> sAllPaths{
650 {Arch::IS_64BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900651 {
652 HAL_LIBRARY_PATH_ODM_64BIT, HAL_LIBRARY_PATH_VENDOR_64BIT,
653 halLibPathVndkSp64.c_str(),
654#ifndef __ANDROID_VNDK__
655 HAL_LIBRARY_PATH_SYSTEM_64BIT,
656#endif
657 }},
Jiyong Park56eb1492017-08-04 16:16:13 +0900658 {Arch::IS_32BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900659 {
660 HAL_LIBRARY_PATH_ODM_32BIT, HAL_LIBRARY_PATH_VENDOR_32BIT,
661 halLibPathVndkSp32.c_str(),
662#ifndef __ANDROID_VNDK__
663 HAL_LIBRARY_PATH_SYSTEM_32BIT,
664#endif
665 }}};
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700666 std::map<std::string, InstanceDebugInfo> map;
Yifan Hong705e5da2017-03-02 16:59:39 -0800667 for (const auto &pair : sAllPaths) {
668 Arch arch = pair.first;
669 for (const auto &path : pair.second) {
Elliott Hughes19306142018-10-26 13:13:04 -0700670 std::vector<std::string> libs = findFiles(path, "", ".so");
Yifan Hong705e5da2017-03-02 16:59:39 -0800671 for (const std::string &lib : libs) {
672 std::string matchedName;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700673 std::string implName;
674 if (matchPackageName(lib, &matchedName, &implName)) {
675 std::string instanceName{"* ("s + path + ")"s};
676 if (!implName.empty()) instanceName += " ("s + implName + ")"s;
677 map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName,
678 .instanceName = instanceName,
679 .clientPids = {},
680 .arch = arch});
Yifan Hong705e5da2017-03-02 16:59:39 -0800681 }
682 }
683 }
684 }
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700685 fetchPidsForPassthroughLibraries(&map);
686 hidl_vec<InstanceDebugInfo> vec;
687 vec.resize(map.size());
688 size_t idx = 0;
689 for (auto&& pair : map) {
690 vec[idx++] = std::move(pair.second);
691 }
Yifan Hong705e5da2017-03-02 16:59:39 -0800692 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800693 return Void();
694 }
695
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700696 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override {
Yifan Hong7f49f592017-02-03 15:11:44 -0800697 // This makes no sense.
698 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
699 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800700 return Void();
701 }
702
Steven Moreland2a2678e2017-07-21 18:07:38 -0700703 Return<bool> unregisterForNotifications(const hidl_string& /* fqName */,
704 const hidl_string& /* name */,
705 const sp<IServiceNotification>& /* callback */) override {
706 // This makes no sense.
707 LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager.";
708 return false;
709 }
710
Steven Moreland337e6b62017-01-18 17:25:13 -0800711};
712
Steven Moreland2a2678e2017-07-21 18:07:38 -0700713sp<IServiceManager1_0> getPassthroughServiceManager() {
714 return getPassthroughServiceManager1_1();
715}
716sp<IServiceManager1_1> getPassthroughServiceManager1_1() {
Steven Moreland337e6b62017-01-18 17:25:13 -0800717 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
718 return manager;
719}
720
Steven Moreland92b247c2019-08-21 14:53:11 -0700721std::vector<std::string> getAllHalInstanceNames(const std::string& descriptor) {
722 std::vector<std::string> ret;
723 auto sm = defaultServiceManager1_2();
724 sm->listManifestByInterface(descriptor, [&](const auto& instances) {
725 ret.reserve(instances.size());
726 for (const auto& i : instances) {
727 ret.push_back(i);
728 }
729 });
730 return ret;
731}
732
Steven Morelandcbefd352017-01-23 20:29:05 -0800733namespace details {
734
Steven Moreland519306f2017-06-06 17:14:12 -0700735void preloadPassthroughService(const std::string &descriptor) {
736 PassthroughServiceManager::openLibs(descriptor,
737 [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) {
738 // do nothing
739 return true; // open all libs
740 });
741}
742
Steven Morelandcbefd352017-01-23 20:29:05 -0800743struct Waiter : IServiceNotification {
Martijn Coenen773609e2017-10-24 09:57:53 +0200744 Waiter(const std::string& interface, const std::string& instanceName,
745 const sp<IServiceManager1_1>& sm) : mInterfaceName(interface),
746 mInstanceName(instanceName), mSm(sm) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100747 }
748
749 void onFirstRef() override {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200750 // If this process only has one binder thread, and we're calling wait() from
751 // that thread, it will block forever because we hung up the one and only
752 // binder thread on a condition variable that can only be notified by an
753 // incoming binder call.
Tobias Lindskog88e886f2018-01-05 10:32:43 +0100754 if (IPCThreadState::self()->isOnlyBinderThread()) {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200755 LOG(WARNING) << "Can't efficiently wait for " << mInterfaceName << "/"
756 << mInstanceName << ", because we are called from "
757 << "the only binder thread in this process.";
758 return;
759 }
760
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100761 Return<bool> ret = mSm->registerForNotifications(mInterfaceName, mInstanceName, this);
Martijn Coenen773609e2017-10-24 09:57:53 +0200762
763 if (!ret.isOk()) {
764 LOG(ERROR) << "Transport error, " << ret.description()
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100765 << ", during notification registration for " << mInterfaceName << "/"
766 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200767 return;
768 }
769
770 if (!ret) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100771 LOG(ERROR) << "Could not register for notifications for " << mInterfaceName << "/"
772 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200773 return;
774 }
775
776 mRegisteredForNotifications = true;
777 }
778
779 ~Waiter() {
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100780 if (!mDoneCalled) {
781 LOG(FATAL)
782 << "Waiter still registered for notifications, call done() before dropping ref!";
Martijn Coenen773609e2017-10-24 09:57:53 +0200783 }
784 }
785
Steven Morelandcbefd352017-01-23 20:29:05 -0800786 Return<void> onRegistration(const hidl_string& /* fqName */,
787 const hidl_string& /* name */,
788 bool /* preexisting */) override {
789 std::unique_lock<std::mutex> lock(mMutex);
790 if (mRegistered) {
791 return Void();
792 }
793 mRegistered = true;
794 lock.unlock();
795
796 mCondition.notify_one();
797 return Void();
798 }
799
Steven Moreland0771d8a2018-05-09 13:29:14 -0700800 void wait(bool timeout) {
Steven Moreland49605102017-03-28 09:33:06 -0700801 using std::literals::chrono_literals::operator""s;
802
Martijn Coenen773609e2017-10-24 09:57:53 +0200803 if (!mRegisteredForNotifications) {
804 // As an alternative, just sleep for a second and return
805 LOG(WARNING) << "Waiting one second for " << mInterfaceName << "/" << mInstanceName;
806 sleep(1);
807 return;
808 }
809
Steven Morelandcbefd352017-01-23 20:29:05 -0800810 std::unique_lock<std::mutex> lock(mMutex);
Steven Moreland0771d8a2018-05-09 13:29:14 -0700811 do {
Steven Moreland49605102017-03-28 09:33:06 -0700812 mCondition.wait_for(lock, 1s, [this]{
813 return mRegistered;
814 });
815
816 if (mRegistered) {
817 break;
818 }
819
Steven Moreland0771d8a2018-05-09 13:29:14 -0700820 LOG(WARNING) << "Waited one second for " << mInterfaceName << "/" << mInstanceName;
821 } while (!timeout);
Steven Morelandcbefd352017-01-23 20:29:05 -0800822 }
823
Martijn Coenen773609e2017-10-24 09:57:53 +0200824 // Be careful when using this; after calling reset(), you must always try to retrieve
825 // the corresponding service before blocking on the waiter; otherwise, you might run
826 // into a race-condition where the service has just (re-)registered, you clear the state
827 // here, and subsequently calling waiter->wait() will block forever.
828 void reset() {
829 std::unique_lock<std::mutex> lock(mMutex);
830 mRegistered = false;
831 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100832
833 // done() must be called before dropping the last strong ref to the Waiter, to make
834 // sure we can properly unregister with hwservicemanager.
835 void done() {
836 if (mRegisteredForNotifications) {
837 if (!mSm->unregisterForNotifications(mInterfaceName, mInstanceName, this)
838 .withDefault(false)) {
839 LOG(ERROR) << "Could not unregister service notification for " << mInterfaceName
840 << "/" << mInstanceName << ".";
841 } else {
842 mRegisteredForNotifications = false;
843 }
844 }
845 mDoneCalled = true;
846 }
847
848 private:
Martijn Coenen773609e2017-10-24 09:57:53 +0200849 const std::string mInterfaceName;
850 const std::string mInstanceName;
Steven Morelandad1f9922018-10-02 19:37:05 -0700851 sp<IServiceManager1_1> mSm;
Steven Morelandcbefd352017-01-23 20:29:05 -0800852 std::mutex mMutex;
853 std::condition_variable mCondition;
854 bool mRegistered = false;
Martijn Coenen773609e2017-10-24 09:57:53 +0200855 bool mRegisteredForNotifications = false;
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100856 bool mDoneCalled = false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800857};
858
859void waitForHwService(
860 const std::string &interface, const std::string &instanceName) {
Martijn Coenen773609e2017-10-24 09:57:53 +0200861 sp<Waiter> waiter = new Waiter(interface, instanceName, defaultServiceManager1_1());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700862 waiter->wait(false /* timeout */);
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100863 waiter->done();
Martijn Coenen773609e2017-10-24 09:57:53 +0200864}
Steven Morelandcbefd352017-01-23 20:29:05 -0800865
Martijn Coenen773609e2017-10-24 09:57:53 +0200866// Prints relevant error/warning messages for error return values from
867// details::canCastInterface(), both transaction errors (!castReturn.isOk())
868// as well as actual cast failures (castReturn.isOk() && castReturn = false).
869// Returns 'true' if the error is non-fatal and it's useful to retry
870bool handleCastError(const Return<bool>& castReturn, const std::string& descriptor,
871 const std::string& instance) {
872 if (castReturn.isOk()) {
873 if (castReturn) {
874 details::logAlwaysFatal("Successful cast value passed into handleCastError.");
875 }
876 // This should never happen, and there's not really a point in retrying.
877 ALOGE("getService: received incompatible service (bug in hwservicemanager?) for "
878 "%s/%s.", descriptor.c_str(), instance.c_str());
879 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800880 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200881 if (castReturn.isDeadObject()) {
882 ALOGW("getService: found dead hwbinder service for %s/%s.", descriptor.c_str(),
883 instance.c_str());
884 return true;
Steven Morelandcbefd352017-01-23 20:29:05 -0800885 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200886 // This can happen due to:
887 // 1) No SELinux permissions
888 // 2) Other transaction failure (no buffer space, kernel error)
889 // The first isn't recoverable, but the second is.
890 // Since we can't yet differentiate between the two, and clients depend
891 // on us not blocking in case 1), treat this as a fatal error for now.
892 ALOGW("getService: unable to call into hwbinder service for %s/%s.",
893 descriptor.c_str(), instance.c_str());
894 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800895}
896
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200897sp<::android::hidl::base::V1_0::IBase> getRawServiceInternal(const std::string& descriptor,
898 const std::string& instance,
899 bool retry, bool getStub) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000900 using Transport = IServiceManager1_0::Transport;
Martijn Coenend35678f2018-03-07 09:51:01 +0100901 sp<Waiter> waiter;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200902
Yifan Hongdeacf142018-07-10 17:33:34 -0700903 sp<IServiceManager1_1> sm;
904 Transport transport = Transport::EMPTY;
905 if (kIsRecovery) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700906 transport = Transport::PASSTHROUGH;
Yifan Hongdeacf142018-07-10 17:33:34 -0700907 } else {
908 sm = defaultServiceManager1_1();
909 if (sm == nullptr) {
910 ALOGE("getService: defaultServiceManager() is null");
911 return nullptr;
912 }
913
914 Return<Transport> transportRet = sm->getTransport(descriptor, instance);
915
916 if (!transportRet.isOk()) {
917 ALOGE("getService: defaultServiceManager()->getTransport returns %s",
918 transportRet.description().c_str());
919 return nullptr;
920 }
921 transport = transportRet;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200922 }
923
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200924 const bool vintfHwbinder = (transport == Transport::HWBINDER);
925 const bool vintfPassthru = (transport == Transport::PASSTHROUGH);
Steven Moreland84fe49e2019-08-20 17:47:26 +0000926 const bool trebleTestingOverride = isTrebleTestingOverride();
Steven Morelandf47cdf72021-04-07 22:13:13 +0000927 const bool allowLegacy = !kEnforceVintfManifest || (trebleTestingOverride && isDebuggable());
Steven Moreland84fe49e2019-08-20 17:47:26 +0000928 const bool vintfLegacy = (transport == Transport::EMPTY) && allowLegacy;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200929
Steven Moreland84fe49e2019-08-20 17:47:26 +0000930 if (!kEnforceVintfManifest) {
931 ALOGE("getService: Potential race detected. The VINTF manifest is not being enforced. If "
932 "a HAL server has a delay in starting and it is not in the manifest, it will not be "
933 "retrieved. Please make sure all HALs on this device are in the VINTF manifest and "
934 "enable PRODUCT_ENFORCE_VINTF_MANIFEST on this device (this is also enabled by "
935 "PRODUCT_FULL_TREBLE). PRODUCT_ENFORCE_VINTF_MANIFEST will ensure that no race "
936 "condition is possible here.");
Steven Morelandaadec3c2019-08-22 17:06:48 -0700937 sleep(1);
Steven Moreland84fe49e2019-08-20 17:47:26 +0000938 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200939
Steven Moreland76371242018-04-19 17:11:39 -0700940 for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) {
941 if (waiter == nullptr && tries > 0) {
Martijn Coenend35678f2018-03-07 09:51:01 +0100942 waiter = new Waiter(descriptor, instance, sm);
943 }
Steven Moreland76371242018-04-19 17:11:39 -0700944 if (waiter != nullptr) {
945 waiter->reset(); // don't reorder this -- see comments on reset()
946 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200947 Return<sp<IBase>> ret = sm->get(descriptor, instance);
948 if (!ret.isOk()) {
949 ALOGE("getService: defaultServiceManager()->get returns %s for %s/%s.",
950 ret.description().c_str(), descriptor.c_str(), instance.c_str());
951 break;
952 }
953 sp<IBase> base = ret;
Martijn Coenen773609e2017-10-24 09:57:53 +0200954 if (base != nullptr) {
955 Return<bool> canCastRet =
956 details::canCastInterface(base.get(), descriptor.c_str(), true /* emitError */);
957
958 if (canCastRet.isOk() && canCastRet) {
Steven Moreland76371242018-04-19 17:11:39 -0700959 if (waiter != nullptr) {
960 waiter->done();
961 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200962 return base; // still needs to be wrapped by Bp class.
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200963 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200964
965 if (!handleCastError(canCastRet, descriptor, instance)) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200966 }
967
Martijn Coenen773609e2017-10-24 09:57:53 +0200968 // In case of legacy or we were not asked to retry, don't.
969 if (vintfLegacy || !retry) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200970
Steven Moreland76371242018-04-19 17:11:39 -0700971 if (waiter != nullptr) {
972 ALOGI("getService: Trying again for %s/%s...", descriptor.c_str(), instance.c_str());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700973 waiter->wait(true /* timeout */);
Steven Moreland76371242018-04-19 17:11:39 -0700974 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200975 }
976
Martijn Coenend35678f2018-03-07 09:51:01 +0100977 if (waiter != nullptr) {
978 waiter->done();
979 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100980
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200981 if (getStub || vintfPassthru || vintfLegacy) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000982 const sp<IServiceManager1_0> pm = getPassthroughServiceManager();
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200983 if (pm != nullptr) {
984 sp<IBase> base = pm->get(descriptor, instance).withDefault(nullptr);
985 if (!getStub || trebleTestingOverride) {
986 base = wrapPassthrough(base);
987 }
988 return base;
989 }
990 }
991
992 return nullptr;
993}
994
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700995status_t registerAsServiceInternal(const sp<IBase>& service, const std::string& name) {
996 if (service == nullptr) {
997 return UNEXPECTED_NULL;
998 }
999
1000 sp<IServiceManager1_2> sm = defaultServiceManager1_2();
1001 if (sm == nullptr) {
1002 return INVALID_OPERATION;
1003 }
1004
Steven Moreland84fe49e2019-08-20 17:47:26 +00001005 const std::string descriptor = getDescriptor(service.get());
1006
1007 if (kEnforceVintfManifest && !isTrebleTestingOverride()) {
1008 using Transport = IServiceManager1_0::Transport;
Steven Moreland7d1b0732021-01-21 22:47:40 +00001009 Return<Transport> transport = sm->getTransport(descriptor, name);
1010
1011 if (!transport.isOk()) {
1012 LOG(ERROR) << "Could not get transport for " << descriptor << "/" << name << ": "
1013 << transport.description();
1014 return UNKNOWN_ERROR;
1015 }
Steven Moreland84fe49e2019-08-20 17:47:26 +00001016
1017 if (transport != Transport::HWBINDER) {
1018 LOG(ERROR) << "Service " << descriptor << "/" << name
1019 << " must be in VINTF manifest in order to register/get.";
1020 return UNKNOWN_ERROR;
1021 }
1022 }
1023
Steven Morelandbb9eb2a2018-10-11 12:10:01 -07001024 bool registered = false;
1025 Return<void> ret = service->interfaceChain([&](const auto& chain) {
1026 registered = sm->addWithChain(name.c_str(), service, chain).withDefault(false);
1027 });
1028
1029 if (!ret.isOk()) {
1030 LOG(ERROR) << "Could not retrieve interface chain: " << ret.description();
1031 }
1032
1033 if (registered) {
Steven Moreland84fe49e2019-08-20 17:47:26 +00001034 onRegistrationImpl(descriptor, name);
Steven Morelandbb9eb2a2018-10-11 12:10:01 -07001035 }
1036
1037 return registered ? OK : UNKNOWN_ERROR;
1038}
1039
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +02001040} // namespace details
Steven Morelandcbefd352017-01-23 20:29:05 -08001041
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +02001042} // namespace hardware
1043} // namespace android