blob: cc31d9e1394004d6290bab3dd716ab462d760cd6 [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 Moreland337e6b62017-01-18 17:25:13 -080032#include <hidl-util/FQName.h>
Steven Moreland0091c092017-01-20 23:15:18 +000033#include <hidl-util/StringHelper.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070034#include <hwbinder/IPCThreadState.h>
35#include <hwbinder/Parcel.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070036
37#include <android/hidl/manager/1.0/IServiceManager.h>
Yifan Hong4e925992017-01-09 17:47:17 -080038#include <android/hidl/manager/1.0/BpHwServiceManager.h>
39#include <android/hidl/manager/1.0/BnHwServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070040
Yifan Hong9a22d1d2017-01-25 14:19:26 -080041#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
42#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
43static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
44
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070045using android::hidl::manager::V1_0::IServiceManager;
Steven Moreland337e6b62017-01-18 17:25:13 -080046using android::hidl::manager::V1_0::IServiceNotification;
Yifan Hong4e925992017-01-09 17:47:17 -080047using android::hidl::manager::V1_0::BpHwServiceManager;
48using android::hidl::manager::V1_0::BnHwServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070049
50namespace android {
51namespace hardware {
52
Yifan Hong953e6b02017-03-16 14:52:40 -070053namespace details {
54extern Mutex gDefaultServiceManagerLock;
55extern sp<android::hidl::manager::V1_0::IServiceManager> gDefaultServiceManager;
56} // namespace details
57
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070058sp<IServiceManager> defaultServiceManager() {
59
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070060 {
Yifan Hong953e6b02017-03-16 14:52:40 -070061 AutoMutex _l(details::gDefaultServiceManagerLock);
62 if (details::gDefaultServiceManager != NULL) {
63 return details::gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -070064 }
65 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
66 // HwBinder not available on this device or not accessible to
67 // this process.
68 return nullptr;
69 }
Yifan Hong953e6b02017-03-16 14:52:40 -070070 while (details::gDefaultServiceManager == NULL) {
71 details::gDefaultServiceManager =
Yifan Hong8fb656b2017-03-16 14:30:40 -070072 fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>(
73 ProcessState::self()->getContextObject(NULL));
Yifan Hong953e6b02017-03-16 14:52:40 -070074 if (details::gDefaultServiceManager == NULL) {
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070075 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -070076 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070077 }
78 }
79
Yifan Hong953e6b02017-03-16 14:52:40 -070080 return details::gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070081}
82
Steven Moreland0091c092017-01-20 23:15:18 +000083std::vector<std::string> search(const std::string &path,
84 const std::string &prefix,
85 const std::string &suffix) {
86 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
87 if (!dir) return {};
88
89 std::vector<std::string> results{};
90
91 dirent* dp;
92 while ((dp = readdir(dir.get())) != nullptr) {
93 std::string name = dp->d_name;
94
95 if (StringHelper::StartsWith(name, prefix) &&
96 StringHelper::EndsWith(name, suffix)) {
97 results.push_back(name);
98 }
99 }
100
101 return results;
102}
103
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800104bool matchPackageName(const std::string &lib, std::string *matchedName) {
105 std::smatch match;
106 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
107 *matchedName = match.str(1) + "::I*";
108 return true;
109 }
110 return false;
111}
112
Yifan Hong7f49f592017-02-03 15:11:44 -0800113static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
114 sp<IServiceManager> binderizedManager = defaultServiceManager();
115 if (binderizedManager == nullptr) {
116 LOG(WARNING) << "Could not registerReference for "
117 << interfaceName << "/" << instanceName
118 << ": null binderized manager.";
119 return;
120 }
121 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName, getpid());
122 if (!ret.isOk()) {
123 LOG(WARNING) << "Could not registerReference for "
124 << interfaceName << "/" << instanceName
125 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700126 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800127 }
Steven Morelande681aa52017-02-15 16:22:37 -0800128 LOG(VERBOSE) << "Successfully registerReference for "
129 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800130}
131
Steven Moreland337e6b62017-01-18 17:25:13 -0800132struct PassthroughServiceManager : IServiceManager {
133 Return<sp<IBase>> get(const hidl_string& fqName,
134 const hidl_string& name) override {
Steven Moreland348802d2017-02-23 12:48:55 -0800135 const FQName iface(fqName);
Steven Moreland337e6b62017-01-18 17:25:13 -0800136
137 if (!iface.isValid() ||
138 !iface.isFullyQualified() ||
139 iface.isIdentifier()) {
140 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
141 return nullptr;
142 }
143
Steven Moreland348802d2017-02-23 12:48:55 -0800144 const std::string prefix = iface.getPackageAndVersion().string() + "-impl";
145 const std::string sym = "HIDL_FETCH_" + iface.name();
146
Steven Moreland337e6b62017-01-18 17:25:13 -0800147 const int dlMode = RTLD_LAZY;
148 void *handle = nullptr;
149
Steven Moreland0091c092017-01-20 23:15:18 +0000150 // TODO: lookup in VINTF instead
151 // TODO(b/34135607): Remove HAL_LIBRARY_PATH_SYSTEM
152
Steven Morelanda29905c2017-03-01 10:42:35 -0800153 dlerror(); // clear
154
Steven Moreland337e6b62017-01-18 17:25:13 -0800155 for (const std::string &path : {
156 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM
157 }) {
Steven Moreland0091c092017-01-20 23:15:18 +0000158 std::vector<std::string> libs = search(path, prefix, ".so");
159
Steven Moreland0091c092017-01-20 23:15:18 +0000160 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800161 const std::string fullPath = path + lib;
162
163 handle = dlopen(fullPath.c_str(), dlMode);
164 if (handle == nullptr) {
165 const char* error = dlerror();
166 LOG(ERROR) << "Failed to dlopen " << lib << ": "
167 << (error == nullptr ? "unknown error" : error);
168 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000169 }
Steven Moreland348802d2017-02-23 12:48:55 -0800170
171 IBase* (*generator)(const char* name);
172 *(void **)(&generator) = dlsym(handle, sym.c_str());
173 if(!generator) {
174 const char* error = dlerror();
175 LOG(ERROR) << "Passthrough lookup opened " << lib
176 << " but could not find symbol " << sym << ": "
177 << (error == nullptr ? "unknown error" : error);
178 dlclose(handle);
179 continue;
180 }
181
182 IBase *interface = (*generator)(name);
183
184 if (interface == nullptr) {
185 dlclose(handle);
186 continue; // this module doesn't provide this instance name
187 }
188
189 registerReference(fqName, name);
190
191 return interface;
Steven Moreland337e6b62017-01-18 17:25:13 -0800192 }
193 }
194
Steven Moreland348802d2017-02-23 12:48:55 -0800195 return nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800196 }
197
Martijn Coenen67a02492017-03-06 13:04:48 +0100198 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800199 const sp<IBase>& /* service */) override {
200 LOG(FATAL) << "Cannot register services with passthrough service manager.";
201 return false;
202 }
203
Yifan Hong705e5da2017-03-02 16:59:39 -0800204 Return<void> list(list_cb /* _hidl_cb */) override {
205 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800206 return Void();
207 }
208 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
209 listByInterface_cb /* _hidl_cb */) override {
210 // TODO: add this functionality
211 LOG(FATAL) << "Cannot list services with passthrough service manager.";
212 return Void();
213 }
214
215 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
216 const hidl_string& /* name */,
217 const sp<IServiceNotification>& /* callback */) override {
218 // This makes no sense.
219 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
220 return false;
221 }
222
Yifan Hong705e5da2017-03-02 16:59:39 -0800223 Return<void> debugDump(debugDump_cb _hidl_cb) override {
224 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
225 static std::vector<std::pair<Arch, std::vector<const char *>>> sAllPaths{
226 {Arch::IS_64BIT, {HAL_LIBRARY_PATH_ODM_64BIT,
227 HAL_LIBRARY_PATH_VENDOR_64BIT,
228 HAL_LIBRARY_PATH_SYSTEM_64BIT}},
229 {Arch::IS_32BIT, {HAL_LIBRARY_PATH_ODM_32BIT,
230 HAL_LIBRARY_PATH_VENDOR_32BIT,
231 HAL_LIBRARY_PATH_SYSTEM_32BIT}}
232 };
233 std::vector<InstanceDebugInfo> vec;
234 for (const auto &pair : sAllPaths) {
235 Arch arch = pair.first;
236 for (const auto &path : pair.second) {
237 std::vector<std::string> libs = search(path, "", ".so");
238 for (const std::string &lib : libs) {
239 std::string matchedName;
240 if (matchPackageName(lib, &matchedName)) {
241 vec.push_back({
242 .interfaceName = matchedName,
243 .instanceName = "*",
244 .clientPids = {},
245 .arch = arch
246 });
247 }
248 }
249 }
250 }
251 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800252 return Void();
253 }
254
255 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &, int32_t) override {
256 // This makes no sense.
257 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
258 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800259 return Void();
260 }
261
Steven Moreland337e6b62017-01-18 17:25:13 -0800262};
263
264sp<IServiceManager> getPassthroughServiceManager() {
265 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
266 return manager;
267}
268
Steven Morelandcbefd352017-01-23 20:29:05 -0800269namespace details {
270
271struct Waiter : IServiceNotification {
272 Return<void> onRegistration(const hidl_string& /* fqName */,
273 const hidl_string& /* name */,
274 bool /* preexisting */) override {
275 std::unique_lock<std::mutex> lock(mMutex);
276 if (mRegistered) {
277 return Void();
278 }
279 mRegistered = true;
280 lock.unlock();
281
282 mCondition.notify_one();
283 return Void();
284 }
285
286 void wait() {
287 std::unique_lock<std::mutex> lock(mMutex);
288 mCondition.wait(lock, [this]{
289 return mRegistered;
290 });
291 }
292
293private:
294 std::mutex mMutex;
295 std::condition_variable mCondition;
296 bool mRegistered = false;
297};
298
299void waitForHwService(
300 const std::string &interface, const std::string &instanceName) {
301 const sp<IServiceManager> manager = defaultServiceManager();
302
303 if (manager == nullptr) {
304 LOG(ERROR) << "Could not get default service manager.";
305 return;
306 }
307
308 sp<Waiter> waiter = new Waiter();
309 Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter);
310
311 if (!ret.isOk()) {
312 LOG(ERROR) << "Transport error, " << ret.description()
313 << ", during notification registration for "
314 << interface << "/" << instanceName << ".";
315 return;
316 }
317
318 if (!ret) {
319 LOG(ERROR) << "Could not register for notifications for "
320 << interface << "/" << instanceName << ".";
321 return;
322 }
323
324 waiter->wait();
325}
326
327}; // namespace details
328
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700329}; // namespace hardware
330}; // namespace android