blob: 15abb923deef2c0c659df6e46f6fb2634f0c02f3 [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>
29#include <hidl/Static.h>
30#include <hidl/Status.h>
31
Steven Moreland337e6b62017-01-18 17:25:13 -080032#include <android-base/logging.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 Moreland5d5ef7f2016-10-20 19:19:55 -070046using android::hidl::manager::V1_0::IServiceManager;
Steven Moreland337e6b62017-01-18 17:25:13 -080047using android::hidl::manager::V1_0::IServiceNotification;
Yifan Hong4e925992017-01-09 17:47:17 -080048using android::hidl::manager::V1_0::BpHwServiceManager;
49using android::hidl::manager::V1_0::BnHwServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070050
51namespace android {
52namespace hardware {
53
54sp<IServiceManager> defaultServiceManager() {
55
56 if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
57 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
58 // HwBinder not available on this device or not accessible to
59 // this process.
60 return nullptr;
61 }
62 {
63 AutoMutex _l(gDefaultServiceManagerLock);
64 while (gDefaultServiceManager == NULL) {
Yifan Hong4e925992017-01-09 17:47:17 -080065 gDefaultServiceManager = fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>(
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070066 ProcessState::self()->getContextObject(NULL));
67 if (gDefaultServiceManager == NULL)
68 sleep(1);
69 }
70 }
71
72 return gDefaultServiceManager;
73}
74
Steven Moreland0091c092017-01-20 23:15:18 +000075std::vector<std::string> search(const std::string &path,
76 const std::string &prefix,
77 const std::string &suffix) {
78 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
79 if (!dir) return {};
80
81 std::vector<std::string> results{};
82
83 dirent* dp;
84 while ((dp = readdir(dir.get())) != nullptr) {
85 std::string name = dp->d_name;
86
87 if (StringHelper::StartsWith(name, prefix) &&
88 StringHelper::EndsWith(name, suffix)) {
89 results.push_back(name);
90 }
91 }
92
93 return results;
94}
95
Yifan Hong9a22d1d2017-01-25 14:19:26 -080096bool matchPackageName(const std::string &lib, std::string *matchedName) {
97 std::smatch match;
98 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
99 *matchedName = match.str(1) + "::I*";
100 return true;
101 }
102 return false;
103}
104
Yifan Hong7f49f592017-02-03 15:11:44 -0800105static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
106 sp<IServiceManager> binderizedManager = defaultServiceManager();
107 if (binderizedManager == nullptr) {
108 LOG(WARNING) << "Could not registerReference for "
109 << interfaceName << "/" << instanceName
110 << ": null binderized manager.";
111 return;
112 }
113 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName, getpid());
114 if (!ret.isOk()) {
115 LOG(WARNING) << "Could not registerReference for "
116 << interfaceName << "/" << instanceName
117 << ": " << ret.description();
118 }
Steven Morelande681aa52017-02-15 16:22:37 -0800119 LOG(VERBOSE) << "Successfully registerReference for "
120 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800121}
122
Steven Moreland337e6b62017-01-18 17:25:13 -0800123struct PassthroughServiceManager : IServiceManager {
124 Return<sp<IBase>> get(const hidl_string& fqName,
125 const hidl_string& name) override {
Steven Moreland348802d2017-02-23 12:48:55 -0800126 const FQName iface(fqName);
Steven Moreland337e6b62017-01-18 17:25:13 -0800127
128 if (!iface.isValid() ||
129 !iface.isFullyQualified() ||
130 iface.isIdentifier()) {
131 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
132 return nullptr;
133 }
134
Steven Moreland348802d2017-02-23 12:48:55 -0800135 const std::string prefix = iface.getPackageAndVersion().string() + "-impl";
136 const std::string sym = "HIDL_FETCH_" + iface.name();
137
Steven Moreland337e6b62017-01-18 17:25:13 -0800138 const int dlMode = RTLD_LAZY;
139 void *handle = nullptr;
140
Steven Moreland0091c092017-01-20 23:15:18 +0000141 // TODO: lookup in VINTF instead
142 // TODO(b/34135607): Remove HAL_LIBRARY_PATH_SYSTEM
143
Steven Morelanda29905c2017-03-01 10:42:35 -0800144 dlerror(); // clear
145
Steven Moreland337e6b62017-01-18 17:25:13 -0800146 for (const std::string &path : {
147 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM
148 }) {
Steven Moreland0091c092017-01-20 23:15:18 +0000149 std::vector<std::string> libs = search(path, prefix, ".so");
150
Steven Moreland0091c092017-01-20 23:15:18 +0000151 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800152 const std::string fullPath = path + lib;
153
154 handle = dlopen(fullPath.c_str(), dlMode);
155 if (handle == nullptr) {
156 const char* error = dlerror();
157 LOG(ERROR) << "Failed to dlopen " << lib << ": "
158 << (error == nullptr ? "unknown error" : error);
159 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000160 }
Steven Moreland348802d2017-02-23 12:48:55 -0800161
162 IBase* (*generator)(const char* name);
163 *(void **)(&generator) = dlsym(handle, sym.c_str());
164 if(!generator) {
165 const char* error = dlerror();
166 LOG(ERROR) << "Passthrough lookup opened " << lib
167 << " but could not find symbol " << sym << ": "
168 << (error == nullptr ? "unknown error" : error);
169 dlclose(handle);
170 continue;
171 }
172
173 IBase *interface = (*generator)(name);
174
175 if (interface == nullptr) {
176 dlclose(handle);
177 continue; // this module doesn't provide this instance name
178 }
179
180 registerReference(fqName, name);
181
182 return interface;
Steven Moreland337e6b62017-01-18 17:25:13 -0800183 }
184 }
185
Steven Moreland348802d2017-02-23 12:48:55 -0800186 return nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800187 }
188
Martijn Coenen67a02492017-03-06 13:04:48 +0100189 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800190 const sp<IBase>& /* service */) override {
191 LOG(FATAL) << "Cannot register services with passthrough service manager.";
192 return false;
193 }
194
Yifan Hong705e5da2017-03-02 16:59:39 -0800195 Return<void> list(list_cb /* _hidl_cb */) override {
196 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800197 return Void();
198 }
199 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
200 listByInterface_cb /* _hidl_cb */) override {
201 // TODO: add this functionality
202 LOG(FATAL) << "Cannot list services with passthrough service manager.";
203 return Void();
204 }
205
206 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
207 const hidl_string& /* name */,
208 const sp<IServiceNotification>& /* callback */) override {
209 // This makes no sense.
210 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
211 return false;
212 }
213
Yifan Hong705e5da2017-03-02 16:59:39 -0800214 Return<void> debugDump(debugDump_cb _hidl_cb) override {
215 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
216 static std::vector<std::pair<Arch, std::vector<const char *>>> sAllPaths{
217 {Arch::IS_64BIT, {HAL_LIBRARY_PATH_ODM_64BIT,
218 HAL_LIBRARY_PATH_VENDOR_64BIT,
219 HAL_LIBRARY_PATH_SYSTEM_64BIT}},
220 {Arch::IS_32BIT, {HAL_LIBRARY_PATH_ODM_32BIT,
221 HAL_LIBRARY_PATH_VENDOR_32BIT,
222 HAL_LIBRARY_PATH_SYSTEM_32BIT}}
223 };
224 std::vector<InstanceDebugInfo> vec;
225 for (const auto &pair : sAllPaths) {
226 Arch arch = pair.first;
227 for (const auto &path : pair.second) {
228 std::vector<std::string> libs = search(path, "", ".so");
229 for (const std::string &lib : libs) {
230 std::string matchedName;
231 if (matchPackageName(lib, &matchedName)) {
232 vec.push_back({
233 .interfaceName = matchedName,
234 .instanceName = "*",
235 .clientPids = {},
236 .arch = arch
237 });
238 }
239 }
240 }
241 }
242 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800243 return Void();
244 }
245
246 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &, int32_t) override {
247 // This makes no sense.
248 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
249 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800250 return Void();
251 }
252
Steven Moreland337e6b62017-01-18 17:25:13 -0800253};
254
255sp<IServiceManager> getPassthroughServiceManager() {
256 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
257 return manager;
258}
259
Steven Morelandcbefd352017-01-23 20:29:05 -0800260namespace details {
261
262struct Waiter : IServiceNotification {
263 Return<void> onRegistration(const hidl_string& /* fqName */,
264 const hidl_string& /* name */,
265 bool /* preexisting */) override {
266 std::unique_lock<std::mutex> lock(mMutex);
267 if (mRegistered) {
268 return Void();
269 }
270 mRegistered = true;
271 lock.unlock();
272
273 mCondition.notify_one();
274 return Void();
275 }
276
277 void wait() {
278 std::unique_lock<std::mutex> lock(mMutex);
279 mCondition.wait(lock, [this]{
280 return mRegistered;
281 });
282 }
283
284private:
285 std::mutex mMutex;
286 std::condition_variable mCondition;
287 bool mRegistered = false;
288};
289
290void waitForHwService(
291 const std::string &interface, const std::string &instanceName) {
292 const sp<IServiceManager> manager = defaultServiceManager();
293
294 if (manager == nullptr) {
295 LOG(ERROR) << "Could not get default service manager.";
296 return;
297 }
298
299 sp<Waiter> waiter = new Waiter();
300 Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter);
301
302 if (!ret.isOk()) {
303 LOG(ERROR) << "Transport error, " << ret.description()
304 << ", during notification registration for "
305 << interface << "/" << instanceName << ".";
306 return;
307 }
308
309 if (!ret) {
310 LOG(ERROR) << "Could not register for notifications for "
311 << interface << "/" << instanceName << ".";
312 return;
313 }
314
315 waiter->wait();
316}
317
318}; // namespace details
319
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700320}; // namespace hardware
321}; // namespace android