The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Moreland | c7a871e | 2020-11-10 21:56:57 +0000 | [diff] [blame] | 17 | #pragma once |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 18 | #include <binder/IInterface.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | #include <utils/Vector.h> |
| 20 | #include <utils/String16.h> |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 21 | #include <optional> |
| 22 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | namespace android { |
| 24 | |
| 25 | // ---------------------------------------------------------------------- |
| 26 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Service manager for C++ services. |
| 29 | * |
| 30 | * IInterface is only for legacy ABI compatibility |
| 31 | */ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | class IServiceManager : public IInterface |
| 33 | { |
| 34 | public: |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 35 | // for ABI compatibility |
| 36 | virtual const String16& getInterfaceDescriptor() const; |
| 37 | |
| 38 | IServiceManager(); |
| 39 | virtual ~IServiceManager(); |
| 40 | |
Vishnu Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 41 | /** |
Steven Moreland | 635a291 | 2019-10-02 15:50:10 -0700 | [diff] [blame] | 42 | * Must match values in IServiceManager.aidl |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 43 | */ |
Vishnu Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 44 | /* Allows services to dump sections according to priorities. */ |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 45 | 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 Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 48 | /** |
| 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
Steven Moreland | 658fc4f | 2022-03-09 00:30:07 +0000 | [diff] [blame^] | 59 | * 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | */ |
| 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 Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 80 | // NOLINTNEXTLINE(google-default-arguments) |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 81 | virtual status_t addService(const String16& name, const sp<IBinder>& service, |
| 82 | bool allowIsolated = false, |
Vishnu Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 83 | int dumpsysFlags = DUMP_FLAG_PRIORITY_DEFAULT) = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Return list of all existing services. |
| 87 | */ |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 88 | // NOLINTNEXTLINE(google-default-arguments) |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 89 | virtual Vector<String16> listServices(int dumpsysFlags = DUMP_FLAG_PRIORITY_ALL) = 0; |
Steven Moreland | 1c47b58 | 2019-08-27 18:05:27 -0700 | [diff] [blame] | 90 | |
| 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 Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 97 | |
| 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 Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 105 | |
| 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 Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 110 | |
| 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 Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 116 | |
| 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 Chowdhary | 3070094 | 2022-01-31 14:12:40 -0800 | [diff] [blame] | 126 | |
| 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; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | sp<IServiceManager> defaultServiceManager(); |
| 140 | |
Brett Chabot | 38dbade | 2020-01-24 12:59:43 -0800 | [diff] [blame] | 141 | /** |
| 142 | * Directly set the default service manager. Only used for testing. |
Martijn Coenen | 402c867 | 2020-02-03 10:01:36 +0100 | [diff] [blame] | 143 | * Note that the caller is responsible for caling this method |
| 144 | * *before* any call to defaultServiceManager(); if the latter is |
| 145 | * called first, setDefaultServiceManager() will abort. |
Brett Chabot | 38dbade | 2020-01-24 12:59:43 -0800 | [diff] [blame] | 146 | */ |
| 147 | void setDefaultServiceManager(const sp<IServiceManager>& sm); |
| 148 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | template<typename INTERFACE> |
Steven Moreland | 1c47b58 | 2019-08-27 18:05:27 -0700 | [diff] [blame] | 150 | sp<INTERFACE> waitForService(const String16& name) { |
| 151 | const sp<IServiceManager> sm = defaultServiceManager(); |
| 152 | return interface_cast<INTERFACE>(sm->waitForService(name)); |
| 153 | } |
| 154 | |
| 155 | template<typename INTERFACE> |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 156 | sp<INTERFACE> waitForDeclaredService(const String16& name) { |
| 157 | const sp<IServiceManager> sm = defaultServiceManager(); |
| 158 | if (!sm->isDeclared(name)) return nullptr; |
| 159 | return interface_cast<INTERFACE>(sm->waitForService(name)); |
| 160 | } |
| 161 | |
Steven Moreland | f1b02a4 | 2019-11-05 16:14:20 -0800 | [diff] [blame] | 162 | template <typename INTERFACE> |
| 163 | sp<INTERFACE> checkDeclaredService(const String16& name) { |
| 164 | const sp<IServiceManager> sm = defaultServiceManager(); |
| 165 | if (!sm->isDeclared(name)) return nullptr; |
| 166 | return interface_cast<INTERFACE>(sm->checkService(name)); |
| 167 | } |
| 168 | |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 169 | template<typename INTERFACE> |
Steven Moreland | c73571f | 2019-10-31 09:48:58 -0700 | [diff] [blame] | 170 | sp<INTERFACE> waitForVintfService( |
| 171 | const String16& instance = String16("default")) { |
| 172 | return waitForDeclaredService<INTERFACE>( |
| 173 | INTERFACE::descriptor + String16("/") + instance); |
| 174 | } |
| 175 | |
| 176 | template<typename INTERFACE> |
Steven Moreland | f1b02a4 | 2019-11-05 16:14:20 -0800 | [diff] [blame] | 177 | sp<INTERFACE> checkVintfService( |
| 178 | const String16& instance = String16("default")) { |
| 179 | return checkDeclaredService<INTERFACE>( |
| 180 | INTERFACE::descriptor + String16("/") + instance); |
| 181 | } |
| 182 | |
| 183 | template<typename INTERFACE> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | status_t getService(const String16& name, sp<INTERFACE>* outService) |
| 185 | { |
| 186 | const sp<IServiceManager> sm = defaultServiceManager(); |
Yi Kong | 0cf7584 | 2018-07-10 11:44:36 -0700 | [diff] [blame] | 187 | if (sm != nullptr) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | *outService = interface_cast<INTERFACE>(sm->getService(name)); |
Yi Kong | 0cf7584 | 2018-07-10 11:44:36 -0700 | [diff] [blame] | 189 | if ((*outService) != nullptr) return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | } |
| 191 | return NAME_NOT_FOUND; |
| 192 | } |
| 193 | |
| 194 | bool checkCallingPermission(const String16& permission); |
| 195 | bool checkCallingPermission(const String16& permission, |
| 196 | int32_t* outPid, int32_t* outUid); |
Jayant Chowdhary | 49bc34b | 2021-07-28 20:27:21 +0000 | [diff] [blame] | 197 | bool checkPermission(const String16& permission, pid_t pid, uid_t uid, |
| 198 | bool logPermissionFailure = true); |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 199 | |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 200 | #ifndef __ANDROID__ |
| 201 | // Create an IServiceManager that delegates the service manager on the device via adb. |
| 202 | // This is can be set as the default service manager at program start, so that |
| 203 | // defaultServiceManager() returns it: |
| 204 | // int main() { |
| 205 | // setDefaultServiceManager(createRpcDelegateServiceManager()); |
| 206 | // auto sm = defaultServiceManager(); |
| 207 | // // ... |
| 208 | // } |
| 209 | // Resources are cleaned up when the object is destroyed. |
Yifan Hong | 5a05ef7 | 2021-10-08 17:33:47 -0700 | [diff] [blame] | 210 | // |
| 211 | // For each returned binder object, at most |maxOutgoingThreads| outgoing threads are instantiated. |
| 212 | // Hence, only |maxOutgoingThreads| calls can be made simultaneously. Additional calls are blocked |
| 213 | // if there are |maxOutgoingThreads| ongoing calls. See RpcSession::setMaxOutgoingThreads. |
| 214 | // If |maxOutgoingThreads| is not set, default is |RpcSession::kDefaultMaxOutgoingThreads|. |
| 215 | struct RpcDelegateServiceManagerOptions { |
| 216 | std::optional<size_t> maxOutgoingThreads; |
| 217 | }; |
| 218 | sp<IServiceManager> createRpcDelegateServiceManager( |
| 219 | const RpcDelegateServiceManagerOptions& options); |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 220 | #endif |
| 221 | |
Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 222 | } // namespace android |