libbpf_errno.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
  4. * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
  5. * Copyright (C) 2015 Huawei Inc.
  6. * Copyright (C) 2017 Nicira, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License (not later!)
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this program; if not, see <http://www.gnu.org/licenses>
  20. */
  21. #undef _GNU_SOURCE
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "libbpf.h"
  25. #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
  26. #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
  27. #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
  28. static const char *libbpf_strerror_table[NR_ERRNO] = {
  29. [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
  30. [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
  31. [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
  32. [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
  33. [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
  34. [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
  35. [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
  36. [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
  37. [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
  38. [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
  39. [ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message",
  40. [ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence",
  41. };
  42. int libbpf_strerror(int err, char *buf, size_t size)
  43. {
  44. if (!buf || !size)
  45. return -1;
  46. err = err > 0 ? err : -err;
  47. if (err < __LIBBPF_ERRNO__START) {
  48. int ret;
  49. ret = strerror_r(err, buf, size);
  50. buf[size - 1] = '\0';
  51. return ret;
  52. }
  53. if (err < __LIBBPF_ERRNO__END) {
  54. const char *msg;
  55. msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
  56. snprintf(buf, size, "%s", msg);
  57. buf[size - 1] = '\0';
  58. return 0;
  59. }
  60. snprintf(buf, size, "Unknown libbpf error %d", err);
  61. buf[size - 1] = '\0';
  62. return -1;
  63. }