Skip to content

Commit

Permalink
Replace zbx_snprintf() function/macro with function pointer which is …
Browse files Browse the repository at this point in the history
…initialized in runtime
  • Loading branch information
i-ky committed Apr 23, 2019
1 parent 671fd21 commit e74a98a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/libzbxpgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

#include "libzbxpgsql.h"

#include <dlfcn.h>

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 */
Expand Down Expand Up @@ -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();
}

Expand Down
4 changes: 4 additions & 0 deletions src/libzbxpgsql.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
#define HAVE_TIME_H 1
#include <sysinc.h>
#include <module.h>
#define zbx_snprintf any_name_is_better /* don't want common.h to declare zbx_snprintf() function */
#include <common.h>
#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 <log.h>
#include <zbxjson.h>
#include <version.h>
Expand Down

0 comments on commit e74a98a

Please sign in to comment.