Merge "Revert "logd: yield in FlushTo() if a write is pending""
diff --git a/TEST_MAPPING b/TEST_MAPPING
new file mode 100644
index 0000000..5e72035
--- /dev/null
+++ b/TEST_MAPPING
@@ -0,0 +1,18 @@
+{
+  "presubmit": [
+    {
+      "name": "CtsLiblogTestCases"
+    },
+    {
+      "name": "CtsLogdTestCases"
+    },
+    {
+      "name": "liblog-host-test",
+      "host": true
+    },
+    {
+      "name": "logd-unit-tests",
+      "host": true
+    }
+  ]
+}
diff --git a/liblog/Android.bp b/liblog/Android.bp
index db06d71..81d4f3c 100644
--- a/liblog/Android.bp
+++ b/liblog/Android.bp
@@ -37,6 +37,7 @@
     host_supported: true,
     vendor_available: true,
     ramdisk_available: true,
+    vendor_ramdisk_available: true,
     recovery_available: true,
     apex_available: [
         "//apex_available:platform",
@@ -67,6 +68,7 @@
     name: "liblog",
     host_supported: true,
     ramdisk_available: true,
+    vendor_ramdisk_available: true,
     recovery_available: true,
     native_bridge_supported: true,
     llndk_stubs: "liblog.llndk",
diff --git a/liblog/logd_writer.cpp b/liblog/logd_writer.cpp
index f5d19ca..a230749 100644
--- a/liblog/logd_writer.cpp
+++ b/liblog/logd_writer.cpp
@@ -59,7 +59,8 @@
     return;
   }
 
-  int new_socket = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0));
+  int new_socket =
+      TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
   if (new_socket <= 0) {
     return;
   }
@@ -90,6 +91,8 @@
   struct iovec newVec[nr + headerLength];
   android_log_header_t header;
   size_t i, payloadSize;
+  static atomic_int dropped;
+  static atomic_int droppedSecurity;
 
   GetSocket();
 
@@ -107,7 +110,6 @@
     return 0;
   }
 
-  header.id = logId;
   header.tid = gettid();
   header.realtime.tv_sec = ts->tv_sec;
   header.realtime.tv_nsec = ts->tv_nsec;
@@ -115,6 +117,44 @@
   newVec[0].iov_base = (unsigned char*)&header;
   newVec[0].iov_len = sizeof(header);
 
+  int32_t snapshot = atomic_exchange_explicit(&droppedSecurity, 0, memory_order_relaxed);
+  if (snapshot) {
+    android_log_event_int_t buffer;
+
+    header.id = LOG_ID_SECURITY;
+    buffer.header.tag = LIBLOG_LOG_TAG;
+    buffer.payload.type = EVENT_TYPE_INT;
+    buffer.payload.data = snapshot;
+
+    newVec[headerLength].iov_base = &buffer;
+    newVec[headerLength].iov_len = sizeof(buffer);
+
+    ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, 2));
+    if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
+      atomic_fetch_add_explicit(&droppedSecurity, snapshot, memory_order_relaxed);
+    }
+  }
+  snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
+  if (snapshot && __android_log_is_loggable_len(ANDROID_LOG_INFO, "liblog", strlen("liblog"),
+                                                ANDROID_LOG_VERBOSE)) {
+    android_log_event_int_t buffer;
+
+    header.id = LOG_ID_EVENTS;
+    buffer.header.tag = LIBLOG_LOG_TAG;
+    buffer.payload.type = EVENT_TYPE_INT;
+    buffer.payload.data = snapshot;
+
+    newVec[headerLength].iov_base = &buffer;
+    newVec[headerLength].iov_len = sizeof(buffer);
+
+    ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, 2));
+    if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
+      atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
+    }
+  }
+
+  header.id = logId;
+
   for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
     newVec[i].iov_base = vec[i - headerLength].iov_base;
     payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
@@ -128,8 +168,11 @@
     }
   }
 
+  // The write below could be lost, but will never block.
+  // EAGAIN occurs if logd is overloaded, other errors indicate that something went wrong with
+  // the connection, so we reset it and try again.
   ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, i));
-  if (ret < 0) {
+  if (ret < 0 && errno != EAGAIN) {
     LogdConnect();
 
     ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, i));
@@ -139,5 +182,14 @@
     ret = -errno;
   }
 
+  if (ret > (ssize_t)sizeof(header)) {
+    ret -= sizeof(header);
+  } else if (ret < 0) {
+    atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
+    if (logId == LOG_ID_SECURITY) {
+      atomic_fetch_add_explicit(&droppedSecurity, 1, memory_order_relaxed);
+    }
+  }
+
   return ret;
 }
diff --git a/liblog/tests/Android.bp b/liblog/tests/Android.bp
index 171aafd..7524750 100644
--- a/liblog/tests/Android.bp
+++ b/liblog/tests/Android.bp
@@ -99,6 +99,7 @@
         "cts",
         "device-tests",
     ],
+    test_config: "device_test_config.xml",
 }
 
 cc_test_host {
@@ -111,4 +112,7 @@
         "liblog_global_state.cpp",
     ],
     isolated: true,
+    test_suites: [
+        "general-tests",
+    ],
 }
diff --git a/liblog/tests/AndroidTest.xml b/liblog/tests/device_test_config.xml
similarity index 100%
rename from liblog/tests/AndroidTest.xml
rename to liblog/tests/device_test_config.xml
diff --git a/logd/Android.bp b/logd/Android.bp
index 00fbb52..63bd505 100644
--- a/logd/Android.bp
+++ b/logd/Android.bp
@@ -173,6 +173,9 @@
     name: "logd-unit-tests",
     host_supported: true,
     defaults: ["logd-unit-test-defaults"],
+    test_suites: [
+        "general-tests",
+    ],
 }
 
 cc_test {
@@ -190,6 +193,7 @@
         "cts",
         "device-tests",
     ],
+    test_config: "device_test_config.xml",
 }
 
 cc_binary {
diff --git a/logd/AndroidTest.xml b/logd/device_test_config.xml
similarity index 100%
rename from logd/AndroidTest.xml
rename to logd/device_test_config.xml
diff --git a/logd/logd_test.cpp b/logd/logd_test.cpp
index 828f580..706c1b2 100644
--- a/logd/logd_test.cpp
+++ b/logd/logd_test.cpp
@@ -843,9 +843,9 @@
 TEST(logd, no_epipe) {
 #ifdef __ANDROID__
     // Actually generating SIGPIPE in logd is racy, since we need to close the socket quicker than
-    // logd finishes writing the data to it, so we try 10 times, which should be enough to trigger
+    // logd finishes writing the data to it, so we try 5 times, which should be enough to trigger
     // SIGPIPE if logd isn't ignoring SIGPIPE
-    for (int i = 0; i < 10; ++i) {
+    for (int i = 0; i < 5; ++i) {
         unique_fd sock1(
                 socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM));
         ASSERT_GT(sock1, 0);
@@ -861,7 +861,7 @@
 
         struct pollfd p = {.fd = sock2, .events = POLLIN, .revents = 0};
 
-        int ret = poll(&p, 1, 20);
+        int ret = poll(&p, 1, 1000);
         EXPECT_EQ(ret, 1);
         EXPECT_TRUE(p.revents & POLLIN);
         EXPECT_FALSE(p.revents & POLL_ERR);