aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf/libbpf_errno.c
diff options
context:
space:
mode:
authorJakub Kicinski <jakub.kicinski@netronome.com>2018-07-10 17:43:04 -0400
committerDaniel Borkmann <daniel@iogearbox.net>2018-07-11 16:13:34 -0400
commit8d13406c02f9c38f106416e1dbe0e68059b9f59a (patch)
tree479c8745b8045b8a024615cbe52bd24d714f0af0 /tools/lib/bpf/libbpf_errno.c
parentc8406848badd0a0b040b0d286e612678662a2ab3 (diff)
tools: libbpf: move library error code into a separate file
libbpf_strerror() depends on XSI-compliant (POSIX) version of strerror_r(), which prevents us from using GNU-extensions in libbpf.c, like reallocarray() or dup3(). Move error printing code into a separate file to allow it to continue using POSIX strerror_r(). No functional changes. Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'tools/lib/bpf/libbpf_errno.c')
-rw-r--r--tools/lib/bpf/libbpf_errno.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/tools/lib/bpf/libbpf_errno.c b/tools/lib/bpf/libbpf_errno.c
new file mode 100644
index 000000000000..d9ba851bd7f9
--- /dev/null
+++ b/tools/lib/bpf/libbpf_errno.c
@@ -0,0 +1,74 @@
1// SPDX-License-Identifier: LGPL-2.1
2
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 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation;
12 * version 2.1 of the License (not later!)
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this program; if not, see <http://www.gnu.org/licenses>
21 */
22
23#include <stdio.h>
24#include <string.h>
25
26#include "libbpf.h"
27
28#define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
29#define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
30#define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
31
32static const char *libbpf_strerror_table[NR_ERRNO] = {
33 [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
34 [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
35 [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
36 [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
37 [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
38 [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
39 [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
40 [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
41 [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
42 [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
43 [ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message",
44 [ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence",
45};
46
47int libbpf_strerror(int err, char *buf, size_t size)
48{
49 if (!buf || !size)
50 return -1;
51
52 err = err > 0 ? err : -err;
53
54 if (err < __LIBBPF_ERRNO__START) {
55 int ret;
56
57 ret = strerror_r(err, buf, size);
58 buf[size - 1] = '\0';
59 return ret;
60 }
61
62 if (err < __LIBBPF_ERRNO__END) {
63 const char *msg;
64
65 msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
66 snprintf(buf, size, "%s", msg);
67 buf[size - 1] = '\0';
68 return 0;
69 }
70
71 snprintf(buf, size, "Unknown libbpf error %d", err);
72 buf[size - 1] = '\0';
73 return -1;
74}