blob: 259cced32bb398522bc9c686f4347925791056af [file] [log] [blame]
Steven Moreland2e87adc2018-08-20 19:47:00 -07001/*
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 Wasilczyke3de8802023-11-01 11:05:27 -070018#include <binder/IServiceManager.h>
19#include <binder/LazyServiceRegistrar.h>
Steven Moreland5d62e442018-09-13 15:01:02 -070020
Tomasz Wasilczyk370408e2024-06-21 15:45:26 -070021#include "../Utils.h"
Steven Moreland4d5ad492018-09-13 12:49:16 -070022#include "ibinder_internal.h"
Steven Moreland5d62e442018-09-13 15:01:02 -070023#include "status_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070024
Steven Moreland2e87adc2018-08-20 19:47:00 -070025using ::android::defaultServiceManager;
26using ::android::IBinder;
27using ::android::IServiceManager;
28using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070029using ::android::status_t;
Steven Moreland09b9d162022-11-18 21:15:35 +000030using ::android::statusToString;
Steven Moreland2e87adc2018-08-20 19:47:00 -070031using ::android::String16;
Steven Morelandd687f332021-02-19 02:06:48 +000032using ::android::String8;
Steven Moreland2e87adc2018-08-20 19:47:00 -070033
Steven Morelandd50b4532020-08-31 21:45:00 +000034binder_exception_t AServiceManager_addService(AIBinder* binder, const char* instance) {
Steven Moreland2e87adc2018-08-20 19:47:00 -070035 if (binder == nullptr || instance == nullptr) {
Steven Morelandd50b4532020-08-31 21:45:00 +000036 return EX_ILLEGAL_ARGUMENT;
Steven Moreland2e87adc2018-08-20 19:47:00 -070037 }
38
39 sp<IServiceManager> sm = defaultServiceManager();
Steven Morelandd50b4532020-08-31 21:45:00 +000040 status_t exception = sm->addService(String16(instance), binder->getBinder());
41 return PruneException(exception);
Steven Moreland2e87adc2018-08-20 19:47:00 -070042}
Charlie Wangeae87df2023-02-03 12:33:16 -080043
Charles Chende6036d2023-03-14 21:30:38 +000044binder_exception_t AServiceManager_addServiceWithFlags(AIBinder* binder, const char* instance,
45 const AServiceManager_AddServiceFlag flags) {
Charlie Wangeae87df2023-02-03 12:33:16 -080046 if (binder == nullptr || instance == nullptr) {
47 return EX_ILLEGAL_ARGUMENT;
48 }
49
50 sp<IServiceManager> sm = defaultServiceManager();
Charles Chen1310f152023-02-13 18:02:10 +000051
Charles Chende6036d2023-03-14 21:30:38 +000052 bool allowIsolated = flags & AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED;
Pechetty Sravani721ca9e2024-06-06 03:02:08 +000053 status_t exception = sm->addService(String16(instance), binder->getBinder(), allowIsolated);
Charlie Wangeae87df2023-02-03 12:33:16 -080054 return PruneException(exception);
55}
56
Steven Morelanddea68bb2019-02-15 10:53:08 -080057AIBinder* 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 Moreland2e87adc2018-08-20 19:47:00 -070069AIBinder* AServiceManager_getService(const char* instance) {
70 if (instance == nullptr) {
71 return nullptr;
72 }
73
74 sp<IServiceManager> sm = defaultServiceManager();
Tomasz Wasilczyk370408e2024-06-21 15:45:26 -070075 LIBBINDER_IGNORE("-Wdeprecated-declarations")
Steven Moreland2e87adc2018-08-20 19:47:00 -070076 sp<IBinder> binder = sm->getService(String16(instance));
Tomasz Wasilczyk370408e2024-06-21 15:45:26 -070077 LIBBINDER_IGNORE_END()
Steven Moreland2e87adc2018-08-20 19:47:00 -070078
Steven Moreland94968952018-09-05 14:42:59 -070079 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
Steven Moreland71cddc32018-08-30 23:39:22 -070080 AIBinder_incStrong(ret.get());
81 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -070082}
Devin Moore6a9394f2020-07-31 17:24:51 -070083binder_status_t AServiceManager_registerLazyService(AIBinder* binder, const char* instance) {
84 if (binder == nullptr || instance == nullptr) {
85 return STATUS_UNEXPECTED_NULL;
86 }
87
88 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
89 status_t status = serviceRegistrar.registerService(binder->getBinder(), instance);
90
91 return PruneStatusT(status);
92}
93AIBinder* AServiceManager_waitForService(const char* instance) {
94 if (instance == nullptr) {
95 return nullptr;
96 }
97
98 sp<IServiceManager> sm = defaultServiceManager();
99 sp<IBinder> binder = sm->waitForService(String16(instance));
100
101 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
102 AIBinder_incStrong(ret.get());
103 return ret.get();
104}
Steven Moreland09b9d162022-11-18 21:15:35 +0000105typedef void (*AServiceManager_onRegister)(const char* instance, AIBinder* registered,
106 void* cookie);
107
108struct AServiceManager_NotificationRegistration
109 : public IServiceManager::LocalRegistrationCallback {
110 std::mutex m;
111 const char* instance = nullptr;
112 void* cookie = nullptr;
113 AServiceManager_onRegister onRegister = nullptr;
114
115 virtual void onServiceRegistration(const String16& smInstance, const sp<IBinder>& binder) {
116 std::lock_guard<std::mutex> l(m);
117 if (onRegister == nullptr) return;
118
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700119 LOG_ALWAYS_FATAL_IF(String8(smInstance) != instance, "onServiceRegistration: %s != %s",
120 String8(smInstance).c_str(), instance);
Steven Moreland09b9d162022-11-18 21:15:35 +0000121
122 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
123 AIBinder_incStrong(ret.get());
124
125 onRegister(instance, ret.get(), cookie);
126 }
127
128 void clear() {
129 std::lock_guard<std::mutex> l(m);
130 instance = nullptr;
131 cookie = nullptr;
132 onRegister = nullptr;
133 }
134};
135
136__attribute__((warn_unused_result)) AServiceManager_NotificationRegistration*
137AServiceManager_registerForServiceNotifications(const char* instance,
138 AServiceManager_onRegister onRegister,
139 void* cookie) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700140 LOG_ALWAYS_FATAL_IF(instance == nullptr, "instance == nullptr");
141 LOG_ALWAYS_FATAL_IF(onRegister == nullptr, "onRegister == nullptr for %s", instance);
Steven Moreland09b9d162022-11-18 21:15:35 +0000142 // cookie can be nullptr
143
144 auto cb = sp<AServiceManager_NotificationRegistration>::make();
145 cb->instance = instance;
146 cb->onRegister = onRegister;
147 cb->cookie = cookie;
148
149 sp<IServiceManager> sm = defaultServiceManager();
150 if (status_t res = sm->registerForNotifications(String16(instance), cb); res != STATUS_OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700151 ALOGE("Failed to register for service notifications for %s: %s", instance,
152 statusToString(res).c_str());
Steven Moreland09b9d162022-11-18 21:15:35 +0000153 return nullptr;
154 }
155
156 cb->incStrong(nullptr);
157 return cb.get();
158}
159
160void AServiceManager_NotificationRegistration_delete(
161 AServiceManager_NotificationRegistration* notification) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700162 LOG_ALWAYS_FATAL_IF(notification == nullptr, "notification == nullptr");
Steven Moreland09b9d162022-11-18 21:15:35 +0000163 notification->clear();
164 notification->decStrong(nullptr);
165}
166
Devin Moore6a9394f2020-07-31 17:24:51 -0700167bool AServiceManager_isDeclared(const char* instance) {
168 if (instance == nullptr) {
169 return false;
170 }
171
172 sp<IServiceManager> sm = defaultServiceManager();
173 return sm->isDeclared(String16(instance));
174}
Steven Morelandd687f332021-02-19 02:06:48 +0000175void AServiceManager_forEachDeclaredInstance(const char* interface, void* context,
176 void (*callback)(const char*, void*)) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700177 LOG_ALWAYS_FATAL_IF(interface == nullptr, "interface == nullptr");
Steven Morelandd687f332021-02-19 02:06:48 +0000178 // context may be nullptr
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700179 LOG_ALWAYS_FATAL_IF(callback == nullptr, "callback == nullptr");
Steven Morelandd687f332021-02-19 02:06:48 +0000180
181 sp<IServiceManager> sm = defaultServiceManager();
182 for (const String16& instance : sm->getDeclaredInstances(String16(interface))) {
183 callback(String8(instance).c_str(), context);
184 }
185}
Steven Morelandedd4e072021-04-21 00:27:29 +0000186bool AServiceManager_isUpdatableViaApex(const char* instance) {
187 if (instance == nullptr) {
188 return false;
189 }
190
191 sp<IServiceManager> sm = defaultServiceManager();
192 return sm->updatableViaApex(String16(instance)) != std::nullopt;
193}
Jooyung Han1abce692022-10-14 16:31:32 +0900194void AServiceManager_getUpdatableApexName(const char* instance, void* context,
195 void (*callback)(const char*, void*)) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700196 LOG_ALWAYS_FATAL_IF(instance == nullptr, "instance == nullptr");
Jooyung Han1abce692022-10-14 16:31:32 +0900197 // context may be nullptr
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700198 LOG_ALWAYS_FATAL_IF(callback == nullptr, "callback == nullptr");
Jooyung Han1abce692022-10-14 16:31:32 +0900199
200 sp<IServiceManager> sm = defaultServiceManager();
201 std::optional<String16> updatableViaApex = sm->updatableViaApex(String16(instance));
202 if (updatableViaApex.has_value()) {
203 callback(String8(updatableViaApex.value()).c_str(), context);
204 }
205}
Jooyung Han75fc06f2024-02-03 03:56:59 +0900206void* AServiceManager_openDeclaredPassthroughHal(const char* interface, const char* instance,
207 int flag) {
208 LOG_ALWAYS_FATAL_IF(interface == nullptr, "interface == nullptr");
209 LOG_ALWAYS_FATAL_IF(instance == nullptr, "instance == nullptr");
210
211 return openDeclaredPassthroughHal(String16(interface), String16(instance), flag);
212}
Amos Bianchi9cf92762021-01-26 11:38:58 -0800213void AServiceManager_forceLazyServicesPersist(bool persist) {
214 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
215 serviceRegistrar.forcePersist(persist);
216}
217void AServiceManager_setActiveServicesCallback(bool (*callback)(bool, void*), void* context) {
218 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
219 std::function<bool(bool)> fn = [=](bool hasClients) -> bool {
220 return callback(hasClients, context);
221 };
222 serviceRegistrar.setActiveServicesCallback(fn);
223}
224bool AServiceManager_tryUnregister() {
225 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
226 return serviceRegistrar.tryUnregister();
227}
228void AServiceManager_reRegister() {
229 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
230 serviceRegistrar.reRegister();
Steven Morelandd687f332021-02-19 02:06:48 +0000231}