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 8 #ifndef ENOTSUPP 9 #define ENOTSUPP 524 10 #endif 11 12 /* make sure libbpf doesn't use kernel-only integer typedefs */ 13 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 14 15 /* 16 * Wrapper to allow for building in non-GNU systems such as Alpine Linux's musl 17 * libc, while checking strerror_r() return to avoid having to check this in 18 * all places calling it. 19 */ 20 char *libbpf_strerror_r(int err, char *dst, int len) 21 { 22 int ret = strerror_r(err < 0 ? -err : err, dst, len); 23 /* on glibc <2.13, ret == -1 and errno is set, if strerror_r() can't 24 * handle the error, on glibc >=2.13 *positive* (errno-like) error 25 * code is returned directly 26 */ 27 if (ret == -1) 28 ret = errno; 29 if (ret) { 30 if (ret == EINVAL) 31 /* strerror_r() doesn't recognize this specific error */ 32 snprintf(dst, len, "unknown error (%d)", err < 0 ? err : -err); 33 else 34 snprintf(dst, len, "ERROR: strerror_r(%d)=%d", err, ret); 35 } 36 return dst; 37 } 38 39 const char *errstr(int err) 40 { 41 static __thread char buf[12]; 42 43 if (err > 0) 44 err = -err; 45 46 switch (err) { 47 case -E2BIG: return "-E2BIG"; 48 case -EACCES: return "-EACCES"; 49 case -EADDRINUSE: return "-EADDRINUSE"; 50 case -EADDRNOTAVAIL: return "-EADDRNOTAVAIL"; 51 case -EAGAIN: return "-EAGAIN"; 52 case -EALREADY: return "-EALREADY"; 53 case -EBADF: return "-EBADF"; 54 case -EBADFD: return "-EBADFD"; 55 case -EBUSY: return "-EBUSY"; 56 case -ECANCELED: return "-ECANCELED"; 57 case -ECHILD: return "-ECHILD"; 58 case -EDEADLK: return "-EDEADLK"; 59 case -EDOM: return "-EDOM"; 60 case -EEXIST: return "-EEXIST"; 61 case -EFAULT: return "-EFAULT"; 62 case -EFBIG: return "-EFBIG"; 63 case -EILSEQ: return "-EILSEQ"; 64 case -EINPROGRESS: return "-EINPROGRESS"; 65 case -EINTR: return "-EINTR"; 66 case -EINVAL: return "-EINVAL"; 67 case -EIO: return "-EIO"; 68 case -EISDIR: return "-EISDIR"; 69 case -ELOOP: return "-ELOOP"; 70 case -EMFILE: return "-EMFILE"; 71 case -EMLINK: return "-EMLINK"; 72 case -EMSGSIZE: return "-EMSGSIZE"; 73 case -ENAMETOOLONG: return "-ENAMETOOLONG"; 74 case -ENFILE: return "-ENFILE"; 75 case -ENODATA: return "-ENODATA"; 76 case -ENODEV: return "-ENODEV"; 77 case -ENOENT: return "-ENOENT"; 78 case -ENOEXEC: return "-ENOEXEC"; 79 case -ENOLINK: return "-ENOLINK"; 80 case -ENOMEM: return "-ENOMEM"; 81 case -ENOSPC: return "-ENOSPC"; 82 case -ENOTBLK: return "-ENOTBLK"; 83 case -ENOTDIR: return "-ENOTDIR"; 84 case -ENOTSUPP: return "-ENOTSUPP"; 85 case -ENOTTY: return "-ENOTTY"; 86 case -ENXIO: return "-ENXIO"; 87 case -EOPNOTSUPP: return "-EOPNOTSUPP"; 88 case -EOVERFLOW: return "-EOVERFLOW"; 89 case -EPERM: return "-EPERM"; 90 case -EPIPE: return "-EPIPE"; 91 case -EPROTO: return "-EPROTO"; 92 case -EPROTONOSUPPORT: return "-EPROTONOSUPPORT"; 93 case -ERANGE: return "-ERANGE"; 94 case -EROFS: return "-EROFS"; 95 case -ESPIPE: return "-ESPIPE"; 96 case -ESRCH: return "-ESRCH"; 97 case -ETXTBSY: return "-ETXTBSY"; 98 case -EUCLEAN: return "-EUCLEAN"; 99 case -EXDEV: return "-EXDEV"; 100 default: 101 snprintf(buf, sizeof(buf), "%d", err); 102 return buf; 103 } 104 } 105