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 #pragma once
18 
19 #include <android/binder_ibinder.h>
20 #include <android/binder_status.h>
21 #include <sys/cdefs.h>
22 
23 __BEGIN_DECLS
24 
25 /**
26  * This registers the service with the default service manager under this instance name. This does
27  * not take ownership of binder.
28  *
29  * WARNING: when using this API across an APEX boundary, do not use with unstable
30  * AIDL services. TODO(b/139325195)
31  *
32  * \param binder object to register globally with the service manager.
33  * \param instance identifier of the service. This will be used to lookup the service.
34  *
35  * \return EX_NONE on success.
36  */
37 __attribute__((warn_unused_result)) binder_exception_t AServiceManager_addService(
38         AIBinder* binder, const char* instance) __INTRODUCED_IN(29);
39 
40 /**
41  * Gets a binder object with this specific instance name. Will return nullptr immediately if the
42  * service is not available This also implicitly calls AIBinder_incStrong (so the caller of this
43  * function is responsible for calling AIBinder_decStrong).
44  *
45  * WARNING: when using this API across an APEX boundary, do not use with unstable
46  * AIDL services. TODO(b/139325195)
47  *
48  * \param instance identifier of the service used to lookup the service.
49  */
50 __attribute__((warn_unused_result)) AIBinder* AServiceManager_checkService(const char* instance)
51         __INTRODUCED_IN(29);
52 
53 /**
54  * Gets a binder object with this specific instance name. Blocks for a couple of seconds waiting on
55  * it. This also implicitly calls AIBinder_incStrong (so the caller of this function is responsible
56  * for calling AIBinder_decStrong).
57  *
58  * WARNING: when using this API across an APEX boundary, do not use with unstable
59  * AIDL services. TODO(b/139325195)
60  *
61  * \param instance identifier of the service used to lookup the service.
62  */
63 __attribute__((warn_unused_result)) AIBinder* AServiceManager_getService(const char* instance)
64         __INTRODUCED_IN(29);
65 
66 /**
67  * Registers a lazy service with the default service manager under the 'instance' name.
68  * Does not take ownership of binder.
69  * The service must be configured statically with init so it can be restarted with
70  * ctl.interface.* messages from servicemanager.
71  * AServiceManager_registerLazyService cannot safely be used with AServiceManager_addService
72  * in the same process. If one service is registered with AServiceManager_registerLazyService,
73  * the entire process will have its lifetime controlled by servicemanager.
74  * Instead, all services in the process should be registered using
75  * AServiceManager_registerLazyService.
76  *
77  * \param binder object to register globally with the service manager.
78  * \param instance identifier of the service. This will be used to lookup the service.
79  *
80  * \return STATUS_OK on success.
81  */
82 binder_status_t AServiceManager_registerLazyService(AIBinder* binder, const char* instance)
83         __INTRODUCED_IN(31);
84 
85 /**
86  * Gets a binder object with this specific instance name. Efficiently waits for the service.
87  * If the service is not declared, it will wait indefinitely. Requires the threadpool
88  * to be started in the service.
89  * This also implicitly calls AIBinder_incStrong (so the caller of this function is responsible
90  * for calling AIBinder_decStrong).
91  *
92  * WARNING: when using this API across an APEX boundary, do not use with unstable
93  * AIDL services. TODO(b/139325195)
94  *
95  * \param instance identifier of the service used to lookup the service.
96  *
97  * \return service if registered, null if not.
98  */
99 __attribute__((warn_unused_result)) AIBinder* AServiceManager_waitForService(const char* instance)
100         __INTRODUCED_IN(31);
101 
102 /**
103  * Check if a service is declared (e.g. VINTF manifest).
104  *
105  * \param instance identifier of the service.
106  *
107  * \return true on success, meaning AServiceManager_waitForService should always
108  *    be able to return the service.
109  */
110 bool AServiceManager_isDeclared(const char* instance) __INTRODUCED_IN(31);
111 
112 /**
113  * Returns all declared instances for a particular interface.
114  *
115  * For instance, if 'android.foo.IFoo/foo' is declared, and 'android.foo.IFoo' is
116  * passed here, then ["foo"] would be returned.
117  *
118  * See also AServiceManager_isDeclared.
119  *
120  * \param interface interface, e.g. 'android.foo.IFoo'
121  * \param context to pass to callback
122  * \param callback taking instance (e.g. 'foo') and context
123  */
124 void AServiceManager_forEachDeclaredInstance(const char* interface, void* context,
125                                              void (*callback)(const char*, void*))
126         __INTRODUCED_IN(31);
127 
128 /**
129  * Check if a service is updatable via an APEX module.
130  *
131  * \param instance identifier of the service
132  *
133  * \return whether the interface is updatable via APEX
134  */
135 bool AServiceManager_isUpdatableViaApex(const char* instance) __INTRODUCED_IN(31);
136 
137 /**
138  * Prevent lazy services without client from shutting down their process
139  *
140  * This should only be used if it is every eventually set to false. If a
141  * service needs to persist but doesn't need to dynamically shut down,
142  * prefer to control it with another mechanism.
143  *
144  * \param persist 'true' if the process should not exit.
145  */
146 void AServiceManager_forceLazyServicesPersist(bool persist) __INTRODUCED_IN(31);
147 
148 /**
149  * Set a callback that is invoked when the active service count (i.e. services with clients)
150  * registered with this process drops to zero (or becomes nonzero).
151  * The callback takes a boolean argument, which is 'true' if there is
152  * at least one service with clients.
153  *
154  * \param callback function to call when the number of services
155  *    with clients changes.
156  * \param context opaque pointer passed back as second parameter to the
157  * callback.
158  *
159  * The callback takes two arguments. The first is a boolean that represents if there are
160  * services with clients (true) or not (false).
161  * The second is the 'context' pointer passed during the registration.
162  *
163  * Callback return value:
164  * - false: Default behavior for lazy services (shut down the process if there
165  *          are no clients).
166  * - true:  Don't shut down the process even if there are no clients.
167  *
168  * This callback gives a chance to:
169  * 1 - Perform some additional operations before exiting;
170  * 2 - Prevent the process from exiting by returning "true" from the callback.
171  */
172 void AServiceManager_setActiveServicesCallback(bool (*callback)(bool, void*), void* context)
173         __INTRODUCED_IN(31);
174 
175 /**
176  * Try to unregister all services previously registered with 'registerService'.
177  *
178  * \return true on success.
179  */
180 bool AServiceManager_tryUnregister() __INTRODUCED_IN(31);
181 
182 /**
183  * Re-register services that were unregistered by 'tryUnregister'.
184  * This method should be called in the case 'tryUnregister' fails
185  * (and should be called on the same thread).
186  */
187 void AServiceManager_reRegister() __INTRODUCED_IN(31);
188 
189 __END_DECLS
190