Add pthread_getname_np.

Also guard both these GNU extensions with _GNU_SOURCE.

Also improve the tests to test each case on both the current thread and
another thread, since the code paths are totally different.

Bug: http://b/27810459
Change-Id: I72b05bca5c5b6ca8ba4585b8edfb716a1c252f92
diff --git a/libc/bionic/pthread_setname_np.cpp b/libc/bionic/pthread_setname_np.cpp
index bb1114e..6d2880e 100644
--- a/libc/bionic/pthread_setname_np.cpp
+++ b/libc/bionic/pthread_setname_np.cpp
@@ -41,41 +41,69 @@
 
 // This value is not exported by kernel headers.
 #define MAX_TASK_COMM_LEN 16
-#define TASK_COMM_FMT "/proc/self/task/%d/comm"
+
+static int __open_task_comm_fd(pthread_t t, int flags) {
+  pthread_internal_t* thread = __pthread_internal_find(t);
+  if (thread == nullptr) {
+    errno = ENOENT;
+    return -1;
+  }
+
+  char comm_name[64];
+  snprintf(comm_name, sizeof(comm_name), "/proc/self/task/%d/comm", thread->tid);
+  return open(comm_name, O_CLOEXEC | flags);
+}
+
+int pthread_getname_np(pthread_t t, char* buf, size_t buf_size) {
+  ErrnoRestorer errno_restorer;
+
+  if (buf_size < MAX_TASK_COMM_LEN) return ERANGE;
+
+  // Getting our own name is an easy special case.
+  if (t == pthread_self()) {
+    return prctl(PR_GET_NAME, buf) ? errno : 0;
+  }
+
+  // We have to get another thread's name.
+  int fd = __open_task_comm_fd(t, O_RDONLY);
+  if (fd == -1) return errno;
+
+  ssize_t n = TEMP_FAILURE_RETRY(read(fd, buf, buf_size));
+  close(fd);
+
+  if (n == -1) return errno;
+
+  // The kernel adds a trailing '\n' to the /proc file,
+  // so this is actually the normal case for short names.
+  if (n > 0 && buf[n - 1] == '\n') {
+    buf[n - 1] = '\0';
+    return 0;
+  }
+
+  if (n == static_cast<ssize_t>(buf_size)) return ERANGE;
+  buf[n] = '\0';
+  return 0;
+}
 
 int pthread_setname_np(pthread_t t, const char* thread_name) {
   ErrnoRestorer errno_restorer;
 
   size_t thread_name_len = strlen(thread_name);
-  if (thread_name_len >= MAX_TASK_COMM_LEN) {
-    return ERANGE;
-  }
+  if (thread_name_len >= MAX_TASK_COMM_LEN) return ERANGE;
 
-  // Changing our own name is an easy special case.
+  // Setting our own name is an easy special case.
   if (t == pthread_self()) {
     return prctl(PR_SET_NAME, thread_name) ? errno : 0;
   }
 
-  // We have to change another thread's name.
-  pthread_internal_t* thread = __pthread_internal_find(t);
-  if (thread == NULL) {
-    return ENOENT;
-  }
-  pid_t tid = thread->tid;
+  // We have to set another thread's name.
+  int fd = __open_task_comm_fd(t, O_WRONLY);
+  if (fd == -1) return errno;
 
-  char comm_name[sizeof(TASK_COMM_FMT) + 8];
-  snprintf(comm_name, sizeof(comm_name), TASK_COMM_FMT, tid);
-  int fd = open(comm_name, O_CLOEXEC | O_WRONLY);
-  if (fd == -1) {
-    return errno;
-  }
   ssize_t n = TEMP_FAILURE_RETRY(write(fd, thread_name, thread_name_len));
   close(fd);
 
-  if (n < 0) {
-    return errno;
-  } else if (n != static_cast<ssize_t>(thread_name_len)) {
-    return EIO;
-  }
+  if (n == -1) return errno;
+  if (n != static_cast<ssize_t>(thread_name_len)) return EIO;
   return 0;
 }