| Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 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 |  | 
| Martijn Coenen | 12f04d9 | 2016-12-07 17:29:41 +0100 | [diff] [blame] | 19 | #include <hidl/HidlBinderSupport.h> | 
| Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 20 | #include <hidl/ServiceManagement.h> | 
|  | 21 | #include <hidl/Static.h> | 
|  | 22 | #include <hidl/Status.h> | 
|  | 23 |  | 
| Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 24 | #include <android-base/logging.h> | 
|  | 25 | #include <dlfcn.h> | 
| Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 26 | #include <dirent.h> | 
| Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 27 | #include <hidl-util/FQName.h> | 
| Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 28 | #include <hidl-util/StringHelper.h> | 
| Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 29 | #include <hwbinder/IPCThreadState.h> | 
|  | 30 | #include <hwbinder/Parcel.h> | 
| Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 31 | #include <unistd.h> | 
|  | 32 |  | 
|  | 33 | #include <android/hidl/manager/1.0/IServiceManager.h> | 
| Yifan Hong | 4e92599 | 2017-01-09 17:47:17 -0800 | [diff] [blame] | 34 | #include <android/hidl/manager/1.0/BpHwServiceManager.h> | 
|  | 35 | #include <android/hidl/manager/1.0/BnHwServiceManager.h> | 
| Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 36 |  | 
| Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 37 | using android::hidl::manager::V1_0::IServiceManager; | 
| Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 38 | using android::hidl::manager::V1_0::IServiceNotification; | 
| Yifan Hong | 4e92599 | 2017-01-09 17:47:17 -0800 | [diff] [blame] | 39 | using android::hidl::manager::V1_0::BpHwServiceManager; | 
|  | 40 | using android::hidl::manager::V1_0::BnHwServiceManager; | 
| Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 41 |  | 
|  | 42 | namespace android { | 
|  | 43 | namespace hardware { | 
|  | 44 |  | 
|  | 45 | sp<IServiceManager> defaultServiceManager() { | 
|  | 46 |  | 
|  | 47 | if (gDefaultServiceManager != NULL) return gDefaultServiceManager; | 
|  | 48 | if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) { | 
|  | 49 | // HwBinder not available on this device or not accessible to | 
|  | 50 | // this process. | 
|  | 51 | return nullptr; | 
|  | 52 | } | 
|  | 53 | { | 
|  | 54 | AutoMutex _l(gDefaultServiceManagerLock); | 
|  | 55 | while (gDefaultServiceManager == NULL) { | 
| Yifan Hong | 4e92599 | 2017-01-09 17:47:17 -0800 | [diff] [blame] | 56 | gDefaultServiceManager = fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>( | 
| Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 57 | ProcessState::self()->getContextObject(NULL)); | 
|  | 58 | if (gDefaultServiceManager == NULL) | 
|  | 59 | sleep(1); | 
|  | 60 | } | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | return gDefaultServiceManager; | 
|  | 64 | } | 
|  | 65 |  | 
| Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 66 | std::vector<std::string> search(const std::string &path, | 
|  | 67 | const std::string &prefix, | 
|  | 68 | const std::string &suffix) { | 
|  | 69 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir); | 
|  | 70 | if (!dir) return {}; | 
|  | 71 |  | 
|  | 72 | std::vector<std::string> results{}; | 
|  | 73 |  | 
|  | 74 | dirent* dp; | 
|  | 75 | while ((dp = readdir(dir.get())) != nullptr) { | 
|  | 76 | std::string name = dp->d_name; | 
|  | 77 |  | 
|  | 78 | if (StringHelper::StartsWith(name, prefix) && | 
|  | 79 | StringHelper::EndsWith(name, suffix)) { | 
|  | 80 | results.push_back(name); | 
|  | 81 | } | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | return results; | 
|  | 85 | } | 
|  | 86 |  | 
| Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 87 | struct PassthroughServiceManager : IServiceManager { | 
|  | 88 | Return<sp<IBase>> get(const hidl_string& fqName, | 
|  | 89 | const hidl_string& name) override { | 
|  | 90 | FQName iface(fqName); | 
|  | 91 |  | 
|  | 92 | if (!iface.isValid() || | 
|  | 93 | !iface.isFullyQualified() || | 
|  | 94 | iface.isIdentifier()) { | 
|  | 95 | LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName; | 
|  | 96 | return nullptr; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | const int dlMode = RTLD_LAZY; | 
|  | 100 | void *handle = nullptr; | 
|  | 101 |  | 
| Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 102 | std::string library; | 
|  | 103 |  | 
|  | 104 | // TODO: lookup in VINTF instead | 
|  | 105 | // TODO(b/34135607): Remove HAL_LIBRARY_PATH_SYSTEM | 
|  | 106 |  | 
| Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 107 | for (const std::string &path : { | 
|  | 108 | HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM | 
|  | 109 | }) { | 
| Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 110 | const std::string prefix = iface.getPackageAndVersion().string() + "-impl"; | 
|  | 111 |  | 
|  | 112 | std::vector<std::string> libs = search(path, prefix, ".so"); | 
|  | 113 |  | 
|  | 114 | if (libs.size() > 1) { | 
|  | 115 | LOG(WARNING) << "Multiple libraries found: " << StringHelper::JoinStrings(libs, ", "); | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | for (const std::string &lib : libs) { | 
|  | 119 | handle = dlopen((path + lib).c_str(), dlMode); | 
|  | 120 | if (handle != nullptr) { | 
|  | 121 | library = lib; | 
|  | 122 | goto beginLookup; | 
|  | 123 | } | 
| Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 124 | } | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | if (handle == nullptr) { | 
|  | 128 | return nullptr; | 
|  | 129 | } | 
| Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 130 | beginLookup: | 
| Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 131 |  | 
|  | 132 | const std::string sym = "HIDL_FETCH_" + iface.name(); | 
|  | 133 |  | 
|  | 134 | IBase* (*generator)(const char* name); | 
|  | 135 | *(void **)(&generator) = dlsym(handle, sym.c_str()); | 
|  | 136 | if(!generator) { | 
| Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 137 | LOG(ERROR) << "Passthrough lookup opened " << library | 
|  | 138 | << " but could not find symbol " << sym; | 
| Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 139 | return nullptr; | 
|  | 140 | } | 
|  | 141 | return (*generator)(name); | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | Return<bool> add(const hidl_vec<hidl_string>& /* interfaceChain */, | 
|  | 145 | const hidl_string& /* name */, | 
|  | 146 | const sp<IBase>& /* service */) override { | 
|  | 147 | LOG(FATAL) << "Cannot register services with passthrough service manager."; | 
|  | 148 | return false; | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | Return<void> list(list_cb /* _hidl_cb */) override { | 
|  | 152 | // TODO: add this functionality | 
|  | 153 | LOG(FATAL) << "Cannot list services with passthrough service manager."; | 
|  | 154 | return Void(); | 
|  | 155 | } | 
|  | 156 | Return<void> listByInterface(const hidl_string& /* fqInstanceName */, | 
|  | 157 | listByInterface_cb /* _hidl_cb */) override { | 
|  | 158 | // TODO: add this functionality | 
|  | 159 | LOG(FATAL) << "Cannot list services with passthrough service manager."; | 
|  | 160 | return Void(); | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | Return<bool> registerForNotifications(const hidl_string& /* fqName */, | 
|  | 164 | const hidl_string& /* name */, | 
|  | 165 | const sp<IServiceNotification>& /* callback */) override { | 
|  | 166 | // This makes no sense. | 
|  | 167 | LOG(FATAL) << "Cannot register for notifications with passthrough service manager."; | 
|  | 168 | return false; | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | }; | 
|  | 172 |  | 
|  | 173 | sp<IServiceManager> getPassthroughServiceManager() { | 
|  | 174 | static sp<PassthroughServiceManager> manager(new PassthroughServiceManager()); | 
|  | 175 | return manager; | 
|  | 176 | } | 
|  | 177 |  | 
| Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 178 | }; // namespace hardware | 
|  | 179 | }; // namespace android |