blob: 3bfdc59ec26dc3007cd0721b92a8b2c583dcaf82 [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
Steven Moreland4d5ad492018-09-13 12:49:16 -070021#include "ibinder_internal.h"
Steven Moreland5d62e442018-09-13 15:01:02 -070022#include "status_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070023
Steven Moreland2e87adc2018-08-20 19:47:00 -070024using ::android::defaultServiceManager;
25using ::android::IBinder;
26using ::android::IServiceManager;
27using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070028using ::android::status_t;
Steven Moreland09b9d162022-11-18 21:15:35 +000029using ::android::statusToString;
Steven Moreland2e87adc2018-08-20 19:47:00 -070030using ::android::String16;
Steven Morelandd687f332021-02-19 02:06:48 +000031using ::android::String8;
Steven Moreland2e87adc2018-08-20 19:47:00 -070032
Steven Morelandd50b4532020-08-31 21:45:00 +000033binder_exception_t AServiceManager_addService(AIBinder* binder, const char* instance) {
Steven Moreland2e87adc2018-08-20 19:47:00 -070034 if (binder == nullptr || instance == nullptr) {
Steven Morelandd50b4532020-08-31 21:45:00 +000035 return EX_ILLEGAL_ARGUMENT;
Steven Moreland2e87adc2018-08-20 19:47:00 -070036 }
37
38 sp<IServiceManager> sm = defaultServiceManager();
Steven Morelandd50b4532020-08-31 21:45:00 +000039 status_t exception = sm->addService(String16(instance), binder->getBinder());
40 return PruneException(exception);
Steven Moreland2e87adc2018-08-20 19:47:00 -070041}
Charlie Wangeae87df2023-02-03 12:33:16 -080042
Charles Chende6036d2023-03-14 21:30:38 +000043binder_exception_t AServiceManager_addServiceWithFlags(AIBinder* binder, const char* instance,
44 const AServiceManager_AddServiceFlag flags) {
Charlie Wangeae87df2023-02-03 12:33:16 -080045 if (binder == nullptr || instance == nullptr) {
46 return EX_ILLEGAL_ARGUMENT;
47 }
48
49 sp<IServiceManager> sm = defaultServiceManager();
Charles Chen1310f152023-02-13 18:02:10 +000050
Charles Chende6036d2023-03-14 21:30:38 +000051 bool allowIsolated = flags & AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED;
Charlie Wangeae87df2023-02-03 12:33:16 -080052 status_t exception = sm->addService(String16(instance), binder->getBinder(), allowIsolated);
53 return PruneException(exception);
54}
55
Steven Morelanddea68bb2019-02-15 10:53:08 -080056AIBinder* AServiceManager_checkService(const char* instance) {
57 if (instance == nullptr) {
58 return nullptr;
59 }
60
61 sp<IServiceManager> sm = defaultServiceManager();
62 sp<IBinder> binder = sm->checkService(String16(instance));
63
64 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
65 AIBinder_incStrong(ret.get());
66 return ret.get();
67}
Steven Moreland2e87adc2018-08-20 19:47:00 -070068AIBinder* AServiceManager_getService(const char* instance) {
69 if (instance == nullptr) {
70 return nullptr;
71 }
72
73 sp<IServiceManager> sm = defaultServiceManager();
74 sp<IBinder> binder = sm->getService(String16(instance));
75
Steven Moreland94968952018-09-05 14:42:59 -070076 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
Steven Moreland71cddc32018-08-30 23:39:22 -070077 AIBinder_incStrong(ret.get());
78 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -070079}
Devin Moore6a9394f2020-07-31 17:24:51 -070080binder_status_t AServiceManager_registerLazyService(AIBinder* binder, const char* instance) {
81 if (binder == nullptr || instance == nullptr) {
82 return STATUS_UNEXPECTED_NULL;
83 }
84
85 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
86 status_t status = serviceRegistrar.registerService(binder->getBinder(), instance);
87
88 return PruneStatusT(status);
89}
90AIBinder* AServiceManager_waitForService(const char* instance) {
91 if (instance == nullptr) {
92 return nullptr;
93 }
94
95 sp<IServiceManager> sm = defaultServiceManager();
96 sp<IBinder> binder = sm->waitForService(String16(instance));
97
98 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
99 AIBinder_incStrong(ret.get());
100 return ret.get();
101}
Steven Moreland09b9d162022-11-18 21:15:35 +0000102typedef void (*AServiceManager_onRegister)(const char* instance, AIBinder* registered,
103 void* cookie);
104
105struct AServiceManager_NotificationRegistration
106 : public IServiceManager::LocalRegistrationCallback {
107 std::mutex m;
108 const char* instance = nullptr;
109 void* cookie = nullptr;
110 AServiceManager_onRegister onRegister = nullptr;
111
112 virtual void onServiceRegistration(const String16& smInstance, const sp<IBinder>& binder) {
113 std::lock_guard<std::mutex> l(m);
114 if (onRegister == nullptr) return;
115
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700116 LOG_ALWAYS_FATAL_IF(String8(smInstance) != instance, "onServiceRegistration: %s != %s",
117 String8(smInstance).c_str(), instance);
Steven Moreland09b9d162022-11-18 21:15:35 +0000118
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*
134AServiceManager_registerForServiceNotifications(const char* instance,
135 AServiceManager_onRegister onRegister,
136 void* cookie) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700137 LOG_ALWAYS_FATAL_IF(instance == nullptr, "instance == nullptr");
138 LOG_ALWAYS_FATAL_IF(onRegister == nullptr, "onRegister == nullptr for %s", instance);
Steven Moreland09b9d162022-11-18 21:15:35 +0000139 // 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) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700148 ALOGE("Failed to register for service notifications for %s: %s", instance,
149 statusToString(res).c_str());
Steven Moreland09b9d162022-11-18 21:15:35 +0000150 return nullptr;
151 }
152
153 cb->incStrong(nullptr);
154 return cb.get();
155}
156
157void AServiceManager_NotificationRegistration_delete(
158 AServiceManager_NotificationRegistration* notification) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700159 LOG_ALWAYS_FATAL_IF(notification == nullptr, "notification == nullptr");
Steven Moreland09b9d162022-11-18 21:15:35 +0000160 notification->clear();
161 notification->decStrong(nullptr);
162}
163
Devin Moore6a9394f2020-07-31 17:24:51 -0700164bool 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 Morelandd687f332021-02-19 02:06:48 +0000172void AServiceManager_forEachDeclaredInstance(const char* interface, void* context,
173 void (*callback)(const char*, void*)) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700174 LOG_ALWAYS_FATAL_IF(interface == nullptr, "interface == nullptr");
Steven Morelandd687f332021-02-19 02:06:48 +0000175 // context may be nullptr
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700176 LOG_ALWAYS_FATAL_IF(callback == nullptr, "callback == nullptr");
Steven Morelandd687f332021-02-19 02:06:48 +0000177
178 sp<IServiceManager> sm = defaultServiceManager();
179 for (const String16& instance : sm->getDeclaredInstances(String16(interface))) {
180 callback(String8(instance).c_str(), context);
181 }
182}
Steven Morelandedd4e072021-04-21 00:27:29 +0000183bool 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 Han1abce692022-10-14 16:31:32 +0900191void AServiceManager_getUpdatableApexName(const char* instance, void* context,
192 void (*callback)(const char*, void*)) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700193 LOG_ALWAYS_FATAL_IF(instance == nullptr, "instance == nullptr");
Jooyung Han1abce692022-10-14 16:31:32 +0900194 // context may be nullptr
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700195 LOG_ALWAYS_FATAL_IF(callback == nullptr, "callback == nullptr");
Jooyung Han1abce692022-10-14 16:31:32 +0900196
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 Bianchi9cf92762021-01-26 11:38:58 -0800203void AServiceManager_forceLazyServicesPersist(bool persist) {
204 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
205 serviceRegistrar.forcePersist(persist);
206}
207void 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}
214bool AServiceManager_tryUnregister() {
215 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
216 return serviceRegistrar.tryUnregister();
217}
218void AServiceManager_reRegister() {
219 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
220 serviceRegistrar.reRegister();
Steven Morelandd687f332021-02-19 02:06:48 +0000221}