rsyslog/backport-core-bugfix-correct-local-host-name-after-config-processing.patch

259 lines
8.8 KiB
Diff
Raw Normal View History

2022-12-27 16:24:40 +08:00
From ba00a9f25293f72137c9a85010276cca014ae7f0 Mon Sep 17 00:00:00 2001
From: Rainer Gerhards <rgerhards@adiscon.com>
Date: Wed, 31 Aug 2022 17:37:07 +0200
Subject: [PATCH] core bugfix: correct local host name after config processing
rsyslog.conf may affect the host's local name. These changes were
so far only activated after the first HUP. This patch now ensures
that the configured local host name is applied correctly throughout
all processing, including early startup.
This patch causes a slight change of behaviour. However, the behaviour
was inconsitent before. Now it is consistent and according to the config.
Please note: this patch also exposes a global entry point via "regular"
dynamic loading as this makes things much easier to do. This is in-line
with ongoing simplification effort.
Finally, we also remove a CI test that we do no longer need because
the problem covered is now addressed differently and the original issue
can no longer occur.
closes https://github.com/rsyslog/rsyslog/issues/4975
Conflict:NA
Reference:https://github.com/rsyslog/rsyslog/commit/ba00a9f25293f72137c9a85010276cca014ae7f0
---
runtime/glbl.c | 16 +++++++++++---
runtime/glbl.h | 3 ++-
runtime/rsyslog.h | 2 +-
tests/Makefile.am | 6 ------
tests/hostname-getaddrinfo-fail.sh | 34 ------------------------------
tools/iminternal.c | 7 +++++-
tools/rsyslogd.c | 10 ++-------
7 files changed, 24 insertions(+), 54 deletions(-)
delete mode 100755 tests/hostname-getaddrinfo-fail.sh
diff --git a/runtime/glbl.c b/runtime/glbl.c
index 52a598f..4feefc4 100644
--- a/runtime/glbl.c
+++ b/runtime/glbl.c
@@ -619,8 +619,8 @@ SetLocalHostName(uchar *const newname)
/* return our local hostname. if it is not set, "[localhost]" is returned
*/
-static uchar*
-GetLocalHostName(void)
+uchar*
+glblGetLocalHostName(void)
{
uchar *pszRet;
@@ -910,6 +910,8 @@ CODESTARTobjQueryInterface(glbl)
pIf->GetOption_DisallowWarning = getOption_DisallowWarning;
pIf->SetParseHOSTNAMEandTAG = setParseHOSTNAMEandTAG;
pIf->GetParseHOSTNAMEandTAG = getParseHOSTNAMEandTAG;
+ pIf->GetLocalHostName = glblGetLocalHostName;
+ pIf->SetLocalHostName = SetLocalHostName;
#define SIMP_PROP(name) \
pIf->Get##name = Get##name; \
pIf->Set##name = Set##name;
@@ -917,7 +919,6 @@ CODESTARTobjQueryInterface(glbl)
SIMP_PROP(DropMalPTRMsgs);
SIMP_PROP(mainqCnfObj);
SIMP_PROP(LocalFQDNName)
- SIMP_PROP(LocalHostName)
SIMP_PROP(LocalDomain)
SIMP_PROP(StripDomains)
SIMP_PROP(LocalHosts)
@@ -1541,6 +1542,15 @@ glblDoneLoadCnf(void)
stddbg = -1;
}
+ /* we have now read the config. We need to query the local host name now
+ * as it was set by the config.
+ *
+ * Note: early messages are already emited, and have "[localhost]" as
+ * hostname. These messages are currently in iminternal queue. Once they
+ * are taken from that queue, the hostname will be adapted.
+ */
+ queryLocalHostname();
+
finalize_it: RETiRet;
}
diff --git a/runtime/glbl.h b/runtime/glbl.h
index 9ccf7b6..4cb5770 100644
--- a/runtime/glbl.h
+++ b/runtime/glbl.h
@@ -8,7 +8,7 @@
* Please note that there currently is no glbl.c file as we do not yet
* have any implementations.
*
- * Copyright 2008-2019 Rainer Gerhards and Adiscon GmbH.
+ * Copyright 2008-2022 Rainer Gerhards and Adiscon GmbH.
*
* This file is part of the rsyslog runtime library.
*
@@ -162,5 +162,6 @@ const uchar* glblGetOperatingStateFile(void);
int glblGetOversizeMsgInputMode(void);
int glblReportOversizeMessage(void);
void glblReportChildProcessExit(const uchar *name, pid_t pid, int status);
+uchar *glblGetLocalHostName(void);
#endif /* #ifndef GLBL_H_INCLUDED */
diff --git a/runtime/rsyslog.h b/runtime/rsyslog.h
index 6492eea..58f8219 100644
--- a/runtime/rsyslog.h
+++ b/runtime/rsyslog.h
@@ -757,8 +757,8 @@ rsRetVal rsrtInit(const char **ppErrObj, obj_if_t *pObjIF);
rsRetVal rsrtExit(void);
int rsrtIsInit(void);
void rsrtSetErrLogger(void (*errLogger)(const int, const int, const uchar*));
-
void dfltErrLogger(const int, const int, const uchar *errMsg);
+rsRetVal queryLocalHostname(void);
/* this define below is (later) intended to be used to implement empty
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5e4f4fe..34b5b38 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -175,7 +175,6 @@ TESTS += \
timestamp-mysql.sh \
timestamp-pgsql.sh \
timestamp-subseconds.sh \
- hostname-getaddrinfo-fail.sh \
msleep_usage_output.sh \
mangle_qi_usage_output.sh \
minitcpsrv_usage_output.sh \
@@ -1608,10 +1607,6 @@ TESTS += \
endif
endif # ENABLE_OMAMQP1
-# test samples...
-#empty-hostname.log: hostname-getaddrinfo-fail.log
-#hostname-getaddrinfo-fail.log: empty-hostname.log
-
endif # if ENABLE_TESTBENCH
TESTS_ENVIRONMENT = RSYSLOG_MODDIR='$(abs_top_builddir)'/runtime/.libs/
@@ -1648,7 +1643,6 @@ EXTRA_DIST= \
config_enabled-off.sh \
empty-app-name.sh \
empty-hostname.sh \
- hostname-getaddrinfo-fail.sh \
hostname-with-slash-pmrfc5424.sh \
hostname-with-slash-pmrfc3164.sh \
pmrfc3164-msgFirstSpace.sh \
diff --git a/tests/hostname-getaddrinfo-fail.sh b/tests/hostname-getaddrinfo-fail.sh
deleted file mode 100755
index d14a1c3..0000000
--- a/tests/hostname-getaddrinfo-fail.sh
+++ /dev/null
@@ -1,34 +0,0 @@
-#!/bin/bash
-# This test check what happens if we cannot doe getaddrinfo early
-# in rsyslog startup (this has caused an error in the past). Even more
-# importantly, it checks that error messages can be issued very early
-# during startup.
-# Note that we use the override of the hostname to ensure we do not
-# accidentally get an acceptable FQDN-type hostname during testing.
-#
-# IMPORTANT: We cannot use the regular plumbing here, as our preload
-# interferes with socket operations (we cannot bind the port for some
-# reason). As we do not necessarily need the full plumbing for this
-# simple test, we emulate what we need. It's a bit ugly, but actually
-# the simplest way forward.
-#
-# This is part of the rsyslog testbench, licensed under ASL 2.0
-. ${srcdir:=.}/diag.sh init
-skip_platform "AIX" "we cannot preload required dummy lib"
-
-echo 'action(type="omfile" file="'$RSYSLOG_DYNNAME'.out.log")' > ${RSYSLOG_DYNNAME}.conf
-LD_PRELOAD=".libs/liboverride_gethostname_nonfqdn.so:.libs/liboverride_getaddrinfo.so" \
- ../tools/rsyslogd -C -n -i$RSYSLOG_DYNNAME.pid -M../runtime/.libs:../.libs -f${RSYSLOG_DYNNAME}.conf &
-wait_process_startup $RSYSLOG_DYNNAME
-sleep 1 # wait a bit so that rsyslog can do some processing...
-kill $(cat $RSYSLOG_DYNNAME.pid )
-
-grep " nonfqdn " < $RSYSLOG_DYNNAME.out.log
-if [ ! $? -eq 0 ]; then
- echo "expected hostname \"nonfqdn\" not found in logs, $RSYSLOG_DYNNAME.out.log is:"
- cat $RSYSLOG_DYNNAME.out.log
- error_exit 1
-fi;
-
-echo EVERYTHING OK - error messages are just as expected!
-exit_test
diff --git a/tools/iminternal.c b/tools/iminternal.c
index 52e9df8..c4dd548 100644
--- a/tools/iminternal.c
+++ b/tools/iminternal.c
@@ -6,7 +6,7 @@
*
* File begun on 2007-08-03 by RGerhards
*
- * Copyright 2007-2017 Rainer Gerhards and Adiscon GmbH.
+ * Copyright 2007-2022 Rainer Gerhards and Adiscon GmbH.
*
* This file is part of rsyslog.
*
@@ -37,6 +37,7 @@
#include "syslogd.h"
#include "linkedlist.h"
#include "iminternal.h"
+#include "unicode-helper.h"
static linkedList_t llMsgs;
static pthread_mutex_t mutList = PTHREAD_MUTEX_INITIALIZER;
@@ -137,6 +138,10 @@ rsRetVal iminternalRemoveMsg(smsg_t **ppMsg)
pthread_mutex_lock(&mutList);
CHKiRet(llGetNextElt(&llMsgs, &llCookie, (void*)&pThis));
+ if(!strcmp((char*)pThis->pMsg->pszHOSTNAME, "[localhost]")) {
+ /* early (pre-conf) startup message detected, need to set real hostname now */
+ MsgSetHOSTNAME(pThis->pMsg, glblGetLocalHostName(), ustrlen(glblGetLocalHostName()));
+ }
*ppMsg = pThis->pMsg;
pThis->pMsg = NULL; /* we do no longer own it - important for destructor */
diff --git a/tools/rsyslogd.c b/tools/rsyslogd.c
index 8410d44..9dedd2f 100644
--- a/tools/rsyslogd.c
+++ b/tools/rsyslogd.c
@@ -3,7 +3,7 @@
* because it was either written from scratch by me (rgerhards) or
* contributors who agreed to ASL 2.0.
*
- * Copyright 2004-2019 Rainer Gerhards and Adiscon
+ * Copyright 2004-2022 Rainer Gerhards and Adiscon
*
* This file is part of rsyslog.
*
@@ -231,7 +231,7 @@ setsid(void)
#endif
-static rsRetVal
+rsRetVal
queryLocalHostname(void)
{
uchar *LocalHostName = NULL;
@@ -1384,12 +1384,6 @@ initAll(int argc, char **argv)
exit(1); /* "good" exit, leaving at init for fatal error */
}
- /* get our host and domain names - we need to do this early as we may emit
- * error log messages, which need the correct hostname. -- rgerhards, 2008-04-04
- * But we need to have imInternal up first!
- */
- queryLocalHostname();
-
/* we now can emit error messages "the regular way" */
if(getenv("TZ") == NULL) {
--
2.27.0