From fb92ea5c1ec42b0f0389094c45390fdc8af739eb Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 21 Aug 2013 13:38:54 -0400 Subject: [PATCH 4/4] sessionmonitor-systemd: [cleanup]: Deduplicate code paths We had the code to go from pid -> session duplicated. --- .../polkitbackendsessionmonitor-systemd.c | 60 +++++++------------- 1 files changed, 20 insertions(+), 40 deletions(-) diff --git a/src/polkitbackend/polkitbackendsessionmonitor-systemd.c b/src/polkitbackend/polkitbackendsessionmonitor-systemd.c index 8d8479e..770cdb4 100644 --- a/src/polkitbackend/polkitbackendsessionmonitor-systemd.c +++ b/src/polkitbackend/polkitbackendsessionmonitor-systemd.c @@ -313,49 +313,20 @@ polkit_backend_session_monitor_get_session_for_subject (PolkitBackendSessionMoni PolkitSubject *subject, GError **error) { - PolkitSubject *session; - - session = NULL; + PolkitUnixProcess *tmp_process = NULL; + PolkitUnixProcess *process = NULL; + PolkitSubject *session = NULL; + char *session_id = NULL; if (POLKIT_IS_UNIX_PROCESS (subject)) - { - gchar *session_id; - pid_t pid; - - pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject)); - if (sd_pid_get_session (pid, &session_id) < 0) - goto out; - - session = polkit_unix_session_new (session_id); - free (session_id); - } + process = POLKIT_UNIX_PROCESS (subject); /* We already have a process */ else if (POLKIT_IS_SYSTEM_BUS_NAME (subject)) { - guint32 pid; - gchar *session_id; - GVariant *result; - - result = g_dbus_connection_call_sync (monitor->system_bus, - "org.freedesktop.DBus", - "/org/freedesktop/DBus", - "org.freedesktop.DBus", - "GetConnectionUnixProcessID", - g_variant_new ("(s)", polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject))), - G_VARIANT_TYPE ("(u)"), - G_DBUS_CALL_FLAGS_NONE, - -1, /* timeout_msec */ - NULL, /* GCancellable */ - error); - if (result == NULL) - goto out; - g_variant_get (result, "(u)", &pid); - g_variant_unref (result); - - if (sd_pid_get_session (pid, &session_id) < 0) - goto out; - - session = polkit_unix_session_new (session_id); - free (session_id); + /* Convert bus name to uid */ + tmp_process = (PolkitUnixProcess*)polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME (subject), NULL, error); + if (!tmp_process) + goto out; + process = tmp_process; } else { @@ -366,8 +337,17 @@ polkit_backend_session_monitor_get_session_for_subject (PolkitBackendSessionMoni g_type_name (G_TYPE_FROM_INSTANCE (subject))); } - out: + /* Now that we have a process, extract the session from it */ + g_assert (process != NULL); + pid = polkit_unix_process_get_pid (process); + if (sd_pid_get_session (pid, &session_id) < 0) + goto out; + + session = polkit_unix_session_new (session_id); + free (session_id); + out: + if (tmp_process) g_object_unref (tmp_process); return session; } -- 1.7.1