Re: [PATCH bpf-next v3 1/4] libbpf: introduce errstr() for stringifying errno

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Nov 11, 2024 at 1:29 PM Mykyta Yatsenko
<mykyta.yatsenko5@xxxxxxxxx> wrote:
>
> From: Mykyta Yatsenko <yatsenko@xxxxxxxx>
>
> Add function errstr(int err) that allows converting numeric error codes
> into string representations.
>
> Signed-off-by: Mykyta Yatsenko <yatsenko@xxxxxxxx>
> ---
>  tools/lib/bpf/str_error.c | 59 +++++++++++++++++++++++++++++++++++++++
>  tools/lib/bpf/str_error.h |  7 +++++
>  2 files changed, 66 insertions(+)
>
> diff --git a/tools/lib/bpf/str_error.c b/tools/lib/bpf/str_error.c
> index 5e6a1e27ddf9..cf817c0c7ddd 100644
> --- a/tools/lib/bpf/str_error.c
> +++ b/tools/lib/bpf/str_error.c
> @@ -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,58 @@ 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 -EINVAL: return "-EINVAL";
> +       case -EPERM: return "-EPERM";
> +       case -ENXIO: return "-ENXIO";
> +       case -ENOMEM: return "-ENOMEM";
> +       case -ENOENT: return "-ENOENT";
> +       case -E2BIG: return "-E2BIG";
> +       case -EEXIST: return "-EEXIST";
> +       case -EFAULT: return "-EFAULT";
> +       case -ENOSPC: return "-ENOSPC";
> +       case -EACCES: return "-EACCES";
> +       case -EAGAIN: return "-EAGAIN";
> +       case -EBADF: return "-EBADF";
> +       case -ENAMETOOLONG: return "-ENAMETOOLONG";
> +       case -ESRCH: return "-ESRCH";
> +       case -EBUSY: return "-EBUSY";
> +       case -ENOTSUPP: return "-ENOTSUPP";
> +       case -EPROTO: return "-EPROTO";
> +       case -ERANGE: return "-ERANGE";
> +       case -EMSGSIZE: return "-EMSGSIZE";
> +       case -EINTR: return "-EINTR";
> +       case -ENODATA: return "-ENODATA";
> +       case -ENODEV: return "-ENODEV";
> +       case -ENOLINK:return "-ENOLINK";
> +       case -EIO: return "-EIO";
> +       case -EUCLEAN: return "-EUCLEAN";
> +       case -EDOM: return "-EDOM";
> +       case -ELOOP: return "-ELOOP";
> +       case -EPROTONOSUPPORT: return "-EPROTONOSUPPORT";
> +       case -EDEADLK: return "-EDEADLK";
> +       case -EOVERFLOW: return "-EOVERFLOW";
> +       case -EOPNOTSUPP: return "-EOPNOTSUPP";
> +       case -EINPROGRESS: return "-EINPROGRESS";
> +       case -EBADFD: return "-EBADFD";
> +       case -EADDRINUSE: return "-EADDRINUSE";
> +       case -EADDRNOTAVAIL: return "-EADDRNOTAVAIL";
> +       case -ECANCELED: return "-ECANCELED";
> +       case -EILSEQ: return "-EILSEQ";
> +       case -EMFILE: return "-EMFILE";
> +       case -ENOTTY: return "-ENOTTY";
> +       case -EALREADY: return "-EALREADY";
> +       case -ECHILD: return "-ECHILD";

I added a few error code from include/uapi/asm-generic/errno-base.h
that were missing (just for completeness, as we already had absolute
majority of them), reordered alphabetically and reformatted to be a
bit more table-like. Applied to bpf-next.

Thanks a lot for this massive clean up, it's great!

> +       default:
> +               snprintf(buf, sizeof(buf), "%d", err);
> +               return buf;
> +       }
> +}
> diff --git a/tools/lib/bpf/str_error.h b/tools/lib/bpf/str_error.h
> index 626d7ffb03d6..66ffebde0684 100644
> --- a/tools/lib/bpf/str_error.h
> +++ b/tools/lib/bpf/str_error.h
> @@ -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 */
> --
> 2.47.0
>





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux