[gnome-remote-desktop] daemon: Handle SIGINT and SIGTERM signals properly



commit 5db49df94a09e67b2254cac5e426a6cb855ed2ad
Author: Pascal Nowack <Pascal Nowack gmx de>
Date:   Tue Mar 29 15:40:39 2022 +0200

    daemon: Handle SIGINT and SIGTERM signals properly
    
    Currently, when the gnome-remote-desktop-daemon is stopped via a SIGINT
    or SIGTERM signal, it directly stops without cleaning up its resources.
    This is especially visible, when there is a current RDP session
    running, as all FUSE mounts will remain mounted, when
    gnome-remote-desktop is terminated via these signals.
    This is very annoying to see, when e.g. running the command `df -h`, as
    it will show that the socket for the FUSE mount is not connected any
    more.
    The SIGINT signal is emitted, when running the daemon manually in a
    terminal window, the SIGTERM signal is emitted by systemd, when
    stopping or restarting the daemon.
    systemd handles SIGTERM signals gracefully, if the service handles
    these signals.
    This means systemd will wait until gnome-remote-desktop stopped all
    sessions and cleaned up its resources.
    This also includes the FUSE unmount operations.
    
    To handle the SIGINT and SIGTERM signals properly, register handlers
    for them, which shut down the daemon, when the respective signals are
    received.

 src/grd-daemon.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
---
diff --git a/src/grd-daemon.c b/src/grd-daemon.c
index 54a27888..35616505 100644
--- a/src/grd-daemon.c
+++ b/src/grd-daemon.c
@@ -25,6 +25,7 @@
 #include "grd-daemon.h"
 
 #include <gio/gio.h>
+#include <glib-unix.h>
 #include <glib/gi18n.h>
 #include <glib/gstdio.h>
 #include <stdio.h>
@@ -41,6 +42,9 @@ struct _GrdDaemon
 {
   GApplication parent;
 
+  GSource *sigint_source;
+  GSource *sigterm_source;
+
   GCancellable *cancellable;
   guint remote_desktop_watch_name_id;
   guint screen_cast_watch_name_id;
@@ -369,6 +373,17 @@ grd_daemon_shutdown (GApplication *app)
 
   g_clear_object (&daemon->context);
 
+  if (daemon->sigterm_source)
+    {
+      g_source_destroy (daemon->sigterm_source);
+      g_clear_pointer (&daemon->sigterm_source, g_source_unref);
+    }
+  if (daemon->sigint_source)
+    {
+      g_source_destroy (daemon->sigint_source);
+      g_clear_pointer (&daemon->sigint_source, g_source_unref);
+    }
+
   G_APPLICATION_CLASS (grd_daemon_parent_class)->shutdown (app);
 }
 
@@ -399,6 +414,44 @@ add_actions (GApplication *app)
   g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action));
 }
 
+static gboolean
+sigint_terminate_daemon (gpointer user_data)
+{
+  GrdDaemon *daemon = user_data;
+
+  g_debug ("Received SIGINT signal. Exiting...");
+  g_clear_pointer (&daemon->sigint_source, g_source_unref);
+  g_application_release (G_APPLICATION (daemon));
+
+  return G_SOURCE_REMOVE;
+}
+
+static gboolean
+sigterm_terminate_daemon (gpointer user_data)
+{
+  GrdDaemon *daemon = user_data;
+
+  g_debug ("Received SIGTERM signal. Exiting...");
+  g_clear_pointer (&daemon->sigterm_source, g_source_unref);
+  g_application_release (G_APPLICATION (daemon));
+
+  return G_SOURCE_REMOVE;
+}
+
+static void
+register_signals (GrdDaemon *daemon)
+{
+  daemon->sigint_source = g_unix_signal_source_new (SIGINT);
+  g_source_set_callback (daemon->sigint_source, sigint_terminate_daemon,
+                         daemon, NULL);
+  g_source_attach (daemon->sigint_source, NULL);
+
+  daemon->sigterm_source = g_unix_signal_source_new (SIGTERM);
+  g_source_set_callback (daemon->sigterm_source, sigterm_terminate_daemon,
+                         daemon, NULL);
+  g_source_attach (daemon->sigterm_source, NULL);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -443,6 +496,7 @@ main (int argc, char **argv)
                       NULL);
 
   add_actions (app);
+  register_signals (GRD_DAEMON (app));
 
   settings = grd_context_get_settings (GRD_DAEMON (app)->context);
   if (rdp_port != -1)


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