2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import android.util.Log;
20 import com.android.internal.util.FastPrintWriter;
22 import java.io.FileDescriptor;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.lang.ref.WeakReference;
27 import java.lang.reflect.Modifier;
30 * Base class for a remotable object, the core part of a lightweight
31 * remote procedure call mechanism defined by {@link IBinder}.
32 * This class is an implementation of IBinder that provides
33 * the standard support creating a local implementation of such an object.
35 * <p>Most developers will not implement this class directly, instead using the
36 * <a href="{@docRoot}guide/components/aidl.html">aidl</a> tool to describe the desired
37 * interface, having it generate the appropriate Binder subclass. You can,
38 * however, derive directly from Binder to implement your own custom RPC
39 * protocol or simply instantiate a raw Binder object directly to use as a
40 * token that can be shared across processes.
44 public class Binder implements IBinder {
46 * Set this flag to true to detect anonymous, local or member classes
47 * that extend this Binder class and that are not static. These kind
48 * of classes can potentially create leaks.
50 private static final boolean FIND_POTENTIAL_LEAKS = false;
51 private static final String TAG = "Binder";
54 * Control whether dump() calls are allowed.
56 private static String sDumpDisabled = null;
58 /* mObject is used by native code, do not remove or rename */
60 private IInterface mOwner;
61 private String mDescriptor;
64 * Return the ID of the process that sent you the current transaction
65 * that is being processed. This pid can be used with higher-level
66 * system services to determine its identity and check permissions.
67 * If the current thread is not currently executing an incoming transaction,
68 * then its own pid is returned.
70 public static final native int getCallingPid();
73 * Return the Linux uid assigned to the process that sent you the
74 * current transaction that is being processed. This uid can be used with
75 * higher-level system services to determine its identity and check
76 * permissions. If the current thread is not currently executing an
77 * incoming transaction, then its own uid is returned.
79 public static final native int getCallingUid();
82 * Return the UserHandle assigned to the process that sent you the
83 * current transaction that is being processed. This is the user
84 * of the caller. It is distinct from {@link #getCallingUid()} in that a
85 * particular user will have multiple distinct apps running under it each
86 * with their own uid. If the current thread is not currently executing an
87 * incoming transaction, then its own UserHandle is returned.
89 public static final UserHandle getCallingUserHandle() {
90 return new UserHandle(UserHandle.getUserId(getCallingUid()));
94 * Reset the identity of the incoming IPC on the current thread. This can
95 * be useful if, while handling an incoming call, you will be calling
96 * on interfaces of other objects that may be local to your process and
97 * need to do permission checks on the calls coming into them (so they
98 * will check the permission of your own local process, and not whatever
99 * process originally called you).
101 * @return Returns an opaque token that can be used to restore the
102 * original calling identity by passing it to
103 * {@link #restoreCallingIdentity(long)}.
105 * @see #getCallingPid()
106 * @see #getCallingUid()
107 * @see #restoreCallingIdentity(long)
109 public static final native long clearCallingIdentity();
112 * Restore the identity of the incoming IPC on the current thread
113 * back to a previously identity that was returned by {@link
114 * #clearCallingIdentity}.
116 * @param token The opaque token that was previously returned by
117 * {@link #clearCallingIdentity}.
119 * @see #clearCallingIdentity
121 public static final native void restoreCallingIdentity(long token);
124 * Sets the native thread-local StrictMode policy mask.
126 * <p>The StrictMode settings are kept in two places: a Java-level
127 * threadlocal for libcore/Dalvik, and a native threadlocal (set
128 * here) for propagation via Binder calls. This is a little
129 * unfortunate, but necessary to break otherwise more unfortunate
130 * dependencies either of Dalvik on Android, or Android
131 * native-only code on Dalvik.
136 public static final native void setThreadStrictModePolicy(int policyMask);
139 * Gets the current native thread-local StrictMode policy mask.
141 * @see #setThreadStrictModePolicy
144 public static final native int getThreadStrictModePolicy();
147 * Flush any Binder commands pending in the current thread to the kernel
148 * driver. This can be
149 * useful to call before performing an operation that may block for a long
150 * time, to ensure that any pending object references have been released
151 * in order to prevent the process from holding on to objects longer than
154 public static final native void flushPendingCommands();
157 * Add the calling thread to the IPC thread pool. This function does
158 * not return until the current process is exiting.
160 public static final native void joinThreadPool();
163 * Returns true if the specified interface is a proxy.
166 public static final boolean isProxy(IInterface iface) {
167 return iface.asBinder() != iface;
171 * Default constructor initializes the object.
176 if (FIND_POTENTIAL_LEAKS) {
177 final Class<? extends Binder> klass = getClass();
178 if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
179 (klass.getModifiers() & Modifier.STATIC) == 0) {
180 Log.w(TAG, "The following Binder class should be static or leaks might occur: " +
181 klass.getCanonicalName());
187 * Convenience method for associating a specific interface with the Binder.
188 * After calling, queryLocalInterface() will be implemented for you
189 * to return the given owner IInterface when the corresponding
190 * descriptor is requested.
192 public void attachInterface(IInterface owner, String descriptor) {
194 mDescriptor = descriptor;
198 * Default implementation returns an empty interface name.
200 public String getInterfaceDescriptor() {
205 * Default implementation always returns true -- if you got here,
206 * the object is alive.
208 public boolean pingBinder() {
215 * Note that if you're calling on a local binder, this always returns true
216 * because your process is alive if you're calling it.
218 public boolean isBinderAlive() {
223 * Use information supplied to attachInterface() to return the
224 * associated IInterface if it matches the requested
227 public IInterface queryLocalInterface(String descriptor) {
228 if (mDescriptor.equals(descriptor)) {
235 * Control disabling of dump calls in this process. This is used by the system
236 * process watchdog to disable incoming dump calls while it has detecting the system
237 * is hung and is reporting that back to the activity controller. This is to
238 * prevent the controller from getting hung up on bug reports at this point.
241 * @param msg The message to show instead of the dump; if null, dumps are
244 public static void setDumpDisabled(String msg) {
245 synchronized (Binder.class) {
251 * Default implementation is a stub that returns false. You will want
252 * to override this to do the appropriate unmarshalling of transactions.
254 * <p>If you want to call this, call transact().
256 protected boolean onTransact(int code, Parcel data, Parcel reply,
257 int flags) throws RemoteException {
258 if (code == INTERFACE_TRANSACTION) {
259 reply.writeString(getInterfaceDescriptor());
261 } else if (code == DUMP_TRANSACTION) {
262 ParcelFileDescriptor fd = data.readFileDescriptor();
263 String[] args = data.readStringArray();
266 dump(fd.getFileDescriptor(), args);
270 } catch (IOException e) {
271 // swallowed, not propagated back to the caller
275 // Write the StrictMode header.
277 reply.writeNoException();
279 StrictMode.clearGatheredViolations();
287 * Implemented to call the more convenient version
288 * {@link #dump(FileDescriptor, PrintWriter, String[])}.
290 public void dump(FileDescriptor fd, String[] args) {
291 FileOutputStream fout = new FileOutputStream(fd);
292 PrintWriter pw = new FastPrintWriter(fout);
294 final String disabled;
295 synchronized (Binder.class) {
296 disabled = sDumpDisabled;
298 if (disabled == null) {
301 } catch (SecurityException e) {
302 pw.println("Security exception: " + e.getMessage());
304 } catch (Throwable e) {
305 // Unlike usual calls, in this case if an exception gets thrown
306 // back to us we want to print it back in to the dump data, since
307 // that is where the caller expects all interesting information to
310 pw.println("Exception occurred while dumping:");
311 e.printStackTrace(pw);
314 pw.println(sDumpDisabled);
322 * Like {@link #dump(FileDescriptor, String[])}, but ensures the target
323 * executes asynchronously.
325 public void dumpAsync(final FileDescriptor fd, final String[] args) {
326 final FileOutputStream fout = new FileOutputStream(fd);
327 final PrintWriter pw = new FastPrintWriter(fout);
328 Thread thr = new Thread("Binder.dumpAsync") {
341 * Print the object's state into the given stream.
343 * @param fd The raw file descriptor that the dump is being sent to.
344 * @param fout The file to which you should dump your state. This will be
345 * closed for you after you return.
346 * @param args additional arguments to the dump request.
348 protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) {
352 * Default implementation rewinds the parcels and calls onTransact. On
353 * the remote side, transact calls into the binder to do the IPC.
355 public final boolean transact(int code, Parcel data, Parcel reply,
356 int flags) throws RemoteException {
357 if (false) Log.v("Binder", "Transact: " + code + " to " + this);
359 data.setDataPosition(0);
361 boolean r = onTransact(code, data, reply, flags);
363 reply.setDataPosition(0);
369 * Local implementation is a no-op.
371 public void linkToDeath(DeathRecipient recipient, int flags) {
375 * Local implementation is a no-op.
377 public boolean unlinkToDeath(DeathRecipient recipient, int flags) {
381 protected void finalize() throws Throwable {
389 private native final void init();
390 private native final void destroy();
392 // Entry point from android_util_Binder.cpp's onTransact
393 private boolean execTransact(int code, long dataObj, long replyObj,
395 Parcel data = Parcel.obtain(dataObj);
396 Parcel reply = Parcel.obtain(replyObj);
397 // theoretically, we should call transact, which will call onTransact,
398 // but all that does is rewind it, and we just got these from an IPC,
399 // so we'll just call it directly.
401 // Log any exceptions as warnings, don't silently suppress them.
402 // If the call was FLAG_ONEWAY then these exceptions disappear into the ether.
404 res = onTransact(code, data, reply, flags);
405 } catch (RemoteException e) {
406 if ((flags & FLAG_ONEWAY) != 0) {
407 Log.w(TAG, "Binder call failed.", e);
409 reply.setDataPosition(0);
410 reply.writeException(e);
412 } catch (RuntimeException e) {
413 if ((flags & FLAG_ONEWAY) != 0) {
414 Log.w(TAG, "Caught a RuntimeException from the binder stub implementation.", e);
416 reply.setDataPosition(0);
417 reply.writeException(e);
419 } catch (OutOfMemoryError e) {
420 // Unconditionally log this, since this is generally unrecoverable.
421 Log.e(TAG, "Caught an OutOfMemoryError from the binder stub implementation.", e);
422 RuntimeException re = new RuntimeException("Out of memory", e);
423 reply.setDataPosition(0);
424 reply.writeException(re);
433 final class BinderProxy implements IBinder {
434 public native boolean pingBinder();
435 public native boolean isBinderAlive();
437 public IInterface queryLocalInterface(String descriptor) {
441 public native String getInterfaceDescriptor() throws RemoteException;
442 public native boolean transact(int code, Parcel data, Parcel reply,
443 int flags) throws RemoteException;
444 public native void linkToDeath(DeathRecipient recipient, int flags)
445 throws RemoteException;
446 public native boolean unlinkToDeath(DeathRecipient recipient, int flags);
448 public void dump(FileDescriptor fd, String[] args) throws RemoteException {
449 Parcel data = Parcel.obtain();
450 Parcel reply = Parcel.obtain();
451 data.writeFileDescriptor(fd);
452 data.writeStringArray(args);
454 transact(DUMP_TRANSACTION, data, reply, 0);
455 reply.readException();
462 public void dumpAsync(FileDescriptor fd, String[] args) throws RemoteException {
463 Parcel data = Parcel.obtain();
464 Parcel reply = Parcel.obtain();
465 data.writeFileDescriptor(fd);
466 data.writeStringArray(args);
468 transact(DUMP_TRANSACTION, data, reply, FLAG_ONEWAY);
476 mSelf = new WeakReference(this);
480 protected void finalize() throws Throwable {
488 private native final void destroy();
490 private static final void sendDeathNotice(DeathRecipient recipient) {
491 if (false) Log.v("JavaBinder", "sendDeathNotice to " + recipient);
493 recipient.binderDied();
495 catch (RuntimeException exc) {
496 Log.w("BinderNative", "Uncaught exception from death notification",
501 final private WeakReference mSelf;
502 private long mObject;