From nobody Sat May 4 12:20:57 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1518431369658906.744552291292; Mon, 12 Feb 2018 02:29:29 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 002A3369B7; Mon, 12 Feb 2018 10:29:27 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6C5EB669E3; Mon, 12 Feb 2018 10:29:27 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 2EE8018033E1; Mon, 12 Feb 2018 10:29:26 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w1CATOm2020178 for ; Mon, 12 Feb 2018 05:29:24 -0500 Received: by smtp.corp.redhat.com (Postfix) id 2D00A2166BB3; Mon, 12 Feb 2018 10:29:24 +0000 (UTC) Received: from localhost.localdomain (ovpn-204-25.brq.redhat.com [10.40.204.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8E9692166BAE; Mon, 12 Feb 2018 10:29:23 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Mon, 12 Feb 2018 11:29:21 +0100 Message-Id: <487e85a9c8af6c91bcc741bda7f0f69a637a504c.1518431361.git.mprivozn@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH] virutil: Introduce virGetHostnameSimple() X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Mon, 12 Feb 2018 10:29:28 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" After 759b4d1b0fe5f we are getting hostname in virLogOnceInit(). Problem with this approach is in the NSS module because the module calls some internal APIs which occasionally want to log something. This results in virLogInitialize() to be called which in turn ends up calling virGetHostnameQuiet() and effectively the control gets to NSS plugin again which calls some internal APIs which occasionally want to log something. You can see the deadlock now. One way out of this is to call only gethostname() and not whole virGetHostnameQuiet() machinery. Signed-off-by: Michal Privoznik --- src/libvirt_private.syms | 1 + src/util/virlog.c | 2 +- src/util/virutil.c | 46 ++++++++++++++++++++++++++++++++++----------= -- src/util/virutil.h | 1 + 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 3b14d7d15..3ef55f809 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3016,6 +3016,7 @@ virGetGroupList; virGetGroupName; virGetHostname; virGetHostnameQuiet; +virGetHostnameSimple; virGetListenFDs; virGetSelfLastChanged; virGetSystemPageSize; diff --git a/src/util/virlog.c b/src/util/virlog.c index 8f1e4800d..fc854ffeb 100644 --- a/src/util/virlog.c +++ b/src/util/virlog.c @@ -276,7 +276,7 @@ virLogOnceInit(void) * 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 =3D virGetHostnameQuiet(); + virLogHostname =3D virGetHostnameSimple(); =20 virLogUnlock(); return 0; diff --git a/src/util/virutil.c b/src/util/virutil.c index cd6fbf2f3..22adecd53 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -684,26 +684,23 @@ static char * virGetHostnameImpl(bool quiet) { int r; - char hostname[HOST_NAME_MAX+1], *result =3D NULL; + char *result; struct addrinfo hints, *info; =20 - r =3D gethostname(hostname, sizeof(hostname)); - if (r =3D=3D -1) { + if (!(result =3D virGetHostnameSimple())) { if (!quiet) virReportSystemError(errno, "%s", _("failed to determine host name")); return NULL; } - NUL_TERMINATE(hostname); =20 - if (STRPREFIX(hostname, "localhost") || strchr(hostname, '.')) { + if (STRPREFIX(result, "localhost") || strchr(result, '.')) { /* in this case, gethostname returned localhost (meaning we can't * do any further canonicalization), or it returned an FQDN (and * we don't need to do any further canonicalization). Return the * string as-is; it's up to callers to check whether "localhost" * is allowed. */ - ignore_value(VIR_STRDUP_QUIET(result, hostname)); goto cleanup; } =20 @@ -714,12 +711,11 @@ virGetHostnameImpl(bool quiet) memset(&hints, 0, sizeof(hints)); hints.ai_flags =3D AI_CANONNAME|AI_CANONIDN; hints.ai_family =3D AF_UNSPEC; - r =3D getaddrinfo(hostname, NULL, &hints, &info); + r =3D getaddrinfo(result, NULL, &hints, &info); if (r !=3D 0) { if (!quiet) VIR_WARN("getaddrinfo failed for '%s': %s", - hostname, gai_strerror(r)); - ignore_value(VIR_STRDUP_QUIET(result, hostname)); + result, gai_strerror(r)); goto cleanup; } =20 @@ -727,15 +723,16 @@ virGetHostnameImpl(bool quiet) sa_assert(info); =20 if (info->ai_canonname =3D=3D NULL || - STRPREFIX(info->ai_canonname, "localhost")) + STRPREFIX(info->ai_canonname, "localhost")) { /* in this case, we tried to canonicalize and we ended up back with * localhost. Ignore the canonicalized name and just return the * original hostname */ - ignore_value(VIR_STRDUP_QUIET(result, hostname)); - else + } else { /* Caller frees this string. */ + VIR_FREE(result); ignore_value(VIR_STRDUP_QUIET(result, info->ai_canonname)); + } =20 freeaddrinfo(info); =20 @@ -760,6 +757,31 @@ virGetHostnameQuiet(void) } =20 =20 +/** + * virGetHostnameSimple: + * + * Plain wrapper over gethostname(). The difference to + * virGetHostname() is that this function doesn't try to + * canonicalize the hostname. + * + * Returns: hostname string (caller must free), + * NULL on error. + */ +char * +virGetHostnameSimple(void) +{ + char hostname[HOST_NAME_MAX+1]; + char *ret; + + if (gethostname(hostname, sizeof(hostname)) =3D=3D -1) + return NULL; + + NUL_TERMINATE(hostname); + ignore_value(VIR_STRDUP_QUIET(ret, hostname)); + return ret; +} + + char * virGetUserDirectory(void) { diff --git a/src/util/virutil.h b/src/util/virutil.h index be0f6b0ea..57148374b 100644 --- a/src/util/virutil.h +++ b/src/util/virutil.h @@ -136,6 +136,7 @@ static inline int pthread_sigmask(int how, =20 char *virGetHostname(void); char *virGetHostnameQuiet(void); +char *virGetHostnameSimple(void); =20 char *virGetUserDirectory(void); char *virGetUserDirectoryByUID(uid_t uid); --=20 2.13.6 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list