1072c8
From 15331267d11713906361ddd767c3e04ae46d9a83 Mon Sep 17 00:00:00 2001
1072c8
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
1072c8
Date: Thu, 29 Jul 2021 04:55:50 -0400
1072c8
Subject: [PATCH 01/14] glib-compat: add g_unix_get_passwd_entry_qemu()
1072c8
MIME-Version: 1.0
1072c8
Content-Type: text/plain; charset=UTF-8
1072c8
Content-Transfer-Encoding: 8bit
1072c8
1072c8
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
1072c8
Message-id: <20210609100615.2501448-2-marcandre.lureau@redhat.com>
1072c8
Patchwork-id: 101687
1072c8
O-Subject: [RHEL-8.5.0 qemu-kvm PATCH 1/4] glib-compat: add g_unix_get_passwd_entry_qemu()
1072c8
Bugzilla: 1967716
1072c8
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
1072c8
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
1072c8
RH-Acked-by: Michal Privoznik <mprivozn@redhat.com>
1072c8
1072c8
From: Marc-André Lureau <marcandre.lureau@redhat.com>
1072c8
1072c8
The glib function was introduced in 2.64. It's a safer version of
1072c8
getpwnam, and also simpler to use than getpwnam_r.
1072c8
1072c8
Currently, it's only use by the next patch in qemu-ga, which doesn't
1072c8
(well well...) need the thread safety guarantees. Since the fallback
1072c8
version is still unsafe, I would rather keep the _qemu postfix, to make
1072c8
sure it's not being misused by mistake. When/if necessary, we can
1072c8
implement a safer fallback and drop the _qemu suffix.
1072c8
1072c8
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
1072c8
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
1072c8
*fix checkpatch warnings about newlines before/after block comments
1072c8
Signed-off-by: Michael Roth <michael.roth@amd.com>
1072c8
1072c8
(cherry picked from commit 6d593ab451c490b0ca941c6a519894231634751e)
1072c8
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
1072c8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
1072c8
---
1072c8
 include/glib-compat.h | 28 ++++++++++++++++++++++++++++
1072c8
 1 file changed, 28 insertions(+)
1072c8
1072c8
diff --git a/include/glib-compat.h b/include/glib-compat.h
1072c8
index 0b0ec76299..695a96f7ea 100644
1072c8
--- a/include/glib-compat.h
1072c8
+++ b/include/glib-compat.h
1072c8
@@ -30,6 +30,11 @@
1072c8
 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1072c8
 
1072c8
 #include <glib.h>
1072c8
+#if defined(G_OS_UNIX)
1072c8
+#include <glib-unix.h>
1072c8
+#include <sys/types.h>
1072c8
+#include <pwd.h>
1072c8
+#endif
1072c8
 
1072c8
 /*
1072c8
  * Note that because of the GLIB_VERSION_MAX_ALLOWED constant above, allowing
1072c8
@@ -72,6 +77,29 @@
1072c8
 gint g_poll_fixed(GPollFD *fds, guint nfds, gint timeout);
1072c8
 #endif
1072c8
 
1072c8
+#if defined(G_OS_UNIX)
1072c8
+/*
1072c8
+ * Note: The fallback implementation is not MT-safe, and it returns a copy of
1072c8
+ * the libc passwd (must be g_free() after use) but not the content. Because of
1072c8
+ * these important differences the caller must be aware of, it's not #define for
1072c8
+ * GLib API substitution.
1072c8
+ */
1072c8
+static inline struct passwd *
1072c8
+g_unix_get_passwd_entry_qemu(const gchar *user_name, GError **error)
1072c8
+{
1072c8
+#if GLIB_CHECK_VERSION(2, 64, 0)
1072c8
+    return g_unix_get_passwd_entry(user_name, error);
1072c8
+#else
1072c8
+    struct passwd *p = getpwnam(user_name);
1072c8
+    if (!p) {
1072c8
+        g_set_error_literal(error, G_UNIX_ERROR, 0, g_strerror(errno));
1072c8
+        return NULL;
1072c8
+    }
1072c8
+    return (struct passwd *)g_memdup(p, sizeof(*p));
1072c8
+#endif
1072c8
+}
1072c8
+#endif /* G_OS_UNIX */
1072c8
+
1072c8
 #pragma GCC diagnostic pop
1072c8
 
1072c8
 #endif
1072c8
-- 
1072c8
2.27.0
1072c8