Make strerror(3) and strsignal(3) thread-safe, and add psignal(3) and psiginfo(3).

Change-Id: I426109db25e907980d6cb3a7a695796e45783b78
diff --git a/libc/bionic/ThreadLocalBuffer.h b/libc/bionic/ThreadLocalBuffer.h
new file mode 100644
index 0000000..99acdba
--- /dev/null
+++ b/libc/bionic/ThreadLocalBuffer.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _BIONIC_THREAD_LOCAL_BUFFER_H_included
+#define _BIONIC_THREAD_LOCAL_BUFFER_H_included
+
+#include <malloc.h>
+#include <pthread.h>
+
+// libstdc++ currently contains __cxa_guard_acquire and __cxa_guard_release,
+// so we make do with macros instead of a C++ class.
+// TODO: move __cxa_guard_acquire and __cxa_guard_release into libc.
+
+#define GLOBAL_INIT_THREAD_LOCAL_BUFFER(name) \
+  static pthread_once_t name ## _once; \
+  static pthread_key_t name ## _key; \
+  static void name ## _key_destroy(void* buffer) { \
+    free(buffer); \
+  } \
+  static void name ## _key_init() { \
+    pthread_key_create(&name ## _key, name ## _key_destroy); \
+  }
+
+// Leaves "name_buffer" and "name_byte_count" defined and initialized.
+#define LOCAL_INIT_THREAD_LOCAL_BUFFER(type, name, byte_count) \
+  pthread_once(&name ## _once, name ## _key_init); \
+  type name ## _buffer = reinterpret_cast<type>(pthread_getspecific(name ## _key)); \
+  if (name ## _buffer == NULL) { \
+    name ## _buffer = reinterpret_cast<type>(malloc(byte_count)); \
+    pthread_setspecific(name ## _key, name ## _buffer); \
+  } \
+  const size_t name ## _buffer_size = byte_count
+
+#endif // _BIONIC_THREAD_LOCAL_BUFFER_H_included
diff --git a/libc/bionic/strerror.cpp b/libc/bionic/strerror.cpp
new file mode 100644
index 0000000..036ec8d
--- /dev/null
+++ b/libc/bionic/strerror.cpp
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <string.h>
+#include "ThreadLocalBuffer.h"
+
+GLOBAL_INIT_THREAD_LOCAL_BUFFER(strerror);
+
+char* strerror(int error_number) {
+  LOCAL_INIT_THREAD_LOCAL_BUFFER(char*, strerror, NL_TEXTMAX);
+  strerror_r(error_number, strerror_buffer, strerror_buffer_size);
+  return strerror_buffer;
+}
diff --git a/libc/bionic/strerror_r.cpp b/libc/bionic/strerror_r.cpp
new file mode 100644
index 0000000..92235a5
--- /dev/null
+++ b/libc/bionic/strerror_r.cpp
@@ -0,0 +1,71 @@
+/* $OpenBSD: strerror_r.c,v 1.6 2005/08/08 08:05:37 espie Exp $ */
+/* Public Domain <marc@snafu.org> */
+
+#include <errno.h>
+#include <limits.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+
+struct Pair {
+  int code;
+  const char* msg;
+};
+
+static const char* __code_string_lookup(const Pair* strings, int code) {
+  for (size_t i = 0; strings[i].msg != NULL; ++i) {
+    if (strings[i].code == code) {
+      return strings[i].msg;
+    }
+  }
+  return NULL;
+}
+
+static const Pair _sys_error_strings[] = {
+#define  __BIONIC_ERRDEF(x,y,z)  { x, z },
+#include <sys/_errdefs.h>
+  { 0, NULL }
+};
+
+int strerror_r(int error_number, char* buf, size_t buf_len) {
+  int saved_errno = errno;
+  size_t length;
+
+  const char* error_name = __code_string_lookup(_sys_error_strings, error_number);
+  if (error_name != NULL) {
+    length = snprintf(buf, buf_len, "%s", error_name);
+  } else {
+    length = snprintf(buf, buf_len, "Unknown error %u", error_number);
+  }
+  if (length >= buf_len) {
+    errno = ERANGE;
+    return -1;
+  }
+
+  errno = saved_errno;
+  return 0;
+}
+
+static const Pair _sys_signal_strings[] = {
+#define  __BIONIC_SIGDEF(x,y,z)  { y, z },
+#include <sys/_sigdefs.h>
+  { 0, NULL }
+};
+
+extern "C" const char* __strsignal(int signal_number, char* buf, size_t buf_len) {
+  const char* signal_name = __code_string_lookup(_sys_signal_strings, signal_number);
+  if (signal_name != NULL) {
+    return signal_name;
+  }
+
+  const char* prefix = "Unknown";
+  if (signal_number >= SIGRTMIN && signal_number <= SIGRTMAX) {
+    prefix = "Real-time";
+    signal_number -= SIGRTMIN;
+  }
+  size_t length = snprintf(buf, buf_len, "%s signal %d", prefix, signal_number);
+  if (length >= buf_len) {
+    return NULL;
+  }
+  return buf;
+}
diff --git a/libc/bionic/strsignal.cpp b/libc/bionic/strsignal.cpp
new file mode 100644
index 0000000..1cbec9b
--- /dev/null
+++ b/libc/bionic/strsignal.cpp
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <string.h>
+#include "ThreadLocalBuffer.h"
+
+extern "C" const char* __strsignal(int, char*, size_t);
+
+GLOBAL_INIT_THREAD_LOCAL_BUFFER(strsignal);
+
+char* strsignal(int signal_number) {
+  LOCAL_INIT_THREAD_LOCAL_BUFFER(char*, strsignal, NL_TEXTMAX);
+  return const_cast<char*>(__strsignal(signal_number, strsignal_buffer, strsignal_buffer_size));
+}