The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.os; |
| 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.util.Log; |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 20 | import android.util.Slog; |
Dianne Hackborn | db4e33f | 2013-04-01 17:28:16 -0700 | [diff] [blame] | 21 | import com.android.internal.util.FastPrintWriter; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 22 | import libcore.io.IoUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
| 24 | import java.io.FileDescriptor; |
| 25 | import java.io.FileOutputStream; |
| 26 | import java.io.IOException; |
| 27 | import java.io.PrintWriter; |
| 28 | import java.lang.ref.WeakReference; |
| 29 | import 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 Hackborn | ab4a81b | 2014-10-09 17:59:38 -0700 | [diff] [blame] | 35 | * standard local implementation of such an object. |
| 36 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | * <p>Most developers will not implement this class directly, instead using the |
Scott Main | 40eee61 | 2012-08-06 17:48:37 -0700 | [diff] [blame] | 38 | * <a href="{@docRoot}guide/components/aidl.html">aidl</a> tool to describe the desired |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | * 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 Hackborn | ab4a81b | 2014-10-09 17:59:38 -0700 | [diff] [blame] | 43 | * |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | * @see IBinder |
| 62 | */ |
| 63 | public 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 Hackborn | 73d6a82 | 2014-09-29 10:52:47 -0700 | [diff] [blame] | 70 | private static final boolean CHECK_PARCEL_SIZE = false; |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 71 | static final String TAG = "Binder"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | |
Makoto Onuki | f9b941f | 2016-03-10 17:19:08 -0800 | [diff] [blame] | 73 | /** @hide */ |
| 74 | public static boolean LOG_RUNTIME_EXCEPTION = false; // DO NOT SUBMIT WITH TRUE |
| 75 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Control whether dump() calls are allowed. |
| 78 | */ |
| 79 | private static String sDumpDisabled = null; |
| 80 | |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 81 | /** |
| 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 Sharkey | 1324531 | 2016-11-21 09:13:52 -0700 | [diff] [blame] | 132 | /** @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 Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 143 | /* mObject is used by native code, do not remove or rename */ |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 144 | private long mObject; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | private IInterface mOwner; |
| 146 | private String mDescriptor; |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 147 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | /** |
| 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 Hackborn | 74ee865 | 2012-09-07 18:33:18 -0700 | [diff] [blame] | 158 | * Return the Linux uid assigned to the process that sent you the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | * 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 Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 165 | |
| 166 | /** |
Dianne Hackborn | 74ee865 | 2012-09-07 18:33:18 -0700 | [diff] [blame] | 167 | * 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 Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 175 | return UserHandle.of(UserHandle.getUserId(getCallingUid())); |
Dianne Hackborn | 74ee865 | 2012-09-07 18:33:18 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
Brad Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 179 | * Reset the identity of the incoming IPC on the current thread. This can |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | * 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 Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 185 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | * @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 Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 189 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | * @see #getCallingPid() |
| 191 | * @see #getCallingUid() |
| 192 | * @see #restoreCallingIdentity(long) |
| 193 | */ |
| 194 | public static final native long clearCallingIdentity(); |
Brad Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 195 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | /** |
Brad Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 197 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | * @param token The opaque token that was previously returned by |
| 202 | * {@link #clearCallingIdentity}. |
Brad Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 203 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | * @see #clearCallingIdentity |
| 205 | */ |
| 206 | public static final native void restoreCallingIdentity(long token); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 207 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | /** |
| 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 Brown | 1951ce8 | 2013-04-04 22:45:12 -0700 | [diff] [blame] | 246 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | /** |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 256 | * Call blocks until the number of executing binder threads is less |
| 257 | * than the maximum number of binder threads allowed for this process. |
Wale Ogunwale | 8d90634 | 2015-04-15 09:10:03 -0700 | [diff] [blame] | 258 | * @hide |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 259 | */ |
| 260 | public static final native void blockUntilThreadAvailable(); |
| 261 | |
| 262 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | * 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 Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 325 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | /** |
| 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 Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 360 | IoUtils.closeQuietly(fd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | } |
| 362 | } |
Brad Fitzpatrick | eb75888e | 2010-07-26 17:47:45 -0700 | [diff] [blame] | 363 | // Write the StrictMode header. |
| 364 | if (reply != null) { |
| 365 | reply.writeNoException(); |
| 366 | } else { |
| 367 | StrictMode.clearGatheredViolations(); |
| 368 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | return true; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 370 | } 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | } |
| 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 Hackborn | db4e33f | 2013-04-01 17:28:16 -0700 | [diff] [blame] | 405 | PrintWriter pw = new FastPrintWriter(fout); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | try { |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 407 | doDump(fd, pw, args); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | } finally { |
| 409 | pw.flush(); |
| 410 | } |
| 411 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 412 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | /** |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 439 | * 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 Hackborn | db4e33f | 2013-04-01 17:28:16 -0700 | [diff] [blame] | 444 | final PrintWriter pw = new FastPrintWriter(fout); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 445 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | * 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 Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 469 | * @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 Hackborn | 2e931f5 | 2016-01-28 12:21:17 -0800 | [diff] [blame] | 485 | * <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 Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 488 | * @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | * 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 Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 505 | if (false) Log.v("Binder", "Transact: " + code + " to " + this); |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 506 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | 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 Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 537 | |
Dianne Hackborn | fad079d | 2014-09-26 15:46:24 -0700 | [diff] [blame] | 538 | static void checkParcel(IBinder obj, int code, Parcel parcel, String msg) { |
Dianne Hackborn | 73d6a82 | 2014-09-29 10:52:47 -0700 | [diff] [blame] | 539 | if (CHECK_PARCEL_SIZE && parcel.dataSize() >= 800*1024) { |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 540 | // Trying to send > 800k, this is way too much |
Dianne Hackborn | fad079d | 2014-09-26 15:46:24 -0700 | [diff] [blame] | 541 | 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 Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | private native final void init(); |
| 562 | private native final void destroy(); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 563 | |
| 564 | // Entry point from android_util_Binder.cpp's onTransact |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 565 | private boolean execTransact(int code, long dataObj, long replyObj, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | 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 Murashkin | 7eb6cfe | 2013-08-16 14:07:11 -0700 | [diff] [blame] | 573 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | try { |
| 576 | res = onTransact(code, data, reply, flags); |
Makoto Onuki | f9b941f | 2016-03-10 17:19:08 -0800 | [diff] [blame] | 577 | } catch (RemoteException|RuntimeException e) { |
| 578 | if (LOG_RUNTIME_EXCEPTION) { |
Igor Murashkin | 7eb6cfe | 2013-08-16 14:07:11 -0700 | [diff] [blame] | 579 | Log.w(TAG, "Caught a RuntimeException from the binder stub implementation.", e); |
Makoto Onuki | f9b941f | 2016-03-10 17:19:08 -0800 | [diff] [blame] | 580 | } |
| 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 Hackborn | ce92b0d | 2014-09-30 11:28:18 -0700 | [diff] [blame] | 587 | } else { |
| 588 | reply.setDataPosition(0); |
| 589 | reply.writeException(e); |
Igor Murashkin | 7eb6cfe | 2013-08-16 14:07:11 -0700 | [diff] [blame] | 590 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | res = true; |
Mattias Petersson | 19f2274 | 2010-11-05 08:25:38 +0100 | [diff] [blame] | 592 | } catch (OutOfMemoryError e) { |
Igor Murashkin | 7eb6cfe | 2013-08-16 14:07:11 -0700 | [diff] [blame] | 593 | // Unconditionally log this, since this is generally unrecoverable. |
| 594 | Log.e(TAG, "Caught an OutOfMemoryError from the binder stub implementation.", e); |
Mattias Petersson | 19f2274 | 2010-11-05 08:25:38 +0100 | [diff] [blame] | 595 | RuntimeException re = new RuntimeException("Out of memory", e); |
Jeff Sharkey | 7f97e65 | 2011-12-14 18:07:54 -0800 | [diff] [blame] | 596 | reply.setDataPosition(0); |
Mattias Petersson | 19f2274 | 2010-11-05 08:25:38 +0100 | [diff] [blame] | 597 | reply.writeException(re); |
| 598 | res = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | } |
Dianne Hackborn | fad079d | 2014-09-26 15:46:24 -0700 | [diff] [blame] | 600 | checkParcel(this, code, reply, "Unreasonably large binder reply buffer"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | reply.recycle(); |
| 602 | data.recycle(); |
Dianne Hackborn | ce92b0d | 2014-09-30 11:28:18 -0700 | [diff] [blame] | 603 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | return res; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | final class BinderProxy implements IBinder { |
| 616 | public native boolean pingBinder(); |
| 617 | public native boolean isBinderAlive(); |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 618 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | public IInterface queryLocalInterface(String descriptor) { |
| 620 | return null; |
| 621 | } |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 622 | |
| 623 | public boolean transact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { |
Dianne Hackborn | fad079d | 2014-09-26 15:46:24 -0700 | [diff] [blame] | 624 | Binder.checkParcel(this, code, data, "Unreasonably large binder buffer"); |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 625 | if (Binder.isTracingEnabled()) { Binder.getTransactionTracker().addTrace(); } |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 626 | return transactNative(code, data, reply, flags); |
| 627 | } |
| 628 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | public native String getInterfaceDescriptor() throws RemoteException; |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 630 | public native boolean transactNative(int code, Parcel data, Parcel reply, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | 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 Fitzpatrick | eb75888e | 2010-07-26 17:47:45 -0700 | [diff] [blame] | 638 | Parcel reply = Parcel.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | data.writeFileDescriptor(fd); |
| 640 | data.writeStringArray(args); |
| 641 | try { |
Brad Fitzpatrick | eb75888e | 2010-07-26 17:47:45 -0700 | [diff] [blame] | 642 | transact(DUMP_TRANSACTION, data, reply, 0); |
| 643 | reply.readException(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | } finally { |
| 645 | data.recycle(); |
Brad Fitzpatrick | eb75888e | 2010-07-26 17:47:45 -0700 | [diff] [blame] | 646 | reply.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 650 | 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 Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 657 | } finally { |
| 658 | data.recycle(); |
| 659 | reply.recycle(); |
| 660 | } |
| 661 | } |
| 662 | |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 663 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | 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 Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 697 | if (false) Log.v("JavaBinder", "sendDeathNotice to " + recipient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | 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 Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 708 | private long mObject; |
| 709 | private long mOrgue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | } |