]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
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 cf8f1f0469935fe328e00147de071270f2752be4..80a1fc234dbb8f39e96965b1871d95a8fbd2049d 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 407fbff60067d332af6f848fee08bf8de59fd37d..592d288672261a6558fd4cfbb70e7d4f908ab5e1 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 50b5080c4e80cb55f35868ec7ce71cca28f0a410..2d926649e818e0fbea2ff4649766d6116080f74f 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 3afaf5a9947f20ed30400bfe7906daa401e9ac34..eba79101bfcba51192eded2b7742140d04e248b2 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.