diff options
author | Xin Liu <liuxin350@huawei.com> | 2022-12-23 16:36:18 +0300 |
---|---|---|
committer | Alexei Starovoitov <ast@kernel.org> | 2022-12-29 01:03:51 +0300 |
commit | 07453245620c075779abefa2a9f469fa336e7510 (patch) | |
tree | 27d0c2b236c8e450653d4ed66dc48dac97a85f5f /tools/lib/bpf/libbpf_internal.h | |
parent | 4633a00682589931e8415c166979d8e5dd174282 (diff) | |
download | linux-07453245620c075779abefa2a9f469fa336e7510.tar.xz |
libbpf: fix errno is overwritten after being closed.
In the ensure_good_fd function, if the fcntl function succeeds but
the close function fails, ensure_good_fd returns a normal fd and
sets errno, which may cause users to misunderstand. The close
failure is not a serious problem, and the correct FD has been
handed over to the upper-layer application. Let's restore errno here.
Signed-off-by: Xin Liu <liuxin350@huawei.com>
Link: https://lore.kernel.org/r/20221223133618.10323-1-liuxin350@huawei.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'tools/lib/bpf/libbpf_internal.h')
-rw-r--r-- | tools/lib/bpf/libbpf_internal.h | 5 |
1 files changed, 2 insertions, 3 deletions
diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index 377642ff51fc..98333a6c38e9 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -543,10 +543,9 @@ static inline int ensure_good_fd(int fd) fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); saved_errno = errno; close(old_fd); - if (fd < 0) { + errno = saved_errno; + if (fd < 0) pr_warn("failed to dup FD %d to FD > 2: %d\n", old_fd, -saved_errno); - errno = saved_errno; - } } return fd; } |