blob: ff9884ebc5c40d9b0b01cf9a3a928b84962902f7 [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 Morelandc1cee2c2017-03-24 16:23:11 +000032#include <android-base/properties.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070033#include <hwbinder/IPCThreadState.h>
34#include <hwbinder/Parcel.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070035
36#include <android/hidl/manager/1.0/IServiceManager.h>
Yifan Hong4e925992017-01-09 17:47:17 -080037#include <android/hidl/manager/1.0/BpHwServiceManager.h>
38#include <android/hidl/manager/1.0/BnHwServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070039
Yifan Hong9a22d1d2017-01-25 14:19:26 -080040#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
41#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
42static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
43
Steven Morelandc1cee2c2017-03-24 16:23:11 +000044using android::base::WaitForProperty;
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
Yifan Hong953e6b02017-03-16 14:52:40 -070054namespace details {
55extern Mutex gDefaultServiceManagerLock;
56extern sp<android::hidl::manager::V1_0::IServiceManager> gDefaultServiceManager;
57} // namespace details
58
Steven Morelandc1cee2c2017-03-24 16:23:11 +000059static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready";
60
61void waitForHwServiceManager() {
62 using std::literals::chrono_literals::operator""s;
63
64 while (!WaitForProperty(kHwServicemanagerReadyProperty, "true", 1s)) {
65 LOG(WARNING) << "Waited for hwservicemanager.ready for a second, waiting another...";
66 }
67}
68
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070069sp<IServiceManager> defaultServiceManager() {
70
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070071 {
Yifan Hong953e6b02017-03-16 14:52:40 -070072 AutoMutex _l(details::gDefaultServiceManagerLock);
73 if (details::gDefaultServiceManager != NULL) {
74 return details::gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -070075 }
76 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
77 // HwBinder not available on this device or not accessible to
78 // this process.
79 return nullptr;
80 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +000081
82 waitForHwServiceManager();
83
Yifan Hong953e6b02017-03-16 14:52:40 -070084 while (details::gDefaultServiceManager == NULL) {
85 details::gDefaultServiceManager =
Yifan Hong8fb656b2017-03-16 14:30:40 -070086 fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>(
87 ProcessState::self()->getContextObject(NULL));
Yifan Hong953e6b02017-03-16 14:52:40 -070088 if (details::gDefaultServiceManager == NULL) {
Steven Morelandc1cee2c2017-03-24 16:23:11 +000089 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070090 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -070091 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070092 }
93 }
94
Yifan Hong953e6b02017-03-16 14:52:40 -070095 return details::gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070096}
97
Steven Moreland819c05d2017-04-06 17:24:22 -070098bool endsWith(const std::string &in, const std::string &suffix) {
99 return in.size() >= suffix.size() &&
100 in.substr(in.size() - suffix.size()) == suffix;
101}
102
103bool startsWith(const std::string &in, const std::string &prefix) {
104 return in.size() >= prefix.size() &&
105 in.substr(0, prefix.size()) == prefix;
106}
107
Steven Moreland0091c092017-01-20 23:15:18 +0000108std::vector<std::string> search(const std::string &path,
109 const std::string &prefix,
110 const std::string &suffix) {
111 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
112 if (!dir) return {};
113
114 std::vector<std::string> results{};
115
116 dirent* dp;
117 while ((dp = readdir(dir.get())) != nullptr) {
118 std::string name = dp->d_name;
119
Steven Moreland819c05d2017-04-06 17:24:22 -0700120 if (startsWith(name, prefix) &&
121 endsWith(name, suffix)) {
Steven Moreland0091c092017-01-20 23:15:18 +0000122 results.push_back(name);
123 }
124 }
125
126 return results;
127}
128
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800129bool matchPackageName(const std::string &lib, std::string *matchedName) {
130 std::smatch match;
131 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
132 *matchedName = match.str(1) + "::I*";
133 return true;
134 }
135 return false;
136}
137
Yifan Hong7f49f592017-02-03 15:11:44 -0800138static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
139 sp<IServiceManager> binderizedManager = defaultServiceManager();
140 if (binderizedManager == nullptr) {
141 LOG(WARNING) << "Could not registerReference for "
142 << interfaceName << "/" << instanceName
143 << ": null binderized manager.";
144 return;
145 }
146 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName, getpid());
147 if (!ret.isOk()) {
148 LOG(WARNING) << "Could not registerReference for "
149 << interfaceName << "/" << instanceName
150 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700151 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800152 }
Steven Morelande681aa52017-02-15 16:22:37 -0800153 LOG(VERBOSE) << "Successfully registerReference for "
154 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800155}
156
Steven Moreland337e6b62017-01-18 17:25:13 -0800157struct PassthroughServiceManager : IServiceManager {
158 Return<sp<IBase>> get(const hidl_string& fqName,
Steven Moreland819c05d2017-04-06 17:24:22 -0700159 const hidl_string& name) override {
160 std::string stdFqName(fqName.c_str());
Steven Moreland337e6b62017-01-18 17:25:13 -0800161
Steven Moreland819c05d2017-04-06 17:24:22 -0700162 //fqName looks like android.hardware.foo@1.0::IFoo
163 size_t idx = stdFqName.find("::");
164
165 if (idx == std::string::npos ||
166 idx + strlen("::") + 1 >= stdFqName.size()) {
Steven Moreland337e6b62017-01-18 17:25:13 -0800167 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
168 return nullptr;
169 }
170
Steven Moreland819c05d2017-04-06 17:24:22 -0700171 std::string packageAndVersion = stdFqName.substr(0, idx);
172 std::string ifaceName = stdFqName.substr(idx + strlen("::"));
173
174 const std::string prefix = packageAndVersion + "-impl";
175 const std::string sym = "HIDL_FETCH_" + ifaceName;
Steven Moreland348802d2017-02-23 12:48:55 -0800176
Steven Moreland337e6b62017-01-18 17:25:13 -0800177 const int dlMode = RTLD_LAZY;
178 void *handle = nullptr;
179
Steven Moreland0091c092017-01-20 23:15:18 +0000180 // TODO: lookup in VINTF instead
181 // TODO(b/34135607): Remove HAL_LIBRARY_PATH_SYSTEM
182
Steven Morelanda29905c2017-03-01 10:42:35 -0800183 dlerror(); // clear
184
Steven Moreland337e6b62017-01-18 17:25:13 -0800185 for (const std::string &path : {
186 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM
187 }) {
Steven Moreland0091c092017-01-20 23:15:18 +0000188 std::vector<std::string> libs = search(path, prefix, ".so");
189
Steven Moreland0091c092017-01-20 23:15:18 +0000190 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800191 const std::string fullPath = path + lib;
192
193 handle = dlopen(fullPath.c_str(), dlMode);
194 if (handle == nullptr) {
195 const char* error = dlerror();
196 LOG(ERROR) << "Failed to dlopen " << lib << ": "
197 << (error == nullptr ? "unknown error" : error);
198 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000199 }
Steven Moreland348802d2017-02-23 12:48:55 -0800200
201 IBase* (*generator)(const char* name);
202 *(void **)(&generator) = dlsym(handle, sym.c_str());
203 if(!generator) {
204 const char* error = dlerror();
205 LOG(ERROR) << "Passthrough lookup opened " << lib
206 << " but could not find symbol " << sym << ": "
207 << (error == nullptr ? "unknown error" : error);
208 dlclose(handle);
209 continue;
210 }
211
212 IBase *interface = (*generator)(name);
213
214 if (interface == nullptr) {
215 dlclose(handle);
216 continue; // this module doesn't provide this instance name
217 }
218
219 registerReference(fqName, name);
220
221 return interface;
Steven Moreland337e6b62017-01-18 17:25:13 -0800222 }
223 }
224
Steven Moreland348802d2017-02-23 12:48:55 -0800225 return nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800226 }
227
Martijn Coenen67a02492017-03-06 13:04:48 +0100228 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800229 const sp<IBase>& /* service */) override {
230 LOG(FATAL) << "Cannot register services with passthrough service manager.";
231 return false;
232 }
233
Steven Morelandc601c5f2017-04-06 09:26:07 -0700234 Return<Transport> getTransport(const hidl_string& /* fqName */,
235 const hidl_string& /* name */) {
236 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
237 return Transport::EMPTY;
238 }
239
Yifan Hong705e5da2017-03-02 16:59:39 -0800240 Return<void> list(list_cb /* _hidl_cb */) override {
241 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800242 return Void();
243 }
244 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
245 listByInterface_cb /* _hidl_cb */) override {
246 // TODO: add this functionality
247 LOG(FATAL) << "Cannot list services with passthrough service manager.";
248 return Void();
249 }
250
251 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
252 const hidl_string& /* name */,
253 const sp<IServiceNotification>& /* callback */) override {
254 // This makes no sense.
255 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
256 return false;
257 }
258
Yifan Hong705e5da2017-03-02 16:59:39 -0800259 Return<void> debugDump(debugDump_cb _hidl_cb) override {
260 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
261 static std::vector<std::pair<Arch, std::vector<const char *>>> sAllPaths{
262 {Arch::IS_64BIT, {HAL_LIBRARY_PATH_ODM_64BIT,
263 HAL_LIBRARY_PATH_VENDOR_64BIT,
264 HAL_LIBRARY_PATH_SYSTEM_64BIT}},
265 {Arch::IS_32BIT, {HAL_LIBRARY_PATH_ODM_32BIT,
266 HAL_LIBRARY_PATH_VENDOR_32BIT,
267 HAL_LIBRARY_PATH_SYSTEM_32BIT}}
268 };
269 std::vector<InstanceDebugInfo> vec;
270 for (const auto &pair : sAllPaths) {
271 Arch arch = pair.first;
272 for (const auto &path : pair.second) {
273 std::vector<std::string> libs = search(path, "", ".so");
274 for (const std::string &lib : libs) {
275 std::string matchedName;
276 if (matchPackageName(lib, &matchedName)) {
277 vec.push_back({
278 .interfaceName = matchedName,
279 .instanceName = "*",
280 .clientPids = {},
281 .arch = arch
282 });
283 }
284 }
285 }
286 }
287 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800288 return Void();
289 }
290
291 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &, int32_t) override {
292 // This makes no sense.
293 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
294 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800295 return Void();
296 }
297
Steven Moreland337e6b62017-01-18 17:25:13 -0800298};
299
300sp<IServiceManager> getPassthroughServiceManager() {
301 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
302 return manager;
303}
304
Steven Morelandcbefd352017-01-23 20:29:05 -0800305namespace details {
306
307struct Waiter : IServiceNotification {
308 Return<void> onRegistration(const hidl_string& /* fqName */,
309 const hidl_string& /* name */,
310 bool /* preexisting */) override {
311 std::unique_lock<std::mutex> lock(mMutex);
312 if (mRegistered) {
313 return Void();
314 }
315 mRegistered = true;
316 lock.unlock();
317
318 mCondition.notify_one();
319 return Void();
320 }
321
Steven Moreland49605102017-03-28 09:33:06 -0700322 void wait(const std::string &interface, const std::string &instanceName) {
323 using std::literals::chrono_literals::operator""s;
324
Steven Morelandcbefd352017-01-23 20:29:05 -0800325 std::unique_lock<std::mutex> lock(mMutex);
Steven Moreland49605102017-03-28 09:33:06 -0700326 while(true) {
327 mCondition.wait_for(lock, 1s, [this]{
328 return mRegistered;
329 });
330
331 if (mRegistered) {
332 break;
333 }
334
335 LOG(WARNING) << "Waited one second for "
336 << interface << "/" << instanceName
337 << ". Waiting another...";
338 }
Steven Morelandcbefd352017-01-23 20:29:05 -0800339 }
340
341private:
342 std::mutex mMutex;
343 std::condition_variable mCondition;
344 bool mRegistered = false;
345};
346
347void waitForHwService(
348 const std::string &interface, const std::string &instanceName) {
349 const sp<IServiceManager> manager = defaultServiceManager();
350
351 if (manager == nullptr) {
352 LOG(ERROR) << "Could not get default service manager.";
353 return;
354 }
355
356 sp<Waiter> waiter = new Waiter();
357 Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter);
358
359 if (!ret.isOk()) {
360 LOG(ERROR) << "Transport error, " << ret.description()
361 << ", during notification registration for "
362 << interface << "/" << instanceName << ".";
363 return;
364 }
365
366 if (!ret) {
367 LOG(ERROR) << "Could not register for notifications for "
368 << interface << "/" << instanceName << ".";
369 return;
370 }
371
Steven Moreland49605102017-03-28 09:33:06 -0700372 waiter->wait(interface, instanceName);
Steven Morelandcbefd352017-01-23 20:29:05 -0800373}
374
375}; // namespace details
376
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700377}; // namespace hardware
378}; // namespace android