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