From 44537379fcba3bc89f3ead1ad9cd69a8d567fc72 Mon Sep 17 00:00:00 2001 From: Valentine Krasnobaeva Date: Fri, 20 Sep 2024 23:14:41 +0200 Subject: [PATCH] MINOR: tools: add errname to print errno macro name Add helper to print the name of errno's corresponding macro, for example "EINVAL" for errno=22. This may be helpful for debugging and for using in some CLI commands output. The switch-case in errname() contains only the errnos currently used in the code. So, it needs to be extended, if one starts to use new syscalls. --- include/haproxy/tools.h | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/include/haproxy/tools.h b/include/haproxy/tools.h index b96436115..435e158f2 100644 --- a/include/haproxy/tools.h +++ b/include/haproxy/tools.h @@ -27,6 +27,7 @@ #define _GNU_SOURCE #endif +#include #include #include #include @@ -1332,4 +1333,59 @@ int backup_env(void); int clean_env(void); int restore_env(void); +/* helper to print the name of errno's corresponding macro (for example "EINVAL" + * for errno=22) instead of calling strerror(errno). + */ +#define CASE_ERR(err) \ + case err: return #err + +static inline const char *errname(int err_num, char **out) +{ + /* only currently used errno values, please, add a new one, if you + * start using it in the code. + */ + switch (err_num) { + case 0: return "SUCCESS"; + CASE_ERR(EPERM); + CASE_ERR(ENOENT); + CASE_ERR(EINTR); + CASE_ERR(EIO); + CASE_ERR(E2BIG); + CASE_ERR(EBADF); + CASE_ERR(ECHILD); + CASE_ERR(EAGAIN); + CASE_ERR(ENOMEM); + CASE_ERR(EACCES); + CASE_ERR(EFAULT); + CASE_ERR(EINVAL); + CASE_ERR(ENFILE); + CASE_ERR(EMFILE); + CASE_ERR(ENOSPC); + CASE_ERR(ERANGE); + CASE_ERR(ENOSYS); + CASE_ERR(EADDRINUSE); + CASE_ERR(ECONNABORTED); + CASE_ERR(ECONNRESET); + CASE_ERR(EINPROGRESS); + CASE_ERR(ENOTCONN); + CASE_ERR(EADDRNOTAVAIL); + CASE_ERR(ECONNREFUSED); + CASE_ERR(ENETUNREACH); + CASE_ERR(ETIME); + CASE_ERR(EPROTO); + CASE_ERR(ENOTSOCK); + CASE_ERR(EMSGSIZE); + CASE_ERR(EPROTONOSUPPORT); + CASE_ERR(EAFNOSUPPORT); + CASE_ERR(ENOBUFS); + CASE_ERR(EISCONN); + CASE_ERR(ETIMEDOUT); + CASE_ERR(EALREADY); + + default: + memprintf(out, "%d", err_num); + return *out; + } +} + #endif /* _HAPROXY_TOOLS_H */ -- 2.39.5