blob: 29777866e228784440fb1120a561951a1644d157 [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>
Steven Moreland5d62e442018-09-13 15:01:02 -070018
Steven Moreland4d5ad492018-09-13 12:49:16 -070019#include "ibinder_internal.h"
Steven Moreland5d62e442018-09-13 15:01:02 -070020#include "status_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070021
Steven Morelandd687f332021-02-19 02:06:48 +000022#include <android-base/logging.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070023#include <binder/IServiceManager.h>
Devin Moore6a9394f2020-07-31 17:24:51 -070024#include <binder/LazyServiceRegistrar.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070025
26using ::android::defaultServiceManager;
27using ::android::IBinder;
28using ::android::IServiceManager;
29using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070030using ::android::status_t;
Steven Moreland09b9d162022-11-18 21:15:35 +000031using ::android::statusToString;
Steven Moreland2e87adc2018-08-20 19:47:00 -070032using ::android::String16;
Steven Morelandd687f332021-02-19 02:06:48 +000033using ::android::String8;
Steven Moreland2e87adc2018-08-20 19:47:00 -070034
Steven Morelandd50b4532020-08-31 21:45:00 +000035binder_exception_t AServiceManager_addService(AIBinder* binder, const char* instance) {
Steven Moreland2e87adc2018-08-20 19:47:00 -070036 if (binder == nullptr || instance == nullptr) {
Steven Morelandd50b4532020-08-31 21:45:00 +000037 return EX_ILLEGAL_ARGUMENT;
Steven Moreland2e87adc2018-08-20 19:47:00 -070038 }
39
40 sp<IServiceManager> sm = defaultServiceManager();
Steven Morelandd50b4532020-08-31 21:45:00 +000041 status_t exception = sm->addService(String16(instance), binder->getBinder());
42 return PruneException(exception);
Steven Moreland2e87adc2018-08-20 19:47:00 -070043}
Charlie Wangeae87df2023-02-03 12:33:16 -080044
Charles Chende6036d2023-03-14 21:30:38 +000045binder_exception_t AServiceManager_addServiceWithFlags(AIBinder* binder, const char* instance,
46 const AServiceManager_AddServiceFlag flags) {
Charlie Wangeae87df2023-02-03 12:33:16 -080047 if (binder == nullptr || instance == nullptr) {
48 return EX_ILLEGAL_ARGUMENT;
49 }
50
51 sp<IServiceManager> sm = defaultServiceManager();
Charles Chen1310f152023-02-13 18:02:10 +000052
Charles Chende6036d2023-03-14 21:30:38 +000053 bool allowIsolated = flags & AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED;
Charlie Wangeae87df2023-02-03 12:33:16 -080054 status_t exception = sm->addService(String16(instance), binder->getBinder(), allowIsolated);
55 return PruneException(exception);
56}
57
Steven Morelanddea68bb2019-02-15 10:53:08 -080058AIBinder* 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 Moreland2e87adc2018-08-20 19:47:00 -070070AIBinder* 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 Moreland94968952018-09-05 14:42:59 -070078 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
Steven Moreland71cddc32018-08-30 23:39:22 -070079 AIBinder_incStrong(ret.get());
80 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -070081}
Devin Moore6a9394f2020-07-31 17:24:51 -070082binder_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}
92AIBinder* 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 Moreland09b9d162022-11-18 21:15:35 +0000104typedef void (*AServiceManager_onRegister)(const char* instance, AIBinder* registered,
105 void* cookie);
106
107struct 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*
135AServiceManager_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
158void AServiceManager_NotificationRegistration_delete(
159 AServiceManager_NotificationRegistration* notification) {
160 CHECK_NE(notification, nullptr);
161 notification->clear();
162 notification->decStrong(nullptr);
163}
164
Devin Moore6a9394f2020-07-31 17:24:51 -0700165bool 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 Morelandd687f332021-02-19 02:06:48 +0000173void 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 Morelandedd4e072021-04-21 00:27:29 +0000184bool 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 Han1abce692022-10-14 16:31:32 +0900192void 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 Bianchi9cf92762021-01-26 11:38:58 -0800204void AServiceManager_forceLazyServicesPersist(bool persist) {
205 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
206 serviceRegistrar.forcePersist(persist);
207}
208void 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}
215bool AServiceManager_tryUnregister() {
216 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
217 return serviceRegistrar.tryUnregister();
218}
219void AServiceManager_reRegister() {
220 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
221 serviceRegistrar.reRegister();
Steven Morelandd687f332021-02-19 02:06:48 +0000222}