blob: 413c97f349229766ca99a6f3ee8701ee20219508 [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
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070018#include <binder/IInterface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <utils/Vector.h>
20#include <utils/String16.h>
Steven Morelandedd4e072021-04-21 00:27:29 +000021#include <optional>
22
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023namespace android {
24
25// ----------------------------------------------------------------------
26
Steven Moreland583685e2019-10-02 16:45:11 -070027/**
28 * Service manager for C++ services.
29 *
30 * IInterface is only for legacy ABI compatibility
31 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032class IServiceManager : public IInterface
33{
34public:
Steven Moreland583685e2019-10-02 16:45:11 -070035 // for ABI compatibility
36 virtual const String16& getInterfaceDescriptor() const;
37
38 IServiceManager();
39 virtual ~IServiceManager();
40
Vishnu Nair64afc022018-02-01 15:29:34 -080041 /**
Steven Moreland635a2912019-10-02 15:50:10 -070042 * Must match values in IServiceManager.aidl
Vishnu Nairf56042d2017-09-19 15:25:10 -070043 */
Vishnu Nair64afc022018-02-01 15:29:34 -080044 /* Allows services to dump sections according to priorities. */
Vishnu Nair6a408532017-10-24 09:11:27 -070045 static const int DUMP_FLAG_PRIORITY_CRITICAL = 1 << 0;
46 static const int DUMP_FLAG_PRIORITY_HIGH = 1 << 1;
47 static const int DUMP_FLAG_PRIORITY_NORMAL = 1 << 2;
Vishnu Nair64afc022018-02-01 15:29:34 -080048 /**
49 * Services are by default registered with a DEFAULT dump priority. DEFAULT priority has the
50 * same priority as NORMAL priority but the services are not called with dump priority
51 * arguments.
52 */
53 static const int DUMP_FLAG_PRIORITY_DEFAULT = 1 << 3;
54 static const int DUMP_FLAG_PRIORITY_ALL = DUMP_FLAG_PRIORITY_CRITICAL |
55 DUMP_FLAG_PRIORITY_HIGH | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PRIORITY_DEFAULT;
56 static const int DUMP_FLAG_PROTO = 1 << 4;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
58 /**
Steven Moreland658fc4f2022-03-09 00:30:07 +000059 * Retrieve an existing service, blocking for a few seconds if it doesn't yet exist. This
60 * does polling. A more efficient way to make sure you unblock as soon as the service is
61 * available is to use waitForService or to use service notifications.
62 *
63 * Warning: when using this API, typically, you should call it in a loop. It's dangerous to
64 * assume that nullptr could mean that the service is not available. The service could just
65 * be starting. Generally, whether a service exists, this information should be declared
66 * externally (for instance, an Android feature might imply the existence of a service,
67 * a system property, or in the case of services in the VINTF manifest, it can be checked
68 * with isDeclared).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069 */
70 virtual sp<IBinder> getService( const String16& name) const = 0;
71
72 /**
73 * Retrieve an existing service, non-blocking.
74 */
75 virtual sp<IBinder> checkService( const String16& name) const = 0;
76
77 /**
78 * Register a service.
79 */
Jiyong Parkb86c8662018-10-29 23:01:57 +090080 // NOLINTNEXTLINE(google-default-arguments)
Vishnu Nairf56042d2017-09-19 15:25:10 -070081 virtual status_t addService(const String16& name, const sp<IBinder>& service,
82 bool allowIsolated = false,
Vishnu Nair64afc022018-02-01 15:29:34 -080083 int dumpsysFlags = DUMP_FLAG_PRIORITY_DEFAULT) = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084
85 /**
86 * Return list of all existing services.
87 */
Jiyong Parkb86c8662018-10-29 23:01:57 +090088 // NOLINTNEXTLINE(google-default-arguments)
Vishnu Nair6a408532017-10-24 09:11:27 -070089 virtual Vector<String16> listServices(int dumpsysFlags = DUMP_FLAG_PRIORITY_ALL) = 0;
Steven Moreland1c47b582019-08-27 18:05:27 -070090
91 /**
92 * Efficiently wait for a service.
93 *
94 * Returns nullptr only for permission problem or fatal error.
95 */
96 virtual sp<IBinder> waitForService(const String16& name) = 0;
Steven Morelandb82b8f82019-10-28 10:52:34 -070097
98 /**
99 * Check if a service is declared (e.g. VINTF manifest).
100 *
101 * If this returns true, waitForService should always be able to return the
102 * service.
103 */
104 virtual bool isDeclared(const String16& name) = 0;
Steven Moreland2e293aa2020-09-23 00:25:16 +0000105
106 /**
107 * Get all instances of a service as declared in the VINTF manifest
108 */
109 virtual Vector<String16> getDeclaredInstances(const String16& interface) = 0;
Steven Morelandedd4e072021-04-21 00:27:29 +0000110
111 /**
112 * If this instance is updatable via an APEX, returns the APEX with which
113 * this can be updated.
114 */
115 virtual std::optional<String16> updatableViaApex(const String16& name) = 0;
Devin Moore5e4c2f12021-09-09 22:36:33 +0000116
117 /**
118 * If this instance has declared remote connection information, returns
119 * the ConnectionInfo.
120 */
121 struct ConnectionInfo {
122 std::string ipAddress;
123 unsigned int port;
124 };
125 virtual std::optional<ConnectionInfo> getConnectionInfo(const String16& name) = 0;
Jayant Chowdhary30700942022-01-31 14:12:40 -0800126
127 struct LocalRegistrationCallback : public virtual RefBase {
128 virtual void onServiceRegistration(const String16& instance, const sp<IBinder>& binder) = 0;
129 virtual ~LocalRegistrationCallback() {}
130 };
131
132 virtual status_t registerForNotifications(const String16& name,
133 const sp<LocalRegistrationCallback>& callback) = 0;
134
135 virtual status_t unregisterForNotifications(const String16& name,
136 const sp<LocalRegistrationCallback>& callback) = 0;
Jayant Chowdharya0a8eb22022-05-20 03:30:09 +0000137
138 struct ServiceDebugInfo {
139 std::string name;
140 int pid;
141 };
142 virtual std::vector<ServiceDebugInfo> getServiceDebugInfo() = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143};
144
145sp<IServiceManager> defaultServiceManager();
146
Brett Chabot38dbade2020-01-24 12:59:43 -0800147/**
148 * Directly set the default service manager. Only used for testing.
Martijn Coenen402c8672020-02-03 10:01:36 +0100149 * Note that the caller is responsible for caling this method
150 * *before* any call to defaultServiceManager(); if the latter is
151 * called first, setDefaultServiceManager() will abort.
Brett Chabot38dbade2020-01-24 12:59:43 -0800152 */
153void setDefaultServiceManager(const sp<IServiceManager>& sm);
154
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155template<typename INTERFACE>
Steven Moreland1c47b582019-08-27 18:05:27 -0700156sp<INTERFACE> waitForService(const String16& name) {
157 const sp<IServiceManager> sm = defaultServiceManager();
158 return interface_cast<INTERFACE>(sm->waitForService(name));
159}
160
161template<typename INTERFACE>
Steven Morelandb82b8f82019-10-28 10:52:34 -0700162sp<INTERFACE> waitForDeclaredService(const String16& name) {
163 const sp<IServiceManager> sm = defaultServiceManager();
164 if (!sm->isDeclared(name)) return nullptr;
165 return interface_cast<INTERFACE>(sm->waitForService(name));
166}
167
Steven Morelandf1b02a42019-11-05 16:14:20 -0800168template <typename INTERFACE>
169sp<INTERFACE> checkDeclaredService(const String16& name) {
170 const sp<IServiceManager> sm = defaultServiceManager();
171 if (!sm->isDeclared(name)) return nullptr;
172 return interface_cast<INTERFACE>(sm->checkService(name));
173}
174
Steven Morelandb82b8f82019-10-28 10:52:34 -0700175template<typename INTERFACE>
Steven Morelandc73571f2019-10-31 09:48:58 -0700176sp<INTERFACE> waitForVintfService(
177 const String16& instance = String16("default")) {
178 return waitForDeclaredService<INTERFACE>(
179 INTERFACE::descriptor + String16("/") + instance);
180}
181
182template<typename INTERFACE>
Steven Morelandf1b02a42019-11-05 16:14:20 -0800183sp<INTERFACE> checkVintfService(
184 const String16& instance = String16("default")) {
185 return checkDeclaredService<INTERFACE>(
186 INTERFACE::descriptor + String16("/") + instance);
187}
188
189template<typename INTERFACE>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190status_t getService(const String16& name, sp<INTERFACE>* outService)
191{
192 const sp<IServiceManager> sm = defaultServiceManager();
Yi Kong0cf75842018-07-10 11:44:36 -0700193 if (sm != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 *outService = interface_cast<INTERFACE>(sm->getService(name));
Yi Kong0cf75842018-07-10 11:44:36 -0700195 if ((*outService) != nullptr) return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 }
197 return NAME_NOT_FOUND;
198}
199
200bool checkCallingPermission(const String16& permission);
201bool checkCallingPermission(const String16& permission,
202 int32_t* outPid, int32_t* outUid);
Jayant Chowdhary49bc34b2021-07-28 20:27:21 +0000203bool checkPermission(const String16& permission, pid_t pid, uid_t uid,
204 bool logPermissionFailure = true);
Mathias Agopian375f5632009-06-15 18:24:59 -0700205
Yifan Hongf7760012021-06-04 16:04:42 -0700206#ifndef __ANDROID__
207// Create an IServiceManager that delegates the service manager on the device via adb.
208// This is can be set as the default service manager at program start, so that
209// defaultServiceManager() returns it:
210// int main() {
211// setDefaultServiceManager(createRpcDelegateServiceManager());
212// auto sm = defaultServiceManager();
213// // ...
214// }
215// Resources are cleaned up when the object is destroyed.
Yifan Hong5a05ef72021-10-08 17:33:47 -0700216//
217// For each returned binder object, at most |maxOutgoingThreads| outgoing threads are instantiated.
218// Hence, only |maxOutgoingThreads| calls can be made simultaneously. Additional calls are blocked
219// if there are |maxOutgoingThreads| ongoing calls. See RpcSession::setMaxOutgoingThreads.
220// If |maxOutgoingThreads| is not set, default is |RpcSession::kDefaultMaxOutgoingThreads|.
221struct RpcDelegateServiceManagerOptions {
222 std::optional<size_t> maxOutgoingThreads;
223};
224sp<IServiceManager> createRpcDelegateServiceManager(
225 const RpcDelegateServiceManagerOptions& options);
Yifan Hongf7760012021-06-04 16:04:42 -0700226#endif
227
Steven Moreland61ff8492019-09-26 16:05:45 -0700228} // namespace android