[sysprof/wip/chergert/sysprof-3] sysprofd: make sysprofd exit after 120 seconds of inactivity



commit b38f4a9319188ead381b285417ffdc0d85e36e17
Author: Christian Hergert <chergert redhat com>
Date:   Thu May 23 16:38:02 2019 -0700

    sysprofd: make sysprofd exit after 120 seconds of inactivity

 src/sysprofd/ipc-service-impl.c | 32 ++++++++++++++++++++++++++------
 src/sysprofd/sysprofd.c         | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+), 6 deletions(-)
---
diff --git a/src/sysprofd/ipc-service-impl.c b/src/sysprofd/ipc-service-impl.c
index 0da8acd..3b668ab 100644
--- a/src/sysprofd/ipc-service-impl.c
+++ b/src/sysprofd/ipc-service-impl.c
@@ -39,6 +39,13 @@ struct _IpcServiceImpl
   IpcServiceSkeleton parent_instance;
 };
 
+enum {
+  ACTIVITY,
+  N_SIGNALS
+};
+
+static guint signals [N_SIGNALS];
+
 static gboolean
 ipc_service_impl_handle_list_processes (IpcService            *service,
                                         GDBusMethodInvocation *invocation)
@@ -210,17 +217,19 @@ ipc_service_impl_g_authorize_method (GDBusInterfaceSkeleton *skeleton,
   g_assert (IPC_IS_SERVICE_IMPL (skeleton));
   g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation));
 
+  g_signal_emit (skeleton, signals [ACTIVITY], 0);
+
   peer_name = g_dbus_method_invocation_get_sender (invocation);
 
   if (!(authority = polkit_authority_get_sync (NULL, NULL)) ||
       !(subject = polkit_system_bus_name_new (peer_name)) ||
       !(res = polkit_authority_check_authorization_sync (authority,
-                                                  POLKIT_SUBJECT (subject),
-                                                  "org.gnome.sysprof3.profile",
-                                                  NULL,
-                                                  POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
-                                                  NULL,
-                                                  NULL)) ||
+                                                         POLKIT_SUBJECT (subject),
+                                                         "org.gnome.sysprof3.profile",
+                                                         NULL,
+                                                         
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
+                                                         NULL,
+                                                         NULL)) ||
       !polkit_authorization_result_get_is_authorized (res))
     {
       g_dbus_method_invocation_return_error (g_steal_pointer (&invocation),
@@ -273,6 +282,17 @@ ipc_service_impl_class_init (IpcServiceImplClass *klass)
   GDBusInterfaceSkeletonClass *skeleton_class = G_DBUS_INTERFACE_SKELETON_CLASS (klass);
 
   skeleton_class->g_authorize_method = ipc_service_impl_g_authorize_method;
+
+  signals [ACTIVITY] =
+    g_signal_new ("activity",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST,
+                  0, NULL, NULL,
+                  g_cclosure_marshal_VOID__VOID,
+                  G_TYPE_NONE, 0);
+  g_signal_set_va_marshaller (signals [ACTIVITY],
+                              G_TYPE_FROM_CLASS (klass),
+                              g_cclosure_marshal_VOID__VOIDv);
 }
 
 static void
diff --git a/src/sysprofd/sysprofd.c b/src/sysprofd/sysprofd.c
index 9734f4c..1b36a65 100644
--- a/src/sysprofd/sysprofd.c
+++ b/src/sysprofd/sysprofd.c
@@ -31,11 +31,36 @@
 #define BUS_NAME                "org.gnome.Sysprof3"
 #define OBJECT_PATH             "/org/gnome/Sysprof3"
 #define NAME_ACQUIRE_DELAY_SECS 3
+#define INACTIVITY_TIMEOUT_SECS 120
 
 static GMainLoop *main_loop;
 static gboolean   name_acquired;
 static gint       exit_status = EXIT_SUCCESS;
 
+static guint inactivity;
+static G_LOCK_DEFINE (activity);
+
+static gboolean
+inactivity_cb (gpointer data)
+{
+  inactivity = 0;
+  g_main_loop_quit (main_loop);
+  return G_SOURCE_REMOVE;
+}
+
+static void
+activity_cb (IpcService *service,
+             gpointer    user_data)
+{
+  G_LOCK (activity);
+  if (inactivity)
+    g_source_remove (inactivity);
+  inactivity = g_timeout_add_seconds (INACTIVITY_TIMEOUT_SECS,
+                                      inactivity_cb,
+                                      NULL);
+  G_UNLOCK (activity);
+}
+
 static void
 name_acquired_cb (GDBusConnection *connection,
                   const gchar     *name,
@@ -93,6 +118,13 @@ main (gint   argc,
     {
       g_autoptr(IpcService) service = ipc_service_impl_new ();
 
+      g_signal_connect (service,
+                        "activity",
+                        G_CALLBACK (activity_cb),
+                        NULL);
+
+      activity_cb (service, NULL);
+
       if (g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (service),
                                             bus,
                                             OBJECT_PATH,


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