MINOR: init: report the haproxy version and executable path once on errors
authorWilly Tarreau <w@1wt.eu>
Thu, 16 Apr 2020 08:52:41 +0000 (10:52 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 16 Apr 2020 08:52:41 +0000 (10:52 +0200)
If haproxy fails to start and emits an alert, then it can be useful
to have it also emit the version and the path used to load it. Some
users may be mistakenly launching the wrong binary due to a misconfigured
PATH variable and this will save them some troubleshooting time when it
reports that some keywords are not understood.

What we do here is that we *try* to extract the binary name from the
AUX vector on glibc, and we report this as a NOTICE tag before the
very first alert is emitted.

include/common/standard.h
include/types/global.h
src/log.c
src/standard.c

index cf8f1f0..80a1fc2 100644 (file)
@@ -1496,6 +1496,7 @@ void dump_addr_and_bytes(struct buffer *buf, const char *pfx, const void *addr,
 void dump_hex(struct buffer *out, const char *pfx, const void *buf, int len, int unsafe);
 int may_access(const void *ptr);
 void *resolve_sym_name(struct buffer *buf, const char *pfx, void *addr);
+const char *get_exec_path();
 
 #if defined(USE_BACKTRACE)
 /* Note that this may result in opening libgcc() on first call, so it may need
index 407fbff..592d288 100644 (file)
@@ -245,6 +245,7 @@ extern unsigned char boot_seed[20];  // per-boot random seed (160 bits initially
 /* bit values to go with "warned" above */
 #define WARN_ANY                    0x00000001 /* any warning was emitted */
 #define WARN_FORCECLOSE_DEPRECATED  0x00000002
+#define WARN_EXEC_PATH              0x00000004 /* executable path already reported */
 
 
 /* to be used with warned and WARN_* */
index 50b5080..2d92664 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -29,6 +29,7 @@
 #include <common/initcall.h>
 #include <common/standard.h>
 #include <common/time.h>
+#include <common/version.h>
 
 #include <types/cli.h>
 #include <types/global.h>
@@ -1109,6 +1110,14 @@ void ha_alert(const char *fmt, ...)
        va_list argp;
 
        if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
+               if (!(warned & WARN_EXEC_PATH)) {
+                       const char *path = get_exec_path();
+
+                       warned |= WARN_EXEC_PATH;
+                       ha_notice("haproxy version is %s\n", haproxy_version);
+                       if (path)
+                               ha_notice("path to executable is %s\n", path);
+               }
                va_start(argp, fmt);
                print_message("ALERT", fmt, argp);
                va_end(argp);
index 3afaf5a..eba7910 100644 (file)
 #include <link.h>
 #endif
 
+#if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
+#include <sys/auxv.h>
+#endif
+
 #include <ctype.h>
 #include <errno.h>
 #include <netdb.h>
@@ -4335,6 +4339,22 @@ void debug_hexdump(FILE *out, const char *pfx, const char *buf,
        }
 }
 
+/* Tries to report the executable path name on platforms supporting this. If
+ * not found or not possible, returns NULL.
+ */
+const char *get_exec_path()
+{
+       const char *ret = NULL;
+
+#if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
+       long execfn = getauxval(AT_EXECFN);
+
+       if (execfn && execfn != ENOENT)
+               ret = (const char *)execfn;
+#endif
+       return ret;
+}
+
 #ifdef __ELF__
 /* calls dladdr() or dladdr1() on <addr> and <dli>. If dladdr1 is available,
  * also returns the symbol size in <size>, otherwise returns 0 there.