Skip to contentSkip to conversation

libbpf: preserve errno across pr_warn/pr_info/pr_debug

PATCH Applied Andrii Nakryiko 3 participants 3 messages Aug 10, 2022

Subsystems (2)
lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

PATCH 1/1 libbpf: preserve errno across pr_warn/pr_info/pr_debug

As suggested in [0], make sure that libbpf_print saves and restored errno and as such guaranteed that no matter what actual print callback user installs, macros like pr_warn/pr_info/pr_debug are completely transparent as far as errno goes.

While libbpf code is pretty careful about not clobbering important errno values accidentally with pr_warn(), it's a trivial change to make sure that pr_warn can be used anywhere without a risk of clobbering errno.

No functional changes, just future proofing.

[0] https://github.com/libbpf/libbpf/pull/536

---
 tools/lib/bpf/libbpf.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/lib/bpf/libbpf.c +5 -0
index f7364ea82ac1..917d975bd4c6 100644
@@ -223,13 +223,18 @@ __printf(2, 3)
 void libbpf_print(enum libbpf_print_level level, const char *format, ...)
 {
 	va_list args;
+	int old_errno;
 
 	if (!__libbpf_pr)
 		return;
 
+	old_errno = errno;
+
 	va_start(args, format);
 	__libbpf_pr(level, format, args);
 	va_end(args);
+
+	errno = old_errno;
 }
 
 static void pr_perm_msg(int err)
Signature · 1 line
2.30.2
Acked-by on v1 1/1
#1 lore raw
Quoted 44 lines · Andrii Nakryiko

As suggested in [0], make sure that libbpf_print saves and restored errno and as such guaranteed that no matter what actual print callback user installs, macros like pr_warn/pr_info/pr_debug are completely transparent as far as errno goes.

While libbpf code is pretty careful about not clobbering important errno values accidentally with pr_warn(), it's a trivial change to make sure that pr_warn can be used anywhere without a risk of clobbering errno.

No functional changes, just future proofing.

[0] https://github.com/libbpf/libbpf/pull/536

---
 tools/lib/bpf/libbpf.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/lib/bpf/libbpf.c +5 -0
index f7364ea82ac1..917d975bd4c6 100644
@@ -223,13 +223,18 @@ __printf(2, 3)
 void libbpf_print(enum libbpf_print_level level, const char *format, ...)
 {
 	va_list args;
+	int old_errno;
 
 	if (!__libbpf_pr)
 		return;
 
+	old_errno = errno;
+
 	va_start(args, format);
 	__libbpf_pr(level, format, args);
 	va_end(args);
+
+	errno = old_errno;
 }
 
 static void pr_perm_msg(int err)
Signature · 1 line
2.30.2

The change looks good to me.

#2 lore raw
In reply to Andrii Nakryiko
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 10 lines · On Wed, 10 Aug 2022 11:34:25 -0700 you wrote:

As suggested in [0], make sure that libbpf_print saves and restored errno and as such guaranteed that no matter what actual print callback user installs, macros like pr_warn/pr_info/pr_debug are completely transparent as far as errno goes.

While libbpf code is pretty careful about not clobbering important errno values accidentally with pr_warn(), it's a trivial change to make sure that pr_warn can be used anywhere without a risk of clobbering errno.

[...]

Here is the summary with links:
- [bpf-next] libbpf: preserve errno across pr_warn/pr_info/pr_debug

    https://git.kernel.org/bpf/bpf-next/c/d7c5802faff6

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html