Merge "Make libnativewindow as LL-NDK"
diff --git a/cmds/atrace/atrace.cpp b/cmds/atrace/atrace.cpp
index 1ce88b9..4c6950f 100644
--- a/cmds/atrace/atrace.cpp
+++ b/cmds/atrace/atrace.cpp
@@ -448,8 +448,8 @@
         return false;
     }
 
-    char buf[4097];
-    ssize_t n = read(fd, buf, 4096);
+    char buf[100];
+    ssize_t n = read(fd, buf, 99);
     close(fd);
     if (n == -1) {
         fprintf(stderr, "error reading %s: %s (%d)\n", k_traceClockPath,
@@ -473,13 +473,38 @@
     return strcmp(mode, start) == 0;
 }
 
-// Enable or disable the kernel's use of the global clock.  Disabling the global
-// clock will result in the kernel using a per-CPU local clock.
+// Read the trace_clock sysfs file and return true if it contains the requested
+// value.  The trace_clock file format is:
+// local [global] counter uptime perf
+static bool traceClockContains(const char *mode)
+{
+    int fd = open((g_traceFolder + k_traceClockPath).c_str(), O_RDONLY);
+    if (fd == -1) {
+        fprintf(stderr, "error opening %s: %s (%d)\n", k_traceClockPath,
+            strerror(errno), errno);
+        return false;
+    }
+
+    char buf[100];
+    ssize_t n = read(fd, buf, 99);
+    close(fd);
+    if (n == -1) {
+        fprintf(stderr, "error reading %s: %s (%d)\n", k_traceClockPath,
+            strerror(errno), errno);
+        return false;
+    }
+    buf[n] = '\0';
+
+    return strstr(buf, mode) != NULL;
+}
+
+// Set the clock to the best available option while tracing. Use 'boot' if it's
+// available; otherwise, use 'mono'.
 // Any write to the trace_clock sysfs file will reset the buffer, so only
 // update it if the requested value is not the current value.
-static bool setGlobalClockEnable(bool enable)
+static bool setClock()
 {
-    const char *clock = enable ? "global" : "local";
+    const char* clock = traceClockContains("boot") ? "boot" : "mono";
 
     if (isTraceClock(clock)) {
         return true;
@@ -778,7 +803,7 @@
     ok &= setCategoriesEnableFromFile(g_categoriesFile);
     ok &= setTraceOverwriteEnable(g_traceOverwrite);
     ok &= setTraceBufferSizeKB(g_traceBufferSizeKB);
-    ok &= setGlobalClockEnable(true);
+    ok &= setClock();
     ok &= setPrintTgidEnableIfPresent(true);
     ok &= setKernelTraceFuncs(g_kernelTraceFuncs);
 
@@ -852,7 +877,6 @@
     // Set the options back to their defaults.
     setTraceOverwriteEnable(true);
     setTraceBufferSizeKB(1);
-    setGlobalClockEnable(false);
     setPrintTgidEnableIfPresent(false);
     setKernelTraceFuncs(NULL);
 }
@@ -1208,7 +1232,7 @@
 
     if (ok && traceStart) {
         if (!traceStream) {
-            printf("capturing trace...");
+            printf("capturing trace...\n");
             fflush(stdout);
         }
 
diff --git a/cmds/atrace/atrace.rc b/cmds/atrace/atrace.rc
index cef41be..cfd2659 100644
--- a/cmds/atrace/atrace.rc
+++ b/cmds/atrace/atrace.rc
@@ -124,6 +124,10 @@
     write /sys/kernel/debug/tracing/tracing_on 0
     write /sys/kernel/tracing/tracing_on 0
 
+    # Set the trace clock to boot if it exists, falling back to mono if not.
+    write /d/tracing/trace_clock mono
+    write /d/tracing/trace_clock boot
+
 # Allow only the shell group to read and truncate the kernel trace.
     chown root shell /sys/kernel/debug/tracing/trace
     chown root shell /sys/kernel/tracing/trace
diff --git a/services/vr/hardware_composer/vr_composer.cpp b/services/vr/hardware_composer/vr_composer.cpp
index c15f8fd..c45fbf4 100644
--- a/services/vr/hardware_composer/vr_composer.cpp
+++ b/services/vr/hardware_composer/vr_composer.cpp
@@ -1,7 +1,25 @@
 #include "vr_composer.h"
 
+#include <binder/IPCThreadState.h>
+#include <binder/PermissionCache.h>
+
 namespace android {
 namespace dvr {
+namespace {
+
+bool CheckPermission() {
+  const android::IPCThreadState* ipc = android::IPCThreadState::self();
+  const pid_t pid = ipc->getCallingPid();
+  const uid_t uid = ipc->getCallingUid();
+  const bool permission = PermissionCache::checkPermission(
+      String16("android.permission.RESTRICTED_VR_ACCESS"), pid, uid);
+  if (!permission)
+    ALOGE("permission denied to pid=%d uid=%u", pid, uid);
+
+  return permission;
+}
+
+}  // namespace
 
 VrComposer::VrComposer() {}
 
@@ -11,6 +29,9 @@
     const sp<IVrComposerCallback>& callback) {
   std::lock_guard<std::mutex> guard(mutex_);
 
+  if (!CheckPermission())
+    return binder::Status::fromStatusT(PERMISSION_DENIED);
+
   if (callback_.get()) {
     ALOGE("Failed to register callback, already registered");
     return binder::Status::fromStatusT(ALREADY_EXISTS);
diff --git a/services/vr/virtual_touchpad/Android.bp b/services/vr/virtual_touchpad/Android.bp
index c8bc884..3d5dfb2 100644
--- a/services/vr/virtual_touchpad/Android.bp
+++ b/services/vr/virtual_touchpad/Android.bp
@@ -80,7 +80,6 @@
     cppflags: ["-std=c++11"],
     cflags: [
         "-DLOG_TAG=\"VrVirtualTouchpad\"",
-        "-DSELINUX_ACCESS_CONTROL",
     ],
     host_ldlibs: ["-llog"],
     name: "virtual_touchpad",
diff --git a/services/vr/virtual_touchpad/VirtualTouchpadService.cpp b/services/vr/virtual_touchpad/VirtualTouchpadService.cpp
index 191bcfb..81edd32 100644
--- a/services/vr/virtual_touchpad/VirtualTouchpadService.cpp
+++ b/services/vr/virtual_touchpad/VirtualTouchpadService.cpp
@@ -122,9 +122,6 @@
 bool VirtualTouchpadService::CheckTouchPermission(pid_t* out_pid) {
   const android::IPCThreadState* ipc = android::IPCThreadState::self();
   *out_pid = ipc->getCallingPid();
-#ifdef SELINUX_ACCESS_CONTROL
-  return true;
-#else
   const uid_t uid = ipc->getCallingUid();
   const bool permission = PermissionCache::checkPermission(kTouchPermission, *out_pid, uid);
   if (!permission) {
@@ -132,7 +129,6 @@
           static_cast<long>(uid));
   }
   return permission;
-#endif
 }
 
 }  // namespace dvr