blob: d6ac4acc2949d5f6af7cc288022249665b3bbde1 [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 Wasilczyk052c5132024-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;
Henry Wanga9209222024-05-31 16:22:58 +080053 int dumpFlags = 0;
54 if (flags & AServiceManager_AddServiceFlag::ADD_SERVICE_DUMP_FLAG_PRIORITY_CRITICAL) {
55 dumpFlags |= IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL;
56 }
57 if (flags & AServiceManager_AddServiceFlag::ADD_SERVICE_DUMP_FLAG_PRIORITY_HIGH) {
58 dumpFlags |= IServiceManager::DUMP_FLAG_PRIORITY_HIGH;
59 }
60 if (flags & AServiceManager_AddServiceFlag::ADD_SERVICE_DUMP_FLAG_PRIORITY_NORMAL) {
61 dumpFlags |= IServiceManager::DUMP_FLAG_PRIORITY_NORMAL;
62 }
63 if (flags & AServiceManager_AddServiceFlag::ADD_SERVICE_DUMP_FLAG_PRIORITY_DEFAULT) {
64 dumpFlags |= IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT;
65 }
66 if (dumpFlags == 0) {
67 dumpFlags = IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT;
68 }
69 status_t exception =
70 sm->addService(String16(instance), binder->getBinder(), allowIsolated, dumpFlags);
71
Charlie Wangeae87df2023-02-03 12:33:16 -080072 return PruneException(exception);
73}
74
Steven Morelanddea68bb2019-02-15 10:53:08 -080075AIBinder* AServiceManager_checkService(const char* instance) {
76 if (instance == nullptr) {
77 return nullptr;
78 }
79
80 sp<IServiceManager> sm = defaultServiceManager();
81 sp<IBinder> binder = sm->checkService(String16(instance));
82
83 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
84 AIBinder_incStrong(ret.get());
85 return ret.get();
86}
Steven Moreland2e87adc2018-08-20 19:47:00 -070087AIBinder* AServiceManager_getService(const char* instance) {
88 if (instance == nullptr) {
89 return nullptr;
90 }
91
92 sp<IServiceManager> sm = defaultServiceManager();
Tomasz Wasilczyk052c5132024-06-21 15:45:26 -070093 LIBBINDER_IGNORE("-Wdeprecated-declarations")
Steven Moreland2e87adc2018-08-20 19:47:00 -070094 sp<IBinder> binder = sm->getService(String16(instance));
Tomasz Wasilczyk052c5132024-06-21 15:45:26 -070095 LIBBINDER_IGNORE_END()
Steven Moreland2e87adc2018-08-20 19:47:00 -070096
Steven Moreland94968952018-09-05 14:42:59 -070097 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
Steven Moreland71cddc32018-08-30 23:39:22 -070098 AIBinder_incStrong(ret.get());
99 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700100}
Devin Moore6a9394f2020-07-31 17:24:51 -0700101binder_status_t AServiceManager_registerLazyService(AIBinder* binder, const char* instance) {
102 if (binder == nullptr || instance == nullptr) {
103 return STATUS_UNEXPECTED_NULL;
104 }
105
106 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
107 status_t status = serviceRegistrar.registerService(binder->getBinder(), instance);
108
109 return PruneStatusT(status);
110}
111AIBinder* AServiceManager_waitForService(const char* instance) {
112 if (instance == nullptr) {
113 return nullptr;
114 }
115
116 sp<IServiceManager> sm = defaultServiceManager();
117 sp<IBinder> binder = sm->waitForService(String16(instance));
118
119 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
120 AIBinder_incStrong(ret.get());
121 return ret.get();
122}
Steven Moreland09b9d162022-11-18 21:15:35 +0000123typedef void (*AServiceManager_onRegister)(const char* instance, AIBinder* registered,
124 void* cookie);
125
126struct AServiceManager_NotificationRegistration
127 : public IServiceManager::LocalRegistrationCallback {
128 std::mutex m;
129 const char* instance = nullptr;
130 void* cookie = nullptr;
131 AServiceManager_onRegister onRegister = nullptr;
132
133 virtual void onServiceRegistration(const String16& smInstance, const sp<IBinder>& binder) {
134 std::lock_guard<std::mutex> l(m);
135 if (onRegister == nullptr) return;
136
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700137 LOG_ALWAYS_FATAL_IF(String8(smInstance) != instance, "onServiceRegistration: %s != %s",
138 String8(smInstance).c_str(), instance);
Steven Moreland09b9d162022-11-18 21:15:35 +0000139
140 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
141 AIBinder_incStrong(ret.get());
142
143 onRegister(instance, ret.get(), cookie);
144 }
145
146 void clear() {
147 std::lock_guard<std::mutex> l(m);
148 instance = nullptr;
149 cookie = nullptr;
150 onRegister = nullptr;
151 }
152};
153
154__attribute__((warn_unused_result)) AServiceManager_NotificationRegistration*
155AServiceManager_registerForServiceNotifications(const char* instance,
156 AServiceManager_onRegister onRegister,
157 void* cookie) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700158 LOG_ALWAYS_FATAL_IF(instance == nullptr, "instance == nullptr");
159 LOG_ALWAYS_FATAL_IF(onRegister == nullptr, "onRegister == nullptr for %s", instance);
Steven Moreland09b9d162022-11-18 21:15:35 +0000160 // cookie can be nullptr
161
162 auto cb = sp<AServiceManager_NotificationRegistration>::make();
163 cb->instance = instance;
164 cb->onRegister = onRegister;
165 cb->cookie = cookie;
166
167 sp<IServiceManager> sm = defaultServiceManager();
168 if (status_t res = sm->registerForNotifications(String16(instance), cb); res != STATUS_OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700169 ALOGE("Failed to register for service notifications for %s: %s", instance,
170 statusToString(res).c_str());
Steven Moreland09b9d162022-11-18 21:15:35 +0000171 return nullptr;
172 }
173
174 cb->incStrong(nullptr);
175 return cb.get();
176}
177
178void AServiceManager_NotificationRegistration_delete(
179 AServiceManager_NotificationRegistration* notification) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700180 LOG_ALWAYS_FATAL_IF(notification == nullptr, "notification == nullptr");
Steven Moreland09b9d162022-11-18 21:15:35 +0000181 notification->clear();
182 notification->decStrong(nullptr);
183}
184
Devin Moore6a9394f2020-07-31 17:24:51 -0700185bool AServiceManager_isDeclared(const char* instance) {
186 if (instance == nullptr) {
187 return false;
188 }
189
190 sp<IServiceManager> sm = defaultServiceManager();
191 return sm->isDeclared(String16(instance));
192}
Steven Morelandd687f332021-02-19 02:06:48 +0000193void AServiceManager_forEachDeclaredInstance(const char* interface, void* context,
194 void (*callback)(const char*, void*)) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700195 LOG_ALWAYS_FATAL_IF(interface == nullptr, "interface == nullptr");
Steven Morelandd687f332021-02-19 02:06:48 +0000196 // context may be nullptr
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700197 LOG_ALWAYS_FATAL_IF(callback == nullptr, "callback == nullptr");
Steven Morelandd687f332021-02-19 02:06:48 +0000198
199 sp<IServiceManager> sm = defaultServiceManager();
200 for (const String16& instance : sm->getDeclaredInstances(String16(interface))) {
201 callback(String8(instance).c_str(), context);
202 }
203}
Steven Morelandedd4e072021-04-21 00:27:29 +0000204bool AServiceManager_isUpdatableViaApex(const char* instance) {
205 if (instance == nullptr) {
206 return false;
207 }
208
209 sp<IServiceManager> sm = defaultServiceManager();
210 return sm->updatableViaApex(String16(instance)) != std::nullopt;
211}
Jooyung Han1abce692022-10-14 16:31:32 +0900212void AServiceManager_getUpdatableApexName(const char* instance, void* context,
213 void (*callback)(const char*, void*)) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700214 LOG_ALWAYS_FATAL_IF(instance == nullptr, "instance == nullptr");
Jooyung Han1abce692022-10-14 16:31:32 +0900215 // context may be nullptr
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700216 LOG_ALWAYS_FATAL_IF(callback == nullptr, "callback == nullptr");
Jooyung Han1abce692022-10-14 16:31:32 +0900217
218 sp<IServiceManager> sm = defaultServiceManager();
219 std::optional<String16> updatableViaApex = sm->updatableViaApex(String16(instance));
220 if (updatableViaApex.has_value()) {
221 callback(String8(updatableViaApex.value()).c_str(), context);
222 }
223}
Jooyung Han75fc06f2024-02-03 03:56:59 +0900224void* AServiceManager_openDeclaredPassthroughHal(const char* interface, const char* instance,
225 int flag) {
226 LOG_ALWAYS_FATAL_IF(interface == nullptr, "interface == nullptr");
227 LOG_ALWAYS_FATAL_IF(instance == nullptr, "instance == nullptr");
228
229 return openDeclaredPassthroughHal(String16(interface), String16(instance), flag);
230}
Amos Bianchi9cf92762021-01-26 11:38:58 -0800231void AServiceManager_forceLazyServicesPersist(bool persist) {
232 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
233 serviceRegistrar.forcePersist(persist);
234}
235void AServiceManager_setActiveServicesCallback(bool (*callback)(bool, void*), void* context) {
236 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
237 std::function<bool(bool)> fn = [=](bool hasClients) -> bool {
238 return callback(hasClients, context);
239 };
240 serviceRegistrar.setActiveServicesCallback(fn);
241}
242bool AServiceManager_tryUnregister() {
243 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
244 return serviceRegistrar.tryUnregister();
245}
246void AServiceManager_reRegister() {
247 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
248 serviceRegistrar.reRegister();
Steven Morelandd687f332021-02-19 02:06:48 +0000249}