yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
8fced6
From 41510fba34cda98cb85a8d04e46dcfdd9a91aa61 Mon Sep 17 00:00:00 2001
8fced6
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
8fced6
Date: Thu, 24 Dec 2020 12:53:03 -0500
8fced6
Subject: [PATCH 3/5] util: Introduce qemu_get_host_name()
8fced6
MIME-Version: 1.0
8fced6
Content-Type: text/plain; charset=UTF-8
8fced6
Content-Transfer-Encoding: 8bit
8fced6
8fced6
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
8fced6
Message-id: <20201224125304.62697-3-marcandre.lureau@redhat.com>
8fced6
Patchwork-id: 100499
8fced6
O-Subject: [RHEL-8.4.0 qemu-kvm PATCH 2/3] util: Introduce qemu_get_host_name()
8fced6
Bugzilla: 1910326
8fced6
RH-Acked-by: Daniel P. Berrange <berrange@redhat.com>
8fced6
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
8fced6
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
8fced6
8fced6
From: Michal Privoznik <mprivozn@redhat.com>
8fced6
8fced6
This function offers operating system agnostic way to fetch host
8fced6
name. It is implemented for both POSIX-like and Windows systems.
8fced6
8fced6
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
8fced6
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
8fced6
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
8fced6
Cc: qemu-stable@nongnu.org
8fced6
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
8fced6
8fced6
(cherry picked from commit e47f4765afcab2b78dfa5b0115abf64d1d49a5d3)
8fced6
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
8fced6
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
8fced6
---
8fced6
 include/qemu/osdep.h | 10 ++++++++++
8fced6
 util/oslib-posix.c   | 35 +++++++++++++++++++++++++++++++++++
8fced6
 util/oslib-win32.c   | 13 +++++++++++++
8fced6
 3 files changed, 58 insertions(+)
8fced6
8fced6
diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
8fced6
index 0f97d68586a..d427e81a427 100644
8fced6
--- a/include/qemu/osdep.h
8fced6
+++ b/include/qemu/osdep.h
8fced6
@@ -620,4 +620,14 @@ static inline void qemu_reset_optind(void)
8fced6
 #endif
8fced6
 }
8fced6
 
8fced6
+/**
8fced6
+ * qemu_get_host_name:
8fced6
+ * @errp: Error object
8fced6
+ *
8fced6
+ * Operating system agnostic way of querying host name.
8fced6
+ *
8fced6
+ * Returns allocated hostname (caller should free), NULL on failure.
8fced6
+ */
8fced6
+char *qemu_get_host_name(Error **errp);
8fced6
+
8fced6
 #endif
8fced6
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
8fced6
index 5a291cc9820..8f88e4dbe10 100644
8fced6
--- a/util/oslib-posix.c
8fced6
+++ b/util/oslib-posix.c
8fced6
@@ -726,3 +726,38 @@ void sigaction_invoke(struct sigaction *action,
8fced6
     }
8fced6
     action->sa_sigaction(info->ssi_signo, &si, NULL);
8fced6
 }
8fced6
+
8fced6
+#ifndef HOST_NAME_MAX
8fced6
+# ifdef _POSIX_HOST_NAME_MAX
8fced6
+#  define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
8fced6
+# else
8fced6
+#  define HOST_NAME_MAX 255
8fced6
+# endif
8fced6
+#endif
8fced6
+
8fced6
+char *qemu_get_host_name(Error **errp)
8fced6
+{
8fced6
+    long len = -1;
8fced6
+    g_autofree char *hostname = NULL;
8fced6
+
8fced6
+#ifdef _SC_HOST_NAME_MAX
8fced6
+    len = sysconf(_SC_HOST_NAME_MAX);
8fced6
+#endif /* _SC_HOST_NAME_MAX */
8fced6
+
8fced6
+    if (len < 0) {
8fced6
+        len = HOST_NAME_MAX;
8fced6
+    }
8fced6
+
8fced6
+    /* Unfortunately, gethostname() below does not guarantee a
8fced6
+     * NULL terminated string. Therefore, allocate one byte more
8fced6
+     * to be sure. */
8fced6
+    hostname = g_new0(char, len + 1);
8fced6
+
8fced6
+    if (gethostname(hostname, len) < 0) {
8fced6
+        error_setg_errno(errp, errno,
8fced6
+                         "cannot get hostname");
8fced6
+        return NULL;
8fced6
+    }
8fced6
+
8fced6
+    return g_steal_pointer(&hostname);
8fced6
+}
8fced6
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
8fced6
index e9b14ab1784..3b49d272972 100644
8fced6
--- a/util/oslib-win32.c
8fced6
+++ b/util/oslib-win32.c
8fced6
@@ -808,3 +808,16 @@ bool qemu_write_pidfile(const char *filename, Error **errp)
8fced6
     }
8fced6
     return true;
8fced6
 }
8fced6
+
8fced6
+char *qemu_get_host_name(Error **errp)
8fced6
+{
8fced6
+    wchar_t tmp[MAX_COMPUTERNAME_LENGTH + 1];
8fced6
+    DWORD size = G_N_ELEMENTS(tmp);
8fced6
+
8fced6
+    if (GetComputerNameW(tmp, &size) == 0) {
8fced6
+        error_setg_win32(errp, GetLastError(), "failed close handle");
8fced6
+        return NULL;
8fced6
+    }
8fced6
+
8fced6
+    return g_utf16_to_utf8(tmp, size, NULL, NULL, NULL);
8fced6
+}
8fced6
-- 
8fced6
2.27.0
8fced6