From 814e597c4ed49ca59b8f24a5f16a487409a4804e Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 17 Jul 2020 12:35:59 +0100 Subject: [PATCH] lib/utils.c: Use strerrordesc_np as alternative in async-safe perror. See: https://lists.fedoraproject.org/archives/list/glibc@lists.fedoraproject.org/thread/WJHGG2OO7ABNAYICGA5WQZ2Q34Q2FEHU/ Thanks: Florian Weimer --- configure.ac | 5 +++++ lib/utils.c | 31 +++++++++++++++++++------------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/configure.ac b/configure.ac index e5c29cd..a44e3b1 100644 --- a/configure.ac +++ b/configure.ac @@ -79,6 +79,11 @@ AC_CHECK_HEADERS([\ AC_CHECK_HEADERS([linux/vm_sockets.h], [], [], [#include ]) +dnl Check for strerrordesc_np (optional, glibc only). +dnl Prefer this over sys_errlist. +dnl https://lists.fedoraproject.org/archives/list/glibc@lists.fedoraproject.org/thread/WJHGG2OO7ABNAYICGA5WQZ2Q34Q2FEHU/ +AC_CHECK_FUNCS([strerrordesc_np]) + dnl Check for sys_errlist (optional). AC_MSG_CHECKING([for sys_errlist]) AC_TRY_LINK([], [extern int sys_errlist; char *p = &sys_errlist;], [ diff --git a/lib/utils.c b/lib/utils.c index bc62f93..86e9849 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -136,6 +136,11 @@ nbd_internal_fork_safe_itoa (long v, char *buf, size_t bufsize) return &buf[i]; } +#if defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-result" +#endif + /* Fork-safe version of perror. ONLY use this after fork and before * exec, the rest of the time use set_error(). */ @@ -143,24 +148,22 @@ void nbd_internal_fork_safe_perror (const char *s) { const int err = errno; + const char *m = NULL; + char buf[32]; -#if defined(__GNUC__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-result" -#endif write (2, s, strlen (s)); write (2, ": ", 2); -#if HAVE_SYS_ERRLIST - write (2, sys_errlist[errno], strlen (sys_errlist[errno])); +#ifdef HAVE_STRERRORDESC_NP + m = strerrordesc_np (errno); #else - char buf[32]; - const char *v = nbd_internal_fork_safe_itoa ((long) errno, buf, sizeof buf); - write (2, v, strlen (v)); +#ifdef HAVE_SYS_ERRLIST + m = errno >= 0 && errno < sys_nerr ? sys_errlist[errno] : NULL; #endif +#endif + if (!m) + m = nbd_internal_fork_safe_itoa ((long) errno, buf, sizeof buf); + write (2, m, strlen (m)); write (2, "\n", 1); -#if defined(__GNUC__) -#pragma GCC diagnostic pop -#endif /* Restore original errno in case it was disturbed by the system * calls above. @@ -168,6 +171,10 @@ nbd_internal_fork_safe_perror (const char *s) errno = err; } +#if defined(__GNUC__) +#pragma GCC diagnostic pop +#endif + /* nbd_internal_printable_* functions are used by the API code to * print debug messages when we trace calls in and out of libnbd. The * calls should attempt to convert the parameter into something -- 2.27.0