From: chenyuan_fl@163.com
To: bpf@vger.kernel.org
Cc: Quentin Monnet <qmo@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Yuan Chen <chenyuan@kylinos.cn>
Subject: [PATCH bpf-next v4 2/3] bpftool: fix spurious batch file read error
Date: Mon, 10 Aug 2026 17:04:58 +0800 [thread overview]
Message-ID: <20260810090459.2666243-3-chenyuan_fl@163.com> (raw)
In-Reply-To: <20260810090459.2666243-1-chenyuan_fl@163.com>
From: Yuan Chen <chenyuan@kylinos.cn>
do_batch() checks errno after the read loop to detect read failures,
but fgets() does not clear errno on success, so a stale errno left by
a previously executed command (e.g. map dump's EBADF from a double
close) makes bpftool report a batch file read failure and exit with an
error even though every command succeeded. Use ferror() instead, and
track the too-long-line case explicitly.
Since do_batch() no longer inspects errno to detect read failures, drop
the USE_LIBCAP errno reset in main() that existed only to keep errno
clean for the batch mode.
Fixes: 71bb428fe2c1 ("tools: bpf: add bpftool")
Signed-off-by: Yuan Chen <chenyuan@kylinos.cn>
---
tools/bpf/bpftool/main.c | 18 ++++++------------
1 file changed, 6 insertions(+), 12 deletions(-)
diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index c91e1a6e1a1e..0bbb2e198450 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -336,6 +336,7 @@ static int do_batch(int argc, char **argv)
char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
char *n_argv[BATCH_ARG_NB_MAX];
unsigned int lines = 0;
+ bool line_too_long = false;
int n_argc;
FILE *fp;
char *cp;
@@ -371,7 +372,7 @@ static int do_batch(int argc, char **argv)
*cp = '\0';
if (strlen(buf) == sizeof(buf) - 1) {
- errno = E2BIG;
+ line_too_long = true;
break;
}
@@ -429,7 +430,10 @@ static int do_batch(int argc, char **argv)
lines++;
}
- if (errno && errno != ENOENT) {
+ if (line_too_long) {
+ p_err("reading batch file failed: %s", strerror(E2BIG));
+ err = -1;
+ } else if (ferror(fp)) {
p_err("reading batch file failed: %s", strerror(errno));
err = -1;
} else {
@@ -467,16 +471,6 @@ int main(int argc, char **argv)
setlinebuf(stdout);
-#ifdef USE_LIBCAP
- /* Libcap < 2.63 hooks before main() to compute the number of
- * capabilities of the running kernel, and doing so it calls prctl()
- * which may fail and set errno to non-zero.
- * Let's reset errno to make sure this does not interfere with the
- * batch mode.
- */
- errno = 0;
-#endif
-
last_do_help = do_help;
pretty_output = false;
json_output = false;
--
2.54.0
next prev parent reply other threads:[~2026-08-10 9:05 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-10 9:04 [PATCH bpf-next v4 0/3] bpftool: fix batch file handling issues chenyuan_fl
2026-08-10 9:04 ` [PATCH bpf-next v4 1/3] bpftool: fix double close in map dump chenyuan_fl
2026-08-10 9:20 ` sashiko-bot
2026-08-10 9:04 ` chenyuan_fl [this message]
2026-08-10 9:15 ` [PATCH bpf-next v4 2/3] bpftool: fix spurious batch file read error sashiko-bot
2026-08-10 10:13 ` bot+bpf-ci
2026-08-10 9:04 ` [PATCH bpf-next v4 3/3] bpftool: Fix bypass of the batch line length check by comments chenyuan_fl
2026-08-10 9:14 ` sashiko-bot
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=20260810090459.2666243-3-chenyuan_fl@163.com \
--to=chenyuan_fl@163.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=chenyuan@kylinos.cn \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=memxor@gmail.com \
--cc=qmo@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