]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
libbpf: Introduce errstr() for stringifying errno
authorMykyta Yatsenko <yatsenko@meta.com>
Mon, 11 Nov 2024 21:29:16 +0000 (21:29 +0000)
committerAndrii Nakryiko <andrii@kernel.org>
Tue, 12 Nov 2024 04:29:20 +0000 (20:29 -0800)
Add function errstr(int err) that allows converting numeric error codes
into string representations.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20241111212919.368971-2-mykyta.yatsenko5@gmail.com
tools/lib/bpf/str_error.c
tools/lib/bpf/str_error.h

index 5e6a1e27ddf90b2e01ebaeb23a9f2b3d43ecc5b1..8743049e32b7d4696b6b57820035686920ac81c3 100644 (file)
@@ -5,6 +5,10 @@
 #include <errno.h>
 #include "str_error.h"
 
+#ifndef ENOTSUPP
+#define ENOTSUPP       524
+#endif
+
 /* make sure libbpf doesn't use kernel-only integer typedefs */
 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
 
@@ -31,3 +35,70 @@ char *libbpf_strerror_r(int err, char *dst, int len)
        }
        return dst;
 }
+
+const char *errstr(int err)
+{
+       static __thread char buf[12];
+
+       if (err > 0)
+               err = -err;
+
+       switch (err) {
+       case -E2BIG:            return "-E2BIG";
+       case -EACCES:           return "-EACCES";
+       case -EADDRINUSE:       return "-EADDRINUSE";
+       case -EADDRNOTAVAIL:    return "-EADDRNOTAVAIL";
+       case -EAGAIN:           return "-EAGAIN";
+       case -EALREADY:         return "-EALREADY";
+       case -EBADF:            return "-EBADF";
+       case -EBADFD:           return "-EBADFD";
+       case -EBUSY:            return "-EBUSY";
+       case -ECANCELED:        return "-ECANCELED";
+       case -ECHILD:           return "-ECHILD";
+       case -EDEADLK:          return "-EDEADLK";
+       case -EDOM:             return "-EDOM";
+       case -EEXIST:           return "-EEXIST";
+       case -EFAULT:           return "-EFAULT";
+       case -EFBIG:            return "-EFBIG";
+       case -EILSEQ:           return "-EILSEQ";
+       case -EINPROGRESS:      return "-EINPROGRESS";
+       case -EINTR:            return "-EINTR";
+       case -EINVAL:           return "-EINVAL";
+       case -EIO:              return "-EIO";
+       case -EISDIR:           return "-EISDIR";
+       case -ELOOP:            return "-ELOOP";
+       case -EMFILE:           return "-EMFILE";
+       case -EMLINK:           return "-EMLINK";
+       case -EMSGSIZE:         return "-EMSGSIZE";
+       case -ENAMETOOLONG:     return "-ENAMETOOLONG";
+       case -ENFILE:           return "-ENFILE";
+       case -ENODATA:          return "-ENODATA";
+       case -ENODEV:           return "-ENODEV";
+       case -ENOENT:           return "-ENOENT";
+       case -ENOEXEC:          return "-ENOEXEC";
+       case -ENOLINK:          return "-ENOLINK";
+       case -ENOMEM:           return "-ENOMEM";
+       case -ENOSPC:           return "-ENOSPC";
+       case -ENOTBLK:          return "-ENOTBLK";
+       case -ENOTDIR:          return "-ENOTDIR";
+       case -ENOTSUPP:         return "-ENOTSUPP";
+       case -ENOTTY:           return "-ENOTTY";
+       case -ENXIO:            return "-ENXIO";
+       case -EOPNOTSUPP:       return "-EOPNOTSUPP";
+       case -EOVERFLOW:        return "-EOVERFLOW";
+       case -EPERM:            return "-EPERM";
+       case -EPIPE:            return "-EPIPE";
+       case -EPROTO:           return "-EPROTO";
+       case -EPROTONOSUPPORT:  return "-EPROTONOSUPPORT";
+       case -ERANGE:           return "-ERANGE";
+       case -EROFS:            return "-EROFS";
+       case -ESPIPE:           return "-ESPIPE";
+       case -ESRCH:            return "-ESRCH";
+       case -ETXTBSY:          return "-ETXTBSY";
+       case -EUCLEAN:          return "-EUCLEAN";
+       case -EXDEV:            return "-EXDEV";
+       default:
+               snprintf(buf, sizeof(buf), "%d", err);
+               return buf;
+       }
+}
index 626d7ffb03d60d27232dc6b9dd101e98f8a71dea..66ffebde0684aad432c119699182210b49a04545 100644 (file)
@@ -6,4 +6,11 @@
 
 char *libbpf_strerror_r(int err, char *dst, int len);
 
+/**
+ * @brief **errstr()** returns string corresponding to numeric errno
+ * @param err negative numeric errno
+ * @return pointer to string representation of the errno, that is invalidated
+ * upon the next call.
+ */
+const char *errstr(int err);
 #endif /* __LIBBPF_STR_ERROR_H */