From e74a98a6e3d6440123bcf9441f647375beb1a0a4 Mon Sep 17 00:00:00 2001 From: i-ky Date: Wed, 24 Apr 2019 01:10:23 +0300 Subject: [PATCH] Replace zbx_snprintf() function/macro with function pointer which is initialized in runtime --- configure.ac | 3 +++ src/libzbxpgsql.c | 23 +++++++++++++++++++++++ src/libzbxpgsql.h | 4 ++++ 3 files changed, 30 insertions(+) diff --git a/configure.ac b/configure.ac index bd91266..a937ccd 100644 --- a/configure.ac +++ b/configure.ac @@ -42,6 +42,9 @@ if test ! "x$found_zabbix" = "xyes"; then AC_MSG_ERROR([Zabbix headers not found]) fi +# Checking for libdl +AC_CHECK_LIB(dl,dlopen) + # output AC_CONFIG_FILES([ Makefile diff --git a/src/libzbxpgsql.c b/src/libzbxpgsql.c index 9d2b654..1439458 100644 --- a/src/libzbxpgsql.c +++ b/src/libzbxpgsql.c @@ -25,6 +25,10 @@ #include "libzbxpgsql.h" +#include + +size_t (*zbx_snprintf)(char *str, size_t count, const char *fmt, ...) = NULL; + // Define custom keys static ZBX_METRIC keys[] = /* KEY FLAG FUNCTION TEST PARAMETERS */ @@ -180,7 +184,26 @@ int zbx_module_uninit() { */ int zbx_module_init() { + void *handle; + zabbix_log(LOG_LEVEL_INFORMATION, "starting agent module %s", PACKAGE_STRING); + + if (NULL == (handle = dlopen(NULL, RTLD_LAZY | RTLD_NOLOAD))) + { + zabbix_log(LOG_LEVEL_ERR, "failed to dlopen() Zabbix binary: %s", dlerror()); + return ZBX_MODULE_FAIL; + } + + if (NULL == (zbx_snprintf = dlsym(handle, "zbx_snprintf")) && + NULL == (zbx_snprintf = dlsym(handle, "__zbx_zbx_snprintf"))) + { + zabbix_log(LOG_LEVEL_ERR, "failed to find zbx_snprintf() or __zbx_zbx_snprintf(): %s", dlerror()); + dlclose(handle); + return ZBX_MODULE_FAIL; + } + + dlclose(handle); + return init_config(); } diff --git a/src/libzbxpgsql.h b/src/libzbxpgsql.h index d1f67c3..26378d0 100644 --- a/src/libzbxpgsql.h +++ b/src/libzbxpgsql.h @@ -43,7 +43,11 @@ #define HAVE_TIME_H 1 #include #include +#define zbx_snprintf any_name_is_better /* don't want common.h to declare zbx_snprintf() function */ #include +#undef zbx_snprintf /* forget macro definition from old common.h and/or our own macro definition two lines above */ +#define zbx_snprintf pgsql_snprintf /* prevent symbol conflict with zbx_snprintf() function in new Zabbix binaries */ +extern size_t (*zbx_snprintf)(char *str, size_t count, const char *fmt, ...); /* use old name to avoid changing much of our code */ #include #include #include