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;
 }
diff --git a/libc/include/pthread.h b/libc/include/pthread.h
index 50f2024..c67f239 100644
--- a/libc/include/pthread.h
+++ b/libc/include/pthread.h
@@ -245,7 +245,10 @@
 
 pthread_t pthread_self(void) __pure2;
 
+#if defined(__USE_GNU)
+int pthread_getname_np(pthread_t, char*, size_t) __nonnull((2));
 int pthread_setname_np(pthread_t, const char*) __nonnull((2));
+#endif
 
 int pthread_setschedparam(pthread_t, int, const struct sched_param*) __nonnull((3));
 
diff --git a/libc/libc.arm.brillo.map b/libc/libc.arm.brillo.map
index 2c0ab15..aca1e50 100644
--- a/libc/libc.arm.brillo.map
+++ b/libc/libc.arm.brillo.map
@@ -1271,6 +1271,11 @@
     tmpfile64;
 } LIBC;
 
+LIBC_O {
+  global:
+    pthread_getname_np;
+} LIBC_N;
+
 LIBC_PRIVATE {
   global:
     ___Unwind_Backtrace; # arm
@@ -1448,4 +1453,4 @@
     SHA1Init; # arm x86 mips
     SHA1Transform; # arm x86 mips
     SHA1Update; # arm x86 mips
-} LIBC_N;
+} LIBC_O;
diff --git a/libc/libc.arm.map b/libc/libc.arm.map
index c22535d..6ccf37e 100644
--- a/libc/libc.arm.map
+++ b/libc/libc.arm.map
@@ -1271,6 +1271,11 @@
     tmpfile64;
 } LIBC;
 
+LIBC_O {
+  global:
+    pthread_getname_np;
+} LIBC_N;
+
 LIBC_PRIVATE {
   global:
     ___Unwind_Backtrace; # arm
@@ -1487,4 +1492,4 @@
     vfdprintf; # arm x86 mips nobrillo
     wait3; # arm x86 mips nobrillo
     wcswcs; # arm x86 mips nobrillo
-} LIBC_N;
+} LIBC_O;
diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map
index 63c82e5..4622c7f 100644
--- a/libc/libc.arm64.map
+++ b/libc/libc.arm64.map
@@ -1193,6 +1193,11 @@
     tmpfile64;
 } LIBC;
 
+LIBC_O {
+  global:
+    pthread_getname_np;
+} LIBC_N;
+
 LIBC_PRIVATE {
   global:
     android_getaddrinfofornet;
@@ -1202,4 +1207,4 @@
     free_malloc_leak_info;
     get_malloc_leak_info;
     gMallocLeakZygoteChild;
-} LIBC_N;
+} LIBC_O;
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index c000fb1..1f523d7 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1297,6 +1297,11 @@
     tmpfile64;
 } LIBC;
 
+LIBC_O {
+  global:
+    pthread_getname_np;
+} LIBC_N;
+
 LIBC_PRIVATE {
   global:
     ___Unwind_Backtrace; # arm
@@ -1514,4 +1519,4 @@
     vfdprintf; # arm x86 mips nobrillo
     wait3; # arm x86 mips nobrillo
     wcswcs; # arm x86 mips nobrillo
-} LIBC_N;
+} LIBC_O;
diff --git a/libc/libc.mips.brillo.map b/libc/libc.mips.brillo.map
index 8e5fdf4..3d11408 100644
--- a/libc/libc.mips.brillo.map
+++ b/libc/libc.mips.brillo.map
@@ -1255,6 +1255,11 @@
     tmpfile64;
 } LIBC;
 
+LIBC_O {
+  global:
+    pthread_getname_np;
+} LIBC_N;
+
 LIBC_PRIVATE {
   global:
     __accept4; # arm x86 mips
@@ -1289,4 +1294,4 @@
     SHA1Init; # arm x86 mips
     SHA1Transform; # arm x86 mips
     SHA1Update; # arm x86 mips
-} LIBC_N;
+} LIBC_O;
diff --git a/libc/libc.mips.map b/libc/libc.mips.map
index d30ead8..69d8178 100644
--- a/libc/libc.mips.map
+++ b/libc/libc.mips.map
@@ -1255,6 +1255,11 @@
     tmpfile64;
 } LIBC;
 
+LIBC_O {
+  global:
+    pthread_getname_np;
+} LIBC_N;
+
 LIBC_PRIVATE {
   global:
     __accept4; # arm x86 mips
@@ -1328,4 +1333,4 @@
     vfdprintf; # arm x86 mips nobrillo
     wait3; # arm x86 mips nobrillo
     wcswcs; # arm x86 mips nobrillo
-} LIBC_N;
+} LIBC_O;
diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map
index 63c82e5..4622c7f 100644
--- a/libc/libc.mips64.map
+++ b/libc/libc.mips64.map
@@ -1193,6 +1193,11 @@
     tmpfile64;
 } LIBC;
 
+LIBC_O {
+  global:
+    pthread_getname_np;
+} LIBC_N;
+
 LIBC_PRIVATE {
   global:
     android_getaddrinfofornet;
@@ -1202,4 +1207,4 @@
     free_malloc_leak_info;
     get_malloc_leak_info;
     gMallocLeakZygoteChild;
-} LIBC_N;
+} LIBC_O;
diff --git a/libc/libc.x86.brillo.map b/libc/libc.x86.brillo.map
index 7ac0976..b717ebe 100644
--- a/libc/libc.x86.brillo.map
+++ b/libc/libc.x86.brillo.map
@@ -1254,6 +1254,11 @@
     tmpfile64;
 } LIBC;
 
+LIBC_O {
+  global:
+    pthread_getname_np;
+} LIBC_N;
+
 LIBC_PRIVATE {
   global:
     __accept4; # arm x86 mips
@@ -1288,4 +1293,4 @@
     SHA1Init; # arm x86 mips
     SHA1Transform; # arm x86 mips
     SHA1Update; # arm x86 mips
-} LIBC_N;
+} LIBC_O;
diff --git a/libc/libc.x86.map b/libc/libc.x86.map
index 11fb710..df28d9a 100644
--- a/libc/libc.x86.map
+++ b/libc/libc.x86.map
@@ -1254,6 +1254,11 @@
     tmpfile64;
 } LIBC;
 
+LIBC_O {
+  global:
+    pthread_getname_np;
+} LIBC_N;
+
 LIBC_PRIVATE {
   global:
     __accept4; # arm x86 mips
@@ -1327,4 +1332,4 @@
     vfdprintf; # arm x86 mips nobrillo
     wait3; # arm x86 mips nobrillo
     wcswcs; # arm x86 mips nobrillo
-} LIBC_N;
+} LIBC_O;
diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map
index 63c82e5..4622c7f 100644
--- a/libc/libc.x86_64.map
+++ b/libc/libc.x86_64.map
@@ -1193,6 +1193,11 @@
     tmpfile64;
 } LIBC;
 
+LIBC_O {
+  global:
+    pthread_getname_np;
+} LIBC_N;
+
 LIBC_PRIVATE {
   global:
     android_getaddrinfofornet;
@@ -1202,4 +1207,4 @@
     free_malloc_leak_info;
     get_malloc_leak_info;
     gMallocLeakZygoteChild;
-} LIBC_N;
+} LIBC_O;