libbpf_errno.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include <stdio.h>
  22. #include <string.h>
  23. #include "libbpf.h"
  24. #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
  25. #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
  26. #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
  27. static const char *libbpf_strerror_table[NR_ERRNO] = {
  28. [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
  29. [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
  30. [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
  31. [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
  32. [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
  33. [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
  34. [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
  35. [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
  36. [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
  37. [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
  38. [ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message",
  39. [ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence",
  40. };
  41. int libbpf_strerror(int err, char *buf, size_t size)
  42. {
  43. if (!buf || !size)
  44. return -1;
  45. err = err > 0 ? err : -err;
  46. if (err < __LIBBPF_ERRNO__START) {
  47. int ret;
  48. ret = strerror_r(err, buf, size);
  49. buf[size - 1] = '\0';
  50. return ret;
  51. }
  52. if (err < __LIBBPF_ERRNO__END) {
  53. const char *msg;
  54. msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
  55. snprintf(buf, size, "%s", msg);
  56. buf[size - 1] = '\0';
  57. return 0;
  58. }
  59. snprintf(buf, size, "Unknown libbpf error %d", err);
  60. buf[size - 1] = '\0';
  61. return -1;
  62. }