blob: 81f7cdbcb12916ba13448960dc09bd0540b77fd7 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 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
Steven Morelandc7a871e2020-11-10 21:56:57 +000017#pragma once
Frederick Maylef7b65d12024-05-14 16:55:14 -070018#include <binder/Common.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070019#include <binder/IInterface.h>
Devin Moore18f63752024-08-08 21:01:24 +000020// Trusty has its own definition of socket APIs from trusty_ipc.h
21#ifndef __TRUSTY__
22#include <sys/socket.h>
23#endif // __TRUSTY__
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <utils/String16.h>
Devin Moore18f63752024-08-08 21:01:24 +000025#include <utils/Vector.h>
Steven Morelandedd4e072021-04-21 00:27:29 +000026#include <optional>
Devin Moorec370db42024-08-09 23:18:05 +000027#include <set>
Steven Morelandedd4e072021-04-21 00:27:29 +000028
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029namespace android {
30
Steven Moreland583685e2019-10-02 16:45:11 -070031/**
32 * Service manager for C++ services.
33 *
34 * IInterface is only for legacy ABI compatibility
35 */
Frederick Maylef7b65d12024-05-14 16:55:14 -070036class LIBBINDER_EXPORTED IServiceManager : public IInterface {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037public:
Steven Moreland583685e2019-10-02 16:45:11 -070038 // for ABI compatibility
39 virtual const String16& getInterfaceDescriptor() const;
40
41 IServiceManager();
42 virtual ~IServiceManager();
43
Vishnu Nair64afc022018-02-01 15:29:34 -080044 /**
Steven Moreland635a2912019-10-02 15:50:10 -070045 * Must match values in IServiceManager.aidl
Vishnu Nairf56042d2017-09-19 15:25:10 -070046 */
Vishnu Nair64afc022018-02-01 15:29:34 -080047 /* Allows services to dump sections according to priorities. */
Vishnu Nair6a408532017-10-24 09:11:27 -070048 static const int DUMP_FLAG_PRIORITY_CRITICAL = 1 << 0;
49 static const int DUMP_FLAG_PRIORITY_HIGH = 1 << 1;
50 static const int DUMP_FLAG_PRIORITY_NORMAL = 1 << 2;
Vishnu Nair64afc022018-02-01 15:29:34 -080051 /**
52 * Services are by default registered with a DEFAULT dump priority. DEFAULT priority has the
53 * same priority as NORMAL priority but the services are not called with dump priority
54 * arguments.
55 */
56 static const int DUMP_FLAG_PRIORITY_DEFAULT = 1 << 3;
57 static const int DUMP_FLAG_PRIORITY_ALL = DUMP_FLAG_PRIORITY_CRITICAL |
58 DUMP_FLAG_PRIORITY_HIGH | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PRIORITY_DEFAULT;
59 static const int DUMP_FLAG_PROTO = 1 << 4;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
61 /**
Steven Moreland658fc4f2022-03-09 00:30:07 +000062 * Retrieve an existing service, blocking for a few seconds if it doesn't yet exist. This
63 * does polling. A more efficient way to make sure you unblock as soon as the service is
64 * available is to use waitForService or to use service notifications.
65 *
66 * Warning: when using this API, typically, you should call it in a loop. It's dangerous to
67 * assume that nullptr could mean that the service is not available. The service could just
68 * be starting. Generally, whether a service exists, this information should be declared
69 * externally (for instance, an Android feature might imply the existence of a service,
70 * a system property, or in the case of services in the VINTF manifest, it can be checked
71 * with isDeclared).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072 */
Steven Moreland800113e2022-12-09 01:32:26 +000073 [[deprecated("this polls for 5s, prefer waitForService or checkService")]]
74 virtual sp<IBinder> getService(const String16& name) const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075
76 /**
77 * Retrieve an existing service, non-blocking.
78 */
79 virtual sp<IBinder> checkService( const String16& name) const = 0;
80
81 /**
82 * Register a service.
83 */
Jiyong Parkb86c8662018-10-29 23:01:57 +090084 // NOLINTNEXTLINE(google-default-arguments)
Vishnu Nairf56042d2017-09-19 15:25:10 -070085 virtual status_t addService(const String16& name, const sp<IBinder>& service,
86 bool allowIsolated = false,
Vishnu Nair64afc022018-02-01 15:29:34 -080087 int dumpsysFlags = DUMP_FLAG_PRIORITY_DEFAULT) = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088
89 /**
90 * Return list of all existing services.
91 */
Jiyong Parkb86c8662018-10-29 23:01:57 +090092 // NOLINTNEXTLINE(google-default-arguments)
Vishnu Nair6a408532017-10-24 09:11:27 -070093 virtual Vector<String16> listServices(int dumpsysFlags = DUMP_FLAG_PRIORITY_ALL) = 0;
Steven Moreland1c47b582019-08-27 18:05:27 -070094
95 /**
96 * Efficiently wait for a service.
97 *
98 * Returns nullptr only for permission problem or fatal error.
99 */
100 virtual sp<IBinder> waitForService(const String16& name) = 0;
Steven Morelandb82b8f82019-10-28 10:52:34 -0700101
102 /**
103 * Check if a service is declared (e.g. VINTF manifest).
104 *
105 * If this returns true, waitForService should always be able to return the
106 * service.
107 */
108 virtual bool isDeclared(const String16& name) = 0;
Steven Moreland2e293aa2020-09-23 00:25:16 +0000109
110 /**
111 * Get all instances of a service as declared in the VINTF manifest
112 */
113 virtual Vector<String16> getDeclaredInstances(const String16& interface) = 0;
Steven Morelandedd4e072021-04-21 00:27:29 +0000114
115 /**
116 * If this instance is updatable via an APEX, returns the APEX with which
117 * this can be updated.
118 */
119 virtual std::optional<String16> updatableViaApex(const String16& name) = 0;
Devin Moore5e4c2f12021-09-09 22:36:33 +0000120
121 /**
Jooyung Han76944fe2022-10-25 17:02:45 +0900122 * Returns all instances which are updatable via the APEX. Instance names are fully qualified
123 * like `pack.age.IFoo/default`.
124 */
125 virtual Vector<String16> getUpdatableNames(const String16& apexName) = 0;
126
127 /**
Devin Moore5e4c2f12021-09-09 22:36:33 +0000128 * If this instance has declared remote connection information, returns
129 * the ConnectionInfo.
130 */
131 struct ConnectionInfo {
132 std::string ipAddress;
133 unsigned int port;
134 };
135 virtual std::optional<ConnectionInfo> getConnectionInfo(const String16& name) = 0;
Jayant Chowdhary30700942022-01-31 14:12:40 -0800136
137 struct LocalRegistrationCallback : public virtual RefBase {
138 virtual void onServiceRegistration(const String16& instance, const sp<IBinder>& binder) = 0;
139 virtual ~LocalRegistrationCallback() {}
140 };
141
142 virtual status_t registerForNotifications(const String16& name,
143 const sp<LocalRegistrationCallback>& callback) = 0;
144
145 virtual status_t unregisterForNotifications(const String16& name,
146 const sp<LocalRegistrationCallback>& callback) = 0;
Jayant Chowdharya0a8eb22022-05-20 03:30:09 +0000147
148 struct ServiceDebugInfo {
149 std::string name;
150 int pid;
151 };
152 virtual std::vector<ServiceDebugInfo> getServiceDebugInfo() = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153};
154
Frederick Maylef7b65d12024-05-14 16:55:14 -0700155LIBBINDER_EXPORTED sp<IServiceManager> defaultServiceManager();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156
Brett Chabot38dbade2020-01-24 12:59:43 -0800157/**
158 * Directly set the default service manager. Only used for testing.
Martijn Coenen402c8672020-02-03 10:01:36 +0100159 * Note that the caller is responsible for caling this method
160 * *before* any call to defaultServiceManager(); if the latter is
161 * called first, setDefaultServiceManager() will abort.
Brett Chabot38dbade2020-01-24 12:59:43 -0800162 */
Frederick Maylef7b65d12024-05-14 16:55:14 -0700163LIBBINDER_EXPORTED void setDefaultServiceManager(const sp<IServiceManager>& sm);
Brett Chabot38dbade2020-01-24 12:59:43 -0800164
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165template<typename INTERFACE>
Steven Moreland1c47b582019-08-27 18:05:27 -0700166sp<INTERFACE> waitForService(const String16& name) {
167 const sp<IServiceManager> sm = defaultServiceManager();
168 return interface_cast<INTERFACE>(sm->waitForService(name));
169}
170
171template<typename INTERFACE>
Steven Morelandb82b8f82019-10-28 10:52:34 -0700172sp<INTERFACE> waitForDeclaredService(const String16& name) {
173 const sp<IServiceManager> sm = defaultServiceManager();
174 if (!sm->isDeclared(name)) return nullptr;
175 return interface_cast<INTERFACE>(sm->waitForService(name));
176}
177
Steven Morelandf1b02a42019-11-05 16:14:20 -0800178template <typename INTERFACE>
179sp<INTERFACE> checkDeclaredService(const String16& name) {
180 const sp<IServiceManager> sm = defaultServiceManager();
181 if (!sm->isDeclared(name)) return nullptr;
182 return interface_cast<INTERFACE>(sm->checkService(name));
183}
184
Steven Morelandb82b8f82019-10-28 10:52:34 -0700185template<typename INTERFACE>
Steven Morelandc73571f2019-10-31 09:48:58 -0700186sp<INTERFACE> waitForVintfService(
187 const String16& instance = String16("default")) {
188 return waitForDeclaredService<INTERFACE>(
189 INTERFACE::descriptor + String16("/") + instance);
190}
191
192template<typename INTERFACE>
Steven Morelandf1b02a42019-11-05 16:14:20 -0800193sp<INTERFACE> checkVintfService(
194 const String16& instance = String16("default")) {
195 return checkDeclaredService<INTERFACE>(
196 INTERFACE::descriptor + String16("/") + instance);
197}
198
199template<typename INTERFACE>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200status_t getService(const String16& name, sp<INTERFACE>* outService)
201{
202 const sp<IServiceManager> sm = defaultServiceManager();
Yi Kong0cf75842018-07-10 11:44:36 -0700203 if (sm != nullptr) {
Pawan Wagh838396f2023-01-10 01:25:34 +0000204#pragma clang diagnostic push
205#pragma clang diagnostic ignored "-Wdeprecated-declarations"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 *outService = interface_cast<INTERFACE>(sm->getService(name));
Pawan Wagh838396f2023-01-10 01:25:34 +0000207#pragma clang diagnostic pop // getService deprecation
Yi Kong0cf75842018-07-10 11:44:36 -0700208 if ((*outService) != nullptr) return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 }
210 return NAME_NOT_FOUND;
211}
212
Frederick Maylef7b65d12024-05-14 16:55:14 -0700213LIBBINDER_EXPORTED void* openDeclaredPassthroughHal(const String16& interface,
214 const String16& instance, int flag);
Jooyung Han75fc06f2024-02-03 03:56:59 +0900215
Frederick Maylef7b65d12024-05-14 16:55:14 -0700216LIBBINDER_EXPORTED bool checkCallingPermission(const String16& permission);
217LIBBINDER_EXPORTED bool checkCallingPermission(const String16& permission, int32_t* outPid,
218 int32_t* outUid);
219LIBBINDER_EXPORTED bool checkPermission(const String16& permission, pid_t pid, uid_t uid,
220 bool logPermissionFailure = true);
Mathias Agopian375f5632009-06-15 18:24:59 -0700221
Devin Moore18f63752024-08-08 21:01:24 +0000222// ----------------------------------------------------------------------
223// Trusty's definition of the socket APIs does not include sockaddr types
224#ifndef __TRUSTY__
225typedef std::function<status_t(const String16& name, sockaddr* outAddr, socklen_t addrSize)>
226 RpcSocketAddressProvider;
227
Devin Moorec370db42024-08-09 23:18:05 +0000228/**
229 * This callback provides a way for clients to get access to remote services by
230 * providing an Accessor object from libbinder that can connect to the remote
231 * service over sockets.
232 *
233 * \param instance name of the service that the callback will provide an
234 * Accessor for. The provided accessor will be used to set up a client
235 * RPC connection in libbinder in order to return a binder for the
236 * associated remote service.
237 *
238 * \return IBinder of the Accessor object that libbinder implements.
239 * nullptr if the provider callback doesn't know how to reach the
240 * service or doesn't want to provide access for any other reason.
241 */
242typedef std::function<sp<IBinder>(const String16& instance)> RpcAccessorProvider;
Devin Moore18f63752024-08-08 21:01:24 +0000243
244class AccessorProvider;
245
246/**
Devin Moorec370db42024-08-09 23:18:05 +0000247 * Register a RpcAccessorProvider for the service manager APIs.
Devin Moore18f63752024-08-08 21:01:24 +0000248 *
Devin Moorec370db42024-08-09 23:18:05 +0000249 * \param instances that the RpcAccessorProvider knows about and can provide an
250 * Accessor for.
Devin Moore18f63752024-08-08 21:01:24 +0000251 * \param provider callback that generates Accessors.
252 *
253 * \return A pointer used as a recept for the successful addition of the
254 * AccessorProvider. This is needed to unregister it later.
255 */
256[[nodiscard]] LIBBINDER_EXPORTED std::weak_ptr<AccessorProvider> addAccessorProvider(
Devin Moorec370db42024-08-09 23:18:05 +0000257 std::set<std::string>&& instances, RpcAccessorProvider&& providerCallback);
Devin Moore18f63752024-08-08 21:01:24 +0000258
259/**
260 * Remove an accessor provider using the pointer provided by addAccessorProvider
261 * along with the cookie pointer that was used.
262 *
263 * \param provider cookie that was returned by addAccessorProvider to keep track
264 * of this instance.
265 */
266[[nodiscard]] LIBBINDER_EXPORTED status_t
267removeAccessorProvider(std::weak_ptr<AccessorProvider> provider);
268
269/**
270 * Create an Accessor associated with a service that can create a socket connection based
271 * on the connection info from the supplied RpcSocketAddressProvider.
272 *
273 * \param instance name of the service that this Accessor is associated with
274 * \param connectionInfoProvider a callback that returns connection info for
275 * connecting to the service.
276 * \return the binder of the IAccessor implementation from libbinder
277 */
278LIBBINDER_EXPORTED sp<IBinder> createAccessor(const String16& instance,
279 RpcSocketAddressProvider&& connectionInfoProvider);
280
281/**
282 * Check to make sure this binder is the expected binder that is an IAccessor
283 * associated with a specific instance.
284 *
285 * This helper function exists to avoid adding the IAccessor type to
286 * libbinder_ndk.
287 *
288 * \param instance name of the service that this Accessor should be associated with
289 * \param binder to validate
290 *
291 * \return OK if the binder is an IAccessor for `instance`
292 */
293LIBBINDER_EXPORTED status_t validateAccessor(const String16& instance, const sp<IBinder>& binder);
Devin Moore0555fbf2024-08-29 15:51:50 +0000294
295/**
296 * Have libbinder wrap this IAccessor binder in an IAccessorDelegator and return
297 * it.
298 *
299 * This is required only in very specific situations when the process that has
300 * permissions to connect the to RPC service's socket and create the FD for it
301 * is in a separate process from this process that wants to service the Accessor
302 * binder and the communication between these two processes is binder RPC. This
303 * is needed because the binder passed over the binder RPC connection can not be
304 * used as a kernel binder, and needs to be wrapped by a kernel binder that can
305 * then be registered with service manager.
306 *
307 * \param instance name of the Accessor.
308 * \param binder to wrap in a Delegator and register with service manager.
309 * \param outDelegator the wrapped kernel binder for IAccessorDelegator
310 *
311 * \return OK if the binder is an IAccessor for `instance` and the delegator was
312 * successfully created.
313 */
314LIBBINDER_EXPORTED status_t delegateAccessor(const String16& name, const sp<IBinder>& accessor,
315 sp<IBinder>* delegator);
Devin Moore18f63752024-08-08 21:01:24 +0000316#endif // __TRUSTY__
317
Yifan Hongf7760012021-06-04 16:04:42 -0700318#ifndef __ANDROID__
319// Create an IServiceManager that delegates the service manager on the device via adb.
320// This is can be set as the default service manager at program start, so that
321// defaultServiceManager() returns it:
322// int main() {
323// setDefaultServiceManager(createRpcDelegateServiceManager());
324// auto sm = defaultServiceManager();
325// // ...
326// }
327// Resources are cleaned up when the object is destroyed.
Yifan Hong5a05ef72021-10-08 17:33:47 -0700328//
Steven Morelandfeb13e82023-03-01 01:25:33 +0000329// For each returned binder object, at most |maxOutgoingConnections| outgoing connections are
330// instantiated, depending on how many the service on the device is configured with.
331// Hence, only |maxOutgoingConnections| calls can be made simultaneously.
332// See also RpcSession::setMaxOutgoingConnections.
Yifan Hong5a05ef72021-10-08 17:33:47 -0700333struct RpcDelegateServiceManagerOptions {
Steven Morelandfeb13e82023-03-01 01:25:33 +0000334 std::optional<size_t> maxOutgoingConnections;
Yifan Hong5a05ef72021-10-08 17:33:47 -0700335};
Frederick Maylef7b65d12024-05-14 16:55:14 -0700336LIBBINDER_EXPORTED sp<IServiceManager> createRpcDelegateServiceManager(
Yifan Hong5a05ef72021-10-08 17:33:47 -0700337 const RpcDelegateServiceManagerOptions& options);
Yifan Hongf7760012021-06-04 16:04:42 -0700338#endif
339
Steven Moreland61ff8492019-09-26 16:05:45 -0700340} // namespace android