tools: add polkit agent to sysprof-cli

The goal for this is to be able to not use sudo to authenticate the user
and elevate privileges.
This commit is contained in:
Christian Hergert
2019-05-24 17:31:37 -07:00
parent aca6baa3bd
commit 88bf04cb37
3 changed files with 22 additions and 1 deletions

View File

@ -61,6 +61,7 @@ if polkit_dep.found()
else
polkit_dep = dependency('polkit-gobject-1', version: polkit_req_version)
endif
polkit_agent_dep = dependency('polkit-agent-1')
debugdir = get_option('debugdir')
if debugdir == ''

View File

@ -4,7 +4,7 @@ tools_deps = [
if host_machine.system() == 'linux'
sysprof_cli = executable('sysprof-cli', 'sysprof-cli.c',
dependencies: tools_deps + [libsysprof_dep],
dependencies: tools_deps + [libsysprof_dep, polkit_dep, polkit_agent_dep],
install_dir: get_option('bindir'),
install: true,
)

View File

@ -24,6 +24,9 @@
#include <glib-unix.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <polkit/polkit.h>
#define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE
#include <polkitagent/polkitagent.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/eventfd.h>
@ -65,6 +68,10 @@ gint
main (gint argc,
gchar *argv[])
{
const gchar *unique_name = NULL;
PolkitAgentListener *polkit = NULL;
PolkitSubject *subject = NULL;
GDBusConnection *bus = NULL;
SysprofCaptureWriter *writer;
SysprofSource *source;
GMainContext *main_context;
@ -128,6 +135,19 @@ main (gint argc,
main_loop = g_main_loop_new (NULL, FALSE);
/* Start polkit agent so that we can elevate privileges from a TTY */
if (g_getenv ("DESKTOP_SESSION") == NULL &&
(bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL)) &&
(unique_name = g_dbus_connection_get_unique_name (bus)) &&
(subject = polkit_system_bus_name_new (unique_name)))
{
polkit = polkit_agent_text_listener_new (NULL, NULL);
polkit_agent_listener_register (polkit,
POLKIT_AGENT_REGISTER_FLAGS_NONE,
subject,
NULL, NULL, NULL);
}
profiler = sysprof_local_profiler_new ();
g_signal_connect (profiler,