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