All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] qga: Use gethostname() instead of g_get_host_name()
@ 2020-06-16  8:34 Michal Privoznik
  2020-06-16  8:43 ` Marc-André Lureau
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Michal Privoznik @ 2020-06-16  8:34 UTC (permalink / raw)
  To: qemu-devel; +Cc: vfeenstr, mdroth

Problem with g_get_host_name() is that on the first call it saves
the hostname into a global variable and from then on, every
subsequent call returns the saved hostname. Even if the hostname
changes. This doesn't play nicely with guest agent, because if
the hostname is acquired before the guest is set up (e.g. on the
first boot, or before DHCP) we will report old, invalid hostname.

Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1845127

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 qga/commands.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)

diff --git a/qga/commands.c b/qga/commands.c
index efc8b90281..ce3c2041a6 100644
--- a/qga/commands.c
+++ b/qga/commands.c
@@ -512,14 +512,62 @@ int ga_parse_whence(GuestFileWhence *whence, Error **errp)
     return -1;
 }
 
+#ifndef HOST_NAME_MAX
+# ifdef _POSIX_HOST_NAME_MAX
+#  define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
+# else
+#  define HOST_NAME_MAX 255
+# endif
+#endif
+
 GuestHostName *qmp_guest_get_host_name(Error **errp)
 {
     GuestHostName *result = NULL;
-    gchar const *hostname = g_get_host_name();
-    if (hostname != NULL) {
-        result = g_new0(GuestHostName, 1);
-        result->host_name = g_strdup(hostname);
+    g_autofree char *hostname = NULL;
+
+    /*
+     * We want to avoid using g_get_host_name() because that
+     * caches the result and we wouldn't reflect changes in the
+     * host name.
+     */
+
+#ifndef G_OS_WIN32
+    long len = -1;
+
+#ifdef _SC_HOST_NAME_MAX
+    len = sysconf(_SC_HOST_NAME_MAX);
+#endif /* _SC_HOST_NAME_MAX */
+
+    if (len < 0) {
+        len = HOST_NAME_MAX;
     }
+
+    hostname = g_malloc0(len + 1);
+
+    if (gethostname(hostname, len) < 0) {
+        return NULL;
+    }
+
+#else /* G_OS_WIN32 */
+
+    wchar_t tmp[MAX_COMPUTERNAME_LENGTH + 1];
+    DWORD size = G_N_ELEMENTS(tmp);
+
+    if (GetComputerNameW(tmp, &size) != 0) {
+        /*
+         * Indeed, on Windows retval of zero means failure
+         * and nonzero means success.
+         */
+        hostname = g_utf16_to_utf8(tmp, size, NULL, NULL, NULL);
+    }
+#endif /* G_OS_WIN32 */
+
+    if (!hostname) {
+        hostname = g_strdup("localhost");
+    }
+
+    result = g_new0(GuestHostName, 1);
+    result->host_name = g_steal_pointer(&hostname);
     return result;
 }
 
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] qga: Use gethostname() instead of g_get_host_name()
  2020-06-16  8:34 [PATCH] qga: Use gethostname() instead of g_get_host_name() Michal Privoznik
@ 2020-06-16  8:43 ` Marc-André Lureau
  2020-06-19 21:54 ` Richard Henderson
  2020-06-22 10:14 ` Philippe Mathieu-Daudé
  2 siblings, 0 replies; 6+ messages in thread
From: Marc-André Lureau @ 2020-06-16  8:43 UTC (permalink / raw)
  To: Michal Privoznik
  Cc: Michael Roth, QEMU, Vinzenz 'evilissimo' Feenstra

[-- Attachment #1: Type: text/plain, Size: 2916 bytes --]

Hi

On Tue, Jun 16, 2020 at 12:35 PM Michal Privoznik <mprivozn@redhat.com>
wrote:

> Problem with g_get_host_name() is that on the first call it saves
> the hostname into a global variable and from then on, every
> subsequent call returns the saved hostname. Even if the hostname
> changes. This doesn't play nicely with guest agent, because if
> the hostname is acquired before the guest is set up (e.g. on the
> first boot, or before DHCP) we will report old, invalid hostname.
>
> Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1845127


lgtm,
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

(fwiw, I opened a glib issue:
https://gitlab.gnome.org/GNOME/glib/-/issues/2130)


>
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  qga/commands.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 52 insertions(+), 4 deletions(-)
>
> diff --git a/qga/commands.c b/qga/commands.c
> index efc8b90281..ce3c2041a6 100644
> --- a/qga/commands.c
> +++ b/qga/commands.c
> @@ -512,14 +512,62 @@ int ga_parse_whence(GuestFileWhence *whence, Error
> **errp)
>      return -1;
>  }
>
> +#ifndef HOST_NAME_MAX
> +# ifdef _POSIX_HOST_NAME_MAX
> +#  define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
> +# else
> +#  define HOST_NAME_MAX 255
> +# endif
> +#endif
> +
>  GuestHostName *qmp_guest_get_host_name(Error **errp)
>  {
>      GuestHostName *result = NULL;
> -    gchar const *hostname = g_get_host_name();
> -    if (hostname != NULL) {
> -        result = g_new0(GuestHostName, 1);
> -        result->host_name = g_strdup(hostname);
> +    g_autofree char *hostname = NULL;
> +
> +    /*
> +     * We want to avoid using g_get_host_name() because that
> +     * caches the result and we wouldn't reflect changes in the
> +     * host name.
> +     */
> +
> +#ifndef G_OS_WIN32
> +    long len = -1;
> +
> +#ifdef _SC_HOST_NAME_MAX
> +    len = sysconf(_SC_HOST_NAME_MAX);
> +#endif /* _SC_HOST_NAME_MAX */
> +
> +    if (len < 0) {
> +        len = HOST_NAME_MAX;
>      }
> +
> +    hostname = g_malloc0(len + 1);
> +
> +    if (gethostname(hostname, len) < 0) {
> +        return NULL;
> +    }
> +
> +#else /* G_OS_WIN32 */
> +
> +    wchar_t tmp[MAX_COMPUTERNAME_LENGTH + 1];
> +    DWORD size = G_N_ELEMENTS(tmp);
> +
> +    if (GetComputerNameW(tmp, &size) != 0) {
> +        /*
> +         * Indeed, on Windows retval of zero means failure
> +         * and nonzero means success.
> +         */
> +        hostname = g_utf16_to_utf8(tmp, size, NULL, NULL, NULL);
> +    }
> +#endif /* G_OS_WIN32 */
> +
> +    if (!hostname) {
> +        hostname = g_strdup("localhost");
> +    }
> +
> +    result = g_new0(GuestHostName, 1);
> +    result->host_name = g_steal_pointer(&hostname);
>      return result;
>  }
>
> --
> 2.26.2
>
>
>

-- 
Marc-André Lureau

[-- Attachment #2: Type: text/html, Size: 4088 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] qga: Use gethostname() instead of g_get_host_name()
  2020-06-16  8:34 [PATCH] qga: Use gethostname() instead of g_get_host_name() Michal Privoznik
  2020-06-16  8:43 ` Marc-André Lureau
@ 2020-06-19 21:54 ` Richard Henderson
  2020-06-22 10:06   ` Michal Privoznik
  2020-06-22 10:14 ` Philippe Mathieu-Daudé
  2 siblings, 1 reply; 6+ messages in thread
From: Richard Henderson @ 2020-06-19 21:54 UTC (permalink / raw)
  To: Michal Privoznik, qemu-devel; +Cc: vfeenstr, mdroth

On 6/16/20 1:34 AM, Michal Privoznik wrote:
> +#ifndef G_OS_WIN32

Nit: positive tests are easier to reason with and extend than negative tests.
I would reverse these two blocks and use a positive test for windows.

Also, CONFIG_WIN32 is what we use elsewhere for this test.


r~


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] qga: Use gethostname() instead of g_get_host_name()
  2020-06-19 21:54 ` Richard Henderson
@ 2020-06-22 10:06   ` Michal Privoznik
  0 siblings, 0 replies; 6+ messages in thread
From: Michal Privoznik @ 2020-06-22 10:06 UTC (permalink / raw)
  To: Richard Henderson, qemu-devel; +Cc: vfeenstr, mdroth

On 6/19/20 11:54 PM, Richard Henderson wrote:
> On 6/16/20 1:34 AM, Michal Privoznik wrote:
>> +#ifndef G_OS_WIN32
> 
> Nit: positive tests are easier to reason with and extend than negative tests.
> I would reverse these two blocks and use a positive test for windows.
> 
> Also, CONFIG_WIN32 is what we use elsewhere for this test.
> 
> 
> r~
> 

Fair enough. Do you want me to send v2 or is it something that committer 
can fix before merging?

Michal



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] qga: Use gethostname() instead of g_get_host_name()
  2020-06-16  8:34 [PATCH] qga: Use gethostname() instead of g_get_host_name() Michal Privoznik
  2020-06-16  8:43 ` Marc-André Lureau
  2020-06-19 21:54 ` Richard Henderson
@ 2020-06-22 10:14 ` Philippe Mathieu-Daudé
  2020-06-22 13:08   ` Michal Privoznik
  2 siblings, 1 reply; 6+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-06-22 10:14 UTC (permalink / raw)
  To: Michal Privoznik, qemu-devel; +Cc: vfeenstr, mdroth

Hi Michal,

On 6/16/20 10:34 AM, Michal Privoznik wrote:
> Problem with g_get_host_name() is that on the first call it saves
> the hostname into a global variable and from then on, every
> subsequent call returns the saved hostname. Even if the hostname
> changes. This doesn't play nicely with guest agent, because if
> the hostname is acquired before the guest is set up (e.g. on the
> first boot, or before DHCP) we will report old, invalid hostname.
> 
> Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1845127
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  qga/commands.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 52 insertions(+), 4 deletions(-)
> 
> diff --git a/qga/commands.c b/qga/commands.c
> index efc8b90281..ce3c2041a6 100644
> --- a/qga/commands.c
> +++ b/qga/commands.c
> @@ -512,14 +512,62 @@ int ga_parse_whence(GuestFileWhence *whence, Error **errp)
>      return -1;
>  }
>  
> +#ifndef HOST_NAME_MAX
> +# ifdef _POSIX_HOST_NAME_MAX
> +#  define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
> +# else
> +#  define HOST_NAME_MAX 255
> +# endif
> +#endif
> +
>  GuestHostName *qmp_guest_get_host_name(Error **errp)
>  {
>      GuestHostName *result = NULL;
> -    gchar const *hostname = g_get_host_name();
> -    if (hostname != NULL) {
> -        result = g_new0(GuestHostName, 1);
> -        result->host_name = g_strdup(hostname);
> +    g_autofree char *hostname = NULL;
> +
> +    /*
> +     * We want to avoid using g_get_host_name() because that
> +     * caches the result and we wouldn't reflect changes in the
> +     * host name.
> +     */

I see there is only one g_get_host_name() call in the
codebase, but can we have a generic qemu_get_host_name()
helper implemented in util/oslib-*c instead?

> +
> +#ifndef G_OS_WIN32
> +    long len = -1;
> +
> +#ifdef _SC_HOST_NAME_MAX
> +    len = sysconf(_SC_HOST_NAME_MAX);
> +#endif /* _SC_HOST_NAME_MAX */
> +
> +    if (len < 0) {
> +        len = HOST_NAME_MAX;
>      }
> +
> +    hostname = g_malloc0(len + 1);
> +
> +    if (gethostname(hostname, len) < 0) {
> +        return NULL;
> +    }
> +
> +#else /* G_OS_WIN32 */
> +
> +    wchar_t tmp[MAX_COMPUTERNAME_LENGTH + 1];
> +    DWORD size = G_N_ELEMENTS(tmp);
> +
> +    if (GetComputerNameW(tmp, &size) != 0) {
> +        /*
> +         * Indeed, on Windows retval of zero means failure
> +         * and nonzero means success.
> +         */
> +        hostname = g_utf16_to_utf8(tmp, size, NULL, NULL, NULL);
> +    }
> +#endif /* G_OS_WIN32 */
> +
> +    if (!hostname) {
> +        hostname = g_strdup("localhost");
> +    }
> +
> +    result = g_new0(GuestHostName, 1);
> +    result->host_name = g_steal_pointer(&hostname);
>      return result;
>  }
>  
> 



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] qga: Use gethostname() instead of g_get_host_name()
  2020-06-22 10:14 ` Philippe Mathieu-Daudé
@ 2020-06-22 13:08   ` Michal Privoznik
  0 siblings, 0 replies; 6+ messages in thread
From: Michal Privoznik @ 2020-06-22 13:08 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Richard Henderson, vfeenstr, mdroth

On 6/22/20 12:14 PM, Philippe Mathieu-Daudé wrote:
> Hi Michal,
> 
> On 6/16/20 10:34 AM, Michal Privoznik wrote:
>> Problem with g_get_host_name() is that on the first call it saves
>> the hostname into a global variable and from then on, every
>> subsequent call returns the saved hostname. Even if the hostname
>> changes. This doesn't play nicely with guest agent, because if
>> the hostname is acquired before the guest is set up (e.g. on the
>> first boot, or before DHCP) we will report old, invalid hostname.
>>
>> Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1845127
>>
>> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
>> ---
>>   qga/commands.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++----
>>   1 file changed, 52 insertions(+), 4 deletions(-)
>>
>> diff --git a/qga/commands.c b/qga/commands.c
>> index efc8b90281..ce3c2041a6 100644
>> --- a/qga/commands.c
>> +++ b/qga/commands.c
>> @@ -512,14 +512,62 @@ int ga_parse_whence(GuestFileWhence *whence, Error **errp)
>>       return -1;
>>   }
>>   
>> +#ifndef HOST_NAME_MAX
>> +# ifdef _POSIX_HOST_NAME_MAX
>> +#  define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
>> +# else
>> +#  define HOST_NAME_MAX 255
>> +# endif
>> +#endif
>> +
>>   GuestHostName *qmp_guest_get_host_name(Error **errp)
>>   {
>>       GuestHostName *result = NULL;
>> -    gchar const *hostname = g_get_host_name();
>> -    if (hostname != NULL) {
>> -        result = g_new0(GuestHostName, 1);
>> -        result->host_name = g_strdup(hostname);
>> +    g_autofree char *hostname = NULL;
>> +
>> +    /*
>> +     * We want to avoid using g_get_host_name() because that
>> +     * caches the result and we wouldn't reflect changes in the
>> +     * host name.
>> +     */
> 
> I see there is only one g_get_host_name() call in the
> codebase, but can we have a generic qemu_get_host_name()
> helper implemented in util/oslib-*c instead?

Sure. Let me post a v2 so that I can include Richard's suggestion too.

Michal



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-06-22 13:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-16  8:34 [PATCH] qga: Use gethostname() instead of g_get_host_name() Michal Privoznik
2020-06-16  8:43 ` Marc-André Lureau
2020-06-19 21:54 ` Richard Henderson
2020-06-22 10:06   ` Michal Privoznik
2020-06-22 10:14 ` Philippe Mathieu-Daudé
2020-06-22 13:08   ` Michal Privoznik

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.