treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / lib / bpf / libbpf_errno.c
blob0afb51f7a91941dbd63e634905d498841c2283b7
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
3 /*
4 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
5 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
6 * Copyright (C) 2015 Huawei Inc.
7 * Copyright (C) 2017 Nicira, Inc.
8 */
10 #undef _GNU_SOURCE
11 #include <stdio.h>
12 #include <string.h>
14 #include "libbpf.h"
16 /* make sure libbpf doesn't use kernel-only integer typedefs */
17 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
19 #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
20 #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
21 #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
23 static const char *libbpf_strerror_table[NR_ERRNO] = {
24 [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
25 [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
26 [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
27 [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
28 [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
29 [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
30 [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
31 [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
32 [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
33 [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
34 [ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message",
35 [ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence",
36 [ERRCODE_OFFSET(NLPARSE)] = "Incorrect netlink message parsing",
39 int libbpf_strerror(int err, char *buf, size_t size)
41 if (!buf || !size)
42 return -1;
44 err = err > 0 ? err : -err;
46 if (err < __LIBBPF_ERRNO__START) {
47 int ret;
49 ret = strerror_r(err, buf, size);
50 buf[size - 1] = '\0';
51 return ret;
54 if (err < __LIBBPF_ERRNO__END) {
55 const char *msg;
57 msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
58 snprintf(buf, size, "%s", msg);
59 buf[size - 1] = '\0';
60 return 0;
63 snprintf(buf, size, "Unknown libbpf error %d", err);
64 buf[size - 1] = '\0';
65 return -1;