/** * pk_dbus_get_uid: * @dbus: the #PkDbus instance * @sender: the sender * * Gets the process UID. * * Return value: the UID, or %G_MAXUINT if it could not be obtained **/ guint pk_dbus_get_uid (PkDbus *dbus, const gchar *sender) { guint uid = G_MAXUINT; g_autoptr(GError) error = NULL; g_autoptr(GVariant) value = NULL; g_return_val_if_fail (PK_IS_DBUS (dbus), G_MAXUINT); g_return_val_if_fail (sender != NULL, G_MAXUINT); /* set in the test suite */ if (g_strcmp0 (sender, ":org.freedesktop.PackageKit") == 0) { g_debug ("using self-check shortcut"); return 500; } value = g_dbus_proxy_call_sync (dbus->priv->proxy_uid, "GetConnectionUnixUser", g_variant_new ("(s)", sender), G_DBUS_CALL_FLAGS_NONE, 2000, NULL, &error); if (value == NULL) { g_warning ("Failed to get uid for %s: %s", sender, error->message); return G_MAXUINT; } g_variant_get (value, "(u)", &uid); return uid; }
/** * pk_dbus_get_cmdline: * @dbus: the #PkDbus instance * @sender: the sender, usually got from dbus_g_method_get_dbus() * * Gets the command line for the ID. * * Return value: the cmdline, or %NULL if it could not be obtained **/ gchar * pk_dbus_get_cmdline (PkDbus *dbus, const gchar *sender) { gboolean ret; gchar *cmdline = NULL; guint pid; g_autoptr(GError) error = NULL; g_autofree gchar *filename = NULL; g_return_val_if_fail (PK_IS_DBUS (dbus), NULL); g_return_val_if_fail (sender != NULL, NULL); /* set in the test suite */ if (g_strcmp0 (sender, ":org.freedesktop.PackageKit") == 0) { g_debug ("using self-check shortcut"); return g_strdup ("/usr/sbin/packagekit"); } /* get pid */ pid = pk_dbus_get_pid (dbus, sender); if (pid == G_MAXUINT) { g_warning ("failed to get PID"); return NULL; } /* get command line from proc */ filename = g_strdup_printf ("/proc/%i/cmdline", pid); ret = g_file_get_contents (filename, &cmdline, NULL, &error); if (!ret) g_warning ("failed to get cmdline: %s", error->message); return cmdline; }
/** * gpk_dbus_get_exec_for_sender: **/ static gchar * gpk_dbus_get_exec_for_sender (GpkDbus *dbus, const gchar *sender) { gchar *filename = NULL; gchar *cmdline = NULL; GError *error = NULL; guint pid; g_return_val_if_fail (PK_IS_DBUS (dbus), NULL); g_return_val_if_fail (sender != NULL, NULL); /* get pid */ pid = gpk_dbus_get_pid (dbus, sender); if (pid == G_MAXUINT) { g_warning ("failed to get PID"); goto out; } /* get command line from proc */ filename = g_strdup_printf ("/proc/%i/exe", pid); cmdline = g_file_read_link (filename, &error); if (cmdline == NULL) { g_warning ("failed to find exec: %s", error->message); g_error_free (error); } out: g_free (filename); return cmdline; }
/** * gpk_dbus_get_pid: **/ static guint gpk_dbus_get_pid (GpkDbus *dbus, const gchar *sender) { guint pid; g_return_val_if_fail (PK_IS_DBUS (dbus), G_MAXUINT); g_return_val_if_fail (dbus->priv->proxy_session_pid != NULL, G_MAXUINT); g_return_val_if_fail (dbus->priv->proxy_system_pid != NULL, G_MAXUINT); g_return_val_if_fail (sender != NULL, G_MAXUINT); /* check system bus first */ pid = gpk_dbus_get_pid_system (dbus, sender); if (pid != G_MAXUINT) goto out; /* and then session bus */ pid = gpk_dbus_get_pid_session (dbus, sender); if (pid != G_MAXUINT) goto out; /* should be impossible */ g_warning ("could not find pid!"); out: return pid; }
/** * pk_dbus_get_session: * @dbus: the #PkDbus instance * @sender: the sender, usually got from dbus_g_method_get_dbus() * * Gets the logind or ConsoleKit session for the ID. * * Return value: the session identifier, or %NULL if it could not be obtained **/ gchar * pk_dbus_get_session (PkDbus *dbus, const gchar *sender) { gchar *session = NULL; #ifndef HAVE_SYSTEMD g_autoptr(GError) error = NULL; #endif guint pid; g_autoptr(GVariant) value = NULL; g_return_val_if_fail (PK_IS_DBUS (dbus), NULL); g_return_val_if_fail (sender != NULL, NULL); /* set in the test suite */ if (g_strcmp0 (sender, ":org.freedesktop.PackageKit") == 0) { g_debug ("using self-check shortcut"); session = g_strdup ("xxx"); goto out; } /* no ConsoleKit? */ if (dbus->priv->proxy_session == NULL) { g_warning ("no ConsoleKit, so cannot get session"); goto out; } /* get pid */ pid = pk_dbus_get_pid (dbus, sender); if (pid == G_MAXUINT) { g_warning ("failed to get PID"); goto out; } /* get session from systemd or ConsoleKit */ #ifdef HAVE_SYSTEMD session = pk_dbus_get_session_systemd (pid); if (session == NULL) g_warning ("failed to get session for pid %u", pid); #else /* get session from ConsoleKit */ value = g_dbus_proxy_call_sync (dbus->priv->proxy_session, "GetSessionForUnixProcess", g_variant_new ("(u)", pid), G_DBUS_CALL_FLAGS_NONE, 2000, NULL, &error); if (value == NULL) { g_warning ("Failed to get session for %s: %s", sender, error->message); goto out; } g_variant_get (value, "(o)", &session); #endif out: return session; }
/** * pk_dbus_finalize: **/ static void pk_dbus_finalize (GObject *object) { PkDbus *dbus; g_return_if_fail (object != NULL); g_return_if_fail (PK_IS_DBUS (object)); dbus = PK_DBUS (object); g_object_unref (dbus->priv->proxy_pid); g_object_unref (dbus->priv->proxy_uid); if (dbus->priv->proxy_session != NULL) g_object_unref (dbus->priv->proxy_session); G_OBJECT_CLASS (pk_dbus_parent_class)->finalize (object); }
/** * gpk_dbus_finalize: * @object: The object to finalize **/ static void gpk_dbus_finalize (GObject *object) { GpkDbus *dbus; g_return_if_fail (PK_IS_DBUS (object)); dbus = GPK_DBUS (object); g_return_if_fail (dbus->priv != NULL); g_timer_destroy (dbus->priv->timer); g_object_unref (dbus->priv->settings); g_object_unref (dbus->priv->x11); g_object_unref (dbus->priv->proxy_session_pid); g_object_unref (dbus->priv->proxy_system_pid); G_OBJECT_CLASS (gpk_dbus_parent_class)->finalize (object); }
/** * pk_dbus_get_pid: * @dbus: the #PkDbus instance * @sender: the sender, usually got from dbus_g_method_get_dbus() * * Gets the process ID. * * Return value: the PID, or %G_MAXUINT if it could not be obtained **/ static guint pk_dbus_get_pid (PkDbus *dbus, const gchar *sender) { guint pid = G_MAXUINT; _cleanup_error_free_ GError *error = NULL; _cleanup_variant_unref_ GVariant *value = NULL; g_return_val_if_fail (PK_IS_DBUS (dbus), G_MAXUINT); g_return_val_if_fail (sender != NULL, G_MAXUINT); /* set in the test suite */ if (g_strcmp0 (sender, ":org.freedesktop.PackageKit") == 0) { g_debug ("using self-check shortcut"); return G_MAXUINT - 1; } /* no connection to DBus */ if (dbus->priv->proxy_pid == NULL) return G_MAXUINT; /* get pid from DBus */ value = g_dbus_proxy_call_sync (dbus->priv->proxy_pid, "GetConnectionUnixProcessID", g_variant_new ("(s)", sender), G_DBUS_CALL_FLAGS_NONE, 2000, NULL, &error); if (value == NULL) { g_warning ("Failed to get pid for %s: %s", sender, error->message); return G_MAXUINT; } g_variant_get (value, "(u)", &pid); return pid; }