str_error.c 1020 B

123456789101112131415161718192021222324252627282930313233
  1. // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
  2. #undef _GNU_SOURCE
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <errno.h>
  6. #include "str_error.h"
  7. /* make sure libbpf doesn't use kernel-only integer typedefs */
  8. #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
  9. /*
  10. * Wrapper to allow for building in non-GNU systems such as Alpine Linux's musl
  11. * libc, while checking strerror_r() return to avoid having to check this in
  12. * all places calling it.
  13. */
  14. char *libbpf_strerror_r(int err, char *dst, int len)
  15. {
  16. int ret = strerror_r(err < 0 ? -err : err, dst, len);
  17. /* on glibc <2.13, ret == -1 and errno is set, if strerror_r() can't
  18. * handle the error, on glibc >=2.13 *positive* (errno-like) error
  19. * code is returned directly
  20. */
  21. if (ret == -1)
  22. ret = errno;
  23. if (ret) {
  24. if (ret == EINVAL)
  25. /* strerror_r() doesn't recognize this specific error */
  26. snprintf(dst, len, "unknown error (%d)", err < 0 ? err : -err);
  27. else
  28. snprintf(dst, len, "ERROR: strerror_r(%d)=%d", err, ret);
  29. }
  30. return dst;
  31. }