blob: 8be2c76490ef2ebf6d4d8d2dc2e74c6d1070d939 [file] [log] [blame]
Steven Moreland5d5ef7f2016-10-20 19:19:55 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "ServiceManagement"
18
Yifan Hong9a22d1d2017-01-25 14:19:26 -080019#include <condition_variable>
20#include <dlfcn.h>
21#include <dirent.h>
Steven Morelandcd4dbdf2017-04-07 20:31:22 -070022#include <fstream>
23#include <pthread.h>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080024#include <unistd.h>
25
26#include <mutex>
27#include <regex>
Yifan Hongbd0d8f72017-05-24 19:43:51 -070028#include <set>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080029
Martijn Coenen12f04d92016-12-07 17:29:41 +010030#include <hidl/HidlBinderSupport.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070031#include <hidl/ServiceManagement.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070032#include <hidl/Status.h>
33
Steven Moreland337e6b62017-01-18 17:25:13 -080034#include <android-base/logging.h>
Steven Morelandc1cee2c2017-03-24 16:23:11 +000035#include <android-base/properties.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070036#include <hwbinder/IPCThreadState.h>
37#include <hwbinder/Parcel.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070038
39#include <android/hidl/manager/1.0/IServiceManager.h>
Yifan Hong4e925992017-01-09 17:47:17 -080040#include <android/hidl/manager/1.0/BpHwServiceManager.h>
41#include <android/hidl/manager/1.0/BnHwServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070042
Yifan Hong9a22d1d2017-01-25 14:19:26 -080043#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
44#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
45static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
46
Steven Morelandc1cee2c2017-03-24 16:23:11 +000047using android::base::WaitForProperty;
48
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070049using android::hidl::manager::V1_0::IServiceManager;
Steven Moreland337e6b62017-01-18 17:25:13 -080050using android::hidl::manager::V1_0::IServiceNotification;
Yifan Hong4e925992017-01-09 17:47:17 -080051using android::hidl::manager::V1_0::BpHwServiceManager;
52using android::hidl::manager::V1_0::BnHwServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070053
54namespace android {
55namespace hardware {
56
Yifan Hong953e6b02017-03-16 14:52:40 -070057namespace details {
58extern Mutex gDefaultServiceManagerLock;
59extern sp<android::hidl::manager::V1_0::IServiceManager> gDefaultServiceManager;
60} // namespace details
61
Steven Morelandc1cee2c2017-03-24 16:23:11 +000062static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready";
63
64void waitForHwServiceManager() {
65 using std::literals::chrono_literals::operator""s;
66
67 while (!WaitForProperty(kHwServicemanagerReadyProperty, "true", 1s)) {
68 LOG(WARNING) << "Waited for hwservicemanager.ready for a second, waiting another...";
69 }
70}
71
Steven Morelandcd4dbdf2017-04-07 20:31:22 -070072bool endsWith(const std::string &in, const std::string &suffix) {
73 return in.size() >= suffix.size() &&
74 in.substr(in.size() - suffix.size()) == suffix;
75}
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070076
Steven Morelandcd4dbdf2017-04-07 20:31:22 -070077bool startsWith(const std::string &in, const std::string &prefix) {
78 return in.size() >= prefix.size() &&
79 in.substr(0, prefix.size()) == prefix;
80}
81
82std::string binaryName() {
83 std::ifstream ifs("/proc/self/cmdline");
84 std::string cmdline;
85 if (!ifs.is_open()) {
86 return "";
87 }
88 ifs >> cmdline;
89
90 size_t idx = cmdline.rfind("/");
91 if (idx != std::string::npos) {
92 cmdline = cmdline.substr(idx + 1);
93 }
94
95 return cmdline;
96}
97
98void tryShortenProcessName(const std::string &packageName) {
99 std::string processName = binaryName();
100
101 if (!startsWith(processName, packageName)) {
102 return;
103 }
104
105 // e.x. android.hardware.module.foo@1.0 -> foo@1.0
106 size_t lastDot = packageName.rfind('.');
107 size_t secondDot = packageName.rfind('.', lastDot - 1);
108
109 if (secondDot == std::string::npos) {
110 return;
111 }
112
113 std::string newName = processName.substr(secondDot + 1,
114 16 /* TASK_COMM_LEN */ - 1);
115 ALOGI("Removing namespace from process name %s to %s.",
116 processName.c_str(), newName.c_str());
117
118 int rc = pthread_setname_np(pthread_self(), newName.c_str());
119 ALOGI_IF(rc != 0, "Removing namespace from process name %s failed.",
120 processName.c_str());
121}
122
123namespace details {
124
125void onRegistration(const std::string &packageName,
126 const std::string& /* interfaceName */,
127 const std::string& /* instanceName */) {
128 tryShortenProcessName(packageName);
129}
130
131} // details
132
133sp<IServiceManager> defaultServiceManager() {
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700134 {
Yifan Hong953e6b02017-03-16 14:52:40 -0700135 AutoMutex _l(details::gDefaultServiceManagerLock);
136 if (details::gDefaultServiceManager != NULL) {
137 return details::gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -0700138 }
Steven Morelandcd4dbdf2017-04-07 20:31:22 -0700139
Yifan Hong8fb656b2017-03-16 14:30:40 -0700140 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
141 // HwBinder not available on this device or not accessible to
142 // this process.
143 return nullptr;
144 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000145
146 waitForHwServiceManager();
147
Yifan Hong953e6b02017-03-16 14:52:40 -0700148 while (details::gDefaultServiceManager == NULL) {
149 details::gDefaultServiceManager =
Yifan Hong8fb656b2017-03-16 14:30:40 -0700150 fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>(
151 ProcessState::self()->getContextObject(NULL));
Yifan Hong953e6b02017-03-16 14:52:40 -0700152 if (details::gDefaultServiceManager == NULL) {
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000153 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700154 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -0700155 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700156 }
157 }
158
Yifan Hong953e6b02017-03-16 14:52:40 -0700159 return details::gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700160}
161
Steven Moreland0091c092017-01-20 23:15:18 +0000162std::vector<std::string> search(const std::string &path,
163 const std::string &prefix,
164 const std::string &suffix) {
165 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
166 if (!dir) return {};
167
168 std::vector<std::string> results{};
169
170 dirent* dp;
171 while ((dp = readdir(dir.get())) != nullptr) {
172 std::string name = dp->d_name;
173
Steven Morelandda8e6172017-04-06 17:24:22 -0700174 if (startsWith(name, prefix) &&
175 endsWith(name, suffix)) {
Steven Moreland0091c092017-01-20 23:15:18 +0000176 results.push_back(name);
177 }
178 }
179
180 return results;
181}
182
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700183bool matchPackageName(const std::string& lib, std::string* matchedName, std::string* implName) {
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800184 std::smatch match;
185 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
186 *matchedName = match.str(1) + "::I*";
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700187 *implName = match.str(2);
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800188 return true;
189 }
190 return false;
191}
192
Yifan Hong7f49f592017-02-03 15:11:44 -0800193static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
194 sp<IServiceManager> binderizedManager = defaultServiceManager();
195 if (binderizedManager == nullptr) {
196 LOG(WARNING) << "Could not registerReference for "
197 << interfaceName << "/" << instanceName
198 << ": null binderized manager.";
199 return;
200 }
Martijn Coenenbf13ad02017-04-27 09:41:13 -0700201 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName);
Yifan Hong7f49f592017-02-03 15:11:44 -0800202 if (!ret.isOk()) {
203 LOG(WARNING) << "Could not registerReference for "
204 << interfaceName << "/" << instanceName
205 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700206 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800207 }
Steven Morelande681aa52017-02-15 16:22:37 -0800208 LOG(VERBOSE) << "Successfully registerReference for "
209 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800210}
211
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700212using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo;
213static inline void fetchPidsForPassthroughLibraries(
214 std::map<std::string, InstanceDebugInfo>* infos) {
215 static const std::string proc = "/proc/";
216
217 std::map<std::string, std::set<pid_t>> pids;
218 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir);
219 if (!dir) return;
220 dirent* dp;
221 while ((dp = readdir(dir.get())) != nullptr) {
222 pid_t pid = strtoll(dp->d_name, NULL, 0);
223 if (pid == 0) continue;
224 std::string mapsPath = proc + dp->d_name + "/maps";
225 std::ifstream ifs{mapsPath};
226 if (!ifs.is_open()) continue;
227
228 for (std::string line; std::getline(ifs, line);) {
229 // The last token of line should look like
230 // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so
231 // Use some simple filters to ignore bad lines before extracting libFileName
232 // and checking the key in info to make parsing faster.
233 if (line.back() != 'o') continue;
234 if (line.rfind('@') == std::string::npos) continue;
235
236 auto spacePos = line.rfind(' ');
237 if (spacePos == std::string::npos) continue;
238 auto libFileName = line.substr(spacePos + 1);
239 auto it = infos->find(libFileName);
240 if (it == infos->end()) continue;
241 pids[libFileName].insert(pid);
242 }
243 }
244 for (auto& pair : *infos) {
245 pair.second.clientPids =
246 std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()};
247 }
248}
249
Steven Moreland337e6b62017-01-18 17:25:13 -0800250struct PassthroughServiceManager : IServiceManager {
Steven Moreland519306f2017-06-06 17:14:12 -0700251 static void openLibs(const std::string& fqName,
252 std::function<bool /* continue */(void* /* handle */,
253 const std::string& /* lib */, const std::string& /* sym */)> eachLib) {
Steven Morelandda8e6172017-04-06 17:24:22 -0700254 //fqName looks like android.hardware.foo@1.0::IFoo
Steven Moreland519306f2017-06-06 17:14:12 -0700255 size_t idx = fqName.find("::");
Steven Morelandda8e6172017-04-06 17:24:22 -0700256
257 if (idx == std::string::npos ||
Steven Moreland519306f2017-06-06 17:14:12 -0700258 idx + strlen("::") + 1 >= fqName.size()) {
Steven Moreland337e6b62017-01-18 17:25:13 -0800259 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
Steven Moreland519306f2017-06-06 17:14:12 -0700260 return;
Steven Moreland337e6b62017-01-18 17:25:13 -0800261 }
262
Steven Moreland519306f2017-06-06 17:14:12 -0700263 std::string packageAndVersion = fqName.substr(0, idx);
264 std::string ifaceName = fqName.substr(idx + strlen("::"));
Steven Morelandda8e6172017-04-06 17:24:22 -0700265
266 const std::string prefix = packageAndVersion + "-impl";
267 const std::string sym = "HIDL_FETCH_" + ifaceName;
Steven Moreland348802d2017-02-23 12:48:55 -0800268
Steven Moreland337e6b62017-01-18 17:25:13 -0800269 const int dlMode = RTLD_LAZY;
270 void *handle = nullptr;
271
Steven Morelanda29905c2017-03-01 10:42:35 -0800272 dlerror(); // clear
273
Steven Moreland337e6b62017-01-18 17:25:13 -0800274 for (const std::string &path : {
275 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM
276 }) {
Steven Moreland0091c092017-01-20 23:15:18 +0000277 std::vector<std::string> libs = search(path, prefix, ".so");
278
Steven Moreland0091c092017-01-20 23:15:18 +0000279 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800280 const std::string fullPath = path + lib;
281
282 handle = dlopen(fullPath.c_str(), dlMode);
283 if (handle == nullptr) {
284 const char* error = dlerror();
285 LOG(ERROR) << "Failed to dlopen " << lib << ": "
286 << (error == nullptr ? "unknown error" : error);
287 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000288 }
Steven Moreland348802d2017-02-23 12:48:55 -0800289
Steven Moreland519306f2017-06-06 17:14:12 -0700290 if (!eachLib(handle, lib, sym)) {
291 return;
Steven Moreland348802d2017-02-23 12:48:55 -0800292 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800293 }
294 }
Steven Moreland519306f2017-06-06 17:14:12 -0700295 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800296
Steven Moreland519306f2017-06-06 17:14:12 -0700297 Return<sp<IBase>> get(const hidl_string& fqName,
298 const hidl_string& name) override {
299 sp<IBase> ret = nullptr;
300
301 openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) {
302 IBase* (*generator)(const char* name);
303 *(void **)(&generator) = dlsym(handle, sym.c_str());
304 if(!generator) {
305 const char* error = dlerror();
306 LOG(ERROR) << "Passthrough lookup opened " << lib
307 << " but could not find symbol " << sym << ": "
308 << (error == nullptr ? "unknown error" : error);
309 dlclose(handle);
310 return true;
311 }
312
313 ret = (*generator)(name.c_str());
314
315 if (ret == nullptr) {
316 dlclose(handle);
317 return true; // this module doesn't provide this instance name
318 }
319
320 registerReference(fqName, name);
321 return false;
322 });
323
324 return ret;
Steven Moreland337e6b62017-01-18 17:25:13 -0800325 }
326
Martijn Coenen67a02492017-03-06 13:04:48 +0100327 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800328 const sp<IBase>& /* service */) override {
329 LOG(FATAL) << "Cannot register services with passthrough service manager.";
330 return false;
331 }
332
Steven Moreland330e3e22017-04-06 09:26:07 -0700333 Return<Transport> getTransport(const hidl_string& /* fqName */,
334 const hidl_string& /* name */) {
335 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
336 return Transport::EMPTY;
337 }
338
Yifan Hong705e5da2017-03-02 16:59:39 -0800339 Return<void> list(list_cb /* _hidl_cb */) override {
340 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800341 return Void();
342 }
343 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
344 listByInterface_cb /* _hidl_cb */) override {
345 // TODO: add this functionality
346 LOG(FATAL) << "Cannot list services with passthrough service manager.";
347 return Void();
348 }
349
350 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
351 const hidl_string& /* name */,
352 const sp<IServiceNotification>& /* callback */) override {
353 // This makes no sense.
354 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
355 return false;
356 }
357
Yifan Hong705e5da2017-03-02 16:59:39 -0800358 Return<void> debugDump(debugDump_cb _hidl_cb) override {
359 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700360 using std::literals::string_literals::operator""s;
Yifan Hong705e5da2017-03-02 16:59:39 -0800361 static std::vector<std::pair<Arch, std::vector<const char *>>> sAllPaths{
362 {Arch::IS_64BIT, {HAL_LIBRARY_PATH_ODM_64BIT,
363 HAL_LIBRARY_PATH_VENDOR_64BIT,
364 HAL_LIBRARY_PATH_SYSTEM_64BIT}},
365 {Arch::IS_32BIT, {HAL_LIBRARY_PATH_ODM_32BIT,
366 HAL_LIBRARY_PATH_VENDOR_32BIT,
367 HAL_LIBRARY_PATH_SYSTEM_32BIT}}
368 };
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700369 std::map<std::string, InstanceDebugInfo> map;
Yifan Hong705e5da2017-03-02 16:59:39 -0800370 for (const auto &pair : sAllPaths) {
371 Arch arch = pair.first;
372 for (const auto &path : pair.second) {
373 std::vector<std::string> libs = search(path, "", ".so");
374 for (const std::string &lib : libs) {
375 std::string matchedName;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700376 std::string implName;
377 if (matchPackageName(lib, &matchedName, &implName)) {
378 std::string instanceName{"* ("s + path + ")"s};
379 if (!implName.empty()) instanceName += " ("s + implName + ")"s;
380 map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName,
381 .instanceName = instanceName,
382 .clientPids = {},
383 .arch = arch});
Yifan Hong705e5da2017-03-02 16:59:39 -0800384 }
385 }
386 }
387 }
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700388 fetchPidsForPassthroughLibraries(&map);
389 hidl_vec<InstanceDebugInfo> vec;
390 vec.resize(map.size());
391 size_t idx = 0;
392 for (auto&& pair : map) {
393 vec[idx++] = std::move(pair.second);
394 }
Yifan Hong705e5da2017-03-02 16:59:39 -0800395 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800396 return Void();
397 }
398
Martijn Coenenbf13ad02017-04-27 09:41:13 -0700399 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override {
Yifan Hong7f49f592017-02-03 15:11:44 -0800400 // This makes no sense.
401 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
402 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800403 return Void();
404 }
405
Steven Moreland337e6b62017-01-18 17:25:13 -0800406};
407
408sp<IServiceManager> getPassthroughServiceManager() {
409 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
410 return manager;
411}
412
Steven Morelandcbefd352017-01-23 20:29:05 -0800413namespace details {
414
Steven Moreland519306f2017-06-06 17:14:12 -0700415void preloadPassthroughService(const std::string &descriptor) {
416 PassthroughServiceManager::openLibs(descriptor,
417 [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) {
418 // do nothing
419 return true; // open all libs
420 });
421}
422
Steven Morelandcbefd352017-01-23 20:29:05 -0800423struct Waiter : IServiceNotification {
424 Return<void> onRegistration(const hidl_string& /* fqName */,
425 const hidl_string& /* name */,
426 bool /* preexisting */) override {
427 std::unique_lock<std::mutex> lock(mMutex);
428 if (mRegistered) {
429 return Void();
430 }
431 mRegistered = true;
432 lock.unlock();
433
434 mCondition.notify_one();
435 return Void();
436 }
437
Steven Morelandf1e14f22017-03-28 09:33:06 -0700438 void wait(const std::string &interface, const std::string &instanceName) {
439 using std::literals::chrono_literals::operator""s;
440
Steven Morelandcbefd352017-01-23 20:29:05 -0800441 std::unique_lock<std::mutex> lock(mMutex);
Steven Morelandf1e14f22017-03-28 09:33:06 -0700442 while(true) {
443 mCondition.wait_for(lock, 1s, [this]{
444 return mRegistered;
445 });
446
447 if (mRegistered) {
448 break;
449 }
450
451 LOG(WARNING) << "Waited one second for "
452 << interface << "/" << instanceName
453 << ". Waiting another...";
454 }
Steven Morelandcbefd352017-01-23 20:29:05 -0800455 }
456
457private:
458 std::mutex mMutex;
459 std::condition_variable mCondition;
460 bool mRegistered = false;
461};
462
463void waitForHwService(
464 const std::string &interface, const std::string &instanceName) {
465 const sp<IServiceManager> manager = defaultServiceManager();
466
467 if (manager == nullptr) {
468 LOG(ERROR) << "Could not get default service manager.";
469 return;
470 }
471
472 sp<Waiter> waiter = new Waiter();
473 Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter);
474
475 if (!ret.isOk()) {
476 LOG(ERROR) << "Transport error, " << ret.description()
477 << ", during notification registration for "
478 << interface << "/" << instanceName << ".";
479 return;
480 }
481
482 if (!ret) {
483 LOG(ERROR) << "Could not register for notifications for "
484 << interface << "/" << instanceName << ".";
485 return;
486 }
487
Steven Morelandf1e14f22017-03-28 09:33:06 -0700488 waiter->wait(interface, instanceName);
Steven Morelandcbefd352017-01-23 20:29:05 -0800489}
490
491}; // namespace details
492
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700493}; // namespace hardware
494}; // namespace android