a02bb2
From 32572618cdcc36162b5769eb4c71964db9734061 Mon Sep 17 00:00:00 2001
a02bb2
From: fujiwarat <takao.fujiwara1@gmail.com>
a02bb2
Date: Tue, 6 Aug 2019 18:55:14 +0900
a02bb2
Subject: [PATCH] bus: Exit ibus-daemon with parent's death
a02bb2
a02bb2
ibus-daemon can be restarted unexpectedly during logging out the session
a02bb2
and double ibus-x11 prevent from enabling XIM in XGetSelectionOwner()
a02bb2
for the "ibus" atom.
a02bb2
ibus-daemon always will exit the process when the parent process dies
a02bb2
to avoid this problem.
a02bb2
---
a02bb2
 bus/main.c   | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++-
a02bb2
 configure.ac |  3 +++
a02bb2
 2 files changed, 58 insertions(+), 1 deletion(-)
a02bb2
a02bb2
diff --git a/bus/main.c b/bus/main.c
a02bb2
index 3223d541..e11a5ebd 100644
a02bb2
--- a/bus/main.c
a02bb2
+++ b/bus/main.c
a02bb2
@@ -32,6 +32,10 @@
a02bb2
 #include <sys/stat.h>
a02bb2
 #include <unistd.h>
a02bb2
 
a02bb2
+#ifdef HAVE_SYS_PRCTL_H
a02bb2
+#include <sys/prctl.h>
a02bb2
+#endif
a02bb2
+
a02bb2
 #include "global.h"
a02bb2
 #include "ibusimpl.h"
a02bb2
 #include "server.h"
a02bb2
@@ -164,6 +168,15 @@ _sig_usr2_handler (int sig)
a02bb2
     g_mem_profile ();
a02bb2
 }
a02bb2
 
a02bb2
+#ifdef HAVE_SYS_PRCTL_H
a02bb2
+static void
a02bb2
+_sig_usr1_handler (int sig)
a02bb2
+{
a02bb2
+    g_warning ("The parent process died.");
a02bb2
+    bus_server_quit (FALSE);
a02bb2
+}
a02bb2
+#endif
a02bb2
+
a02bb2
 gint
a02bb2
 main (gint argc, gchar **argv)
a02bb2
 {
a02bb2
@@ -204,7 +217,7 @@ main (gint argc, gchar **argv)
a02bb2
     /* daemonize process */
a02bb2
     if (daemonize) {
a02bb2
         if (daemon (1, 0) != 0) {
a02bb2
-            g_printerr ("Can not daemonize ibus.\n");
a02bb2
+            g_printerr ("Cannot daemonize ibus.\n");
a02bb2
             exit (-1);
a02bb2
         }
a02bb2
     }
a02bb2
@@ -276,6 +289,47 @@ main (gint argc, gchar **argv)
a02bb2
             exit (-1);
a02bb2
     }
a02bb2
 
a02bb2
+    if (!daemonize) {
a02bb2
+        if (getppid () == 1) {
a02bb2
+            g_warning ("The parent process died.");
a02bb2
+            exit (0);
a02bb2
+        }
a02bb2
+#ifdef HAVE_SYS_PRCTL_H
a02bb2
+       /* Currently ibus-x11 detects XIOError and assume the error as the
a02bb2
+        * desktop session is closed and ibus-x11 calls Exit D-Bus method to
a02bb2
+        * exit ibus-daemon. But a few desktop sessions cause XError before
a02bb2
+        * XIOError and GTK does not allow to bind XError by applications and
a02bb2
+        * GTK calls gdk_x_error() with XError.
a02bb2
+        *
a02bb2
+        * E.g. GdkX11Screen calls XGetSelectionOwner() for "_XSETTINGS_S?"
a02bb2
+        * atoms during the logout but the selection owner already becomes
a02bb2
+        * NULL and the NULL window causes XError with
a02bb2
+        * gdk_x11_window_foreign_new_for_display().
a02bb2
+        *
a02bb2
+        * Since ibus-x11 exits with XError before XIOError, gnome-shell
a02bb2
+        * can detects the exit of ibus-daemon a little earlier and
a02bb2
+        * gnome-shell restarts ibus-daemon but gnome-shell dies soon.
a02bb2
+        * Then gnome-shell dies but ibus-daemon is alive, it's a problem.
a02bb2
+        * Because it causes double ibus-x11 of GDM and a login user
a02bb2
+        * and double XSetSelectionOwner() is not allowed for the unique
a02bb2
+        * "ibus" atom and the user cannot use XIM but not GtkIMModule.
a02bb2
+        *
a02bb2
+        * Probably we could fix the ibus process problem if we would fix
a02bb2
+        * XError about the X selection owner or stop to restart ibus-daemon
a02bb2
+        * in gonme-shell when the session is logging out.
a02bb2
+        * Maybe using SessionManager.LogoutRemote() or
a02bb2
+        * global.screen.get_display().get_xdisplay()
a02bb2
+        * But I assume thereare other scenarios to causes the problem.
a02bb2
+        *
a02bb2
+        * And I decided ibus-daemon always exits with the parent's death here
a02bb2
+        * to avoid unexpected ibus restarts during the logout.
a02bb2
+        */
a02bb2
+        if (prctl (PR_SET_PDEATHSIG, SIGUSR1))
a02bb2
+            g_printerr ("Cannot bind SIGUSR1 for parent death\n");
a02bb2
+        else
a02bb2
+            signal (SIGUSR1, _sig_usr1_handler);
a02bb2
+#endif
a02bb2
+    }
a02bb2
     bus_server_run ();
a02bb2
     return 0;
a02bb2
 }
a02bb2
diff --git a/configure.ac b/configure.ac
a02bb2
index f1df3ac1..fdd316a9 100644
a02bb2
--- a/configure.ac
a02bb2
+++ b/configure.ac
a02bb2
@@ -140,6 +140,9 @@ AC_HEADER_STDC
a02bb2
 LT_INIT
a02bb2
 IT_PROG_INTLTOOL([0.35.0])
a02bb2
 
a02bb2
+# Check header filess.
a02bb2
+AC_CHECK_HEADERS([sys/prctl.h])
a02bb2
+
a02bb2
 # Check functions.
a02bb2
 AC_CHECK_FUNCS(daemon)
a02bb2