blob: e107c83d14f15e398490d2b4eb9197e295f46eed [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}
Steven Morelanddea68bb2019-02-15 10:53:08 -080044AIBinder* AServiceManager_checkService(const char* instance) {
45 if (instance == nullptr) {
46 return nullptr;
47 }
48
49 sp<IServiceManager> sm = defaultServiceManager();
50 sp<IBinder> binder = sm->checkService(String16(instance));
51
52 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
53 AIBinder_incStrong(ret.get());
54 return ret.get();
55}
Steven Moreland2e87adc2018-08-20 19:47:00 -070056AIBinder* AServiceManager_getService(const char* instance) {
57 if (instance == nullptr) {
58 return nullptr;
59 }
60
61 sp<IServiceManager> sm = defaultServiceManager();
62 sp<IBinder> binder = sm->getService(String16(instance));
63
Steven Moreland94968952018-09-05 14:42:59 -070064 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
Steven Moreland71cddc32018-08-30 23:39:22 -070065 AIBinder_incStrong(ret.get());
66 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -070067}
Devin Moore6a9394f2020-07-31 17:24:51 -070068binder_status_t AServiceManager_registerLazyService(AIBinder* binder, const char* instance) {
69 if (binder == nullptr || instance == nullptr) {
70 return STATUS_UNEXPECTED_NULL;
71 }
72
73 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
74 status_t status = serviceRegistrar.registerService(binder->getBinder(), instance);
75
76 return PruneStatusT(status);
77}
78AIBinder* AServiceManager_waitForService(const char* instance) {
79 if (instance == nullptr) {
80 return nullptr;
81 }
82
83 sp<IServiceManager> sm = defaultServiceManager();
84 sp<IBinder> binder = sm->waitForService(String16(instance));
85
86 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
87 AIBinder_incStrong(ret.get());
88 return ret.get();
89}
Steven Moreland09b9d162022-11-18 21:15:35 +000090typedef void (*AServiceManager_onRegister)(const char* instance, AIBinder* registered,
91 void* cookie);
92
93struct AServiceManager_NotificationRegistration
94 : public IServiceManager::LocalRegistrationCallback {
95 std::mutex m;
96 const char* instance = nullptr;
97 void* cookie = nullptr;
98 AServiceManager_onRegister onRegister = nullptr;
99
100 virtual void onServiceRegistration(const String16& smInstance, const sp<IBinder>& binder) {
101 std::lock_guard<std::mutex> l(m);
102 if (onRegister == nullptr) return;
103
104 CHECK_EQ(String8(smInstance), instance);
105
106 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
107 AIBinder_incStrong(ret.get());
108
109 onRegister(instance, ret.get(), cookie);
110 }
111
112 void clear() {
113 std::lock_guard<std::mutex> l(m);
114 instance = nullptr;
115 cookie = nullptr;
116 onRegister = nullptr;
117 }
118};
119
120__attribute__((warn_unused_result)) AServiceManager_NotificationRegistration*
121AServiceManager_registerForServiceNotifications(const char* instance,
122 AServiceManager_onRegister onRegister,
123 void* cookie) {
124 CHECK_NE(instance, nullptr);
125 CHECK_NE(onRegister, nullptr) << instance;
126 // cookie can be nullptr
127
128 auto cb = sp<AServiceManager_NotificationRegistration>::make();
129 cb->instance = instance;
130 cb->onRegister = onRegister;
131 cb->cookie = cookie;
132
133 sp<IServiceManager> sm = defaultServiceManager();
134 if (status_t res = sm->registerForNotifications(String16(instance), cb); res != STATUS_OK) {
135 LOG(ERROR) << "Failed to register for service notifications for " << instance << ": "
136 << statusToString(res);
137 return nullptr;
138 }
139
140 cb->incStrong(nullptr);
141 return cb.get();
142}
143
144void AServiceManager_NotificationRegistration_delete(
145 AServiceManager_NotificationRegistration* notification) {
146 CHECK_NE(notification, nullptr);
147 notification->clear();
148 notification->decStrong(nullptr);
149}
150
Devin Moore6a9394f2020-07-31 17:24:51 -0700151bool AServiceManager_isDeclared(const char* instance) {
152 if (instance == nullptr) {
153 return false;
154 }
155
156 sp<IServiceManager> sm = defaultServiceManager();
157 return sm->isDeclared(String16(instance));
158}
Steven Morelandd687f332021-02-19 02:06:48 +0000159void AServiceManager_forEachDeclaredInstance(const char* interface, void* context,
160 void (*callback)(const char*, void*)) {
161 CHECK(interface != nullptr);
162 // context may be nullptr
163 CHECK(callback != nullptr);
164
165 sp<IServiceManager> sm = defaultServiceManager();
166 for (const String16& instance : sm->getDeclaredInstances(String16(interface))) {
167 callback(String8(instance).c_str(), context);
168 }
169}
Steven Morelandedd4e072021-04-21 00:27:29 +0000170bool AServiceManager_isUpdatableViaApex(const char* instance) {
171 if (instance == nullptr) {
172 return false;
173 }
174
175 sp<IServiceManager> sm = defaultServiceManager();
176 return sm->updatableViaApex(String16(instance)) != std::nullopt;
177}
Jooyung Han1abce692022-10-14 16:31:32 +0900178void AServiceManager_getUpdatableApexName(const char* instance, void* context,
179 void (*callback)(const char*, void*)) {
180 CHECK_NE(instance, nullptr);
181 // context may be nullptr
182 CHECK_NE(callback, nullptr);
183
184 sp<IServiceManager> sm = defaultServiceManager();
185 std::optional<String16> updatableViaApex = sm->updatableViaApex(String16(instance));
186 if (updatableViaApex.has_value()) {
187 callback(String8(updatableViaApex.value()).c_str(), context);
188 }
189}
Amos Bianchi9cf92762021-01-26 11:38:58 -0800190void AServiceManager_forceLazyServicesPersist(bool persist) {
191 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
192 serviceRegistrar.forcePersist(persist);
193}
194void AServiceManager_setActiveServicesCallback(bool (*callback)(bool, void*), void* context) {
195 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
196 std::function<bool(bool)> fn = [=](bool hasClients) -> bool {
197 return callback(hasClients, context);
198 };
199 serviceRegistrar.setActiveServicesCallback(fn);
200}
201bool AServiceManager_tryUnregister() {
202 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
203 return serviceRegistrar.tryUnregister();
204}
205void AServiceManager_reRegister() {
206 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
207 serviceRegistrar.reRegister();
Steven Morelandd687f332021-02-19 02:06:48 +0000208}