From e6a975ac7c9b30e01dc93bc617a07241acce79f8 Mon Sep 17 00:00:00 2001 From: Richard Kojedzinszky Date: Sat, 4 Jan 2025 19:33:27 +0100 Subject: [PATCH] fix: make zfs_strerror really thread-safe and portable turned out that strerror_l() usage was wrong, and also, some libc implementations dont have strerror_l(). zfs_strerror() now simply calls original strerror() and copies the result to a thread-local buffer, then returns that. Reviewed-by: Brian Behlendorf Reviewed-by: Alexander Motin Signed-off-by: Richard Kojedzinszky Closes #15793 Closes #16640 Closes #16923 Upstream commit dc0324bfa90011a5edf65bc0947aaf51ac9a8b61 --- config/user.m4 | 2 +- include/libzutil.h | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/config/user.m4 b/config/user.m4 index 4e31745a2abc..badd920d2b8a 100644 --- a/config/user.m4 +++ b/config/user.m4 @@ -33,7 +33,7 @@ AC_DEFUN([ZFS_AC_CONFIG_USER], [ ZFS_AC_CONFIG_USER_MAKEDEV_IN_MKDEV ZFS_AC_CONFIG_USER_ZFSEXEC - AC_CHECK_FUNCS([execvpe issetugid mlockall strerror_l strlcat strlcpy gettid]) + AC_CHECK_FUNCS([execvpe issetugid mlockall strlcat strlcpy gettid]) AC_SUBST(RM) ]) diff --git a/include/libzutil.h b/include/libzutil.h index 3a527410fd73..bff2e13b29d9 100644 --- a/include/libzutil.h +++ b/include/libzutil.h @@ -27,7 +27,7 @@ #define _LIBZUTIL_H extern __attribute__((visibility("default"))) #include -#include +#include #include #include @@ -274,11 +274,14 @@ _LIBZUTIL_H void update_vdev_config_dev_sysfs_path(nvlist_t *nv, * Thread-safe strerror() for use in ZFS libraries */ static inline char *zfs_strerror(int errnum) { -#ifdef HAVE_STRERROR_L - return (strerror_l(errnum, uselocale(0))); -#else - return (strerror(errnum)); -#endif + static __thread char errbuf[512]; + static pthread_mutex_t zfs_strerror_lock = PTHREAD_MUTEX_INITIALIZER; + + (void) pthread_mutex_lock(&zfs_strerror_lock); + (void) strlcpy(errbuf, strerror(errnum), sizeof (errbuf)); + (void) pthread_mutex_unlock(&zfs_strerror_lock); + + return (errbuf); } #ifdef __cplusplus