blob: 1734935aa720bd78fd4ab210a783d7d1b6796fff [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>
25#include <dlfcn.h>
Steven Moreland0091c092017-01-20 23:15:18 +000026#include <dirent.h>
Steven Moreland337e6b62017-01-18 17:25:13 -080027#include <hidl-util/FQName.h>
Steven Moreland0091c092017-01-20 23:15:18 +000028#include <hidl-util/StringHelper.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070029#include <hwbinder/IPCThreadState.h>
30#include <hwbinder/Parcel.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070031#include <unistd.h>
32
33#include <android/hidl/manager/1.0/IServiceManager.h>
Yifan Hong4e925992017-01-09 17:47:17 -080034#include <android/hidl/manager/1.0/BpHwServiceManager.h>
35#include <android/hidl/manager/1.0/BnHwServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070036
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070037using android::hidl::manager::V1_0::IServiceManager;
Steven Moreland337e6b62017-01-18 17:25:13 -080038using android::hidl::manager::V1_0::IServiceNotification;
Yifan Hong4e925992017-01-09 17:47:17 -080039using android::hidl::manager::V1_0::BpHwServiceManager;
40using android::hidl::manager::V1_0::BnHwServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070041
42namespace android {
43namespace hardware {
44
45sp<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 Hong4e925992017-01-09 17:47:17 -080056 gDefaultServiceManager = fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>(
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070057 ProcessState::self()->getContextObject(NULL));
58 if (gDefaultServiceManager == NULL)
59 sleep(1);
60 }
61 }
62
63 return gDefaultServiceManager;
64}
65
Steven Moreland0091c092017-01-20 23:15:18 +000066std::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 Moreland337e6b62017-01-18 17:25:13 -080087struct 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 Moreland0091c092017-01-20 23:15:18 +0000102 std::string library;
103
104 // TODO: lookup in VINTF instead
105 // TODO(b/34135607): Remove HAL_LIBRARY_PATH_SYSTEM
106
Steven Moreland337e6b62017-01-18 17:25:13 -0800107 for (const std::string &path : {
108 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM
109 }) {
Steven Moreland0091c092017-01-20 23:15:18 +0000110 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 Moreland337e6b62017-01-18 17:25:13 -0800124 }
125 }
126
127 if (handle == nullptr) {
128 return nullptr;
129 }
Steven Moreland0091c092017-01-20 23:15:18 +0000130beginLookup:
Steven Moreland337e6b62017-01-18 17:25:13 -0800131
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 Moreland0091c092017-01-20 23:15:18 +0000137 LOG(ERROR) << "Passthrough lookup opened " << library
138 << " but could not find symbol " << sym;
Steven Moreland337e6b62017-01-18 17:25:13 -0800139 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
173sp<IServiceManager> getPassthroughServiceManager() {
174 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
175 return manager;
176}
177
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700178}; // namespace hardware
179}; // namespace android