ServiceManagement.cpp revision 953e6b0f57b5003390b3e70904bfa47f535492e4
1/* 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 19#include <condition_variable> 20#include <dlfcn.h> 21#include <dirent.h> 22#include <unistd.h> 23 24#include <mutex> 25#include <regex> 26 27#include <hidl/HidlBinderSupport.h> 28#include <hidl/ServiceManagement.h> 29#include <hidl/Status.h> 30 31#include <android-base/logging.h> 32#include <hidl-util/FQName.h> 33#include <hidl-util/StringHelper.h> 34#include <hwbinder/IPCThreadState.h> 35#include <hwbinder/Parcel.h> 36 37#include <android/hidl/manager/1.0/IServiceManager.h> 38#include <android/hidl/manager/1.0/BpHwServiceManager.h> 39#include <android/hidl/manager/1.0/BnHwServiceManager.h> 40 41#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 45using android::hidl::manager::V1_0::IServiceManager; 46using android::hidl::manager::V1_0::IServiceNotification; 47using android::hidl::manager::V1_0::BpHwServiceManager; 48using android::hidl::manager::V1_0::BnHwServiceManager; 49 50namespace android { 51namespace hardware { 52 53namespace details { 54extern Mutex gDefaultServiceManagerLock; 55extern sp<android::hidl::manager::V1_0::IServiceManager> gDefaultServiceManager; 56} // namespace details 57 58sp<IServiceManager> defaultServiceManager() { 59 60 { 61 AutoMutex _l(details::gDefaultServiceManagerLock); 62 if (details::gDefaultServiceManager != NULL) { 63 return details::gDefaultServiceManager; 64 } 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 } 70 while (details::gDefaultServiceManager == NULL) { 71 details::gDefaultServiceManager = 72 fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>( 73 ProcessState::self()->getContextObject(NULL)); 74 if (details::gDefaultServiceManager == NULL) { 75 sleep(1); 76 } 77 } 78 } 79 80 return details::gDefaultServiceManager; 81} 82 83std::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 104bool 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 113static 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(); 126 } 127 LOG(VERBOSE) << "Successfully registerReference for " 128 << interfaceName << "/" << instanceName; 129} 130 131struct PassthroughServiceManager : IServiceManager { 132 Return<sp<IBase>> get(const hidl_string& fqName, 133 const hidl_string& name) override { 134 const FQName iface(fqName); 135 136 if (!iface.isValid() || 137 !iface.isFullyQualified() || 138 iface.isIdentifier()) { 139 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName; 140 return nullptr; 141 } 142 143 const std::string prefix = iface.getPackageAndVersion().string() + "-impl"; 144 const std::string sym = "HIDL_FETCH_" + iface.name(); 145 146 const int dlMode = RTLD_LAZY; 147 void *handle = nullptr; 148 149 // TODO: lookup in VINTF instead 150 // TODO(b/34135607): Remove HAL_LIBRARY_PATH_SYSTEM 151 152 dlerror(); // clear 153 154 for (const std::string &path : { 155 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM 156 }) { 157 std::vector<std::string> libs = search(path, prefix, ".so"); 158 159 for (const std::string &lib : libs) { 160 const std::string fullPath = path + lib; 161 162 handle = dlopen(fullPath.c_str(), dlMode); 163 if (handle == nullptr) { 164 const char* error = dlerror(); 165 LOG(ERROR) << "Failed to dlopen " << lib << ": " 166 << (error == nullptr ? "unknown error" : error); 167 continue; 168 } 169 170 IBase* (*generator)(const char* name); 171 *(void **)(&generator) = dlsym(handle, sym.c_str()); 172 if(!generator) { 173 const char* error = dlerror(); 174 LOG(ERROR) << "Passthrough lookup opened " << lib 175 << " but could not find symbol " << sym << ": " 176 << (error == nullptr ? "unknown error" : error); 177 dlclose(handle); 178 continue; 179 } 180 181 IBase *interface = (*generator)(name); 182 183 if (interface == nullptr) { 184 dlclose(handle); 185 continue; // this module doesn't provide this instance name 186 } 187 188 registerReference(fqName, name); 189 190 return interface; 191 } 192 } 193 194 return nullptr; 195 } 196 197 Return<bool> add(const hidl_string& /* name */, 198 const sp<IBase>& /* service */) override { 199 LOG(FATAL) << "Cannot register services with passthrough service manager."; 200 return false; 201 } 202 203 Return<void> list(list_cb /* _hidl_cb */) override { 204 LOG(FATAL) << "Cannot list services with passthrough service manager."; 205 return Void(); 206 } 207 Return<void> listByInterface(const hidl_string& /* fqInstanceName */, 208 listByInterface_cb /* _hidl_cb */) override { 209 // TODO: add this functionality 210 LOG(FATAL) << "Cannot list services with passthrough service manager."; 211 return Void(); 212 } 213 214 Return<bool> registerForNotifications(const hidl_string& /* fqName */, 215 const hidl_string& /* name */, 216 const sp<IServiceNotification>& /* callback */) override { 217 // This makes no sense. 218 LOG(FATAL) << "Cannot register for notifications with passthrough service manager."; 219 return false; 220 } 221 222 Return<void> debugDump(debugDump_cb _hidl_cb) override { 223 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture; 224 static std::vector<std::pair<Arch, std::vector<const char *>>> sAllPaths{ 225 {Arch::IS_64BIT, {HAL_LIBRARY_PATH_ODM_64BIT, 226 HAL_LIBRARY_PATH_VENDOR_64BIT, 227 HAL_LIBRARY_PATH_SYSTEM_64BIT}}, 228 {Arch::IS_32BIT, {HAL_LIBRARY_PATH_ODM_32BIT, 229 HAL_LIBRARY_PATH_VENDOR_32BIT, 230 HAL_LIBRARY_PATH_SYSTEM_32BIT}} 231 }; 232 std::vector<InstanceDebugInfo> vec; 233 for (const auto &pair : sAllPaths) { 234 Arch arch = pair.first; 235 for (const auto &path : pair.second) { 236 std::vector<std::string> libs = search(path, "", ".so"); 237 for (const std::string &lib : libs) { 238 std::string matchedName; 239 if (matchPackageName(lib, &matchedName)) { 240 vec.push_back({ 241 .interfaceName = matchedName, 242 .instanceName = "*", 243 .clientPids = {}, 244 .arch = arch 245 }); 246 } 247 } 248 } 249 } 250 _hidl_cb(vec); 251 return Void(); 252 } 253 254 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &, int32_t) override { 255 // This makes no sense. 256 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. " 257 << "Call it on defaultServiceManager() instead."; 258 return Void(); 259 } 260 261}; 262 263sp<IServiceManager> getPassthroughServiceManager() { 264 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager()); 265 return manager; 266} 267 268namespace details { 269 270struct Waiter : IServiceNotification { 271 Return<void> onRegistration(const hidl_string& /* fqName */, 272 const hidl_string& /* name */, 273 bool /* preexisting */) override { 274 std::unique_lock<std::mutex> lock(mMutex); 275 if (mRegistered) { 276 return Void(); 277 } 278 mRegistered = true; 279 lock.unlock(); 280 281 mCondition.notify_one(); 282 return Void(); 283 } 284 285 void wait() { 286 std::unique_lock<std::mutex> lock(mMutex); 287 mCondition.wait(lock, [this]{ 288 return mRegistered; 289 }); 290 } 291 292private: 293 std::mutex mMutex; 294 std::condition_variable mCondition; 295 bool mRegistered = false; 296}; 297 298void waitForHwService( 299 const std::string &interface, const std::string &instanceName) { 300 const sp<IServiceManager> manager = defaultServiceManager(); 301 302 if (manager == nullptr) { 303 LOG(ERROR) << "Could not get default service manager."; 304 return; 305 } 306 307 sp<Waiter> waiter = new Waiter(); 308 Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter); 309 310 if (!ret.isOk()) { 311 LOG(ERROR) << "Transport error, " << ret.description() 312 << ", during notification registration for " 313 << interface << "/" << instanceName << "."; 314 return; 315 } 316 317 if (!ret) { 318 LOG(ERROR) << "Could not register for notifications for " 319 << interface << "/" << instanceName << "."; 320 return; 321 } 322 323 waiter->wait(); 324} 325 326}; // namespace details 327 328}; // namespace hardware 329}; // namespace android 330