blob: 7649a26ff6a533265615d5d33e218447657349d0 [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 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}
Steven Morelanddea68bb2019-02-15 10:53:08 -080043AIBinder* AServiceManager_checkService(const char* instance) {
44 if (instance == nullptr) {
45 return nullptr;
46 }
47
48 sp<IServiceManager> sm = defaultServiceManager();
49 sp<IBinder> binder = sm->checkService(String16(instance));
50
51 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
52 AIBinder_incStrong(ret.get());
53 return ret.get();
54}
Steven Moreland2e87adc2018-08-20 19:47:00 -070055AIBinder* AServiceManager_getService(const char* instance) {
56 if (instance == nullptr) {
57 return nullptr;
58 }
59
60 sp<IServiceManager> sm = defaultServiceManager();
61 sp<IBinder> binder = sm->getService(String16(instance));
62
Steven Moreland94968952018-09-05 14:42:59 -070063 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
Steven Moreland71cddc32018-08-30 23:39:22 -070064 AIBinder_incStrong(ret.get());
65 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -070066}
Devin Moore6a9394f2020-07-31 17:24:51 -070067binder_status_t AServiceManager_registerLazyService(AIBinder* binder, const char* instance) {
68 if (binder == nullptr || instance == nullptr) {
69 return STATUS_UNEXPECTED_NULL;
70 }
71
72 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
73 status_t status = serviceRegistrar.registerService(binder->getBinder(), instance);
74
75 return PruneStatusT(status);
76}
77AIBinder* AServiceManager_waitForService(const char* instance) {
78 if (instance == nullptr) {
79 return nullptr;
80 }
81
82 sp<IServiceManager> sm = defaultServiceManager();
83 sp<IBinder> binder = sm->waitForService(String16(instance));
84
85 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
86 AIBinder_incStrong(ret.get());
87 return ret.get();
88}
89bool AServiceManager_isDeclared(const char* instance) {
90 if (instance == nullptr) {
91 return false;
92 }
93
94 sp<IServiceManager> sm = defaultServiceManager();
95 return sm->isDeclared(String16(instance));
96}
Steven Morelandd687f332021-02-19 02:06:48 +000097void AServiceManager_forEachDeclaredInstance(const char* interface, void* context,
98 void (*callback)(const char*, void*)) {
99 CHECK(interface != nullptr);
100 // context may be nullptr
101 CHECK(callback != nullptr);
102
103 sp<IServiceManager> sm = defaultServiceManager();
104 for (const String16& instance : sm->getDeclaredInstances(String16(interface))) {
105 callback(String8(instance).c_str(), context);
106 }
107}
Steven Morelandedd4e072021-04-21 00:27:29 +0000108bool AServiceManager_isUpdatableViaApex(const char* instance) {
109 if (instance == nullptr) {
110 return false;
111 }
112
113 sp<IServiceManager> sm = defaultServiceManager();
114 return sm->updatableViaApex(String16(instance)) != std::nullopt;
115}
Amos Bianchi9cf92762021-01-26 11:38:58 -0800116void AServiceManager_forceLazyServicesPersist(bool persist) {
117 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
118 serviceRegistrar.forcePersist(persist);
119}
120void AServiceManager_setActiveServicesCallback(bool (*callback)(bool, void*), void* context) {
121 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
122 std::function<bool(bool)> fn = [=](bool hasClients) -> bool {
123 return callback(hasClients, context);
124 };
125 serviceRegistrar.setActiveServicesCallback(fn);
126}
127bool AServiceManager_tryUnregister() {
128 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
129 return serviceRegistrar.tryUnregister();
130}
131void AServiceManager_reRegister() {
132 auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
133 serviceRegistrar.reRegister();
Steven Morelandd687f332021-02-19 02:06:48 +0000134}