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 enum AServiceManager_AddServiceFlag : uint32_t { 26 /** 27 * This allows processes with AID_ISOLATED to get the binder of the service added. 28 * 29 * Services with methods that perform file IO, web socket creation or ways to egress data must 30 * not be added with this flag for privacy concerns. 31 */ 32 ADD_SERVICE_ALLOW_ISOLATED = 1, 33 }; 34 35 /** 36 * This registers the service with the default service manager under this instance name. This does 37 * not take ownership of binder. 38 * 39 * WARNING: when using this API across an APEX boundary, do not use with unstable 40 * AIDL services. TODO(b/139325195) 41 * 42 * \param binder object to register globally with the service manager. 43 * \param instance identifier of the service. This will be used to lookup the service. 44 * 45 * \return EX_NONE on success. 46 */ 47 __attribute__((warn_unused_result)) binder_exception_t AServiceManager_addService( 48 AIBinder* binder, const char* instance) __INTRODUCED_IN(29); 49 50 /** 51 * This registers the service with the default service manager under this instance name. This does 52 * not take ownership of binder. 53 * 54 * WARNING: when using this API across an APEX boundary, do not use with unstable 55 * AIDL services. TODO(b/139325195) 56 * 57 * \param binder object to register globally with the service manager. 58 * \param instance identifier of the service. This will be used to lookup the service. 59 * \param flags an AServiceManager_AddServiceFlag enum to denote how the service should be added. 60 * 61 * \return EX_NONE on success. 62 */ 63 __attribute__((warn_unused_result)) binder_exception_t AServiceManager_addServiceWithFlags( 64 AIBinder* binder, const char* instance, const AServiceManager_AddServiceFlag flags) 65 __INTRODUCED_IN(34); 66 67 /** 68 * Gets a binder object with this specific instance name. Will return nullptr immediately if the 69 * service is not available This also implicitly calls AIBinder_incStrong (so the caller of this 70 * function is responsible for calling AIBinder_decStrong). 71 * 72 * WARNING: when using this API across an APEX boundary, do not use with unstable 73 * AIDL services. TODO(b/139325195) 74 * 75 * \param instance identifier of the service used to lookup the service. 76 */ 77 __attribute__((warn_unused_result)) AIBinder* AServiceManager_checkService(const char* instance) 78 __INTRODUCED_IN(29); 79 80 /** 81 * Gets a binder object with this specific instance name. Blocks for a couple of seconds waiting on 82 * it. This also implicitly calls AIBinder_incStrong (so the caller of this function is responsible 83 * for calling AIBinder_decStrong). This does polling. A more efficient way to make sure you 84 * unblock as soon as the service is available is to use AIBinder_waitForService. 85 * 86 * WARNING: when using this API across an APEX boundary, do not use with unstable 87 * AIDL services. TODO(b/139325195) 88 * 89 * WARNING: when using this API, typically, you should call it in a loop. It's dangerous to 90 * assume that nullptr could mean that the service is not available. The service could just 91 * be starting. Generally, whether a service exists, this information should be declared 92 * externally (for instance, an Android feature might imply the existence of a service, 93 * a system property, or in the case of services in the VINTF manifest, it can be checked 94 * with AServiceManager_isDeclared). 95 * 96 * \param instance identifier of the service used to lookup the service. 97 */ 98 [[deprecated("this polls 5s, use AServiceManager_waitForService or AServiceManager_checkService")]] 99 __attribute__((warn_unused_result)) AIBinder* AServiceManager_getService(const char* instance) 100 __INTRODUCED_IN(29); 101 102 /** 103 * Registers a lazy service with the default service manager under the 'instance' name. 104 * Does not take ownership of binder. 105 * The service must be configured statically with init so it can be restarted with 106 * ctl.interface.* messages from servicemanager. 107 * AServiceManager_registerLazyService cannot safely be used with AServiceManager_addService 108 * in the same process. If one service is registered with AServiceManager_registerLazyService, 109 * the entire process will have its lifetime controlled by servicemanager. 110 * Instead, all services in the process should be registered using 111 * AServiceManager_registerLazyService. 112 * 113 * \param binder object to register globally with the service manager. 114 * \param instance identifier of the service. This will be used to lookup the service. 115 * 116 * \return STATUS_OK on success. 117 */ 118 binder_status_t AServiceManager_registerLazyService(AIBinder* binder, const char* instance) 119 __INTRODUCED_IN(31); 120 121 /** 122 * Gets a binder object with this specific instance name. Efficiently waits for the service. 123 * If the service is not declared, it will wait indefinitely. Requires the threadpool 124 * to be started in the service. 125 * This also implicitly calls AIBinder_incStrong (so the caller of this function is responsible 126 * for calling AIBinder_decStrong). 127 * 128 * WARNING: when using this API across an APEX boundary, do not use with unstable 129 * AIDL services. TODO(b/139325195) 130 * 131 * \param instance identifier of the service used to lookup the service. 132 * 133 * \return service if registered, null if not. 134 */ 135 __attribute__((warn_unused_result)) AIBinder* AServiceManager_waitForService(const char* instance) 136 __INTRODUCED_IN(31); 137 138 /** 139 * Function to call when a service is registered. The instance is passed as well as 140 * ownership of the binder named 'registered'. 141 * 142 * WARNING: a lock is held when this method is called in order to prevent races with 143 * AServiceManager_NotificationRegistration_delete. Do not make synchronous binder calls when 144 * implementing this method to avoid deadlocks. 145 * 146 * \param instance instance name of service registered 147 * \param registered ownership-passed instance of service registered 148 * \param cookie data passed during registration for notifications 149 */ 150 typedef void (*AServiceManager_onRegister)(const char* instance, AIBinder* registered, 151 void* cookie); 152 153 /** 154 * Represents a registration to servicemanager which can be cleared anytime. 155 */ 156 struct AServiceManager_NotificationRegistration; 157 158 /** 159 * Get notifications when a service is registered. If the service is already registered, 160 * you will immediately get a notification. 161 * 162 * WARNING: it is strongly recommended to use AServiceManager_waitForService API instead. 163 * That API will wait synchronously, which is what you usually want in cases, including 164 * using some feature or during boot up. There is a history of bugs where waiting for 165 * notifications like this races with service startup. Also, when this API is used, a service 166 * bug will result in silent failure (rather than a debuggable deadlock). Furthermore, there 167 * is a history of this API being used to know when a service is up as a proxy for whethre 168 * that service should be started. This should only be used if you are intending to get 169 * ahold of the service as a client. For lazy services, whether a service is registered 170 * should not be used as a proxy for when it should be registered, which is only known 171 * by the real client. 172 * 173 * WARNING: if you use this API, you must also ensure that you check missing services are 174 * started and crash otherwise. If service failures are ignored, the system rots. 175 * 176 * \param instance name of service to wait for notifications about 177 * \param onRegister callback for when service is registered 178 * \param cookie data associated with this callback 179 * 180 * \return the token for this registration. Deleting this token will unregister. 181 */ 182 __attribute__((warn_unused_result)) AServiceManager_NotificationRegistration* 183 AServiceManager_registerForServiceNotifications(const char* instance, 184 AServiceManager_onRegister onRegister, void* cookie) 185 __INTRODUCED_IN(34); 186 187 /** 188 * Unregister for notifications and delete the object. 189 * 190 * After this method is called, the callback is guaranteed to no longer be invoked. This will block 191 * until any in-progress onRegister callbacks have completed. It is therefore safe to immediately 192 * destroy the void* cookie that was registered when this method returns. 193 * 194 * \param notification object to dismiss 195 */ 196 void AServiceManager_NotificationRegistration_delete( 197 AServiceManager_NotificationRegistration* notification) __INTRODUCED_IN(34); 198 199 /** 200 * Check if a service is declared (e.g. VINTF manifest). 201 * 202 * \param instance identifier of the service. 203 * 204 * \return true on success, meaning AServiceManager_waitForService should always 205 * be able to return the service. 206 */ 207 bool AServiceManager_isDeclared(const char* instance) __INTRODUCED_IN(31); 208 209 /** 210 * Returns all declared instances for a particular interface. 211 * 212 * For instance, if 'android.foo.IFoo/foo' is declared, and 'android.foo.IFoo' is 213 * passed here, then ["foo"] would be returned. 214 * 215 * See also AServiceManager_isDeclared. 216 * 217 * \param interface interface, e.g. 'android.foo.IFoo' 218 * \param context to pass to callback 219 * \param callback taking instance (e.g. 'foo') and context 220 */ 221 void AServiceManager_forEachDeclaredInstance(const char* interface, void* context, 222 void (*callback)(const char*, void*)) 223 __INTRODUCED_IN(31); 224 225 /** 226 * Check if a service is updatable via an APEX module. 227 * 228 * \param instance identifier of the service 229 * 230 * \return whether the interface is updatable via APEX 231 */ 232 bool AServiceManager_isUpdatableViaApex(const char* instance) __INTRODUCED_IN(31); 233 234 /** 235 * Returns the APEX name if a service is declared as updatable via an APEX module. 236 * 237 * \param instance identifier of the service 238 * \param context to pass to callback 239 * \param callback taking the APEX name (e.g. 'com.android.foo') and context 240 */ 241 void AServiceManager_getUpdatableApexName(const char* instance, void* context, 242 void (*callback)(const char*, void*)) 243 __INTRODUCED_IN(__ANDROID_API_U__); 244 245 /** 246 * Prevent lazy services without client from shutting down their process 247 * 248 * This should only be used if it is every eventually set to false. If a 249 * service needs to persist but doesn't need to dynamically shut down, 250 * prefer to control it with another mechanism. 251 * 252 * \param persist 'true' if the process should not exit. 253 */ 254 void AServiceManager_forceLazyServicesPersist(bool persist) __INTRODUCED_IN(31); 255 256 /** 257 * Set a callback that is invoked when the active service count (i.e. services with clients) 258 * registered with this process drops to zero (or becomes nonzero). 259 * The callback takes a boolean argument, which is 'true' if there is 260 * at least one service with clients. 261 * 262 * \param callback function to call when the number of services 263 * with clients changes. 264 * \param context opaque pointer passed back as second parameter to the 265 * callback. 266 * 267 * The callback takes two arguments. The first is a boolean that represents if there are 268 * services with clients (true) or not (false). 269 * The second is the 'context' pointer passed during the registration. 270 * 271 * Callback return value: 272 * - false: Default behavior for lazy services (shut down the process if there 273 * are no clients). 274 * - true: Don't shut down the process even if there are no clients. 275 * 276 * This callback gives a chance to: 277 * 1 - Perform some additional operations before exiting; 278 * 2 - Prevent the process from exiting by returning "true" from the callback. 279 */ 280 void AServiceManager_setActiveServicesCallback(bool (*callback)(bool, void*), void* context) 281 __INTRODUCED_IN(31); 282 283 /** 284 * Try to unregister all services previously registered with 'registerService'. 285 * 286 * \return true on success. 287 */ 288 bool AServiceManager_tryUnregister() __INTRODUCED_IN(31); 289 290 /** 291 * Re-register services that were unregistered by 'tryUnregister'. 292 * This method should be called in the case 'tryUnregister' fails 293 * (and should be called on the same thread). 294 */ 295 void AServiceManager_reRegister() __INTRODUCED_IN(31); 296 297 __END_DECLS 298