blob: e3cbea4776a74e43363bb5720f1c3b6a7b96167a [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
Steven Moreland337e6b62017-01-18 17:25:13 -0800105struct PassthroughServiceManager : IServiceManager {
106 Return<sp<IBase>> get(const hidl_string& fqName,
107 const hidl_string& name) override {
108 FQName iface(fqName);
109
110 if (!iface.isValid() ||
111 !iface.isFullyQualified() ||
112 iface.isIdentifier()) {
113 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
114 return nullptr;
115 }
116
117 const int dlMode = RTLD_LAZY;
118 void *handle = nullptr;
119
Steven Moreland0091c092017-01-20 23:15:18 +0000120 std::string library;
121
122 // TODO: lookup in VINTF instead
123 // TODO(b/34135607): Remove HAL_LIBRARY_PATH_SYSTEM
124
Steven Moreland337e6b62017-01-18 17:25:13 -0800125 for (const std::string &path : {
126 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM
127 }) {
Steven Moreland0091c092017-01-20 23:15:18 +0000128 const std::string prefix = iface.getPackageAndVersion().string() + "-impl";
129
130 std::vector<std::string> libs = search(path, prefix, ".so");
131
132 if (libs.size() > 1) {
133 LOG(WARNING) << "Multiple libraries found: " << StringHelper::JoinStrings(libs, ", ");
134 }
135
136 for (const std::string &lib : libs) {
137 handle = dlopen((path + lib).c_str(), dlMode);
138 if (handle != nullptr) {
139 library = lib;
140 goto beginLookup;
141 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800142 }
143 }
144
145 if (handle == nullptr) {
146 return nullptr;
147 }
Steven Moreland0091c092017-01-20 23:15:18 +0000148beginLookup:
Steven Moreland337e6b62017-01-18 17:25:13 -0800149
150 const std::string sym = "HIDL_FETCH_" + iface.name();
151
152 IBase* (*generator)(const char* name);
153 *(void **)(&generator) = dlsym(handle, sym.c_str());
154 if(!generator) {
Steven Moreland0091c092017-01-20 23:15:18 +0000155 LOG(ERROR) << "Passthrough lookup opened " << library
156 << " but could not find symbol " << sym;
Steven Moreland337e6b62017-01-18 17:25:13 -0800157 return nullptr;
158 }
159 return (*generator)(name);
160 }
161
162 Return<bool> add(const hidl_vec<hidl_string>& /* interfaceChain */,
163 const hidl_string& /* name */,
164 const sp<IBase>& /* service */) override {
165 LOG(FATAL) << "Cannot register services with passthrough service manager.";
166 return false;
167 }
168
169 Return<void> list(list_cb /* _hidl_cb */) override {
170 // TODO: add this functionality
171 LOG(FATAL) << "Cannot list services with passthrough service manager.";
172 return Void();
173 }
174 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
175 listByInterface_cb /* _hidl_cb */) override {
176 // TODO: add this functionality
177 LOG(FATAL) << "Cannot list services with passthrough service manager.";
178 return Void();
179 }
180
181 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
182 const hidl_string& /* name */,
183 const sp<IServiceNotification>& /* callback */) override {
184 // This makes no sense.
185 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
186 return false;
187 }
188
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800189 Return<void> debugDump(debugDump_cb _cb) override {
190 std::vector<InstanceDebugInfo> vec;
191 for (const std::string &path : {
192 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM
193 }) {
194 std::vector<std::string> libs = search(path, "", ".so");
195 for (const std::string &lib : libs) {
196 std::string matchedName;
197 if (matchPackageName(lib, &matchedName)) {
198 vec.push_back({
199 .interfaceName = matchedName,
200 .instanceName = "",
Yifan Hong777bef92017-02-01 15:50:36 -0800201 .pid = -1,
202 .ptr = 0,
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800203 });
204 }
205 }
206 }
207 _cb(vec);
208 return Void();
209 }
210
Steven Moreland337e6b62017-01-18 17:25:13 -0800211};
212
213sp<IServiceManager> getPassthroughServiceManager() {
214 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
215 return manager;
216}
217
Steven Morelandcbefd352017-01-23 20:29:05 -0800218namespace details {
219
220struct Waiter : IServiceNotification {
221 Return<void> onRegistration(const hidl_string& /* fqName */,
222 const hidl_string& /* name */,
223 bool /* preexisting */) override {
224 std::unique_lock<std::mutex> lock(mMutex);
225 if (mRegistered) {
226 return Void();
227 }
228 mRegistered = true;
229 lock.unlock();
230
231 mCondition.notify_one();
232 return Void();
233 }
234
235 void wait() {
236 std::unique_lock<std::mutex> lock(mMutex);
237 mCondition.wait(lock, [this]{
238 return mRegistered;
239 });
240 }
241
242private:
243 std::mutex mMutex;
244 std::condition_variable mCondition;
245 bool mRegistered = false;
246};
247
248void waitForHwService(
249 const std::string &interface, const std::string &instanceName) {
250 const sp<IServiceManager> manager = defaultServiceManager();
251
252 if (manager == nullptr) {
253 LOG(ERROR) << "Could not get default service manager.";
254 return;
255 }
256
257 sp<Waiter> waiter = new Waiter();
258 Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter);
259
260 if (!ret.isOk()) {
261 LOG(ERROR) << "Transport error, " << ret.description()
262 << ", during notification registration for "
263 << interface << "/" << instanceName << ".";
264 return;
265 }
266
267 if (!ret) {
268 LOG(ERROR) << "Could not register for notifications for "
269 << interface << "/" << instanceName << ".";
270 return;
271 }
272
273 waiter->wait();
274}
275
276}; // namespace details
277
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700278}; // namespace hardware
279}; // namespace android