Skip to content

Commit

Permalink
fixup! utils: Use GetConnectionCredentials to fetch the PID and pidfd
Browse files Browse the repository at this point in the history
  • Loading branch information
swick committed Mar 14, 2024
1 parent 0abbc5c commit dcb493f
Showing 1 changed file with 52 additions and 8 deletions.
60 changes: 52 additions & 8 deletions src/xdp-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit dcb493f

Please sign in to comment.