diff --git a/cfg.mk b/cfg.mk index 56cb14bd94..a4131592c2 100644 --- a/cfg.mk +++ b/cfg.mk @@ -1158,7 +1158,7 @@ _src2=src/(util/vircommand|libvirt|lxc/lxc_controller|locking/lock_daemon|loggin exclude_file_name_regexp--sc_prohibit_fork_wrappers = \ (^($(_src2)|tests/testutils|daemon/libvirtd)\.c$$) -exclude_file_name_regexp--sc_prohibit_gethostname = ^src/util/virutil\.c$$ +exclude_file_name_regexp--sc_prohibit_gethostname = ^src/util/vir(util|log)\.c$$ exclude_file_name_regexp--sc_prohibit_internal_functions = \ ^src/(util/(viralloc|virutil|virfile)\.[hc]|esx/esx_vi\.c)$$ diff --git a/src/util/virlog.c b/src/util/virlog.c index 056b53cda2..f76fc2cafe 100644 --- a/src/util/virlog.c +++ b/src/util/virlog.c @@ -64,7 +64,7 @@ VIR_LOG_INIT("util.log"); static regex_t *virLogRegex; -static char *virLogHostname; +static char virLogHostname[HOST_NAME_MAX+1]; #define VIR_LOG_DATE_REGEX "[0-9]{4}-[0-9]{2}-[0-9]{2}" @@ -261,6 +261,8 @@ virLogPriorityString(virLogPriority lvl) static int virLogOnceInit(void) { + int r; + if (virMutexInit(&virLogMutex) < 0) return -1; @@ -275,8 +277,17 @@ virLogOnceInit(void) /* We get and remember the hostname early, because at later time * it might not be possible to load NSS modules via getaddrinfo() * (e.g. at container startup the host filesystem will not be - * accessible anymore. */ - virLogHostname = virGetHostnameQuiet(); + * accessible anymore. + * Must not use virGetHostname though as that causes re-entrancy + * problems if it triggers logging codepaths + */ + r = gethostname(virLogHostname, sizeof(virLogHostname)); + if (r == -1) { + ignore_value(virStrcpy(virLogHostname, + "(unknown)", sizeof(virLogHostname))); + } else { + NUL_TERMINATE(virLogHostname); + } virLogUnlock(); return 0; @@ -475,9 +486,6 @@ virLogHostnameString(char **rawmsg, { char *hoststr; - if (!virLogHostname) - return -1; - if (virAsprintfQuiet(&hoststr, "hostname: %s", virLogHostname) < 0) return -1;