From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7A975C5DF61 for ; Thu, 7 Nov 2019 13:39:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 447DD2075C for ; Thu, 7 Nov 2019 13:39:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="KMNY2d8a" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388886AbfKGNjT (ORCPT ); Thu, 7 Nov 2019 08:39:19 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:39921 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727858AbfKGNjS (ORCPT ); Thu, 7 Nov 2019 08:39:18 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573133956; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tq2JeUBVUQ/XsuBt3nC8IiPmPbwKvnLljLM+JcrAMJk=; b=KMNY2d8aZwBcqGuLQVwZMWK5L2y8WMKXk5SSK2uvfi/LLOa4rnDOSoH4nXz8nidHG6x1NW 03KKNd0OUA7hRM6bakJDUfBHtdZZONhMJQsoCKsAcAP096pXZLW9btoYn+Khzvp9vPwQNl 4lu/4KxhjDCIpCf6Z8VARoBwVOBzhMI= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-395-Xcs4GK4rNDixMXRgLGnU6Q-1; Thu, 07 Nov 2019 08:39:14 -0500 Received: by mail-wm1-f70.google.com with SMTP id i23so790591wmb.3 for ; Thu, 07 Nov 2019 05:39:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=R0mNyYZd6bScGehmpEaPxSSN+C3Fm91zz0KVrfqWw10=; b=hUd/UMFrLcLoyi0QEm2kC9gIgoCFp/osEvro8khSJi1GKRSl6KnN7wl3ZWsLfGVgz5 NDVS9gDKuQBoXbqsxQLNK2CL3XhXJWNYAprvjxdCuZ5GgBbm2KEUhzUg6NXuSGjBxXa8 KAX6KbVMEupBQsUHJPQsJ7YF5CjQeTA/10PS4wHqg36BuDKS7Nxc87mKO2ojCOsoYltP CR/ovq9vgpERTzVyp1rLpf+z93sGeWV4GT1Ln5F+GuOlgrYJJdkEUp1mRlIrJ/ukxnfU FHHVFhDMQTMtiOgiBRrDl0xFOWEw7l29CO988z4xH+zSL5s8UfXD8bJgd+vqkXLO0iH2 Cwhg== X-Gm-Message-State: APjAAAVNIEaf5oiUvE7LCdX3N01iUIcsWeZAGW7YnoMACruKUbv2rSFQ Tf9Zg2uJeMtYfJO9Yxpaajit3zz2mM0FxBnhiWZM/yQ6sx4sWiPunYwKE1ZFo5Q1YZxYLqHiHF4 h+lLkfg4FxEFwqbB5dt/pVyDi X-Received: by 2002:a05:6000:101:: with SMTP id o1mr3011985wrx.394.1573133953386; Thu, 07 Nov 2019 05:39:13 -0800 (PST) X-Google-Smtp-Source: APXvYqxlJH4U7SHbrC8hxVGAeT6H8Xo6YW+iuKy+hdOOvg2QwqKGRTGrF+FvXRmJs1SP18+ynLahVA== X-Received: by 2002:a05:6000:101:: with SMTP id o1mr3011954wrx.394.1573133953071; Thu, 07 Nov 2019 05:39:13 -0800 (PST) Received: from vitty.brq.redhat.com (nat-pool-brq-t.redhat.com. [213.175.37.10]) by smtp.gmail.com with ESMTPSA id c15sm1843353wmb.45.2019.11.07.05.39.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Nov 2019 05:39:12 -0800 (PST) From: Vitaly Kuznetsov To: Olaf Hering , "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , Sasha Levin , "open list\:Hyper-V CORE AND DRIVERS" , open list Cc: Olaf Hering Subject: Re: [PATCH v1] tools/hv: async name resolution in kvp_daemon In-Reply-To: <20191024144943.26199-1-olaf@aepfle.de> References: <20191024144943.26199-1-olaf@aepfle.de> Date: Thu, 07 Nov 2019 14:39:11 +0100 Message-ID: <874kzfbybk.fsf@vitty.brq.redhat.com> MIME-Version: 1.0 X-MC-Unique: Xcs4GK4rNDixMXRgLGnU6Q-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Olaf Hering writes: > The hostname is resolved just once since commit 58125210ab3b ("Tools: > hv: cache FQDN in kvp_daemon to avoid timeouts") to make sure the VM > responds within the timeout limits to requests from the host. > > If for some reason getaddrinfo fails, the string returned by the > "FullyQualifiedDomainName" request contains some error string, which is > then used by tools on the host side. > > Adjust the code to resolve the current hostname in a separate thread. > This thread loops until getaddrinfo returns success. During this time > all "FullyQualifiedDomainName" requests will be answered with an empty > string. > > Signed-off-by: Olaf Hering > --- > tools/hv/Makefile | 2 ++ > tools/hv/hv_kvp_daemon.c | 69 ++++++++++++++++++++++++++++++++----------= ------ > 2 files changed, 48 insertions(+), 23 deletions(-) > > diff --git a/tools/hv/Makefile b/tools/hv/Makefile > index b57143d9459c..3b5481015a84 100644 > --- a/tools/hv/Makefile > +++ b/tools/hv/Makefile > @@ -22,6 +22,8 @@ ALL_PROGRAMS :=3D $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS= )) > =20 > ALL_SCRIPTS :=3D hv_get_dhcp_info.sh hv_get_dns_info.sh hv_set_ifconfig.= sh > =20 > +$(OUTPUT)hv_kvp_daemon: LDFLAGS +=3D -lpthread > + > all: $(ALL_PROGRAMS) > =20 > export srctree OUTPUT CC LD CFLAGS > diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c > index e9ef4ca6a655..22cf1c4dbf5c 100644 > --- a/tools/hv/hv_kvp_daemon.c > +++ b/tools/hv/hv_kvp_daemon.c > @@ -41,6 +41,7 @@ > #include > #include > #include > +#include > =20 > /* > * KVP protocol: The user mode component first registers with the > @@ -85,7 +86,7 @@ static char *processor_arch; > static char *os_build; > static char *os_version; > static char *lic_version =3D "Unknown version"; > -static char full_domain_name[HV_KVP_EXCHANGE_MAX_VALUE_SIZE]; > +static char *full_domain_name; > static struct utsname uts_buf; > =20 > /* > @@ -1327,27 +1328,53 @@ static int kvp_set_ip_info(char *if_name, struct = hv_kvp_ipaddr_value *new_val) > =09return error; > } > =20 > - > -static void > -kvp_get_domain_name(char *buffer, int length) > +/* > + * Async retrival of Fully Qualified Domain Name because getaddrinfo tak= es an > + * unpredictable amount of time to finish. > + */ > +static void *kvp_getaddrinfo(void *p) > { > -=09struct addrinfo=09hints, *info ; > -=09int error =3D 0; > +=09char *tmp, **str_ptr =3D (char **)p; > +=09char hostname[HOST_NAME_MAX + 1]; > +=09struct addrinfo=09*info, hints =3D { > +=09=09.ai_family =3D AF_INET, /* Get only ipv4 addrinfo. */ > +=09=09.ai_socktype =3D SOCK_STREAM, > +=09=09.ai_flags =3D AI_CANONNAME, > +=09}; > +=09int ret; > + > +=09if (gethostname(hostname, sizeof(hostname) - 1) < 0) > +=09=09goto out; > + > +=09do { > +=09=09ret =3D getaddrinfo(hostname, NULL, &hints, &info); > +=09=09if (ret) > +=09=09=09sleep(1); Is it only EAI_AGAIN or do you see any other return values which justify the retry? I'm afraid that in case of a e.g. non-existing hostname we'll be infinitely looping with EAI_FAIL. > +=09} while (ret); > + > +=09ret =3D asprintf(&tmp, "%s", info->ai_canonname); > +=09freeaddrinfo(info); > +=09if (ret <=3D 0) > +=09=09goto out; > + > +=09if (ret > HV_KVP_EXCHANGE_MAX_VALUE_SIZE) > +=09=09tmp[HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1] =3D '\0'; > +=09*str_ptr =3D tmp; > =20 > -=09gethostname(buffer, length); > -=09memset(&hints, 0, sizeof(hints)); > -=09hints.ai_family =3D AF_INET; /*Get only ipv4 addrinfo. */ > -=09hints.ai_socktype =3D SOCK_STREAM; > -=09hints.ai_flags =3D AI_CANONNAME; > +out: > +=09pthread_exit(NULL); > +} > + > +static void kvp_obtain_domain_name(char **str_ptr) > +{ > +=09pthread_t t; > =20 > -=09error =3D getaddrinfo(buffer, NULL, &hints, &info); > -=09if (error !=3D 0) { > -=09=09snprintf(buffer, length, "getaddrinfo failed: 0x%x %s", > -=09=09=09error, gai_strerror(error)); > +=09if (pthread_create(&t, NULL, kvp_getaddrinfo, str_ptr)) { > +=09=09syslog(LOG_ERR, "pthread_create failed; error: %d %s", > +=09=09=09errno, strerror(errno)); > =09=09return; > =09} > -=09snprintf(buffer, length, "%s", info->ai_canonname); > -=09freeaddrinfo(info); > +=09pthread_detach(t); I think this should be complemented with pthread_cancel/pthread_join before exiting main(). > } > =20 > void print_usage(char *argv[]) > @@ -1412,11 +1439,7 @@ int main(int argc, char *argv[]) > =09 * Retrieve OS release information. > =09 */ > =09kvp_get_os_info(); > -=09/* > -=09 * Cache Fully Qualified Domain Name because getaddrinfo takes an > -=09 * unpredictable amount of time to finish. > -=09 */ > -=09kvp_get_domain_name(full_domain_name, sizeof(full_domain_name)); > +=09kvp_obtain_domain_name(&full_domain_name); > =20 > =09if (kvp_file_init()) { > =09=09syslog(LOG_ERR, "Failed to initialize the pools"); > @@ -1571,7 +1594,7 @@ int main(int argc, char *argv[]) > =20 > =09=09switch (hv_msg->body.kvp_enum_data.index) { > =09=09case FullyQualifiedDomainName: > -=09=09=09strcpy(key_value, full_domain_name); > +=09=09=09strcpy(key_value, full_domain_name ? : ""); > =09=09=09strcpy(key_name, "FullyQualifiedDomainName"); > =09=09=09break; > =09=09case IntegrationServicesVersion: --=20 Vitaly