blob: f9cdac7de939425f480c8f9b9c13015cc0139dc9 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
Steven Morelandc7a871e2020-11-10 21:56:57 +000017#pragma once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018
Yifan Hongb1d573d2021-04-23 16:21:44 -070019#include <android-base/unique_fd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <utils/Errors.h>
21#include <utils/RefBase.h>
22#include <utils/String16.h>
23#include <utils/Vector.h>
24
Steven Moreland28723ae2019-04-01 18:52:30 -070025// linux/binder.h defines this, but we don't want to include it here in order to
26// avoid exporting the kernel headers
Casey Dahlinb0343a92015-12-14 16:09:06 -080027#ifndef B_PACK_CHARS
Bart Searse86316a2015-12-05 03:38:34 +000028#define B_PACK_CHARS(c1, c2, c3, c4) \
29 ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
Casey Dahlinb0343a92015-12-14 16:09:06 -080030#endif // B_PACK_CHARS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32// ---------------------------------------------------------------------------
33namespace android {
34
35class BBinder;
36class BpBinder;
37class IInterface;
38class Parcel;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070039class IResultReceiver;
Dianne Hackborn1941a402016-08-29 12:30:43 -070040class IShellCallback;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
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 */
Marissa Wall202f6d12018-10-11 12:28:34 -070049class [[clang::lto_visibility_public]] IBinder : public virtual RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050{
51public:
52 enum {
Yifan Hongdab940e2021-04-23 15:10:14 -070053 FIRST_CALL_TRANSACTION = 0x00000001,
54 LAST_CALL_TRANSACTION = 0x00ffffff,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Yifan Hongdab940e2021-04-23 15:10:14 -070056 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'),
Yifan Hong84bedeb2021-04-21 21:37:17 -070063 SET_RPC_CLIENT_TRANSACTION = B_PACK_CHARS('_', 'R', 'P', 'C'),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
Steven Morelandaa59d362021-02-03 00:16:29 +000065 // See android.os.IBinder.TWEET_TRANSACTION
66 // Most importantly, messages can be anything not exceeding 130 UTF-8
67 // characters, and callees should exclaim "jolly good message old boy!"
Yifan Hongdab940e2021-04-23 15:10:14 -070068 TWEET_TRANSACTION = B_PACK_CHARS('_', 'T', 'W', 'T'),
Steven Morelandaa59d362021-02-03 00:16:29 +000069
70 // See android.os.IBinder.LIKE_TRANSACTION
71 // Improve binder self-esteem.
Yifan Hongdab940e2021-04-23 15:10:14 -070072 LIKE_TRANSACTION = B_PACK_CHARS('_', 'L', 'I', 'K'),
Steven Morelandaa59d362021-02-03 00:16:29 +000073
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -080074 // Corresponds to TF_ONE_WAY -- an asynchronous call.
Yifan Hongdab940e2021-04-23 15:10:14 -070075 FLAG_ONEWAY = 0x00000001,
Steven Moreland46b5fea2019-10-15 11:22:18 -070076
Steven Morelandf183fdd2020-10-27 00:12:12 +000077 // Corresponds to TF_CLEAR_BUF -- clear transaction buffers after call
78 // is made
Yifan Hongdab940e2021-04-23 15:10:14 -070079 FLAG_CLEAR_BUF = 0x00000020,
Steven Morelandf183fdd2020-10-27 00:12:12 +000080
Steven Moreland46b5fea2019-10-15 11:22:18 -070081 // Private userspace flag for transaction which is being requested from
82 // a vendor context.
Yifan Hongdab940e2021-04-23 15:10:14 -070083 FLAG_PRIVATE_VENDOR = 0x10000000,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 };
85
Yifan Hongdab940e2021-04-23 15:10:14 -070086 IBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087
88 /**
89 * Check if this IBinder implements the interface named by
90 * @a descriptor. If it does, the base pointer to it is returned,
91 * which you can safely static_cast<> to the concrete C++ interface.
92 */
93 virtual sp<IInterface> queryLocalInterface(const String16& descriptor);
94
95 /**
96 * Return the canonical name of the interface provided by this IBinder
97 * object.
98 */
Mathias Agopian83c04462009-05-22 19:00:22 -070099 virtual const String16& getInterfaceDescriptor() const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100
101 virtual bool isBinderAlive() const = 0;
102 virtual status_t pingBinder() = 0;
103 virtual status_t dump(int fd, const Vector<String16>& args) = 0;
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700104 static status_t shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -0700105 Vector<String16>& args, const sp<IShellCallback>& callback,
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700106 const sp<IResultReceiver>& resultReceiver);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700108 /**
109 * This allows someone to add their own additions to an interface without
110 * having to modify the original interface.
111 *
112 * For instance, imagine if we have this interface:
113 * interface IFoo { void doFoo(); }
114 *
115 * If an unrelated owner (perhaps in a downstream codebase) wants to make a
116 * change to the interface, they have two options:
117 *
118 * A). Historical option that has proven to be BAD! Only the original
119 * author of an interface should change an interface. If someone
120 * downstream wants additional functionality, they should not ever
121 * change the interface or use this method.
122 *
123 * BAD TO DO: interface IFoo { BAD TO DO
124 * BAD TO DO: void doFoo(); BAD TO DO
125 * BAD TO DO: + void doBar(); // adding a method BAD TO DO
126 * BAD TO DO: } BAD TO DO
127 *
128 * B). Option that this method enables!
129 * Leave the original interface unchanged (do not change IFoo!).
130 * Instead, create a new interface in a downstream package:
131 *
132 * package com.<name>; // new functionality in a new package
133 * interface IBar { void doBar(); }
134 *
135 * When registering the interface, add:
136 * sp<MyFoo> foo = new MyFoo; // class in AOSP codebase
137 * sp<MyBar> bar = new MyBar; // custom extension class
138 * foo->setExtension(bar); // use method in BBinder
139 *
140 * Then, clients of IFoo can get this extension:
141 * sp<IBinder> binder = ...;
142 * sp<IFoo> foo = interface_cast<IFoo>(binder); // handle if null
143 * sp<IBinder> barBinder;
144 * ... handle error ... = binder->getExtension(&barBinder);
145 * sp<IBar> bar = interface_cast<IBar>(barBinder);
146 * // if bar is null, then there is no extension or a different
147 * // type of extension
148 */
149 status_t getExtension(sp<IBinder>* out);
150
Steven Moreland86080b82019-09-23 15:41:18 -0700151 /**
152 * Dump PID for a binder, for debugging.
153 */
154 status_t getDebugPid(pid_t* outPid);
155
Yifan Hong84bedeb2021-04-21 21:37:17 -0700156 /**
157 * Set the RPC client fd to this binder service, for debugging. This is only available on
158 * debuggable builds.
159 *
Yifan Hong84bedeb2021-04-21 21:37:17 -0700160 * When this is called on a binder service, the service:
161 * 1. sets up RPC server
162 * 2. spawns 1 new thread that calls RpcServer::join()
Yifan Hong34823232021-06-07 17:23:00 -0700163 * - join() spawns some number of threads that accept() connections; see RpcServer
Yifan Hong84bedeb2021-04-21 21:37:17 -0700164 *
Yifan Hong8b890852021-06-10 13:44:09 -0700165 * setRpcClientDebug() may be called multiple times. Each call will add a new RpcServer
166 * and opens up a TCP port.
Yifan Hong84bedeb2021-04-21 21:37:17 -0700167 *
168 * Note: A thread is spawned for each accept()'ed fd, which may call into functions of the
169 * interface freely. See RpcServer::join(). To avoid such race conditions, implement the service
170 * functions with multithreading support.
Yifan Hong02530ec2021-06-10 13:38:38 -0700171 *
172 * On death of @a keepAliveBinder, the RpcServer shuts down.
Yifan Hong84bedeb2021-04-21 21:37:17 -0700173 */
Yifan Hong02530ec2021-06-10 13:38:38 -0700174 [[nodiscard]] status_t setRpcClientDebug(android::base::unique_fd socketFd,
175 const sp<IBinder>& keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700176
Jiyong Parkb86c8662018-10-29 23:01:57 +0900177 // NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 virtual status_t transact( uint32_t code,
179 const Parcel& data,
180 Parcel* reply,
181 uint32_t flags = 0) = 0;
182
Colin Cross97b64db2016-09-26 13:48:02 -0700183 // DeathRecipient is pure abstract, there is no virtual method
184 // implementation to put in a translation unit in order to silence the
185 // weak vtables warning.
186 #if defined(__clang__)
187 #pragma clang diagnostic push
188 #pragma clang diagnostic ignored "-Wweak-vtables"
189 #endif
190
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 class DeathRecipient : public virtual RefBase
192 {
193 public:
194 virtual void binderDied(const wp<IBinder>& who) = 0;
195 };
196
Colin Cross97b64db2016-09-26 13:48:02 -0700197 #if defined(__clang__)
198 #pragma clang diagnostic pop
199 #endif
200
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201 /**
202 * Register the @a recipient for a notification if this binder
203 * goes away. If this binder object unexpectedly goes away
204 * (typically because its hosting process has been killed),
Christopher Tate71f64dd2011-02-17 13:00:38 -0800205 * then DeathRecipient::binderDied() will be called with a reference
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 * to this.
207 *
208 * The @a cookie is optional -- if non-NULL, it should be a
209 * memory address that you own (that is, you know it is unique).
210 *
Steven Morelandea2ab0b2020-03-31 14:27:20 -0700211 * @note When all references to the binder being linked to are dropped, the
212 * recipient is automatically unlinked. So, you must hold onto a binder in
213 * order to receive death notifications about it.
214 *
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 * @note You will only receive death notifications for remote binders,
216 * as local binders by definition can't die without you dying as well.
217 * Trying to use this function on a local binder will result in an
218 * INVALID_OPERATION code being returned and nothing happening.
219 *
220 * @note This link always holds a weak reference to its recipient.
221 *
222 * @note You will only receive a weak reference to the dead
223 * binder. You should not try to promote this to a strong reference.
224 * (Nor should you need to, as there is nothing useful you can
225 * directly do with it now that it has passed on.)
226 */
Jiyong Parkb86c8662018-10-29 23:01:57 +0900227 // NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 virtual status_t linkToDeath(const sp<DeathRecipient>& recipient,
Yi Kong87d465c2018-07-24 01:14:06 -0700229 void* cookie = nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 uint32_t flags = 0) = 0;
231
232 /**
233 * Remove a previously registered death notification.
234 * The @a recipient will no longer be called if this object
235 * dies. The @a cookie is optional. If non-NULL, you can
236 * supply a NULL @a recipient, and the recipient previously
237 * added with that cookie will be unlinked.
Steven Moreland64127ca2019-03-13 09:25:44 -0700238 *
239 * If the binder is dead, this will return DEAD_OBJECT. Deleting
240 * the object will also unlink all death recipients.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 */
Jiyong Parkb86c8662018-10-29 23:01:57 +0900242 // NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient,
Yi Kong87d465c2018-07-24 01:14:06 -0700244 void* cookie = nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 uint32_t flags = 0,
Yi Kong87d465c2018-07-24 01:14:06 -0700246 wp<DeathRecipient>* outRecipient = nullptr) = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247
248 virtual bool checkSubclass(const void* subclassID) const;
249
250 typedef void (*object_cleanup_func)(const void* id, void* obj, void* cleanupCookie);
251
Steven Moreland20380082018-09-13 15:43:10 -0700252 /**
253 * This object is attached for the lifetime of this binder object. When
254 * this binder object is destructed, the cleanup function of all attached
255 * objects are invoked with their respective objectID, object, and
256 * cleanupCookie. Access to these APIs can be made from multiple threads,
257 * but calls from different threads are allowed to be interleaved.
258 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 virtual void attachObject( const void* objectID,
260 void* object,
261 void* cleanupCookie,
262 object_cleanup_func func) = 0;
Steven Moreland20380082018-09-13 15:43:10 -0700263 /**
264 * Returns object attached with attachObject.
265 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 virtual void* findObject(const void* objectID) const = 0;
Steven Moreland20380082018-09-13 15:43:10 -0700267 /**
268 * WARNING: this API does not call the cleanup function for legacy reasons.
269 * It also does not return void* for legacy reasons. If you need to detach
270 * an object and destroy it, there are two options:
271 * - if you can, don't call detachObject and instead wait for the destructor
272 * to clean it up.
273 * - manually retrieve and destruct the object (if multiple of your threads
274 * are accessing these APIs, you must guarantee that attachObject isn't
275 * called after findObject and before detachObject is called).
276 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277 virtual void detachObject(const void* objectID) = 0;
278
279 virtual BBinder* localBinder();
280 virtual BpBinder* remoteBinder();
281
282protected:
Mathias Agopian83c04462009-05-22 19:00:22 -0700283 virtual ~IBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284
285private:
286};
287
Steven Moreland61ff8492019-09-26 16:05:45 -0700288} // namespace android
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289
290// ---------------------------------------------------------------------------