[gnome-shell/benzea/systemd-scopes: 1/2] utils: Add utility function to place a PID into a systemd scope



commit 315cd9f8434a941d8574a7d88597871686f8020d
Author: Benjamin Berg <bberg redhat com>
Date:   Tue Nov 26 19:44:43 2019 +0100

    utils: Add utility function to place a PID into a systemd scope

 src/shell-util.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/shell-util.h |  3 +++
 2 files changed, 63 insertions(+)
---
diff --git a/src/shell-util.c b/src/shell-util.c
index f93462c73e..18d893595b 100644
--- a/src/shell-util.c
+++ b/src/shell-util.c
@@ -668,6 +668,66 @@ shell_util_stop_systemd_unit (const char  *unit,
   return shell_util_systemd_call ("StopUnit", unit, mode, error);
 }
 
+
+void
+shell_util_start_systemd_scope (GAppInfo          *info,
+                                gint32             pid)
+{
+  GVariantBuilder builder;
+  const gchar *app_id;
+  g_autofree gchar *unit_name = NULL;
+  g_autoptr (GDBusConnection) connection = NULL;
+
+  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
+
+  if (connection == NULL)
+    return;
+
+  g_debug ("Trying to create transient scope for PID %d\n", pid);
+
+  /* Prepare some information */
+  app_id = g_app_info_get_id (info);
+  if (app_id == NULL)
+    app_id = "anonymous";
+
+  /* This needs to be unique, hopefully the pid will be enough. */
+  unit_name = g_strdup_printf ("gnome-shell-launched-%s-%d.scope", app_id, pid);
+
+
+  g_variant_builder_init (&builder, G_VARIANT_TYPE ("(ssa(sv)a(sa(sv)))"));
+  g_variant_builder_add (&builder, "s", unit_name);
+  g_variant_builder_add (&builder, "s", "fail");
+
+  g_variant_builder_open (&builder, G_VARIANT_TYPE ("a(sv)"));
+  /* Note that futher settings are controlled using a drop-in. */
+  g_variant_builder_add (&builder,
+                         "(sv)",
+                         "Description",
+                         g_variant_new_string ("Application launched by gnome-shell"));
+  g_variant_builder_add (&builder,
+                         "(sv)",
+                         "PIDs",
+                          g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, &pid, 1, 4));
+
+  g_variant_builder_close (&builder);
+
+  g_variant_builder_open (&builder, G_VARIANT_TYPE ("a(sa(sv))"));
+  g_variant_builder_close (&builder);
+
+  g_dbus_connection_call (connection,
+                          "org.freedesktop.systemd1",
+                          "/org/freedesktop/systemd1",
+                          "org.freedesktop.systemd1.Manager",
+                          "StartTransientUnit",
+                          g_variant_builder_end (&builder),
+                          G_VARIANT_TYPE ("(o)"),
+                          G_DBUS_CALL_FLAGS_NO_AUTO_START,
+                          1000,
+                          NULL,
+                          on_systemd_call_cb,
+                          (gpointer) "StartTransientUnit");
+}
+
 void
 shell_util_sd_notify (void)
 {
diff --git a/src/shell-util.h b/src/shell-util.h
index 4453beb4a5..18d9eac240 100644
--- a/src/shell-util.h
+++ b/src/shell-util.h
@@ -66,6 +66,9 @@ gboolean shell_util_stop_systemd_unit  (const char  *unit,
                                         const char  *mode,
                                         GError     **error);
 
+void shell_util_start_systemd_scope (GAppInfo          *info,
+                                     gint32             pid);
+
 void shell_util_sd_notify (void);
 
 G_END_DECLS


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