Blame SOURCES/xsane-0.999-signal-handling.patch

0aa144
From 3b5d3b7e1f320b0bfbe48024a586c0a22375aa2d Mon Sep 17 00:00:00 2001
0aa144
From: Nils Philippsen <nils@redhat.com>
0aa144
Date: Thu, 3 Jul 2014 10:38:03 +0200
0aa144
Subject: [PATCH] patch: signal-handling
0aa144
0aa144
Squashed commit of the following:
0aa144
0aa144
commit 1e9e8cf5edc469114c8eadf46817cd5c1261b35c
0aa144
Author: Nils Philippsen <nils@redhat.com>
0aa144
Date:   Thu Jul 3 10:14:52 2014 +0200
0aa144
0aa144
    don't use g_unix_open_pipe(), g_unix_fd_add()
0aa144
0aa144
    These functions have only recently been added to glib. Use pipe()/
0aa144
    fcntl() and g_io_channel_unix_new()/g_io_add_watch() instead which are
0aa144
    available in the minimum glib version needed for gtk+-2.x.
0aa144
0aa144
commit acbdf3f693d3d2a78ee7490ca1bf76957daf00cf
0aa144
Author: Nils Philippsen <nils@redhat.com>
0aa144
Date:   Thu Mar 13 13:38:12 2014 +0100
0aa144
0aa144
    separate signal handlers in top and bottom half
0aa144
0aa144
    This is to avoid race-conditions occurring when a signal is received
0aa144
    while the signal handler is not yet finished. It also avoids calling
0aa144
    non-reentrant functions from a signal handler. The top half (the real
0aa144
    signal handler) just writes a character into a pipe which gets picked up
0aa144
    and serviced by the bottom half from the normal event loop, this
0aa144
    serializes things and makes using non-reentrant functions safe.
0aa144
---
0aa144
 src/xsane.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------
0aa144
 1 file changed, 136 insertions(+), 15 deletions(-)
0aa144
0aa144
diff --git a/src/xsane.c b/src/xsane.c
0aa144
index 2b9211b..fc2ebbe 100644
0aa144
--- a/src/xsane.c
0aa144
+++ b/src/xsane.c
0aa144
@@ -47,6 +47,7 @@
0aa144
 #endif
0aa144
 
0aa144
 #include <sys/wait.h>
0aa144
+#include <glib-unix.h>
0aa144
 
0aa144
 #include <stdarg.h>
0aa144
 
0aa144
@@ -121,6 +122,7 @@ static const Preferences_medium_t pref_default_medium[]=
0aa144
 
0aa144
 int DBG_LEVEL = 0;
0aa144
 static guint xsane_resolution_timer = 0;
0aa144
+static int xsane_signal_pipe[2];
0aa144
 
0aa144
 /* ---------------------------------------------------------------------------------------------------------------------- */
0aa144
 
0aa144
@@ -161,8 +163,11 @@ void xsane_pref_save(void);
0aa144
 static int xsane_pref_restore(void);
0aa144
 static void xsane_pref_save_media(void);
0aa144
 static void xsane_pref_restore_media(void);
0aa144
-static RETSIGTYPE xsane_quit_handler(int signal);
0aa144
-static RETSIGTYPE xsane_sigchld_handler(int signal);
0aa144
+static RETSIGTYPE xsane_signal_handler_top_half(int signal);
0aa144
+static gboolean xsane_signal_handler_bottom_half(GIOChannel *source,
0aa144
+                                                 GIOCondition condition,
0aa144
+                                                 gpointer user_data);
0aa144
+static void xsane_sigchld_handler(void);
0aa144
 static void xsane_quit(void);
0aa144
 static void xsane_exit(void);
0aa144
 static gint xsane_standard_option_win_delete(GtkWidget *widget, gpointer data);
0aa144
@@ -2296,16 +2301,119 @@ static void xsane_pref_restore_media(void)
0aa144
 
0aa144
 /* ---------------------------------------------------------------------------------------------------------------------- */
0aa144
 
0aa144
-static RETSIGTYPE xsane_quit_handler(int signal)
0aa144
+static RETSIGTYPE xsane_signal_handler_top_half(int signal)
0aa144
 {
0aa144
-  DBG(DBG_proc, "xsane_quit_handler\n");
0aa144
+  const char *msg_func = "xsane_signal_handler_top_half(): ";
0aa144
+  const char *msg_short_write = "Short write() while processing signal.\n";
0aa144
+  const char *msg_err = "Error during write().\n";
0aa144
+  char sig_char;
0aa144
+  ssize_t written;
0aa144
+  int errno_saved = errno;
0aa144
 
0aa144
-  xsane_quit();
0aa144
+  switch (signal)
0aa144
+  {
0aa144
+    case SIGTERM:
0aa144
+      sig_char = 't';
0aa144
+      break;
0aa144
+    case SIGINT:
0aa144
+      sig_char = 'i';
0aa144
+      break;
0aa144
+    case SIGHUP:
0aa144
+      sig_char = 'h';
0aa144
+      break;
0aa144
+    case SIGCHLD:
0aa144
+      sig_char = 'c';
0aa144
+      break;
0aa144
+    default:
0aa144
+      sig_char = '?';
0aa144
+      break;
0aa144
+  }
0aa144
+
0aa144
+  if ((written = write(xsane_signal_pipe[1], &sig_char, 1)) <= 0)
0aa144
+  {
0aa144
+    /* At this point, all bets are off. Salvage what we can. */
0aa144
+
0aa144
+    const char *msg = (written == 0) ? msg_short_write : msg_err;
0aa144
+
0aa144
+    if ((write(STDERR_FILENO, msg_func, strlen(msg_func)) < 0) ||
0aa144
+        (write(STDERR_FILENO, msg, strlen(msg)) < 0))
0aa144
+    {
0aa144
+      /* This is really a no-op, but at this point it doesn't really matter
0aa144
+       * anymore if the writes succeeded or not. */
0aa144
+      goto bail_out;
0aa144
+    }
0aa144
+
0aa144
+bail_out:
0aa144
+    /* Ignore SIGCHLD errors, zombie processes don't hurt that much. */
0aa144
+    if (signal != SIGCHLD)
0aa144
+    {
0aa144
+      struct SIGACTION act;
0aa144
+      memset(&act, 0, sizeof(act));
0aa144
+      act.sa_handler = SIG_DFL;
0aa144
+      sigaction(signal, &act, NULL);
0aa144
+      raise(signal);
0aa144
+    }
0aa144
+  }
0aa144
+
0aa144
+  errno = errno_saved;
0aa144
+}
0aa144
+
0aa144
+static gboolean xsane_signal_handler_bottom_half(GIOChannel *source,
0aa144
+                                                 GIOCondition condition,
0aa144
+                                                 gpointer user_data)
0aa144
+{
0aa144
+  char sig_char;
0aa144
+  ssize_t readlen;
0aa144
+
0aa144
+  DBG(DBG_proc, "xsane_signal_handler_bottom_half\n");
0aa144
+
0aa144
+  while ((readlen = read(xsane_signal_pipe[0], &sig_char, 1)) != 0)
0aa144
+  {
0aa144
+    if (readlen < 0)
0aa144
+    {
0aa144
+      if (errno == EINTR)
0aa144
+      {
0aa144
+        /* if interrupted by signal, just repeat reading */
0aa144
+        continue;
0aa144
+      }
0aa144
+      else
0aa144
+      {
0aa144
+        break;
0aa144
+      }
0aa144
+    }
0aa144
+
0aa144
+    switch (sig_char)
0aa144
+    {
0aa144
+      case 't':
0aa144
+      case 'i':
0aa144
+      case 'h':
0aa144
+        xsane_quit();
0aa144
+        break;
0aa144
+      case 'c':
0aa144
+        xsane_sigchld_handler();
0aa144
+        break;
0aa144
+      default:
0aa144
+        DBG(DBG_error,
0aa144
+            "Don't know how to cope with character-encoded signal: '%c'\n",
0aa144
+            sig_char);
0aa144
+        break;
0aa144
+    }
0aa144
+  }
0aa144
+
0aa144
+  /* previous invocation might have read more than it should, so ignore
0aa144
+   * EAGAIN/EWOULDBLOCK */
0aa144
+  if (readlen < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
0aa144
+  {
0aa144
+    DBG(DBG_error, "Error while reading from pipe: %d '%s'\n", errno,
0aa144
+        strerror(errno));
0aa144
+  }
0aa144
+
0aa144
+  return TRUE;
0aa144
 }
0aa144
 
0aa144
 /* ---------------------------------------------------------------------------------------------------------------------- */
0aa144
 
0aa144
-static RETSIGTYPE xsane_sigchld_handler(int signal)
0aa144
+static void xsane_sigchld_handler(void)
0aa144
 {
0aa144
  int status;
0aa144
  XsaneChildprocess **childprocess_listptr = &xsane.childprocess_list;
0aa144
@@ -6026,6 +6134,8 @@ void xsane_interface(int argc, char **argv)
0aa144
 {
0aa144
  struct SIGACTION act;
0aa144
 
0aa144
+  GIOChannel *gio_pipe_read;
0aa144
+
0aa144
   DBG(DBG_proc, "xsane_interface\n");
0aa144
 
0aa144
   xsane.info_label = NULL;
0aa144
@@ -6069,18 +6179,29 @@ void xsane_interface(int argc, char **argv)
0aa144
     }
0aa144
   }
0aa144
 
0aa144
+  if ((pipe(xsane_signal_pipe) == -1) ||
0aa144
+      (fcntl(xsane_signal_pipe[0], F_SETFD, FD_CLOEXEC) == -1) ||
0aa144
+      (fcntl(xsane_signal_pipe[0], F_SETFL, O_NONBLOCK) == -1) ||
0aa144
+      (fcntl(xsane_signal_pipe[1], F_SETFD, FD_CLOEXEC) == -1) ||
0aa144
+      (fcntl(xsane_signal_pipe[1], F_SETFL, O_NONBLOCK) == -1) ||
0aa144
+      !(gio_pipe_read = g_io_channel_unix_new(xsane_signal_pipe[0])) ||
0aa144
+      !g_io_add_watch(gio_pipe_read, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
0aa144
+          xsane_signal_handler_bottom_half, NULL))
0aa144
+  {
0aa144
+    DBG(DBG_error,
0aa144
+        "Couldn't create signal handling pipe, set flags on it or install\n"
0aa144
+        "bottom half of handler.\n");
0aa144
+    exit(1);
0aa144
+  }
0aa144
+
0aa144
   /* define SIGTERM, SIGINT, SIGHUP-handler to make sure that e.g. all temporary files are deleted */
0aa144
   /* when xsane gets such a signal */
0aa144
   memset(&act, 0, sizeof(act));
0aa144
-  act.sa_handler = xsane_quit_handler;
0aa144
-  sigaction(SIGTERM, &act, 0);
0aa144
-  sigaction(SIGINT,  &act, 0);
0aa144
-  sigaction(SIGHUP,  &act, 0);
0aa144
-
0aa144
-  /* add a signal handler that cleans up zombie child processes */
0aa144
-  memset(&act, 0, sizeof(act));
0aa144
-  act.sa_handler = xsane_sigchld_handler;
0aa144
-  sigaction(SIGCHLD, &act, 0);
0aa144
+  act.sa_handler = xsane_signal_handler_top_half;
0aa144
+  sigaction(SIGTERM, &act, NULL);
0aa144
+  sigaction(SIGINT,  &act, NULL);
0aa144
+  sigaction(SIGHUP,  &act, NULL);
0aa144
+  sigaction(SIGCHLD, &act, NULL);
0aa144
 
0aa144
   gtk_main();
0aa144
   sane_exit();
0aa144
-- 
0aa144
1.9.3
0aa144