[libpeas] Better SIGINT handling for python signals module



commit 252e2f4ead0fbf1d2b439f38b718f419e07c803d
Author: Jesse van den Kieboom <jesse vandenkieboom epfl ch>
Date:   Thu Aug 16 17:02:31 2012 +0200

    Better SIGINT handling for python signals module
    
    Even though PyInitialize_Ex does not initialize signal handlers,
    the python signal module does install a handler for SIGINT when
    the handler for SIGINT is set to SIG_DFL on importing the signal
    module.
    
    To avoid applications not handling SIGINT we set a custom SIGINT
    handler (if the current SIGINT handler is SIG_DFL) before
    initializing the python interpreter. This signal handler when
    invoked simply chains to the default SIGINT handler.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=682014

 loaders/python/peas-plugin-loader-python.c |   38 ++++++++++++++++++++++++++++
 1 files changed, 38 insertions(+), 0 deletions(-)
---
diff --git a/loaders/python/peas-plugin-loader-python.c b/loaders/python/peas-plugin-loader-python.c
index 23da7b4..6769ec3 100644
--- a/loaders/python/peas-plugin-loader-python.c
+++ b/loaders/python/peas-plugin-loader-python.c
@@ -348,6 +348,23 @@ peas_wchar_from_str (const gchar *str)
 }
 #endif
 
+#ifdef HAVE_SIGACTION
+static void
+default_sigint (int sig)
+{
+  struct sigaction sigint;
+
+  /* Invoke default sigint handler */
+  sigint.sa_handler = SIG_DFL;
+  sigint.sa_flags = 0;
+  sigemptyset (&sigint.sa_mask);
+
+  sigaction (SIGINT, &sigint, NULL);
+
+  raise (SIGINT);
+}
+#endif
+
 static gboolean
 peas_plugin_loader_python_initialize (PeasPluginLoader *loader)
 {
@@ -368,6 +385,27 @@ peas_plugin_loader_python_initialize (PeasPluginLoader *loader)
   /* Python initialization */
   if (!Py_IsInitialized ())
     {
+#ifdef HAVE_SIGACTION
+      struct sigaction sigint;
+
+      /* We are going to install a signal handler for SIGINT if the current
+         signal handler for sigint is SIG_DFL. We do this because even if
+         Py_InitializeEx will not set the signal handlers, the 'signal' module
+         (which can be used by plugins for various reasons) will install a
+         SIGINT handler when imported, if SIGINT is set to SIG_DFL. Our
+         override will simply call the default SIGINT handler in the end. */
+      sigaction (SIGINT, NULL, &sigint);
+
+      if (sigint.sa_handler == SIG_DFL)
+        {
+          sigemptyset (&sigint.sa_mask);
+          sigint.sa_flags = 0;
+          sigint.sa_handler = default_sigint;
+
+          sigaction (SIGINT, &sigint, NULL);
+        }
+#endif
+
       Py_InitializeEx (FALSE);
       pyloader->priv->must_finalize_python = TRUE;
     }



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