Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 <android/binder_manager.h> |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 18 | #include <binder/IServiceManager.h> |
| 19 | #include <binder/LazyServiceRegistrar.h> |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 20 | |
Tomasz Wasilczyk | 052c513 | 2024-06-21 15:45:26 -0700 | [diff] [blame] | 21 | #include "../Utils.h" |
Steven Moreland | 4d5ad49 | 2018-09-13 12:49:16 -0700 | [diff] [blame] | 22 | #include "ibinder_internal.h" |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 23 | #include "status_internal.h" |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 24 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 25 | using ::android::defaultServiceManager; |
| 26 | using ::android::IBinder; |
| 27 | using ::android::IServiceManager; |
| 28 | using ::android::sp; |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 29 | using ::android::status_t; |
Steven Moreland | 09b9d16 | 2022-11-18 21:15:35 +0000 | [diff] [blame] | 30 | using ::android::statusToString; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 31 | using ::android::String16; |
Steven Moreland | d687f33 | 2021-02-19 02:06:48 +0000 | [diff] [blame] | 32 | using ::android::String8; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 33 | |
Steven Moreland | d50b453 | 2020-08-31 21:45:00 +0000 | [diff] [blame] | 34 | binder_exception_t AServiceManager_addService(AIBinder* binder, const char* instance) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 35 | if (binder == nullptr || instance == nullptr) { |
Steven Moreland | d50b453 | 2020-08-31 21:45:00 +0000 | [diff] [blame] | 36 | return EX_ILLEGAL_ARGUMENT; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | sp<IServiceManager> sm = defaultServiceManager(); |
Steven Moreland | d50b453 | 2020-08-31 21:45:00 +0000 | [diff] [blame] | 40 | status_t exception = sm->addService(String16(instance), binder->getBinder()); |
| 41 | return PruneException(exception); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 42 | } |
Charlie Wang | eae87df | 2023-02-03 12:33:16 -0800 | [diff] [blame] | 43 | |
Charles Chen | de6036d | 2023-03-14 21:30:38 +0000 | [diff] [blame] | 44 | binder_exception_t AServiceManager_addServiceWithFlags(AIBinder* binder, const char* instance, |
| 45 | const AServiceManager_AddServiceFlag flags) { |
Charlie Wang | eae87df | 2023-02-03 12:33:16 -0800 | [diff] [blame] | 46 | if (binder == nullptr || instance == nullptr) { |
| 47 | return EX_ILLEGAL_ARGUMENT; |
| 48 | } |
| 49 | |
| 50 | sp<IServiceManager> sm = defaultServiceManager(); |
Charles Chen | 1310f15 | 2023-02-13 18:02:10 +0000 | [diff] [blame] | 51 | |
Charles Chen | de6036d | 2023-03-14 21:30:38 +0000 | [diff] [blame] | 52 | bool allowIsolated = flags & AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED; |
Henry Wang | a920922 | 2024-05-31 16:22:58 +0800 | [diff] [blame] | 53 | int dumpFlags = 0; |
| 54 | if (flags & AServiceManager_AddServiceFlag::ADD_SERVICE_DUMP_FLAG_PRIORITY_CRITICAL) { |
| 55 | dumpFlags |= IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL; |
| 56 | } |
| 57 | if (flags & AServiceManager_AddServiceFlag::ADD_SERVICE_DUMP_FLAG_PRIORITY_HIGH) { |
| 58 | dumpFlags |= IServiceManager::DUMP_FLAG_PRIORITY_HIGH; |
| 59 | } |
| 60 | if (flags & AServiceManager_AddServiceFlag::ADD_SERVICE_DUMP_FLAG_PRIORITY_NORMAL) { |
| 61 | dumpFlags |= IServiceManager::DUMP_FLAG_PRIORITY_NORMAL; |
| 62 | } |
| 63 | if (flags & AServiceManager_AddServiceFlag::ADD_SERVICE_DUMP_FLAG_PRIORITY_DEFAULT) { |
| 64 | dumpFlags |= IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT; |
| 65 | } |
| 66 | if (dumpFlags == 0) { |
| 67 | dumpFlags = IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT; |
| 68 | } |
| 69 | status_t exception = |
| 70 | sm->addService(String16(instance), binder->getBinder(), allowIsolated, dumpFlags); |
| 71 | |
Charlie Wang | eae87df | 2023-02-03 12:33:16 -0800 | [diff] [blame] | 72 | return PruneException(exception); |
| 73 | } |
| 74 | |
Steven Moreland | dea68bb | 2019-02-15 10:53:08 -0800 | [diff] [blame] | 75 | AIBinder* AServiceManager_checkService(const char* instance) { |
| 76 | if (instance == nullptr) { |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
| 80 | sp<IServiceManager> sm = defaultServiceManager(); |
| 81 | sp<IBinder> binder = sm->checkService(String16(instance)); |
| 82 | |
| 83 | sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder); |
| 84 | AIBinder_incStrong(ret.get()); |
| 85 | return ret.get(); |
| 86 | } |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 87 | AIBinder* AServiceManager_getService(const char* instance) { |
| 88 | if (instance == nullptr) { |
| 89 | return nullptr; |
| 90 | } |
| 91 | |
| 92 | sp<IServiceManager> sm = defaultServiceManager(); |
Tomasz Wasilczyk | 052c513 | 2024-06-21 15:45:26 -0700 | [diff] [blame] | 93 | LIBBINDER_IGNORE("-Wdeprecated-declarations") |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 94 | sp<IBinder> binder = sm->getService(String16(instance)); |
Tomasz Wasilczyk | 052c513 | 2024-06-21 15:45:26 -0700 | [diff] [blame] | 95 | LIBBINDER_IGNORE_END() |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 96 | |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 97 | sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder); |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 98 | AIBinder_incStrong(ret.get()); |
| 99 | return ret.get(); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 100 | } |
Devin Moore | 6a9394f | 2020-07-31 17:24:51 -0700 | [diff] [blame] | 101 | binder_status_t AServiceManager_registerLazyService(AIBinder* binder, const char* instance) { |
| 102 | if (binder == nullptr || instance == nullptr) { |
| 103 | return STATUS_UNEXPECTED_NULL; |
| 104 | } |
| 105 | |
| 106 | auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance(); |
| 107 | status_t status = serviceRegistrar.registerService(binder->getBinder(), instance); |
| 108 | |
| 109 | return PruneStatusT(status); |
| 110 | } |
| 111 | AIBinder* AServiceManager_waitForService(const char* instance) { |
| 112 | if (instance == nullptr) { |
| 113 | return nullptr; |
| 114 | } |
| 115 | |
| 116 | sp<IServiceManager> sm = defaultServiceManager(); |
| 117 | sp<IBinder> binder = sm->waitForService(String16(instance)); |
| 118 | |
| 119 | sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder); |
| 120 | AIBinder_incStrong(ret.get()); |
| 121 | return ret.get(); |
| 122 | } |
Steven Moreland | 09b9d16 | 2022-11-18 21:15:35 +0000 | [diff] [blame] | 123 | typedef void (*AServiceManager_onRegister)(const char* instance, AIBinder* registered, |
| 124 | void* cookie); |
| 125 | |
| 126 | struct AServiceManager_NotificationRegistration |
| 127 | : public IServiceManager::LocalRegistrationCallback { |
| 128 | std::mutex m; |
| 129 | const char* instance = nullptr; |
| 130 | void* cookie = nullptr; |
| 131 | AServiceManager_onRegister onRegister = nullptr; |
| 132 | |
| 133 | virtual void onServiceRegistration(const String16& smInstance, const sp<IBinder>& binder) { |
| 134 | std::lock_guard<std::mutex> l(m); |
| 135 | if (onRegister == nullptr) return; |
| 136 | |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 137 | LOG_ALWAYS_FATAL_IF(String8(smInstance) != instance, "onServiceRegistration: %s != %s", |
| 138 | String8(smInstance).c_str(), instance); |
Steven Moreland | 09b9d16 | 2022-11-18 21:15:35 +0000 | [diff] [blame] | 139 | |
| 140 | sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder); |
| 141 | AIBinder_incStrong(ret.get()); |
| 142 | |
| 143 | onRegister(instance, ret.get(), cookie); |
| 144 | } |
| 145 | |
| 146 | void clear() { |
| 147 | std::lock_guard<std::mutex> l(m); |
| 148 | instance = nullptr; |
| 149 | cookie = nullptr; |
| 150 | onRegister = nullptr; |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | __attribute__((warn_unused_result)) AServiceManager_NotificationRegistration* |
| 155 | AServiceManager_registerForServiceNotifications(const char* instance, |
| 156 | AServiceManager_onRegister onRegister, |
| 157 | void* cookie) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 158 | LOG_ALWAYS_FATAL_IF(instance == nullptr, "instance == nullptr"); |
| 159 | LOG_ALWAYS_FATAL_IF(onRegister == nullptr, "onRegister == nullptr for %s", instance); |
Steven Moreland | 09b9d16 | 2022-11-18 21:15:35 +0000 | [diff] [blame] | 160 | // cookie can be nullptr |
| 161 | |
| 162 | auto cb = sp<AServiceManager_NotificationRegistration>::make(); |
| 163 | cb->instance = instance; |
| 164 | cb->onRegister = onRegister; |
| 165 | cb->cookie = cookie; |
| 166 | |
| 167 | sp<IServiceManager> sm = defaultServiceManager(); |
| 168 | if (status_t res = sm->registerForNotifications(String16(instance), cb); res != STATUS_OK) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 169 | ALOGE("Failed to register for service notifications for %s: %s", instance, |
| 170 | statusToString(res).c_str()); |
Steven Moreland | 09b9d16 | 2022-11-18 21:15:35 +0000 | [diff] [blame] | 171 | return nullptr; |
| 172 | } |
| 173 | |
| 174 | cb->incStrong(nullptr); |
| 175 | return cb.get(); |
| 176 | } |
| 177 | |
| 178 | void AServiceManager_NotificationRegistration_delete( |
| 179 | AServiceManager_NotificationRegistration* notification) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 180 | LOG_ALWAYS_FATAL_IF(notification == nullptr, "notification == nullptr"); |
Steven Moreland | 09b9d16 | 2022-11-18 21:15:35 +0000 | [diff] [blame] | 181 | notification->clear(); |
| 182 | notification->decStrong(nullptr); |
| 183 | } |
| 184 | |
Devin Moore | 6a9394f | 2020-07-31 17:24:51 -0700 | [diff] [blame] | 185 | bool AServiceManager_isDeclared(const char* instance) { |
| 186 | if (instance == nullptr) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | sp<IServiceManager> sm = defaultServiceManager(); |
| 191 | return sm->isDeclared(String16(instance)); |
| 192 | } |
Steven Moreland | d687f33 | 2021-02-19 02:06:48 +0000 | [diff] [blame] | 193 | void AServiceManager_forEachDeclaredInstance(const char* interface, void* context, |
| 194 | void (*callback)(const char*, void*)) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 195 | LOG_ALWAYS_FATAL_IF(interface == nullptr, "interface == nullptr"); |
Steven Moreland | d687f33 | 2021-02-19 02:06:48 +0000 | [diff] [blame] | 196 | // context may be nullptr |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 197 | LOG_ALWAYS_FATAL_IF(callback == nullptr, "callback == nullptr"); |
Steven Moreland | d687f33 | 2021-02-19 02:06:48 +0000 | [diff] [blame] | 198 | |
| 199 | sp<IServiceManager> sm = defaultServiceManager(); |
| 200 | for (const String16& instance : sm->getDeclaredInstances(String16(interface))) { |
| 201 | callback(String8(instance).c_str(), context); |
| 202 | } |
| 203 | } |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 204 | bool AServiceManager_isUpdatableViaApex(const char* instance) { |
| 205 | if (instance == nullptr) { |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | sp<IServiceManager> sm = defaultServiceManager(); |
| 210 | return sm->updatableViaApex(String16(instance)) != std::nullopt; |
| 211 | } |
Jooyung Han | 1abce69 | 2022-10-14 16:31:32 +0900 | [diff] [blame] | 212 | void AServiceManager_getUpdatableApexName(const char* instance, void* context, |
| 213 | void (*callback)(const char*, void*)) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 214 | LOG_ALWAYS_FATAL_IF(instance == nullptr, "instance == nullptr"); |
Jooyung Han | 1abce69 | 2022-10-14 16:31:32 +0900 | [diff] [blame] | 215 | // context may be nullptr |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 216 | LOG_ALWAYS_FATAL_IF(callback == nullptr, "callback == nullptr"); |
Jooyung Han | 1abce69 | 2022-10-14 16:31:32 +0900 | [diff] [blame] | 217 | |
| 218 | sp<IServiceManager> sm = defaultServiceManager(); |
| 219 | std::optional<String16> updatableViaApex = sm->updatableViaApex(String16(instance)); |
| 220 | if (updatableViaApex.has_value()) { |
| 221 | callback(String8(updatableViaApex.value()).c_str(), context); |
| 222 | } |
| 223 | } |
Jooyung Han | 75fc06f | 2024-02-03 03:56:59 +0900 | [diff] [blame] | 224 | void* AServiceManager_openDeclaredPassthroughHal(const char* interface, const char* instance, |
| 225 | int flag) { |
| 226 | LOG_ALWAYS_FATAL_IF(interface == nullptr, "interface == nullptr"); |
| 227 | LOG_ALWAYS_FATAL_IF(instance == nullptr, "instance == nullptr"); |
| 228 | |
| 229 | return openDeclaredPassthroughHal(String16(interface), String16(instance), flag); |
| 230 | } |
Amos Bianchi | 9cf9276 | 2021-01-26 11:38:58 -0800 | [diff] [blame] | 231 | void AServiceManager_forceLazyServicesPersist(bool persist) { |
| 232 | auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance(); |
| 233 | serviceRegistrar.forcePersist(persist); |
| 234 | } |
| 235 | void AServiceManager_setActiveServicesCallback(bool (*callback)(bool, void*), void* context) { |
| 236 | auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance(); |
| 237 | std::function<bool(bool)> fn = [=](bool hasClients) -> bool { |
| 238 | return callback(hasClients, context); |
| 239 | }; |
| 240 | serviceRegistrar.setActiveServicesCallback(fn); |
| 241 | } |
| 242 | bool AServiceManager_tryUnregister() { |
| 243 | auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance(); |
| 244 | return serviceRegistrar.tryUnregister(); |
| 245 | } |
| 246 | void AServiceManager_reRegister() { |
| 247 | auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance(); |
| 248 | serviceRegistrar.reRegister(); |
Steven Moreland | d687f33 | 2021-02-19 02:06:48 +0000 | [diff] [blame] | 249 | } |