blob: 34a3c4753e5308a3db099c9e22462f3097625de3 [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>
22#include <unistd.h>
23
24#include <mutex>
25#include <regex>
26
Martijn Coenen12f04d92016-12-07 17:29:41 +010027#include <hidl/HidlBinderSupport.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070028#include <hidl/ServiceManagement.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070029#include <hidl/Status.h>
30
Steven Moreland337e6b62017-01-18 17:25:13 -080031#include <android-base/logging.h>
Steven Moreland7aca85a2017-03-22 09:21:26 -070032#include <android-base/properties.h>
Steven Moreland337e6b62017-01-18 17:25:13 -080033#include <hidl-util/FQName.h>
Steven Moreland0091c092017-01-20 23:15:18 +000034#include <hidl-util/StringHelper.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070035#include <hwbinder/IPCThreadState.h>
36#include <hwbinder/Parcel.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070037
38#include <android/hidl/manager/1.0/IServiceManager.h>
Yifan Hong4e925992017-01-09 17:47:17 -080039#include <android/hidl/manager/1.0/BpHwServiceManager.h>
40#include <android/hidl/manager/1.0/BnHwServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070041
Yifan Hong9a22d1d2017-01-25 14:19:26 -080042#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
43#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
44static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
45
Steven Moreland7aca85a2017-03-22 09:21:26 -070046using android::base::WaitForProperty;
47
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070048using android::hidl::manager::V1_0::IServiceManager;
Steven Moreland337e6b62017-01-18 17:25:13 -080049using android::hidl::manager::V1_0::IServiceNotification;
Yifan Hong4e925992017-01-09 17:47:17 -080050using android::hidl::manager::V1_0::BpHwServiceManager;
51using android::hidl::manager::V1_0::BnHwServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070052
53namespace android {
54namespace hardware {
55
Yifan Hong953e6b02017-03-16 14:52:40 -070056namespace details {
57extern Mutex gDefaultServiceManagerLock;
58extern sp<android::hidl::manager::V1_0::IServiceManager> gDefaultServiceManager;
59} // namespace details
60
Steven Moreland7aca85a2017-03-22 09:21:26 -070061static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready";
62
63void waitForHwServiceManager() {
64 while (!WaitForProperty(kHwServicemanagerReadyProperty, "true", std::chrono::milliseconds::max())) {}
65}
66
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070067sp<IServiceManager> defaultServiceManager() {
68
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070069 {
Yifan Hong953e6b02017-03-16 14:52:40 -070070 AutoMutex _l(details::gDefaultServiceManagerLock);
71 if (details::gDefaultServiceManager != NULL) {
72 return details::gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -070073 }
74 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
75 // HwBinder not available on this device or not accessible to
76 // this process.
77 return nullptr;
78 }
Steven Moreland7aca85a2017-03-22 09:21:26 -070079
80 waitForHwServiceManager();
81
Yifan Hong953e6b02017-03-16 14:52:40 -070082 while (details::gDefaultServiceManager == NULL) {
83 details::gDefaultServiceManager =
Yifan Hong8fb656b2017-03-16 14:30:40 -070084 fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>(
85 ProcessState::self()->getContextObject(NULL));
Yifan Hong953e6b02017-03-16 14:52:40 -070086 if (details::gDefaultServiceManager == NULL) {
Steven Moreland7aca85a2017-03-22 09:21:26 -070087 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070088 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -070089 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070090 }
91 }
92
Yifan Hong953e6b02017-03-16 14:52:40 -070093 return details::gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070094}
95
Steven Moreland0091c092017-01-20 23:15:18 +000096std::vector<std::string> search(const std::string &path,
97 const std::string &prefix,
98 const std::string &suffix) {
99 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
100 if (!dir) return {};
101
102 std::vector<std::string> results{};
103
104 dirent* dp;
105 while ((dp = readdir(dir.get())) != nullptr) {
106 std::string name = dp->d_name;
107
108 if (StringHelper::StartsWith(name, prefix) &&
109 StringHelper::EndsWith(name, suffix)) {
110 results.push_back(name);
111 }
112 }
113
114 return results;
115}
116
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800117bool matchPackageName(const std::string &lib, std::string *matchedName) {
118 std::smatch match;
119 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
120 *matchedName = match.str(1) + "::I*";
121 return true;
122 }
123 return false;
124}
125
Yifan Hong7f49f592017-02-03 15:11:44 -0800126static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
127 sp<IServiceManager> binderizedManager = defaultServiceManager();
128 if (binderizedManager == nullptr) {
129 LOG(WARNING) << "Could not registerReference for "
130 << interfaceName << "/" << instanceName
131 << ": null binderized manager.";
132 return;
133 }
134 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName, getpid());
135 if (!ret.isOk()) {
136 LOG(WARNING) << "Could not registerReference for "
137 << interfaceName << "/" << instanceName
138 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700139 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800140 }
Steven Morelande681aa52017-02-15 16:22:37 -0800141 LOG(VERBOSE) << "Successfully registerReference for "
142 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800143}
144
Steven Moreland337e6b62017-01-18 17:25:13 -0800145struct PassthroughServiceManager : IServiceManager {
146 Return<sp<IBase>> get(const hidl_string& fqName,
147 const hidl_string& name) override {
Steven Moreland348802d2017-02-23 12:48:55 -0800148 const FQName iface(fqName);
Steven Moreland337e6b62017-01-18 17:25:13 -0800149
150 if (!iface.isValid() ||
151 !iface.isFullyQualified() ||
152 iface.isIdentifier()) {
153 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
154 return nullptr;
155 }
156
Steven Moreland348802d2017-02-23 12:48:55 -0800157 const std::string prefix = iface.getPackageAndVersion().string() + "-impl";
158 const std::string sym = "HIDL_FETCH_" + iface.name();
159
Steven Moreland337e6b62017-01-18 17:25:13 -0800160 const int dlMode = RTLD_LAZY;
161 void *handle = nullptr;
162
Steven Moreland0091c092017-01-20 23:15:18 +0000163 // TODO: lookup in VINTF instead
164 // TODO(b/34135607): Remove HAL_LIBRARY_PATH_SYSTEM
165
Steven Morelanda29905c2017-03-01 10:42:35 -0800166 dlerror(); // clear
167
Steven Moreland337e6b62017-01-18 17:25:13 -0800168 for (const std::string &path : {
169 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM
170 }) {
Steven Moreland0091c092017-01-20 23:15:18 +0000171 std::vector<std::string> libs = search(path, prefix, ".so");
172
Steven Moreland0091c092017-01-20 23:15:18 +0000173 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800174 const std::string fullPath = path + lib;
175
176 handle = dlopen(fullPath.c_str(), dlMode);
177 if (handle == nullptr) {
178 const char* error = dlerror();
179 LOG(ERROR) << "Failed to dlopen " << lib << ": "
180 << (error == nullptr ? "unknown error" : error);
181 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000182 }
Steven Moreland348802d2017-02-23 12:48:55 -0800183
184 IBase* (*generator)(const char* name);
185 *(void **)(&generator) = dlsym(handle, sym.c_str());
186 if(!generator) {
187 const char* error = dlerror();
188 LOG(ERROR) << "Passthrough lookup opened " << lib
189 << " but could not find symbol " << sym << ": "
190 << (error == nullptr ? "unknown error" : error);
191 dlclose(handle);
192 continue;
193 }
194
195 IBase *interface = (*generator)(name);
196
197 if (interface == nullptr) {
198 dlclose(handle);
199 continue; // this module doesn't provide this instance name
200 }
201
202 registerReference(fqName, name);
203
204 return interface;
Steven Moreland337e6b62017-01-18 17:25:13 -0800205 }
206 }
207
Steven Moreland348802d2017-02-23 12:48:55 -0800208 return nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800209 }
210
Martijn Coenen67a02492017-03-06 13:04:48 +0100211 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800212 const sp<IBase>& /* service */) override {
213 LOG(FATAL) << "Cannot register services with passthrough service manager.";
214 return false;
215 }
216
Yifan Hong705e5da2017-03-02 16:59:39 -0800217 Return<void> list(list_cb /* _hidl_cb */) override {
218 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800219 return Void();
220 }
221 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
222 listByInterface_cb /* _hidl_cb */) override {
223 // TODO: add this functionality
224 LOG(FATAL) << "Cannot list services with passthrough service manager.";
225 return Void();
226 }
227
228 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
229 const hidl_string& /* name */,
230 const sp<IServiceNotification>& /* callback */) override {
231 // This makes no sense.
232 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
233 return false;
234 }
235
Yifan Hong705e5da2017-03-02 16:59:39 -0800236 Return<void> debugDump(debugDump_cb _hidl_cb) override {
237 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
238 static std::vector<std::pair<Arch, std::vector<const char *>>> sAllPaths{
239 {Arch::IS_64BIT, {HAL_LIBRARY_PATH_ODM_64BIT,
240 HAL_LIBRARY_PATH_VENDOR_64BIT,
241 HAL_LIBRARY_PATH_SYSTEM_64BIT}},
242 {Arch::IS_32BIT, {HAL_LIBRARY_PATH_ODM_32BIT,
243 HAL_LIBRARY_PATH_VENDOR_32BIT,
244 HAL_LIBRARY_PATH_SYSTEM_32BIT}}
245 };
246 std::vector<InstanceDebugInfo> vec;
247 for (const auto &pair : sAllPaths) {
248 Arch arch = pair.first;
249 for (const auto &path : pair.second) {
250 std::vector<std::string> libs = search(path, "", ".so");
251 for (const std::string &lib : libs) {
252 std::string matchedName;
253 if (matchPackageName(lib, &matchedName)) {
254 vec.push_back({
255 .interfaceName = matchedName,
256 .instanceName = "*",
257 .clientPids = {},
258 .arch = arch
259 });
260 }
261 }
262 }
263 }
264 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800265 return Void();
266 }
267
268 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &, int32_t) override {
269 // This makes no sense.
270 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
271 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800272 return Void();
273 }
274
Steven Moreland337e6b62017-01-18 17:25:13 -0800275};
276
277sp<IServiceManager> getPassthroughServiceManager() {
278 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
279 return manager;
280}
281
Steven Morelandcbefd352017-01-23 20:29:05 -0800282namespace details {
283
284struct Waiter : IServiceNotification {
285 Return<void> onRegistration(const hidl_string& /* fqName */,
286 const hidl_string& /* name */,
287 bool /* preexisting */) override {
288 std::unique_lock<std::mutex> lock(mMutex);
289 if (mRegistered) {
290 return Void();
291 }
292 mRegistered = true;
293 lock.unlock();
294
295 mCondition.notify_one();
296 return Void();
297 }
298
299 void wait() {
300 std::unique_lock<std::mutex> lock(mMutex);
301 mCondition.wait(lock, [this]{
302 return mRegistered;
303 });
304 }
305
306private:
307 std::mutex mMutex;
308 std::condition_variable mCondition;
309 bool mRegistered = false;
310};
311
312void waitForHwService(
313 const std::string &interface, const std::string &instanceName) {
314 const sp<IServiceManager> manager = defaultServiceManager();
315
316 if (manager == nullptr) {
317 LOG(ERROR) << "Could not get default service manager.";
318 return;
319 }
320
321 sp<Waiter> waiter = new Waiter();
322 Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter);
323
324 if (!ret.isOk()) {
325 LOG(ERROR) << "Transport error, " << ret.description()
326 << ", during notification registration for "
327 << interface << "/" << instanceName << ".";
328 return;
329 }
330
331 if (!ret) {
332 LOG(ERROR) << "Could not register for notifications for "
333 << interface << "/" << instanceName << ".";
334 return;
335 }
336
337 waiter->wait();
338}
339
340}; // namespace details
341
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700342}; // namespace hardware
343}; // namespace android