Skip to content

Commit 401c356

Browse files
rkojedzinszkybehlendorf
authored andcommitted
libzfs: use zfs_strerror() in place of strerror()
Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: Tino Reichardt <[email protected]> Signed-off-by: Richard Kojedzinszky <[email protected]> Closes #15793
1 parent 692f0da commit 401c356

File tree

12 files changed

+65
-47
lines changed

12 files changed

+65
-47
lines changed

include/libzutil.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#ifndef _LIBZUTIL_H
2727
#define _LIBZUTIL_H extern __attribute__((visibility("default")))
2828

29+
#include <string.h>
30+
#include <locale.h>
2931
#include <sys/nvpair.h>
3032
#include <sys/fs/zfs.h>
3133

@@ -267,6 +269,14 @@ int for_each_vdev_in_nvlist(nvlist_t *nvroot, pool_vdev_iter_f func,
267269
void update_vdevs_config_dev_sysfs_path(nvlist_t *config);
268270
_LIBZUTIL_H void update_vdev_config_dev_sysfs_path(nvlist_t *nv,
269271
const char *path, const char *key);
272+
273+
/*
274+
* Thread-safe strerror() for use in ZFS libraries
275+
*/
276+
static inline char *zfs_strerror(int errnum) {
277+
return (strerror_l(errnum, uselocale(0)));
278+
}
279+
270280
#ifdef __cplusplus
271281
}
272282
#endif

lib/libshare/nfs.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <errno.h>
3030
#include <libshare.h>
3131
#include <unistd.h>
32+
#include <libzutil.h>
3233
#include "nfs.h"
3334

3435

@@ -45,15 +46,17 @@ nfs_exports_lock(const char *name, int *nfs_lock_fd)
4546
*nfs_lock_fd = open(name, O_RDWR | O_CREAT | O_CLOEXEC, 0600);
4647
if (*nfs_lock_fd == -1) {
4748
err = errno;
48-
fprintf(stderr, "failed to lock %s: %s\n", name, strerror(err));
49+
fprintf(stderr, "failed to lock %s: %s\n", name,
50+
zfs_strerror(err));
4951
return (err);
5052
}
5153

5254
while ((err = flock(*nfs_lock_fd, LOCK_EX)) != 0 && errno == EINTR)
5355
;
5456
if (err != 0) {
5557
err = errno;
56-
fprintf(stderr, "failed to lock %s: %s\n", name, strerror(err));
58+
fprintf(stderr, "failed to lock %s: %s\n", name,
59+
zfs_strerror(err));
5760
(void) close(*nfs_lock_fd);
5861
*nfs_lock_fd = -1;
5962
return (err);
@@ -69,7 +72,7 @@ nfs_exports_unlock(const char *name, int *nfs_lock_fd)
6972

7073
if (flock(*nfs_lock_fd, LOCK_UN) != 0)
7174
fprintf(stderr, "failed to unlock %s: %s\n",
72-
name, strerror(errno));
75+
name, zfs_strerror(errno));
7376

7477
(void) close(*nfs_lock_fd);
7578
*nfs_lock_fd = -1;
@@ -92,7 +95,7 @@ nfs_init_tmpfile(const char *prefix, const char *mdir, struct tmpfile *tmpf)
9295
errno != EEXIST) {
9396
fprintf(stderr, "failed to create %s: %s\n",
9497
// cppcheck-suppress uninitvar
95-
mdir, strerror(errno));
98+
mdir, zfs_strerror(errno));
9699
return (B_FALSE);
97100
}
98101

@@ -102,14 +105,14 @@ nfs_init_tmpfile(const char *prefix, const char *mdir, struct tmpfile *tmpf)
102105
int fd = mkostemp(tmpf->name, O_CLOEXEC);
103106
if (fd == -1) {
104107
fprintf(stderr, "Unable to create temporary file: %s",
105-
strerror(errno));
108+
zfs_strerror(errno));
106109
return (B_FALSE);
107110
}
108111

109112
tmpf->fp = fdopen(fd, "w+");
110113
if (tmpf->fp == NULL) {
111114
fprintf(stderr, "Unable to reopen temporary file: %s",
112-
strerror(errno));
115+
zfs_strerror(errno));
113116
close(fd);
114117
return (B_FALSE);
115118
}
@@ -129,14 +132,14 @@ nfs_fini_tmpfile(const char *exports, struct tmpfile *tmpf)
129132
{
130133
if (fflush(tmpf->fp) != 0) {
131134
fprintf(stderr, "Failed to write to temporary file: %s\n",
132-
strerror(errno));
135+
zfs_strerror(errno));
133136
nfs_abort_tmpfile(tmpf);
134137
return (SA_SYSTEM_ERR);
135138
}
136139

137140
if (rename(tmpf->name, exports) == -1) {
138141
fprintf(stderr, "Unable to rename %s -> %s: %s\n",
139-
tmpf->name, exports, strerror(errno));
142+
tmpf->name, exports, zfs_strerror(errno));
140143
nfs_abort_tmpfile(tmpf);
141144
return (SA_SYSTEM_ERR);
142145
}
@@ -213,7 +216,7 @@ nfs_process_exports(const char *exports, const char *mountpoint,
213216

214217
if (fclose(oldfp) != 0) {
215218
fprintf(stderr, "Unable to close file %s: %s\n",
216-
exports, strerror(errno));
219+
exports, zfs_strerror(errno));
217220
error = error != SA_OK ? error : SA_SYSTEM_ERR;
218221
}
219222
}

lib/libspl/os/freebsd/getmntany.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <sys/sysmacros.h>
3737
#include <sys/stat.h>
3838
#include <unistd.h>
39+
#include <libzutil.h>
3940

4041
int
4142
getextmntent(const char *path, struct extmnttab *entry, struct stat64 *statbuf)
@@ -49,13 +50,13 @@ getextmntent(const char *path, struct extmnttab *entry, struct stat64 *statbuf)
4950

5051
if (stat64(path, statbuf) != 0) {
5152
(void) fprintf(stderr, "cannot open '%s': %s\n",
52-
path, strerror(errno));
53+
path, zfs_strerror(errno));
5354
return (-1);
5455
}
5556

5657
if (statfs(path, &sfs) != 0) {
5758
(void) fprintf(stderr, "%s: %s\n", path,
58-
strerror(errno));
59+
zfs_strerror(errno));
5960
return (-1);
6061
}
6162
statfs2mnttab(&sfs, (struct mnttab *)entry);

lib/libspl/os/linux/getmntany.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <sys/sysmacros.h>
3939
#include <sys/stat.h>
4040
#include <unistd.h>
41+
#include <libzutil.h>
4142

4243
#define BUFSIZE (MNT_LINE_MAX + 2)
4344

@@ -122,7 +123,7 @@ getextmntent(const char *path, struct extmnttab *entry, struct stat64 *statbuf)
122123
*/
123124
if (stat64(path, statbuf) != 0) {
124125
(void) fprintf(stderr, "cannot open '%s': %s\n",
125-
path, strerror(errno));
126+
path, zfs_strerror(errno));
126127
return (-1);
127128
}
128129

lib/libzfs/libzfs_crypto.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <curl/curl.h>
3838
#endif
3939
#include <libzfs.h>
40+
#include <libzutil.h>
4041
#include "libzfs_impl.h"
4142
#include "zfeature_common.h"
4243

@@ -493,7 +494,7 @@ get_key_material_file(libzfs_handle_t *hdl, const char *uri,
493494
ret = errno;
494495
errno = 0;
495496
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
496-
"Failed to open key material file: %s"), strerror(ret));
497+
"Failed to open key material file: %s"), zfs_strerror(ret));
497498
return (ret);
498499
}
499500

@@ -595,7 +596,7 @@ get_key_material_https(libzfs_handle_t *hdl, const char *uri,
595596
"%s/libzfs-XXXXXXXX.https", getenv("TMPDIR") ?: "/tmp") == -1) {
596597
ret = ENOMEM;
597598
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s"),
598-
strerror(ret));
599+
zfs_strerror(ret));
599600
goto end;
600601
}
601602

@@ -604,7 +605,7 @@ get_key_material_https(libzfs_handle_t *hdl, const char *uri,
604605
ret = errno;
605606
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
606607
"Couldn't create temporary file %s: %s"),
607-
path, strerror(ret));
608+
path, zfs_strerror(ret));
608609
free(path);
609610
goto end;
610611
}
@@ -616,7 +617,7 @@ get_key_material_https(libzfs_handle_t *hdl, const char *uri,
616617
ret = errno;
617618
(void) close(kfd);
618619
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
619-
"Couldn't reopen temporary file: %s"), strerror(ret));
620+
"Couldn't reopen temporary file: %s"), zfs_strerror(ret));
620621
goto end;
621622
}
622623

lib/libzfs/libzfs_dataset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5225,7 +5225,7 @@ zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
52255225

52265226
nvbuf = malloc(nvsz);
52275227
if (nvbuf == NULL) {
5228-
err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
5228+
err = (zfs_error(hdl, EZFS_NOMEM, zfs_strerror(errno)));
52295229
goto out;
52305230
}
52315231

lib/libzfs/libzfs_diff.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ write_inuse_diffs_one(FILE *fp, differ_info_t *di, uint64_t dobj)
283283
fobjerr = get_stats_for_obj(di, di->fromsnap, dobj, fobjname,
284284
MAXPATHLEN, &fsb);
285285
if (fobjerr && di->zerr != ENOTSUP && di->zerr != ENOENT) {
286-
zfs_error_aux(di->zhp->zfs_hdl, "%s", strerror(di->zerr));
286+
zfs_error_aux(di->zhp->zfs_hdl, "%s", zfs_strerror(di->zerr));
287287
zfs_error(di->zhp->zfs_hdl, di->zerr, di->errbuf);
288288
/*
289289
* Let's not print an error for the same object more than
@@ -298,7 +298,7 @@ write_inuse_diffs_one(FILE *fp, differ_info_t *di, uint64_t dobj)
298298
if (tobjerr && di->zerr != ENOTSUP && di->zerr != ENOENT) {
299299
if (!already_logged) {
300300
zfs_error_aux(di->zhp->zfs_hdl,
301-
"%s", strerror(di->zerr));
301+
"%s", zfs_strerror(di->zerr));
302302
zfs_error(di->zhp->zfs_hdl, di->zerr, di->errbuf);
303303
}
304304
}
@@ -445,7 +445,7 @@ differ(void *arg)
445445

446446
if ((ofp = fdopen(di->outputfd, "w")) == NULL) {
447447
di->zerr = errno;
448-
strlcpy(di->errbuf, strerror(errno), sizeof (di->errbuf));
448+
strlcpy(di->errbuf, zfs_strerror(errno), sizeof (di->errbuf));
449449
(void) close(di->datafd);
450450
return ((void *)-1);
451451
}
@@ -762,7 +762,7 @@ zfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
762762
}
763763

764764
if (pipe2(pipefd, O_CLOEXEC)) {
765-
zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
765+
zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(errno));
766766
teardown_differ_info(&di);
767767
return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, errbuf));
768768
}
@@ -776,7 +776,7 @@ zfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
776776
di.datafd = pipefd[0];
777777

778778
if (pthread_create(&tid, NULL, differ, &di)) {
779-
zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
779+
zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(errno));
780780
(void) close(pipefd[0]);
781781
(void) close(pipefd[1]);
782782
teardown_differ_info(&di);
@@ -802,14 +802,15 @@ zfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
802802
zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
803803
"\n Not an earlier snapshot from the same fs"));
804804
} else if (errno != EPIPE || di.zerr == 0) {
805-
zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
805+
zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(errno));
806806
}
807807
(void) close(pipefd[1]);
808808
(void) pthread_cancel(tid);
809809
(void) pthread_join(tid, NULL);
810810
teardown_differ_info(&di);
811811
if (di.zerr != 0 && di.zerr != EPIPE) {
812-
zfs_error_aux(zhp->zfs_hdl, "%s", strerror(di.zerr));
812+
zfs_error_aux(zhp->zfs_hdl, "%s",
813+
zfs_strerror(di.zerr));
813814
return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
814815
} else {
815816
return (zfs_error(zhp->zfs_hdl, EZFS_DIFFDATA, errbuf));
@@ -820,7 +821,7 @@ zfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
820821
(void) pthread_join(tid, NULL);
821822

822823
if (di.zerr != 0) {
823-
zfs_error_aux(zhp->zfs_hdl, "%s", strerror(di.zerr));
824+
zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(di.zerr));
824825
return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
825826
}
826827
teardown_differ_info(&di);

lib/libzfs/libzfs_mount.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include <sys/dsl_crypt.h>
7575

7676
#include <libzfs.h>
77+
#include <libzutil.h>
7778

7879
#include "libzfs_impl.h"
7980
#include <thread_pool.h>
@@ -466,7 +467,7 @@ zfs_mount_at(zfs_handle_t *zhp, const char *options, int flags,
466467
if (mkdirp(mountpoint, 0755) != 0) {
467468
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
468469
"failed to create mountpoint: %s"),
469-
strerror(errno));
470+
zfs_strerror(errno));
470471
return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
471472
dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
472473
mountpoint));
@@ -524,7 +525,7 @@ zfs_mount_at(zfs_handle_t *zhp, const char *options, int flags,
524525
(u_longlong_t)zfs_prop_get_int(zhp,
525526
ZFS_PROP_VERSION), spa_version);
526527
} else {
527-
zfs_error_aux(hdl, "%s", strerror(rc));
528+
zfs_error_aux(hdl, "%s", zfs_strerror(rc));
528529
}
529530
return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
530531
dgettext(TEXT_DOMAIN, "cannot mount '%s'"),

lib/libzfs/libzfs_sendrecv.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ zfs_send_space(zfs_handle_t *zhp, const char *snapname, const char *from,
793793
case EFAULT:
794794
case EROFS:
795795
case EINVAL:
796-
zfs_error_aux(hdl, "%s", strerror(error));
796+
zfs_error_aux(hdl, "%s", zfs_strerror(error));
797797
return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
798798

799799
default:
@@ -876,7 +876,7 @@ dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
876876
case EFAULT:
877877
case EROFS:
878878
case EINVAL:
879-
zfs_error_aux(hdl, "%s", strerror(errno));
879+
zfs_error_aux(hdl, "%s", zfs_strerror(errno));
880880
return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
881881

882882
default:
@@ -1632,7 +1632,7 @@ estimate_size(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags,
16321632
err = pthread_create(&ptid, NULL,
16331633
send_progress_thread, &pa);
16341634
if (err != 0) {
1635-
zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
1635+
zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(errno));
16361636
return (zfs_error(zhp->zfs_hdl,
16371637
EZFS_THREADCREATEFAILED, errbuf));
16381638
}
@@ -1651,7 +1651,7 @@ estimate_size(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags,
16511651
return (err);
16521652

16531653
if (err != 0) {
1654-
zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err));
1654+
zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(err));
16551655
return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
16561656
errbuf));
16571657
}
@@ -1767,7 +1767,7 @@ find_redact_book(libzfs_handle_t *hdl, const char *path,
17671767
"dataset to be sent no longer exists"));
17681768
} else {
17691769
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1770-
"unknown error: %s"), strerror(error));
1770+
"unknown error: %s"), zfs_strerror(error));
17711771
}
17721772
return (zfs_error(hdl, EZFS_BADPROP, errbuf));
17731773
}
@@ -2004,7 +2004,7 @@ zfs_send_resume_impl_cb_impl(libzfs_handle_t *hdl, sendflags_t *flags,
20042004
case ERANGE:
20052005
case EFAULT:
20062006
case EROFS:
2007-
zfs_error_aux(hdl, "%s", strerror(errno));
2007+
zfs_error_aux(hdl, "%s", zfs_strerror(errno));
20082008
return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
20092009

20102010
default:
@@ -2290,13 +2290,13 @@ send_prelim_records(zfs_handle_t *zhp, const char *from, int fd,
22902290
err = dump_record(&drr, packbuf, buflen, &zc, fd);
22912291
free(packbuf);
22922292
if (err != 0) {
2293-
zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err));
2293+
zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(err));
22942294
return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
22952295
errbuf));
22962296
}
22972297
err = send_conclusion_record(fd, &zc);
22982298
if (err != 0) {
2299-
zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err));
2299+
zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(err));
23002300
return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
23012301
errbuf));
23022302
}
@@ -2765,7 +2765,7 @@ zfs_send_one_cb_impl(zfs_handle_t *zhp, const char *from, int fd,
27652765
err = pthread_create(&ptid, NULL,
27662766
send_progress_thread, &pa);
27672767
if (err != 0) {
2768-
zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
2768+
zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(errno));
27692769
return (zfs_error(zhp->zfs_hdl,
27702770
EZFS_THREADCREATEFAILED, errbuf));
27712771
}
@@ -2823,7 +2823,7 @@ zfs_send_one_cb_impl(zfs_handle_t *zhp, const char *from, int fd,
28232823
case EPIPE:
28242824
case ERANGE:
28252825
case EROFS:
2826-
zfs_error_aux(hdl, "%s", strerror(errno));
2826+
zfs_error_aux(hdl, "%s", zfs_strerror(errno));
28272827
return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
28282828

28292829
default:

0 commit comments

Comments
 (0)