xref: /aosp_15_r20/frameworks/native/libs/binder/ndk/service_manager.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
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>
18 #include <binder/IServiceManager.h>
19 #include <binder/LazyServiceRegistrar.h>
20 
21 #include "../Utils.h"
22 #include "ibinder_internal.h"
23 #include "status_internal.h"
24 
25 using ::android::defaultServiceManager;
26 using ::android::IBinder;
27 using ::android::IServiceManager;
28 using ::android::sp;
29 using ::android::status_t;
30 using ::android::statusToString;
31 using ::android::String16;
32 using ::android::String8;
33 
AServiceManager_addService(AIBinder * binder,const char * instance)34 binder_exception_t AServiceManager_addService(AIBinder* binder, const char* instance) {
35     if (binder == nullptr || instance == nullptr) {
36         return EX_ILLEGAL_ARGUMENT;
37     }
38 
39     sp<IServiceManager> sm = defaultServiceManager();
40     status_t exception = sm->addService(String16(instance), binder->getBinder());
41     return PruneException(exception);
42 }
43 
AServiceManager_addServiceWithFlags(AIBinder * binder,const char * instance,const AServiceManager_AddServiceFlag flags)44 binder_exception_t AServiceManager_addServiceWithFlags(AIBinder* binder, const char* instance,
45                                                        const AServiceManager_AddServiceFlag flags) {
46     if (binder == nullptr || instance == nullptr) {
47         return EX_ILLEGAL_ARGUMENT;
48     }
49 
50     sp<IServiceManager> sm = defaultServiceManager();
51 
52     bool allowIsolated = flags & AServiceManager_AddServiceFlag::ADD_SERVICE_ALLOW_ISOLATED;
53     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 
72     return PruneException(exception);
73 }
74 
AServiceManager_checkService(const char * instance)75 AIBinder* 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 }
AServiceManager_getService(const char * instance)87 AIBinder* AServiceManager_getService(const char* instance) {
88     if (instance == nullptr) {
89         return nullptr;
90     }
91 
92     sp<IServiceManager> sm = defaultServiceManager();
93     LIBBINDER_IGNORE("-Wdeprecated-declarations")
94     sp<IBinder> binder = sm->getService(String16(instance));
95     LIBBINDER_IGNORE_END()
96 
97     sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
98     AIBinder_incStrong(ret.get());
99     return ret.get();
100 }
AServiceManager_registerLazyService(AIBinder * binder,const char * instance)101 binder_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 }
AServiceManager_waitForService(const char * instance)111 AIBinder* 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 }
123 typedef void (*AServiceManager_onRegister)(const char* instance, AIBinder* registered,
124                                            void* cookie);
125 
126 struct 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 
onServiceRegistrationAServiceManager_NotificationRegistration133     virtual void onServiceRegistration(const String16& smInstance, const sp<IBinder>& binder) {
134         std::lock_guard<std::mutex> l(m);
135         if (onRegister == nullptr) return;
136 
137         LOG_ALWAYS_FATAL_IF(String8(smInstance) != instance, "onServiceRegistration: %s != %s",
138                             String8(smInstance).c_str(), instance);
139 
140         sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
141         AIBinder_incStrong(ret.get());
142 
143         onRegister(instance, ret.get(), cookie);
144     }
145 
clearAServiceManager_NotificationRegistration146     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*
AServiceManager_registerForServiceNotifications(const char * instance,AServiceManager_onRegister onRegister,void * cookie)155 AServiceManager_registerForServiceNotifications(const char* instance,
156                                                 AServiceManager_onRegister onRegister,
157                                                 void* cookie) {
158     LOG_ALWAYS_FATAL_IF(instance == nullptr, "instance == nullptr");
159     LOG_ALWAYS_FATAL_IF(onRegister == nullptr, "onRegister == nullptr for %s", instance);
160     // 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) {
169         ALOGE("Failed to register for service notifications for %s: %s", instance,
170               statusToString(res).c_str());
171         return nullptr;
172     }
173 
174     cb->incStrong(nullptr);
175     return cb.get();
176 }
177 
AServiceManager_NotificationRegistration_delete(AServiceManager_NotificationRegistration * notification)178 void AServiceManager_NotificationRegistration_delete(
179         AServiceManager_NotificationRegistration* notification) {
180     LOG_ALWAYS_FATAL_IF(notification == nullptr, "notification == nullptr");
181     notification->clear();
182     notification->decStrong(nullptr);
183 }
184 
AServiceManager_isDeclared(const char * instance)185 bool 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 }
AServiceManager_forEachDeclaredInstance(const char * interface,void * context,void (* callback)(const char *,void *))193 void AServiceManager_forEachDeclaredInstance(const char* interface, void* context,
194                                              void (*callback)(const char*, void*)) {
195     LOG_ALWAYS_FATAL_IF(interface == nullptr, "interface == nullptr");
196     // context may be nullptr
197     LOG_ALWAYS_FATAL_IF(callback == nullptr, "callback == nullptr");
198 
199     sp<IServiceManager> sm = defaultServiceManager();
200     for (const String16& instance : sm->getDeclaredInstances(String16(interface))) {
201         callback(String8(instance).c_str(), context);
202     }
203 }
AServiceManager_isUpdatableViaApex(const char * instance)204 bool 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 }
AServiceManager_getUpdatableApexName(const char * instance,void * context,void (* callback)(const char *,void *))212 void AServiceManager_getUpdatableApexName(const char* instance, void* context,
213                                           void (*callback)(const char*, void*)) {
214     LOG_ALWAYS_FATAL_IF(instance == nullptr, "instance == nullptr");
215     // context may be nullptr
216     LOG_ALWAYS_FATAL_IF(callback == nullptr, "callback == nullptr");
217 
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 }
AServiceManager_openDeclaredPassthroughHal(const char * interface,const char * instance,int flag)224 void* 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 }
AServiceManager_forceLazyServicesPersist(bool persist)231 void AServiceManager_forceLazyServicesPersist(bool persist) {
232     auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
233     serviceRegistrar.forcePersist(persist);
234 }
AServiceManager_setActiveServicesCallback(bool (* callback)(bool,void *),void * context)235 void 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 }
AServiceManager_tryUnregister()242 bool AServiceManager_tryUnregister() {
243     auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
244     return serviceRegistrar.tryUnregister();
245 }
AServiceManager_reRegister()246 void AServiceManager_reRegister() {
247     auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
248     serviceRegistrar.reRegister();
249 }
250