blob: 50a3f4c010510918a9c3f9217550feb6f4aa5463 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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
17package android.os;
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.util.Log;
Dianne Hackborn017c6a22014-09-25 17:41:34 -070020import android.util.Slog;
Dianne Hackborndb4e33f2013-04-01 17:28:16 -070021import com.android.internal.util.FastPrintWriter;
Dianne Hackborn9461b6f2015-10-07 17:33:16 -070022import libcore.io.IoUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24import java.io.FileDescriptor;
25import java.io.FileOutputStream;
26import java.io.IOException;
27import java.io.PrintWriter;
28import java.lang.ref.WeakReference;
29import java.lang.reflect.Modifier;
30
31/**
32 * Base class for a remotable object, the core part of a lightweight
33 * remote procedure call mechanism defined by {@link IBinder}.
34 * This class is an implementation of IBinder that provides
Dianne Hackbornab4a81b2014-10-09 17:59:38 -070035 * standard local implementation of such an object.
36 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 * <p>Most developers will not implement this class directly, instead using the
Scott Main40eee612012-08-06 17:48:37 -070038 * <a href="{@docRoot}guide/components/aidl.html">aidl</a> tool to describe the desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 * interface, having it generate the appropriate Binder subclass. You can,
40 * however, derive directly from Binder to implement your own custom RPC
41 * protocol or simply instantiate a raw Binder object directly to use as a
42 * token that can be shared across processes.
Dianne Hackbornab4a81b2014-10-09 17:59:38 -070043 *
44 * <p>This class is just a basic IPC primitive; it has no impact on an application's
45 * lifecycle, and is valid only as long as the process that created it continues to run.
46 * To use this correctly, you must be doing so within the context of a top-level
47 * application component (a {@link android.app.Service}, {@link android.app.Activity},
48 * or {@link android.content.ContentProvider}) that lets the system know your process
49 * should remain running.</p>
50 *
51 * <p>You must keep in mind the situations in which your process
52 * could go away, and thus require that you later re-create a new Binder and re-attach
53 * it when the process starts again. For example, if you are using this within an
54 * {@link android.app.Activity}, your activity's process may be killed any time the
55 * activity is not started; if the activity is later re-created you will need to
56 * create a new Binder and hand it back to the correct place again; you need to be
57 * aware that your process may be started for another reason (for example to receive
58 * a broadcast) that will not involve re-creating the activity and thus run its code
59 * to create a new Binder.</p>
60 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 * @see IBinder
62 */
63public class Binder implements IBinder {
64 /*
65 * Set this flag to true to detect anonymous, local or member classes
66 * that extend this Binder class and that are not static. These kind
67 * of classes can potentially create leaks.
68 */
69 private static final boolean FIND_POTENTIAL_LEAKS = false;
Dianne Hackborn73d6a822014-09-29 10:52:47 -070070 private static final boolean CHECK_PARCEL_SIZE = false;
Dianne Hackborn017c6a22014-09-25 17:41:34 -070071 static final String TAG = "Binder";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
Makoto Onukif9b941f2016-03-10 17:19:08 -080073 /** @hide */
74 public static boolean LOG_RUNTIME_EXCEPTION = false; // DO NOT SUBMIT WITH TRUE
75
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -070076 /**
77 * Control whether dump() calls are allowed.
78 */
79 private static String sDumpDisabled = null;
80
Rahul Chaturvedi52613f92015-06-17 23:54:08 -040081 /**
82 * Global transaction tracker instance for this process.
83 */
84 private static TransactionTracker sTransactionTracker = null;
85
86 // Transaction tracking code.
87
88 /**
89 * Flag indicating whether we should be tracing transact calls.
90 *
91 */
92 private static boolean sTracingEnabled = false;
93
94 /**
95 * Enable Binder IPC tracing.
96 *
97 * @hide
98 */
99 public static void enableTracing() {
100 sTracingEnabled = true;
101 };
102
103 /**
104 * Disable Binder IPC tracing.
105 *
106 * @hide
107 */
108 public static void disableTracing() {
109 sTracingEnabled = false;
110 }
111
112 /**
113 * Check if binder transaction tracing is enabled.
114 *
115 * @hide
116 */
117 public static boolean isTracingEnabled() {
118 return sTracingEnabled;
119 }
120
121 /**
122 * Get the binder transaction tracker for this process.
123 *
124 * @hide
125 */
126 public synchronized static TransactionTracker getTransactionTracker() {
127 if (sTransactionTracker == null)
128 sTransactionTracker = new TransactionTracker();
129 return sTransactionTracker;
130 }
131
Jeff Sharkey13245312016-11-21 09:13:52 -0700132 /** @hide */
133 public static IBinder allowBlocking(IBinder binder) {
134 // NOTE: real implementation on internal branch
135 return binder;
136 }
137
138 /** @hide */
139 public static void copyAllowBlocking(IBinder fromBinder, IBinder toBinder) {
140 // NOTE: real implementation on internal branch
141 }
142
Amith Yamasani742a6712011-05-04 14:49:28 -0700143 /* mObject is used by native code, do not remove or rename */
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000144 private long mObject;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 private IInterface mOwner;
146 private String mDescriptor;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 /**
149 * Return the ID of the process that sent you the current transaction
150 * that is being processed. This pid can be used with higher-level
151 * system services to determine its identity and check permissions.
152 * If the current thread is not currently executing an incoming transaction,
153 * then its own pid is returned.
154 */
155 public static final native int getCallingPid();
156
157 /**
Dianne Hackborn74ee8652012-09-07 18:33:18 -0700158 * Return the Linux uid assigned to the process that sent you the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 * current transaction that is being processed. This uid can be used with
160 * higher-level system services to determine its identity and check
161 * permissions. If the current thread is not currently executing an
162 * incoming transaction, then its own uid is returned.
163 */
164 public static final native int getCallingUid();
Amith Yamasani742a6712011-05-04 14:49:28 -0700165
166 /**
Dianne Hackborn74ee8652012-09-07 18:33:18 -0700167 * Return the UserHandle assigned to the process that sent you the
168 * current transaction that is being processed. This is the user
169 * of the caller. It is distinct from {@link #getCallingUid()} in that a
170 * particular user will have multiple distinct apps running under it each
171 * with their own uid. If the current thread is not currently executing an
172 * incoming transaction, then its own UserHandle is returned.
173 */
174 public static final UserHandle getCallingUserHandle() {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700175 return UserHandle.of(UserHandle.getUserId(getCallingUid()));
Dianne Hackborn74ee8652012-09-07 18:33:18 -0700176 }
177
178 /**
Brad Fitzpatricka0527f22010-03-25 20:40:34 -0700179 * Reset the identity of the incoming IPC on the current thread. This can
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 * be useful if, while handling an incoming call, you will be calling
181 * on interfaces of other objects that may be local to your process and
182 * need to do permission checks on the calls coming into them (so they
183 * will check the permission of your own local process, and not whatever
184 * process originally called you).
Brad Fitzpatricka0527f22010-03-25 20:40:34 -0700185 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * @return Returns an opaque token that can be used to restore the
187 * original calling identity by passing it to
188 * {@link #restoreCallingIdentity(long)}.
Brad Fitzpatricka0527f22010-03-25 20:40:34 -0700189 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 * @see #getCallingPid()
191 * @see #getCallingUid()
192 * @see #restoreCallingIdentity(long)
193 */
194 public static final native long clearCallingIdentity();
Brad Fitzpatricka0527f22010-03-25 20:40:34 -0700195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 /**
Brad Fitzpatricka0527f22010-03-25 20:40:34 -0700197 * Restore the identity of the incoming IPC on the current thread
198 * back to a previously identity that was returned by {@link
199 * #clearCallingIdentity}.
200 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * @param token The opaque token that was previously returned by
202 * {@link #clearCallingIdentity}.
Brad Fitzpatricka0527f22010-03-25 20:40:34 -0700203 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * @see #clearCallingIdentity
205 */
206 public static final native void restoreCallingIdentity(long token);
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700207
208 /**
209 * Sets the native thread-local StrictMode policy mask.
210 *
211 * <p>The StrictMode settings are kept in two places: a Java-level
212 * threadlocal for libcore/Dalvik, and a native threadlocal (set
213 * here) for propagation via Binder calls. This is a little
214 * unfortunate, but necessary to break otherwise more unfortunate
215 * dependencies either of Dalvik on Android, or Android
216 * native-only code on Dalvik.
217 *
218 * @see StrictMode
219 * @hide
220 */
221 public static final native void setThreadStrictModePolicy(int policyMask);
222
223 /**
224 * Gets the current native thread-local StrictMode policy mask.
225 *
226 * @see #setThreadStrictModePolicy
227 * @hide
228 */
229 public static final native int getThreadStrictModePolicy();
230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 /**
232 * Flush any Binder commands pending in the current thread to the kernel
233 * driver. This can be
234 * useful to call before performing an operation that may block for a long
235 * time, to ensure that any pending object references have been released
236 * in order to prevent the process from holding on to objects longer than
237 * it needs to.
238 */
239 public static final native void flushPendingCommands();
240
241 /**
242 * Add the calling thread to the IPC thread pool. This function does
243 * not return until the current process is exiting.
244 */
245 public static final native void joinThreadPool();
Jeff Brown1951ce82013-04-04 22:45:12 -0700246
247 /**
248 * Returns true if the specified interface is a proxy.
249 * @hide
250 */
251 public static final boolean isProxy(IInterface iface) {
252 return iface.asBinder() != iface;
253 }
254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
Wale Ogunwaled7fdd022015-04-13 16:22:38 -0700256 * Call blocks until the number of executing binder threads is less
257 * than the maximum number of binder threads allowed for this process.
Wale Ogunwale8d906342015-04-15 09:10:03 -0700258 * @hide
Wale Ogunwaled7fdd022015-04-13 16:22:38 -0700259 */
260 public static final native void blockUntilThreadAvailable();
261
262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 * Default constructor initializes the object.
264 */
265 public Binder() {
266 init();
267
268 if (FIND_POTENTIAL_LEAKS) {
269 final Class<? extends Binder> klass = getClass();
270 if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
271 (klass.getModifiers() & Modifier.STATIC) == 0) {
272 Log.w(TAG, "The following Binder class should be static or leaks might occur: " +
273 klass.getCanonicalName());
274 }
275 }
276 }
277
278 /**
279 * Convenience method for associating a specific interface with the Binder.
280 * After calling, queryLocalInterface() will be implemented for you
281 * to return the given owner IInterface when the corresponding
282 * descriptor is requested.
283 */
284 public void attachInterface(IInterface owner, String descriptor) {
285 mOwner = owner;
286 mDescriptor = descriptor;
287 }
288
289 /**
290 * Default implementation returns an empty interface name.
291 */
292 public String getInterfaceDescriptor() {
293 return mDescriptor;
294 }
295
296 /**
297 * Default implementation always returns true -- if you got here,
298 * the object is alive.
299 */
300 public boolean pingBinder() {
301 return true;
302 }
303
304 /**
305 * {@inheritDoc}
306 *
307 * Note that if you're calling on a local binder, this always returns true
308 * because your process is alive if you're calling it.
309 */
310 public boolean isBinderAlive() {
311 return true;
312 }
313
314 /**
315 * Use information supplied to attachInterface() to return the
316 * associated IInterface if it matches the requested
317 * descriptor.
318 */
319 public IInterface queryLocalInterface(String descriptor) {
320 if (mDescriptor.equals(descriptor)) {
321 return mOwner;
322 }
323 return null;
324 }
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700325
326 /**
327 * Control disabling of dump calls in this process. This is used by the system
328 * process watchdog to disable incoming dump calls while it has detecting the system
329 * is hung and is reporting that back to the activity controller. This is to
330 * prevent the controller from getting hung up on bug reports at this point.
331 * @hide
332 *
333 * @param msg The message to show instead of the dump; if null, dumps are
334 * re-enabled.
335 */
336 public static void setDumpDisabled(String msg) {
337 synchronized (Binder.class) {
338 sDumpDisabled = msg;
339 }
340 }
341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
343 * Default implementation is a stub that returns false. You will want
344 * to override this to do the appropriate unmarshalling of transactions.
345 *
346 * <p>If you want to call this, call transact().
347 */
348 protected boolean onTransact(int code, Parcel data, Parcel reply,
349 int flags) throws RemoteException {
350 if (code == INTERFACE_TRANSACTION) {
351 reply.writeString(getInterfaceDescriptor());
352 return true;
353 } else if (code == DUMP_TRANSACTION) {
354 ParcelFileDescriptor fd = data.readFileDescriptor();
355 String[] args = data.readStringArray();
356 if (fd != null) {
357 try {
358 dump(fd.getFileDescriptor(), args);
359 } finally {
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700360 IoUtils.closeQuietly(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
362 }
Brad Fitzpatrickeb75888e2010-07-26 17:47:45 -0700363 // Write the StrictMode header.
364 if (reply != null) {
365 reply.writeNoException();
366 } else {
367 StrictMode.clearGatheredViolations();
368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 return true;
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700370 } else if (code == SHELL_COMMAND_TRANSACTION) {
371 ParcelFileDescriptor in = data.readFileDescriptor();
372 ParcelFileDescriptor out = data.readFileDescriptor();
373 ParcelFileDescriptor err = data.readFileDescriptor();
374 String[] args = data.readStringArray();
375 ResultReceiver resultReceiver = ResultReceiver.CREATOR.createFromParcel(data);
376 try {
377 if (out != null) {
378 shellCommand(in != null ? in.getFileDescriptor() : null,
379 out.getFileDescriptor(),
380 err != null ? err.getFileDescriptor() : out.getFileDescriptor(),
381 args, resultReceiver);
382 }
383 } finally {
384 IoUtils.closeQuietly(in);
385 IoUtils.closeQuietly(out);
386 IoUtils.closeQuietly(err);
387 // Write the StrictMode header.
388 if (reply != null) {
389 reply.writeNoException();
390 } else {
391 StrictMode.clearGatheredViolations();
392 }
393 }
394 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 }
396 return false;
397 }
398
399 /**
400 * Implemented to call the more convenient version
401 * {@link #dump(FileDescriptor, PrintWriter, String[])}.
402 */
403 public void dump(FileDescriptor fd, String[] args) {
404 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborndb4e33f2013-04-01 17:28:16 -0700405 PrintWriter pw = new FastPrintWriter(fout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 try {
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700407 doDump(fd, pw, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 } finally {
409 pw.flush();
410 }
411 }
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700412
413 void doDump(FileDescriptor fd, PrintWriter pw, String[] args) {
414 final String disabled;
415 synchronized (Binder.class) {
416 disabled = sDumpDisabled;
417 }
418 if (disabled == null) {
419 try {
420 dump(fd, pw, args);
421 } catch (SecurityException e) {
422 pw.println("Security exception: " + e.getMessage());
423 throw e;
424 } catch (Throwable e) {
425 // Unlike usual calls, in this case if an exception gets thrown
426 // back to us we want to print it back in to the dump data, since
427 // that is where the caller expects all interesting information to
428 // go.
429 pw.println();
430 pw.println("Exception occurred while dumping:");
431 e.printStackTrace(pw);
432 }
433 } else {
434 pw.println(sDumpDisabled);
435 }
436 }
437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 /**
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700439 * Like {@link #dump(FileDescriptor, String[])}, but ensures the target
440 * executes asynchronously.
441 */
442 public void dumpAsync(final FileDescriptor fd, final String[] args) {
443 final FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborndb4e33f2013-04-01 17:28:16 -0700444 final PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700445 Thread thr = new Thread("Binder.dumpAsync") {
446 public void run() {
447 try {
448 dump(fd, pw, args);
449 } finally {
450 pw.flush();
451 }
452 }
453 };
454 thr.start();
455 }
456
457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 * Print the object's state into the given stream.
459 *
460 * @param fd The raw file descriptor that the dump is being sent to.
461 * @param fout The file to which you should dump your state. This will be
462 * closed for you after you return.
463 * @param args additional arguments to the dump request.
464 */
465 protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) {
466 }
467
468 /**
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700469 * @param in The raw file descriptor that an input data stream can be read from.
470 * @param out The raw file descriptor that normal command messages should be written to.
471 * @param err The raw file descriptor that command error messages should be written to.
472 * @param args Command-line arguments.
473 * @param resultReceiver Called when the command has finished executing, with the result code.
474 * @throws RemoteException
475 * @hide
476 */
477 public void shellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
478 String[] args, ResultReceiver resultReceiver) throws RemoteException {
479 onShellCommand(in, out, err, args, resultReceiver);
480 }
481
482 /**
483 * Handle a call to {@link #shellCommand}. The default implementation simply prints
484 * an error message. Override and replace with your own.
Dianne Hackborn2e931f52016-01-28 12:21:17 -0800485 * <p class="caution">Note: no permission checking is done before calling this method; you must
486 * apply any security checks as appropriate for the command being executed.
487 * Consider using {@link ShellCommand} to help in the implementation.</p>
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700488 * @hide
489 */
490 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
491 String[] args, ResultReceiver resultReceiver) throws RemoteException {
492 FileOutputStream fout = new FileOutputStream(err != null ? err : out);
493 PrintWriter pw = new FastPrintWriter(fout);
494 pw.println("No shell command implementation.");
495 pw.flush();
496 resultReceiver.send(0, null);
497 }
498
499 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 * Default implementation rewinds the parcels and calls onTransact. On
501 * the remote side, transact calls into the binder to do the IPC.
502 */
503 public final boolean transact(int code, Parcel data, Parcel reply,
504 int flags) throws RemoteException {
Joe Onorato43a17652011-04-06 19:22:23 -0700505 if (false) Log.v("Binder", "Transact: " + code + " to " + this);
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 if (data != null) {
508 data.setDataPosition(0);
509 }
510 boolean r = onTransact(code, data, reply, flags);
511 if (reply != null) {
512 reply.setDataPosition(0);
513 }
514 return r;
515 }
516
517 /**
518 * Local implementation is a no-op.
519 */
520 public void linkToDeath(DeathRecipient recipient, int flags) {
521 }
522
523 /**
524 * Local implementation is a no-op.
525 */
526 public boolean unlinkToDeath(DeathRecipient recipient, int flags) {
527 return true;
528 }
529
530 protected void finalize() throws Throwable {
531 try {
532 destroy();
533 } finally {
534 super.finalize();
535 }
536 }
Dianne Hackborn017c6a22014-09-25 17:41:34 -0700537
Dianne Hackbornfad079d2014-09-26 15:46:24 -0700538 static void checkParcel(IBinder obj, int code, Parcel parcel, String msg) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -0700539 if (CHECK_PARCEL_SIZE && parcel.dataSize() >= 800*1024) {
Dianne Hackborn017c6a22014-09-25 17:41:34 -0700540 // Trying to send > 800k, this is way too much
Dianne Hackbornfad079d2014-09-26 15:46:24 -0700541 StringBuilder sb = new StringBuilder();
542 sb.append(msg);
543 sb.append(": on ");
544 sb.append(obj);
545 sb.append(" calling ");
546 sb.append(code);
547 sb.append(" size ");
548 sb.append(parcel.dataSize());
549 sb.append(" (data: ");
550 parcel.setDataPosition(0);
551 sb.append(parcel.readInt());
552 sb.append(", ");
553 sb.append(parcel.readInt());
554 sb.append(", ");
555 sb.append(parcel.readInt());
556 sb.append(")");
557 Slog.wtfStack(TAG, sb.toString());
Dianne Hackborn017c6a22014-09-25 17:41:34 -0700558 }
559 }
560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 private native final void init();
562 private native final void destroy();
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700563
564 // Entry point from android_util_Binder.cpp's onTransact
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000565 private boolean execTransact(int code, long dataObj, long replyObj,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 int flags) {
567 Parcel data = Parcel.obtain(dataObj);
568 Parcel reply = Parcel.obtain(replyObj);
569 // theoretically, we should call transact, which will call onTransact,
570 // but all that does is rewind it, and we just got these from an IPC,
571 // so we'll just call it directly.
572 boolean res;
Igor Murashkin7eb6cfe2013-08-16 14:07:11 -0700573 // Log any exceptions as warnings, don't silently suppress them.
574 // If the call was FLAG_ONEWAY then these exceptions disappear into the ether.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 try {
576 res = onTransact(code, data, reply, flags);
Makoto Onukif9b941f2016-03-10 17:19:08 -0800577 } catch (RemoteException|RuntimeException e) {
578 if (LOG_RUNTIME_EXCEPTION) {
Igor Murashkin7eb6cfe2013-08-16 14:07:11 -0700579 Log.w(TAG, "Caught a RuntimeException from the binder stub implementation.", e);
Makoto Onukif9b941f2016-03-10 17:19:08 -0800580 }
581 if ((flags & FLAG_ONEWAY) != 0) {
582 if (e instanceof RemoteException) {
583 Log.w(TAG, "Binder call failed.", e);
584 } else {
585 Log.w(TAG, "Caught a RuntimeException from the binder stub implementation.", e);
586 }
Dianne Hackbornce92b0d2014-09-30 11:28:18 -0700587 } else {
588 reply.setDataPosition(0);
589 reply.writeException(e);
Igor Murashkin7eb6cfe2013-08-16 14:07:11 -0700590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 res = true;
Mattias Petersson19f22742010-11-05 08:25:38 +0100592 } catch (OutOfMemoryError e) {
Igor Murashkin7eb6cfe2013-08-16 14:07:11 -0700593 // Unconditionally log this, since this is generally unrecoverable.
594 Log.e(TAG, "Caught an OutOfMemoryError from the binder stub implementation.", e);
Mattias Petersson19f22742010-11-05 08:25:38 +0100595 RuntimeException re = new RuntimeException("Out of memory", e);
Jeff Sharkey7f97e652011-12-14 18:07:54 -0800596 reply.setDataPosition(0);
Mattias Petersson19f22742010-11-05 08:25:38 +0100597 reply.writeException(re);
598 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 }
Dianne Hackbornfad079d2014-09-26 15:46:24 -0700600 checkParcel(this, code, reply, "Unreasonably large binder reply buffer");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 reply.recycle();
602 data.recycle();
Dianne Hackbornce92b0d2014-09-30 11:28:18 -0700603
604 // Just in case -- we are done with the IPC, so there should be no more strict
605 // mode violations that have gathered for this thread. Either they have been
606 // parceled and are now in transport off to the caller, or we are returning back
607 // to the main transaction loop to wait for another incoming transaction. Either
608 // way, strict mode begone!
609 StrictMode.clearGatheredViolations();
610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 return res;
612 }
613}
614
615final class BinderProxy implements IBinder {
616 public native boolean pingBinder();
617 public native boolean isBinderAlive();
Dianne Hackborn017c6a22014-09-25 17:41:34 -0700618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 public IInterface queryLocalInterface(String descriptor) {
620 return null;
621 }
Dianne Hackborn017c6a22014-09-25 17:41:34 -0700622
623 public boolean transact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
Dianne Hackbornfad079d2014-09-26 15:46:24 -0700624 Binder.checkParcel(this, code, data, "Unreasonably large binder buffer");
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400625 if (Binder.isTracingEnabled()) { Binder.getTransactionTracker().addTrace(); }
Dianne Hackborn017c6a22014-09-25 17:41:34 -0700626 return transactNative(code, data, reply, flags);
627 }
628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 public native String getInterfaceDescriptor() throws RemoteException;
Dianne Hackborn017c6a22014-09-25 17:41:34 -0700630 public native boolean transactNative(int code, Parcel data, Parcel reply,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 int flags) throws RemoteException;
632 public native void linkToDeath(DeathRecipient recipient, int flags)
633 throws RemoteException;
634 public native boolean unlinkToDeath(DeathRecipient recipient, int flags);
635
636 public void dump(FileDescriptor fd, String[] args) throws RemoteException {
637 Parcel data = Parcel.obtain();
Brad Fitzpatrickeb75888e2010-07-26 17:47:45 -0700638 Parcel reply = Parcel.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 data.writeFileDescriptor(fd);
640 data.writeStringArray(args);
641 try {
Brad Fitzpatrickeb75888e2010-07-26 17:47:45 -0700642 transact(DUMP_TRANSACTION, data, reply, 0);
643 reply.readException();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 } finally {
645 data.recycle();
Brad Fitzpatrickeb75888e2010-07-26 17:47:45 -0700646 reply.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 }
648 }
649
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700650 public void dumpAsync(FileDescriptor fd, String[] args) throws RemoteException {
651 Parcel data = Parcel.obtain();
652 Parcel reply = Parcel.obtain();
653 data.writeFileDescriptor(fd);
654 data.writeStringArray(args);
655 try {
656 transact(DUMP_TRANSACTION, data, reply, FLAG_ONEWAY);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700657 } finally {
658 data.recycle();
659 reply.recycle();
660 }
661 }
662
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700663 public void shellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
664 String[] args, ResultReceiver resultReceiver) throws RemoteException {
665 Parcel data = Parcel.obtain();
666 Parcel reply = Parcel.obtain();
667 data.writeFileDescriptor(in);
668 data.writeFileDescriptor(out);
669 data.writeFileDescriptor(err);
670 data.writeStringArray(args);
671 resultReceiver.writeToParcel(data, 0);
672 try {
673 transact(SHELL_COMMAND_TRANSACTION, data, reply, 0);
674 reply.readException();
675 } finally {
676 data.recycle();
677 reply.recycle();
678 }
679 }
680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 BinderProxy() {
682 mSelf = new WeakReference(this);
683 }
684
685 @Override
686 protected void finalize() throws Throwable {
687 try {
688 destroy();
689 } finally {
690 super.finalize();
691 }
692 }
693
694 private native final void destroy();
695
696 private static final void sendDeathNotice(DeathRecipient recipient) {
Joe Onorato43a17652011-04-06 19:22:23 -0700697 if (false) Log.v("JavaBinder", "sendDeathNotice to " + recipient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 try {
699 recipient.binderDied();
700 }
701 catch (RuntimeException exc) {
702 Log.w("BinderNative", "Uncaught exception from death notification",
703 exc);
704 }
705 }
706
707 final private WeakReference mSelf;
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000708 private long mObject;
709 private long mOrgue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710}