blob: 9a6a3b5200ea524cb5785cc67073ca45858bb8c7 [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
Martijn Coenen12f04d92016-12-07 17:29:41 +010019#include <hidl/HidlBinderSupport.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070020#include <hidl/ServiceManagement.h>
21#include <hidl/Static.h>
22#include <hidl/Status.h>
23
Steven Moreland337e6b62017-01-18 17:25:13 -080024#include <android-base/logging.h>
Steven Morelandcbefd352017-01-23 20:29:05 -080025#include <condition_variable>
Steven Moreland337e6b62017-01-18 17:25:13 -080026#include <dlfcn.h>
Steven Moreland0091c092017-01-20 23:15:18 +000027#include <dirent.h>
Steven Moreland337e6b62017-01-18 17:25:13 -080028#include <hidl-util/FQName.h>
Steven Moreland0091c092017-01-20 23:15:18 +000029#include <hidl-util/StringHelper.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070030#include <hwbinder/IPCThreadState.h>
31#include <hwbinder/Parcel.h>
Steven Morelandcbefd352017-01-23 20:29:05 -080032#include <mutex>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070033#include <unistd.h>
34
35#include <android/hidl/manager/1.0/IServiceManager.h>
Yifan Hong4e925992017-01-09 17:47:17 -080036#include <android/hidl/manager/1.0/BpHwServiceManager.h>
37#include <android/hidl/manager/1.0/BnHwServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070038
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070039using android::hidl::manager::V1_0::IServiceManager;
Steven Moreland337e6b62017-01-18 17:25:13 -080040using android::hidl::manager::V1_0::IServiceNotification;
Yifan Hong4e925992017-01-09 17:47:17 -080041using android::hidl::manager::V1_0::BpHwServiceManager;
42using android::hidl::manager::V1_0::BnHwServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070043
44namespace android {
45namespace hardware {
46
47sp<IServiceManager> defaultServiceManager() {
48
49 if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
50 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
51 // HwBinder not available on this device or not accessible to
52 // this process.
53 return nullptr;
54 }
55 {
56 AutoMutex _l(gDefaultServiceManagerLock);
57 while (gDefaultServiceManager == NULL) {
Yifan Hong4e925992017-01-09 17:47:17 -080058 gDefaultServiceManager = fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>(
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070059 ProcessState::self()->getContextObject(NULL));
60 if (gDefaultServiceManager == NULL)
61 sleep(1);
62 }
63 }
64
65 return gDefaultServiceManager;
66}
67
Steven Moreland0091c092017-01-20 23:15:18 +000068std::vector<std::string> search(const std::string &path,
69 const std::string &prefix,
70 const std::string &suffix) {
71 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
72 if (!dir) return {};
73
74 std::vector<std::string> results{};
75
76 dirent* dp;
77 while ((dp = readdir(dir.get())) != nullptr) {
78 std::string name = dp->d_name;
79
80 if (StringHelper::StartsWith(name, prefix) &&
81 StringHelper::EndsWith(name, suffix)) {
82 results.push_back(name);
83 }
84 }
85
86 return results;
87}
88
Steven Moreland337e6b62017-01-18 17:25:13 -080089struct PassthroughServiceManager : IServiceManager {
90 Return<sp<IBase>> get(const hidl_string& fqName,
91 const hidl_string& name) override {
92 FQName iface(fqName);
93
94 if (!iface.isValid() ||
95 !iface.isFullyQualified() ||
96 iface.isIdentifier()) {
97 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
98 return nullptr;
99 }
100
101 const int dlMode = RTLD_LAZY;
102 void *handle = nullptr;
103
Steven Moreland0091c092017-01-20 23:15:18 +0000104 std::string library;
105
106 // TODO: lookup in VINTF instead
107 // TODO(b/34135607): Remove HAL_LIBRARY_PATH_SYSTEM
108
Steven Moreland337e6b62017-01-18 17:25:13 -0800109 for (const std::string &path : {
110 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM
111 }) {
Steven Moreland0091c092017-01-20 23:15:18 +0000112 const std::string prefix = iface.getPackageAndVersion().string() + "-impl";
113
114 std::vector<std::string> libs = search(path, prefix, ".so");
115
116 if (libs.size() > 1) {
117 LOG(WARNING) << "Multiple libraries found: " << StringHelper::JoinStrings(libs, ", ");
118 }
119
120 for (const std::string &lib : libs) {
121 handle = dlopen((path + lib).c_str(), dlMode);
122 if (handle != nullptr) {
123 library = lib;
124 goto beginLookup;
125 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800126 }
127 }
128
129 if (handle == nullptr) {
130 return nullptr;
131 }
Steven Moreland0091c092017-01-20 23:15:18 +0000132beginLookup:
Steven Moreland337e6b62017-01-18 17:25:13 -0800133
134 const std::string sym = "HIDL_FETCH_" + iface.name();
135
136 IBase* (*generator)(const char* name);
137 *(void **)(&generator) = dlsym(handle, sym.c_str());
138 if(!generator) {
Steven Moreland0091c092017-01-20 23:15:18 +0000139 LOG(ERROR) << "Passthrough lookup opened " << library
140 << " but could not find symbol " << sym;
Steven Moreland337e6b62017-01-18 17:25:13 -0800141 return nullptr;
142 }
143 return (*generator)(name);
144 }
145
146 Return<bool> add(const hidl_vec<hidl_string>& /* interfaceChain */,
147 const hidl_string& /* name */,
148 const sp<IBase>& /* service */) override {
149 LOG(FATAL) << "Cannot register services with passthrough service manager.";
150 return false;
151 }
152
153 Return<void> list(list_cb /* _hidl_cb */) override {
154 // TODO: add this functionality
155 LOG(FATAL) << "Cannot list services with passthrough service manager.";
156 return Void();
157 }
158 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
159 listByInterface_cb /* _hidl_cb */) override {
160 // TODO: add this functionality
161 LOG(FATAL) << "Cannot list services with passthrough service manager.";
162 return Void();
163 }
164
165 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
166 const hidl_string& /* name */,
167 const sp<IServiceNotification>& /* callback */) override {
168 // This makes no sense.
169 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
170 return false;
171 }
172
173};
174
175sp<IServiceManager> getPassthroughServiceManager() {
176 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
177 return manager;
178}
179
Steven Morelandcbefd352017-01-23 20:29:05 -0800180namespace details {
181
182struct Waiter : IServiceNotification {
183 Return<void> onRegistration(const hidl_string& /* fqName */,
184 const hidl_string& /* name */,
185 bool /* preexisting */) override {
186 std::unique_lock<std::mutex> lock(mMutex);
187 if (mRegistered) {
188 return Void();
189 }
190 mRegistered = true;
191 lock.unlock();
192
193 mCondition.notify_one();
194 return Void();
195 }
196
197 void wait() {
198 std::unique_lock<std::mutex> lock(mMutex);
199 mCondition.wait(lock, [this]{
200 return mRegistered;
201 });
202 }
203
204private:
205 std::mutex mMutex;
206 std::condition_variable mCondition;
207 bool mRegistered = false;
208};
209
210void waitForHwService(
211 const std::string &interface, const std::string &instanceName) {
212 const sp<IServiceManager> manager = defaultServiceManager();
213
214 if (manager == nullptr) {
215 LOG(ERROR) << "Could not get default service manager.";
216 return;
217 }
218
219 sp<Waiter> waiter = new Waiter();
220 Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter);
221
222 if (!ret.isOk()) {
223 LOG(ERROR) << "Transport error, " << ret.description()
224 << ", during notification registration for "
225 << interface << "/" << instanceName << ".";
226 return;
227 }
228
229 if (!ret) {
230 LOG(ERROR) << "Could not register for notifications for "
231 << interface << "/" << instanceName << ".";
232 return;
233 }
234
235 waiter->wait();
236}
237
238}; // namespace details
239
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700240}; // namespace hardware
241}; // namespace android