1*f7c14bbaSAndroid Build Coastguard Worker // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2*f7c14bbaSAndroid Build Coastguard Worker
3*f7c14bbaSAndroid Build Coastguard Worker /*
4*f7c14bbaSAndroid Build Coastguard Worker * Copyright (C) 2013-2015 Alexei Starovoitov <[email protected]>
5*f7c14bbaSAndroid Build Coastguard Worker * Copyright (C) 2015 Wang Nan <[email protected]>
6*f7c14bbaSAndroid Build Coastguard Worker * Copyright (C) 2015 Huawei Inc.
7*f7c14bbaSAndroid Build Coastguard Worker * Copyright (C) 2017 Nicira, Inc.
8*f7c14bbaSAndroid Build Coastguard Worker */
9*f7c14bbaSAndroid Build Coastguard Worker
10*f7c14bbaSAndroid Build Coastguard Worker #undef _GNU_SOURCE
11*f7c14bbaSAndroid Build Coastguard Worker #include <stdio.h>
12*f7c14bbaSAndroid Build Coastguard Worker #include <string.h>
13*f7c14bbaSAndroid Build Coastguard Worker
14*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf.h"
15*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_internal.h"
16*f7c14bbaSAndroid Build Coastguard Worker
17*f7c14bbaSAndroid Build Coastguard Worker /* make sure libbpf doesn't use kernel-only integer typedefs */
18*f7c14bbaSAndroid Build Coastguard Worker #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
19*f7c14bbaSAndroid Build Coastguard Worker
20*f7c14bbaSAndroid Build Coastguard Worker #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
21*f7c14bbaSAndroid Build Coastguard Worker #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
22*f7c14bbaSAndroid Build Coastguard Worker #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
23*f7c14bbaSAndroid Build Coastguard Worker
24*f7c14bbaSAndroid Build Coastguard Worker static const char *libbpf_strerror_table[NR_ERRNO] = {
25*f7c14bbaSAndroid Build Coastguard Worker [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
26*f7c14bbaSAndroid Build Coastguard Worker [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
27*f7c14bbaSAndroid Build Coastguard Worker [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
28*f7c14bbaSAndroid Build Coastguard Worker [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
29*f7c14bbaSAndroid Build Coastguard Worker [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
30*f7c14bbaSAndroid Build Coastguard Worker [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
31*f7c14bbaSAndroid Build Coastguard Worker [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
32*f7c14bbaSAndroid Build Coastguard Worker [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
33*f7c14bbaSAndroid Build Coastguard Worker [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
34*f7c14bbaSAndroid Build Coastguard Worker [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
35*f7c14bbaSAndroid Build Coastguard Worker [ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message",
36*f7c14bbaSAndroid Build Coastguard Worker [ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence",
37*f7c14bbaSAndroid Build Coastguard Worker [ERRCODE_OFFSET(NLPARSE)] = "Incorrect netlink message parsing",
38*f7c14bbaSAndroid Build Coastguard Worker };
39*f7c14bbaSAndroid Build Coastguard Worker
libbpf_strerror(int err,char * buf,size_t size)40*f7c14bbaSAndroid Build Coastguard Worker int libbpf_strerror(int err, char *buf, size_t size)
41*f7c14bbaSAndroid Build Coastguard Worker {
42*f7c14bbaSAndroid Build Coastguard Worker int ret;
43*f7c14bbaSAndroid Build Coastguard Worker
44*f7c14bbaSAndroid Build Coastguard Worker if (!buf || !size)
45*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
46*f7c14bbaSAndroid Build Coastguard Worker
47*f7c14bbaSAndroid Build Coastguard Worker err = err > 0 ? err : -err;
48*f7c14bbaSAndroid Build Coastguard Worker
49*f7c14bbaSAndroid Build Coastguard Worker if (err < __LIBBPF_ERRNO__START) {
50*f7c14bbaSAndroid Build Coastguard Worker ret = strerror_r(err, buf, size);
51*f7c14bbaSAndroid Build Coastguard Worker buf[size - 1] = '\0';
52*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_errno(ret);
53*f7c14bbaSAndroid Build Coastguard Worker }
54*f7c14bbaSAndroid Build Coastguard Worker
55*f7c14bbaSAndroid Build Coastguard Worker if (err < __LIBBPF_ERRNO__END) {
56*f7c14bbaSAndroid Build Coastguard Worker const char *msg;
57*f7c14bbaSAndroid Build Coastguard Worker
58*f7c14bbaSAndroid Build Coastguard Worker msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
59*f7c14bbaSAndroid Build Coastguard Worker ret = snprintf(buf, size, "%s", msg);
60*f7c14bbaSAndroid Build Coastguard Worker buf[size - 1] = '\0';
61*f7c14bbaSAndroid Build Coastguard Worker /* The length of the buf and msg is positive.
62*f7c14bbaSAndroid Build Coastguard Worker * A negative number may be returned only when the
63*f7c14bbaSAndroid Build Coastguard Worker * size exceeds INT_MAX. Not likely to appear.
64*f7c14bbaSAndroid Build Coastguard Worker */
65*f7c14bbaSAndroid Build Coastguard Worker if (ret >= size)
66*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ERANGE);
67*f7c14bbaSAndroid Build Coastguard Worker return 0;
68*f7c14bbaSAndroid Build Coastguard Worker }
69*f7c14bbaSAndroid Build Coastguard Worker
70*f7c14bbaSAndroid Build Coastguard Worker ret = snprintf(buf, size, "Unknown libbpf error %d", err);
71*f7c14bbaSAndroid Build Coastguard Worker buf[size - 1] = '\0';
72*f7c14bbaSAndroid Build Coastguard Worker if (ret >= size)
73*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ERANGE);
74*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOENT);
75*f7c14bbaSAndroid Build Coastguard Worker }
76