Skip to content

Commit

Permalink
fix: make zfs_strerror really thread-safe and portable
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Alexander Motin <[email protected]>
Signed-off-by: Richard Kojedzinszky <[email protected]>
Closes openzfs#15793
Closes openzfs#16640
Closes openzfs#16923

Upstream commit dc0324b
  • Loading branch information
rkojedzinszky committed Jan 6, 2025
1 parent 28b7c7f commit e6a975a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion config/user.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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)
])
15 changes: 9 additions & 6 deletions include/libzutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#define _LIBZUTIL_H extern __attribute__((visibility("default")))

#include <string.h>
#include <locale.h>
#include <pthread.h>
#include <sys/nvpair.h>
#include <sys/fs/zfs.h>

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e6a975a

Please sign in to comment.