blob: 357bb30885c9ac058c2e01ef6618cd3298b77ef7 [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
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 Morelande69e8d32018-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>
Peter Kalauskas64ffced2018-09-05 16:41:08 -070036#include <utils/SystemClock.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070037
Peter Kalauskas64ffced2018-09-05 16:41:08 -070038#include <android-base/file.h>
Steven Moreland337e6b62017-01-18 17:25:13 -080039#include <android-base/logging.h>
Peter Kalauskas64ffced2018-09-05 16:41:08 -070040#include <android-base/parseint.h>
Steven Morelandc1cee2c2017-03-24 16:23:11 +000041#include <android-base/properties.h>
Justin Yun1f048102017-12-01 15:30:08 +090042#include <android-base/stringprintf.h>
Peter Kalauskas64ffced2018-09-05 16:41:08 -070043#include <android-base/strings.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070044#include <hwbinder/IPCThreadState.h>
45#include <hwbinder/Parcel.h>
Jerry Zhang86ae9992018-05-30 17:11:09 -070046#if !defined(__ANDROID_RECOVERY__)
Jiyong Parkba8ace12017-05-15 15:44:39 +090047#include <vndksupport/linker.h>
Jerry Zhang86ae9992018-05-30 17:11:09 -070048#endif
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070049
Steven Moreland89909692018-05-30 14:11:19 -070050#include <android/hidl/manager/1.2/BnHwServiceManager.h>
51#include <android/hidl/manager/1.2/BpHwServiceManager.h>
52#include <android/hidl/manager/1.2/IServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070053
Yifan Hong9a22d1d2017-01-25 14:19:26 -080054#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
55#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
56static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
57
Steven Morelandc1cee2c2017-03-24 16:23:11 +000058using android::base::WaitForProperty;
59
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070060using ::android::hidl::base::V1_0::IBase;
Steven Moreland2a2678e2017-07-21 18:07:38 -070061using IServiceManager1_0 = android::hidl::manager::V1_0::IServiceManager;
62using IServiceManager1_1 = android::hidl::manager::V1_1::IServiceManager;
Steven Moreland89909692018-05-30 14:11:19 -070063using IServiceManager1_2 = android::hidl::manager::V1_2::IServiceManager;
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070064using ::android::hidl::manager::V1_0::IServiceNotification;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070065
66namespace android {
67namespace hardware {
68
Steven Morelandc1cee2c2017-03-24 16:23:11 +000069static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready";
70
Yifan Hongdeacf142018-07-10 17:33:34 -070071#if defined(__ANDROID_RECOVERY__)
72static constexpr bool kIsRecovery = true;
73#else
74static constexpr bool kIsRecovery = false;
75#endif
76
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070077static void waitForHwServiceManager() {
Steven Morelandc1cee2c2017-03-24 16:23:11 +000078 using std::literals::chrono_literals::operator""s;
79
80 while (!WaitForProperty(kHwServicemanagerReadyProperty, "true", 1s)) {
81 LOG(WARNING) << "Waited for hwservicemanager.ready for a second, waiting another...";
82 }
83}
84
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070085static std::string binaryName() {
Steven Moreland405d7612017-04-07 20:31:22 -070086 std::ifstream ifs("/proc/self/cmdline");
87 std::string cmdline;
88 if (!ifs.is_open()) {
89 return "";
90 }
91 ifs >> cmdline;
92
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -070093 size_t idx = cmdline.rfind('/');
Steven Moreland405d7612017-04-07 20:31:22 -070094 if (idx != std::string::npos) {
95 cmdline = cmdline.substr(idx + 1);
96 }
97
98 return cmdline;
99}
100
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700101static std::string packageWithoutVersion(const std::string& packageAndVersion) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700102 size_t at = packageAndVersion.find('@');
103 if (at == std::string::npos) return packageAndVersion;
104 return packageAndVersion.substr(0, at);
105}
106
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700107static void tryShortenProcessName(const std::string& descriptor) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700108 const static std::string kTasks = "/proc/self/task/";
109
110 // make sure that this binary name is in the same package
Steven Moreland405d7612017-04-07 20:31:22 -0700111 std::string processName = binaryName();
112
Steven Moreland7d09a402018-04-06 10:44:05 -0700113 // e.x. android.hardware.foo is this package
Steven Moreland8092aa02018-10-12 15:54:56 -0700114 if (!base::StartsWith(packageWithoutVersion(processName), packageWithoutVersion(descriptor))) {
Steven Moreland405d7612017-04-07 20:31:22 -0700115 return;
116 }
117
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700118 // e.x. android.hardware.module.foo@1.2::IFoo -> foo@1.2
119 size_t lastDot = descriptor.rfind('.');
Steven Moreland7d09a402018-04-06 10:44:05 -0700120 if (lastDot == std::string::npos) return;
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700121 size_t secondDot = descriptor.rfind('.', lastDot - 1);
Steven Moreland7d09a402018-04-06 10:44:05 -0700122 if (secondDot == std::string::npos) return;
Steven Moreland405d7612017-04-07 20:31:22 -0700123
Steven Moreland7d09a402018-04-06 10:44:05 -0700124 std::string newName = processName.substr(secondDot + 1, std::string::npos);
125 ALOGI("Removing namespace from process name %s to %s.", processName.c_str(), newName.c_str());
126
127 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(kTasks.c_str()), closedir);
128 if (dir == nullptr) return;
129
130 dirent* dp;
131 while ((dp = readdir(dir.get())) != nullptr) {
132 if (dp->d_type != DT_DIR) continue;
133 if (dp->d_name[0] == '.') continue;
134
135 std::fstream fs(kTasks + dp->d_name + "/comm");
136 if (!fs.is_open()) {
137 ALOGI("Could not rename process, failed read comm for %s.", dp->d_name);
138 continue;
139 }
140
141 std::string oldComm;
142 fs >> oldComm;
143
144 // don't rename if it already has an explicit name
Steven Moreland8092aa02018-10-12 15:54:56 -0700145 if (base::StartsWith(descriptor, oldComm)) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700146 fs.seekg(0, fs.beg);
147 fs << newName;
148 }
Steven Moreland405d7612017-04-07 20:31:22 -0700149 }
Steven Moreland405d7612017-04-07 20:31:22 -0700150}
151
152namespace details {
153
Peter Kalauskas64ffced2018-09-05 16:41:08 -0700154/*
155 * Returns the age of the current process by reading /proc/self/stat and comparing starttime to the
156 * current time. This is useful for measuring how long it took a HAL to register itself.
157 */
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700158static long getProcessAgeMs() {
Peter Kalauskas64ffced2018-09-05 16:41:08 -0700159 constexpr const int PROCFS_STAT_STARTTIME_INDEX = 21;
160 std::string content;
161 android::base::ReadFileToString("/proc/self/stat", &content, false);
162 auto stats = android::base::Split(content, " ");
163 if (stats.size() <= PROCFS_STAT_STARTTIME_INDEX) {
164 LOG(INFO) << "Could not read starttime from /proc/self/stat";
165 return -1;
166 }
167 const std::string& startTimeString = stats[PROCFS_STAT_STARTTIME_INDEX];
168 static const int64_t ticksPerSecond = sysconf(_SC_CLK_TCK);
169 const int64_t uptime = android::uptimeMillis();
170
171 unsigned long long startTimeInClockTicks = 0;
172 if (android::base::ParseUint(startTimeString, &startTimeInClockTicks)) {
173 long startTimeMs = 1000ULL * startTimeInClockTicks / ticksPerSecond;
174 return uptime - startTimeMs;
175 }
176 return -1;
177}
178
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700179static void onRegistrationImpl(const std::string& descriptor, const std::string& instanceName) {
Peter Kalauskas64ffced2018-09-05 16:41:08 -0700180 long halStartDelay = getProcessAgeMs();
181 if (halStartDelay >= 0) {
182 // The "start delay" printed here is an estimate of how long it took the HAL to go from
183 // process creation to registering itself as a HAL. Actual start time could be longer
184 // because the process might not have joined the threadpool yet, so it might not be ready to
185 // process transactions.
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700186 LOG(INFO) << "Registered " << descriptor << "/" << instanceName << " (start delay of "
Peter Kalauskas64ffced2018-09-05 16:41:08 -0700187 << halStartDelay << "ms)";
188 }
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700189
190 tryShortenProcessName(descriptor);
191}
192
193void onRegistration(const std::string& packageName, const std::string& interfaceName,
194 const std::string& instanceName) {
195 return onRegistrationImpl(packageName + "::" + interfaceName, instanceName);
Steven Moreland405d7612017-04-07 20:31:22 -0700196}
197
198} // details
199
Steven Moreland2a2678e2017-07-21 18:07:38 -0700200sp<IServiceManager1_0> defaultServiceManager() {
Steven Moreland89909692018-05-30 14:11:19 -0700201 return defaultServiceManager1_2();
Steven Moreland2a2678e2017-07-21 18:07:38 -0700202}
203sp<IServiceManager1_1> defaultServiceManager1_1() {
Steven Moreland89909692018-05-30 14:11:19 -0700204 return defaultServiceManager1_2();
205}
206sp<IServiceManager1_2> defaultServiceManager1_2() {
207 using android::hidl::manager::V1_2::BnHwServiceManager;
208 using android::hidl::manager::V1_2::BpHwServiceManager;
209
210 static std::mutex gDefaultServiceManagerLock;
211 static sp<IServiceManager1_2> gDefaultServiceManager;
212
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700213 {
Steven Moreland89909692018-05-30 14:11:19 -0700214 std::lock_guard<std::mutex> _l(gDefaultServiceManagerLock);
215 if (gDefaultServiceManager != nullptr) {
216 return gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -0700217 }
Steven Moreland405d7612017-04-07 20:31:22 -0700218
Yifan Hong8fb656b2017-03-16 14:30:40 -0700219 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
220 // HwBinder not available on this device or not accessible to
221 // this process.
222 return nullptr;
223 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000224
225 waitForHwServiceManager();
226
Steven Moreland89909692018-05-30 14:11:19 -0700227 while (gDefaultServiceManager == nullptr) {
228 gDefaultServiceManager =
229 fromBinder<IServiceManager1_2, BpHwServiceManager, BnHwServiceManager>(
230 ProcessState::self()->getContextObject(nullptr));
231 if (gDefaultServiceManager == nullptr) {
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000232 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700233 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -0700234 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700235 }
236 }
237
Steven Moreland89909692018-05-30 14:11:19 -0700238 return gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700239}
240
Elliott Hughes19306142018-10-26 13:13:04 -0700241static std::vector<std::string> findFiles(const std::string& path, const std::string& prefix,
242 const std::string& suffix) {
Steven Moreland0091c092017-01-20 23:15:18 +0000243 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
244 if (!dir) return {};
245
246 std::vector<std::string> results{};
247
248 dirent* dp;
249 while ((dp = readdir(dir.get())) != nullptr) {
250 std::string name = dp->d_name;
251
Steven Moreland8092aa02018-10-12 15:54:56 -0700252 if (base::StartsWith(name, prefix) && base::EndsWith(name, suffix)) {
Steven Moreland0091c092017-01-20 23:15:18 +0000253 results.push_back(name);
254 }
255 }
256
257 return results;
258}
259
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700260bool matchPackageName(const std::string& lib, std::string* matchedName, std::string* implName) {
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800261 std::smatch match;
262 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
263 *matchedName = match.str(1) + "::I*";
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700264 *implName = match.str(2);
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800265 return true;
266 }
267 return false;
268}
269
Yifan Hong7f49f592017-02-03 15:11:44 -0800270static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700271 if (kIsRecovery) {
272 // No hwservicemanager in recovery.
273 return;
274 }
275
Steven Moreland2a2678e2017-07-21 18:07:38 -0700276 sp<IServiceManager1_0> binderizedManager = defaultServiceManager();
Yifan Hong7f49f592017-02-03 15:11:44 -0800277 if (binderizedManager == nullptr) {
278 LOG(WARNING) << "Could not registerReference for "
279 << interfaceName << "/" << instanceName
280 << ": null binderized manager.";
281 return;
282 }
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700283 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName);
Yifan Hong7f49f592017-02-03 15:11:44 -0800284 if (!ret.isOk()) {
285 LOG(WARNING) << "Could not registerReference for "
286 << interfaceName << "/" << instanceName
287 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700288 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800289 }
Steven Morelande681aa52017-02-15 16:22:37 -0800290 LOG(VERBOSE) << "Successfully registerReference for "
291 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800292}
293
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700294using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo;
295static inline void fetchPidsForPassthroughLibraries(
296 std::map<std::string, InstanceDebugInfo>* infos) {
297 static const std::string proc = "/proc/";
298
299 std::map<std::string, std::set<pid_t>> pids;
300 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir);
301 if (!dir) return;
302 dirent* dp;
303 while ((dp = readdir(dir.get())) != nullptr) {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700304 pid_t pid = strtoll(dp->d_name, nullptr, 0);
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700305 if (pid == 0) continue;
306 std::string mapsPath = proc + dp->d_name + "/maps";
307 std::ifstream ifs{mapsPath};
308 if (!ifs.is_open()) continue;
309
310 for (std::string line; std::getline(ifs, line);) {
311 // The last token of line should look like
312 // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so
313 // Use some simple filters to ignore bad lines before extracting libFileName
314 // and checking the key in info to make parsing faster.
315 if (line.back() != 'o') continue;
316 if (line.rfind('@') == std::string::npos) continue;
317
318 auto spacePos = line.rfind(' ');
319 if (spacePos == std::string::npos) continue;
320 auto libFileName = line.substr(spacePos + 1);
321 auto it = infos->find(libFileName);
322 if (it == infos->end()) continue;
323 pids[libFileName].insert(pid);
324 }
325 }
326 for (auto& pair : *infos) {
327 pair.second.clientPids =
328 std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()};
329 }
330}
331
Steven Moreland2a2678e2017-07-21 18:07:38 -0700332struct PassthroughServiceManager : IServiceManager1_1 {
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700333 static void openLibs(
334 const std::string& fqName,
335 const std::function<bool /* continue */ (void* /* handle */, const std::string& /* lib */,
336 const std::string& /* sym */)>& eachLib) {
Steven Moreland819c05d2017-04-06 17:24:22 -0700337 //fqName looks like android.hardware.foo@1.0::IFoo
Steven Moreland519306f2017-06-06 17:14:12 -0700338 size_t idx = fqName.find("::");
Steven Moreland819c05d2017-04-06 17:24:22 -0700339
340 if (idx == std::string::npos ||
Steven Moreland519306f2017-06-06 17:14:12 -0700341 idx + strlen("::") + 1 >= fqName.size()) {
Steven Moreland337e6b62017-01-18 17:25:13 -0800342 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
Steven Moreland519306f2017-06-06 17:14:12 -0700343 return;
Steven Moreland337e6b62017-01-18 17:25:13 -0800344 }
345
Steven Moreland519306f2017-06-06 17:14:12 -0700346 std::string packageAndVersion = fqName.substr(0, idx);
347 std::string ifaceName = fqName.substr(idx + strlen("::"));
Steven Moreland819c05d2017-04-06 17:24:22 -0700348
349 const std::string prefix = packageAndVersion + "-impl";
350 const std::string sym = "HIDL_FETCH_" + ifaceName;
Steven Moreland348802d2017-02-23 12:48:55 -0800351
Steven Moreland77f4c852017-10-12 11:05:53 -0700352 constexpr int dlMode = RTLD_LAZY;
353 void* handle = nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800354
Steven Morelanda29905c2017-03-01 10:42:35 -0800355 dlerror(); // clear
356
Justin Yun1f048102017-12-01 15:30:08 +0900357 static std::string halLibPathVndkSp = android::base::StringPrintf(
358 HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION, details::getVndkVersionStr().c_str());
Justin Yun6a323ed2018-10-18 18:41:56 +0900359 std::vector<std::string> paths = {
360 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, halLibPathVndkSp,
361#ifndef __ANDROID_VNDK__
362 HAL_LIBRARY_PATH_SYSTEM,
363#endif
364 };
Steven Moreland77f4c852017-10-12 11:05:53 -0700365
Steven Morelandf7dea692017-07-14 12:49:28 -0700366#ifdef LIBHIDL_TARGET_DEBUGGABLE
367 const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
368 const bool trebleTestingOverride = env && !strcmp(env, "true");
369 if (trebleTestingOverride) {
Steven Moreland77f4c852017-10-12 11:05:53 -0700370 // Load HAL implementations that are statically linked
371 handle = dlopen(nullptr, dlMode);
372 if (handle == nullptr) {
373 const char* error = dlerror();
374 LOG(ERROR) << "Failed to dlopen self: "
375 << (error == nullptr ? "unknown error" : error);
376 } else if (!eachLib(handle, "SELF", sym)) {
377 return;
378 }
379
Steven Morelandf7dea692017-07-14 12:49:28 -0700380 const char* vtsRootPath = std::getenv("VTS_ROOT_PATH");
381 if (vtsRootPath && strlen(vtsRootPath) > 0) {
382 const std::string halLibraryPathVtsOverride =
383 std::string(vtsRootPath) + HAL_LIBRARY_PATH_SYSTEM;
Steven Moreland77f4c852017-10-12 11:05:53 -0700384 paths.insert(paths.begin(), halLibraryPathVtsOverride);
Steven Morelandf7dea692017-07-14 12:49:28 -0700385 }
386 }
387#endif
Steven Moreland77f4c852017-10-12 11:05:53 -0700388
Steven Morelandf7dea692017-07-14 12:49:28 -0700389 for (const std::string& path : paths) {
Elliott Hughes19306142018-10-26 13:13:04 -0700390 std::vector<std::string> libs = findFiles(path, prefix, ".so");
Steven Moreland0091c092017-01-20 23:15:18 +0000391
Steven Moreland0091c092017-01-20 23:15:18 +0000392 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800393 const std::string fullPath = path + lib;
394
Yifan Hongdeacf142018-07-10 17:33:34 -0700395 if (kIsRecovery || path == HAL_LIBRARY_PATH_SYSTEM) {
Jiyong Park3ab546c2017-04-06 20:28:07 +0900396 handle = dlopen(fullPath.c_str(), dlMode);
Steven Moreland65c00cb2017-10-12 11:35:22 -0700397 } else {
Jerry Zhang86ae9992018-05-30 17:11:09 -0700398#if !defined(__ANDROID_RECOVERY__)
Steven Moreland65c00cb2017-10-12 11:35:22 -0700399 handle = android_load_sphal_library(fullPath.c_str(), dlMode);
Jerry Zhang86ae9992018-05-30 17:11:09 -0700400#endif
Jiyong Park3ab546c2017-04-06 20:28:07 +0900401 }
402
Steven Moreland348802d2017-02-23 12:48:55 -0800403 if (handle == nullptr) {
404 const char* error = dlerror();
405 LOG(ERROR) << "Failed to dlopen " << lib << ": "
406 << (error == nullptr ? "unknown error" : error);
407 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000408 }
Steven Moreland348802d2017-02-23 12:48:55 -0800409
Steven Moreland519306f2017-06-06 17:14:12 -0700410 if (!eachLib(handle, lib, sym)) {
411 return;
Steven Moreland348802d2017-02-23 12:48:55 -0800412 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800413 }
414 }
Steven Moreland519306f2017-06-06 17:14:12 -0700415 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800416
Steven Moreland519306f2017-06-06 17:14:12 -0700417 Return<sp<IBase>> get(const hidl_string& fqName,
418 const hidl_string& name) override {
419 sp<IBase> ret = nullptr;
420
421 openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) {
422 IBase* (*generator)(const char* name);
423 *(void **)(&generator) = dlsym(handle, sym.c_str());
424 if(!generator) {
425 const char* error = dlerror();
426 LOG(ERROR) << "Passthrough lookup opened " << lib
427 << " but could not find symbol " << sym << ": "
428 << (error == nullptr ? "unknown error" : error);
429 dlclose(handle);
430 return true;
431 }
432
433 ret = (*generator)(name.c_str());
434
435 if (ret == nullptr) {
436 dlclose(handle);
437 return true; // this module doesn't provide this instance name
438 }
439
Steven Morelande69e8d32018-03-14 10:55:42 -0700440 // Actual fqname might be a subclass.
441 // This assumption is tested in vts_treble_vintf_test
442 using ::android::hardware::details::getDescriptor;
443 std::string actualFqName = getDescriptor(ret.get());
444 CHECK(actualFqName.size() > 0);
445 registerReference(actualFqName, name);
Steven Moreland519306f2017-06-06 17:14:12 -0700446 return false;
447 });
448
449 return ret;
Steven Moreland337e6b62017-01-18 17:25:13 -0800450 }
451
Martijn Coenen67a02492017-03-06 13:04:48 +0100452 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800453 const sp<IBase>& /* service */) override {
454 LOG(FATAL) << "Cannot register services with passthrough service manager.";
455 return false;
456 }
457
Steven Morelandc601c5f2017-04-06 09:26:07 -0700458 Return<Transport> getTransport(const hidl_string& /* fqName */,
459 const hidl_string& /* name */) {
460 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
461 return Transport::EMPTY;
462 }
463
Yifan Hong705e5da2017-03-02 16:59:39 -0800464 Return<void> list(list_cb /* _hidl_cb */) override {
465 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800466 return Void();
467 }
468 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
469 listByInterface_cb /* _hidl_cb */) override {
470 // TODO: add this functionality
471 LOG(FATAL) << "Cannot list services with passthrough service manager.";
472 return Void();
473 }
474
475 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
476 const hidl_string& /* name */,
477 const sp<IServiceNotification>& /* callback */) override {
478 // This makes no sense.
479 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
480 return false;
481 }
482
Yifan Hong705e5da2017-03-02 16:59:39 -0800483 Return<void> debugDump(debugDump_cb _hidl_cb) override {
484 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700485 using std::literals::string_literals::operator""s;
Justin Yun1f048102017-12-01 15:30:08 +0900486 static std::string halLibPathVndkSp64 = android::base::StringPrintf(
487 HAL_LIBRARY_PATH_VNDK_SP_64BIT_FOR_VERSION, details::getVndkVersionStr().c_str());
488 static std::string halLibPathVndkSp32 = android::base::StringPrintf(
489 HAL_LIBRARY_PATH_VNDK_SP_32BIT_FOR_VERSION, details::getVndkVersionStr().c_str());
Jiyong Park56eb1492017-08-04 16:16:13 +0900490 static std::vector<std::pair<Arch, std::vector<const char*>>> sAllPaths{
491 {Arch::IS_64BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900492 {
493 HAL_LIBRARY_PATH_ODM_64BIT, HAL_LIBRARY_PATH_VENDOR_64BIT,
494 halLibPathVndkSp64.c_str(),
495#ifndef __ANDROID_VNDK__
496 HAL_LIBRARY_PATH_SYSTEM_64BIT,
497#endif
498 }},
Jiyong Park56eb1492017-08-04 16:16:13 +0900499 {Arch::IS_32BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900500 {
501 HAL_LIBRARY_PATH_ODM_32BIT, HAL_LIBRARY_PATH_VENDOR_32BIT,
502 halLibPathVndkSp32.c_str(),
503#ifndef __ANDROID_VNDK__
504 HAL_LIBRARY_PATH_SYSTEM_32BIT,
505#endif
506 }}};
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700507 std::map<std::string, InstanceDebugInfo> map;
Yifan Hong705e5da2017-03-02 16:59:39 -0800508 for (const auto &pair : sAllPaths) {
509 Arch arch = pair.first;
510 for (const auto &path : pair.second) {
Elliott Hughes19306142018-10-26 13:13:04 -0700511 std::vector<std::string> libs = findFiles(path, "", ".so");
Yifan Hong705e5da2017-03-02 16:59:39 -0800512 for (const std::string &lib : libs) {
513 std::string matchedName;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700514 std::string implName;
515 if (matchPackageName(lib, &matchedName, &implName)) {
516 std::string instanceName{"* ("s + path + ")"s};
517 if (!implName.empty()) instanceName += " ("s + implName + ")"s;
518 map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName,
519 .instanceName = instanceName,
520 .clientPids = {},
521 .arch = arch});
Yifan Hong705e5da2017-03-02 16:59:39 -0800522 }
523 }
524 }
525 }
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700526 fetchPidsForPassthroughLibraries(&map);
527 hidl_vec<InstanceDebugInfo> vec;
528 vec.resize(map.size());
529 size_t idx = 0;
530 for (auto&& pair : map) {
531 vec[idx++] = std::move(pair.second);
532 }
Yifan Hong705e5da2017-03-02 16:59:39 -0800533 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800534 return Void();
535 }
536
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700537 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override {
Yifan Hong7f49f592017-02-03 15:11:44 -0800538 // This makes no sense.
539 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
540 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800541 return Void();
542 }
543
Steven Moreland2a2678e2017-07-21 18:07:38 -0700544 Return<bool> unregisterForNotifications(const hidl_string& /* fqName */,
545 const hidl_string& /* name */,
546 const sp<IServiceNotification>& /* callback */) override {
547 // This makes no sense.
548 LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager.";
549 return false;
550 }
551
Steven Moreland337e6b62017-01-18 17:25:13 -0800552};
553
Steven Moreland2a2678e2017-07-21 18:07:38 -0700554sp<IServiceManager1_0> getPassthroughServiceManager() {
555 return getPassthroughServiceManager1_1();
556}
557sp<IServiceManager1_1> getPassthroughServiceManager1_1() {
Steven Moreland337e6b62017-01-18 17:25:13 -0800558 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
559 return manager;
560}
561
Steven Morelandcbefd352017-01-23 20:29:05 -0800562namespace details {
563
Steven Moreland519306f2017-06-06 17:14:12 -0700564void preloadPassthroughService(const std::string &descriptor) {
565 PassthroughServiceManager::openLibs(descriptor,
566 [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) {
567 // do nothing
568 return true; // open all libs
569 });
570}
571
Steven Morelandcbefd352017-01-23 20:29:05 -0800572struct Waiter : IServiceNotification {
Martijn Coenen773609e2017-10-24 09:57:53 +0200573 Waiter(const std::string& interface, const std::string& instanceName,
574 const sp<IServiceManager1_1>& sm) : mInterfaceName(interface),
575 mInstanceName(instanceName), mSm(sm) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100576 }
577
578 void onFirstRef() override {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200579 // If this process only has one binder thread, and we're calling wait() from
580 // that thread, it will block forever because we hung up the one and only
581 // binder thread on a condition variable that can only be notified by an
582 // incoming binder call.
Tobias Lindskog88e886f2018-01-05 10:32:43 +0100583 if (IPCThreadState::self()->isOnlyBinderThread()) {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200584 LOG(WARNING) << "Can't efficiently wait for " << mInterfaceName << "/"
585 << mInstanceName << ", because we are called from "
586 << "the only binder thread in this process.";
587 return;
588 }
589
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100590 Return<bool> ret = mSm->registerForNotifications(mInterfaceName, mInstanceName, this);
Martijn Coenen773609e2017-10-24 09:57:53 +0200591
592 if (!ret.isOk()) {
593 LOG(ERROR) << "Transport error, " << ret.description()
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100594 << ", during notification registration for " << mInterfaceName << "/"
595 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200596 return;
597 }
598
599 if (!ret) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100600 LOG(ERROR) << "Could not register for notifications for " << mInterfaceName << "/"
601 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200602 return;
603 }
604
605 mRegisteredForNotifications = true;
606 }
607
608 ~Waiter() {
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100609 if (!mDoneCalled) {
610 LOG(FATAL)
611 << "Waiter still registered for notifications, call done() before dropping ref!";
Martijn Coenen773609e2017-10-24 09:57:53 +0200612 }
613 }
614
Steven Morelandcbefd352017-01-23 20:29:05 -0800615 Return<void> onRegistration(const hidl_string& /* fqName */,
616 const hidl_string& /* name */,
617 bool /* preexisting */) override {
618 std::unique_lock<std::mutex> lock(mMutex);
619 if (mRegistered) {
620 return Void();
621 }
622 mRegistered = true;
623 lock.unlock();
624
625 mCondition.notify_one();
626 return Void();
627 }
628
Steven Moreland0771d8a2018-05-09 13:29:14 -0700629 void wait(bool timeout) {
Steven Moreland49605102017-03-28 09:33:06 -0700630 using std::literals::chrono_literals::operator""s;
631
Martijn Coenen773609e2017-10-24 09:57:53 +0200632 if (!mRegisteredForNotifications) {
633 // As an alternative, just sleep for a second and return
634 LOG(WARNING) << "Waiting one second for " << mInterfaceName << "/" << mInstanceName;
635 sleep(1);
636 return;
637 }
638
Steven Morelandcbefd352017-01-23 20:29:05 -0800639 std::unique_lock<std::mutex> lock(mMutex);
Steven Moreland0771d8a2018-05-09 13:29:14 -0700640 do {
Steven Moreland49605102017-03-28 09:33:06 -0700641 mCondition.wait_for(lock, 1s, [this]{
642 return mRegistered;
643 });
644
645 if (mRegistered) {
646 break;
647 }
648
Steven Moreland0771d8a2018-05-09 13:29:14 -0700649 LOG(WARNING) << "Waited one second for " << mInterfaceName << "/" << mInstanceName;
650 } while (!timeout);
Steven Morelandcbefd352017-01-23 20:29:05 -0800651 }
652
Martijn Coenen773609e2017-10-24 09:57:53 +0200653 // Be careful when using this; after calling reset(), you must always try to retrieve
654 // the corresponding service before blocking on the waiter; otherwise, you might run
655 // into a race-condition where the service has just (re-)registered, you clear the state
656 // here, and subsequently calling waiter->wait() will block forever.
657 void reset() {
658 std::unique_lock<std::mutex> lock(mMutex);
659 mRegistered = false;
660 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100661
662 // done() must be called before dropping the last strong ref to the Waiter, to make
663 // sure we can properly unregister with hwservicemanager.
664 void done() {
665 if (mRegisteredForNotifications) {
666 if (!mSm->unregisterForNotifications(mInterfaceName, mInstanceName, this)
667 .withDefault(false)) {
668 LOG(ERROR) << "Could not unregister service notification for " << mInterfaceName
669 << "/" << mInstanceName << ".";
670 } else {
671 mRegisteredForNotifications = false;
672 }
673 }
674 mDoneCalled = true;
675 }
676
677 private:
Martijn Coenen773609e2017-10-24 09:57:53 +0200678 const std::string mInterfaceName;
679 const std::string mInstanceName;
Steven Morelandad1f9922018-10-02 19:37:05 -0700680 sp<IServiceManager1_1> mSm;
Steven Morelandcbefd352017-01-23 20:29:05 -0800681 std::mutex mMutex;
682 std::condition_variable mCondition;
683 bool mRegistered = false;
Martijn Coenen773609e2017-10-24 09:57:53 +0200684 bool mRegisteredForNotifications = false;
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100685 bool mDoneCalled = false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800686};
687
688void waitForHwService(
689 const std::string &interface, const std::string &instanceName) {
Martijn Coenen773609e2017-10-24 09:57:53 +0200690 sp<Waiter> waiter = new Waiter(interface, instanceName, defaultServiceManager1_1());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700691 waiter->wait(false /* timeout */);
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100692 waiter->done();
Martijn Coenen773609e2017-10-24 09:57:53 +0200693}
Steven Morelandcbefd352017-01-23 20:29:05 -0800694
Martijn Coenen773609e2017-10-24 09:57:53 +0200695// Prints relevant error/warning messages for error return values from
696// details::canCastInterface(), both transaction errors (!castReturn.isOk())
697// as well as actual cast failures (castReturn.isOk() && castReturn = false).
698// Returns 'true' if the error is non-fatal and it's useful to retry
699bool handleCastError(const Return<bool>& castReturn, const std::string& descriptor,
700 const std::string& instance) {
701 if (castReturn.isOk()) {
702 if (castReturn) {
703 details::logAlwaysFatal("Successful cast value passed into handleCastError.");
704 }
705 // This should never happen, and there's not really a point in retrying.
706 ALOGE("getService: received incompatible service (bug in hwservicemanager?) for "
707 "%s/%s.", descriptor.c_str(), instance.c_str());
708 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800709 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200710 if (castReturn.isDeadObject()) {
711 ALOGW("getService: found dead hwbinder service for %s/%s.", descriptor.c_str(),
712 instance.c_str());
713 return true;
Steven Morelandcbefd352017-01-23 20:29:05 -0800714 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200715 // This can happen due to:
716 // 1) No SELinux permissions
717 // 2) Other transaction failure (no buffer space, kernel error)
718 // The first isn't recoverable, but the second is.
719 // Since we can't yet differentiate between the two, and clients depend
720 // on us not blocking in case 1), treat this as a fatal error for now.
721 ALOGW("getService: unable to call into hwbinder service for %s/%s.",
722 descriptor.c_str(), instance.c_str());
723 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800724}
725
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200726sp<::android::hidl::base::V1_0::IBase> getRawServiceInternal(const std::string& descriptor,
727 const std::string& instance,
728 bool retry, bool getStub) {
729 using Transport = ::android::hidl::manager::V1_0::IServiceManager::Transport;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200730 using ::android::hidl::manager::V1_0::IServiceManager;
Martijn Coenend35678f2018-03-07 09:51:01 +0100731 sp<Waiter> waiter;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200732
Yifan Hongdeacf142018-07-10 17:33:34 -0700733 sp<IServiceManager1_1> sm;
734 Transport transport = Transport::EMPTY;
735 if (kIsRecovery) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700736 transport = Transport::PASSTHROUGH;
Yifan Hongdeacf142018-07-10 17:33:34 -0700737 } else {
738 sm = defaultServiceManager1_1();
739 if (sm == nullptr) {
740 ALOGE("getService: defaultServiceManager() is null");
741 return nullptr;
742 }
743
744 Return<Transport> transportRet = sm->getTransport(descriptor, instance);
745
746 if (!transportRet.isOk()) {
747 ALOGE("getService: defaultServiceManager()->getTransport returns %s",
748 transportRet.description().c_str());
749 return nullptr;
750 }
751 transport = transportRet;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200752 }
753
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200754 const bool vintfHwbinder = (transport == Transport::HWBINDER);
755 const bool vintfPassthru = (transport == Transport::PASSTHROUGH);
756
Steven Moreland757394b2017-12-13 14:09:24 -0800757#ifdef ENFORCE_VINTF_MANIFEST
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200758
759#ifdef LIBHIDL_TARGET_DEBUGGABLE
760 const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
761 const bool trebleTestingOverride = env && !strcmp(env, "true");
762 const bool vintfLegacy = (transport == Transport::EMPTY) && trebleTestingOverride;
Steven Moreland757394b2017-12-13 14:09:24 -0800763#else // ENFORCE_VINTF_MANIFEST but not LIBHIDL_TARGET_DEBUGGABLE
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200764 const bool trebleTestingOverride = false;
765 const bool vintfLegacy = false;
766#endif // LIBHIDL_TARGET_DEBUGGABLE
767
Steven Moreland757394b2017-12-13 14:09:24 -0800768#else // not ENFORCE_VINTF_MANIFEST
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200769 const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
770 const bool trebleTestingOverride = env && !strcmp(env, "true");
771 const bool vintfLegacy = (transport == Transport::EMPTY);
Steven Moreland5b8eb352019-06-06 11:09:52 -0700772
773 ALOGE("getService: Potential race detected. The VINTF manifest is not being enforced. If a HAL "
774 "server has a delay in starting and it is not in the manifest, it will not be retrieved. "
775 "Please make sure all HALs on this device are in the VINTF manifest and enable "
776 "PRODUCT_ENFORCE_VINTF_MANIFEST on this device (this is also enabled by "
777 "PRODUCT_FULL_TREBLE). PRODUCT_ENFORCE_VINTF_MANIFEST will ensure that no race condition "
778 "is possible here.");
Steven Moreland757394b2017-12-13 14:09:24 -0800779#endif // ENFORCE_VINTF_MANIFEST
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200780
Steven Moreland76371242018-04-19 17:11:39 -0700781 for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) {
782 if (waiter == nullptr && tries > 0) {
Martijn Coenend35678f2018-03-07 09:51:01 +0100783 waiter = new Waiter(descriptor, instance, sm);
784 }
Steven Moreland76371242018-04-19 17:11:39 -0700785 if (waiter != nullptr) {
786 waiter->reset(); // don't reorder this -- see comments on reset()
787 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200788 Return<sp<IBase>> ret = sm->get(descriptor, instance);
789 if (!ret.isOk()) {
790 ALOGE("getService: defaultServiceManager()->get returns %s for %s/%s.",
791 ret.description().c_str(), descriptor.c_str(), instance.c_str());
792 break;
793 }
794 sp<IBase> base = ret;
Martijn Coenen773609e2017-10-24 09:57:53 +0200795 if (base != nullptr) {
796 Return<bool> canCastRet =
797 details::canCastInterface(base.get(), descriptor.c_str(), true /* emitError */);
798
799 if (canCastRet.isOk() && canCastRet) {
Steven Moreland76371242018-04-19 17:11:39 -0700800 if (waiter != nullptr) {
801 waiter->done();
802 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200803 return base; // still needs to be wrapped by Bp class.
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200804 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200805
806 if (!handleCastError(canCastRet, descriptor, instance)) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200807 }
808
Martijn Coenen773609e2017-10-24 09:57:53 +0200809 // In case of legacy or we were not asked to retry, don't.
810 if (vintfLegacy || !retry) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200811
Steven Moreland76371242018-04-19 17:11:39 -0700812 if (waiter != nullptr) {
813 ALOGI("getService: Trying again for %s/%s...", descriptor.c_str(), instance.c_str());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700814 waiter->wait(true /* timeout */);
Steven Moreland76371242018-04-19 17:11:39 -0700815 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200816 }
817
Martijn Coenend35678f2018-03-07 09:51:01 +0100818 if (waiter != nullptr) {
819 waiter->done();
820 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100821
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200822 if (getStub || vintfPassthru || vintfLegacy) {
823 const sp<IServiceManager> pm = getPassthroughServiceManager();
824 if (pm != nullptr) {
825 sp<IBase> base = pm->get(descriptor, instance).withDefault(nullptr);
826 if (!getStub || trebleTestingOverride) {
827 base = wrapPassthrough(base);
828 }
829 return base;
830 }
831 }
832
833 return nullptr;
834}
835
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700836status_t registerAsServiceInternal(const sp<IBase>& service, const std::string& name) {
837 if (service == nullptr) {
838 return UNEXPECTED_NULL;
839 }
840
841 sp<IServiceManager1_2> sm = defaultServiceManager1_2();
842 if (sm == nullptr) {
843 return INVALID_OPERATION;
844 }
845
846 bool registered = false;
847 Return<void> ret = service->interfaceChain([&](const auto& chain) {
848 registered = sm->addWithChain(name.c_str(), service, chain).withDefault(false);
849 });
850
851 if (!ret.isOk()) {
852 LOG(ERROR) << "Could not retrieve interface chain: " << ret.description();
853 }
854
855 if (registered) {
856 onRegistrationImpl(getDescriptor(service.get()), name);
857 }
858
859 return registered ? OK : UNKNOWN_ERROR;
860}
861
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +0200862} // namespace details
Steven Morelandcbefd352017-01-23 20:29:05 -0800863
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +0200864} // namespace hardware
865} // namespace android