BPF List
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii@kernel.org>
To: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net,
	martin.lau@kernel.org
Cc: andrii@kernel.org, kernel-team@meta.com
Subject: [PATCH bpf-next 4/7] libbpf: fix libbpf_strerror_r() handling unknown errors
Date: Mon,  6 May 2024 17:13:32 -0700	[thread overview]
Message-ID: <20240507001335.1445325-5-andrii@kernel.org> (raw)
In-Reply-To: <20240507001335.1445325-1-andrii@kernel.org>

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>
---
 tools/lib/bpf/str_error.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/tools/lib/bpf/str_error.c b/tools/lib/bpf/str_error.c
index 146da01979c7..5e6a1e27ddf9 100644
--- a/tools/lib/bpf/str_error.c
+++ b/tools/lib/bpf/str_error.c
@@ -2,6 +2,7 @@
 #undef _GNU_SOURCE
 #include <string.h>
 #include <stdio.h>
+#include <errno.h>
 #include "str_error.h"
 
 /* make sure libbpf doesn't use kernel-only integer typedefs */
@@ -15,7 +16,18 @@
 char *libbpf_strerror_r(int err, char *dst, int len)
 {
 	int ret = strerror_r(err < 0 ? -err : err, dst, len);
-	if (ret)
-		snprintf(dst, len, "ERROR: strerror_r(%d)=%d", err, ret);
+	/* on glibc <2.13, ret == -1 and errno is set, if strerror_r() can't
+	 * handle the error, on glibc >=2.13 *positive* (errno-like) error
+	 * code is returned directly
+	 */
+	if (ret == -1)
+		ret = errno;
+	if (ret) {
+		if (ret == EINVAL)
+			/* strerror_r() doesn't recognize this specific error */
+			snprintf(dst, len, "unknown error (%d)", err < 0 ? err : -err);
+		else
+			snprintf(dst, len, "ERROR: strerror_r(%d)=%d", err, ret);
+	}
 	return dst;
 }
-- 
2.43.0


  parent reply	other threads:[~2024-05-07  0:13 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-07  0:13 [PATCH bpf-next 0/7] libbpf: further struct_ops fixes and improvements Andrii Nakryiko
2024-05-07  0:13 ` [PATCH bpf-next 1/7] libbpf: remove unnecessary struct_ops prog validity check Andrii Nakryiko
2024-05-07  0:13 ` [PATCH bpf-next 2/7] libbpf: handle yet another corner case of nulling out struct_ops program Andrii Nakryiko
2024-05-07  0:13 ` [PATCH bpf-next 3/7] selftests/bpf: add another struct_ops callback use case test Andrii Nakryiko
2024-05-07  0:13 ` Andrii Nakryiko [this message]
2024-05-07  0:13 ` [PATCH bpf-next 5/7] libbpf: improve early detection of doomed-to-fail BPF program loading Andrii Nakryiko
2024-05-07  0:13 ` [PATCH bpf-next 6/7] selftests/bpf: validate struct_ops early failure detection logic Andrii Nakryiko
2024-05-07  0:13 ` [PATCH bpf-next 7/7] selftests/bpf: shorten subtest names for struct_ops_module test Andrii Nakryiko
2024-05-08  0:30 ` [PATCH bpf-next 0/7] libbpf: further struct_ops fixes and improvements patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240507001335.1445325-5-andrii@kernel.org \
    --to=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@meta.com \
    --cc=martin.lau@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox