cryptospore / rpms / qemu-kvm

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