[gnome-session/wip/benzea/systemd-user-switch: 10/15] main: Add systemd gnome-session monitoring code for leader



commit 78a7dd664d23cde127da6f707a3587fed65e806b
Author: Benjamin Berg <bberg redhat com>
Date:   Sat Apr 20 01:22:38 2019 +0200

    main: Add systemd gnome-session monitoring code for leader
    
    We are in the situation that gnome-session-binary is the session leader
    process for the user. This process is managed via logind and is inside
    the session scope of the user. This process has an important role for
    the session lifetime management, but we cannot track or manage its state
    from the systemd user instance.
    
    This adds a simple protocol to allow us managing the state. The
    counterpart is in gnome-session-ctl.c.
    
    It works by creating a named fifo called gnome-session-leader-fifo in
    the users runtime directory. The session leader opens it for writing,
    the monitoring process opens it for reading.
    
    By closing the FD the monitor process can signal to the leader that the
    session has been shut down normally. By writing to the FD the leader can
    signal the monitoring process to initiate a clean shutdown of the
    session. If either process crashes or is killed, the FD is closed and
    the other side will also quit.

 gnome-session/main.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
---
diff --git a/gnome-session/main.c b/gnome-session/main.c
index e934ac1f..47b31120 100644
--- a/gnome-session/main.c
+++ b/gnome-session/main.c
@@ -26,9 +26,11 @@
 #include <locale.h>
 #include <unistd.h>
 #include <errno.h>
+#include <sys/stat.h>
 
 #include <glib/gi18n.h>
 #include <glib.h>
+#include <glib/gstdio.h>
 #include <glib-unix.h>
 #include <gio/gio.h>
 
@@ -263,6 +265,54 @@ initialize_gio (void)
         }
 }
 
+#ifdef HAVE_SYSTEMD
+static gboolean
+leader_term_or_int_signal_cb (gpointer data)
+{
+        gint fifo_fd = GPOINTER_TO_INT (data);
+
+        /* Start a shutdown explicitly. */
+        gsm_util_start_systemd_unit ("gnome-session-shutdown.target", "replace-irreversibly", NULL);
+
+        /* If we have a fifo, try to signal the other side. */
+        if (fifo_fd >= 0) {
+                /* For good measures, also signal the shutdown. */
+                write (fifo_fd, "S", 1);
+        } else {
+                /* Quit immediately */
+                gsm_quit ();
+        }
+
+        return G_SOURCE_REMOVE;
+}
+
+static void
+systemd_leader_run(void)
+{
+        g_autofree char *fifo_name = NULL;
+        int fifo_fd;
+
+        fifo_name = g_strdup_printf ("%s/gnome-session-leader-fifo",
+                                     g_get_user_runtime_dir ());
+        if (!mkfifo (fifo_name, 0666))
+                g_debug ("Error creating FIFO, it probably already exists!");
+
+        fifo_fd = g_open (fifo_name, O_WRONLY, 0600);
+        if (fifo_fd >= 0) {
+                g_unix_fd_add (fifo_fd, G_IO_HUP, (GUnixFDSourceFunc) gsm_quit, NULL);
+        } else {
+                g_warning ("Could not connect to monitor process");
+        }
+
+        g_unix_signal_add (SIGTERM, leader_term_or_int_signal_cb, GINT_TO_POINTER (fifo_fd));
+        g_unix_signal_add (SIGINT, leader_term_or_int_signal_cb, GINT_TO_POINTER (fifo_fd));
+
+        /* Sleep until we receive HUP or are killed. */
+        gsm_main ();
+        exit(0);
+}
+#endif
+
 int
 main (int argc, char **argv)
 {


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