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