From 88bf04cb374dcbc42cac7cc0e48c19ced832ea9b Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Fri, 24 May 2019 17:31:37 -0700 Subject: [PATCH] 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. --- meson.build | 1 + src/tools/meson.build | 2 +- src/tools/sysprof-cli.c | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index b3bd939c..ceeeffb2 100644 --- a/meson.build +++ b/meson.build @@ -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 == '' diff --git a/src/tools/meson.build b/src/tools/meson.build index 5c6c1910..b1a534ee 100644 --- a/src/tools/meson.build +++ b/src/tools/meson.build @@ -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, ) diff --git a/src/tools/sysprof-cli.c b/src/tools/sysprof-cli.c index 6fdcfcdd..5c5e8bf9 100644 --- a/src/tools/sysprof-cli.c +++ b/src/tools/sysprof-cli.c @@ -24,6 +24,9 @@ #include #include #include +#include +#define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE +#include #include #include #include @@ -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,