diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2021-03-29 00:03:14 +0200 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2021-03-29 00:47:41 +0200 |
commit | 12efeb634552cc993f9bacb8174b34214f777283 (patch) | |
tree | eeee97e1fa5c0383a4d01ed89c0a2a0dffd3dc76 | |
parent | d2e1bb55bcc38d6f118654fa38d6396e2f980721 (diff) | |
download | blogc-12efeb634552cc993f9bacb8174b34214f777283.tar.gz blogc-12efeb634552cc993f9bacb8174b34214f777283.tar.bz2 blogc-12efeb634552cc993f9bacb8174b34214f777283.zip |
blogc: sysinfo: get fqdn if available
-rw-r--r-- | Makefile.am | 6 | ||||
-rw-r--r-- | configure.ac | 3 | ||||
-rw-r--r-- | src/blogc/sysinfo.c | 10 | ||||
-rw-r--r-- | 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 <time.h> #endif /* HAVE_TIME_H */ +#ifdef HAVE_NETDB_H +#include <netdb.h> +#endif + #include <stdbool.h> #include <stdlib.h> #include <string.h> @@ -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 <time.h> #endif +#ifdef HAVE_NETDB_H +#include <netdb.h> + +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); } |