From dcb493f8f511e1752e0bc293b5554281bcbcbae7 Mon Sep 17 00:00:00 2001 From: Sebastian Wick Date: Thu, 14 Mar 2024 13:33:54 +0100 Subject: [PATCH] fixup! utils: Use GetConnectionCredentials to fetch the PID and pidfd --- src/xdp-utils.c | 60 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/src/xdp-utils.c b/src/xdp-utils.c index 919a5f150..ec969ed7f 100644 --- a/src/xdp-utils.c +++ b/src/xdp-utils.c @@ -850,6 +850,35 @@ xdp_app_info_from_snap (int pid, return TRUE; } +static gboolean +xdp_connection_get_pid_legacy (GDBusConnection *connection, + const char *sender, + GCancellable *cancellable, + int *out_pidfd, + guint32 *out_pid, + GError **error) +{ + g_autoptr(GVariant) reply = NULL; + + reply = g_dbus_connection_call_sync (connection, + DBUS_NAME_DBUS, + DBUS_PATH_DBUS, + DBUS_INTERFACE_DBUS, + "GetConnectionUnixProcessID", + g_variant_new ("(s)", sender), + G_VARIANT_TYPE ("(u)"), + G_DBUS_CALL_FLAGS_NONE, + 30000, + cancellable, + error); + if (!reply) + return FALSE; + + *out_pidfd = -1; + g_variant_get (reply, "(u)", out_pid); + return TRUE; +} + static gboolean xdp_connection_get_pidfd (GDBusConnection *connection, const char *sender, @@ -858,8 +887,8 @@ xdp_connection_get_pidfd (GDBusConnection *connection, guint32 *out_pid, GError **error) { - g_autoptr(GVariant) parameters = NULL; g_autoptr(GVariant) reply = NULL; + g_autoptr(GError) local_error = NULL; g_autoptr(GVariant) process_fd = NULL; g_autoptr(GVariant) process_id = NULL; guint32 pid; @@ -869,30 +898,45 @@ xdp_connection_get_pidfd (GDBusConnection *connection, const gint *fds; gint pidfd; - parameters = g_variant_new ("(s)", sender); - reply = g_dbus_connection_call_with_unix_fd_list_sync (connection, DBUS_NAME_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "GetConnectionCredentials", - parameters, + g_variant_new ("(s)", sender), G_VARIANT_TYPE ("(a{sv})"), G_DBUS_CALL_FLAGS_NONE, 30000, NULL, &fd_list, cancellable, - error); + &local_error); if (!reply) - return FALSE; + { + if (g_error_matches (local_error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_INTERFACE)) + { + return xdp_connection_get_pid_legacy (connection, + sender, + cancellable, + out_pidfd, + out_pid, + error); + } + + g_propagate_error (error, local_error); + return FALSE; + } process_id = g_variant_lookup_value (reply, "ProcessID", G_VARIANT_TYPE_UINT32); if (!process_id) { - g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Can't find peer pid"); - return FALSE; + return xdp_connection_get_pid_legacy (connection, + sender, + cancellable, + out_pidfd, + out_pid, + error); } pid = g_variant_get_uint32 (process_id);