Skip to content

Commit 548c2ed

Browse files
anakryikoMartin KaFai Lau
authored andcommitted
libbpf: fix libbpf_strerror_r() handling unknown errors
strerror_r(), used from libbpf-specific libbpf_strerror_r() wrapper is documented to return error in two different ways, depending on glibc version. Take that into account when handling strerror_r()'s own errors, which happens when we pass some non-standard (internal) kernel error to it. Before this patch we'd have "ERROR: strerror_r(524)=22", which is quite confusing. Now for the same situation we'll see a bit less visually scary "unknown error (-524)". At least we won't confuse user with irrelevant EINVAL (22). Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/r/20240507001335.1445325-5-andrii@kernel.org Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
1 parent 9d66d60 commit 548c2ed

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

tools/lib/bpf/str_error.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#undef _GNU_SOURCE
33
#include <string.h>
44
#include <stdio.h>
5+
#include <errno.h>
56
#include "str_error.h"
67

78
/* make sure libbpf doesn't use kernel-only integer typedefs */
@@ -15,7 +16,18 @@
1516
char *libbpf_strerror_r(int err, char *dst, int len)
1617
{
1718
int ret = strerror_r(err < 0 ? -err : err, dst, len);
18-
if (ret)
19-
snprintf(dst, len, "ERROR: strerror_r(%d)=%d", err, ret);
19+
/* on glibc <2.13, ret == -1 and errno is set, if strerror_r() can't
20+
* handle the error, on glibc >=2.13 *positive* (errno-like) error
21+
* code is returned directly
22+
*/
23+
if (ret == -1)
24+
ret = errno;
25+
if (ret) {
26+
if (ret == EINVAL)
27+
/* strerror_r() doesn't recognize this specific error */
28+
snprintf(dst, len, "unknown error (%d)", err < 0 ? err : -err);
29+
else
30+
snprintf(dst, len, "ERROR: strerror_r(%d)=%d", err, ret);
31+
}
2032
return dst;
2133
}

0 commit comments

Comments
 (0)