[automerger skipped] getCallingSid: get calling security context am: 683b6d043d
am: d7acd8a870 -s ours
am skip reason: change_id Ia8a4c0cb4a9c86dcc0d3b7583014237f879a3074 with SHA1 f0a7d7899e is in history

Change-Id: I373c99de28c510c92b516d13a0c7d07f28ea0142
diff --git a/Android.bp b/Android.bp
index c351493..b62d197 100644
--- a/Android.bp
+++ b/Android.bp
@@ -28,6 +28,10 @@
     include_dirs: ["frameworks/native/include"],
 
     vendor_available: true,
+    vndk: {
+        enabled: true,
+        support_system_process: true,
+    },
     clang: true,
     sanitize: {
         misc_undefined: ["integer"],
diff --git a/Debug.cpp b/Debug.cpp
index ea7e9b7..fb97069 100644
--- a/Debug.cpp
+++ b/Debug.cpp
@@ -15,6 +15,7 @@
  */
 
 #include <hwbinder/Debug.h>
+#include <hwbinder/ProcessState.h>
 
 #include <utils/misc.h>
 
@@ -295,6 +296,15 @@
     }
 }
 
+ssize_t getHWBinderKernelReferences(size_t count, uintptr_t* buf) {
+    sp<ProcessState> proc = ProcessState::selfOrNull();
+    if (proc.get() == NULL) {
+        return 0;
+    }
+
+    return proc->getKernelReferences(count, buf);
+}
+
 }; // namespace hardware
 }; // namespace android
 
diff --git a/IPCThreadState.cpp b/IPCThreadState.cpp
index 6a95435..b9ca20a 100644
--- a/IPCThreadState.cpp
+++ b/IPCThreadState.cpp
@@ -444,7 +444,7 @@
         pthread_mutex_lock(&mProcess->mThreadCountLock);
         mProcess->mExecutingThreadsCount++;
         if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
-                mProcess->mStarvationStartTimeMs == 0) {
+            mProcess->mMaxThreads > 1 && mProcess->mStarvationStartTimeMs == 0) {
             mProcess->mStarvationStartTimeMs = uptimeMillis();
         }
         pthread_mutex_unlock(&mProcess->mThreadCountLock);
@@ -453,11 +453,11 @@
 
         pthread_mutex_lock(&mProcess->mThreadCountLock);
         mProcess->mExecutingThreadsCount--;
-        if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
-                mProcess->mStarvationStartTimeMs != 0) {
+        if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads && mProcess->mMaxThreads > 1 &&
+            mProcess->mStarvationStartTimeMs != 0) {
             int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
             if (starvationTimeMs > 100) {
-                ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
+                ALOGW("All binder threads in pool (%zu threads) busy for %" PRId64 " ms",
                       mProcess->mMaxThreads, starvationTimeMs);
             }
             mProcess->mStarvationStartTimeMs = 0;
diff --git a/Parcel.cpp b/Parcel.cpp
index 964b6b5..7f80c01 100644
--- a/Parcel.cpp
+++ b/Parcel.cpp
@@ -217,7 +217,7 @@
             int priority = local->getMinSchedulingPriority();
 
             obj.flags = priority & FLAT_BINDER_FLAG_PRIORITY_MASK;
-            obj.flags |= FLAT_BINDER_FLAG_ACCEPTS_FDS;
+            obj.flags |= FLAT_BINDER_FLAG_ACCEPTS_FDS | FLAT_BINDER_FLAG_INHERIT_RT;
             obj.flags |= (policy & 3) << FLAT_BINDER_FLAG_SCHEDPOLICY_SHIFT;
             if (local->isRequestingSid()) {
                 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
diff --git a/ProcessState.cpp b/ProcessState.cpp
index d3bb821..e4e1a12 100644
--- a/ProcessState.cpp
+++ b/ProcessState.cpp
@@ -18,13 +18,12 @@
 
 #include <hwbinder/ProcessState.h>
 
-#include <utils/Atomic.h>
 #include <hwbinder/BpHwBinder.h>
 #include <hwbinder/IPCThreadState.h>
 #include <hwbinder/binder_kernel.h>
+#include <utils/Atomic.h>
 #include <utils/Log.h>
 #include <utils/String8.h>
-#include <utils/String8.h>
 #include <utils/threads.h>
 
 #include <private/binder/binder_module.h>
@@ -76,6 +75,11 @@
     return gProcess;
 }
 
+sp<ProcessState> ProcessState::selfOrNull() {
+    Mutex::Autolock _l(gProcessMutex);
+    return gProcess;
+}
+
 void ProcessState::setContextObject(const sp<IBinder>& object)
 {
     setContextObject(object, String16("default"));
@@ -176,6 +180,34 @@
     return mManagesContexts;
 }
 
+// Get references to userspace objects held by the kernel binder driver
+// Writes up to count elements into buf, and returns the total number
+// of references the kernel has, which may be larger than count.
+// buf may be NULL if count is 0.  The pointers returned by this method
+// should only be used for debugging and not dereferenced, they may
+// already be invalid.
+ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf) {
+    binder_node_debug_info info = {};
+
+    uintptr_t* end = buf ? buf + buf_count : NULL;
+    size_t count = 0;
+
+    do {
+        status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info);
+        if (result < 0) {
+            return -1;
+        }
+        if (info.ptr != 0) {
+            if (buf && buf < end) *buf++ = info.ptr;
+            count++;
+            if (buf && buf < end) *buf++ = info.cookie;
+            count++;
+        }
+    } while (info.ptr != 0);
+
+    return count;
+}
+
 ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
 {
     const size_t N=mHandleToObject.size();
diff --git a/include/hwbinder/Debug.h b/include/hwbinder/Debug.h
index 2848d5d..03127cb 100644
--- a/include/hwbinder/Debug.h
+++ b/include/hwbinder/Debug.h
@@ -18,15 +18,14 @@
 #define ANDROID_HARDWARE_BINDER_DEBUG_H
 
 #include <stdint.h>
+#include <sys/cdefs.h>
 #include <sys/types.h>
 
 namespace android {
 namespace hardware {
 // ---------------------------------------------------------------------------
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+__BEGIN_DECLS
 
 const char* stringForIndent(int32_t indentLevel);
 
@@ -40,9 +39,9 @@
     size_t alignment=0, bool cArrayStyle=false,
     debugPrintFunc func = 0, void* cookie = 0);
 
-#ifdef __cplusplus
-}
-#endif
+ssize_t getHWBinderKernelReferences(size_t count, uintptr_t* buf);
+
+__END_DECLS
 
 // ---------------------------------------------------------------------------
 }; // namespace hardware
diff --git a/include/hwbinder/IBinder.h b/include/hwbinder/IBinder.h
index 89bfe88..5648101 100644
--- a/include/hwbinder/IBinder.h
+++ b/include/hwbinder/IBinder.h
@@ -22,7 +22,6 @@
 #include <utils/Errors.h>
 #include <utils/RefBase.h>
 #include <utils/String16.h>
-#include <utils/Vector.h>
 
 // ---------------------------------------------------------------------------
 namespace android {
diff --git a/include/hwbinder/ProcessState.h b/include/hwbinder/ProcessState.h
index 2ed1304..5441b75 100644
--- a/include/hwbinder/ProcessState.h
+++ b/include/hwbinder/ProcessState.h
@@ -36,6 +36,7 @@
 {
 public:
     static  sp<ProcessState>    self();
+    static  sp<ProcessState>    selfOrNull();
 
             void                setContextObject(const sp<IBinder>& object);
             sp<IBinder>         getContextObject(const sp<IBinder>& caller);
@@ -65,6 +66,8 @@
             status_t            setThreadPoolConfiguration(size_t maxThreads, bool callerJoinsPool);
             void                giveThreadPoolName();
 
+            ssize_t             getKernelReferences(size_t count, uintptr_t* buf);
+
 private:
     friend class IPCThreadState;
 
diff --git a/include/hwbinder/binder_kernel.h b/include/hwbinder/binder_kernel.h
index caba666..48da770 100644
--- a/include/hwbinder/binder_kernel.h
+++ b/include/hwbinder/binder_kernel.h
@@ -109,6 +109,7 @@
 enum {
         FLAT_BINDER_FLAG_SCHEDPOLICY_MASK = 0x600,
         FLAT_BINDER_FLAG_SCHEDPOLICY_SHIFT = 9,
+        FLAT_BINDER_FLAG_INHERIT_RT = 0x800,
         FLAT_BINDER_FLAG_TXN_SECURITY_CTX = 0x1000,
 };
 
@@ -124,5 +125,13 @@
                               struct binder_transaction_data_secctx),
 };
 
+struct binder_node_debug_info {
+	binder_uintptr_t ptr;
+	binder_uintptr_t cookie;
+	__u32 has_strong_ref;
+	__u32 has_weak_ref;
+};
+
+#define BINDER_GET_NODE_DEBUG_INFO _IOWR('b', 11, struct binder_node_debug_info)
 
 #endif // ANDROID_HARDWARE_BINDER_KERNEL_H
diff --git a/vts/OWNERS b/vts/OWNERS
new file mode 100644
index 0000000..db577ad
--- /dev/null
+++ b/vts/OWNERS
@@ -0,0 +1,3 @@
+set noparent
+yim@google.com
+zhuoyao@google.com