Skip to contentSkip to conversation

qga: Use gethostname() instead of g_get_host_name()

PATCH In review Michal Privoznik 4 participants 6 messages Jun 16, 2020 – Jun 22, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qga: Use gethostname() instead of g_get_host_name()

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.

---
 qga/commands.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)
qga/commands.c +52 -4
index efc8b90281..ce3c2041a6 100644
@@ -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;
 }
 
Signature · 1 line
2.26.2
Reviewed-by on v1 1/1
#1 lore raw

Hi

Quoted 8 lines · Michal Privoznik

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.

lgtm,

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

Quoted 83 lines
---
 qga/commands.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)
qga/commands.c +52 -5
index efc8b90281..ce3c2041a6 100644
@@ -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

Signature · 1 line
Marc-André Lureau
#2 lore raw
In reply to Michal Privoznik
Quoted 1 line · Michal Privoznik

+#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~

#3 lore raw
Quoted 11 lines · Richard Henderson
Quoted 1 line · Michal Privoznik

+#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

#4 lore raw
In reply to Michal Privoznik

Hi Michal,

Quoted 34 lines · Michal Privoznik

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.

---
 qga/commands.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)
qga/commands.c +8 -1
index efc8b90281..ce3c2041a6 100644
@@ -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();

qga/commands.c int ga_parse_whence(GuestFileWhence *whence, Error **errp) · line 532

Quoted 10 lines
qga/commands.c +7 -3
-    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?

Quoted 42 lines
qga/commands.c +37 -0
+
+#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;
 }
 
#5 lore raw
Quoted 43 lines · Philippe Mathieu-Daudé

Hi Michal,

Quoted 40 lines · Michal Privoznik

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.

---
  qga/commands.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++----
  1 file changed, 52 insertions(+), 4 deletions(-)
qga/commands.c +11 -4
index efc8b90281..ce3c2041a6 100644
@@ -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;
+
+    /*
Quoted 8 lines

qga/commands.c int ga_parse_whence(GuestFileWhence *whence, Error **errp) · line 532

Quoted 4 lines
qga/commands.c +4 -0
+     * 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