• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include "ibinder_internal.h"
20 #include "status_internal.h"
21 
22 #include <android-base/logging.h>
23 #include <binder/IServiceManager.h>
24 #include <binder/LazyServiceRegistrar.h>
25 
26 using ::android::defaultServiceManager;
27 using ::android::IBinder;
28 using ::android::IServiceManager;
29 using ::android::sp;
30 using ::android::status_t;
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 }
AServiceManager_checkService(const char * instance)43 AIBinder* 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 }
AServiceManager_getService(const char * instance)55 AIBinder* 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 
63     sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
64     AIBinder_incStrong(ret.get());
65     return ret.get();
66 }
AServiceManager_registerLazyService(AIBinder * binder,const char * instance)67 binder_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 }
AServiceManager_waitForService(const char * instance)77 AIBinder* 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 }
AServiceManager_isDeclared(const char * instance)89 bool 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 }
AServiceManager_forEachDeclaredInstance(const char * interface,void * context,void (* callback)(const char *,void *))97 void 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 }
AServiceManager_isUpdatableViaApex(const char * instance)108 bool 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 }
AServiceManager_forceLazyServicesPersist(bool persist)116 void AServiceManager_forceLazyServicesPersist(bool persist) {
117     auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
118     serviceRegistrar.forcePersist(persist);
119 }
AServiceManager_setActiveServicesCallback(bool (* callback)(bool,void *),void * context)120 void 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 }
AServiceManager_tryUnregister()127 bool AServiceManager_tryUnregister() {
128     auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
129     return serviceRegistrar.tryUnregister();
130 }
AServiceManager_reRegister()131 void AServiceManager_reRegister() {
132     auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
133     serviceRegistrar.reRegister();
134 }
135