blob: fec29d1302b2881dbeb73e74d77d9aa5128ae594 [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 Moreland337e6b62017-01-18 17:25:13 -0800144 for (const std::string &path : {
145 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM
146 }) {
Steven Moreland0091c092017-01-20 23:15:18 +0000147 std::vector<std::string> libs = search(path, prefix, ".so");
148
Steven Moreland0091c092017-01-20 23:15:18 +0000149 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800150 const std::string fullPath = path + lib;
151
152 handle = dlopen(fullPath.c_str(), dlMode);
153 if (handle == nullptr) {
154 const char* error = dlerror();
155 LOG(ERROR) << "Failed to dlopen " << lib << ": "
156 << (error == nullptr ? "unknown error" : error);
157 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000158 }
Steven Moreland348802d2017-02-23 12:48:55 -0800159
160 IBase* (*generator)(const char* name);
161 *(void **)(&generator) = dlsym(handle, sym.c_str());
162 if(!generator) {
163 const char* error = dlerror();
164 LOG(ERROR) << "Passthrough lookup opened " << lib
165 << " but could not find symbol " << sym << ": "
166 << (error == nullptr ? "unknown error" : error);
167 dlclose(handle);
168 continue;
169 }
170
171 IBase *interface = (*generator)(name);
172
173 if (interface == nullptr) {
174 dlclose(handle);
175 continue; // this module doesn't provide this instance name
176 }
177
178 registerReference(fqName, name);
179
180 return interface;
Steven Moreland337e6b62017-01-18 17:25:13 -0800181 }
182 }
183
Steven Moreland348802d2017-02-23 12:48:55 -0800184 return nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800185 }
186
187 Return<bool> add(const hidl_vec<hidl_string>& /* interfaceChain */,
188 const hidl_string& /* name */,
189 const sp<IBase>& /* service */) override {
190 LOG(FATAL) << "Cannot register services with passthrough service manager.";
191 return false;
192 }
193
Yifan Hong7f49f592017-02-03 15:11:44 -0800194 Return<void> list(list_cb _hidl_cb) override {
195 std::vector<hidl_string> vec;
196 for (const std::string &path : {
197 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM
198 }) {
199 std::vector<std::string> libs = search(path, "", ".so");
200 for (const std::string &lib : libs) {
201 std::string matchedName;
202 if (matchPackageName(lib, &matchedName)) {
203 vec.push_back(matchedName + "/*");
204 }
205 }
206 }
207 _hidl_cb(vec);
Steven Moreland337e6b62017-01-18 17:25:13 -0800208 return Void();
209 }
210 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
211 listByInterface_cb /* _hidl_cb */) override {
212 // TODO: add this functionality
213 LOG(FATAL) << "Cannot list services with passthrough service manager.";
214 return Void();
215 }
216
217 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
218 const hidl_string& /* name */,
219 const sp<IServiceNotification>& /* callback */) override {
220 // This makes no sense.
221 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
222 return false;
223 }
224
Yifan Hong7f49f592017-02-03 15:11:44 -0800225 Return<void> debugDump(debugDump_cb) override {
226 // This makes no sense.
227 LOG(FATAL) << "Cannot call debugDump on passthrough service manager."
228 << "Call it on defaultServiceManager() instead.";
229 return Void();
230 }
231
232 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &, int32_t) override {
233 // This makes no sense.
234 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
235 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800236 return Void();
237 }
238
Steven Moreland337e6b62017-01-18 17:25:13 -0800239};
240
241sp<IServiceManager> getPassthroughServiceManager() {
242 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
243 return manager;
244}
245
Steven Morelandcbefd352017-01-23 20:29:05 -0800246namespace details {
247
248struct Waiter : IServiceNotification {
249 Return<void> onRegistration(const hidl_string& /* fqName */,
250 const hidl_string& /* name */,
251 bool /* preexisting */) override {
252 std::unique_lock<std::mutex> lock(mMutex);
253 if (mRegistered) {
254 return Void();
255 }
256 mRegistered = true;
257 lock.unlock();
258
259 mCondition.notify_one();
260 return Void();
261 }
262
263 void wait() {
264 std::unique_lock<std::mutex> lock(mMutex);
265 mCondition.wait(lock, [this]{
266 return mRegistered;
267 });
268 }
269
270private:
271 std::mutex mMutex;
272 std::condition_variable mCondition;
273 bool mRegistered = false;
274};
275
276void waitForHwService(
277 const std::string &interface, const std::string &instanceName) {
278 const sp<IServiceManager> manager = defaultServiceManager();
279
280 if (manager == nullptr) {
281 LOG(ERROR) << "Could not get default service manager.";
282 return;
283 }
284
285 sp<Waiter> waiter = new Waiter();
286 Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter);
287
288 if (!ret.isOk()) {
289 LOG(ERROR) << "Transport error, " << ret.description()
290 << ", during notification registration for "
291 << interface << "/" << instanceName << ".";
292 return;
293 }
294
295 if (!ret) {
296 LOG(ERROR) << "Could not register for notifications for "
297 << interface << "/" << instanceName << ".";
298 return;
299 }
300
301 waiter->wait();
302}
303
304}; // namespace details
305
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700306}; // namespace hardware
307}; // namespace android