blob: feef01fcab7f6ca920808fbe7141b878a085b3d2 [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
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070056 {
57 AutoMutex _l(gDefaultServiceManagerLock);
Yifan Hong8fb656b2017-03-16 14:30:40 -070058 if (gDefaultServiceManager != NULL) {
59 return gDefaultServiceManager;
60 }
61 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
62 // HwBinder not available on this device or not accessible to
63 // this process.
64 return nullptr;
65 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070066 while (gDefaultServiceManager == NULL) {
Yifan Hong8fb656b2017-03-16 14:30:40 -070067 gDefaultServiceManager =
68 fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>(
69 ProcessState::self()->getContextObject(NULL));
70 if (gDefaultServiceManager == NULL) {
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070071 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -070072 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070073 }
74 }
75
76 return gDefaultServiceManager;
77}
78
Steven Moreland0091c092017-01-20 23:15:18 +000079std::vector<std::string> search(const std::string &path,
80 const std::string &prefix,
81 const std::string &suffix) {
82 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
83 if (!dir) return {};
84
85 std::vector<std::string> results{};
86
87 dirent* dp;
88 while ((dp = readdir(dir.get())) != nullptr) {
89 std::string name = dp->d_name;
90
91 if (StringHelper::StartsWith(name, prefix) &&
92 StringHelper::EndsWith(name, suffix)) {
93 results.push_back(name);
94 }
95 }
96
97 return results;
98}
99
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800100bool matchPackageName(const std::string &lib, std::string *matchedName) {
101 std::smatch match;
102 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
103 *matchedName = match.str(1) + "::I*";
104 return true;
105 }
106 return false;
107}
108
Yifan Hong7f49f592017-02-03 15:11:44 -0800109static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
110 sp<IServiceManager> binderizedManager = defaultServiceManager();
111 if (binderizedManager == nullptr) {
112 LOG(WARNING) << "Could not registerReference for "
113 << interfaceName << "/" << instanceName
114 << ": null binderized manager.";
115 return;
116 }
117 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName, getpid());
118 if (!ret.isOk()) {
119 LOG(WARNING) << "Could not registerReference for "
120 << interfaceName << "/" << instanceName
121 << ": " << ret.description();
122 }
Steven Morelande681aa52017-02-15 16:22:37 -0800123 LOG(VERBOSE) << "Successfully registerReference for "
124 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800125}
126
Steven Moreland337e6b62017-01-18 17:25:13 -0800127struct PassthroughServiceManager : IServiceManager {
128 Return<sp<IBase>> get(const hidl_string& fqName,
129 const hidl_string& name) override {
Steven Moreland348802d2017-02-23 12:48:55 -0800130 const FQName iface(fqName);
Steven Moreland337e6b62017-01-18 17:25:13 -0800131
132 if (!iface.isValid() ||
133 !iface.isFullyQualified() ||
134 iface.isIdentifier()) {
135 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
136 return nullptr;
137 }
138
Steven Moreland348802d2017-02-23 12:48:55 -0800139 const std::string prefix = iface.getPackageAndVersion().string() + "-impl";
140 const std::string sym = "HIDL_FETCH_" + iface.name();
141
Steven Moreland337e6b62017-01-18 17:25:13 -0800142 const int dlMode = RTLD_LAZY;
143 void *handle = nullptr;
144
Steven Moreland0091c092017-01-20 23:15:18 +0000145 // TODO: lookup in VINTF instead
146 // TODO(b/34135607): Remove HAL_LIBRARY_PATH_SYSTEM
147
Steven Morelanda29905c2017-03-01 10:42:35 -0800148 dlerror(); // clear
149
Steven Moreland337e6b62017-01-18 17:25:13 -0800150 for (const std::string &path : {
151 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM
152 }) {
Steven Moreland0091c092017-01-20 23:15:18 +0000153 std::vector<std::string> libs = search(path, prefix, ".so");
154
Steven Moreland0091c092017-01-20 23:15:18 +0000155 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800156 const std::string fullPath = path + lib;
157
158 handle = dlopen(fullPath.c_str(), dlMode);
159 if (handle == nullptr) {
160 const char* error = dlerror();
161 LOG(ERROR) << "Failed to dlopen " << lib << ": "
162 << (error == nullptr ? "unknown error" : error);
163 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000164 }
Steven Moreland348802d2017-02-23 12:48:55 -0800165
166 IBase* (*generator)(const char* name);
167 *(void **)(&generator) = dlsym(handle, sym.c_str());
168 if(!generator) {
169 const char* error = dlerror();
170 LOG(ERROR) << "Passthrough lookup opened " << lib
171 << " but could not find symbol " << sym << ": "
172 << (error == nullptr ? "unknown error" : error);
173 dlclose(handle);
174 continue;
175 }
176
177 IBase *interface = (*generator)(name);
178
179 if (interface == nullptr) {
180 dlclose(handle);
181 continue; // this module doesn't provide this instance name
182 }
183
184 registerReference(fqName, name);
185
186 return interface;
Steven Moreland337e6b62017-01-18 17:25:13 -0800187 }
188 }
189
Steven Moreland348802d2017-02-23 12:48:55 -0800190 return nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800191 }
192
Martijn Coenen67a02492017-03-06 13:04:48 +0100193 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800194 const sp<IBase>& /* service */) override {
195 LOG(FATAL) << "Cannot register services with passthrough service manager.";
196 return false;
197 }
198
Yifan Hong705e5da2017-03-02 16:59:39 -0800199 Return<void> list(list_cb /* _hidl_cb */) override {
200 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800201 return Void();
202 }
203 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
204 listByInterface_cb /* _hidl_cb */) override {
205 // TODO: add this functionality
206 LOG(FATAL) << "Cannot list services with passthrough service manager.";
207 return Void();
208 }
209
210 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
211 const hidl_string& /* name */,
212 const sp<IServiceNotification>& /* callback */) override {
213 // This makes no sense.
214 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
215 return false;
216 }
217
Yifan Hong705e5da2017-03-02 16:59:39 -0800218 Return<void> debugDump(debugDump_cb _hidl_cb) override {
219 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
220 static std::vector<std::pair<Arch, std::vector<const char *>>> sAllPaths{
221 {Arch::IS_64BIT, {HAL_LIBRARY_PATH_ODM_64BIT,
222 HAL_LIBRARY_PATH_VENDOR_64BIT,
223 HAL_LIBRARY_PATH_SYSTEM_64BIT}},
224 {Arch::IS_32BIT, {HAL_LIBRARY_PATH_ODM_32BIT,
225 HAL_LIBRARY_PATH_VENDOR_32BIT,
226 HAL_LIBRARY_PATH_SYSTEM_32BIT}}
227 };
228 std::vector<InstanceDebugInfo> vec;
229 for (const auto &pair : sAllPaths) {
230 Arch arch = pair.first;
231 for (const auto &path : pair.second) {
232 std::vector<std::string> libs = search(path, "", ".so");
233 for (const std::string &lib : libs) {
234 std::string matchedName;
235 if (matchPackageName(lib, &matchedName)) {
236 vec.push_back({
237 .interfaceName = matchedName,
238 .instanceName = "*",
239 .clientPids = {},
240 .arch = arch
241 });
242 }
243 }
244 }
245 }
246 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800247 return Void();
248 }
249
250 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &, int32_t) override {
251 // This makes no sense.
252 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
253 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800254 return Void();
255 }
256
Steven Moreland337e6b62017-01-18 17:25:13 -0800257};
258
259sp<IServiceManager> getPassthroughServiceManager() {
260 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
261 return manager;
262}
263
Steven Morelandcbefd352017-01-23 20:29:05 -0800264namespace details {
265
266struct Waiter : IServiceNotification {
267 Return<void> onRegistration(const hidl_string& /* fqName */,
268 const hidl_string& /* name */,
269 bool /* preexisting */) override {
270 std::unique_lock<std::mutex> lock(mMutex);
271 if (mRegistered) {
272 return Void();
273 }
274 mRegistered = true;
275 lock.unlock();
276
277 mCondition.notify_one();
278 return Void();
279 }
280
281 void wait() {
282 std::unique_lock<std::mutex> lock(mMutex);
283 mCondition.wait(lock, [this]{
284 return mRegistered;
285 });
286 }
287
288private:
289 std::mutex mMutex;
290 std::condition_variable mCondition;
291 bool mRegistered = false;
292};
293
294void waitForHwService(
295 const std::string &interface, const std::string &instanceName) {
296 const sp<IServiceManager> manager = defaultServiceManager();
297
298 if (manager == nullptr) {
299 LOG(ERROR) << "Could not get default service manager.";
300 return;
301 }
302
303 sp<Waiter> waiter = new Waiter();
304 Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter);
305
306 if (!ret.isOk()) {
307 LOG(ERROR) << "Transport error, " << ret.description()
308 << ", during notification registration for "
309 << interface << "/" << instanceName << ".";
310 return;
311 }
312
313 if (!ret) {
314 LOG(ERROR) << "Could not register for notifications for "
315 << interface << "/" << instanceName << ".";
316 return;
317 }
318
319 waiter->wait();
320}
321
322}; // namespace details
323
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700324}; // namespace hardware
325}; // namespace android