mirror of
https://github.com/torvalds/linux.git
synced 2024-11-26 06:02:05 +00:00
c9883ee9d1
This is a small improvement in libbpf_strerror. When libbpf_strerror is used to obtain the system error description, if the length of the buf is insufficient, libbpf_sterror returns ERANGE and sets errno to ERANGE. However, this processing is not performed when the error code customized by libbpf is obtained. Make some minor improvements here, return -ERANGE and set errno to ERANGE when buf is not enough for custom description. Signed-off-by: Xin Liu <liuxin350@huawei.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20221210082045.233697-1-liuxin350@huawei.com
76 lines
2.3 KiB
C
76 lines
2.3 KiB
C
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
|
|
|
|
/*
|
|
* Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
|
|
* Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
|
|
* Copyright (C) 2015 Huawei Inc.
|
|
* Copyright (C) 2017 Nicira, Inc.
|
|
*/
|
|
|
|
#undef _GNU_SOURCE
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "libbpf.h"
|
|
#include "libbpf_internal.h"
|
|
|
|
/* make sure libbpf doesn't use kernel-only integer typedefs */
|
|
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
|
|
|
|
#define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
|
|
#define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
|
|
#define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
|
|
|
|
static const char *libbpf_strerror_table[NR_ERRNO] = {
|
|
[ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
|
|
[ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
|
|
[ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
|
|
[ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
|
|
[ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
|
|
[ERRCODE_OFFSET(RELOC)] = "Relocation failed",
|
|
[ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
|
|
[ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
|
|
[ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
|
|
[ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
|
|
[ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message",
|
|
[ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence",
|
|
[ERRCODE_OFFSET(NLPARSE)] = "Incorrect netlink message parsing",
|
|
};
|
|
|
|
int libbpf_strerror(int err, char *buf, size_t size)
|
|
{
|
|
int ret;
|
|
|
|
if (!buf || !size)
|
|
return libbpf_err(-EINVAL);
|
|
|
|
err = err > 0 ? err : -err;
|
|
|
|
if (err < __LIBBPF_ERRNO__START) {
|
|
ret = strerror_r(err, buf, size);
|
|
buf[size - 1] = '\0';
|
|
return libbpf_err_errno(ret);
|
|
}
|
|
|
|
if (err < __LIBBPF_ERRNO__END) {
|
|
const char *msg;
|
|
|
|
msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
|
|
ret = snprintf(buf, size, "%s", msg);
|
|
buf[size - 1] = '\0';
|
|
/* The length of the buf and msg is positive.
|
|
* A negative number may be returned only when the
|
|
* size exceeds INT_MAX. Not likely to appear.
|
|
*/
|
|
if (ret >= size)
|
|
return libbpf_err(-ERANGE);
|
|
return 0;
|
|
}
|
|
|
|
ret = snprintf(buf, size, "Unknown libbpf error %d", err);
|
|
buf[size - 1] = '\0';
|
|
if (ret >= size)
|
|
return libbpf_err(-ERANGE);
|
|
return libbpf_err(-ENOENT);
|
|
}
|