Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #include "ServiceManager.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 20 | #include <android-base/properties.h> |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 21 | #include <android-base/scopeguard.h> |
Devin Moore | 42407bc | 2023-09-26 21:30:39 +0000 | [diff] [blame] | 22 | #include <android-base/strings.h> |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 23 | #include <binder/BpBinder.h> |
| 24 | #include <binder/IPCThreadState.h> |
| 25 | #include <binder/ProcessState.h> |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 26 | #include <binder/Stability.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 27 | #include <cutils/android_filesystem_config.h> |
| 28 | #include <cutils/multiuser.h> |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 29 | #include <thread> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 30 | |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 31 | #if !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__) |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 32 | #include "perfetto/public/protos/trace/android/android_track_event.pzc.h" |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 33 | #include "perfetto/public/te_category_macros.h" |
| 34 | #include "perfetto/public/te_macros.h" |
| 35 | #endif // !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__) |
| 36 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 37 | #ifndef VENDORSERVICEMANAGER |
| 38 | #include <vintf/VintfObject.h> |
Yifan Hong | 0a9b56e | 2021-11-30 16:45:40 -0800 | [diff] [blame] | 39 | #ifdef __ANDROID_RECOVERY__ |
| 40 | #include <vintf/VintfObjectRecovery.h> |
| 41 | #endif // __ANDROID_RECOVERY__ |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 42 | #include <vintf/constants.h> |
| 43 | #endif // !VENDORSERVICEMANAGER |
| 44 | |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 45 | #include "NameUtil.h" |
| 46 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 47 | using ::android::binder::Status; |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 48 | using ::android::internal::Stability; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 49 | |
| 50 | namespace android { |
| 51 | |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 52 | #if defined(VENDORSERVICEMANAGER) || defined(__ANDROID_RECOVERY__) |
| 53 | #define SM_PERFETTO_TRACE_FUNC(...) |
| 54 | #else |
| 55 | |
| 56 | PERFETTO_TE_CATEGORIES_DEFINE(PERFETTO_SM_CATEGORIES); |
| 57 | |
| 58 | #define SM_PERFETTO_TRACE_FUNC(...) \ |
| 59 | PERFETTO_TE_SCOPED(servicemanager, PERFETTO_TE_SLICE_BEGIN(__func__) __VA_OPT__(, ) __VA_ARGS__) |
| 60 | |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 61 | constexpr uint32_t kProtoServiceName = |
| 62 | perfetto_protos_AndroidTrackEvent_binder_service_name_field_number; |
| 63 | constexpr uint32_t kProtoInterfaceName = |
| 64 | perfetto_protos_AndroidTrackEvent_binder_interface_name_field_number; |
| 65 | constexpr uint32_t kProtoApexName = perfetto_protos_AndroidTrackEvent_apex_name_field_number; |
| 66 | |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 67 | #endif // !(defined(VENDORSERVICEMANAGER) || defined(__ANDROID_RECOVERY__)) |
| 68 | |
Steven Moreland | b9e1cbe | 2023-02-01 22:44:45 +0000 | [diff] [blame] | 69 | bool is_multiuser_uid_isolated(uid_t uid) { |
| 70 | uid_t appid = multiuser_get_app_id(uid); |
| 71 | return appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END; |
| 72 | } |
| 73 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 74 | #ifndef VENDORSERVICEMANAGER |
Yifan Hong | 0a9b56e | 2021-11-30 16:45:40 -0800 | [diff] [blame] | 75 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 76 | struct ManifestWithDescription { |
| 77 | std::shared_ptr<const vintf::HalManifest> manifest; |
| 78 | const char* description; |
| 79 | }; |
Yifan Hong | 0a9b56e | 2021-11-30 16:45:40 -0800 | [diff] [blame] | 80 | static std::vector<ManifestWithDescription> GetManifestsWithDescription() { |
| 81 | #ifdef __ANDROID_RECOVERY__ |
| 82 | auto vintfObject = vintf::VintfObjectRecovery::GetInstance(); |
| 83 | if (vintfObject == nullptr) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 84 | ALOGE("NULL VintfObjectRecovery!"); |
Yifan Hong | 0a9b56e | 2021-11-30 16:45:40 -0800 | [diff] [blame] | 85 | return {}; |
| 86 | } |
| 87 | return {ManifestWithDescription{vintfObject->getRecoveryHalManifest(), "recovery"}}; |
| 88 | #else |
| 89 | auto vintfObject = vintf::VintfObject::GetInstance(); |
| 90 | if (vintfObject == nullptr) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 91 | ALOGE("NULL VintfObject!"); |
Yifan Hong | 0a9b56e | 2021-11-30 16:45:40 -0800 | [diff] [blame] | 92 | return {}; |
| 93 | } |
| 94 | return {ManifestWithDescription{vintfObject->getDeviceHalManifest(), "device"}, |
| 95 | ManifestWithDescription{vintfObject->getFrameworkHalManifest(), "framework"}}; |
| 96 | #endif |
| 97 | } |
| 98 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 99 | // func true -> stop search and forEachManifest will return true |
| 100 | static bool forEachManifest(const std::function<bool(const ManifestWithDescription&)>& func) { |
Yifan Hong | 0a9b56e | 2021-11-30 16:45:40 -0800 | [diff] [blame] | 101 | for (const ManifestWithDescription& mwd : GetManifestsWithDescription()) { |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 102 | if (mwd.manifest == nullptr) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 103 | ALOGE("NULL VINTF MANIFEST!: %s", mwd.description); |
| 104 | // note, we explicitly do not retry here, so that we can detect VINTF |
| 105 | // or other bugs (b/151696835) |
| 106 | continue; |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 107 | } |
| 108 | if (func(mwd)) return true; |
| 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 113 | static std::string getNativeInstanceName(const vintf::ManifestInstance& instance) { |
| 114 | return instance.package() + "/" + instance.instance(); |
| 115 | } |
| 116 | |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 117 | struct AidlName { |
| 118 | std::string package; |
| 119 | std::string iface; |
| 120 | std::string instance; |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 121 | |
Devin Moore | 9d1dfa0 | 2024-07-23 21:40:05 +0000 | [diff] [blame] | 122 | static bool fill(const std::string& name, AidlName* aname, bool logError) { |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 123 | size_t firstSlash = name.find('/'); |
| 124 | size_t lastDot = name.rfind('.', firstSlash); |
| 125 | if (firstSlash == std::string::npos || lastDot == std::string::npos) { |
Devin Moore | 9d1dfa0 | 2024-07-23 21:40:05 +0000 | [diff] [blame] | 126 | if (logError) { |
| 127 | ALOGE("VINTF HALs require names in the format type/instance (e.g. " |
| 128 | "some.package.foo.IFoo/default) but got: %s", |
| 129 | name.c_str()); |
| 130 | } |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | aname->package = name.substr(0, lastDot); |
| 134 | aname->iface = name.substr(lastDot + 1, firstSlash - lastDot - 1); |
| 135 | aname->instance = name.substr(firstSlash + 1); |
| 136 | return true; |
| 137 | } |
| 138 | }; |
| 139 | |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 140 | static std::string getAidlInstanceName(const vintf::ManifestInstance& instance) { |
| 141 | return instance.package() + "." + instance.interface() + "/" + instance.instance(); |
| 142 | } |
| 143 | |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 144 | static bool isVintfDeclared(const Access::CallingContext& ctx, const std::string& name) { |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 145 | NativeName nname; |
| 146 | if (NativeName::fill(name, &nname)) { |
| 147 | bool found = forEachManifest([&](const ManifestWithDescription& mwd) { |
| 148 | if (mwd.manifest->hasNativeInstance(nname.package, nname.instance)) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 149 | ALOGI("%s Found %s in %s VINTF manifest.", ctx.toDebugString().c_str(), |
| 150 | name.c_str(), mwd.description); |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 151 | return true; // break |
| 152 | } |
| 153 | return false; // continue |
| 154 | }); |
| 155 | if (!found) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 156 | ALOGI("%s Could not find %s in the VINTF manifest.", ctx.toDebugString().c_str(), |
| 157 | name.c_str()); |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 158 | } |
| 159 | return found; |
| 160 | } |
| 161 | |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 162 | AidlName aname; |
Devin Moore | 9d1dfa0 | 2024-07-23 21:40:05 +0000 | [diff] [blame] | 163 | if (!AidlName::fill(name, &aname, true)) return false; |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 164 | |
| 165 | bool found = forEachManifest([&](const ManifestWithDescription& mwd) { |
| 166 | if (mwd.manifest->hasAidlInstance(aname.package, aname.iface, aname.instance)) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 167 | ALOGI("%s Found %s in %s VINTF manifest.", ctx.toDebugString().c_str(), name.c_str(), |
| 168 | mwd.description); |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 169 | return true; // break |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 170 | } |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 171 | return false; // continue |
| 172 | }); |
| 173 | |
| 174 | if (!found) { |
Devin Moore | 42407bc | 2023-09-26 21:30:39 +0000 | [diff] [blame] | 175 | std::set<std::string> instances; |
| 176 | forEachManifest([&](const ManifestWithDescription& mwd) { |
| 177 | std::set<std::string> res = mwd.manifest->getAidlInstances(aname.package, aname.iface); |
| 178 | instances.insert(res.begin(), res.end()); |
| 179 | return true; |
| 180 | }); |
| 181 | |
| 182 | std::string available; |
| 183 | if (instances.empty()) { |
| 184 | available = "No alternative instances declared in VINTF"; |
| 185 | } else { |
| 186 | // for logging only. We can't return this information to the client |
| 187 | // because they may not have permissions to find or list those |
| 188 | // instances |
| 189 | available = "VINTF declared instances: " + base::Join(instances, ", "); |
| 190 | } |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 191 | // Although it is tested, explicitly rebuilding qualified name, in case it |
| 192 | // becomes something unexpected. |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 193 | ALOGI("%s Could not find %s.%s/%s in the VINTF manifest. %s.", ctx.toDebugString().c_str(), |
| 194 | aname.package.c_str(), aname.iface.c_str(), aname.instance.c_str(), |
| 195 | available.c_str()); |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 196 | } |
Steven Moreland | 2edde8e | 2020-04-30 17:04:54 -0700 | [diff] [blame] | 197 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 198 | return found; |
| 199 | } |
| 200 | |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 201 | static std::optional<std::string> getVintfUpdatableApex(const std::string& name) { |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 202 | NativeName nname; |
| 203 | if (NativeName::fill(name, &nname)) { |
| 204 | std::optional<std::string> updatableViaApex; |
| 205 | |
| 206 | forEachManifest([&](const ManifestWithDescription& mwd) { |
| 207 | bool cont = mwd.manifest->forEachInstance([&](const auto& manifestInstance) { |
| 208 | if (manifestInstance.format() != vintf::HalFormat::NATIVE) return true; |
| 209 | if (manifestInstance.package() != nname.package) return true; |
| 210 | if (manifestInstance.instance() != nname.instance) return true; |
| 211 | updatableViaApex = manifestInstance.updatableViaApex(); |
| 212 | return false; // break (libvintf uses opposite convention) |
| 213 | }); |
| 214 | return !cont; |
| 215 | }); |
| 216 | |
| 217 | return updatableViaApex; |
| 218 | } |
| 219 | |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 220 | AidlName aname; |
Devin Moore | 9d1dfa0 | 2024-07-23 21:40:05 +0000 | [diff] [blame] | 221 | if (!AidlName::fill(name, &aname, true)) return std::nullopt; |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 222 | |
| 223 | std::optional<std::string> updatableViaApex; |
| 224 | |
| 225 | forEachManifest([&](const ManifestWithDescription& mwd) { |
Jooyung Han | 9f1c687 | 2024-01-23 06:42:02 +0900 | [diff] [blame] | 226 | bool cont = mwd.manifest->forEachInstance([&](const auto& manifestInstance) { |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 227 | if (manifestInstance.format() != vintf::HalFormat::AIDL) return true; |
| 228 | if (manifestInstance.package() != aname.package) return true; |
| 229 | if (manifestInstance.interface() != aname.iface) return true; |
| 230 | if (manifestInstance.instance() != aname.instance) return true; |
| 231 | updatableViaApex = manifestInstance.updatableViaApex(); |
| 232 | return false; // break (libvintf uses opposite convention) |
| 233 | }); |
Jooyung Han | 9f1c687 | 2024-01-23 06:42:02 +0900 | [diff] [blame] | 234 | return !cont; |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 235 | }); |
| 236 | |
| 237 | return updatableViaApex; |
| 238 | } |
| 239 | |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 240 | static std::vector<std::string> getVintfUpdatableNames(const std::string& apexName) { |
| 241 | std::vector<std::string> names; |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 242 | |
| 243 | forEachManifest([&](const ManifestWithDescription& mwd) { |
| 244 | mwd.manifest->forEachInstance([&](const auto& manifestInstance) { |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 245 | if (manifestInstance.updatableViaApex().has_value() && |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 246 | manifestInstance.updatableViaApex().value() == apexName) { |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 247 | if (manifestInstance.format() == vintf::HalFormat::NATIVE) { |
| 248 | names.push_back(getNativeInstanceName(manifestInstance)); |
| 249 | } else if (manifestInstance.format() == vintf::HalFormat::AIDL) { |
| 250 | names.push_back(getAidlInstanceName(manifestInstance)); |
| 251 | } |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 252 | } |
Jooyung Han | ce94b75 | 2022-11-14 18:55:06 +0900 | [diff] [blame] | 253 | return true; // continue (libvintf uses opposite convention) |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 254 | }); |
| 255 | return false; // continue |
| 256 | }); |
| 257 | |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 258 | return names; |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 259 | } |
| 260 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 261 | static std::optional<std::string> getVintfAccessorName(const std::string& name) { |
| 262 | AidlName aname; |
Devin Moore | 9d1dfa0 | 2024-07-23 21:40:05 +0000 | [diff] [blame] | 263 | if (!AidlName::fill(name, &aname, false)) return std::nullopt; |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 264 | |
| 265 | std::optional<std::string> accessor; |
| 266 | forEachManifest([&](const ManifestWithDescription& mwd) { |
| 267 | mwd.manifest->forEachInstance([&](const auto& manifestInstance) { |
| 268 | if (manifestInstance.format() != vintf::HalFormat::AIDL) return true; |
| 269 | if (manifestInstance.package() != aname.package) return true; |
| 270 | if (manifestInstance.interface() != aname.iface) return true; |
| 271 | if (manifestInstance.instance() != aname.instance) return true; |
| 272 | accessor = manifestInstance.accessor(); |
| 273 | return false; // break (libvintf uses opposite convention) |
| 274 | }); |
| 275 | return false; // continue |
| 276 | }); |
| 277 | return accessor; |
| 278 | } |
| 279 | |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 280 | static std::optional<ConnectionInfo> getVintfConnectionInfo(const std::string& name) { |
| 281 | AidlName aname; |
Devin Moore | 9d1dfa0 | 2024-07-23 21:40:05 +0000 | [diff] [blame] | 282 | if (!AidlName::fill(name, &aname, true)) return std::nullopt; |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 283 | |
| 284 | std::optional<std::string> ip; |
| 285 | std::optional<uint64_t> port; |
| 286 | forEachManifest([&](const ManifestWithDescription& mwd) { |
| 287 | mwd.manifest->forEachInstance([&](const auto& manifestInstance) { |
| 288 | if (manifestInstance.format() != vintf::HalFormat::AIDL) return true; |
| 289 | if (manifestInstance.package() != aname.package) return true; |
| 290 | if (manifestInstance.interface() != aname.iface) return true; |
| 291 | if (manifestInstance.instance() != aname.instance) return true; |
| 292 | ip = manifestInstance.ip(); |
| 293 | port = manifestInstance.port(); |
| 294 | return false; // break (libvintf uses opposite convention) |
| 295 | }); |
| 296 | return false; // continue |
| 297 | }); |
| 298 | |
| 299 | if (ip.has_value() && port.has_value()) { |
| 300 | ConnectionInfo info; |
| 301 | info.ipAddress = *ip; |
| 302 | info.port = *port; |
| 303 | return std::make_optional<ConnectionInfo>(info); |
| 304 | } else { |
| 305 | return std::nullopt; |
| 306 | } |
| 307 | } |
| 308 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 309 | static std::vector<std::string> getVintfInstances(const std::string& interface) { |
| 310 | size_t lastDot = interface.rfind('.'); |
| 311 | if (lastDot == std::string::npos) { |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 312 | // This might be a package for native instance. |
| 313 | std::vector<std::string> ret; |
| 314 | (void)forEachManifest([&](const ManifestWithDescription& mwd) { |
| 315 | auto instances = mwd.manifest->getNativeInstances(interface); |
| 316 | ret.insert(ret.end(), instances.begin(), instances.end()); |
| 317 | return false; // continue |
| 318 | }); |
| 319 | // If found, return it without error log. |
| 320 | if (!ret.empty()) { |
| 321 | return ret; |
| 322 | } |
| 323 | |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 324 | ALOGE("VINTF interfaces require names in Java package format (e.g. some.package.foo.IFoo) " |
| 325 | "but got: %s", |
| 326 | interface.c_str()); |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 327 | return {}; |
| 328 | } |
| 329 | const std::string package = interface.substr(0, lastDot); |
| 330 | const std::string iface = interface.substr(lastDot+1); |
| 331 | |
| 332 | std::vector<std::string> ret; |
| 333 | (void)forEachManifest([&](const ManifestWithDescription& mwd) { |
| 334 | auto instances = mwd.manifest->getAidlInstances(package, iface); |
| 335 | ret.insert(ret.end(), instances.begin(), instances.end()); |
| 336 | return false; // continue |
| 337 | }); |
| 338 | |
| 339 | return ret; |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 340 | } |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 341 | |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 342 | static bool meetsDeclarationRequirements(const Access::CallingContext& ctx, |
| 343 | const sp<IBinder>& binder, const std::string& name) { |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 344 | if (!Stability::requiresVintfDeclaration(binder)) { |
| 345 | return true; |
| 346 | } |
| 347 | |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 348 | return isVintfDeclared(ctx, name); |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 349 | } |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 350 | #endif // !VENDORSERVICEMANAGER |
| 351 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 352 | ServiceManager::Service::~Service() { |
Steven Moreland | cb59156 | 2023-03-06 15:53:44 +0000 | [diff] [blame] | 353 | if (hasClients) { |
| 354 | // only expected to happen on process death, we don't store the service |
| 355 | // name this late (it's in the map that holds this service), but if it |
| 356 | // is happening, we might want to change 'unlinkToDeath' to explicitly |
| 357 | // clear this bit so that we can abort in other cases, where it would |
| 358 | // mean inconsistent logic in servicemanager (unexpected and tested, but |
| 359 | // the original lazy service impl here had that bug). |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 360 | ALOGW("A service was removed when there are clients"); |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Steven Moreland | d13f08b | 2019-11-18 14:23:09 -0800 | [diff] [blame] | 364 | ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) { |
Steven Moreland | 8d0c9a7 | 2020-04-30 16:51:56 -0700 | [diff] [blame] | 365 | // TODO(b/151696835): reenable performance hack when we solve bug, since with |
| 366 | // this hack and other fixes, it is unlikely we will see even an ephemeral |
| 367 | // failure when the manifest parse fails. The goal is that the manifest will |
| 368 | // be read incorrectly and cause the process trying to register a HAL to |
| 369 | // fail. If this is in fact an early boot kernel contention issue, then we |
| 370 | // will get no failure, and by its absence, be signalled to invest more |
| 371 | // effort in re-adding this performance hack. |
| 372 | // #ifndef VENDORSERVICEMANAGER |
| 373 | // // can process these at any times, don't want to delay first VINTF client |
| 374 | // std::thread([] { |
| 375 | // vintf::VintfObject::GetDeviceHalManifest(); |
| 376 | // vintf::VintfObject::GetFrameworkHalManifest(); |
| 377 | // }).detach(); |
| 378 | // #endif // !VENDORSERVICEMANAGER |
Steven Moreland | d13f08b | 2019-11-18 14:23:09 -0800 | [diff] [blame] | 379 | } |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 380 | ServiceManager::~ServiceManager() { |
| 381 | // this should only happen in tests |
| 382 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 383 | for (const auto& [name, callbacks] : mNameToRegistrationCallback) { |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 384 | CHECK(!callbacks.empty()) << name; |
| 385 | for (const auto& callback : callbacks) { |
| 386 | CHECK(callback != nullptr) << name; |
| 387 | } |
| 388 | } |
| 389 | |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 390 | for (const auto& [name, service] : mNameToService) { |
| 391 | CHECK(service.binder != nullptr) << name; |
| 392 | } |
| 393 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 394 | |
Alice Wang | 11da150 | 2024-07-25 12:03:22 +0000 | [diff] [blame] | 395 | Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) { |
| 396 | SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS( |
| 397 | PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str()))); |
| 398 | |
| 399 | *outBinder = tryGetBinder(name, true); |
| 400 | // returns ok regardless of result for legacy reasons |
| 401 | return Status::ok(); |
| 402 | } |
| 403 | |
| 404 | Status ServiceManager::getService2(const std::string& name, os::Service* outService) { |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 405 | SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS( |
| 406 | PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str()))); |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 407 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 408 | *outService = tryGetService(name, true); |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 409 | // returns ok regardless of result for legacy reasons |
| 410 | return Status::ok(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 413 | Status ServiceManager::checkService(const std::string& name, os::Service* outService) { |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 414 | SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS( |
| 415 | PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str()))); |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 416 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 417 | *outService = tryGetService(name, false); |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 418 | // returns ok regardless of result for legacy reasons |
| 419 | return Status::ok(); |
| 420 | } |
| 421 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 422 | os::Service ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) { |
| 423 | std::optional<std::string> accessorName; |
| 424 | #ifndef VENDORSERVICEMANAGER |
| 425 | accessorName = getVintfAccessorName(name); |
| 426 | #endif |
| 427 | if (accessorName.has_value()) { |
| 428 | auto ctx = mAccess->getCallingContext(); |
| 429 | if (!mAccess->canFind(ctx, name)) { |
| 430 | return os::Service::make<os::Service::Tag::accessor>(nullptr); |
| 431 | } |
| 432 | return os::Service::make<os::Service::Tag::accessor>( |
| 433 | tryGetBinder(*accessorName, startIfNotFound)); |
| 434 | } else { |
| 435 | return os::Service::make<os::Service::Tag::binder>(tryGetBinder(name, startIfNotFound)); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | sp<IBinder> ServiceManager::tryGetBinder(const std::string& name, bool startIfNotFound) { |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 440 | SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS( |
| 441 | PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str()))); |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 442 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 443 | auto ctx = mAccess->getCallingContext(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 444 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 445 | sp<IBinder> out; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 446 | Service* service = nullptr; |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 447 | if (auto it = mNameToService.find(name); it != mNameToService.end()) { |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 448 | service = &(it->second); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 449 | |
Steven Moreland | b9e1cbe | 2023-02-01 22:44:45 +0000 | [diff] [blame] | 450 | if (!service->allowIsolated && is_multiuser_uid_isolated(ctx.uid)) { |
Steven Moreland | bad7588 | 2023-06-16 20:59:06 +0000 | [diff] [blame] | 451 | LOG(WARNING) << "Isolated app with UID " << ctx.uid << " requested '" << name |
| 452 | << "', but the service is not allowed for isolated apps."; |
Steven Moreland | b9e1cbe | 2023-02-01 22:44:45 +0000 | [diff] [blame] | 453 | return nullptr; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 454 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 455 | out = service->binder; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 458 | if (!mAccess->canFind(ctx, name)) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 459 | return nullptr; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 462 | if (!out && startIfNotFound) { |
Steven Moreland | aa33e85 | 2023-05-10 16:42:15 +0000 | [diff] [blame] | 463 | tryStartService(ctx, name); |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 466 | if (out) { |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 467 | // Force onClients to get sent, and then make sure the timerfd won't clear it |
| 468 | // by setting guaranteeClient again. This logic could be simplified by using |
| 469 | // a time-based guarantee. However, forcing onClients(true) to get sent |
| 470 | // right here is always going to be important for processes serving multiple |
| 471 | // lazy interfaces. |
| 472 | service->guaranteeClient = true; |
| 473 | CHECK(handleServiceClientCallback(2 /* sm + transaction */, name, false)); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 474 | service->guaranteeClient = true; |
| 475 | } |
| 476 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 477 | return out; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 480 | bool isValidServiceName(const std::string& name) { |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 481 | SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS( |
| 482 | PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str()))); |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 483 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 484 | if (name.size() == 0) return false; |
| 485 | if (name.size() > 127) return false; |
| 486 | |
| 487 | for (char c : name) { |
Steven Moreland | bb7951d | 2019-08-20 16:58:25 -0700 | [diff] [blame] | 488 | if (c == '_' || c == '-' || c == '.' || c == '/') continue; |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 489 | if (c >= 'a' && c <= 'z') continue; |
| 490 | if (c >= 'A' && c <= 'Z') continue; |
| 491 | if (c >= '0' && c <= '9') continue; |
| 492 | return false; |
| 493 | } |
| 494 | |
| 495 | return true; |
| 496 | } |
| 497 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 498 | Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) { |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 499 | SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS( |
| 500 | PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str()))); |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 501 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 502 | auto ctx = mAccess->getCallingContext(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 503 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 504 | if (multiuser_get_app_id(ctx.uid) >= AID_APP) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 505 | return Status::fromExceptionCode(Status::EX_SECURITY, "App UIDs cannot add services."); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Alice Wang | d404e0f | 2024-07-26 16:18:09 +0000 | [diff] [blame^] | 508 | std::optional<std::string> accessorName; |
| 509 | if (auto status = canAddService(ctx, name, &accessorName); !status.isOk()) { |
| 510 | return status; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | if (binder == nullptr) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 514 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Null binder."); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 517 | if (!isValidServiceName(name)) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 518 | ALOGE("%s Invalid service name: %s", ctx.toDebugString().c_str(), name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 519 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name."); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 522 | #ifndef VENDORSERVICEMANAGER |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 523 | if (!meetsDeclarationRequirements(ctx, binder, name)) { |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 524 | // already logged |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 525 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "VINTF declaration error."); |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 526 | } |
| 527 | #endif // !VENDORSERVICEMANAGER |
| 528 | |
Devin Moore | 4e21def | 2023-02-24 21:54:14 +0000 | [diff] [blame] | 529 | if ((dumpPriority & DUMP_FLAG_PRIORITY_ALL) == 0) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 530 | ALOGW("%s Dump flag priority is not set when adding %s", ctx.toDebugString().c_str(), |
| 531 | name.c_str()); |
Devin Moore | 4e21def | 2023-02-24 21:54:14 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Steven Moreland | 88860b0 | 2019-08-12 14:24:14 -0700 | [diff] [blame] | 534 | // implicitly unlinked when the binder is removed |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 535 | if (binder->remoteBinder() != nullptr && |
| 536 | binder->linkToDeath(sp<ServiceManager>::fromExisting(this)) != OK) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 537 | ALOGE("%s Could not linkToDeath when adding %s", ctx.toDebugString().c_str(), name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 538 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't linkToDeath."); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 541 | auto it = mNameToService.find(name); |
Steven Moreland | 7957867 | 2023-04-27 19:38:00 +0000 | [diff] [blame] | 542 | bool prevClients = false; |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 543 | if (it != mNameToService.end()) { |
| 544 | const Service& existing = it->second; |
Steven Moreland | 7957867 | 2023-04-27 19:38:00 +0000 | [diff] [blame] | 545 | prevClients = existing.hasClients; |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 546 | |
| 547 | // We could do better than this because if the other service dies, it |
| 548 | // may not have an entry here. However, this case is unlikely. We are |
| 549 | // only trying to detect when two different services are accidentally installed. |
| 550 | |
| 551 | if (existing.ctx.uid != ctx.uid) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 552 | ALOGW("Service '%s' originally registered from UID %u but it is now being registered " |
| 553 | "from UID %u. Multiple instances installed?", |
| 554 | name.c_str(), existing.ctx.uid, ctx.uid); |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | if (existing.ctx.sid != ctx.sid) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 558 | ALOGW("Service '%s' originally registered from SID %s but it is now being registered " |
| 559 | "from SID %s. Multiple instances installed?", |
| 560 | name.c_str(), existing.ctx.sid.c_str(), ctx.sid.c_str()); |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 563 | ALOGI("Service '%s' originally registered from PID %d but it is being registered again " |
| 564 | "from PID %d. Bad state? Late death notification? Multiple instances installed?", |
| 565 | name.c_str(), existing.ctx.debugPid, ctx.debugPid); |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Devin Moore | 05ffe52 | 2020-08-06 13:58:29 -0700 | [diff] [blame] | 568 | // Overwrite the old service if it exists |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 569 | mNameToService[name] = Service{ |
| 570 | .binder = binder, |
| 571 | .allowIsolated = allowIsolated, |
| 572 | .dumpPriority = dumpPriority, |
Steven Moreland | 7957867 | 2023-04-27 19:38:00 +0000 | [diff] [blame] | 573 | .hasClients = prevClients, // see b/279898063, matters if existing callbacks |
Steven Moreland | efea66b | 2023-06-17 01:59:34 +0000 | [diff] [blame] | 574 | .guaranteeClient = false, |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 575 | .ctx = ctx, |
Devin Moore | 05ffe52 | 2020-08-06 13:58:29 -0700 | [diff] [blame] | 576 | }; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 577 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 578 | if (auto it = mNameToRegistrationCallback.find(name); it != mNameToRegistrationCallback.end()) { |
Steven Moreland | efea66b | 2023-06-17 01:59:34 +0000 | [diff] [blame] | 579 | // If someone is currently waiting on the service, notify the service that |
| 580 | // we're waiting and flush it to the service. |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 581 | mNameToService[name].guaranteeClient = true; |
| 582 | CHECK(handleServiceClientCallback(2 /* sm + transaction */, name, false)); |
| 583 | mNameToService[name].guaranteeClient = true; |
| 584 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 585 | for (const sp<IServiceCallback>& cb : it->second) { |
| 586 | // permission checked in registerForNotifications |
| 587 | cb->onRegistration(name, binder); |
| 588 | } |
| 589 | } |
| 590 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 591 | return Status::ok(); |
| 592 | } |
| 593 | |
| 594 | Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) { |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 595 | SM_PERFETTO_TRACE_FUNC(); |
| 596 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 597 | if (!mAccess->canList(mAccess->getCallingContext())) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 598 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied."); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | size_t toReserve = 0; |
| 602 | for (auto const& [name, service] : mNameToService) { |
| 603 | (void) name; |
| 604 | |
| 605 | if (service.dumpPriority & dumpPriority) ++toReserve; |
| 606 | } |
| 607 | |
| 608 | CHECK(outList->empty()); |
| 609 | |
| 610 | outList->reserve(toReserve); |
| 611 | for (auto const& [name, service] : mNameToService) { |
| 612 | (void) service; |
| 613 | |
| 614 | if (service.dumpPriority & dumpPriority) { |
| 615 | outList->push_back(name); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | return Status::ok(); |
| 620 | } |
| 621 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 622 | Status ServiceManager::registerForNotifications( |
| 623 | const std::string& name, const sp<IServiceCallback>& callback) { |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 624 | SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS( |
| 625 | PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str()))); |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 626 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 627 | auto ctx = mAccess->getCallingContext(); |
| 628 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 629 | // TODO(b/338541373): Implement the notification mechanism for services accessed via |
| 630 | // IAccessor. |
| 631 | std::optional<std::string> accessorName; |
| 632 | if (auto status = canFindService(ctx, name, &accessorName); !status.isOk()) { |
| 633 | return status; |
Steven Moreland | b9e1cbe | 2023-02-01 22:44:45 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | // note - we could allow isolated apps to get notifications if we |
| 637 | // keep track of isolated callbacks and non-isolated callbacks, but |
| 638 | // this is done since isolated apps shouldn't access lazy services |
| 639 | // so we should be able to use different APIs to keep things simple. |
| 640 | // Here, we disallow everything, because the service might not be |
| 641 | // registered yet. |
| 642 | if (is_multiuser_uid_isolated(ctx.uid)) { |
| 643 | return Status::fromExceptionCode(Status::EX_SECURITY, "isolated app"); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | if (!isValidServiceName(name)) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 647 | ALOGE("%s Invalid service name: %s", ctx.toDebugString().c_str(), name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 648 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name."); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | if (callback == nullptr) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 652 | return Status::fromExceptionCode(Status::EX_NULL_POINTER, "Null callback."); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 655 | if (OK != |
| 656 | IInterface::asBinder(callback)->linkToDeath( |
| 657 | sp<ServiceManager>::fromExisting(this))) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 658 | ALOGE("%s Could not linkToDeath when adding %s", ctx.toDebugString().c_str(), name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 659 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't link to death."); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 662 | mNameToRegistrationCallback[name].push_back(callback); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 663 | |
| 664 | if (auto it = mNameToService.find(name); it != mNameToService.end()) { |
| 665 | const sp<IBinder>& binder = it->second.binder; |
| 666 | |
| 667 | // never null if an entry exists |
| 668 | CHECK(binder != nullptr) << name; |
| 669 | callback->onRegistration(name, binder); |
| 670 | } |
| 671 | |
| 672 | return Status::ok(); |
| 673 | } |
| 674 | Status ServiceManager::unregisterForNotifications( |
| 675 | const std::string& name, const sp<IServiceCallback>& callback) { |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 676 | SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS( |
| 677 | PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str()))); |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 678 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 679 | auto ctx = mAccess->getCallingContext(); |
| 680 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 681 | std::optional<std::string> accessorName; |
| 682 | if (auto status = canFindService(ctx, name, &accessorName); !status.isOk()) { |
| 683 | return status; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | bool found = false; |
| 687 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 688 | auto it = mNameToRegistrationCallback.find(name); |
| 689 | if (it != mNameToRegistrationCallback.end()) { |
| 690 | removeRegistrationCallback(IInterface::asBinder(callback), &it, &found); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | if (!found) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 694 | ALOGE("%s Trying to unregister callback, but none exists %s", ctx.toDebugString().c_str(), |
| 695 | name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 696 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Nothing to unregister."); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | return Status::ok(); |
| 700 | } |
| 701 | |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 702 | Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) { |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 703 | SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS( |
| 704 | PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str()))); |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 705 | |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 706 | auto ctx = mAccess->getCallingContext(); |
| 707 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 708 | std::optional<std::string> accessorName; |
| 709 | if (auto status = canFindService(ctx, name, &accessorName); !status.isOk()) { |
| 710 | return status; |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | *outReturn = false; |
| 714 | |
| 715 | #ifndef VENDORSERVICEMANAGER |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 716 | *outReturn = isVintfDeclared(ctx, name); |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 717 | #endif |
| 718 | return Status::ok(); |
| 719 | } |
| 720 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 721 | binder::Status ServiceManager::getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) { |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 722 | SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS( |
| 723 | PERFETTO_TE_PROTO_FIELD_CSTR(kProtoInterfaceName, interface.c_str()))); |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 724 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 725 | auto ctx = mAccess->getCallingContext(); |
| 726 | |
| 727 | std::vector<std::string> allInstances; |
| 728 | #ifndef VENDORSERVICEMANAGER |
| 729 | allInstances = getVintfInstances(interface); |
| 730 | #endif |
| 731 | |
| 732 | outReturn->clear(); |
| 733 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 734 | std::optional<std::string> _accessorName; |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 735 | for (const std::string& instance : allInstances) { |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 736 | if (auto status = canFindService(ctx, interface + "/" + instance, &_accessorName); |
| 737 | status.isOk()) { |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 738 | outReturn->push_back(instance); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | if (outReturn->size() == 0 && allInstances.size() != 0) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 743 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied."); |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | return Status::ok(); |
| 747 | } |
| 748 | |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 749 | Status ServiceManager::updatableViaApex(const std::string& name, |
| 750 | std::optional<std::string>* outReturn) { |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 751 | SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS( |
| 752 | PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str()))); |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 753 | |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 754 | auto ctx = mAccess->getCallingContext(); |
| 755 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 756 | std::optional<std::string> _accessorName; |
| 757 | if (auto status = canFindService(ctx, name, &_accessorName); !status.isOk()) { |
| 758 | return status; |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | *outReturn = std::nullopt; |
| 762 | |
| 763 | #ifndef VENDORSERVICEMANAGER |
| 764 | *outReturn = getVintfUpdatableApex(name); |
| 765 | #endif |
| 766 | return Status::ok(); |
| 767 | } |
| 768 | |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 769 | Status ServiceManager::getUpdatableNames([[maybe_unused]] const std::string& apexName, |
| 770 | std::vector<std::string>* outReturn) { |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 771 | SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS( |
| 772 | PERFETTO_TE_PROTO_FIELD_CSTR(kProtoApexName, apexName.c_str()))); |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 773 | |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 774 | auto ctx = mAccess->getCallingContext(); |
| 775 | |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 776 | std::vector<std::string> apexUpdatableNames; |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 777 | #ifndef VENDORSERVICEMANAGER |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 778 | apexUpdatableNames = getVintfUpdatableNames(apexName); |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 779 | #endif |
| 780 | |
| 781 | outReturn->clear(); |
| 782 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 783 | std::optional<std::string> _accessorName; |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 784 | for (const std::string& name : apexUpdatableNames) { |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 785 | if (auto status = canFindService(ctx, name, &_accessorName); status.isOk()) { |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 786 | outReturn->push_back(name); |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Jooyung Han | 205e282 | 2023-12-19 16:59:39 +0900 | [diff] [blame] | 790 | if (outReturn->size() == 0 && apexUpdatableNames.size() != 0) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 791 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied."); |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 792 | } |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 793 | return Status::ok(); |
| 794 | } |
| 795 | |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 796 | Status ServiceManager::getConnectionInfo(const std::string& name, |
| 797 | std::optional<ConnectionInfo>* outReturn) { |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 798 | SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS( |
| 799 | PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str()))); |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 800 | |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 801 | auto ctx = mAccess->getCallingContext(); |
| 802 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 803 | std::optional<std::string> _accessorName; |
| 804 | if (auto status = canFindService(ctx, name, &_accessorName); !status.isOk()) { |
| 805 | return status; |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | *outReturn = std::nullopt; |
| 809 | |
| 810 | #ifndef VENDORSERVICEMANAGER |
| 811 | *outReturn = getVintfConnectionInfo(name); |
| 812 | #endif |
| 813 | return Status::ok(); |
| 814 | } |
| 815 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 816 | void ServiceManager::removeRegistrationCallback(const wp<IBinder>& who, |
| 817 | ServiceCallbackMap::iterator* it, |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 818 | bool* found) { |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 819 | SM_PERFETTO_TRACE_FUNC(); |
| 820 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 821 | std::vector<sp<IServiceCallback>>& listeners = (*it)->second; |
| 822 | |
| 823 | for (auto lit = listeners.begin(); lit != listeners.end();) { |
| 824 | if (IInterface::asBinder(*lit) == who) { |
| 825 | if(found) *found = true; |
| 826 | lit = listeners.erase(lit); |
| 827 | } else { |
| 828 | ++lit; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | if (listeners.empty()) { |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 833 | *it = mNameToRegistrationCallback.erase(*it); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 834 | } else { |
Jon Spivack | e223f08 | 2019-11-19 16:21:20 -0800 | [diff] [blame] | 835 | (*it)++; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 839 | void ServiceManager::binderDied(const wp<IBinder>& who) { |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 840 | SM_PERFETTO_TRACE_FUNC(); |
| 841 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 842 | for (auto it = mNameToService.begin(); it != mNameToService.end();) { |
| 843 | if (who == it->second.binder) { |
Steven Moreland | 7957867 | 2023-04-27 19:38:00 +0000 | [diff] [blame] | 844 | // TODO: currently, this entry contains the state also |
| 845 | // associated with mNameToClientCallback. If we allowed |
| 846 | // other processes to register client callbacks, we |
| 847 | // would have to preserve hasClients (perhaps moving |
| 848 | // that state into mNameToClientCallback, which is complicated |
| 849 | // because those callbacks are associated w/ particular binder |
| 850 | // objects, though they are indexed by name now, they may |
| 851 | // need to be indexed by binder at that point). |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 852 | it = mNameToService.erase(it); |
| 853 | } else { |
| 854 | ++it; |
| 855 | } |
| 856 | } |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 857 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 858 | for (auto it = mNameToRegistrationCallback.begin(); it != mNameToRegistrationCallback.end();) { |
| 859 | removeRegistrationCallback(who, &it, nullptr /*found*/); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 860 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 861 | |
| 862 | for (auto it = mNameToClientCallback.begin(); it != mNameToClientCallback.end();) { |
| 863 | removeClientCallback(who, &it); |
| 864 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Steven Moreland | aa33e85 | 2023-05-10 16:42:15 +0000 | [diff] [blame] | 867 | void ServiceManager::tryStartService(const Access::CallingContext& ctx, const std::string& name) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 868 | ALOGI("%s Since '%s' could not be found trying to start it as a lazy AIDL service. (if it's " |
| 869 | "not configured to be a lazy service, it may be stuck starting or still starting).", |
| 870 | ctx.toDebugString().c_str(), name.c_str()); |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 871 | |
| 872 | std::thread([=] { |
Steven Moreland | bfe9fba | 2021-04-27 18:39:57 +0000 | [diff] [blame] | 873 | if (!base::SetProperty("ctl.interface_start", "aidl/" + name)) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 874 | ALOGI("%s Tried to start aidl service %s as a lazy service, but was unable to. Usually " |
| 875 | "this happens when a service is not installed, but if the service is intended to " |
| 876 | "be used as a lazy service, then it may be configured incorrectly.", |
| 877 | ctx.toDebugString().c_str(), name.c_str()); |
Steven Moreland | bfe9fba | 2021-04-27 18:39:57 +0000 | [diff] [blame] | 878 | } |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 879 | }).detach(); |
| 880 | } |
| 881 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 882 | Status ServiceManager::registerClientCallback(const std::string& name, const sp<IBinder>& service, |
| 883 | const sp<IClientCallback>& cb) { |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 884 | SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS( |
| 885 | PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str()))); |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 886 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 887 | if (cb == nullptr) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 888 | return Status::fromExceptionCode(Status::EX_NULL_POINTER, "Callback null."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | auto ctx = mAccess->getCallingContext(); |
Alice Wang | d404e0f | 2024-07-26 16:18:09 +0000 | [diff] [blame^] | 892 | std::optional<std::string> accessorName; |
| 893 | if (auto status = canAddService(ctx, name, &accessorName); !status.isOk()) { |
| 894 | return status; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | auto serviceIt = mNameToService.find(name); |
| 898 | if (serviceIt == mNameToService.end()) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 899 | ALOGE("%s Could not add callback for nonexistent service: %s", ctx.toDebugString().c_str(), |
| 900 | name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 901 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Service doesn't exist."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 904 | if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 905 | ALOGW("%s Only a server can register for client callbacks (for %s)", |
| 906 | ctx.toDebugString().c_str(), name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 907 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION, |
| 908 | "Only service can register client callback for itself."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | if (serviceIt->second.binder != service) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 912 | ALOGW("%s Tried to register client callback for %s but a different service is registered " |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 913 | "under this name.", |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 914 | ctx.toDebugString().c_str(), name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 915 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Service mismatch."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 918 | if (OK != |
| 919 | IInterface::asBinder(cb)->linkToDeath(sp<ServiceManager>::fromExisting(this))) { |
David Duarte | 67d6528 | 2024-04-10 23:54:36 +0000 | [diff] [blame] | 920 | ALOGE("%s Could not linkToDeath when adding client callback for %s", |
| 921 | ctx.toDebugString().c_str(), name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 922 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't linkToDeath."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Steven Moreland | 7957867 | 2023-04-27 19:38:00 +0000 | [diff] [blame] | 925 | // WARNING: binderDied makes an assumption about this. If we open up client |
| 926 | // callbacks to other services, certain race conditions may lead to services |
| 927 | // getting extra client callback notifications. |
| 928 | // Make sure all callbacks have been told about a consistent state - b/278038751 |
Steven Moreland | 7bb4ab8 | 2023-04-13 20:29:33 +0000 | [diff] [blame] | 929 | if (serviceIt->second.hasClients) { |
| 930 | cb->onClients(service, true); |
| 931 | } |
| 932 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 933 | mNameToClientCallback[name].push_back(cb); |
| 934 | |
Steven Moreland | efea66b | 2023-06-17 01:59:34 +0000 | [diff] [blame] | 935 | // Flush updated info to client callbacks (especially if guaranteeClient |
| 936 | // and !hasClient, see b/285202885). We may or may not have clients at |
| 937 | // this point, so ignore the return value. |
| 938 | (void)handleServiceClientCallback(2 /* sm + transaction */, name, false); |
| 939 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 940 | return Status::ok(); |
| 941 | } |
| 942 | |
| 943 | void ServiceManager::removeClientCallback(const wp<IBinder>& who, |
| 944 | ClientCallbackMap::iterator* it) { |
| 945 | std::vector<sp<IClientCallback>>& listeners = (*it)->second; |
| 946 | |
| 947 | for (auto lit = listeners.begin(); lit != listeners.end();) { |
| 948 | if (IInterface::asBinder(*lit) == who) { |
| 949 | lit = listeners.erase(lit); |
| 950 | } else { |
| 951 | ++lit; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | if (listeners.empty()) { |
| 956 | *it = mNameToClientCallback.erase(*it); |
| 957 | } else { |
| 958 | (*it)++; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | ssize_t ServiceManager::Service::getNodeStrongRefCount() { |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 963 | sp<BpBinder> bpBinder = sp<BpBinder>::fromExisting(binder->remoteBinder()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 964 | if (bpBinder == nullptr) return -1; |
| 965 | |
Steven Moreland | e839388 | 2020-12-18 02:27:20 +0000 | [diff] [blame] | 966 | return ProcessState::self()->getStrongRefCountForNode(bpBinder); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | void ServiceManager::handleClientCallbacks() { |
| 970 | for (const auto& [name, service] : mNameToService) { |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 971 | handleServiceClientCallback(1 /* sm has one refcount */, name, true); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 975 | bool ServiceManager::handleServiceClientCallback(size_t knownClients, |
| 976 | const std::string& serviceName, |
| 977 | bool isCalledOnInterval) { |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 978 | auto serviceIt = mNameToService.find(serviceName); |
| 979 | if (serviceIt == mNameToService.end() || mNameToClientCallback.count(serviceName) < 1) { |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 980 | return true; // return we do have clients a.k.a. DON'T DO ANYTHING |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | Service& service = serviceIt->second; |
| 984 | ssize_t count = service.getNodeStrongRefCount(); |
| 985 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 986 | // binder driver doesn't support this feature, consider we have clients |
| 987 | if (count == -1) return true; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 988 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 989 | bool hasKernelReportedClients = static_cast<size_t>(count) > knownClients; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 990 | |
| 991 | if (service.guaranteeClient) { |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 992 | if (!service.hasClients && !hasKernelReportedClients) { |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 993 | sendClientCallbackNotifications(serviceName, true, |
| 994 | "service is guaranteed to be in use"); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | // guarantee is temporary |
| 998 | service.guaranteeClient = false; |
| 999 | } |
| 1000 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 1001 | // Regardless of this situation, we want to give this notification as soon as possible. |
| 1002 | // This way, we have a chance of preventing further thrashing. |
| 1003 | if (hasKernelReportedClients && !service.hasClients) { |
| 1004 | sendClientCallbackNotifications(serviceName, true, "we now have a record of a client"); |
| 1005 | } |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame] | 1006 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 1007 | // But limit rate of shutting down service. |
| 1008 | if (isCalledOnInterval) { |
| 1009 | if (!hasKernelReportedClients && service.hasClients) { |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 1010 | sendClientCallbackNotifications(serviceName, false, |
| 1011 | "we now have no record of a client"); |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 1012 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 1015 | // May be different than 'hasKernelReportedClients'. We intentionally delay |
| 1016 | // information about clients going away to reduce thrashing. |
| 1017 | return service.hasClients; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 1020 | void ServiceManager::sendClientCallbackNotifications(const std::string& serviceName, |
| 1021 | bool hasClients, const char* context) { |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1022 | auto serviceIt = mNameToService.find(serviceName); |
| 1023 | if (serviceIt == mNameToService.end()) { |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 1024 | ALOGW("sendClientCallbackNotifications could not find service %s when %s", |
| 1025 | serviceName.c_str(), context); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1026 | return; |
| 1027 | } |
| 1028 | Service& service = serviceIt->second; |
| 1029 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 1030 | CHECK_NE(hasClients, service.hasClients) << context; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1031 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 1032 | ALOGI("Notifying %s they %s (previously: %s) have clients when %s", serviceName.c_str(), |
| 1033 | hasClients ? "do" : "don't", service.hasClients ? "do" : "don't", context); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1034 | |
| 1035 | auto ccIt = mNameToClientCallback.find(serviceName); |
| 1036 | CHECK(ccIt != mNameToClientCallback.end()) |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 1037 | << "sendClientCallbackNotifications could not find callbacks for service when " |
| 1038 | << context; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1039 | |
| 1040 | for (const auto& callback : ccIt->second) { |
| 1041 | callback->onClients(service.binder, hasClients); |
| 1042 | } |
| 1043 | |
| 1044 | service.hasClients = hasClients; |
| 1045 | } |
| 1046 | |
| 1047 | Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IBinder>& binder) { |
Daniele Di Proietto | f7bdee4 | 2024-07-04 14:27:56 +0000 | [diff] [blame] | 1048 | SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS( |
| 1049 | PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str()))); |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 1050 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1051 | if (binder == nullptr) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 1052 | return Status::fromExceptionCode(Status::EX_NULL_POINTER, "Null service."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | auto ctx = mAccess->getCallingContext(); |
Alice Wang | d404e0f | 2024-07-26 16:18:09 +0000 | [diff] [blame^] | 1056 | std::optional<std::string> accessorName; |
| 1057 | if (auto status = canAddService(ctx, name, &accessorName); !status.isOk()) { |
| 1058 | return status; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | auto serviceIt = mNameToService.find(name); |
| 1062 | if (serviceIt == mNameToService.end()) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 1063 | ALOGW("%s Tried to unregister %s, but that service wasn't registered to begin with.", |
| 1064 | ctx.toDebugString().c_str(), name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 1065 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Service not registered."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 1068 | if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 1069 | ALOGW("%s Only a server can unregister itself (for %s)", ctx.toDebugString().c_str(), |
| 1070 | name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 1071 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION, |
| 1072 | "Service can only unregister itself."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | sp<IBinder> storedBinder = serviceIt->second.binder; |
| 1076 | |
| 1077 | if (binder != storedBinder) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 1078 | ALOGW("%s Tried to unregister %s, but a different service is registered under this name.", |
| 1079 | ctx.toDebugString().c_str(), name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 1080 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, |
| 1081 | "Different service registered under this name."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 1084 | // important because we don't have timer-based guarantees, we don't want to clear |
| 1085 | // this |
Jon Spivack | 0f18f2c | 2020-03-13 20:45:18 -0700 | [diff] [blame] | 1086 | if (serviceIt->second.guaranteeClient) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 1087 | ALOGI("%s Tried to unregister %s, but there is about to be a client.", |
| 1088 | ctx.toDebugString().c_str(), name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 1089 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, |
| 1090 | "Can't unregister, pending client."); |
Jon Spivack | 0f18f2c | 2020-03-13 20:45:18 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1093 | // - kernel driver will hold onto one refcount (during this transaction) |
| 1094 | // - servicemanager has a refcount (guaranteed by this transaction) |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 1095 | constexpr size_t kKnownClients = 2; |
| 1096 | |
| 1097 | if (handleServiceClientCallback(kKnownClients, name, false)) { |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 1098 | ALOGI("%s Tried to unregister %s, but there are clients.", ctx.toDebugString().c_str(), |
| 1099 | name.c_str()); |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 1100 | |
| 1101 | // Since we had a failed registration attempt, and the HIDL implementation of |
| 1102 | // delaying service shutdown for multiple periods wasn't ported here... this may |
| 1103 | // help reduce thrashing, but we should be able to remove it. |
Jon Spivack | 620d2dc | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 1104 | serviceIt->second.guaranteeClient = true; |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 1105 | |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 1106 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, |
| 1107 | "Can't unregister, known client."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 1110 | ALOGI("%s Unregistering %s", ctx.toDebugString().c_str(), name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 1111 | mNameToService.erase(name); |
| 1112 | |
| 1113 | return Status::ok(); |
| 1114 | } |
| 1115 | |
Alice Wang | d404e0f | 2024-07-26 16:18:09 +0000 | [diff] [blame^] | 1116 | Status ServiceManager::canAddService(const Access::CallingContext& ctx, const std::string& name, |
| 1117 | std::optional<std::string>* accessor) { |
| 1118 | if (!mAccess->canAdd(ctx, name)) { |
| 1119 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied for service."); |
| 1120 | } |
| 1121 | #ifndef VENDORSERVICEMANAGER |
| 1122 | *accessor = getVintfAccessorName(name); |
| 1123 | #endif |
| 1124 | if (accessor->has_value()) { |
| 1125 | if (!mAccess->canAdd(ctx, accessor->value())) { |
| 1126 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 1127 | "SELinux denied for the accessor of the service."); |
| 1128 | } |
| 1129 | } |
| 1130 | return Status::ok(); |
| 1131 | } |
| 1132 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 1133 | Status ServiceManager::canFindService(const Access::CallingContext& ctx, const std::string& name, |
| 1134 | std::optional<std::string>* accessor) { |
| 1135 | if (!mAccess->canFind(ctx, name)) { |
| 1136 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied for service."); |
| 1137 | } |
| 1138 | #ifndef VENDORSERVICEMANAGER |
| 1139 | *accessor = getVintfAccessorName(name); |
| 1140 | #endif |
| 1141 | if (accessor->has_value()) { |
| 1142 | if (!mAccess->canFind(ctx, accessor->value())) { |
| 1143 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 1144 | "SELinux denied for the accessor of the service."); |
| 1145 | } |
| 1146 | } |
| 1147 | return Status::ok(); |
| 1148 | } |
| 1149 | |
Steven Moreland | 3ea4327 | 2021-01-28 22:49:28 +0000 | [diff] [blame] | 1150 | Status ServiceManager::getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) { |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 1151 | SM_PERFETTO_TRACE_FUNC(); |
Steven Moreland | 3ea4327 | 2021-01-28 22:49:28 +0000 | [diff] [blame] | 1152 | if (!mAccess->canList(mAccess->getCallingContext())) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 1153 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied."); |
Steven Moreland | 3ea4327 | 2021-01-28 22:49:28 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | outReturn->reserve(mNameToService.size()); |
| 1157 | for (auto const& [name, service] : mNameToService) { |
| 1158 | ServiceDebugInfo info; |
| 1159 | info.name = name; |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 1160 | info.debugPid = service.ctx.debugPid; |
Steven Moreland | 3ea4327 | 2021-01-28 22:49:28 +0000 | [diff] [blame] | 1161 | |
| 1162 | outReturn->push_back(std::move(info)); |
| 1163 | } |
| 1164 | |
| 1165 | return Status::ok(); |
| 1166 | } |
| 1167 | |
Pawan Wagh | 243888e | 2022-09-20 19:37:35 +0000 | [diff] [blame] | 1168 | void ServiceManager::clear() { |
| 1169 | mNameToService.clear(); |
| 1170 | mNameToRegistrationCallback.clear(); |
| 1171 | mNameToClientCallback.clear(); |
| 1172 | } |
| 1173 | |
Steven Moreland | 8d0c9a7 | 2020-04-30 16:51:56 -0700 | [diff] [blame] | 1174 | } // namespace android |