[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/2] glib-compat: add g_unix_get_passwd_entry_qemu()
From: |
marcandre . lureau |
Subject: |
[PATCH 1/2] glib-compat: add g_unix_get_passwd_entry_qemu() |
Date: |
Wed, 14 Oct 2020 00:25:01 +0400 |
From: Marc-André Lureau <marcandre.lureau@redhat.com>
The glib function was introduced in 2.64. It's a safer version of
getpwnam, and also simpler to use than getpwnam_r.
Currently, it's only use by the next patch in qemu-ga, which doesn't
(well well...) need the thread safety guarantees. Since the fallback
version is still unsafe, I would rather keep the _qemu postfix, to make
sure it's not being misused by mistake. When/if necessary, we can
implement a safer fallback and drop the _qemu suffix.
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
include/glib-compat.h | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/include/glib-compat.h b/include/glib-compat.h
index 0b0ec76299..2a56b3462b 100644
--- a/include/glib-compat.h
+++ b/include/glib-compat.h
@@ -30,6 +30,9 @@
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include <glib.h>
+#if defined(G_OS_UNIX)
+#include <glib-unix.h>
+#endif
/*
* Note that because of the GLIB_VERSION_MAX_ALLOWED constant above, allowing
@@ -72,6 +75,27 @@
gint g_poll_fixed(GPollFD *fds, guint nfds, gint timeout);
#endif
+#if defined(G_OS_UNIX)
+/* Note: The fallback implementation is not MT-safe, and it returns a copy of
+ * the libc passwd (must be g_free() after use) but not the content. Because of
+ * these important differences the caller must be aware of, it's not #define
for
+ * GLib API substitution. */
+static inline struct passwd *
+g_unix_get_passwd_entry_qemu(const gchar *user_name, GError **error)
+{
+#if GLIB_CHECK_VERSION(2, 64, 0)
+ return g_unix_get_passwd_entry(user_name, error);
+#else
+ struct passwd *p = getpwnam(user_name);
+ if (!p) {
+ g_set_error(error, G_UNIX_ERROR, 0, g_strerror(errno));
+ return NULL;
+ }
+ return g_memdup(p, sizeof(*p));
+#endif
+}
+#endif /* G_OS_UNIX */
+
#pragma GCC diagnostic pop
#endif
--
2.28.0
- [PATCH 0/2] qemu-ga: add ssh-{add,remove}-authorized-keys, marcandre . lureau, 2020/10/13
- [PATCH 1/2] glib-compat: add g_unix_get_passwd_entry_qemu(),
marcandre . lureau <=
- [PATCH 2/2] qga: add ssh-{add,remove}-authorized-keys, marcandre . lureau, 2020/10/13
- Re: [PATCH 2/2] qga: add ssh-{add,remove}-authorized-keys, Daniel P . Berrangé, 2020/10/15
- Re: [PATCH 2/2] qga: add ssh-{add,remove}-authorized-keys, Eric Blake, 2020/10/19
- Re: [PATCH 0/2] qemu-ga: add ssh-{add,remove}-authorized-keys, no-reply, 2020/10/13
- Re: [PATCH 0/2] qemu-ga: add ssh-{add,remove}-authorized-keys, Michal Privoznik, 2020/10/14
- Re: [PATCH 0/2] qemu-ga: add ssh-{add,remove}-authorized-keys, Daniel P . Berrangé, 2020/10/14