diff --git a/runtime/net.c b/runtime/net.c index a47ec25cc..f4675b548 100644 --- a/runtime/net.c +++ b/runtime/net.c @@ -1150,8 +1150,16 @@ getLocalHostname(uchar **ppName) if(gethostname(hnbuf, sizeof(hnbuf)) != 0) { strcpy(hnbuf, "localhost"); } else { - hnbuf[sizeof(hnbuf)-1] = '\0'; /* be on the safe side... */ + /* now guard against empty hostname + * see https://github.com/rsyslog/rsyslog/issues/1040 + */ + if(hnbuf[0] == '\0') { + strcpy(hnbuf, "localhost"); + } else { + hnbuf[sizeof(hnbuf)-1] = '\0'; /* be on the safe side... */ + } } + char *dot = strstr(hnbuf, "."); if(dot == NULL) { /* we need to (try) to find the real name via resolver */ diff --git a/tests/Makefile.am b/tests/Makefile.am index 16ac33aa1..75def2246 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -4,6 +4,14 @@ CLEANFILES = \ # IN_AUTO_DEBUG should be deleted each time make check is run, but # there exists no such hook. Se we at least delete it on make clean. + +pkglib_LTLIBRARIES = + +pkglib_LTLIBRARIES += liboverride_gethostname.la +liboverride_gethostname_la_SOURCES = override_gethostname.c +liboverride_gethostname_la_CFLAGS = +liboverride_gethostname_la_LDFLAGS = -avoid-version -shared + # TODO: reenable TESTRUNS = rt_init rscript check_PROGRAMS = $(TESTRUNS) ourtail nettester tcpflood chkseq msleep randomgen \ diagtalker uxsockrcvr syslog_caller inputfilegen minitcpsrv \ @@ -13,6 +21,7 @@ TESTS = $(TESTRUNS) #TESTS = $(TESTRUNS) cfg.sh TESTS += \ + empty-hostname.sh \ hostname-with-slash-pmrfc5424.sh \ hostname-with-slash-pmrfc3164.sh \ hostname-with-slash-dflt-invld.sh \ @@ -529,6 +538,7 @@ DISTCLEANFILES=rsyslog.pid test_files = testbench.h runtime-dummy.c EXTRA_DIST= \ + empty-hostname.sh \ hostname-with-slash-pmrfc5424.sh \ hostname-with-slash-pmrfc3164.sh \ hostname-with-slash-dflt-invld.sh \ diff --git a/tests/diag.sh b/tests/diag.sh index d08b00d88..f60549e6d 100755 --- a/tests/diag.sh +++ b/tests/diag.sh @@ -145,7 +145,7 @@ case $1 in echo "ERROR: config file '$CONF_FILE' not found!" exit 1 fi - $valgrind ../tools/rsyslogd -C -n -irsyslog$3.pid -M../runtime/.libs:../.libs -f$CONF_FILE & + LD_PRELOAD=$RSYSLOG_PRELOAD $valgrind ../tools/rsyslogd -C -n -irsyslog$3.pid -M../runtime/.libs:../.libs -f$CONF_FILE & . $srcdir/diag.sh wait-startup $3 ;; 'startup-silent') # start rsyslogd with default params. $2 is the config file name to use diff --git a/tests/empty-hostname.sh b/tests/empty-hostname.sh new file mode 100755 index 000000000..10f2d40b2 --- /dev/null +++ b/tests/empty-hostname.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# This is part of the rsyslog testbench, licensed under ASL 2.0 +echo ====================================================================== +. $srcdir/diag.sh init +. $srcdir/diag.sh generate-conf +. $srcdir/diag.sh add-conf ' +action(type="omfile" file="rsyslog.out.log") +' +export RSYSLOG_PRELOAD=.libs/liboverride_gethostname.so +. $srcdir/diag.sh startup +. $srcdir/diag.sh shutdown-when-empty # shut down rsyslogd when done processing messages +. $srcdir/diag.sh wait-shutdown # we need to wait until rsyslogd is finished! + +grep " localhost " < rsyslog.out.log +if [ ! $? -eq 0 ]; then + echo "expected hostname \"localhost\" not found in logs, rsyslog.out.log is:" + cat rsyslog.out.log + . $srcdir/diag.sh error-exit 1 +fi; + +. $srcdir/diag.sh exit diff --git a/tests/faketime_common.sh b/tests/faketime_common.sh index 00d54c293..d27332506 100644 --- a/tests/faketime_common.sh +++ b/tests/faketime_common.sh @@ -36,11 +36,11 @@ rsyslog_testbench_preload_libfaketime() { exit 77 else echo "Test passed! Will use '${RSYSLOG_LIBFAKETIME}' library!" - export LD_PRELOAD="${RSYSLOG_LIBFAKETIME}" + export RSYSLOG_PRELOAD="${RSYSLOG_LIBFAKETIME}" fi # GMT-1 (POSIX TIME) is GMT+1 in "Human Time" - faketime_testtime=$(FAKETIME="2040-01-01 16:00:00" TZ=GMT-1 date +%s 2>/dev/null) + faketime_testtime=$(LD_PRELOAD="${RSYSLOG_LIBFAKETIME}" FAKETIME="2040-01-01 16:00:00" TZ=GMT-1 date +%s 2>/dev/null) if [ ${faketime_testtime} -eq -1 ]; then echo "Note: System is not year-2038 compliant" RSYSLOG_TESTBENCH_Y2K38_INCOMPATIBLE="yes" diff --git a/tests/override_gethostname.c b/tests/override_gethostname.c new file mode 100644 index 000000000..561381130 --- /dev/null +++ b/tests/override_gethostname.c @@ -0,0 +1,22 @@ +// we need this for dlsym(): #include +#include + + +int gethostname(char *name, size_t __attribute__((unused)) len) +{ + *name = '\0'; + return 0; +} + +static void __attribute__((constructor)) +my_init(void) +{ + /* we currently do not need this entry point, but keep it as + * a "template". It can be used, e.g. to emit some diagnostic + * information: + printf("loaded\n"); + * or - more importantly - obtain a pointer to the overriden + * API: + orig_etry = dlsym(RTLD_NEXT, "original_entry_point"); + */ +}