Add posix_strerror_r and ErrnoNumberAsString to libbase

Calling strerror_r is tricky, as glibc provides the GNU version
if _GNU_SOURCE is set, and musl libc only provides the posix version.
Handle the complexity once in libbase to export a posix_strerror_r
function, and move update_engine's ErrnoNumberAsString into libbase.

Bug: 190084016
Test: m USE_HOST_MUSL=true host-native
Test: atest --host libbase_test
Change-Id: Ied24f5bb8cf34fc7948d129c368ee17adb1460e1
diff --git a/strings.cpp b/strings.cpp
index 8f3c7d9..deb6e28 100644
--- a/strings.cpp
+++ b/strings.cpp
@@ -16,12 +16,18 @@
 
 #include "android-base/strings.h"
 
+#include "android-base/stringprintf.h"
+
 #include <stdlib.h>
 #include <string.h>
 
 #include <string>
 #include <vector>
 
+// Wraps the posix version of strerror_r to make it available in translation units
+// that define _GNU_SOURCE.
+extern "C" int posix_strerror_r(int errnum, char* buf, size_t buflen);
+
 namespace android {
 namespace base {
 
@@ -152,5 +158,15 @@
   return result;
 }
 
+std::string ErrnoNumberAsString(int errnum) {
+  char buf[100];
+  buf[0] = '\0';
+  int strerror_err = posix_strerror_r(errnum, buf, sizeof(buf));
+  if (strerror_err < 0) {
+    return StringPrintf("Failed to convert errno %d to string: %d", errnum, strerror_err);
+  }
+  return buf;
+}
+
 }  // namespace base
 }  // namespace android