thebeanogamer / rpms / qemu-kvm

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