From 12efeb634552cc993f9bacb8174b34214f777283 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Mon, 29 Mar 2021 00:03:14 +0200 Subject: blogc: sysinfo: get fqdn if available --- Makefile.am | 6 ++++++ configure.ac | 3 ++- src/blogc/sysinfo.c | 10 ++++++++++ tests/blogc/check_sysinfo.c | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/Makefile.am b/Makefile.am index 9471577..571cb41 100644 --- a/Makefile.am +++ b/Makefile.am @@ -558,6 +558,12 @@ tests_blogc_check_sysinfo_LDFLAGS = \ -Wl,--wrap=getenv \ $(NULL) +if HAVE_NETDB_H +tests_blogc_check_sysinfo_LDFLAGS += \ + -Wl,--wrap=gethostbyname \ + $(NULL) +endif + if HAVE_UNISTD_H tests_blogc_check_sysinfo_LDFLAGS += \ -Wl,--wrap=gethostname \ diff --git a/configure.ac b/configure.ac index 880acf4..3e98afc 100644 --- a/configure.ac +++ b/configure.ac @@ -219,9 +219,10 @@ AM_CONDITIONAL([USE_BGR_DEPS], [test "x$have_bgr_deps" = "xyes"]) BASH="$ac_cv_path_bash" AC_SUBST(BASH) -AC_CHECK_HEADERS([sys/resource.h sys/stat.h sys/time.h sys/wait.h time.h unistd.h sysexits.h]) +AC_CHECK_HEADERS([netdb.h sys/resource.h sys/stat.h sys/time.h sys/wait.h time.h unistd.h sysexits.h]) AC_CHECK_FUNCS([gethostname]) +AM_CONDITIONAL([HAVE_NETDB_H], [test "x$ac_cv_header_netdb_h" = "xyes"]) AM_CONDITIONAL([HAVE_TIME_H], [test "x$ac_cv_header_time_h" = "xyes"]) AM_CONDITIONAL([HAVE_UNISTD_H], [test "x$ac_cv_header_unistd_h" = "xyes"]) AM_CONDITIONAL([HAVE_SYS_RESOURCE_H], [test "x$ac_cv_header_sys_resource_h" = "xyes"]) diff --git a/src/blogc/sysinfo.c b/src/blogc/sysinfo.c index cdb860e..3c30996 100644 --- a/src/blogc/sysinfo.c +++ b/src/blogc/sysinfo.c @@ -18,6 +18,10 @@ #include #endif /* HAVE_TIME_H */ +#ifdef HAVE_NETDB_H +#include +#endif + #include #include #include @@ -38,6 +42,12 @@ blogc_sysinfo_get_hostname(void) if (-1 == gethostname(buf, 1024)) return NULL; +#ifdef HAVE_NETDB_H + struct hostent *h = gethostbyname(buf); + if (h != NULL && h->h_name != NULL) + return bc_strdup(h->h_name); +#endif + // FIXME: return FQDN instead of local host name return bc_strdup(buf); #endif diff --git a/tests/blogc/check_sysinfo.c b/tests/blogc/check_sysinfo.c index 3951bbf..197092c 100644 --- a/tests/blogc/check_sysinfo.c +++ b/tests/blogc/check_sysinfo.c @@ -21,6 +21,23 @@ #include #endif +#ifdef HAVE_NETDB_H +#include + +static struct hostent h = { + .h_name = "bola.example.com", +}; + +struct hostent* +__wrap_gethostbyname(const char *name) +{ + if (0 == strcmp(name, "bola")) + return &h; + return NULL; +} +#endif + + #ifdef HAVE_SYSINFO_HOSTNAME int __wrap_gethostname(char *name, size_t len) @@ -95,7 +112,14 @@ test_sysinfo_get_hostname(void **state) will_return(__wrap_gethostname, 0); f = blogc_sysinfo_get_hostname(); assert_non_null(f); - assert_string_equal(f, "bola"); + assert_string_equal(f, "bola.example.com"); + free(f); + + will_return(__wrap_gethostname, "guda"); + will_return(__wrap_gethostname, 0); + f = blogc_sysinfo_get_hostname(); + assert_non_null(f); + assert_string_equal(f, "guda"); free(f); } @@ -114,7 +138,15 @@ test_sysinfo_inject_hostname(void **state) will_return(__wrap_gethostname, 0); blogc_sysinfo_inject_hostname(t); assert_int_equal(bc_trie_size(t), 1); - assert_string_equal(bc_trie_lookup(t, "BLOGC_SYSINFO_HOSTNAME"), "bola"); + assert_string_equal(bc_trie_lookup(t, "BLOGC_SYSINFO_HOSTNAME"), "bola.example.com"); + bc_trie_free(t); + + t = bc_trie_new(free); + will_return(__wrap_gethostname, "guda"); + will_return(__wrap_gethostname, 0); + blogc_sysinfo_inject_hostname(t); + assert_int_equal(bc_trie_size(t), 1); + assert_string_equal(bc_trie_lookup(t, "BLOGC_SYSINFO_HOSTNAME"), "guda"); bc_trie_free(t); } -- cgit v1.2.3-18-g5258