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