]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: add errname to print errno macro name
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Fri, 20 Sep 2024 21:14:41 +0000 (23:14 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 24 Jan 2025 08:54:57 +0000 (09:54 +0100)
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

index b964361152fd59bb0661e289077a479f72ffdd17..435e158f263ce4447603fc9cddda690f6c4ce983 100644 (file)
@@ -27,6 +27,7 @@
 #define _GNU_SOURCE
 #endif
 
+#include <errno.h>
 #include <string.h>
 #include <stdio.h>
 #include <time.h>
@@ -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 */