[sysprof/wip/chergert/sysprof-3] tools: add polkit agent to sysprof-cli



commit 891135de54596872ce25a62aacbed516492dbb48
Author: Christian Hergert <chergert redhat com>
Date:   Fri May 24 17:31:37 2019 -0700

    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 b3bd939..ceeeffb 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 5c6c191..b1a534e 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 6fdcfcd..5c5e8bf 100644
--- a/src/tools/sysprof-cli.c
+++ b/src/tools/sysprof-cli.c
@@ -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,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]