Add wrappers for pidfd_{open,getfd,send_signal}.

Bug: http://b/172518739
Test: `/data/nativetest64/bionic-unit-tests/bionic-unit-tests --gtest_filter="*pidfd*"` on blueline
Change-Id: Ibae32bbedbcf26535a80a5cbfb55ce180906b610
diff --git a/tests/Android.bp b/tests/Android.bp
index 9b7e6f5..02a33ee 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -375,6 +375,7 @@
         "netinet_ip_icmp_test.cpp",
         "netinet_udp_test.cpp",
         "nl_types_test.cpp",
+        "pidfd_test.cpp",
         "poll_test.cpp",
         "prio_ctor_test.cpp",
         "pthread_test.cpp",
diff --git a/tests/NOTICE b/tests/NOTICE
index 305e816..a58cf46 100644
--- a/tests/NOTICE
+++ b/tests/NOTICE
@@ -338,3 +338,19 @@
 
 -------------------------------------------------------------------
 
+Copyright (C) 2021 The Android Open Source Project
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+-------------------------------------------------------------------
+
diff --git a/tests/fdtrack_test.cpp b/tests/fdtrack_test.cpp
index 95758bf..13f1b2e 100644
--- a/tests/fdtrack_test.cpp
+++ b/tests/fdtrack_test.cpp
@@ -28,6 +28,8 @@
 #include <unistd.h>
 
 #if defined(__BIONIC__)
+#include <sys/pidfd.h>
+
 #include "platform/bionic/fdtrack.h"
 #include "platform/bionic/reserved_signals.h"
 #endif
@@ -61,9 +63,7 @@
   events.clear();
 
   android_fdtrack_hook_t previous = nullptr;
-  android_fdtrack_hook_t hook = [](android_fdtrack_event* event) {
-    events.push_back(*event);
-  };
+  android_fdtrack_hook_t hook = [](android_fdtrack_event* event) { events.push_back(*event); };
 
   if (!android_fdtrack_compare_exchange_hook(&previous, hook)) {
     errx(1, "failed to exchange hook: previous hook was %p", previous);
@@ -178,7 +178,7 @@
     static std::vector<int> expected_fds;                                                        \
     auto events = FdtrackRun([]() { SetFdResult(&expected_fds, expression); });                  \
     for (auto& fd : expected_fds) {                                                              \
-      ASSERT_NE(-1, fd);                                                                         \
+      ASSERT_NE(-1, fd) << strerror(errno);                                                      \
     }                                                                                            \
     if (events.size() != expected_fds.size()) {                                                  \
       fprintf(stderr, "too many events received: expected %zu, got %zu:\n", expected_fds.size(), \
@@ -211,6 +211,37 @@
 FDTRACK_TEST(openat, openat(AT_EMPTY_PATH, "/dev/null", O_WRONLY | O_CLOEXEC));
 FDTRACK_TEST(socket, socket(AF_UNIX, SOCK_STREAM, 0));
 
+FDTRACK_TEST(pidfd_open, ({
+  int rc = pidfd_open(getpid(), 0);
+  if (rc == -1) {
+    ASSERT_EQ(ENOSYS, errno);
+    GTEST_SKIP() << "pidfd_open not available";
+  }
+  rc;
+}));
+
+FDTRACK_TEST(pidfd_getfd, ({
+  android_fdtrack_set_enabled(false);
+  int pidfd_self = pidfd_open(getpid(), 0);
+  if (pidfd_self == -1) {
+    ASSERT_EQ(ENOSYS, errno);
+    GTEST_SKIP() << "pidfd_open not available";
+  }
+  android_fdtrack_set_enabled(true);
+
+  int rc = pidfd_getfd(pidfd_self, STDIN_FILENO, 0);
+  if (rc == -1) {
+    ASSERT_EQ(ENOSYS, errno);
+    GTEST_SKIP() << "pidfd_getfd not available";
+  }
+
+  android_fdtrack_set_enabled(false);
+  close(pidfd_self);
+  android_fdtrack_set_enabled(true);
+
+  rc;
+}));
+
 FDTRACK_TEST(dup, dup(STDOUT_FILENO));
 FDTRACK_TEST(dup2, dup2(STDOUT_FILENO, STDERR_FILENO));
 FDTRACK_TEST(dup3, dup3(STDOUT_FILENO, STDERR_FILENO, 0));
diff --git a/tests/pidfd_test.cpp b/tests/pidfd_test.cpp
new file mode 100644
index 0000000..7c691ef
--- /dev/null
+++ b/tests/pidfd_test.cpp
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#if defined(__BIONIC__)
+#include <sys/pidfd.h>
+#endif
+
+#include <android-base/unique_fd.h>
+
+#include <gtest/gtest.h>
+
+#include "BionicDeathTest.h"
+
+using android::base::unique_fd;
+using namespace std::chrono_literals;
+
+using pidfd_DeathTest = BionicDeathTest;
+
+TEST(pidfd, pidfd_open) {
+#if defined(__BIONIC__)
+  pid_t child = fork();
+  ASSERT_NE(-1, child);
+  if (child == 0) {
+    _exit(42);
+  }
+
+  unique_fd pidfd(pidfd_open(child, 0));
+  if (pidfd.get() == -1) {
+    ASSERT_EQ(ENOSYS, errno);
+    GTEST_SKIP() << "pidfd_open not available";
+  }
+
+  siginfo_t siginfo;
+  int rc = waitid(P_PIDFD, pidfd.get(), &siginfo, WEXITED);
+  if (rc == -1) {
+    ASSERT_EQ(EINVAL, errno) << strerror(errno);
+    GTEST_SKIP() << "P_PIDFD not available";
+  }
+
+  ASSERT_EQ(child, siginfo.si_pid);
+#endif
+}
+
+TEST(pidfd, pidfd_getfd) {
+#if defined(__BIONIC__)
+  unique_fd r, w;
+  ASSERT_TRUE(android::base::Pipe(&r, &w));
+  unique_fd self(pidfd_open(getpid(), 0));
+  if (self.get() == -1) {
+    ASSERT_EQ(ENOSYS, errno);
+    GTEST_SKIP() << "pidfd_open not available";
+  }
+
+  unique_fd dup(pidfd_getfd(self.get(), r.get(), 0));
+  if (dup.get() == -1) {
+    ASSERT_EQ(ENOSYS, errno) << strerror(errno);
+    GTEST_SKIP() << "pidfd_getfd not available";
+  }
+
+  ASSERT_NE(r.get(), dup.get());
+  ASSERT_EQ(3, write(w.get(), "foo", 3));
+  char buf[4];
+  ASSERT_EQ(3, read(dup.get(), buf, sizeof(buf)));
+  ASSERT_EQ(0, memcmp(buf, "foo", 3));
+#endif
+}
+
+TEST_F(pidfd_DeathTest, pidfd_send_signal) {
+#if defined(__BIONIC__)
+  unique_fd self(pidfd_open(getpid(), 0));
+  if (self.get() == -1) {
+    ASSERT_EQ(ENOSYS, errno);
+    GTEST_SKIP() << "pidfd_open not available";
+  }
+
+  if (pidfd_send_signal(self.get(), 0, nullptr, 0) == -1) {
+    ASSERT_EQ(ENOSYS, errno);
+    GTEST_SKIP() << "pidfd_send_signal not available";
+  }
+
+  ASSERT_EXIT(({
+                // gtest will fork a child off for ASSERT_EXIT: `self` refers to the parent.
+                unique_fd child(pidfd_open(getpid(), 0));
+                pidfd_send_signal(child.get(), SIGINT, nullptr, 0);
+              }),
+              testing::KilledBySignal(SIGINT), "");
+
+#endif
+}