1 /* 2 * Copyright (C) 2008 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-base/unique_fd.h> 20 #include <utils/Errors.h> 21 #include <utils/RefBase.h> 22 #include <utils/String16.h> 23 #include <utils/Vector.h> 24 25 #include <functional> 26 27 // linux/binder.h defines this, but we don't want to include it here in order to 28 // avoid exporting the kernel headers 29 #ifndef B_PACK_CHARS 30 #define B_PACK_CHARS(c1, c2, c3, c4) \ 31 ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4)) 32 #endif // B_PACK_CHARS 33 34 // --------------------------------------------------------------------------- 35 namespace android { 36 37 class BBinder; 38 class BpBinder; 39 class IInterface; 40 class Parcel; 41 class IResultReceiver; 42 class IShellCallback; 43 44 /** 45 * Base class and low-level protocol for a remotable object. 46 * You can derive from this class to create an object for which other 47 * processes can hold references to it. Communication between processes 48 * (method calls, property get and set) is down through a low-level 49 * protocol implemented on top of the transact() API. 50 */ 51 class [[clang::lto_visibility_public]] IBinder : public virtual RefBase 52 { 53 public: 54 enum { 55 FIRST_CALL_TRANSACTION = 0x00000001, 56 LAST_CALL_TRANSACTION = 0x00ffffff, 57 58 PING_TRANSACTION = B_PACK_CHARS('_', 'P', 'N', 'G'), 59 DUMP_TRANSACTION = B_PACK_CHARS('_', 'D', 'M', 'P'), 60 SHELL_COMMAND_TRANSACTION = B_PACK_CHARS('_', 'C', 'M', 'D'), 61 INTERFACE_TRANSACTION = B_PACK_CHARS('_', 'N', 'T', 'F'), 62 SYSPROPS_TRANSACTION = B_PACK_CHARS('_', 'S', 'P', 'R'), 63 EXTENSION_TRANSACTION = B_PACK_CHARS('_', 'E', 'X', 'T'), 64 DEBUG_PID_TRANSACTION = B_PACK_CHARS('_', 'P', 'I', 'D'), 65 SET_RPC_CLIENT_TRANSACTION = B_PACK_CHARS('_', 'R', 'P', 'C'), 66 67 // See android.os.IBinder.TWEET_TRANSACTION 68 // Most importantly, messages can be anything not exceeding 130 UTF-8 69 // characters, and callees should exclaim "jolly good message old boy!" 70 TWEET_TRANSACTION = B_PACK_CHARS('_', 'T', 'W', 'T'), 71 72 // See android.os.IBinder.LIKE_TRANSACTION 73 // Improve binder self-esteem. 74 LIKE_TRANSACTION = B_PACK_CHARS('_', 'L', 'I', 'K'), 75 76 // Corresponds to TF_ONE_WAY -- an asynchronous call. 77 FLAG_ONEWAY = 0x00000001, 78 79 // Corresponds to TF_CLEAR_BUF -- clear transaction buffers after call 80 // is made 81 FLAG_CLEAR_BUF = 0x00000020, 82 83 // Private userspace flag for transaction which is being requested from 84 // a vendor context. 85 FLAG_PRIVATE_VENDOR = 0x10000000, 86 }; 87 88 IBinder(); 89 90 /** 91 * Check if this IBinder implements the interface named by 92 * @a descriptor. If it does, the base pointer to it is returned, 93 * which you can safely static_cast<> to the concrete C++ interface. 94 */ 95 virtual sp<IInterface> queryLocalInterface(const String16& descriptor); 96 97 /** 98 * Return the canonical name of the interface provided by this IBinder 99 * object. 100 */ 101 virtual const String16& getInterfaceDescriptor() const = 0; 102 103 virtual bool isBinderAlive() const = 0; 104 virtual status_t pingBinder() = 0; 105 virtual status_t dump(int fd, const Vector<String16>& args) = 0; 106 static status_t shellCommand(const sp<IBinder>& target, int in, int out, int err, 107 Vector<String16>& args, const sp<IShellCallback>& callback, 108 const sp<IResultReceiver>& resultReceiver); 109 110 /** 111 * This allows someone to add their own additions to an interface without 112 * having to modify the original interface. 113 * 114 * For instance, imagine if we have this interface: 115 * interface IFoo { void doFoo(); } 116 * 117 * If an unrelated owner (perhaps in a downstream codebase) wants to make a 118 * change to the interface, they have two options: 119 * 120 * A). Historical option that has proven to be BAD! Only the original 121 * author of an interface should change an interface. If someone 122 * downstream wants additional functionality, they should not ever 123 * change the interface or use this method. 124 * 125 * BAD TO DO: interface IFoo { BAD TO DO 126 * BAD TO DO: void doFoo(); BAD TO DO 127 * BAD TO DO: + void doBar(); // adding a method BAD TO DO 128 * BAD TO DO: } BAD TO DO 129 * 130 * B). Option that this method enables! 131 * Leave the original interface unchanged (do not change IFoo!). 132 * Instead, create a new interface in a downstream package: 133 * 134 * package com.<name>; // new functionality in a new package 135 * interface IBar { void doBar(); } 136 * 137 * When registering the interface, add: 138 * sp<MyFoo> foo = new MyFoo; // class in AOSP codebase 139 * sp<MyBar> bar = new MyBar; // custom extension class 140 * foo->setExtension(bar); // use method in BBinder 141 * 142 * Then, clients of IFoo can get this extension: 143 * sp<IBinder> binder = ...; 144 * sp<IFoo> foo = interface_cast<IFoo>(binder); // handle if null 145 * sp<IBinder> barBinder; 146 * ... handle error ... = binder->getExtension(&barBinder); 147 * sp<IBar> bar = interface_cast<IBar>(barBinder); 148 * // if bar is null, then there is no extension or a different 149 * // type of extension 150 */ 151 status_t getExtension(sp<IBinder>* out); 152 153 /** 154 * Dump PID for a binder, for debugging. 155 */ 156 status_t getDebugPid(pid_t* outPid); 157 158 /** 159 * Set the RPC client fd to this binder service, for debugging. This is only available on 160 * debuggable builds. 161 * 162 * When this is called on a binder service, the service: 163 * 1. sets up RPC server 164 * 2. spawns 1 new thread that calls RpcServer::join() 165 * - join() spawns some number of threads that accept() connections; see RpcServer 166 * 167 * setRpcClientDebug() may be called multiple times. Each call will add a new RpcServer 168 * and opens up a TCP port. 169 * 170 * Note: A thread is spawned for each accept()'ed fd, which may call into functions of the 171 * interface freely. See RpcServer::join(). To avoid such race conditions, implement the service 172 * functions with multithreading support. 173 * 174 * On death of @a keepAliveBinder, the RpcServer shuts down. 175 */ 176 [[nodiscard]] status_t setRpcClientDebug(android::base::unique_fd socketFd, 177 const sp<IBinder>& keepAliveBinder); 178 179 // NOLINTNEXTLINE(google-default-arguments) 180 virtual status_t transact( uint32_t code, 181 const Parcel& data, 182 Parcel* reply, 183 uint32_t flags = 0) = 0; 184 185 // DeathRecipient is pure abstract, there is no virtual method 186 // implementation to put in a translation unit in order to silence the 187 // weak vtables warning. 188 #if defined(__clang__) 189 #pragma clang diagnostic push 190 #pragma clang diagnostic ignored "-Wweak-vtables" 191 #endif 192 193 class DeathRecipient : public virtual RefBase 194 { 195 public: 196 virtual void binderDied(const wp<IBinder>& who) = 0; 197 }; 198 199 #if defined(__clang__) 200 #pragma clang diagnostic pop 201 #endif 202 203 /** 204 * Register the @a recipient for a notification if this binder 205 * goes away. If this binder object unexpectedly goes away 206 * (typically because its hosting process has been killed), 207 * then DeathRecipient::binderDied() will be called with a reference 208 * to this. 209 * 210 * The @a cookie is optional -- if non-NULL, it should be a 211 * memory address that you own (that is, you know it is unique). 212 * 213 * @note When all references to the binder being linked to are dropped, the 214 * recipient is automatically unlinked. So, you must hold onto a binder in 215 * order to receive death notifications about it. 216 * 217 * @note You will only receive death notifications for remote binders, 218 * as local binders by definition can't die without you dying as well. 219 * Trying to use this function on a local binder will result in an 220 * INVALID_OPERATION code being returned and nothing happening. 221 * 222 * @note This link always holds a weak reference to its recipient. 223 * 224 * @note You will only receive a weak reference to the dead 225 * binder. You should not try to promote this to a strong reference. 226 * (Nor should you need to, as there is nothing useful you can 227 * directly do with it now that it has passed on.) 228 */ 229 // NOLINTNEXTLINE(google-default-arguments) 230 virtual status_t linkToDeath(const sp<DeathRecipient>& recipient, 231 void* cookie = nullptr, 232 uint32_t flags = 0) = 0; 233 234 /** 235 * Remove a previously registered death notification. 236 * The @a recipient will no longer be called if this object 237 * dies. The @a cookie is optional. If non-NULL, you can 238 * supply a NULL @a recipient, and the recipient previously 239 * added with that cookie will be unlinked. 240 * 241 * If the binder is dead, this will return DEAD_OBJECT. Deleting 242 * the object will also unlink all death recipients. 243 */ 244 // NOLINTNEXTLINE(google-default-arguments) 245 virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient, 246 void* cookie = nullptr, 247 uint32_t flags = 0, 248 wp<DeathRecipient>* outRecipient = nullptr) = 0; 249 250 virtual bool checkSubclass(const void* subclassID) const; 251 252 typedef void (*object_cleanup_func)(const void* id, void* obj, void* cleanupCookie); 253 254 /** 255 * This object is attached for the lifetime of this binder object. When 256 * this binder object is destructed, the cleanup function of all attached 257 * objects are invoked with their respective objectID, object, and 258 * cleanupCookie. Access to these APIs can be made from multiple threads, 259 * but calls from different threads are allowed to be interleaved. 260 * 261 * This returns the object which is already attached. If this returns a 262 * non-null value, it means that attachObject failed (a given objectID can 263 * only be used once). 264 */ 265 [[nodiscard]] virtual void* attachObject(const void* objectID, void* object, 266 void* cleanupCookie, object_cleanup_func func) = 0; 267 /** 268 * Returns object attached with attachObject. 269 */ 270 [[nodiscard]] virtual void* findObject(const void* objectID) const = 0; 271 /** 272 * Returns object attached with attachObject, and detaches it. This does not 273 * delete the object. 274 */ 275 [[nodiscard]] virtual void* detachObject(const void* objectID) = 0; 276 277 /** 278 * Use the lock that this binder contains internally. For instance, this can 279 * be used to modify an attached object without needing to add an additional 280 * lock (though, that attached object must be retrieved before calling this 281 * method). Calling (most) IBinder methods inside this will deadlock. 282 */ 283 void withLock(const std::function<void()>& doWithLock); 284 285 virtual BBinder* localBinder(); 286 virtual BpBinder* remoteBinder(); 287 288 protected: 289 virtual ~IBinder(); 290 291 private: 292 }; 293 294 } // namespace android 295 296 // --------------------------------------------------------------------------- 297