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